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
],
39 Index
[IPBUF
], Metric
[IPBUF
];
42 if( (Error
= GetIpForwardTable( NULL
, &Size
, TRUE
)) ==
43 ERROR_INSUFFICIENT_BUFFER
) {
44 IpForwardTable
= malloc( Size
);
45 Error
= GetIpForwardTable( IpForwardTable
, &Size
, TRUE
);
48 if( Error
== ERROR_SUCCESS
) {
49 printf( "%-16s%-16s%-16s%-10s%-10s\n",
55 for( i
= 0; i
< IpForwardTable
->dwNumEntries
; i
++ ) {
57 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
60 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
63 inet_ntoa( IN_ADDR_OF(IpForwardTable
->table
[i
].
64 dwForwardNextHop
) ) );
66 printf( "%-16s%-16s%-16s%-10d%-10d\n",
70 IpForwardTable
->table
[i
].dwForwardIfIndex
,
71 IpForwardTable
->table
[i
].dwForwardMetric1
);
74 free( IpForwardTable
);
78 fprintf( stderr
, "Route enumerate failed\n" );
83 int convert_add_cmd_line( PMIB_IPFORWARDROW RowToAdd
,
84 int argc
, char **argv
) {
87 if( argc
> 1 ) RowToAdd
->dwForwardDest
= inet_addr( argv
[0] );
89 for( i
= 1; i
< argc
; i
++ ) {
90 if( !strcasecmp( argv
[i
], "mask" ) ) {
91 i
++; if( i
>= argc
) return FALSE
;
92 RowToAdd
->dwForwardMask
= inet_addr( argv
[i
] );
93 } else if( !strcasecmp( argv
[i
], "metric" ) ) {
94 i
++; if( i
>= argc
) return FALSE
;
95 RowToAdd
->dwForwardMetric1
= atoi( argv
[i
] );
97 RowToAdd
->dwForwardNextHop
= inet_addr( argv
[i
] );
104 int add_route( int argc
, char **argv
) {
105 MIB_IPFORWARDROW RowToAdd
= { 0 };
108 if( argc
< 2 || !convert_add_cmd_line( &RowToAdd
, argc
, argv
) ) {
111 "route add <target> [mask <mask>] <gw> [metric <m>]\n"
112 " Adds a route to the IP route table.\n"
113 " <target> is the network or host to add a route to.\n"
114 " <mask> is the netmask to use (autodetected if unspecified)\n"
115 " <gw> is the gateway to use to access the network\n"
116 " <m> is the metric to use (lower is preferred)\n" );
120 if( (Error
= CreateIpForwardEntry( &RowToAdd
)) == ERROR_SUCCESS
)
123 fprintf( stderr
, "Route addition failed\n" );
127 int del_route( int argc
, char **argv
) {
128 MIB_IPFORWARDROW RowToDel
= { 0 };
131 if( argc
< 2 || !convert_add_cmd_line( &RowToDel
, argc
, argv
) ) {
133 "route delete usage:\n"
134 "route delete <target> <gw>\n"
135 " Removes a route from the IP route table.\n"
136 " <target> is the network or host to add a route to.\n"
137 " <gw> is the gateway to remove the route from.\n" );
141 if( (Error
= DeleteIpForwardEntry( &RowToDel
)) == ERROR_SUCCESS
)
144 fprintf( stderr
, "Route addition failed\n" );
148 int main( int argc
, char **argv
) {
149 if( argc
< 2 ) return usage();
150 else if( !strcasecmp( argv
[1], "print" ) )
151 return print_routes();
152 else if( !strcasecmp( argv
[1], "add" ) )
153 return add_route( argc
-2, argv
+2 );
154 else if( !strcasecmp( argv
[1], "delete" ) )
155 return del_route( argc
-2, argv
+2 );