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

📄 at_queryras.cpp

📁 Query Ras Connection
💻 CPP
字号:
#include "StdAfx.h"

#include "AT_QueryRas.h"

AT_QueryRas::AT_QueryRas(void)
: m_bA(false)
, m_bB(false)
, m_bX(false)
, m_bS(false)
, m_bQ(false)
{
}

AT_QueryRas::~AT_QueryRas(void)
{
}

CString AT_QueryRas::Init(CString strArgs)
{
    strArgs = strArgs.MakeUpper();
    int length = strArgs.GetLength();
    while(length)
    {
        switch(strArgs.GetAt(length-1))
        {
        case 'A':
            m_bA = true;
            break;
        case 'B':
            m_bB = true;
            break;
        case 'X':
            m_bX = true;
            break;
        case 'S':
            m_bS = true;
            break;
        case '?':
            m_bQ = true;
            break;
        }
        length--;
    }

    CString result = "";
    if( m_bS && m_bQ )
        return "ERROR: Cannot query servers and connection status simulatenously.";
    
    else if( !m_bS && !m_bQ )
        return "ERROR: Must specify S or ? to indicate action.";

	EnumVPNEntries();

	if(m_bS)
	{
		CString f = "";
		CString sa = "";
		CString sb = "";
		CString sx = "";
		for(int i=0;i<re.GetCount();i++) 
		{
			CString c = re[i];
			if(GetConnectionType(c)=="A")
				sa=sa+GetConnectionServer(c)+",";
			if(GetConnectionType(c)=="B")
				sb=sb+GetConnectionServer(c)+",";
			if(GetConnectionType(c)=="X")
				sx=sx+GetConnectionServer(c)+",";
		}
		if(m_bA) f+=sa;
		if(m_bB) f+=sb;
		if(m_bX) f+=sx;
		while(f.Find(",,"))
			f.Replace(",,",",");
		f.Truncate(f.GetLength()-1);
		return f;
	}
	else if(m_bQ)
	{
		return "Process connection query";
	}
	else
		return "ERROR: Unexpectedly fell through if-else chain.";



}


bool AT_QueryRas::EnumVPNEntries(void)
{
	DWORD dwCb = sizeof(RASENTRYNAME);
	DWORD dwErr = ERROR_SUCCESS;
	DWORD dwRetries = 5;
	DWORD dwEntries = 0;
	RASENTRYNAME* lpRasEntryName = NULL;

	// Loop through in case the information from RAS changes between calls.
	while( dwRetries-- )
	{
		// If the memory is allocated, free it.
		if( NULL != lpRasEntryName )
		{
			HeapFree( GetProcessHeap(), 0, lpRasEntryName );
			lpRasEntryName = NULL;
		}

		// Allocate the size need for the RAS structure.
		lpRasEntryName = static_cast<RASENTRYNAME*>( HeapAlloc( GetProcessHeap(), 0, dwCb ) );
		if( NULL == lpRasEntryName )
		{
			dwErr = ERROR_NOT_ENOUGH_MEMORY;
			break;
		}
		// Set the structure size for version checking purposes.
		lpRasEntryName->dwSize = sizeof(RASENTRYNAME);
		// Call the RAS API, bail on the loop if we are successful or an unknown
		// error occurs.

		// Enumerate RAS Entries which are present on the system
		dwErr = RasEnumEntries( NULL, NULL, lpRasEntryName, &dwCb, &dwEntries );
		if( ERROR_BUFFER_TOO_SMALL != dwErr )
		{
			break;
		}
	}
	std::cout << (int)dwEntries << " entries." << std::endl;

	se = "";
	for( int nIndex = 0; nIndex < (int)dwEntries; nIndex++ )
	{
		RASENTRYNAME* pRasEntryName = (lpRasEntryName + nIndex);

		re.Add(pRasEntryName->szEntryName);
		
		if( !strcmp( pRasEntryName->szEntryName, "" ) )
			continue;

		if( GetConnectionType( *pRasEntryName )!="" )
		{
			std::cout << pRasEntryName->szEntryName << std::endl;
        }
	}

	// Free the memory if necessary.
	if (NULL != lpRasEntryName)
	{
		HeapFree(GetProcessHeap(), 0, lpRasEntryName);
		lpRasEntryName = NULL;
	}

	return true;
}


CString AT_QueryRas::GetConnectionType(RASENTRYNAME& rRasEntryName)
{
	RASENTRY		RasEntry;
	RASDIALPARAMS	RasDialParam;
	HRASCONN		hRasConn = NULL;

	DWORD			dwErr = ERROR_SUCCESS;
	DWORD			dwEntryInfoSize;
	BOOL			bPasswd = FALSE;

	CString strEntryName( rRasEntryName.szEntryName );
	CString strObjectID;

	CString m_strPhoneBook="";
	CString m_strEntryName="";
	RASENTRY m_pRasEntry;
	RASDIALPARAMS m_pRasDialParams;
	

	strEntryName.MakeReverse();
	strEntryName = strEntryName.Left( 7 );
	strObjectID = strEntryName.Left( 1 );
	strEntryName.MakeReverse();
	strEntryName = strEntryName.Left( 6 );
	CString strReturnString;

	// check if it belongs to the VPN Dialer
	if(strEntryName.CompareNoCase("AUTO-A")==0) strReturnString = "A";
	else if(strEntryName.CompareNoCase("AUTO-B")==0) strReturnString = "B";
	else strReturnString = "X";

	// go ahead with remaining pocesses
	ZeroMemory( &RasEntry, sizeof(RASENTRY) );
	RasEntry.dwSize = sizeof(RASENTRY);
	ZeroMemory( &RasDialParam, sizeof(RASDIALPARAMS) );
	RasDialParam.dwSize = sizeof(RASDIALPARAMS);

	strcpy_s( RasDialParam.szEntryName, sizeof(RasDialParam.szEntryName), rRasEntryName.szEntryName );

	// Obtain the Buffer Size
	// return value is ignored here
	dwErr = RasGetEntryProperties( NULL, rRasEntryName.szEntryName, 
		NULL, &dwEntryInfoSize, NULL, NULL );

	// Get the property of each Entries
	dwErr = RasGetEntryProperties( rRasEntryName.szPhonebookPath,
		rRasEntryName.szEntryName, &RasEntry, &dwEntryInfoSize, NULL, NULL );

	if( ERROR_SUCCESS == dwErr )
	{
		// Get the Parameters of RAS Entries
		dwErr = RasGetEntryDialParams( rRasEntryName.szPhonebookPath, &RasDialParam, &bPasswd );

		if( ERROR_SUCCESS == dwErr 
			&& ( 3 == strlen(RasEntry.szDeviceType) ) 
			&& ( !_strnicmp( RasEntry.szDeviceType, _T("vpn"), 3 ) ) )
		{
			// populate all the parameters
			
			/* // This section must be recent and full of errors.
			strcpy_s( m_strPhoneBook, rRasEntryName.szPhonebookPath );
			strcpy_s( m_strEntryName, rRasEntryName.szEntryName );

			m_pRasEntry = new RASENTRY;
			memcpy( m_pRasEntry, &RasEntry, sizeof(RASENTRY) );

			m_pRasDialParams = new RASDIALPARAMS;
			memcpy( m_pRasDialParams, &RasDialParam, sizeof(RASDIALPARAMS) );
			*/
		}
		else
		{
			// post message
			//m_pApp->PostMsgToLog( GS_LOG_DETAILED, "\"%s\" : Is not a valid VPN connection;", rRasEntryName.szEntryName );
			return "";
		}
	}
	else
	{
		// post message
		//m_pApp->PostMsgToLog( GS_LOG_DETAILED, "Failed to get the properties of \"%s\" VPN connection;", rRasEntryName.szEntryName );
		return "";
	}
    return strReturnString;
}

CString AT_QueryRas::GetConnectionServer(RASENTRYNAME& rRasEntryName)
{
	return "server.domain.tld";
}

⌨️ 快捷键说明

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