GetIpForwardTable
The GetIpForwardTable function retrieves the IP routing table.
Syntax
DWORD GetIpForwardTable(
PMIB_IPFORWARDTABLE pIpForwardTable,
PDWORD pdwSize,
BOOL bOrder
);
Parameters
| Parameter | Description |
|---|---|
pIpForwardTable |
Pointer to a buffer that receives the IP routing table as a MIB_IPFORWARDTABLE structure.
|
pdwSize |
On input, specifies the size of the buffer pointed to by pIpForwardTable, in bytes.
On output, if the function fails and returns ERROR_INSUFFICIENT_BUFFER, this parameter receives the required buffer size.
|
bOrder |
A boolean value that specifies whether the routing table should be sorted. If TRUE, the table is sorted.
|
Return Value
Return Value
If the function succeeds, the return value is NO_ERROR.
If the function fails, the return value is one of the following error codes:
ERROR_INSUFFICIENT_BUFFER: The buffer pointed to bypIpForwardTableis not large enough to contain the routing table. The required buffer size is returned in thepdwSizeparameter.- Other error codes may be returned.
Remarks
This function is used to retrieve the IP routing table. The routing table contains information about how IP packets are forwarded across networks.
The returned routing table can be sorted by setting the bOrder parameter to TRUE. Sorting is performed based on the destination network, netmask, and interface.
Example
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int main() {
DWORD dwSize = 0;
MIB_IPFORWARDTABLE *pIpForwardTable = NULL;
DWORD dwRetVal = 0;
// Allocate memory for the IP forwarding table.
dwRetVal = GetIpForwardTable(NULL, &dwSize, TRUE);
if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
pIpForwardTable = (MIB_IPFORWARDTABLE *) malloc(dwSize);
if (pIpForwardTable == NULL) {
printf("Error allocating memory for IP forwarding table.\n");
return 1;
}
} else {
printf("Error getting IP forwarding table size: %lu\n", dwRetVal);
return 1;
}
// Get the IP forwarding table.
dwRetVal = GetIpForwardTable(pIpForwardTable, &dwSize, TRUE);
if (dwRetVal == NO_ERROR) {
printf("Successfully retrieved IP forwarding table.\n");
printf("Number of entries: %lu\n", pIpForwardTable->dwNumEntries);
for (DWORD i = 0; i < pIpForwardTable->dwNumEntries; i++) {
printf(" Index: %lu\n", pIpForwardTable->table[i].dwForwardIfIndex);
printf(" Destination Address: %s\n", inet_ntoa(*(struct in_addr *)&pIpForwardTable->table[i].dwForwardDest));
printf(" Netmask: %s\n", inet_ntoa(*(struct in_addr *)&pIpForwardTable->table[i].dwForwardMask));
printf(" Next Hop Address: %s\n", inet_ntoa(*(struct in_addr *)&pIpForwardTable->table[i].dwForwardNextHop));
printf(" Protocol: %lu\n", pIpForwardTable->table[i].dwForwardProto);
printf(" Age: %lu\n", pIpForwardTable->table[i].dwForwardAge);
printf(" Next Hop Age: %lu\n", pIpForwardTable->table[i].dwForwardNextHopAge);
printf(" Type: %lu\n", pIpForwardTable->table[i].dwForwardType);
printf(" Policy Based Routing: %lu\n", pIpForwardTable->table[i].dwForwardPolicyBasedRouting);
printf("--------------------\n");
}
} else {
printf("Error retrieving IP forwarding table: %lu\n", dwRetVal);
}
// Free allocated memory.
if (pIpForwardTable != NULL) {
free(pIpForwardTable);
pIpForwardTable = NULL;
}
return 0;
}
Structure Definition
MIB_IPFORWARDTABLE
The MIB_IPFORWARDTABLE structure contains the IP routing table for the local system.
typedef struct _MIB_IPFORWARDTABLE {
DWORD dwNumEntries;
MIB_IPFORWARDROW table[ANY_SIZE];
} MIB_IPFORWARDTABLE, *PMIB_IPFORWARDTABLE;
MIB_IPFORWARDROW
The MIB_IPFORWARDROW structure stores one entry in the IP routing table.
typedef struct _MIB_IPFORWARDROW {
DWORD dwForwardDest;
DWORD dwForwardMask;
DWORD dwForwardPolicyBasedRouting;
DWORD dwForwardNextHop;
DWORD dwForwardIfIndex;
DWORD dwForwardType;
DWORD dwForwardProto;
DWORD dwForwardAge;
DWORD dwForwardNextHopAge;
} MIB_IPFORWARDROW, *PMIB_IPFORWARDROW;
Note: The
dwForwardDest, dwForwardMask, and dwForwardNextHop members are in network order.