Connection Management

Connection Management

RAS has two useful functions that allow you to retrieve the properties of connections established on your system: RasEnumConnections and RasGetProjectionInfo. RasEnumConnections can retrieve all the available active RAS connections on your system. This is useful when you need to obtain connection specific information about a RAS connection on your system using RasGetProjectionInfo, as you will see later in this chapter. The RasEnumConnections is defined as

DWORD RasEnumConnections(     LPRASCONN lprasconn,     LPDWORD lpcb,      LPDWORD lpcConnections );

The lprasconn parameter is an application buffer that will receive an array of RASCONN structures. A RASCONN structure is defined as

typedef struct _RASCONN  {      DWORD dwSize;      HRASCONN hrasconn;      TCHAR szEntryName[RAS_MaxEntryName + 1];  #if (WINVER >= 0x400)      TCHAR szDeviceType[RAS_MaxDeviceType + 1];      TCHAR szDeviceName[RAS_MaxDeviceName + 1];  #endif #if (WINVER >= 0x401)     TCHAR szPhonebook[MAX_PATH];     DWORD dwSubEntry; #endif #if (WINVER >= 0x500)     GUID guidEntry; #endif #if (WINVER >= 0x501)     DWORD dwSessionId;     DWORD dwFlags;     LUID luid; #endif } RASCONN; 

The most useful field in the RASCONN structure is the hrasconn, which receives the connection handle that RasDial originally created. You can use this handle to retrieve more connection information from RasGetProjectionInfo, which is described later. You need to pass to RasEnumConnections a large enough buffer to hold several RASCONN structures; otherwise, this function will fail with the error ERROR_BUFFER_TOO_SMALL. Also, the first RASCONN structure in your buffer must have the dwSize field set to the byte size of a RASCONN structure. The next parameter, lpcb, is a pointer to a variable that you must set to the size (in bytes) of your lprasconn array. When this function returns, lpcb will contain the number of bytes required to enumerate all connections. If you don't supply a large enough buffer, you can always try again with the correct buffer size returned in lpcb. The lpcConnections parameter is a pointer to a variable that receives a count of the number of RASCONN structures written to lprasconn.

With the RAS connection handles you receive from RasEnumConnections, you can obtain network protocol–specific information that is used over an established RAS connection. This is known as projection information. A remote access server uses projection information to represent a remote client on the network. For example, when you make a RAS connection that uses IP over a framing protocol, IP configuration information (such as an assigned IP address) is established from the RAS to your client. You can retrieve projection information for the protocols that travel over the PPP framing protocol by calling the RasGetProjectionInfo function, which is defined as

DWORD RasGetProjectionInfo(     HRASCONN hrasconn,     RASPROJECTION rasprojection,     LPVOID lpprojection,     LPDWORD lpcb );

The hrasconn parameter is a RAS connection handle. The rasprojection parameter is a RASPROJECTION enumeration type that allows you to specify a protocol to receive connection information for. The lpprojection parameter receives a data structure that is associated with the enumeration type specified in rasprojection. The final parameter, lpcb, is a pointer to a variable that you must set to the size of your lpprojection structure. When this function completes, this variable will contain the size of the buffer needed to obtain the projection information.

The following RASPROJECTION enumeration types allow you to receive connection information:

  • RASP_Amb

  • RASP_PppCcp

  • RASP_PppNbf

  • RASP_PppIp

  • RASP_PppIpx

  • RASP_PppLcp

  • RASP_Slip

To retrieve IP address information for a PPP framing protocol connection, you must specify the RASP_PppIp enumeration type. When you specify a RASP_PppIp, you will receive a RASPPPIP structure that is defined as

typedef  struct  _RASPPPIP {    DWORD    dwSize;    DWORD    dwError;    TCHAR    szIpAddress[ RAS_MaxIpAddress + 1 ];  #ifndef WINNT35COMPATIBLE   TCHAR    szServerIpAddress[ RAS_MaxIpAddress + 1 ]; #endif #if (WINVER >= 0x500)   DWORD    dwOptions;   DWORD    dwServerOptions; #endif } RASPPPIP; 

The fields are defined as

  • dwSize Should be set to the size (in bytes) of a RASPPPIP structure

  • dwError Receives an error code from the PPP negotiation process

  • szIpAddress Receives a string representing the client's IP address

  • szServerIpAddress Receives a string representing the server's IP address

  • dwOptions Compression options for the local host

  • dwServerOptions Compression options for the remote host

The following code demonstrates how to call RasGetProjectionInfo to retrieve the IP addresses assigned to a client when a PPP connection is made over RAS:

lpProjection = (RASPPPIP *) GlobalAlloc(GPTR, cb); lpProjection->dwSize = sizeof(RASPPPIP); cb = sizeof(RASPPPIP); Ret = RasGetProjectionInfo(hRasConn, RASP_PppIp,      lpProjection, &cb); if (Ret != ERROR_SUCCESS) {     printf("RasGetProjectionInfo failed with error %d", Ret);     return; }  else {     printf("\nRas Client IP address: %s\n",          lpProjection->szIpAddress);     printf("Ras Server IP address: %s\n",          lpProjection->szServerIpAddress); }



Network Programming for Microsoft Windows
Network Programming for Microsoft Windows (Microsoft Professional Series)
ISBN: 0735605602
EAN: 2147483647
Year: 2001
Pages: 172
Authors: Anthony Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net