How To Retrieve the IP Address of the Remote PPP Peer (183988)



The information in this article applies to:

  • Microsoft Windows CE Operating System, Versions 2.0

This article was previously published under Q183988

SUMMARY

There is a special host name called "ppp_peer" that serves as a proxy for the IP address of the remote PPP peer that the Windows CE device is connected to. This article demonstrates how to programmatically retrieve the IP address of the remote peer by using Windows Sockets gethostbyname() API with "ppp_peer".

MORE INFORMATION

The following sample code demonstrates how to programmatically retrieve the IP address of the remote peer by using Windows Sockets gethostbyname() API with "ppp_peer".

Sample Code

   #include <windows.h>
   #include <winsock.h>

   #define MAXLEN   64

   BOOL GetIpAddress(char * hostname);

   int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdSHow)
   {
      WSADATA  wsadata;
      HOSTENT *lpHost=NULL;
      char    hostname[MAXLEN];
      WCHAR    msg[128];
      int      ret, addrlen;
      SOCKET    sock;
      struct   sockaddr_in   dest, retaddr;


      WSAStartup(0x0101, &wsadata);

      if ((ret = gethostname(hostname, MAXLEN)) != 0)
      {
         wsprintf(msg, L"Error calling gethostname: %d", ret);
         MessageBox(NULL, msg, NULL, MB_OK);
      }
      else
      {
         wsprintf(msg, L"Hostname is: %S", hostname);
         MessageBox(NULL, msg, L"Hostname", MB_OK);
      }

      // Print the local IP address.
      MessageBox(NULL, L"Local IP address is", L"Address", MB_OK);
      GetIpAddress (hostname);
      // Print the remote peer IP address.
      MessageBox(NULL, L"Remote IP address is", L"Address", MB_OK);
      GetIpAddress ("ppp_peer");

      WSACleanup();

      return 0;
   }



   BOOL GetIpAddress(char *hostname)
   {
      WCHAR    msg[128];
      HOSTENT *lpHost=NULL;
           struct sockaddr_in   dest;

      lpHost = gethostbyname(hostname);
      if (lpHost == NULL)
      {
         wsprintf(msg, L"gethostbyname failed: %d", WSAGetLastError());
         MessageBox(NULL, msg, NULL, MB_OK);
      }
      else
      {
         for(int i=0; lpHost->h_addr_list[i] != NULL ;i++)
         {
            memcpy(&(dest.sin_addr), lpHost->h_addr_list[i],
                   lpHost->h_length);
            wsprintf(msg, L"IP address is: '%S'",
                     inet_ntoa(dest.sin_addr));
            MessageBox(NULL, msg, L"IP Address", MB_OK);
         }

      }
      return 0;
   }
				

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbIP kbnetwork kbWinsock KB183988