diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-11 21:16:24 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2018-02-11 21:16:24 +0100 |
commit | b5257a670196d3fee6b1307adce62c68bb3eb4fc (patch) | |
tree | 02ca0c86077ad9aa17a45a15a36442410c45374f | |
parent | ba3b9dbf065438402d89655d7baefb0ccc6f0663 (diff) | |
download | busybox-w32-b5257a670196d3fee6b1307adce62c68bb3eb4fc.tar.gz busybox-w32-b5257a670196d3fee6b1307adce62c68bb3eb4fc.tar.bz2 busybox-w32-b5257a670196d3fee6b1307adce62c68bb3eb4fc.zip |
arping: code shrink
function old new delta
catcher 310 309 -1
arping_main 1668 1641 -27
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/arping.c | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/networking/arping.c b/networking/arping.c index 59092a7d7..788fded3c 100644 --- a/networking/arping.c +++ b/networking/arping.c | |||
@@ -28,6 +28,7 @@ | |||
28 | //usage: "\n -A ARP answer mode, update your neighbors" | 28 | //usage: "\n -A ARP answer mode, update your neighbors" |
29 | //usage: "\n -c N Stop after sending N ARP requests" | 29 | //usage: "\n -c N Stop after sending N ARP requests" |
30 | //usage: "\n -w TIMEOUT Seconds to wait for ARP reply" | 30 | //usage: "\n -w TIMEOUT Seconds to wait for ARP reply" |
31 | //NB: in iputils-s20160308, iface is mandatory, no default | ||
31 | //usage: "\n -I IFACE Interface to use (default eth0)" | 32 | //usage: "\n -I IFACE Interface to use (default eth0)" |
32 | //usage: "\n -s SRC_IP Sender IP address" | 33 | //usage: "\n -s SRC_IP Sender IP address" |
33 | //usage: "\n DST_IP Target IP address" | 34 | //usage: "\n DST_IP Target IP address" |
@@ -56,8 +57,8 @@ enum { | |||
56 | #define GETOPT32(str_timeout, device, source) \ | 57 | #define GETOPT32(str_timeout, device, source) \ |
57 | getopt32(argv, "^" \ | 58 | getopt32(argv, "^" \ |
58 | "UDAqfbc:+w:I:s:" \ | 59 | "UDAqfbc:+w:I:s:" \ |
59 | /* Dad also sets quit_on_reply, */ \ | 60 | /* DAD also sets quit_on_reply, */ \ |
60 | /* Advert also sets unsolicited: */ \ | 61 | /* advert also sets unsolicited: */ \ |
61 | "\0" "=1:Df:AU", \ | 62 | "\0" "=1:Df:AU", \ |
62 | &count, &str_timeout, &device, &source \ | 63 | &count, &str_timeout, &device, &source \ |
63 | ); | 64 | ); |
@@ -67,7 +68,6 @@ struct globals { | |||
67 | struct in_addr dst; | 68 | struct in_addr dst; |
68 | struct sockaddr_ll me; | 69 | struct sockaddr_ll me; |
69 | struct sockaddr_ll he; | 70 | struct sockaddr_ll he; |
70 | int sock_fd; | ||
71 | 71 | ||
72 | int count; // = -1; | 72 | int count; // = -1; |
73 | unsigned last; | 73 | unsigned last; |
@@ -80,7 +80,9 @@ struct globals { | |||
80 | unsigned brd_recv; | 80 | unsigned brd_recv; |
81 | unsigned req_recv; | 81 | unsigned req_recv; |
82 | 82 | ||
83 | /* should be in main(), but are here to reduce stack use: */ | ||
83 | struct ifreq ifr; | 84 | struct ifreq ifr; |
85 | struct sockaddr_in probe_saddr; | ||
84 | sigset_t sset; | 86 | sigset_t sset; |
85 | unsigned char packet[4096]; | 87 | unsigned char packet[4096]; |
86 | } FIX_ALIASING; | 88 | } FIX_ALIASING; |
@@ -88,7 +90,6 @@ struct globals { | |||
88 | #define dst (G.dst ) | 90 | #define dst (G.dst ) |
89 | #define me (G.me ) | 91 | #define me (G.me ) |
90 | #define he (G.he ) | 92 | #define he (G.he ) |
91 | #define sock_fd (G.sock_fd ) | ||
92 | #define count (G.count ) | 93 | #define count (G.count ) |
93 | #define last (G.last ) | 94 | #define last (G.last ) |
94 | #define timeout_us (G.timeout_us) | 95 | #define timeout_us (G.timeout_us) |
@@ -106,6 +107,8 @@ struct globals { | |||
106 | count = -1; \ | 107 | count = -1; \ |
107 | } while (0) | 108 | } while (0) |
108 | 109 | ||
110 | #define sock_fd 3 | ||
111 | |||
109 | static int send_pack(struct in_addr *src_addr, | 112 | static int send_pack(struct in_addr *src_addr, |
110 | struct in_addr *dst_addr, | 113 | struct in_addr *dst_addr, |
111 | struct sockaddr_ll *ME, | 114 | struct sockaddr_ll *ME, |
@@ -299,7 +302,7 @@ int arping_main(int argc UNUSED_PARAM, char **argv) | |||
299 | 302 | ||
300 | INIT_G(); | 303 | INIT_G(); |
301 | 304 | ||
302 | sock_fd = xsocket(AF_PACKET, SOCK_DGRAM, 0); | 305 | xmove_fd(xsocket(AF_PACKET, SOCK_DGRAM, 0), sock_fd); |
303 | 306 | ||
304 | // If you ever change BB_SUID_DROP to BB_SUID_REQUIRE, | 307 | // If you ever change BB_SUID_DROP to BB_SUID_REQUIRE, |
305 | // drop suid root privileges here: | 308 | // drop suid root privileges here: |
@@ -351,31 +354,29 @@ int arping_main(int argc UNUSED_PARAM, char **argv) | |||
351 | src = dst; | 354 | src = dst; |
352 | 355 | ||
353 | if (!(option_mask32 & DAD) || src.s_addr) { | 356 | if (!(option_mask32 & DAD) || src.s_addr) { |
354 | struct sockaddr_in saddr; | 357 | /*struct sockaddr_in probe_saddr;*/ |
355 | int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); | 358 | int probe_fd = xsocket(AF_INET, SOCK_DGRAM, 0); |
356 | 359 | ||
357 | setsockopt_bindtodevice(probe_fd, device); | 360 | setsockopt_bindtodevice(probe_fd, device); |
358 | memset(&saddr, 0, sizeof(saddr)); | 361 | |
359 | saddr.sin_family = AF_INET; | 362 | /*memset(&G.probe_saddr, 0, sizeof(G.probe_saddr)); - zeroed by INIT_G */ |
363 | G.probe_saddr.sin_family = AF_INET; | ||
360 | if (src.s_addr) { | 364 | if (src.s_addr) { |
361 | /* Check that this is indeed our IP */ | 365 | /* Check that this is indeed our IP */ |
362 | saddr.sin_addr = src; | 366 | G.probe_saddr.sin_addr = src; |
363 | xbind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); | 367 | xbind(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr)); |
364 | } else { /* !(option_mask32 & DAD) case */ | 368 | } else { /* !(option_mask32 & DAD) case */ |
365 | /* Find IP address on this iface */ | 369 | /* Find IP address on this iface */ |
366 | saddr.sin_port = htons(1025); | 370 | G.probe_saddr.sin_port = htons(1025); |
367 | saddr.sin_addr = dst; | 371 | G.probe_saddr.sin_addr = dst; |
368 | 372 | ||
369 | if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0) | 373 | if (setsockopt_SOL_SOCKET_1(probe_fd, SO_DONTROUTE) != 0) |
370 | bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE"); | 374 | bb_perror_msg("setsockopt(%s)", "SO_DONTROUTE"); |
371 | xconnect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); | 375 | xconnect(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr)); |
372 | bb_getsockname(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)); | 376 | bb_getsockname(probe_fd, (struct sockaddr *) &G.probe_saddr, sizeof(G.probe_saddr)); |
373 | //never happens: | 377 | if (G.probe_saddr.sin_family != AF_INET) |
374 | //if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == -1) | ||
375 | // bb_perror_msg_and_die("getsockname"); | ||
376 | if (saddr.sin_family != AF_INET) | ||
377 | bb_error_msg_and_die("no IP address configured"); | 378 | bb_error_msg_and_die("no IP address configured"); |
378 | src = saddr.sin_addr; | 379 | src = G.probe_saddr.sin_addr; |
379 | } | 380 | } |
380 | close(probe_fd); | 381 | close(probe_fd); |
381 | } | 382 | } |