Deleting an Entry to the IP Forward Table (Windows Embedded CE 6.0)
1/6/2010
This code deletes a route with the destination Dest, netmask Mask, and gateway Gateway. Note that IP will not allow default routes to be deleted.
PMIB_IPFORWARDTABLE pIpForwardTable = NULL;
PMIB_IPFORWARDROW pRow = NULL;
ULONG dwSize = 0;
BOOL bOrder = FALSE;
DWORD dwStatus = 0;
DWORD Dest = 0xDDBBCCAA; // this is in host order Ip Address AA.BB.CC.DD is DDCCBBAA
DWORD Mask = 0x00FFFFFF; // 255.255.255.0
DWORD Gateway = 0xDDBBCCAA; // this is in host order Ip Address AA.BB.CC.DD is DDCCBBAA
// Find out how big our buffer needs to be
dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
if (dwStatus == ERROR_INSUFFICIENT_BUFFER) {
// Allocate the memory for the table
if (!(pIpForwardTable = (PMIB_IPFORWARDTABLE)malloc(dwSize))) {
OutputMessage(TEXT("Malloc failed, Out of Memory!\r\n"));
exit(1);
}
// Now get the table
dwStatus = GetIpForwardTable(pIpForwardTable, &dwSize, bOrder);
}
if (dwStatus != ERROR_SUCCESS) {
OutputMessage(TEXT("GetIpForwardTable Failed, ERROR %d\r\n"), dwStatus);
if (pIpForwardTable)
free(pIpForwardTable);
exit(1);
}
// Search for the row in the table that is wanted. The default gateway has a destination
// of 0.0.0.0 - Notice that while the table is looked through, but only one row is copied
// This is so that if there happen to be multiple default gateways, all
// of them can be deleted.
for (unsigned long int i=0; i < pIpForwardTable->dwNumEntries; i++) {
if ((pIpForwardTable->table[i].dwForwardDest == Dest) &&
(pIpForwardTable->table[i].dwForwardMask == Mask) &&
(pIpForwardTable->table[i].dwForwardNextHop == Gateway)) {
// Delete the old default gateway entry
dwStatus = DeleteIpForwardEntry(&(pIpForwardTable->table[i]));
if (dwStatus != ERROR_SUCCESS) {
OutputMessage(TEXT("Could not delete route!\n"));
exit(1);
}
}
}
if (dwStatus == NO_ERROR)
OutputMessage(TEXT("Route deleted successfully\n"));
else if (dwStatus == ERROR_INVALID_PARAMETER)
OutputMessage(TEXT("Invalid Parameter\n"));
else
DisplayErrorMessage(dwStatus);
// Free resources
if (pIpForwardTable)
free(pIpForwardTable);