diff options
Diffstat (limited to 'networking/route.c')
-rw-r--r-- | networking/route.c | 69 |
1 files changed, 29 insertions, 40 deletions
diff --git a/networking/route.c b/networking/route.c index ee3533100..43180296d 100644 --- a/networking/route.c +++ b/networking/route.c | |||
@@ -15,7 +15,7 @@ | |||
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.13 2001/09/05 19:32:00 andersen Exp $ | 18 | * $Id: route.c,v 1.14 2001/11/10 11:22:43 andersen 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 | * adjustments by Larry Doolittle <LRDoolittle@lbl.gov> | 21 | * adjustments by Larry Doolittle <LRDoolittle@lbl.gov> |
@@ -23,11 +23,9 @@ | |||
23 | 23 | ||
24 | #include <sys/types.h> | 24 | #include <sys/types.h> |
25 | #include <sys/ioctl.h> | 25 | #include <sys/ioctl.h> |
26 | #include <sys/socket.h> | 26 | #include "inet_common.h" |
27 | #include <net/route.h> | 27 | #include <net/route.h> |
28 | #include <linux/param.h> // HZ | 28 | #include <linux/param.h> // HZ |
29 | #include <netinet/in.h> | ||
30 | #include <arpa/inet.h> | ||
31 | #include <stdio.h> | 29 | #include <stdio.h> |
32 | #include <errno.h> | 30 | #include <errno.h> |
33 | #include <fcntl.h> | 31 | #include <fcntl.h> |
@@ -55,29 +53,6 @@ | |||
55 | #define E_INTERN 2 | 53 | #define E_INTERN 2 |
56 | #define E_NOSUPP 1 | 54 | #define E_NOSUPP 1 |
57 | 55 | ||
58 | /* resolve XXX.YYY.ZZZ.QQQ -> binary */ | ||
59 | |||
60 | static int | ||
61 | INET_resolve(char *name, struct sockaddr *sa) | ||
62 | { | ||
63 | struct sockaddr_in *s_in = (struct sockaddr_in *)sa; | ||
64 | |||
65 | s_in->sin_family = AF_INET; | ||
66 | s_in->sin_port = 0; | ||
67 | |||
68 | /* Default is special, meaning 0.0.0.0. */ | ||
69 | if (strcmp(name, "default")==0) { | ||
70 | s_in->sin_addr.s_addr = INADDR_ANY; | ||
71 | return 1; | ||
72 | } | ||
73 | /* Look to see if it's a dotted quad. */ | ||
74 | if (inet_aton(name, &s_in->sin_addr)) { | ||
75 | return 0; | ||
76 | } | ||
77 | /* guess not.. */ | ||
78 | return -1; | ||
79 | } | ||
80 | |||
81 | #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */ | 56 | #if defined (SIOCADDRTOLD) || defined (RTF_IRTT) /* route */ |
82 | #define HAVE_NEW_ADDRT 1 | 57 | #define HAVE_NEW_ADDRT 1 |
83 | #endif | 58 | #endif |
@@ -96,13 +71,16 @@ INET_resolve(char *name, struct sockaddr *sa) | |||
96 | #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr) | 71 | #define full_mask(x) (((struct sockaddr_in *)&(x))->sin_addr.s_addr) |
97 | #endif | 72 | #endif |
98 | 73 | ||
74 | |||
75 | |||
99 | /* add or delete a route depending on action */ | 76 | /* add or delete a route depending on action */ |
100 | 77 | ||
101 | static int | 78 | static int |
102 | INET_setroute(int action, int options, char **args) | 79 | INET_setroute(int action, int options, char **args) |
103 | { | 80 | { |
104 | struct rtentry rt; | 81 | struct rtentry rt; |
105 | char target[128], gateway[128] = "NONE", netmask[128] = "default"; | 82 | char target[128], gateway[128] = "NONE"; |
83 | const char *netmask = bb_INET_default; | ||
106 | int xflag, isnet; | 84 | int xflag, isnet; |
107 | int skfd; | 85 | int skfd; |
108 | 86 | ||
@@ -117,13 +95,15 @@ INET_setroute(int action, int options, char **args) | |||
117 | xflag = 2; | 95 | xflag = 2; |
118 | args++; | 96 | args++; |
119 | } | 97 | } |
98 | if (*args == NULL) | ||
99 | show_usage(); | ||
120 | safe_strncpy(target, *args++, (sizeof target)); | 100 | safe_strncpy(target, *args++, (sizeof target)); |
121 | 101 | ||
122 | /* Clean out the RTREQ structure. */ | 102 | /* Clean out the RTREQ structure. */ |
123 | memset((char *) &rt, 0, sizeof(struct rtentry)); | 103 | memset((char *) &rt, 0, sizeof(struct rtentry)); |
124 | 104 | ||
125 | 105 | ||
126 | if ((isnet = INET_resolve(target, &rt.rt_dst)) < 0) { | 106 | if ((isnet = INET_resolve(target, (struct sockaddr_in *)&rt.rt_dst, xflag!=1)) < 0) { |
127 | error_msg(_("can't resolve %s"), target); | 107 | error_msg(_("can't resolve %s"), target); |
128 | return EXIT_FAILURE; /* XXX change to E_something */ | 108 | return EXIT_FAILURE; /* XXX change to E_something */ |
129 | } | 109 | } |
@@ -169,8 +149,8 @@ INET_setroute(int action, int options, char **args) | |||
169 | args++; | 149 | args++; |
170 | if (!*args || mask_in_addr(rt)) | 150 | if (!*args || mask_in_addr(rt)) |
171 | show_usage(); | 151 | show_usage(); |
172 | safe_strncpy(netmask, *args, (sizeof netmask)); | 152 | netmask = *args; |
173 | if ((isnet = INET_resolve(netmask, &mask)) < 0) { | 153 | if ((isnet = INET_resolve(netmask, (struct sockaddr_in *)&mask, 0)) < 0) { |
174 | error_msg(_("can't resolve netmask %s"), netmask); | 154 | error_msg(_("can't resolve netmask %s"), netmask); |
175 | return E_LOOKUP; | 155 | return E_LOOKUP; |
176 | } | 156 | } |
@@ -186,7 +166,7 @@ INET_setroute(int action, int options, char **args) | |||
186 | if (rt.rt_flags & RTF_GATEWAY) | 166 | if (rt.rt_flags & RTF_GATEWAY) |
187 | show_usage(); | 167 | show_usage(); |
188 | safe_strncpy(gateway, *args, (sizeof gateway)); | 168 | safe_strncpy(gateway, *args, (sizeof gateway)); |
189 | if ((isnet = INET_resolve(gateway, &rt.rt_gateway)) < 0) { | 169 | if ((isnet = INET_resolve(gateway, (struct sockaddr_in *)&rt.rt_gateway, 1)) < 0) { |
190 | error_msg(_("can't resolve gw %s"), gateway); | 170 | error_msg(_("can't resolve gw %s"), gateway); |
191 | return E_LOOKUP; | 171 | return E_LOOKUP; |
192 | } | 172 | } |
@@ -362,7 +342,7 @@ INET_setroute(int action, int options, char **args) | |||
362 | #define RTF_REJECT 0x0200 /* Reject route */ | 342 | #define RTF_REJECT 0x0200 /* Reject route */ |
363 | #endif | 343 | #endif |
364 | 344 | ||
365 | static void displayroutes(void) | 345 | static void displayroutes(int noresolve) |
366 | { | 346 | { |
367 | char buff[256]; | 347 | char buff[256]; |
368 | int nl = 0 ; | 348 | int nl = 0 ; |
@@ -375,12 +355,17 @@ static void displayroutes(void) | |||
375 | 355 | ||
376 | char sdest[16], sgw[16]; | 356 | char sdest[16], sgw[16]; |
377 | 357 | ||
378 | |||
379 | FILE *fp = xfopen("/proc/net/route", "r"); | 358 | FILE *fp = xfopen("/proc/net/route", "r"); |
380 | 359 | ||
360 | if(noresolve) | ||
361 | noresolve = 0x0fff; | ||
362 | |||
381 | while( fgets(buff, sizeof(buff), fp) != NULL ) { | 363 | while( fgets(buff, sizeof(buff), fp) != NULL ) { |
382 | if(nl) { | 364 | if(nl) { |
383 | int ifl = 0; | 365 | int ifl = 0; |
366 | int numeric; | ||
367 | struct sockaddr_in s_addr; | ||
368 | |||
384 | while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0') | 369 | while(buff[ifl]!=' ' && buff[ifl]!='\t' && buff[ifl]!='\0') |
385 | ifl++; | 370 | ifl++; |
386 | buff[ifl]=0; /* interface */ | 371 | buff[ifl]=0; /* interface */ |
@@ -412,10 +397,14 @@ static void displayroutes(void) | |||
412 | dest.s_addr = d; | 397 | dest.s_addr = d; |
413 | gw.s_addr = g; | 398 | gw.s_addr = g; |
414 | mask.s_addr = m; | 399 | mask.s_addr = m; |
415 | strcpy(sdest, (dest.s_addr==0 ? "default" : | 400 | memset(&s_addr, 0, sizeof(struct sockaddr_in)); |
416 | inet_ntoa(dest))); | 401 | s_addr.sin_family = AF_INET; |
417 | strcpy(sgw, (gw.s_addr==0 ? "*" : | 402 | s_addr.sin_addr = dest; |
418 | inet_ntoa(gw))); | 403 | numeric = noresolve | 0x8000; /* default instead of * */ |
404 | INET_rresolve(sdest, sizeof(sdest), &s_addr, numeric, m); | ||
405 | numeric = noresolve | 0x4000; /* host instead of net */ | ||
406 | s_addr.sin_addr = gw; | ||
407 | INET_rresolve(sgw, sizeof(sgw), &s_addr, numeric, m); | ||
419 | printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n", | 408 | printf("%-16s%-16s%-16s%-6s%-6d %-2d %7d %s\n", |
420 | sdest, sgw, | 409 | sdest, sgw, |
421 | inet_ntoa(mask), | 410 | inet_ntoa(mask), |
@@ -433,8 +422,8 @@ int route_main(int argc, char **argv) | |||
433 | argc--; | 422 | argc--; |
434 | argv++; | 423 | argv++; |
435 | 424 | ||
436 | if (*argv == NULL) { | 425 | if (*argv == NULL || (*(argv+1)==NULL && strcmp(*argv, "-n")==0)) { |
437 | displayroutes(); | 426 | displayroutes(*argv != NULL); |
438 | return EXIT_SUCCESS; | 427 | return EXIT_SUCCESS; |
439 | } else { | 428 | } else { |
440 | /* check verb */ | 429 | /* check verb */ |