aboutsummaryrefslogtreecommitdiff
path: root/route.c
diff options
context:
space:
mode:
Diffstat (limited to 'route.c')
-rw-r--r--route.c107
1 files changed, 54 insertions, 53 deletions
diff --git a/route.c b/route.c
index 3fad81a01..fe8fd915e 100644
--- a/route.c
+++ b/route.c
@@ -15,9 +15,11 @@
15 * Foundation; either version 2 of the License, or (at 15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version. 16 * your option) any later version.
17 * 17 *
18 * $Id: route.c,v 1.3 2001/02/14 21:23:06 andersen Exp $ 18 * $Id: route.c,v 1.4 2001/02/15 23:00:48 markw Exp $
19 * 19 *
20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru> 20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
21 * busybox style adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
22 * displayroute() format now matches net-tools-1.57/lib/inet_gr.c line 173.
21 */ 23 */
22 24
23#include "busybox.h" 25#include "busybox.h"
@@ -65,9 +67,9 @@ INET_resolve(char *name, struct sockaddr *sa)
65 sin->sin_port = 0; 67 sin->sin_port = 0;
66 68
67 /* Default is special, meaning 0.0.0.0. */ 69 /* Default is special, meaning 0.0.0.0. */
68 if (!strcmp(name, "default")) { 70 if (strcmp(name, "default")==0) {
69 sin->sin_addr.s_addr = INADDR_ANY; 71 sin->sin_addr.s_addr = INADDR_ANY;
70 return (1); 72 return 1;
71 } 73 }
72 /* Look to see if it's a dotted quad. */ 74 /* Look to see if it's a dotted quad. */
73 if (inet_aton(name, &sin->sin_addr)) { 75 if (inet_aton(name, &sin->sin_addr)) {
@@ -107,10 +109,10 @@ INET_setroute(int action, int options, char **args)
107 109
108 xflag = 0; 110 xflag = 0;
109 111
110 if (!strcmp(*args, "-net")) { 112 if (strcmp(*args, "-net")==0) {
111 xflag = 1; 113 xflag = 1;
112 args++; 114 args++;
113 } else if (!strcmp(*args, "-host")) { 115 } else if (strcmp(*args, "-host")==0) {
114 xflag = 2; 116 xflag = 2;
115 args++; 117 args++;
116 } 118 }
@@ -124,8 +126,8 @@ INET_setroute(int action, int options, char **args)
124 126
125 127
126 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) { 128 if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) {
127 fprintf(stderr, "cant resolve %s\n", target); 129 error_msg(_("can't resolve %s"), target);
128 return (1); 130 return EXIT_FAILURE; /* XXX change to E_something */
129 } 131 }
130 132
131 switch (xflag) { 133 switch (xflag) {
@@ -147,7 +149,7 @@ INET_setroute(int action, int options, char **args)
147 rt.rt_flags &= ~RTF_HOST; 149 rt.rt_flags &= ~RTF_HOST;
148 150
149 while (*args) { 151 while (*args) {
150 if (!strcmp(*args, "metric")) { 152 if (strcmp(*args, "metric")==0) {
151 int metric; 153 int metric;
152 154
153 args++; 155 args++;
@@ -157,13 +159,13 @@ INET_setroute(int action, int options, char **args)
157#if HAVE_NEW_ADDRT 159#if HAVE_NEW_ADDRT
158 rt.rt_metric = metric + 1; 160 rt.rt_metric = metric + 1;
159#else 161#else
160 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); 162 ENOSUPP("inet_setroute", "NEW_ADDRT (metric)"); /* XXX Fixme */
161#endif 163#endif
162 args++; 164 args++;
163 continue; 165 continue;
164 } 166 }
165 167
166 if (!strcmp(*args, "netmask")) { 168 if (strcmp(*args, "netmask")==0) {
167 struct sockaddr mask; 169 struct sockaddr mask;
168 170
169 args++; 171 args++;
@@ -171,15 +173,15 @@ INET_setroute(int action, int options, char **args)
171 show_usage(); 173 show_usage();
172 safe_strncpy(netmask, *args, (sizeof netmask)); 174 safe_strncpy(netmask, *args, (sizeof netmask));
173 if ((isnet = INET_resolve(netmask, &mask)) < 0) { 175 if ((isnet = INET_resolve(netmask, &mask)) < 0) {
174 fprintf(stderr, "cant resolve netmask %s\n", netmask); 176 error_msg(_("can't resolve netmask %s"), netmask);
175 return (E_LOOKUP); 177 return E_LOOKUP;
176 } 178 }
177 rt.rt_genmask = full_mask(mask); 179 rt.rt_genmask = full_mask(mask);
178 args++; 180 args++;
179 continue; 181 continue;
180 } 182 }
181 183
182 if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) { 184 if (strcmp(*args, "gw")==0 || strcmp(*args, "gateway")==0) {
183 args++; 185 args++;
184 if (!*args) 186 if (!*args)
185 show_usage(); 187 show_usage();
@@ -187,21 +189,21 @@ INET_setroute(int action, int options, char **args)
187 show_usage(); 189 show_usage();
188 safe_strncpy(gateway, *args, (sizeof gateway)); 190 safe_strncpy(gateway, *args, (sizeof gateway));
189 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) { 191 if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) {
190 fprintf(stderr, "cant resolve gw %s\n", gateway); 192 error_msg(_("can't resolve gw %s"), gateway);
191 return (E_LOOKUP); 193 return E_LOOKUP;
192 } 194 }
193 if (isnet) { 195 if (isnet) {
194 fprintf(stderr, 196 error_msg(
195 _("route: %s: cannot use a NETWORK as gateway!\n"), 197 _("%s: cannot use a NETWORK as gateway!"),
196 gateway); 198 gateway);
197 return (E_OPTERR); 199 return E_OPTERR;
198 } 200 }
199 rt.rt_flags |= RTF_GATEWAY; 201 rt.rt_flags |= RTF_GATEWAY;
200 args++; 202 args++;
201 continue; 203 continue;
202 } 204 }
203 205
204 if (!strcmp(*args, "mss")) { 206 if (strcmp(*args, "mss")==0) {
205 args++; 207 args++;
206 rt.rt_flags |= RTF_MSS; 208 rt.rt_flags |= RTF_MSS;
207 if (!*args) 209 if (!*args)
@@ -209,13 +211,13 @@ INET_setroute(int action, int options, char **args)
209 rt.rt_mss = atoi(*args); 211 rt.rt_mss = atoi(*args);
210 args++; 212 args++;
211 if (rt.rt_mss < 64 || rt.rt_mss > 32768) { 213 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
212 fprintf(stderr, _("route: Invalid MSS.\n")); 214 error_msg(_("Invalid MSS."));
213 return (E_OPTERR); 215 return E_OPTERR;
214 } 216 }
215 continue; 217 continue;
216 } 218 }
217 219
218 if (!strcmp(*args, "window")) { 220 if (strcmp(*args, "window")==0) {
219 args++; 221 args++;
220 if (!*args) 222 if (!*args)
221 show_usage(); 223 show_usage();
@@ -223,13 +225,13 @@ INET_setroute(int action, int options, char **args)
223 rt.rt_window = atoi(*args); 225 rt.rt_window = atoi(*args);
224 args++; 226 args++;
225 if (rt.rt_window < 128) { 227 if (rt.rt_window < 128) {
226 fprintf(stderr, _("route: Invalid window.\n")); 228 error_msg(_("Invalid window."));
227 return (E_OPTERR); 229 return E_OPTERR;
228 } 230 }
229 continue; 231 continue;
230 } 232 }
231 233
232 if (!strcmp(*args, "irtt")) { 234 if (strcmp(*args, "irtt")==0) {
233 args++; 235 args++;
234 if (!*args) 236 if (!*args)
235 show_usage(); 237 show_usage();
@@ -240,41 +242,41 @@ INET_setroute(int action, int options, char **args)
240 rt.rt_irtt *= (HZ / 100); /* FIXME */ 242 rt.rt_irtt *= (HZ / 100); /* FIXME */
241#if 0 /* FIXME: do we need to check anything of this? */ 243#if 0 /* FIXME: do we need to check anything of this? */
242 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) { 244 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
243 fprintf(stderr, _("route: Invalid initial rtt.\n")); 245 error_msg(_("Invalid initial rtt."));
244 return (E_OPTERR); 246 return E_OPTERR;
245 } 247 }
246#endif 248#endif
247#else 249#else
248 ENOSUPP("inet_setroute", "RTF_IRTT"); 250 ENOSUPP("inet_setroute", "RTF_IRTT"); /* XXX Fixme */
249#endif 251#endif
250 continue; 252 continue;
251 } 253 }
252 254
253 if (!strcmp(*args, "reject")) { 255 if (strcmp(*args, "reject")==0) {
254 args++; 256 args++;
255#if HAVE_RTF_REJECT 257#if HAVE_RTF_REJECT
256 rt.rt_flags |= RTF_REJECT; 258 rt.rt_flags |= RTF_REJECT;
257#else 259#else
258 ENOSUPP("inet_setroute", "RTF_REJECT"); 260 ENOSUPP("inet_setroute", "RTF_REJECT"); /* XXX Fixme */
259#endif 261#endif
260 continue; 262 continue;
261 } 263 }
262 if (!strcmp(*args, "mod")) { 264 if (strcmp(*args, "mod")==0) {
263 args++; 265 args++;
264 rt.rt_flags |= RTF_MODIFIED; 266 rt.rt_flags |= RTF_MODIFIED;
265 continue; 267 continue;
266 } 268 }
267 if (!strcmp(*args, "dyn")) { 269 if (strcmp(*args, "dyn")==0) {
268 args++; 270 args++;
269 rt.rt_flags |= RTF_DYNAMIC; 271 rt.rt_flags |= RTF_DYNAMIC;
270 continue; 272 continue;
271 } 273 }
272 if (!strcmp(*args, "reinstate")) { 274 if (strcmp(*args, "reinstate")==0) {
273 args++; 275 args++;
274 rt.rt_flags |= RTF_REINSTATE; 276 rt.rt_flags |= RTF_REINSTATE;
275 continue; 277 continue;
276 } 278 }
277 if (!strcmp(*args, "device") || !strcmp(*args, "dev")) { 279 if (strcmp(*args, "device")==0 || strcmp(*args, "dev")==0) {
278 args++; 280 args++;
279 if (rt.rt_dev || *args == NULL) 281 if (rt.rt_dev || *args == NULL)
280 show_usage(); 282 show_usage();
@@ -286,8 +288,9 @@ INET_setroute(int action, int options, char **args)
286 rt.rt_dev = *args++; 288 rt.rt_dev = *args++;
287 if (*args) 289 if (*args)
288 show_usage(); /* must be last to catch typos */ 290 show_usage(); /* must be last to catch typos */
289 } else 291 } else {
290 show_usage(); 292 show_usage();
293 }
291 } 294 }
292 295
293#if HAVE_RTF_REJECT 296#if HAVE_RTF_REJECT
@@ -300,19 +303,19 @@ INET_setroute(int action, int options, char **args)
300 unsigned long mask = mask_in_addr(rt); 303 unsigned long mask = mask_in_addr(rt);
301 mask = ~ntohl(mask); 304 mask = ~ntohl(mask);
302 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) { 305 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
303 fprintf(stderr, 306 error_msg(
304 _("route: netmask %.8x doesn't make sense with host route\n"), 307 _("netmask %.8x doesn't make sense with host route"),
305 (unsigned int)mask); 308 (unsigned int)mask);
306 return (E_OPTERR); 309 return E_OPTERR;
307 } 310 }
308 if (mask & (mask + 1)) { 311 if (mask & (mask + 1)) {
309 fprintf(stderr, _("route: bogus netmask %s\n"), netmask); 312 error_msg(_("bogus netmask %s"), netmask);
310 return (E_OPTERR); 313 return E_OPTERR;
311 } 314 }
312 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr; 315 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
313 if (mask & ~mask_in_addr(rt)) { 316 if (mask & ~mask_in_addr(rt)) {
314 fprintf(stderr, _("route: netmask doesn't match route address\n")); 317 error_msg(_("netmask doesn't match route address"));
315 return (E_OPTERR); 318 return E_OPTERR;
316 } 319 }
317 } 320 }
318 /* Fill out netmask if still unset */ 321 /* Fill out netmask if still unset */
@@ -322,26 +325,26 @@ INET_setroute(int action, int options, char **args)
322 /* Create a socket to the INET kernel. */ 325 /* Create a socket to the INET kernel. */
323 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 326 if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
324 perror("socket"); 327 perror("socket");
325 return (E_SOCK); 328 return E_SOCK;
326 } 329 }
327 /* Tell the kernel to accept this route. */ 330 /* Tell the kernel to accept this route. */
328 if (action == RTACTION_DEL) { 331 if (action == RTACTION_DEL) {
329 if (ioctl(skfd, SIOCDELRT, &rt) < 0) { 332 if (ioctl(skfd, SIOCDELRT, &rt) < 0) {
330 perror("SIOCDELRT"); 333 perror("SIOCDELRT");
331 close(skfd); 334 close(skfd);
332 return (E_SOCK); 335 return E_SOCK;
333 } 336 }
334 } else { 337 } else {
335 if (ioctl(skfd, SIOCADDRT, &rt) < 0) { 338 if (ioctl(skfd, SIOCADDRT, &rt) < 0) {
336 perror("SIOCADDRT"); 339 perror("SIOCADDRT");
337 close(skfd); 340 close(skfd);
338 return (E_SOCK); 341 return E_SOCK;
339 } 342 }
340 } 343 }
341 344
342 /* Close the socket. */ 345 /* Close the socket. */
343 (void) close(skfd); 346 (void) close(skfd);
344 return (0); 347 return EXIT_SUCCESS;
345} 348}
346 349
347void displayroutes(void) 350void displayroutes(void)
@@ -392,7 +395,7 @@ Destination\tGateway\t\tGenmask\t\tFlags Metric Ref Use Iface\n");
392 inet_ntoa(dest))); 395 inet_ntoa(dest)));
393 strcpy(sgw, (gw.s_addr==0 ? "*" : 396 strcpy(sgw, (gw.s_addr==0 ? "*" :
394 inet_ntoa(gw))); 397 inet_ntoa(gw)));
395 printf("%-16s%-16s%-16s%-6s%-7d%-9d%-2d%s\n", 398 printf("%-15s %-15s %-15s %-5s %-6d %-2d %7d %s\n",
396 sdest, sgw, 399 sdest, sgw,
397 inet_ntoa(mask), 400 inet_ntoa(mask),
398 flags, metric, ref, use, buff); 401 flags, metric, ref, use, buff);
@@ -413,17 +416,15 @@ int route_main(int argc, char **argv)
413 exit(EXIT_SUCCESS); 416 exit(EXIT_SUCCESS);
414 } else { 417 } else {
415 /* check verb */ 418 /* check verb */
416 if (!strcmp(*argv, "add")) 419 if (strcmp(*argv, "add")==0)
417 what = RTACTION_ADD; 420 what = RTACTION_ADD;
418 else if (!strcmp(*argv, "del") || !strcmp(*argv, "delete")) 421 else if (strcmp(*argv, "del")==0 || strcmp(*argv, "delete")==0)
419 what = RTACTION_DEL; 422 what = RTACTION_DEL;
420 else if (!strcmp(*argv, "flush")) 423 else if (strcmp(*argv, "flush")==0)
421 what = RTACTION_FLUSH; 424 what = RTACTION_FLUSH;
422 else 425 else
423 show_usage(); 426 show_usage();
424 } 427 }
425 428
426 INET_setroute(what, 0, ++argv); 429 return INET_setroute(what, 0, ++argv);
427
428 exit(EXIT_SUCCESS);
429} 430}