Adding an Entry to the IP Forward Table
A version of this page is also available for
4/8/2010
This code creates a new route in the IP Forward Table. Note the route must be a viable one or IP will not take it.
PMIB_IPFORWARDROW pRow = NULL;
ULONG dwSize = 0;
DWORD dwStatus = 0;
// Allocate some memory to store the row in
pRow = (PMIB_IPFORWARDROW)malloc(sizeof(MIB_IPFORWARDROW));
if (!pRow) {
OutputMessage(TEXT("Malloc failed, Out of Memory!\r\n"));
exit(1);
}
pRow->dwForwardDest = 0xAACCBBDD; // AA.BB.CC.DD
pRow->dwForwardNextHop = 0x01CCBBAA; // this is in host order Ip
//Address AA.BB.CC.01 is 01CCBBAA
pRow->dwForwardMask = 0x0000FFFF; // 255.255.0.0
pRow->dwForwardIfIndex = 2;
pRow->dwForwardType = MIB_IPROUTE_TYPE_DIRECT; // A local route
pRow->dwForwardProto = MIB_IPPROTO_RIP; // This is defined in
//iprtrmib.h
pRow->dwForwardAge = 0; // Just created it
pRow->dwForwardMetric1 = 1;
// Create a new route entry for the default gateway.
dwStatus = CreateIpForwardEntry(pRow);
if (dwStatus == NO_ERROR)
OutputMessage(TEXT("Route Added Successfully\n"));
else if (dwStatus == ERROR_INVALID_PARAMETER)
OutputMessage(TEXT("Invalid Parameter\n"));
else
DisplayErrorMessage(dwStatus);
// Free resources
if (pRow)
free(pRow);