Skip to content

Instantly share code, notes, and snippets.

@shuffle2
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shuffle2/a4deca329f885d26da71 to your computer and use it in GitHub Desktop.
Save shuffle2/a4deca329f885d26da71 to your computer and use it in GitHub Desktop.
GetIpForwardTable C++
#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;
}
@shuffle2
Copy link
Author

output:

ReturnValue =  0202a8c0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment