aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-03-14 22:11:20 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-03-14 22:11:20 +0000
commit7f2527e5a7f513b9f415643ddc0ecd236b596887 (patch)
tree710d52b0a41425e8e82ad3ea5c674b5f61264cf7
parent650a0459b87314fc412dc81a2a863fefb969ca37 (diff)
downloadbusybox-w32-7f2527e5a7f513b9f415643ddc0ecd236b596887.tar.gz
busybox-w32-7f2527e5a7f513b9f415643ddc0ecd236b596887.tar.bz2
busybox-w32-7f2527e5a7f513b9f415643ddc0ecd236b596887.zip
arp, networking/interface.c: eliminate statics
and unneeded on-stack buffers, disable (comment out) some apparently unused (and buggy) code paths. -700 bytes.
-rw-r--r--include/libbb.h6
-rw-r--r--networking/arp.c132
-rw-r--r--networking/interface.c37
3 files changed, 90 insertions, 85 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 632ed937d..9ea9f5ae6 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -129,8 +129,8 @@ struct aftype {
129 int af; 129 int af;
130 int alen; 130 int alen;
131 char *(*print) (unsigned char *); 131 char *(*print) (unsigned char *);
132 char *(*sprint) (struct sockaddr *, int numeric); 132 const char *(*sprint) (struct sockaddr *, int numeric);
133 int (*input) (int type, char *bufp, struct sockaddr *); 133 int (*input) (/*int type,*/ const char *bufp, struct sockaddr *);
134 void (*herror) (char *text); 134 void (*herror) (char *text);
135 int (*rprint) (int options); 135 int (*rprint) (int options);
136 int (*rinput) (int typ, int ext, char **argv); 136 int (*rinput) (int typ, int ext, char **argv);
@@ -149,7 +149,7 @@ struct hwtype {
149 int type; 149 int type;
150 int alen; 150 int alen;
151 char *(*print) (unsigned char *); 151 char *(*print) (unsigned char *);
152 int (*input) (char *, struct sockaddr *); 152 int (*input) (const char *, struct sockaddr *);
153 int (*activate) (int fd); 153 int (*activate) (int fd);
154 int suppress_null_addr; 154 int suppress_null_addr;
155}; 155};
diff --git a/networking/arp.c b/networking/arp.c
index 2af6bc970..7a36f44bb 100644
--- a/networking/arp.c
+++ b/networking/arp.c
@@ -27,8 +27,6 @@
27#define DFLT_AF "inet" 27#define DFLT_AF "inet"
28#define DFLT_HW "ether" 28#define DFLT_HW "ether"
29 29
30#define _PATH_PROCNET_ARP "/proc/net/arp"
31
32#define ARP_OPT_A (0x1) 30#define ARP_OPT_A (0x1)
33#define ARP_OPT_p (0x2) 31#define ARP_OPT_p (0x2)
34#define ARP_OPT_H (0x4) 32#define ARP_OPT_H (0x4)
@@ -64,7 +62,7 @@ static const char *const options[] = {
64/* Called only from main, once */ 62/* Called only from main, once */
65static int arp_del(char **args) 63static int arp_del(char **args)
66{ 64{
67 char host[128]; 65 char *host;
68 struct arpreq req; 66 struct arpreq req;
69 struct sockaddr sa; 67 struct sockaddr sa;
70 int flags = 0; 68 int flags = 0;
@@ -73,8 +71,8 @@ static int arp_del(char **args)
73 memset(&req, 0, sizeof(req)); 71 memset(&req, 0, sizeof(req));
74 72
75 /* Resolve the host name. */ 73 /* Resolve the host name. */
76 safe_strncpy(host, *args, 128); 74 host = *args;
77 if (ap->input(0, host, &sa) < 0) { 75 if (ap->input(host, &sa) < 0) {
78 bb_herror_msg_and_die("%s", host); 76 bb_herror_msg_and_die("%s", host);
79 } 77 }
80 78
@@ -130,8 +128,8 @@ static int arp_del(char **args)
130 if (*++args == NULL) 128 if (*++args == NULL)
131 bb_show_usage(); 129 bb_show_usage();
132 if (strcmp(*args, "255.255.255.255") != 0) { 130 if (strcmp(*args, "255.255.255.255") != 0) {
133 safe_strncpy(host, *args, 128); 131 host = *args;
134 if (ap->input(0, host, &sa) < 0) { 132 if (ap->input(host, &sa) < 0) {
135 bb_herror_msg_and_die("%s", host); 133 bb_herror_msg_and_die("%s", host);
136 } 134 }
137 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr)); 135 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
@@ -213,15 +211,15 @@ static void arp_getdevhw(char *ifname, struct sockaddr *sa,
213/* Called only from main, once */ 211/* Called only from main, once */
214static int arp_set(char **args) 212static int arp_set(char **args)
215{ 213{
216 char host[128]; 214 char *host;
217 struct arpreq req; 215 struct arpreq req;
218 struct sockaddr sa; 216 struct sockaddr sa;
219 int flags; 217 int flags;
220 218
221 memset(&req, 0, sizeof(req)); 219 memset(&req, 0, sizeof(req));
222 220
223 safe_strncpy(host, *args++, 128); 221 host = *args++;
224 if (ap->input(0, host, &sa) < 0) { 222 if (ap->input(host, &sa) < 0) {
225 bb_herror_msg_and_die("%s", host); 223 bb_herror_msg_and_die("%s", host);
226 } 224 }
227 /* If a host has more than one address, use the correct one! */ 225 /* If a host has more than one address, use the correct one! */
@@ -285,8 +283,8 @@ static int arp_set(char **args)
285 if (*++args == NULL) 283 if (*++args == NULL)
286 bb_show_usage(); 284 bb_show_usage();
287 if (strcmp(*args, "255.255.255.255") != 0) { 285 if (strcmp(*args, "255.255.255.255") != 0) {
288 safe_strncpy(host, *args++, 128); 286 host = *args;
289 if (ap->input(0, host, &sa) < 0) { 287 if (ap->input(host, &sa) < 0) {
290 bb_herror_msg_and_die("%s", host); 288 bb_herror_msg_and_die("%s", host);
291 } 289 }
292 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr)); 290 memcpy(&req.arp_netmask, &sa, sizeof(struct sockaddr));
@@ -362,82 +360,82 @@ arp_disp(const char *name, char *ip, int type, int arp_flags,
362/* Called only from main, once */ 360/* Called only from main, once */
363static int arp_show(char *name) 361static int arp_show(char *name)
364{ 362{
365 char host[100]; 363 const char *host;
364 const char *hostname;
365 FILE *fp;
366 struct sockaddr sa; 366 struct sockaddr sa;
367 char ip[100];
368 char hwa[100];
369 char mask[100];
370 char line[200];
371 char dev[100];
372 int type, flags; 367 int type, flags;
373 FILE *fp;
374 const char *hostname;
375 int num; 368 int num;
376 unsigned entries = 0, shown = 0; 369 unsigned entries = 0, shown = 0;
370 char ip[128];
371 char hwa[128];
372 char mask[128];
373 char line[128];
374 char dev[128];
377 375
378 host[0] = '\0'; 376 host = NULL;
379
380 if (name != NULL) { 377 if (name != NULL) {
381 /* Resolve the host name. */ 378 /* Resolve the host name. */
382 safe_strncpy(host, name, (sizeof host)); 379 if (ap->input(name, &sa) < 0) {
383 if (ap->input(0, host, &sa) < 0) { 380 bb_herror_msg_and_die("%s", name);
384 bb_herror_msg_and_die("%s", host);
385 } 381 }
386 safe_strncpy(host, ap->sprint(&sa, 1), sizeof(host)); 382 host = xstrdup(ap->sprint(&sa, 1));
387 } 383 }
388 /* Open the PROCps kernel table. */ 384 fp = xfopen("/proc/net/arp", "r");
389 fp = xfopen(_PATH_PROCNET_ARP, "r"); 385 /* Bypass header -- read one line */
390 /* Bypass header -- read until newline */ 386 fgets(line, sizeof(line), fp);
391 if (fgets(line, sizeof(line), fp) != (char *) NULL) { 387
388 /* Read the ARP cache entries. */
389 while (fgets(line, sizeof(line), fp)) {
390
392 mask[0] = '-'; mask[1] = '\0'; 391 mask[0] = '-'; mask[1] = '\0';
393 dev[0] = '-'; dev[1] = '\0'; 392 dev[0] = '-'; dev[1] = '\0';
394 /* Read the ARP cache entries. */ 393 /* All these strings can't overflow
395 for (; fgets(line, sizeof(line), fp);) { 394 * because fgets above reads limited amount of data */
396 num = sscanf(line, "%s 0x%x 0x%x %100s %100s %100s\n", 395 num = sscanf(line, "%s 0x%x 0x%x %s %s %s\n",
397 ip, &type, &flags, hwa, mask, dev); 396 ip, &type, &flags, hwa, mask, dev);
398 if (num < 4) 397 if (num < 4)
399 break; 398 break;
400
401 entries++;
402 /* if the user specified hw-type differs, skip it */
403 if (hw_set && (type != hw->type))
404 continue;
405
406 /* if the user specified address differs, skip it */
407 if (host[0] && strcmp(ip, host) != 0)
408 continue;
409
410 /* if the user specified device differs, skip it */
411 if (device[0] && strcmp(dev, device) != 0)
412 continue;
413
414 shown++;
415 /* This IS ugly but it works -be */
416 if (option_mask32 & ARP_OPT_n)
417 hostname = "?";
418 else {
419 if (ap->input(0, ip, &sa) < 0)
420 hostname = ip;
421 else
422 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
423 if (strcmp(hostname, ip) == 0)
424 hostname = "?";
425 }
426 399
427 arp_disp(hostname, ip, type, flags, hwa, mask, dev); 400 entries++;
401 /* if the user specified hw-type differs, skip it */
402 if (hw_set && (type != hw->type))
403 continue;
404
405 /* if the user specified address differs, skip it */
406 if (host && strcmp(ip, host) != 0)
407 continue;
408
409 /* if the user specified device differs, skip it */
410 if (device[0] && strcmp(dev, device) != 0)
411 continue;
412
413 shown++;
414 /* This IS ugly but it works -be */
415 hostname = "?";
416 if (!(option_mask32 & ARP_OPT_n)) {
417 if (ap->input(ip, &sa) < 0)
418 hostname = ip;
419 else
420 hostname = ap->sprint(&sa, (option_mask32 & ARP_OPT_n) | 0x8000);
421 if (strcmp(hostname, ip) == 0)
422 hostname = "?";
428 } 423 }
424
425 arp_disp(hostname, ip, type, flags, hwa, mask, dev);
429 } 426 }
430 if (option_mask32 & ARP_OPT_v) 427 if (option_mask32 & ARP_OPT_v)
431 printf("Entries: %d\tSkipped: %d\tFound: %d\n", 428 printf("Entries: %d\tSkipped: %d\tFound: %d\n",
432 entries, entries - shown, shown); 429 entries, entries - shown, shown);
433 430
434 if (!shown) { 431 if (!shown) {
435 if (hw_set || host[0] || device[0]) 432 if (hw_set || host || device[0])
436 printf("No match found in %d entries\n", entries); 433 printf("No match found in %d entries\n", entries);
437 } 434 }
438 435 if (ENABLE_FEATURE_CLEAN_UP) {
439 fclose(fp); 436 free((char*)host);
440 437 fclose(fp);
438 }
441 return 0; 439 return 0;
442} 440}
443 441
diff --git a/networking/interface.c b/networking/interface.c
index c03471238..262b97879 100644
--- a/networking/interface.c
+++ b/networking/interface.c
@@ -82,12 +82,12 @@ struct in6_ifreq {
82#endif 82#endif
83 83
84/* Display an Internet socket address. */ 84/* Display an Internet socket address. */
85static char *INET_sprint(struct sockaddr *sap, int numeric) 85static const char *INET_sprint(struct sockaddr *sap, int numeric)
86{ 86{
87 static char buff[128]; 87 static char buff[128];
88 88
89 if (sap->sa_family == 0xFFFF || sap->sa_family == 0) 89 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
90 return safe_strncpy(buff, "[NONE SET]", sizeof(buff)); 90 return "[NONE SET]";
91 91
92 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, 92 if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap,
93 numeric, 0xffffff00) != 0) 93 numeric, 0xffffff00) != 0)
@@ -96,6 +96,7 @@ static char *INET_sprint(struct sockaddr *sap, int numeric)
96 return buff; 96 return buff;
97} 97}
98 98
99#ifdef UNUSED_AND_BUGGY
99static int INET_getsock(char *bufp, struct sockaddr *sap) 100static int INET_getsock(char *bufp, struct sockaddr *sap)
100{ 101{
101 char *sp = bufp, *bp; 102 char *sp = bufp, *bp;
@@ -136,9 +137,12 @@ static int INET_getsock(char *bufp, struct sockaddr *sap)
136 137
137 return (sp - bufp); 138 return (sp - bufp);
138} 139}
140#endif
139 141
140static int INET_input(int type, char *bufp, struct sockaddr *sap) 142static int INET_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
141{ 143{
144 return INET_resolve(bufp, (struct sockaddr_in *) sap, 0);
145/*
142 switch (type) { 146 switch (type) {
143 case 1: 147 case 1:
144 return (INET_getsock(bufp, sap)); 148 return (INET_getsock(bufp, sap));
@@ -147,6 +151,7 @@ static int INET_input(int type, char *bufp, struct sockaddr *sap)
147 default: 151 default:
148 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0)); 152 return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0));
149 } 153 }
154*/
150} 155}
151 156
152static struct aftype inet_aftype = { 157static struct aftype inet_aftype = {
@@ -163,17 +168,18 @@ static struct aftype inet_aftype = {
163 168
164/* Display an Internet socket address. */ 169/* Display an Internet socket address. */
165/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ 170/* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */
166static char *INET6_sprint(struct sockaddr *sap, int numeric) 171static const char *INET6_sprint(struct sockaddr *sap, int numeric)
167{ 172{
168 static char buff[128]; 173 static char buff[128];
169 174
170 if (sap->sa_family == 0xFFFF || sap->sa_family == 0) 175 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
171 return safe_strncpy(buff, "[NONE SET]", sizeof(buff)); 176 return "[NONE SET]";
172 if (INET6_rresolve(buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric)) 177 if (INET6_rresolve(buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric))
173 return safe_strncpy(buff, "[UNKNOWN]", sizeof(buff)); 178 return "[UNKNOWN]";
174 return buff; 179 return buff;
175} 180}
176 181
182#ifdef UNUSED
177static int INET6_getsock(char *bufp, struct sockaddr *sap) 183static int INET6_getsock(char *bufp, struct sockaddr *sap)
178{ 184{
179 struct sockaddr_in6 *sin6; 185 struct sockaddr_in6 *sin6;
@@ -187,15 +193,19 @@ static int INET6_getsock(char *bufp, struct sockaddr *sap)
187 193
188 return 16; /* ?;) */ 194 return 16; /* ?;) */
189} 195}
196#endif
190 197
191static int INET6_input(int type, char *bufp, struct sockaddr *sap) 198static int INET6_input(/*int type,*/ const char *bufp, struct sockaddr *sap)
192{ 199{
200 return INET6_resolve(bufp, (struct sockaddr_in6 *) sap);
201/*
193 switch (type) { 202 switch (type) {
194 case 1: 203 case 1:
195 return (INET6_getsock(bufp, sap)); 204 return (INET6_getsock(bufp, sap));
196 default: 205 default:
197 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap)); 206 return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap));
198 } 207 }
208*/
199} 209}
200 210
201static struct aftype inet6_aftype = { 211static struct aftype inet6_aftype = {
@@ -229,12 +239,10 @@ static char *UNSPEC_print(unsigned char *ptr)
229} 239}
230 240
231/* Display an UNSPEC socket address. */ 241/* Display an UNSPEC socket address. */
232static char *UNSPEC_sprint(struct sockaddr *sap, int numeric) 242static const char *UNSPEC_sprint(struct sockaddr *sap, int numeric)
233{ 243{
234 static char buf[64];
235
236 if (sap->sa_family == 0xFFFF || sap->sa_family == 0) 244 if (sap->sa_family == 0xFFFF || sap->sa_family == 0)
237 return safe_strncpy(buf, "[NONE SET]", sizeof(buf)); 245 return "[NONE SET]";
238 return UNSPEC_print((unsigned char *)sap->sa_data); 246 return UNSPEC_print((unsigned char *)sap->sa_data);
239} 247}
240 248
@@ -809,7 +817,7 @@ static char *pr_ether(unsigned char *ptr)
809 return buff; 817 return buff;
810} 818}
811 819
812static int in_ether(char *bufp, struct sockaddr *sap); 820static int in_ether(const char *bufp, struct sockaddr *sap);
813 821
814static struct hwtype ether_hwtype = { 822static struct hwtype ether_hwtype = {
815 .name = "ether", 823 .name = "ether",
@@ -831,10 +839,10 @@ static unsigned hexchar2int(char c)
831} 839}
832 840
833/* Input an Ethernet address and convert to binary. */ 841/* Input an Ethernet address and convert to binary. */
834static int in_ether(char *bufp, struct sockaddr *sap) 842static int in_ether(const char *bufp, struct sockaddr *sap)
835{ 843{
836 unsigned char *ptr; 844 unsigned char *ptr;
837 char c, *orig; 845 char c;
838 int i; 846 int i;
839 unsigned val; 847 unsigned val;
840 848
@@ -842,7 +850,6 @@ static int in_ether(char *bufp, struct sockaddr *sap)
842 ptr = (unsigned char*) sap->sa_data; 850 ptr = (unsigned char*) sap->sa_data;
843 851
844 i = 0; 852 i = 0;
845 orig = bufp;
846 while ((*bufp != '\0') && (i < ETH_ALEN)) { 853 while ((*bufp != '\0') && (i < ETH_ALEN)) {
847 val = hexchar2int(*bufp++) * 0x10; 854 val = hexchar2int(*bufp++) * 0x10;
848 if (val > 0xff) { 855 if (val > 0xff) {