Last active
August 29, 2015 14:01
-
-
Save shuffle2/a4deca329f885d26da71 to your computer and use it in GitHub Desktop.
GetIpForwardTable C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <winsock2.h> | |
#include <iphlpapi.h> | |
#include <cstdio> | |
#include <memory> | |
#pragma comment(lib, "iphlpapi.lib") | |
#pragma comment(lib, "ws2_32.lib") | |
int main() | |
{ | |
std::unique_ptr<MIB_IPFORWARDTABLE> forwardTable; | |
ULONG forwardTableSize = 0; | |
if (GetIpForwardTable(forwardTable.get(), &forwardTableSize, FALSE) != ERROR_INSUFFICIENT_BUFFER) | |
{ | |
// Handle error | |
printf("error1\n"); | |
} | |
forwardTable = std::unique_ptr<MIB_IPFORWARDTABLE>((PMIB_IPFORWARDTABLE)operator new(forwardTableSize)); | |
if (GetIpForwardTable(forwardTable.get(), &forwardTableSize, FALSE) != NO_ERROR) | |
{ | |
// Handle error | |
printf("error2\n"); | |
} | |
// forwardTable is finally valid | |
// Now same for ipTable | |
std::unique_ptr<MIB_IPADDRTABLE> ipTable; | |
ULONG ipTableSize = 0; | |
if (GetIpAddrTable(ipTable.get(), &ipTableSize, FALSE) != ERROR_INSUFFICIENT_BUFFER) | |
{ | |
// Handle error | |
printf("error1\n"); | |
} | |
ipTable = std::unique_ptr<MIB_IPADDRTABLE>((PMIB_IPADDRTABLE)operator new(ipTableSize)); | |
if (GetIpAddrTable(ipTable.get(), &ipTableSize, FALSE) != NO_ERROR) | |
{ | |
// Handle error | |
printf("error2\n"); | |
} | |
for (DWORD i = 0; i < forwardTable->dwNumEntries; i++) | |
{ | |
auto &forwardRow = forwardTable->table[i]; | |
if (forwardRow.dwForwardDest == 0) | |
{ | |
for (DWORD j = 0; j < ipTable->dwNumEntries; j++) | |
{ | |
auto &ipRow = ipTable->table[j]; | |
if (ipRow.dwIndex == forwardRow.dwForwardIfIndex) | |
{ | |
printf("ReturnValue = %08x", ipRow.dwAddr); | |
break; | |
} | |
} | |
break; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output: