Documentation for developers
The GetIpForwardTable
function retrieves the IP forwarding table for the local computer.
This function is declared in iphlpapi.h
.
PMIB_IPFORWARDTABLE GetIpForwardTable(
[out] PMIB_IPFORWARDTABLE pIpForwardTable,
[in, out] PULONG pSize,
[in] BOOL bOrder
);
Parameter | Description |
---|---|
pIpForwardTable |
A pointer to a buffer that receives the IP forwarding table as a MIB_IPFORWARDTABLE structure.
If the buffer is not large enough, the function returns ERROR_INSUFFICIENT_BUFFER and this parameter is populated with the required buffer size.
|
pSize |
On input, specifies the size of the buffer pointed to by pIpForwardTable , in bytes.
On output, if the buffer is not large enough, this parameter receives the required buffer size.
|
bOrder |
A boolean value that specifies whether the returned forwarding entries should be sorted. If this parameter is TRUE , the entries are sorted in ascending order by destination address.
|
If the function succeeds, the return value is NO_ERROR
.
If the buffer pointed to by pIpForwardTable
is not large enough to hold the returned forwarding entries, GetIpForwardTable
returns ERROR_INSUFFICIENT_BUFFER
.
If the function fails, the return value is one of the following error codes.
Return Code | Description |
---|---|
ERROR_INVALID_PARAMETER |
One of the parameter values is invalid. |
ERROR_NOT_SUPPORTED |
This operation is not supported on the local system. |
The IP forwarding table is managed by the IP router service. The IP forwarding table contains entries that define how to forward IP datagrams between different networks.
The GetIpForwardTable
function is used to retrieve the contents of the IP forwarding table. The information returned is in the form of a MIB_IPFORWARDTABLE
structure.
Note: To use this function, you must have administrative privileges on the local computer.
The MIB_IPFORWARDTABLE
structure contains an array of MIB_IPFORWARDROW
structures. Each MIB_IPFORWARDROW
structure represents an entry in the IP forwarding table.
An example of how to use GetIpForwardTable
to retrieve and display the IP forwarding table:
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "iphlpapi.lib")
int main() {
ULONG ulSize = 0;
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
DWORD dwRetVal = 0;
// Get the required buffer size
dwRetVal = GetIpForwardTable(NULL, &ulSize, TRUE);
if (dwRetVal == ERROR_INSUFFICIENT_BUFFER) {
pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(ulSize);
if (pIpForwardTable == NULL) {
printf("Error allocating memory\n");
return 1;
}
} else {
printf("Error getting buffer size: %d\n", dwRetVal);
return 1;
}
// Get the IP forwarding table
dwRetVal = GetIpForwardTable(pIpForwardTable, &ulSize, TRUE);
if (dwRetVal == NO_ERROR) {
printf("Successfully retrieved IP forwarding table.\n");
printf("Number of entries: %ld\n", pIpForwardTable->dwNumEntries);
for (int i = 0; i < pIpForwardTable->dwNumEntries; i++) {
char destIp[INET_ADDRSTRLEN];
char maskIp[INET_ADDRSTRLEN];
char gateIp[INET_ADDRSTRLEN];
char nextHopIp[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(pIpForwardTable->table[i].dwForwardDest), destIp, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(pIpForwardTable->table[i].dwForwardMask), maskIp, INET_ADDRSTRLEN);
inet_ntop(AF_INET, &(pIpForwardTable->table[i].dwForwardPolicy), gateIp, INET_ADDRSTRLEN); // This might be wrong, should be gateway? Or policy related.
inet_ntop(AF_INET, &(pIpForwardTable->table[i].dwForwardNextHop), nextHopIp, INET_ADDRSTRLEN);
printf("\nEntry %d:\n", i + 1);
printf(" Destination: %s\n", destIp);
printf(" Mask: %s\n", maskIp);
printf(" Next Hop: %s\n", nextHopIp);
printf(" Interface: %ld\n", pIpForwardTable->table[i].dwForwardIfIndex);
printf(" Protocol: %d\n", pIpForwardTable->table[i].dwForwardProto);
printf(" Type: %d\n", pIpForwardTable->table[i].dwForwardType);
printf(" Age: %ld\n", pIpForwardTable->table[i].dwForwardAge);
printf(" Next Hop AS: %ld\n", pIpForwardTable->table[i].dwForwardNextHopAS);
printf(" Policy: %ld\n", pIpForwardTable->table[i].dwForwardPolicy); // Assuming this represents policy
}
} else {
printf("Error getting IP forwarding table: %d\n", dwRetVal);
}
if (pIpForwardTable) {
free(pIpForwardTable);
}
return 0;
}
Attribute | Value |
---|---|
Minimum supported client | Windows 2000 Professional |
Minimum supported server | Windows 2000 Server |
Header | iphlpapi.h (include winsock2.h ) |
Library | Iphlpapi.lib |
DLL | Iphlpapi.dll |