Getting IP information from network interface

In this page, a way to obtain IP address, broadcast address and netmask information from network interfaces using IPHLPAPI is shown. These information can be obtained using GetIpAddrTable().

Sample code

The following sample code shows how to use GetIpAddrTable().


#include <stdio.h>

#include <winsock2.h>
#include <iphlpapi.h>

int
main()
{
 DWORD i;
 PMIB_IPADDRTABLE pIpAddrTable;
 DWORD dwSize = 0;
 DWORD dwRetVal = 0;

 /* Get size required by GetIpAddrTable() */
 if (GetIpAddrTable(NULL, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
   pIpAddrTable = (MIB_IPADDRTABLE *) malloc (dwSize);
 }

 /* Get actual data using GetIpAddrTable() */
 if ((dwRetVal = GetIpAddrTable(pIpAddrTable, &dwSize, 0))
      == NO_ERROR) {
   if (pIpAddrTable->dwNumEntries > 0) {
     for (i=1; i<=pIpAddrTable->dwNumEntries; i++) {
       printf("Address: %s\n",
        inet_ntoa(*(struct in_addr *)&pIpAddrTable->table[i].dwAddr));
       printf("Mask:    %s\n",
        inet_ntoa(*(struct in_addr *)&pIpAddrTable->table[i].dwMask));
       printf("Index:   %ld\n", pIpAddrTable->table[i].dwIndex);
       printf("BCast:   %ld\n", pIpAddrTable->table[i].dwBCastAddr);
       printf("Reasm:   %ld\n", pIpAddrTable->table[i].dwReasmSize);
       printf("\n");
     }
   }
 } else {
   printf("GetIpAddrTable failed.\n");
   LPVOID lpMsgBuf;
			
   if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                     FORMAT_MESSAGE_FROM_SYSTEM | 
                     FORMAT_MESSAGE_IGNORE_INSERTS,
                     NULL,
                     dwRetVal,
                     MAKELANGID(LANG_NEUTRAL,
                                SUBLANG_DEFAULT), //Default language
                     (LPTSTR) &lpMsgBuf,
                     0,
                     NULL ))  {
                       printf("\tError: %s", lpMsgBuf);
   }

   LocalFree( lpMsgBuf );
 }

 return 0;
}

Sample code output

The sample will output an message like the following.


C:> a.exe
Address: 192.168.0.3
Mask:    255.255.255.0
Index:   2
BCast:   1
Reasm:   65535

Address: 127.0.0.1
Mask:    255.0.0.0
Index:   1
BCast:   1
Reasm:   65535

Structure used by GetIpAddrTable()

The following structure is MIB_IPADDRTABLE that is used by GetIpAddrTable().


typedef struct _MIB_IPADDRROW {
  DWORD dwAddr;
  DWORDIF_INDEX dwIndex;
  DWORD dwMask;
  DWORD dwBCastAddr;
  DWORD dwReasmSize;
  unsigned short unused1;
  unsigned short wType;
} MIB_IPADDRROW, *PMIB_IPADDRROW;

typedef struct _MIB_IPADDRTABLE {
  DWORD dwNumEntries;
  MIB_IPADDRROW table[ANY_SIZE];
} MIB_IPADDRTABLE, *PMIB_IPADDRTABLE;

MIB_IPADDRROW

dwAddr IP address
dwIndex The Adapter Index related to the IP address.
dwMask Netmask.
dwBCastAddr Broadcast address.
dwReasmSize Re-assembly size for received datagrams.
unused1 Unused.
wType
MIB_IPADDR_PRIMARY
0x0001
Primary IP address
MIB_IPADDR_DYNAMIC
0x0004
Dynamic IP address
MIB_IPADDR_DISCONNECTED
0x0008
Address is on disconnected interface
MIB_IPADDR_DELETED
0x0040
Address is being deleted
MIB_IPADDR_TRANSIENT
0x0080
Transient address

MIB_IPADDRTABLE

dwNumEntries Number of IP address entries.
table IP address entry information is stored as array of MIB_IPADDRROW. The length of array is shown in dwNumEntries.

Note

In WindowsXP platform, it is recommended to use GetAdaptersAddresses() rather than GetIpAddrTable(). GetAdaptersAddresses() supports both IPv4 and IPv6, while GetIpAddrTable() only supports IPv4. However, if you decide to use only IPv4, you can use GetIpAddrTable().

  

Copyright (C) GeekPage.JP. All rights reserved.