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
= NULL
;
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
)))
69 Error
= ERROR_NOT_ENOUGH_MEMORY
;
74 if (((Error
= GetAdaptersInfo(pAdapterInfo
, &adaptOutBufLen
)) == NO_ERROR
) &&
75 ((Error
= GetIpForwardTable(IpForwardTable
, &Size
, TRUE
)) == NO_ERROR
))
83 pAdapterInfo
->GatewayList
.IpAddress
.String
);
84 _tprintf(_T("===========================================================================\n"));
85 _tprintf(_T("Interface List\n"));
86 /* FIXME - sort by the index! */
90 _tprintf(_T("0x%lu ........................... %hs\n"),
92 _tprintf(_T("0x%lu ........................... %s\n"),
94 pAdapterInfo
->Index
, pAdapterInfo
->Description
);
95 pAdapterInfo
= pAdapterInfo
->Next
;
97 _tprintf(_T("===========================================================================\n"));
99 _tprintf(_T("===========================================================================\n"));
100 _tprintf(_T("Active Routes:\n"));
101 _tprintf( _T("%-27s%-17s%-14s%-11s%-10s\n"),
102 _T("Network Destination"),
107 for( i
= 0; i
< IpForwardTable
->dwNumEntries
; i
++ )
109 _stprintf( Destination
,
115 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardDest
) ) );
122 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardMask
) ) );
129 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].dwForwardNextHop
) ) );
131 _tprintf( _T("%17s%17s%17s%16ld%9ld\n"),
135 IpForwardTable
->table
[i
].dwForwardIfIndex
,
136 IpForwardTable
->table
[i
].dwForwardMetric1
);
138 _tprintf(_T("Default Gateway:%18s\n"), DefGate
);
139 _tprintf(_T("===========================================================================\n"));
140 _tprintf(_T("Persistent Routes:\n"));
142 free(IpForwardTable
);
145 return ERROR_SUCCESS
;
150 if (pAdapterInfo
) free(pAdapterInfo
);
151 if (IpForwardTable
) free(IpForwardTable
);
152 _ftprintf( stderr
, _T("Route enumerate failed\n") );
157 static int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd
,
158 int argc
, TCHAR
**argv
) {
167 sprintf( addr
, "%ls", argv
[0] );
168 RowToAdd
->dwForwardDest
= inet_addr( addr
);
170 RowToAdd
->dwForwardDest
= inet_addr( argv
[0] );
175 for( i
= 1; i
< argc
; i
++ )
177 if( !_tcscmp( argv
[i
], _T("mask") ) )
179 i
++; if( i
>= argc
) return FALSE
;
181 sprintf( addr
, "%ls", argv
[i
] );
182 RowToAdd
->dwForwardMask
= inet_addr( addr
);
184 RowToAdd
->dwForwardMask
= inet_addr( argv
[i
] );
187 else if( !_tcscmp( argv
[i
], _T("metric") ) )
192 RowToAdd
->dwForwardMetric1
= _ttoi( argv
[i
] );
197 sprintf( addr
, "%ls", argv
[i
] );
198 RowToAdd
->dwForwardNextHop
= inet_addr( addr
);
200 RowToAdd
->dwForwardNextHop
= inet_addr( argv
[i
] );
208 static int add_route( int argc
, TCHAR
**argv
) {
209 MIB_IPFORWARDROW RowToAdd
= { 0 };
212 if( argc
< 2 || !convert_add_cmd_line( &RowToAdd
, argc
, argv
) )
215 _T("route add usage:\n")
216 _T("route add <target> [mask <mask>] <gw> [metric <m>]\n")
217 _T(" Adds a route to the IP route table.\n")
218 _T(" <target> is the network or host to add a route to.\n")
219 _T(" <mask> is the netmask to use (autodetected if unspecified)\n")
220 _T(" <gw> is the gateway to use to access the network\n")
221 _T(" <m> is the metric to use (lower is preferred)\n") );
225 if( (Error
= CreateIpForwardEntry( &RowToAdd
)) == ERROR_SUCCESS
)
228 _ftprintf( stderr
, _T("Route addition failed\n") );
232 static int del_route( int argc
, TCHAR
**argv
)
234 MIB_IPFORWARDROW RowToDel
= { 0 };
237 if( argc
< 2 || !convert_add_cmd_line( &RowToDel
, argc
, argv
) )
240 _T("route delete usage:\n")
241 _T("route delete <target> <gw>\n")
242 _T(" Removes a route from the IP route table.\n")
243 _T(" <target> is the network or host to add a route to.\n")
244 _T(" <gw> is the gateway to remove the route from.\n") );
248 if( (Error
= DeleteIpForwardEntry( &RowToDel
)) == ERROR_SUCCESS
)
251 _ftprintf( stderr
, _T("Route addition failed\n") );
255 int _tmain( int argc
, TCHAR
**argv
)
259 else if ( !_tcscmp( argv
[1], _T("print") ) )
260 return PrintRoutes();
261 else if( !_tcscmp( argv
[1], _T("add") ) )
262 return add_route( argc
-2, argv
+2 );
263 else if( !_tcscmp( argv
[1], _T("delete") ) )
264 return del_route( argc
-2, argv
+2 );