Start to alter print output so it correctly simulates the MS 'route print'

svn path=/trunk/; revision=18193
This commit is contained in:
Ged Murphy 2005-10-01 15:47:40 +00:00
parent 6526069195
commit d67541c53b

View file

@ -15,86 +15,116 @@
#include <windows.h> #include <windows.h>
#include <iphlpapi.h> #include <iphlpapi.h>
#include <winsock2.h> #include <winsock2.h>
#include <tchar.h>
#define IPBUF 17 #define IPBUF 17
#define IN_ADDR_OF(x) *((struct in_addr *)&(x)) #define IN_ADDR_OF(x) *((struct in_addr *)&(x))
int usage() { int usage()
{
fprintf( stderr, fprintf( stderr,
"route usage:\n" "route usage:\n"
"route print\n" "route print\n"
" prints the route table\n" " prints the route table\n"
"route add <target> [mask <mask>] <gw> [metric <m>]\n" "route add <target> [mask <mask>] <gw> [metric <m>]\n"
" adds a route\n" " adds a route\n"
"route delete <target> <gw>\n" "route delete <target> <gw>\n"
" deletes a route\n" ); " deletes a route\n" );
return 1; return 1;
} }
int print_routes() { int print_routes()
{
PMIB_IPFORWARDTABLE IpForwardTable = NULL; PMIB_IPFORWARDTABLE IpForwardTable = NULL;
PIP_ADAPTER_INFO pAdapterInfo = NULL;
DWORD Error; DWORD Error;
ULONG Size = 0; ULONG Size = 0;
ULONG adaptOutBufLen;
TCHAR DefGate[16];
char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
unsigned int i; unsigned int i;
if( (Error = GetIpForwardTable( NULL, &Size, TRUE )) == /* set required buffer size */
ERROR_INSUFFICIENT_BUFFER ) { pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
IpForwardTable = malloc( Size ); if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
Error = GetIpForwardTable( IpForwardTable, &Size, TRUE ); pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
if( (GetIpForwardTable( NULL, &Size, TRUE )) == ERROR_INSUFFICIENT_BUFFER )
IpForwardTable = malloc( Size );
if ((GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen) == NO_ERROR) &&
GetIpForwardTable( IpForwardTable, &Size, TRUE ) == NO_ERROR)
{
_tcsncpy(DefGate, pAdapterInfo->GatewayList.IpAddress.String, 16);
_tprintf(_T("===========================================================================\n"));
_tprintf(_T("Interface List\n"));
while (pAdapterInfo)
{
_tprintf(_T("0x%lu ........................... %s\n"), pAdapterInfo->Index, pAdapterInfo->Description);
pAdapterInfo = pAdapterInfo->Next;
}
_tprintf(_T("===========================================================================\n"));
_tprintf(_T("===========================================================================\n"));
_tprintf(_T("Active Routes:\n"));
printf( "%-27s%-17s%-14s%-11s%-10s\n",
"Network Destination",
"Netmask",
"Gateway",
"Interface",
"Metric" );
for( i = 0; i < IpForwardTable->dwNumEntries; i++ )
{
strcpy( Destination, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) );
strcpy( Netmask, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) );
strcpy( Gateway, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) );
printf( "%17s%17s%17s%16ld%9ld\n",
Destination,
Netmask,
Gateway,
IpForwardTable->table[i].dwForwardIfIndex,
IpForwardTable->table[i].dwForwardMetric1 );
}
_tprintf(_T("Default Gateway:%18s\n"), DefGate);
_tprintf(_T("===========================================================================\n"));
_tprintf(_T("Persistent Routes:\n"));
free( IpForwardTable );
return ERROR_SUCCESS;
} }
else
if( Error == ERROR_SUCCESS ) { {
printf( "%-16s%-16s%-16s%-10s%-10s\n", fprintf( stderr, "Route enumerate failed\n" );
"Destination", return Error;
"Netmask",
"Gateway",
"Index",
"Metric" );
for( i = 0; i < IpForwardTable->dwNumEntries; i++ ) {
strcpy( Destination,
inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].
dwForwardDest) ) );
strcpy( Netmask,
inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].
dwForwardMask) ) );
strcpy( Gateway,
inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].
dwForwardNextHop) ) );
printf( "%-16s%-16s%-16s%-10ld%-10ld\n",
Destination,
Netmask,
Gateway,
IpForwardTable->table[i].dwForwardIfIndex,
IpForwardTable->table[i].dwForwardMetric1 );
}
free( IpForwardTable );
return ERROR_SUCCESS;
} else {
fprintf( stderr, "Route enumerate failed\n" );
return Error;
} }
} }
int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd, int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd,
int argc, char **argv ) { int argc, char **argv ) {
int i; int i;
if( argc > 1 ) RowToAdd->dwForwardDest = inet_addr( argv[0] ); if( argc > 1 )
else return FALSE; RowToAdd->dwForwardDest = inet_addr( argv[0] );
for( i = 1; i < argc; i++ ) { else
if( !strcasecmp( argv[i], "mask" ) ) { return FALSE;
i++; if( i >= argc ) return FALSE; for( i = 1; i < argc; i++ )
RowToAdd->dwForwardMask = inet_addr( argv[i] ); {
} else if( !strcasecmp( argv[i], "metric" ) ) { if( !strcasecmp( argv[i], "mask" ) )
i++; if( i >= argc ) return FALSE; {
RowToAdd->dwForwardMetric1 = atoi( argv[i] ); i++; if( i >= argc ) return FALSE;
} else { RowToAdd->dwForwardMask = inet_addr( argv[i] );
RowToAdd->dwForwardNextHop = inet_addr( argv[i] ); }
} else if( !strcasecmp( argv[i], "metric" ) )
{
i++;
if( i >= argc )
return FALSE;
RowToAdd->dwForwardMetric1 = atoi( argv[i] );
}
else
RowToAdd->dwForwardNextHop = inet_addr( argv[i] );
} }
return TRUE; return TRUE;
@ -104,53 +134,60 @@ int add_route( int argc, char **argv ) {
MIB_IPFORWARDROW RowToAdd = { 0 }; MIB_IPFORWARDROW RowToAdd = { 0 };
DWORD Error; DWORD Error;
if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) ) { if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) )
fprintf( stderr, {
"route add usage:\n" fprintf( stderr,
"route add <target> [mask <mask>] <gw> [metric <m>]\n" "route add usage:\n"
" Adds a route to the IP route table.\n" "route add <target> [mask <mask>] <gw> [metric <m>]\n"
" <target> is the network or host to add a route to.\n" " Adds a route to the IP route table.\n"
" <mask> is the netmask to use (autodetected if unspecified)\n" " <target> is the network or host to add a route to.\n"
" <gw> is the gateway to use to access the network\n" " <mask> is the netmask to use (autodetected if unspecified)\n"
" <m> is the metric to use (lower is preferred)\n" ); " <gw> is the gateway to use to access the network\n"
return 1; " <m> is the metric to use (lower is preferred)\n" );
return 1;
} }
if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS ) if( (Error = CreateIpForwardEntry( &RowToAdd )) == ERROR_SUCCESS )
return 0; return 0;
fprintf( stderr, "Route addition failed\n" ); fprintf( stderr, "Route addition failed\n" );
return Error; return Error;
} }
int del_route( int argc, char **argv ) { int del_route( int argc, char **argv )
{
MIB_IPFORWARDROW RowToDel = { 0 }; MIB_IPFORWARDROW RowToDel = { 0 };
DWORD Error; DWORD Error;
if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) ) { if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) )
fprintf( stderr, {
"route delete usage:\n" fprintf( stderr,
"route delete <target> <gw>\n" "route delete usage:\n"
" Removes a route from the IP route table.\n" "route delete <target> <gw>\n"
" <target> is the network or host to add a route to.\n" " Removes a route from the IP route table.\n"
" <gw> is the gateway to remove the route from.\n" ); " <target> is the network or host to add a route to.\n"
return 1; " <gw> is the gateway to remove the route from.\n" );
return 1;
} }
if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS ) if( (Error = DeleteIpForwardEntry( &RowToDel )) == ERROR_SUCCESS )
return 0; return 0;
fprintf( stderr, "Route addition failed\n" ); fprintf( stderr, "Route addition failed\n" );
return Error; return Error;
} }
int main( int argc, char **argv ) { int main( int argc, char **argv )
if( argc < 2 ) return usage(); {
else if( !strcasecmp( argv[1], "print" ) )
return print_routes(); if( argc < 2 )
return usage();
else if ( !strcasecmp( argv[1], "print" ) )
return print_routes();
else if( !strcasecmp( argv[1], "add" ) ) else if( !strcasecmp( argv[1], "add" ) )
return add_route( argc-2, argv+2 ); return add_route( argc-2, argv+2 );
else if( !strcasecmp( argv[1], "delete" ) ) else if( !strcasecmp( argv[1], "delete" ) )
return del_route( argc-2, argv+2 ); return del_route( argc-2, argv+2 );
else return usage(); else
return usage();
} }