6 * "add" target ["mask" mask] gw ["metric" metric]
21 #define IN_ADDR_OF(x) *((struct in_addr *)&(x))
28 _T(" prints the route table\n")
29 _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
31 _T("route delete <target> <gw>\n")
32 _T(" deletes a route\n") );
36 static int PrintRoutes()
38 PMIB_IPFORWARDTABLE IpForwardTable
= NULL
;
39 PIP_ADAPTER_INFO pAdapterInfo
;
42 ULONG adaptOutBufLen
= sizeof(IP_ADAPTER_INFO
);
44 TCHAR Destination
[IPBUF
], Gateway
[IPBUF
], Netmask
[IPBUF
];
47 /* set required buffer size */
48 pAdapterInfo
= (IP_ADAPTER_INFO
*) malloc( adaptOutBufLen
);
49 if (pAdapterInfo
== NULL
)
51 Error
= ERROR_NOT_ENOUGH_MEMORY
;
54 if (GetAdaptersInfo( pAdapterInfo
, &adaptOutBufLen
) == ERROR_BUFFER_OVERFLOW
)
57 pAdapterInfo
= (IP_ADAPTER_INFO
*) malloc (adaptOutBufLen
);
58 if (pAdapterInfo
== NULL
)
60 Error
= ERROR_NOT_ENOUGH_MEMORY
;
65 if( (GetIpForwardTable( NULL
, &Size
, TRUE
)) == ERROR_INSUFFICIENT_BUFFER
)
67 if (!(IpForwardTable
= malloc( Size
)))
70 Error
= ERROR_NOT_ENOUGH_MEMORY
;
75 if (((Error
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
)) == NO_ERROR
) &&
76 ((Error
= GetIpForwardTable(IpForwardTable
, &Size
, TRUE
)) == NO_ERROR
))
84 pAdapterInfo
->GatewayList
.IpAddress
.String
);
85 _tprintf(_T("===========================================================================\n"));
86 _tprintf(_T("Interface List\n"));
87 /* FIXME - sort by the index! */
91 _tprintf(_T("0x%lu ........................... %hs\n"),
93 _tprintf(_T("0x%lu ........................... %s\n"),
95 pAdapterInfo
->Index
, pAdapterInfo
->Description
);
96 pAdapterInfo
= pAdapterInfo
->Next
;
98 _tprintf(_T("===========================================================================\n"));
100 _tprintf(_T("===========================================================================\n"));
101 _tprintf(_T("Active Routes:\n"));
102 _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"),
103 _T("Network Destination"),
108 for( i
= 0; i
< IpForwardTable
->dwNumEntries
; i
++ )
110 _stprintf( Destination
,
116 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardDest
) ) );
123 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardMask
) ) );
130 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardNextHop
) ) );
132 _tprintf( _T("%17s%17s%17s%16ld%9ld\n"),
136 IpForwardTable
->table
[i
].dwForwardIfIndex
,
137 IpForwardTable
->table
[i
].dwForwardMetric1
);
139 _tprintf(_T("Default Gateway:%18s\n"), DefGate
);
140 _tprintf(_T("===========================================================================\n"));
141 _tprintf(_T("Persistent Routes:\n"));
143 free(IpForwardTable
);
146 return ERROR_SUCCESS
;
151 _ftprintf( stderr
, _T("Route enumerate failed\n") );
156 static int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd
,
157 int argc
, TCHAR
**argv
) {
166 sprintf( addr
, "%ls", argv
[0] );
167 RowToAdd
->dwForwardDest
= inet_addr( addr
);
169 RowToAdd
->dwForwardDest
= inet_addr( argv
[0] );
174 for( i
= 1; i
< argc
; i
++ )
176 if( !_tcscmp( argv
[i
], _T("mask") ) )
178 i
++; if( i
>= argc
) return FALSE
;
180 sprintf( addr
, "%ls", argv
[i
] );
181 RowToAdd
->dwForwardMask
= inet_addr( addr
);
183 RowToAdd
->dwForwardMask
= inet_addr( argv
[i
] );
186 else if( !_tcscmp( argv
[i
], _T("metric") ) )
191 RowToAdd
->dwForwardMetric1
= _ttoi( argv
[i
] );
196 sprintf( addr
, "%ls", argv
[i
] );
197 RowToAdd
->dwForwardNextHop
= inet_addr( addr
);
199 RowToAdd
->dwForwardNextHop
= inet_addr( argv
[i
] );
207 static int add_route( int argc
, TCHAR
**argv
) {
208 MIB_IPFORWARDROW RowToAdd
= { 0 };
211 if( argc
< 2 || !convert_add_cmd_line( &RowToAdd
, argc
, argv
) )
214 _T("route add usage:\n")
215 _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
216 _T(" Adds a route to the IP route table.\n")
217 _T(" <target> is the network or host to add a route to.\n")
218 _T(" <mask> is the netmask to use (autodetected if unspecified)\n")
219 _T(" <gw> is the gateway to use to access the network\n")
220 _T(" <m> is the metric to use (lower is preferred)\n") );
224 if( (Error
= CreateIpForwardEntry( &RowToAdd
)) == ERROR_SUCCESS
)
227 _ftprintf( stderr
, _T("Route addition failed\n") );
231 static int del_route( int argc
, TCHAR
**argv
)
233 MIB_IPFORWARDROW RowToDel
= { 0 };
236 if( argc
< 2 || !convert_add_cmd_line( &RowToDel
, argc
, argv
) )
239 _T("route delete usage:\n")
240 _T("route delete <target> <gw>\n")
241 _T(" Removes a route from the IP route table.\n")
242 _T(" <target> is the network or host to add a route to.\n")
243 _T(" <gw> is the gateway to remove the route from.\n") );
247 if( (Error
= DeleteIpForwardEntry( &RowToDel
)) == ERROR_SUCCESS
)
250 _ftprintf( stderr
, _T("Route addition failed\n") );
254 int _tmain( int argc
, TCHAR
**argv
)
258 else if ( !_tcscmp( argv
[1], _T("print") ) )
259 return PrintRoutes();
260 else if( !_tcscmp( argv
[1], _T("add") ) )
261 return add_route( argc
-2, argv
+2 );
262 else if( !_tcscmp( argv
[1], _T("delete") ) )
263 return del_route( argc
-2, argv
+2 );