Click here to Skip to main content
15,891,739 members
Articles / Desktop Programming / MFC

CRRAS v.1.1 for Routing and RAS (RRAS) Server under Windows 2000

Rate me:
Please Sign up or sign in to vote.
4.20/5 (2 votes)
30 Jun 2003CPOL2 min read 62.7K   966   14  
Connect, query and terminate connections on a Windows 2000 RRAS Server
//
//	MODULE:   CRRas
//
//	AUTHOR: Gavin Lyons
//
//
//	Date: 04/26/2002
//
//	Version 1.1
// 

#include "stdafx.h"
#undef WINVER // Windows 2000 Only
#define WINVER 0x0500 // Windows 2000 Only
#include "D:\Program Files\Microsoft SDK\include\mprapi.h" // RRAS
// YOU MUST LINK IN Mprapi.Lib for the latest Microsoft SDK
#include "CRRas.h"


BOOL CRRas::connect(CString ComputerName)
{
	HANDLE hConnEvent;
	DWORD hOpenRRAS;
	handle=NULL;
	hConnEvent = CreateEvent(NULL,TRUE,FALSE,TEXT("RAS Connection"));
	if (hConnEvent) 
		hOpenRRAS=MprAdminServerConnect((LPWSTR)ComputerName.AllocSysString(), &handle );
	if (hOpenRRAS==NO_ERROR) 
		return TRUE;
	else
		return FALSE;
};

BOOL CRRas::list()
{	/* See MSDN for more details
	http://msdn.microsoft.com/library/default.asp?url=/library/en-us/rras/rasadmin_1831.asp
	*/
	// RAS_CONNECTION_0 & RAS_CONNECTION_2 & Ports
	LPBYTE lpRASinfo=NULL,lpRASConnection2=NULL, lpRASinfo2=NULL, lpPortinfo=NULL;
	DWORD lpdwEntriesRead,lpdwTotalEntries, hRRasInfo, hRRasInfoPort,lpdwResumeHandle=NULL;
	DWORD hRRasInfo2, lpdwEntriesRead2, lpdwTotalEntries2, lpdwResumeHandle2=NULL;
	DWORD lpdwPortEntriesRead,lpdwPortTotalEntries;
	int i, c;
	do {
		hRRasInfo= MprAdminConnectionEnum(handle, NULL, &lpRASinfo, -1, &lpdwEntriesRead, &lpdwTotalEntries, &lpdwResumeHandle);
		hRRasInfo2= MprAdminConnectionEnum(handle, 2, &lpRASConnection2, -1, &lpdwEntriesRead2, &lpdwTotalEntries2, &lpdwResumeHandle2);
		if (((hRRasInfo==NO_ERROR) || (hRRasInfo==ERROR_MORE_DATA)) && 
			((hRRasInfo2==NO_ERROR) || (hRRasInfo2==ERROR_MORE_DATA)))
		{	
			RAS_CONNECTION_0 *pRas = (RAS_CONNECTION_0 *)lpRASinfo;
			RAS_CONNECTION_2 *pRas2 = (RAS_CONNECTION_2 *)lpRASConnection2;
			for (i=0; i<(int)lpdwEntriesRead; i++)
			{ 
				client[i].username=pRas[i].wszUserName;
				client[i].username.MakeUpper();
				client[i].domain=pRas[i].wszLogonDomain;
				client[i].Interface=pRas[i].wszInterfaceName;
				client[i].computername=pRas[i].wszRemoteComputer;
				client[i].computername.MakeUpper();
				client[i].remoteip=pRas2[i].PppInfo2.ip.wszRemoteAddress;
				// Port Information Enumation
				hRRasInfoPort= MprAdminPortEnum(handle, NULL, pRas[i].hConnection, &lpPortinfo, -1, &lpdwPortEntriesRead, &lpdwPortTotalEntries, 0);
				if (hRRasInfoPort==NO_ERROR)
				{
					RAS_PORT_0 *pRasPort = (RAS_PORT_0 *)lpPortinfo;
					for (c=0; c<(int)lpdwPortEntriesRead; c++)
					{ 
						client[i].porthandle=pRasPort[c].hPort;
						client[i].portname=pRasPort[c].wszPortName;
						client[i].mediatype=pRasPort[c].wszDeviceType;
						client[i].connectionperiod=pRasPort[c].dwConnectDuration;
					}
					MprAdminBufferFree(&lpPortinfo); 
				}
			}
			MprAdminBufferFree(&lpRASinfo);
			MprAdminBufferFree(&pRas);
			MprAdminBufferFree(&pRas2);
			connectedclients=(int)lpdwEntriesRead;
		}
		else 
		{
			return FALSE;// Error Querying RRAS Server;
		}
		if ((hRRasInfo==NO_ERROR) &&(hRRasInfo2==NO_ERROR))
			return TRUE;
	} while (hRRasInfo==ERROR_MORE_DATA || hRRasInfo2==ERROR_MORE_DATA);
	if ((hRRasInfo==NO_ERROR) &&(hRRasInfo2==NO_ERROR))
			return TRUE;
	else
			return FALSE;
}

void CRRas::disconnect(void)
{
	MprAdminServerDisconnect(&handle); 
}


void CRRas::terminate(HANDLE hPort)
{
	MprAdminPortDisconnect(handle, hPort);
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Austria Austria
Programmer, Cook, Photographer I guess that's me.

http://www.i386.com

http://blog.glyons.at

Comments and Discussions