diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-10-27 11:54:45 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-10-27 11:54:45 +0000 |
commit | 59f502b36ff59e81cf3468c4484dd7c317dd5591 (patch) | |
tree | ee56542483cbcf84abc4554cd7efe8b00c5be009 /networking/ping.c | |
parent | c503dde01a3be1ddabf3a3bcc1d1074457cf5f1a (diff) | |
download | busybox-w32-59f502b36ff59e81cf3468c4484dd7c317dd5591.tar.gz busybox-w32-59f502b36ff59e81cf3468c4484dd7c317dd5591.tar.bz2 busybox-w32-59f502b36ff59e81cf3468c4484dd7c317dd5591.zip |
ping: trivial code shrink
function old new delta
ping6_main 31 30 -1
ping_main 405 370 -35
Diffstat (limited to 'networking/ping.c')
-rw-r--r-- | networking/ping.c | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/networking/ping.c b/networking/ping.c index 7cccfbfdc..01a9f9ac5 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -241,10 +241,11 @@ enum { | |||
241 | struct globals { | 241 | struct globals { |
242 | int pingsock; | 242 | int pingsock; |
243 | int if_index; | 243 | int if_index; |
244 | char *opt_I; | 244 | char *str_I; |
245 | len_and_sockaddr *source_lsa; | 245 | len_and_sockaddr *source_lsa; |
246 | unsigned datalen; | 246 | unsigned datalen; |
247 | unsigned long ntransmitted, nreceived, nrepeats, pingcount; | 247 | unsigned pingcount; /* must be int-sized */ |
248 | unsigned long ntransmitted, nreceived, nrepeats; | ||
248 | uint16_t myid; | 249 | uint16_t myid; |
249 | unsigned tmin, tmax; /* in us */ | 250 | unsigned tmin, tmax; /* in us */ |
250 | unsigned long long tsum; /* in us, sum of all times */ | 251 | unsigned long long tsum; /* in us, sum of all times */ |
@@ -266,7 +267,7 @@ struct globals { | |||
266 | #define pingsock (G.pingsock ) | 267 | #define pingsock (G.pingsock ) |
267 | #define if_index (G.if_index ) | 268 | #define if_index (G.if_index ) |
268 | #define source_lsa (G.source_lsa ) | 269 | #define source_lsa (G.source_lsa ) |
269 | #define opt_I (G.opt_I ) | 270 | #define str_I (G.str_I ) |
270 | #define datalen (G.datalen ) | 271 | #define datalen (G.datalen ) |
271 | #define ntransmitted (G.ntransmitted) | 272 | #define ntransmitted (G.ntransmitted) |
272 | #define nreceived (G.nreceived ) | 273 | #define nreceived (G.nreceived ) |
@@ -570,8 +571,8 @@ static void ping4(len_and_sockaddr *lsa) | |||
570 | bb_error_msg_and_die("can't set multicast source interface"); | 571 | bb_error_msg_and_die("can't set multicast source interface"); |
571 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); | 572 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); |
572 | } | 573 | } |
573 | if (opt_I) | 574 | if (str_I) |
574 | setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, opt_I, strlen(opt_I) + 1); | 575 | setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, str_I, strlen(str_I) + 1); |
575 | 576 | ||
576 | /* enable broadcast pings */ | 577 | /* enable broadcast pings */ |
577 | setsockopt_broadcast(pingsock); | 578 | setsockopt_broadcast(pingsock); |
@@ -620,8 +621,8 @@ static void ping6(len_and_sockaddr *lsa) | |||
620 | /* untested whether "-I addr" really works for IPv6: */ | 621 | /* untested whether "-I addr" really works for IPv6: */ |
621 | if (source_lsa) | 622 | if (source_lsa) |
622 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); | 623 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); |
623 | if (opt_I) | 624 | if (str_I) |
624 | setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, opt_I, strlen(opt_I) + 1); | 625 | setsockopt(pingsock, SOL_SOCKET, SO_BINDTODEVICE, str_I, strlen(str_I) + 1); |
625 | 626 | ||
626 | #ifdef ICMP6_FILTER | 627 | #ifdef ICMP6_FILTER |
627 | { | 628 | { |
@@ -719,34 +720,35 @@ int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | |||
719 | int ping_main(int argc UNUSED_PARAM, char **argv) | 720 | int ping_main(int argc UNUSED_PARAM, char **argv) |
720 | { | 721 | { |
721 | len_and_sockaddr *lsa; | 722 | len_and_sockaddr *lsa; |
722 | char *opt_c, *opt_s; | 723 | char *str_s; |
723 | USE_PING6(sa_family_t af = AF_UNSPEC;) | 724 | int opt; |
724 | 725 | ||
725 | INIT_G(); | 726 | INIT_G(); |
726 | 727 | ||
727 | /* exactly one argument needed; -v and -q don't mix; -w NUM, -W NUM */ | 728 | /* exactly one argument needed; -v and -q don't mix; -c NUM, -w NUM, -W NUM */ |
728 | opt_complementary = "=1:q--v:v--q:w+:W+"; | 729 | opt_complementary = "=1:q--v:v--q:c+:w+:W+"; |
729 | getopt32(argv, OPT_STRING, &opt_c, &opt_s, &deadline, &timeout, &opt_I); | 730 | opt = getopt32(argv, OPT_STRING, &pingcount, &str_s, &deadline, &timeout, &str_I); |
730 | if (option_mask32 & OPT_c) | 731 | if (opt & OPT_s) |
731 | pingcount = xatoul(opt_c); // -c | 732 | datalen = xatou16(str_s); // -s |
732 | if (option_mask32 & OPT_s) | 733 | if (opt & OPT_I) { // -I |
733 | datalen = xatou16(opt_s); // -s | 734 | if_index = if_nametoindex(str_I); |
734 | if (option_mask32 & OPT_I) { // -I | ||
735 | if_index = if_nametoindex(opt_I); | ||
736 | if (!if_index) { | 735 | if (!if_index) { |
737 | /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */ | 736 | /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */ |
738 | source_lsa = xdotted2sockaddr(opt_I, 0); | 737 | source_lsa = xdotted2sockaddr(str_I, 0); |
739 | opt_I = NULL; /* don't try to bind to device later */ | 738 | str_I = NULL; /* don't try to bind to device later */ |
740 | } | 739 | } |
741 | } | 740 | } |
742 | myid = (uint16_t) getpid(); | 741 | myid = (uint16_t) getpid(); |
743 | hostname = argv[optind]; | 742 | hostname = argv[optind]; |
744 | #if ENABLE_PING6 | 743 | #if ENABLE_PING6 |
745 | if (option_mask32 & OPT_IPV4) | 744 | { |
746 | af = AF_INET; | 745 | sa_family_t af = AF_UNSPEC; |
747 | if (option_mask32 & OPT_IPV6) | 746 | if (opt & OPT_IPV4) |
748 | af = AF_INET6; | 747 | af = AF_INET; |
749 | lsa = xhost_and_af2sockaddr(hostname, 0, af); | 748 | if (opt & OPT_IPV6) |
749 | af = AF_INET6; | ||
750 | lsa = xhost_and_af2sockaddr(hostname, 0, af); | ||
751 | } | ||
750 | #else | 752 | #else |
751 | lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET); | 753 | lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET); |
752 | #endif | 754 | #endif |
@@ -765,10 +767,11 @@ int ping_main(int argc UNUSED_PARAM, char **argv) | |||
765 | 767 | ||
766 | #if ENABLE_PING6 | 768 | #if ENABLE_PING6 |
767 | int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 769 | int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
768 | int ping6_main(int argc, char **argv) | 770 | int ping6_main(int argc UNUSED_PARAM, char **argv) |
769 | { | 771 | { |
770 | argv[0] = (char*)"-6"; | 772 | argv[0] = (char*)"-6"; |
771 | return ping_main(argc + 1, argv - 1); | 773 | return ping_main(0 /* argc+1 - but it's unused anyway */, |
774 | argv - 1); | ||
772 | } | 775 | } |
773 | #endif | 776 | #endif |
774 | 777 | ||