diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-13 14:58:11 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-12-13 14:58:11 +0100 |
commit | b4b182807687a872c49a89b840754a553c00f8a1 (patch) | |
tree | 47904a2844d19d8ace84b6b0ae28034ff8758214 | |
parent | 80551731288a6abf9657e07e82d55a23bc5a970e (diff) | |
download | busybox-w32-b4b182807687a872c49a89b840754a553c00f8a1.tar.gz busybox-w32-b4b182807687a872c49a89b840754a553c00f8a1.tar.bz2 busybox-w32-b4b182807687a872c49a89b840754a553c00f8a1.zip |
traceroute: make "ipv4 or ipv6?" cheaper to find out
function old new delta
traceroute_init 1131 1135 +4
hexdump 274 270 -4
common_traceroute_main 1730 1715 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 4/-19) Total: -15 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | networking/traceroute.c | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/networking/traceroute.c b/networking/traceroute.c index 8e13238bf..4d0ebc3b7 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c | |||
@@ -324,7 +324,6 @@ | |||
324 | #ifndef IPPROTO_IP | 324 | #ifndef IPPROTO_IP |
325 | # define IPPROTO_IP 0 | 325 | # define IPPROTO_IP 0 |
326 | #endif | 326 | #endif |
327 | |||
328 | /* Some operating systems, like GNU/Hurd, don't define SOL_RAW, but do have | 327 | /* Some operating systems, like GNU/Hurd, don't define SOL_RAW, but do have |
329 | * IPPROTO_RAW. Since the IPPROTO definitions are also valid to use for | 328 | * IPPROTO_RAW. Since the IPPROTO definitions are also valid to use for |
330 | * setsockopt (and take the same value as their corresponding SOL definitions, | 329 | * setsockopt (and take the same value as their corresponding SOL definitions, |
@@ -391,19 +390,24 @@ struct globals { | |||
391 | struct ip *outip; | 390 | struct ip *outip; |
392 | /* Pointer to ICMP or UDP payload (not header): */ | 391 | /* Pointer to ICMP or UDP payload (not header): */ |
393 | struct outdata_t *outdata; | 392 | struct outdata_t *outdata; |
394 | |||
395 | len_and_sockaddr *dest_lsa; | 393 | len_and_sockaddr *dest_lsa; |
396 | len_and_sockaddr *from_lsa; /* response came from this address */ | 394 | len_and_sockaddr *from_lsa; /* response came from this address */ |
397 | struct sockaddr *to; /* response came to this (local) address */ | 395 | struct sockaddr *to; /* response came to this (local) address */ |
398 | int packlen; /* total length of packet */ | ||
399 | int pmtu; /* Path MTU Discovery (RFC1191) */ | ||
400 | uint32_t ident; | 396 | uint32_t ident; |
401 | uint16_t port; /* start udp dest port # for probe packets */ | 397 | uint16_t port; /* start udp dest port # for probe packets */ |
398 | #if ENABLE_TRACEROUTE6 | ||
399 | smallint ipv6; | ||
400 | # define G_ipv6 G.ipv6 | ||
401 | #else | ||
402 | # define G_ipv6 0 | ||
403 | #endif | ||
404 | int packlen; /* total length of packet */ | ||
405 | int pmtu; /* Path MTU Discovery (RFC1191) */ | ||
402 | int waittime; /* time to wait for response (in seconds) */ | 406 | int waittime; /* time to wait for response (in seconds) */ |
403 | int first_ttl; | 407 | int first_ttl; |
404 | int nprobes; | 408 | int nprobes; |
405 | unsigned pausemsecs; | ||
406 | int max_ttl; | 409 | int max_ttl; |
410 | unsigned pausemsecs; | ||
407 | unsigned char recv_pkt[512]; /* last inbound (icmp) packet */ | 411 | unsigned char recv_pkt[512]; /* last inbound (icmp) packet */ |
408 | }; | 412 | }; |
409 | 413 | ||
@@ -482,7 +486,7 @@ send_probe(int seq, int ttl) | |||
482 | 486 | ||
483 | /* Payload */ | 487 | /* Payload */ |
484 | #if ENABLE_TRACEROUTE6 | 488 | #if ENABLE_TRACEROUTE6 |
485 | if (dest_lsa->u.sa.sa_family == AF_INET6) { | 489 | if (G_ipv6) { |
486 | struct outdata6_t *pkt = (void *) outdata; | 490 | struct outdata6_t *pkt = (void *) outdata; |
487 | pkt->ident6 = ident; | 491 | pkt->ident6 = ident; |
488 | pkt->seq6 = htonl(seq); | 492 | pkt->seq6 = htonl(seq); |
@@ -541,7 +545,7 @@ send_probe(int seq, int ttl) | |||
541 | #endif | 545 | #endif |
542 | 546 | ||
543 | #if ENABLE_TRACEROUTE6 | 547 | #if ENABLE_TRACEROUTE6 |
544 | if (dest_lsa->u.sa.sa_family == AF_INET6) { | 548 | if (G_ipv6) { |
545 | res = setsockopt_int(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, ttl); | 549 | res = setsockopt_int(sndsock, SOL_IPV6, IPV6_UNICAST_HOPS, ttl); |
546 | if (res != 0) | 550 | if (res != 0) |
547 | bb_perror_msg_and_die("setsockopt(%s) %d", "UNICAST_HOPS", ttl); | 551 | bb_perror_msg_and_die("setsockopt(%s) %d", "UNICAST_HOPS", ttl); |
@@ -585,7 +589,7 @@ pr_type(unsigned char t) | |||
585 | [16] = "Neighbor Advert", "Redirect", | 589 | [16] = "Neighbor Advert", "Redirect", |
586 | }; | 590 | }; |
587 | 591 | ||
588 | if (dest_lsa->u.sa.sa_family == AF_INET6) { | 592 | if (G_ipv6) { |
589 | if (t < 5) | 593 | if (t < 5) |
590 | return ttab6[t]; | 594 | return ttab6[t]; |
591 | if (t < 128 || t > ND_REDIRECT) | 595 | if (t < 128 || t > ND_REDIRECT) |
@@ -772,7 +776,7 @@ packet6_ok(int read_len, int seq) | |||
772 | static int | 776 | static int |
773 | packet_ok(int read_len, int seq) | 777 | packet_ok(int read_len, int seq) |
774 | { | 778 | { |
775 | if (G.from_lsa->u.sa.sa_family == AF_INET) | 779 | if (!G_ipv6) |
776 | return packet4_ok(read_len, seq); | 780 | return packet4_ok(read_len, seq); |
777 | return packet6_ok(read_len, seq); | 781 | return packet6_ok(read_len, seq); |
778 | } | 782 | } |
@@ -792,10 +796,10 @@ print(int read_len) | |||
792 | printf(" %s", ina); | 796 | printf(" %s", ina); |
793 | } else { | 797 | } else { |
794 | char *n = NULL; | 798 | char *n = NULL; |
795 | if (G.from_lsa->u.sa.sa_family != AF_INET | 799 | if (G_ipv6 |
796 | || G.from_lsa->u.sin.sin_addr.s_addr != INADDR_ANY | 800 | || G.from_lsa->u.sin.sin_addr.s_addr != INADDR_ANY |
797 | ) { | 801 | ) { |
798 | /* Try to reverse resolve if it is not 0.0.0.0 */ | 802 | /* Reverse resolve if IPV6 or not 0.0.0.0 */ |
799 | n = auto_string(xmalloc_sockaddr2host_noport(&G.from_lsa->u.sa)); | 803 | n = auto_string(xmalloc_sockaddr2host_noport(&G.from_lsa->u.sa)); |
800 | } | 804 | } |
801 | printf(" %s (%s)", (n ? n : ina), ina); | 805 | printf(" %s (%s)", (n ? n : ina), ina); |
@@ -808,7 +812,7 @@ print(int read_len) | |||
808 | * Reads from (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6) do strip IPv6 | 812 | * Reads from (AF_INET6, SOCK_RAW, IPPROTO_ICMPV6) do strip IPv6 |
809 | * header and return only ICMP6 packet. Weird. | 813 | * header and return only ICMP6 packet. Weird. |
810 | */ | 814 | */ |
811 | if (G.from_lsa->u.sa.sa_family == AF_INET6) { | 815 | if (G_ipv6) { |
812 | /* read_len -= sizeof(struct ip6_hdr); - WRONG! */ | 816 | /* read_len -= sizeof(struct ip6_hdr); - WRONG! */ |
813 | } else | 817 | } else |
814 | #endif | 818 | #endif |
@@ -904,7 +908,9 @@ traceroute_init(int op, char **argv) | |||
904 | af = AF_INET6; | 908 | af = AF_INET6; |
905 | dest_lsa = xhost_and_af2sockaddr(argv[0], port, af); | 909 | dest_lsa = xhost_and_af2sockaddr(argv[0], port, af); |
906 | af = dest_lsa->u.sa.sa_family; | 910 | af = dest_lsa->u.sa.sa_family; |
911 | //TODO: make sure af == AF_INET[6]? (FEATURE_UNIX_LOCAL=y allows "local:/PATH" to be translated to AF_UNIX) | ||
907 | if (af == AF_INET6) { | 912 | if (af == AF_INET6) { |
913 | G_ipv6 = 1; | ||
908 | packlen = sizeof(struct ip6_hdr) | 914 | packlen = sizeof(struct ip6_hdr) |
909 | + sizeof(struct udphdr) | 915 | + sizeof(struct udphdr) |
910 | + sizeof(struct outdata6_t); | 916 | + sizeof(struct outdata6_t); |
@@ -914,9 +920,9 @@ traceroute_init(int op, char **argv) | |||
914 | + sizeof(struct outdata6_t); | 920 | + sizeof(struct outdata6_t); |
915 | } | 921 | } |
916 | #else | 922 | #else |
917 | dest_lsa = xhost2sockaddr(argv[0], port); | 923 | /* accept only IPv4 addresses */ |
924 | dest_lsa = xhost_and_af2sockaddr(argv[0], port, AF_INET); | ||
918 | #endif | 925 | #endif |
919 | //TODO: make sure af == AF_INET[6]? (FEATURE_UNIX_LOCAL=y allows "local:/PATH" to be translated to AF_UNIX) | ||
920 | G.from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len); | 926 | G.from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len); |
921 | G.to = xzalloc(dest_lsa->len); | 927 | G.to = xzalloc(dest_lsa->len); |
922 | if (argv[1]) | 928 | if (argv[1]) |
@@ -1117,7 +1123,7 @@ common_traceroute_main(int op, char **argv) | |||
1117 | memcpy(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len); | 1123 | memcpy(lastaddr, &G.from_lsa->u.sa, G.from_lsa->len); |
1118 | } | 1124 | } |
1119 | print_delta_ms(t1, t2); | 1125 | print_delta_ms(t1, t2); |
1120 | if (G.from_lsa->u.sa.sa_family == AF_INET) { | 1126 | if (!G_ipv6) { |
1121 | if (op & OPT_TTL_FLAG) { | 1127 | if (op & OPT_TTL_FLAG) { |
1122 | struct ip *ip = (struct ip *)recv_pkt; | 1128 | struct ip *ip = (struct ip *)recv_pkt; |
1123 | printf(" (%d)", ip->ip_ttl); | 1129 | printf(" (%d)", ip->ip_ttl); |