diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-14 22:11:20 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-03-14 22:11:20 +0000 |
commit | 7f2527e5a7f513b9f415643ddc0ecd236b596887 (patch) | |
tree | 710d52b0a41425e8e82ad3ea5c674b5f61264cf7 /networking/arp.c | |
parent | 650a0459b87314fc412dc81a2a863fefb969ca37 (diff) | |
download | busybox-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.
Diffstat (limited to 'networking/arp.c')
-rw-r--r-- | networking/arp.c | 132 |
1 files changed, 65 insertions, 67 deletions
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 */ |
65 | static int arp_del(char **args) | 63 | static 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 */ |
214 | static int arp_set(char **args) | 212 | static 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 */ |
363 | static int arp_show(char *name) | 361 | static 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 | ||