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

📄 vpnconfig.cpp

📁 VPN拨号程序源码(from sourceforge.net)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// VPNConfig.cpp: implementation of the CVPNConfig class.
//
//////////////////////////////////////////////////////////////////////

/*******************************************************************************
 *                                                                             *
 *  This file is part of VPNDialer.                                            *
 *                                                                             *
 *  VPNDialer is free software; you can redistribute it and/or modify          *
 *  it under the terms of the GNU General Public License as published by       *
 *  the Free Software Foundation; either version 2 of the License, or          *
 *  (at your option) any later version.                                        *
 *                                                                             *
 *  VPNDialer is distributed in the hope that it will be useful,               *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 *  GNU General Public License for more details.                               *
 *                                                                             *
 *  You should have received a copy of the GNU General Public License          *
 *  along with VPNDialer; if not, write to the Free Software                   *
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  *
 *                                                                             *
 *  Copyright (c) 2003 Evidian GmbH, written by Thomas Kriener                 *
 *                                                                             *
 *******************************************************************************/

#include "stdafx.h"
#include "VPNDialer.h"
#include "VPNConfig.h"
#include "VPNSession.h"
#include "SelectInterfaceDialog.h"

#include <conio.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define bzero(a) memset(a,0,sizeof(a)) //easier -- shortcut

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVPNConfig::CVPNConfig()
{
	HKEY    hKey;
	DWORD   valueSize;
	DWORD   valueType;
	LONG    regRetVal;
	char*   value;
	int     intValue;
	int     i=0;
	CString key;

	do
	{
		CVPNSession actSession;
		key.Format("SOFTWARE\\Evidian\\VPNDialer\\Session%i",i);
		// get Registry-Key
		regRetVal=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
		                       key,
						       0,
						       KEY_QUERY_VALUE,
						       &hKey);
		if(regRetVal==ERROR_SUCCESS)
		{
			// Name of Connection
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "Name",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "Name",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_name=value;
				}
				delete value;
			}
			// Gateway
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "Gateway",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "Gateway",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_gateway=value;
				}
				delete value;
			}
			// Network
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "Network",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "Network",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_network=value;
				}
				delete value;
			}
			// Netmask
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "Netmask",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "Netmask",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_netmask=value;
				}
				delete value;
			}
			// AuthType
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "AuthType",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_authType=intValue;
			}
			// AuthValue
			valueType=REG_MULTI_SZ;
			if(RegQueryValueEx(hKey,
			                   "AuthValue",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "AuthValue",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					for(DWORD j=0; j<valueSize; j++)
					{
						if((value[j]=='\0') && (j!=(valueSize-1)))
							value[j]='\n';
					}
					actSession.m_authValue=value;
				}
				delete value;
			}
			// IpsecMode
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecMode",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecMode;
			}
			// IpsecEncryption
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecEncryption",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecEncryption=intValue;
			}
			// IpsecAuth
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecAuth",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecAuth=intValue;
			}
			// IpsecPFS
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecPFS",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecPFS=intValue;
			}
			// IpsecRekeyData
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecRekeyData",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecRekeyData=intValue;
			}
			// IpsecRekeyTime
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "IpsecRekeyTime",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_ipsecRekeyTime=intValue;
			}
			// RAS
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "RAS",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "RAS",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_ras=value;
				}
				delete value;
			}
			// L2TP
			valueType=REG_SZ;
			if(RegQueryValueEx(hKey,
			                   "L2TP",
							   NULL,
							   &valueType,
							   NULL,
							   &valueSize)==ERROR_SUCCESS)
			{
				value=new char[valueSize];
				if(RegQueryValueEx(hKey,
				                   "L2TP",
								   NULL,
								   &valueType,
								   (LPBYTE)value,
								   &valueSize)==ERROR_SUCCESS)
				{
					actSession.m_l2tp=value;
				}
				delete value;
			}
			// BlockNonIPSec
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "BlockNonIPSec",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_blockNonIPSec=intValue;
			}
			// AllowLocalTraffic
			valueType=REG_DWORD;
			valueSize=sizeof(intValue);
			if(RegQueryValueEx(hKey,
			                   "AllowLocalTraffic",
							   NULL,
							   &valueType,
							   (LPBYTE)&intValue,
							   &valueSize)==ERROR_SUCCESS)
			{
				actSession.m_allowLocalTraffic=intValue;
			}


			// Add Session to Array
			if(actSession.m_name!="")
				m_vpnSessions.Add(actSession);

			// Close Session-Key
			RegCloseKey(hKey);
		}

		// Try next Session
		i++;
	} while (regRetVal==ERROR_SUCCESS);


}

CVPNConfig::~CVPNConfig()
{

}

int CVPNConfig::SaveConfig()
{
	HKEY    hKey;
	DWORD   valueSize;
	DWORD   valueType;
	CString key;
	LONG    regRetVal;
	char*   value;
	int     intValue;
	int     i;

	for(i=0;i<m_vpnSessions.GetSize();i++)
	{
		CVPNSession actSession=m_vpnSessions[i];
		key.Format("SOFTWARE\\Evidian\\VPNDialer\\Session%i",i);
		// open Key with write-rights
		if(RegCreateKeyEx(HKEY_LOCAL_MACHINE,
		                  key,
						  0,
						  "CLASS",
						  REG_OPTION_NON_VOLATILE,
						  KEY_WRITE,
						  NULL,
						  &hKey,
						  NULL)==ERROR_SUCCESS)
		{
			// Name
			valueSize=actSession.m_name.GetLength()+1;
			valueType=REG_SZ;
			if(RegSetValueEx(hKey,
			                 "Name",
							 NULL,
							 valueType,
							 (LPBYTE)(LPCSTR)actSession.m_name,
							 valueSize)!=ERROR_SUCCESS)
			{
				return 1;
			}

			// Gateway
			valueSize=actSession.m_gateway.GetLength()+1;
			valueType=REG_SZ;
			if(RegSetValueEx(hKey,
			                 "Gateway",
							 NULL,
							 valueType,
							 (LPBYTE)(LPCSTR)actSession.m_gateway,
							 valueSize)!=ERROR_SUCCESS)
			{
				return 1;
			}
			// Network
			valueSize=actSession.m_network.GetLength()+1;
			valueType=REG_SZ;
			if(RegSetValueEx(hKey,
			                 "Network",
							 NULL,
							 valueType,
							 (LPBYTE)(LPCSTR)actSession.m_network,
							 valueSize)!=ERROR_SUCCESS)
			{
				return 1;
			}
			// Netmask
			valueSize=actSession.m_netmask.GetLength()+1;
			valueType=REG_SZ;
			if(RegSetValueEx(hKey,
			                 "Netmask",
							 NULL,
							 valueType,
							 (LPBYTE)(LPCSTR)actSession.m_netmask,
							 valueSize)!=ERROR_SUCCESS)
			{
				return 1;
			}
			// AuthType

⌨️ 快捷键说明

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