6 * "add" target ["mask" mask] gw ["metric" metric]
20 #define IN_ADDR_OF(x) *((struct in_addr *)&(x))
26 " prints the route table\n"
27 "route add <target> [mask <mask>] <gw> [metric <m>]\n"
29 "route delete <target> <gw>\n"
30 " deletes a route\n" );
35 PMIB_IPFORWARDTABLE IpForwardTable
;
38 char Destination
[IPBUF
], Gateway
[IPBUF
], Netmask
[IPBUF
];
41 if( (Error
= GetIpForwardTable( NULL
, &Size
, TRUE
)) ==
42 ERROR_INSUFFICIENT_BUFFER
) {
43 IpForwardTable
= malloc( Size
);
44 Error
= GetIpForwardTable( IpForwardTable
, &Size
, TRUE
);
47 if( Error
== ERROR_SUCCESS
) {
48 printf( "%-16s%-16s%-16s%-10s%-10s\n",
54 for( i
= 0; i
< IpForwardTable
->dwNumEntries
; i
++ ) {
56 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
59 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
62 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
63 dwForwardNextHop
) ) );
65 printf( "%-16s%-16s%-16s%-10ld%-10ld\n",
69 IpForwardTable
->table
[i
].dwForwardIfIndex
,
70 IpForwardTable
->table
[i
].dwForwardMetric1
);
73 free( IpForwardTable
);
77 fprintf( stderr
, "Route enumerate failed\n" );
82 int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd
,
83 int argc
, char **argv
) {
86 if( argc
> 1 ) RowToAdd
->dwForwardDest
= inet_addr( argv
[0] );
88 for( i
= 1; i
< argc
; i
++ ) {
89 if( !strcasecmp( argv
[i
], "mask" ) ) {
90 i
++; if( i
>= argc
) return FALSE
;
91 RowToAdd
->dwForwardMask
= inet_addr( argv
[i
] );
92 } else if( !strcasecmp( argv
[i
], "metric" ) ) {
93 i
++; if( i
>= argc
) return FALSE
;
94 RowToAdd
->dwForwardMetric1
= atoi( argv
[i
] );
96 RowToAdd
->dwForwardNextHop
= inet_addr( argv
[i
] );
103 int add_route( int argc
, char **argv
) {
104 MIB_IPFORWARDROW RowToAdd
= { 0 };
107 if( argc
< 2 || !convert_add_cmd_line( &RowToAdd
, argc
, argv
) ) {
110 "route add <target> [mask <mask>] <gw> [metric <m>]\n"
111 " Adds a route to the IP route table.\n"
112 " <target> is the network or host to add a route to.\n"
113 " <mask> is the netmask to use (autodetected if unspecified)\n"
114 " <gw> is the gateway to use to access the network\n"
115 " <m> is the metric to use (lower is preferred)\n" );
119 if( (Error
= CreateIpForwardEntry( &RowToAdd
)) == ERROR_SUCCESS
)
122 fprintf( stderr
, "Route addition failed\n" );
126 int del_route( int argc
, char **argv
) {
127 MIB_IPFORWARDROW RowToDel
= { 0 };
130 if( argc
< 2 || !convert_add_cmd_line( &RowToDel
, argc
, argv
) ) {
132 "route delete usage:\n"
133 "route delete <target> <gw>\n"
134 " Removes a route from the IP route table.\n"
135 " <target> is the network or host to add a route to.\n"
136 " <gw> is the gateway to remove the route from.\n" );
140 if( (Error
= DeleteIpForwardEntry( &RowToDel
)) == ERROR_SUCCESS
)
143 fprintf( stderr
, "Route addition failed\n" );
147 int main( int argc
, char **argv
) {
148 if( argc
< 2 ) return usage();
149 else if( !strcasecmp( argv
[1], "print" ) )
150 return print_routes();
151 else if( !strcasecmp( argv
[1], "add" ) )
152 return add_route( argc
-2, argv
+2 );
153 else if( !strcasecmp( argv
[1], "delete" ) )
154 return del_route( argc
-2, argv
+2 );