Getting ARP information

You can use GetIpNetTable() to get information about the ARP table in Windows platform. In this page, I will show how to use GetIpNetTable().

Sample code

This is a sample code that uses GetIpNetTable().


#include <stdio.h>

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

int
main()
{
 DWORD i;
 PMIB_IPNETTABLE pIpNetTable = NULL;
 DWORD dwSize = 0;
 DWORD dwRetVal = 0;
 DWORD dwResult;

 dwResult = GetIpNetTable(NULL, &dwSize, 0);
 /* Get the size required by GetIpNetTable() */
 if (dwResult == ERROR_INSUFFICIENT_BUFFER) {
   pIpNetTable = (MIB_IPNETTABLE *) malloc (dwSize);
 }

 /* Now that we know the size, lets use GetIpNetTable() */
 if ((dwRetVal = GetIpNetTable(pIpNetTable, &dwSize, 0))
      == NO_ERROR) {
   if (pIpNetTable->dwNumEntries > 0) {
     for (i=0; i<pIpNetTable->dwNumEntries; i++) {
      printf("Address: %s\n",
        inet_ntoa(*(struct in_addr *)&pIpNetTable->table[i].dwAddr));

      /*
       * WARN : In this sample, it assumes that
       * dwPhysAddrLen is 6.
       * It is better not to assume dwPhysAddrLen is 6.
       */
      printf("Phys Addr Len: %d\n",
		pIpNetTable->table[i].dwPhysAddrLen);
      printf("Phys Address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
	    pIpNetTable->table[i].bPhysAddr[0],
		pIpNetTable->table[i].bPhysAddr[1],
        pIpNetTable->table[i].bPhysAddr[2],
        pIpNetTable->table[i].bPhysAddr[3],
        pIpNetTable->table[i].bPhysAddr[4],
        pIpNetTable->table[i].bPhysAddr[5]);

       printf("Index:  %ld\n", pIpNetTable->table[i].dwIndex);
       printf("Type:   %ld\n", pIpNetTable->table[i].dwType);
       printf("\n");
     }
   }
 } else {
   printf("GetIpNetTable 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;
}

Output of the sample code

The sample will output an message like the following.


C:> a.exe
Address: 192.168.0.1
Phys Addr Len: 6
Phys Address: 00:d2:03:6e:3c:17
Index:   16788213
Type:   3

Structure used by GetIpNetTable()

The following structure is MIB_IPNETTABLE that is used by GetIpNetTable().


typedef struct _MIB_IPNETROW {
  DWORD dwIndex;
  DWORD dwPhysAddrLen;
  BYTE bPhysAddr[MAXLEN_PHYSADDR];
  DWORD dwAddr;
  DWORD dwType;
} MIB_IPNETROW, *PMIB_IPNETROW;

typedef struct _MIB_IPNETTABLE {
  DWORD dwNumEntries;
  MIB_IPNETROW table[ANY_SIZE];
} MIB_IPNETTABLE, *PMIB_IPNETTABLE;

MIB_IPNETROW

dwIndex The index of the Adapter that is related to the ARP entry.
dwPhysAddrLen Length of Physical Address.
bPhysAddr Physical Address. The length is shown by dwPhysAddrLen.
dwAddr IP address.
dwType Type of ARP entry. 4:static, 3:dynamic, 2:invalid, 1:other

MIB_IPNETTABLE

dwNumEntries Number of ARP entries.
table An array of MIB_IPNETROW. Array length is shown by dwNumEntries.

  

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