⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vpndialerdlg.cpp

📁 VPN拨号程序源码(from sourceforge.net)
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	// Get Calling Dialog
	dlg=(CVPNDialerDlg*)pParam;

	// Get Current Connection
	sessionNr=dlg->m_connectionCtrl.GetCurSel();
	actSession=dlg->m_vpnConfig.m_vpnSessions[sessionNr];

	dlg->m_log="Opening Connection \""+actSession.m_name;
	dlg->m_log+="\"\r\n";
	AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);

	/*
	 * Dial if RAS
	 */
	if(actSession.m_ras!="")
	{
		dlg->m_log+="Dialing RAS-Connection \""+actSession.m_ras;
		dlg->m_log+="\"\r\n";
		AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);

		switch(dlg->m_rasSession.Dial(actSession.m_ras))
		{
		case UTE_SUCCESS: // successful dialup
			// Just wait for IP-Address
			i=0;
			do
			{
				my_ip=dlg->m_rasSession.GetIPAddress();
				if(my_ip=="")
					Sleep(5);
				i++;
				// Prevent endless loop
				if(i>1000)
				{
					dlg->m_log+="Unable to get IP from RAS-Connection\r\n";
					AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
					AfxMessageBox("Unable to get IP from RAS-Connection",MB_OK|MB_ICONERROR);
					goto cleanup;
				}
			}
			while(my_ip=="");
#ifdef _DEBUG
			dlg->MessageBox(my_ip,"IP of your RAS-Connection");
#endif
			dlg->m_log+="You got IP: "+my_ip;
			dlg->m_log+="\r\n";
			AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
			break;
		case UTE_RAS_DIALINIT_ERROR: // error when starting the dial process (use GetLastRASError for more details)
			tmp.Format("error when starting the dial process : %i\r\n%s",dlg->m_rasSession.GetLastRASError(),dlg->m_rasSession.GetRASErrorString(dlg->m_rasSession.GetLastRASError()));
			dlg->m_log+=tmp+"\r\n";
			AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
			goto cleanup;
			break;
        case UTE_RAS_DIAL_ERROR: // error during the dial process. Use the GetDialState function to get more detailed information.
			dialState=dlg->m_rasSession.GetDialState();
			tmp.Format("Error during Dial process in state %i: %i\r\n%s",LOWORD(dialState),HIWORD(dialState),dlg->m_rasSession.GetRASErrorString(HIWORD(dialState)));
			AfxMessageBox(tmp,MB_OK|MB_ICONERROR);
			dlg->m_log+=tmp+"\r\n";
			AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
			goto cleanup;
			break;
		case UTE_RAS_LOAD_ERROR: // unable to load the RAS DLLs
			AfxMessageBox("unable to load the RAS DLLs");
			dlg->m_log+="unable to load the RAS DLLs\r\n";
			AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
			goto cleanup;
			break;
		}
	}

	/*
	 * Configure IPSec
	 */

	// Get IPSec-Command
#ifdef _DEBUG
	ipsectool=dlg->m_vpnConfig.GetIPSecTool();
	dlg->MessageBox(ipsectool,"Your ipsectool");
#endif
	dlg->m_log+="Configuring IPSec-Policy\r\n";
	AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);

	if(dlg->m_vpnConfig.ActivateIPSec(sessionNr, my_ip, dlg->m_log)!=0)
	{
		AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
		goto cleanup;
	}
	AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);

	/*
	 * Send a packet to the other side to initiate the IPSEC-Tunnel
	 */
	testIp=actSession.GetFirstVPNIP();
	tmp.Format("UDP-Echo sent to %s\r\n",testIp);
	dlg->m_vpnConfig.SendUDPEcho(testIp);
	dlg->m_log+=tmp; AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
	Sleep(100);
	dlg->m_vpnConfig.SendUDPEcho(testIp);
	dlg->m_log+=tmp; AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
	Sleep(3000);

	/*
	 * Dial if L2TP
	 */
	if(actSession.m_l2tp!="")
	{
		dlg->m_log+="Connecting L2TP-Tunnel \""+actSession.m_l2tp;
		dlg->m_log+="\"\r\n";
		AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);

		exitDialLoop=false;
		dialTry=0;
		do
		{
			tmp.Format("Try %i of %i\r\n",dialTry+1,20);
			dlg->m_log+=tmp;
			AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
			//CString name="kriener";
			//CString pass="letmein";
			//CString server="129.184.210.8";
			//switch(dlg->m_l2tpSession.Dial(actSession.m_l2tp,name,pass,server))
			switch(dlg->m_l2tpSession.L2TPDial(actSession.m_l2tp))
			{
			case UTE_SUCCESS: // successful dialup
				// Just wait for IP-Address
				i=0;
				do
				{
					my_l2tpip=dlg->m_l2tpSession.GetIPAddress();
					if(my_l2tpip=="")
						Sleep(5);
					i++;
					// Prevent endless loop
					if(i>1000)
					{
						dlg->m_log+="Unable to get IP from L2TP-Connection\r\n";
						AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
						AfxMessageBox("Unable to get IP from RAS-Connection",MB_OK|MB_ICONERROR);
						goto cleanup;
					}
				}
				while(my_l2tpip=="");
				dlg->m_log+="You got IP:"+my_l2tpip;
				dlg->m_log+="\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				break;
			case UTE_RAS_DIALINIT_ERROR: // error when starting the dial process (use GetLastRASError for more details)
				tmp.Format("error when starting the dial process : %i\r\n%s",dlg->m_l2tpSession.GetLastRASError(),dlg->m_l2tpSession.GetRASErrorString(dlg->m_l2tpSession.GetLastRASError()));
				dlg->m_log+=tmp+"\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				if(!((dlg->m_l2tpSession.GetLastRASError()==617)||(dlg->m_l2tpSession.GetLastRASError()==668)))
				{
					AfxMessageBox(tmp);
					exitDialLoop=true;
				}
				break;
	       case UTE_RAS_DIAL_ERROR: // error during the dial process. Use the GetDialState function to get more detailed information.
				dialState=dlg->m_l2tpSession.GetDialState();
				tmp.Format("Error during Dial process in state %i: %i\r\n%s",LOWORD(dialState),HIWORD(dialState),dlg->m_l2tpSession.GetRASErrorString(HIWORD(dialState)));
				dlg->m_log+=tmp+"\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				if(!((HIWORD(dialState)==737)||(HIWORD(dialState)==769)))
				// 737: Loopback detected, 769: Host unreachable
				{
					AfxMessageBox(tmp);
					exitDialLoop=true;
				}
				break;
			case UTE_RAS_LOAD_ERROR: // unable to load the RAS DLLs
				AfxMessageBox("unable to load the RAS DLLs");
				dlg->m_log+="unable to load the RAS DLLs\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				exitDialLoop=true;
				break;
			}
			dialTry++;
			if(dialTry>20)
				exitDialLoop=true;
			if(!dlg->m_l2tpSession.IsConnected())
				Sleep(100);
		} while((!exitDialLoop) && (!dlg->m_l2tpSession.IsConnected()));

		if(!dlg->m_l2tpSession.IsConnected())
			goto cleanup;
	}

	dlg->m_connectionCtrl.EnableWindow(FALSE);
	dlg->m_buttonConnect.EnableWindow(FALSE);
	dlg->m_buttonDisconnect.EnableWindow(TRUE);
	dlg->m_buttonCancel.EnableWindow(TRUE);
	dlg->m_online=true;


	dlg->EndWaitCursor();
	dlg->ShowWindow(SW_MINIMIZE);

	/*
	 * Check Sessions
	 */
	while(dlg->m_online)
	{
		if(actSession.m_ras!="")
		{
			if(!dlg->m_rasSession.IsConnected())
			{
				dlg->m_log+="RAS-Connection disconnected by failure\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				dlg->ShowWindow(SW_RESTORE);
				dlg->OnButtonDisconnect();
			}
		}
		if(actSession.m_l2tp!="")
		{
			if(!dlg->m_l2tpSession.IsConnected())
			{
				dlg->m_log+="L2TP-Connection disconnected by failure\r\n";
				AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
				dlg->ShowWindow(SW_RESTORE);
				dlg->OnButtonDisconnect();
			}
		}
		Sleep(100);
	}

	return 0;

cleanup:
	if(dlg->m_l2tpSession.IsConnected())
		dlg->m_l2tpSession.HangUp();
	if(dlg->m_rasSession.IsConnected())
		dlg->m_rasSession.HangUp();
	dlg->m_vpnConfig.RemoveIPSec();

	dlg->m_connectionCtrl.EnableWindow(TRUE);
	dlg->m_buttonConnect.EnableWindow(TRUE);
	dlg->m_buttonCancel.EnableWindow(TRUE);
	
	dlg->EndWaitCursor();

	return 0;
}


LRESULT CVPNDialerDlg::OnUpdateDisplay(WPARAM wParam, LPARAM lParam)
{
	UpdateData(FALSE);
	Invalidate();
	UpdateWindow();
	BeginWaitCursor(); // display the hourglass cursor
	SendDlgItemMessage(IDC_LOG, EM_LINESCROLL, 0, 10000);
	return (LRESULT)0;
}

void CVPNDialerDlg::OnCancel() 
{
	// Make shure we disconnect first
	if(m_online)
		OnButtonDisconnect();
	
	CDialog::OnCancel();
}


void CVPNDialerDlg::OnButtonStatus() 
{
	CString tmp;

	m_log+="=============IPSec-Status===============\r\n";

	if(m_vpnConfig.GetIPSecStatus(tmp)<0)
	{
		AfxMessageBox("Error getting Status!",MB_OK|MB_ICONERROR);
		m_log+="Error getting Status!\r\n";
	}
	else
	{
		m_log+=tmp;
	}

	m_log+="========================================\r\n";

	AfxGetMainWnd()->SendMessage(MYWM_UPDATEDISPLAY);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -