Created
February 5, 2013 23:41
-
-
Save daviddesberg/4718783 to your computer and use it in GitHub Desktop.
Windows routing table dumper
This file contains hidden or 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 <iostream> | |
#include <string> | |
#include <sstream> | |
#include <ws2tcpip.h> | |
#include <iphlpapi.h> | |
#pragma comment(lib, "iphlpapi.lib") | |
#pragma comment(lib, "ws2_32.lib") | |
int main() | |
{ | |
PMIB_IPFORWARDTABLE ip_routing_table; | |
DWORD size = 0; | |
DWORD retval = 0; | |
std::stringstream jsonstream; | |
char ip_buffer[16]; | |
ip_routing_table = (PMIB_IPFORWARDTABLE) malloc(sizeof(MIB_IPFORWARDTABLE)); | |
if (ip_routing_table == NULL) { | |
std::cout << "{ \"error_desc\": \"error_allocating_memory\", \"error_code\": " << 1 << "}"; | |
return 1; | |
} | |
PMIB_IFROW interface_row = (PMIB_IFROW) malloc(sizeof(MIB_IFROW)); | |
if( interface_row == NULL ) { | |
std::cout << "{ \"error_desc\": \"error_allocating_memory\", \"error_code\": " << 1 << "}"; | |
return 1; | |
} | |
retval = GetIpForwardTable(ip_routing_table, &size, 0); | |
if (retval == ERROR_INSUFFICIENT_BUFFER) { | |
// buffer wasn\"t big enough, use the size passed by windows API and re-allocate/re-call API | |
free(ip_routing_table); | |
ip_routing_table = (MIB_IPFORWARDTABLE*) malloc(size); | |
if (ip_routing_table == NULL) { | |
// Failed to allocate memory after initially allocated buffer was too small for routing table. | |
std::cout << "{ \"error_desc\": \"error_allocating_memory\", \"error_code\": " << 1 << "}"; | |
return 1; | |
} | |
// re-try | |
retval = GetIpForwardTable(ip_routing_table, &size, 0); | |
} | |
if (retval != NO_ERROR) { | |
std::cout << "{ \"error_desc\": \"error_obtaining_routing_table\", \"error_code\": " << retval << "}"; | |
return retval; | |
} | |
// open array | |
jsonstream << "["; | |
for(int i = 0; i < (int) ip_routing_table->dwNumEntries; i++) | |
{ | |
// open object | |
jsonstream << "{"; | |
// destination ip address | |
inet_ntop(AF_INET /* ipv4 */, &ip_routing_table->table[i].dwForwardDest, ip_buffer, 16); | |
jsonstream << "\"destination_ip\":\"" << ip_buffer << "\","; | |
// subnet mask | |
inet_ntop(AF_INET, &ip_routing_table->table[i].dwForwardMask, ip_buffer, 16); | |
jsonstream << "\"subnet_mask\":\"" << ip_buffer << "\","; | |
// gateway | |
inet_ntop(AF_INET, &ip_routing_table->table[i].dwForwardNextHop, ip_buffer, 16); | |
jsonstream << "\"gateway\":\"" << ip_buffer << "\","; | |
// interface | |
interface_row->dwIndex = ip_routing_table->table[i].dwForwardIfIndex; | |
retval = GetIfEntry(interface_row); | |
if( retval != NO_ERROR ) { | |
std::cout << "{ \"error_desc\": \"error_getting_interface_row\", \"error_code\": " << retval << "}"; | |
return 1; | |
} | |
jsonstream << "\"interface_description\":\"" << interface_row->bDescr << "\","; | |
// metric | |
jsonstream << "\"metric\":" << ip_routing_table->table[i].dwForwardMetric1; | |
// close object | |
jsonstream << "}"; | |
if( ip_routing_table->dwNumEntries - i != 1) { | |
jsonstream << ","; | |
} | |
} | |
// close array | |
jsonstream << "]"; | |
std::cout << jsonstream.str(); | |
free(ip_routing_table); | |
free(interface_row); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment