diff options
Diffstat (limited to 'networking/ping.c')
-rw-r--r-- | networking/ping.c | 105 |
1 files changed, 63 insertions, 42 deletions
diff --git a/networking/ping.c b/networking/ping.c index d1d59d545..8f85d3ec2 100644 --- a/networking/ping.c +++ b/networking/ping.c | |||
@@ -74,6 +74,7 @@ | |||
74 | //usage: ) | 74 | //usage: ) |
75 | //usage: "\n -c CNT Send only CNT pings" | 75 | //usage: "\n -c CNT Send only CNT pings" |
76 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)" | 76 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)" |
77 | //usage: "\n -A Ping as soon as reply is recevied" | ||
77 | //usage: "\n -t TTL Set TTL" | 78 | //usage: "\n -t TTL Set TTL" |
78 | //usage: "\n -I IFACE/IP Source interface or IP address" | 79 | //usage: "\n -I IFACE/IP Source interface or IP address" |
79 | //usage: "\n -W SEC Seconds to wait for the first response (default 10)" | 80 | //usage: "\n -W SEC Seconds to wait for the first response (default 10)" |
@@ -90,6 +91,7 @@ | |||
90 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" | 91 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" |
91 | //usage: "\n -c CNT Send only CNT pings" | 92 | //usage: "\n -c CNT Send only CNT pings" |
92 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)" | 93 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default 56)" |
94 | //usage: "\n -A Ping as soon as reply is recevied" | ||
93 | //usage: "\n -I IFACE/IP Source interface or IP address" | 95 | //usage: "\n -I IFACE/IP Source interface or IP address" |
94 | //usage: "\n -q Quiet, only display output at start" | 96 | //usage: "\n -q Quiet, only display output at start" |
95 | //usage: "\n and when finished" | 97 | //usage: "\n and when finished" |
@@ -348,20 +350,21 @@ static int common_ping_main(sa_family_t af, char **argv) | |||
348 | /* Full(er) version */ | 350 | /* Full(er) version */ |
349 | 351 | ||
350 | /* -c NUM, -t NUM, -w NUM, -W NUM */ | 352 | /* -c NUM, -t NUM, -w NUM, -W NUM */ |
351 | #define OPT_STRING "qvc:+s:t:+w:+W:+I:np:4"IF_PING6("6") | 353 | #define OPT_STRING "qvAc:+s:t:+w:+W:+I:np:4"IF_PING6("6") |
352 | enum { | 354 | enum { |
353 | OPT_QUIET = 1 << 0, | 355 | OPT_QUIET = 1 << 0, |
354 | OPT_VERBOSE = 1 << 1, | 356 | OPT_VERBOSE = 1 << 1, |
355 | OPT_c = 1 << 2, | 357 | OPT_A = 1 << 2, |
356 | OPT_s = 1 << 3, | 358 | OPT_c = 1 << 3, |
357 | OPT_t = 1 << 4, | 359 | OPT_s = 1 << 4, |
358 | OPT_w = 1 << 5, | 360 | OPT_t = 1 << 5, |
359 | OPT_W = 1 << 6, | 361 | OPT_w = 1 << 6, |
360 | OPT_I = 1 << 7, | 362 | OPT_W = 1 << 7, |
361 | /*OPT_n = 1 << 8, - ignored */ | 363 | OPT_I = 1 << 8, |
362 | OPT_p = 1 << 9, | 364 | /*OPT_n = 1 << 9, - ignored */ |
363 | OPT_IPV4 = 1 << 10, | 365 | OPT_p = 1 << 10, |
364 | OPT_IPV6 = (1 << 11) * ENABLE_PING6, | 366 | OPT_IPV4 = 1 << 11, |
367 | OPT_IPV6 = (1 << 12) * ENABLE_PING6, | ||
365 | }; | 368 | }; |
366 | 369 | ||
367 | 370 | ||
@@ -377,9 +380,9 @@ struct globals { | |||
377 | uint8_t pattern; | 380 | uint8_t pattern; |
378 | unsigned tmin, tmax; /* in us */ | 381 | unsigned tmin, tmax; /* in us */ |
379 | unsigned long long tsum; /* in us, sum of all times */ | 382 | unsigned long long tsum; /* in us, sum of all times */ |
380 | unsigned deadline; | 383 | unsigned cur_us; /* low word only, we don't need more */ |
384 | unsigned deadline_us; | ||
381 | unsigned timeout; | 385 | unsigned timeout; |
382 | unsigned total_secs; | ||
383 | unsigned sizeof_rcv_packet; | 386 | unsigned sizeof_rcv_packet; |
384 | char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */ | 387 | char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */ |
385 | void *snd_packet; /* [datalen + ipv4/ipv6_const] */ | 388 | void *snd_packet; /* [datalen + ipv4/ipv6_const] */ |
@@ -405,9 +408,7 @@ struct globals { | |||
405 | #define tmin (G.tmin ) | 408 | #define tmin (G.tmin ) |
406 | #define tmax (G.tmax ) | 409 | #define tmax (G.tmax ) |
407 | #define tsum (G.tsum ) | 410 | #define tsum (G.tsum ) |
408 | #define deadline (G.deadline ) | ||
409 | #define timeout (G.timeout ) | 411 | #define timeout (G.timeout ) |
410 | #define total_secs (G.total_secs ) | ||
411 | #define hostname (G.hostname ) | 412 | #define hostname (G.hostname ) |
412 | #define dotted (G.dotted ) | 413 | #define dotted (G.dotted ) |
413 | #define pingaddr (G.pingaddr ) | 414 | #define pingaddr (G.pingaddr ) |
@@ -455,7 +456,7 @@ static void print_stats_and_exit(int junk UNUSED_PARAM) | |||
455 | tmax / 1000, tmax % 1000); | 456 | tmax / 1000, tmax % 1000); |
456 | } | 457 | } |
457 | /* if condition is true, exit with 1 -- 'failure' */ | 458 | /* if condition is true, exit with 1 -- 'failure' */ |
458 | exit(nrecv == 0 || (deadline && nrecv < pingcount)); | 459 | exit(nrecv == 0 || (G.deadline_us && nrecv < pingcount)); |
459 | } | 460 | } |
460 | 461 | ||
461 | static void sendping_tail(void (*sp)(int), int size_pkt) | 462 | static void sendping_tail(void (*sp)(int), int size_pkt) |
@@ -467,22 +468,23 @@ static void sendping_tail(void (*sp)(int), int size_pkt) | |||
467 | 468 | ||
468 | size_pkt += datalen; | 469 | size_pkt += datalen; |
469 | 470 | ||
471 | if (G.deadline_us) { | ||
472 | unsigned n = G.cur_us - G.deadline_us; | ||
473 | if ((int)n >= 0) | ||
474 | print_stats_and_exit(0); | ||
475 | } | ||
476 | |||
470 | /* sizeof(pingaddr) can be larger than real sa size, but I think | 477 | /* sizeof(pingaddr) can be larger than real sa size, but I think |
471 | * it doesn't matter */ | 478 | * it doesn't matter */ |
472 | sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr)); | 479 | sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr)); |
473 | if (sz != size_pkt) | 480 | if (sz != size_pkt) |
474 | bb_error_msg_and_die(bb_msg_write_error); | 481 | bb_error_msg_and_die(bb_msg_write_error); |
475 | 482 | ||
476 | if (pingcount == 0 || deadline || G.ntransmitted < pingcount) { | 483 | if (pingcount == 0 || G.ntransmitted < pingcount) { |
477 | /* Didn't send all pings yet - schedule next in 1s */ | 484 | /* Didn't send all pings yet - schedule next in 1s */ |
478 | signal(SIGALRM, sp); | 485 | signal(SIGALRM, sp); |
479 | if (deadline) { | ||
480 | total_secs += PINGINTERVAL; | ||
481 | if (total_secs >= deadline) | ||
482 | signal(SIGALRM, print_stats_and_exit); | ||
483 | } | ||
484 | alarm(PINGINTERVAL); | 486 | alarm(PINGINTERVAL); |
485 | } else { /* -c NN, and all NN are sent (and no deadline) */ | 487 | } else { /* -c NN, and all NN are sent */ |
486 | /* Wait for the last ping to come back. | 488 | /* Wait for the last ping to come back. |
487 | * -W timeout: wait for a response in seconds. | 489 | * -W timeout: wait for a response in seconds. |
488 | * Affects only timeout in absence of any responses, | 490 | * Affects only timeout in absence of any responses, |
@@ -516,7 +518,7 @@ static void sendping4(int junk UNUSED_PARAM) | |||
516 | */ | 518 | */ |
517 | /*if (datalen >= 4)*/ | 519 | /*if (datalen >= 4)*/ |
518 | /* No hton: we'll read it back on the same machine */ | 520 | /* No hton: we'll read it back on the same machine */ |
519 | *(uint32_t*)&pkt->icmp_dun = monotonic_us(); | 521 | *(uint32_t*)&pkt->icmp_dun = G.cur_us = monotonic_us(); |
520 | 522 | ||
521 | pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN); | 523 | pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN); |
522 | 524 | ||
@@ -535,7 +537,7 @@ static void sendping6(int junk UNUSED_PARAM) | |||
535 | pkt->icmp6_id = myid; | 537 | pkt->icmp6_id = myid; |
536 | 538 | ||
537 | /*if (datalen >= 4)*/ | 539 | /*if (datalen >= 4)*/ |
538 | *(bb__aliased_uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); | 540 | *(bb__aliased_uint32_t*)(&pkt->icmp6_data8[4]) = G.cur_us = monotonic_us(); |
539 | 541 | ||
540 | //TODO? pkt->icmp_cksum = inet_cksum(...); | 542 | //TODO? pkt->icmp_cksum = inet_cksum(...); |
541 | 543 | ||
@@ -632,7 +634,7 @@ static void unpack_tail(int sz, uint32_t *tp, | |||
632 | puts(dupmsg); | 634 | puts(dupmsg); |
633 | fflush_all(); | 635 | fflush_all(); |
634 | } | 636 | } |
635 | static void unpack4(char *buf, int sz, struct sockaddr_in *from) | 637 | static int unpack4(char *buf, int sz, struct sockaddr_in *from) |
636 | { | 638 | { |
637 | struct icmp *icmppkt; | 639 | struct icmp *icmppkt; |
638 | struct iphdr *iphdr; | 640 | struct iphdr *iphdr; |
@@ -640,7 +642,7 @@ static void unpack4(char *buf, int sz, struct sockaddr_in *from) | |||
640 | 642 | ||
641 | /* discard if too short */ | 643 | /* discard if too short */ |
642 | if (sz < (datalen + ICMP_MINLEN)) | 644 | if (sz < (datalen + ICMP_MINLEN)) |
643 | return; | 645 | return 0; |
644 | 646 | ||
645 | /* check IP header */ | 647 | /* check IP header */ |
646 | iphdr = (struct iphdr *) buf; | 648 | iphdr = (struct iphdr *) buf; |
@@ -648,7 +650,7 @@ static void unpack4(char *buf, int sz, struct sockaddr_in *from) | |||
648 | sz -= hlen; | 650 | sz -= hlen; |
649 | icmppkt = (struct icmp *) (buf + hlen); | 651 | icmppkt = (struct icmp *) (buf + hlen); |
650 | if (icmppkt->icmp_id != myid) | 652 | if (icmppkt->icmp_id != myid) |
651 | return; /* not our ping */ | 653 | return 0; /* not our ping */ |
652 | 654 | ||
653 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { | 655 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
654 | uint16_t recv_seq = ntohs(icmppkt->icmp_seq); | 656 | uint16_t recv_seq = ntohs(icmppkt->icmp_seq); |
@@ -659,25 +661,28 @@ static void unpack4(char *buf, int sz, struct sockaddr_in *from) | |||
659 | unpack_tail(sz, tp, | 661 | unpack_tail(sz, tp, |
660 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), | 662 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
661 | recv_seq, iphdr->ttl); | 663 | recv_seq, iphdr->ttl); |
662 | } else if (icmppkt->icmp_type != ICMP_ECHO) { | 664 | return 1; |
665 | } | ||
666 | if (icmppkt->icmp_type != ICMP_ECHO) { | ||
663 | bb_error_msg("warning: got ICMP %d (%s)", | 667 | bb_error_msg("warning: got ICMP %d (%s)", |
664 | icmppkt->icmp_type, | 668 | icmppkt->icmp_type, |
665 | icmp_type_name(icmppkt->icmp_type)); | 669 | icmp_type_name(icmppkt->icmp_type)); |
666 | } | 670 | } |
671 | return 0; | ||
667 | } | 672 | } |
668 | #if ENABLE_PING6 | 673 | #if ENABLE_PING6 |
669 | static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit) | 674 | static int unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit) |
670 | { | 675 | { |
671 | struct icmp6_hdr *icmppkt; | 676 | struct icmp6_hdr *icmppkt; |
672 | char buf[INET6_ADDRSTRLEN]; | 677 | char buf[INET6_ADDRSTRLEN]; |
673 | 678 | ||
674 | /* discard if too short */ | 679 | /* discard if too short */ |
675 | if (sz < (datalen + sizeof(struct icmp6_hdr))) | 680 | if (sz < (datalen + sizeof(struct icmp6_hdr))) |
676 | return; | 681 | return 0; |
677 | 682 | ||
678 | icmppkt = (struct icmp6_hdr *) packet; | 683 | icmppkt = (struct icmp6_hdr *) packet; |
679 | if (icmppkt->icmp6_id != myid) | 684 | if (icmppkt->icmp6_id != myid) |
680 | return; /* not our ping */ | 685 | return 0; /* not our ping */ |
681 | 686 | ||
682 | if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { | 687 | if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { |
683 | uint16_t recv_seq = ntohs(icmppkt->icmp6_seq); | 688 | uint16_t recv_seq = ntohs(icmppkt->icmp6_seq); |
@@ -689,11 +694,14 @@ static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimi | |||
689 | inet_ntop(AF_INET6, &from->sin6_addr, | 694 | inet_ntop(AF_INET6, &from->sin6_addr, |
690 | buf, sizeof(buf)), | 695 | buf, sizeof(buf)), |
691 | recv_seq, hoplimit); | 696 | recv_seq, hoplimit); |
692 | } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) { | 697 | return 1; |
698 | } | ||
699 | if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) { | ||
693 | bb_error_msg("warning: got ICMP %d (%s)", | 700 | bb_error_msg("warning: got ICMP %d (%s)", |
694 | icmppkt->icmp6_type, | 701 | icmppkt->icmp6_type, |
695 | icmp6_type_name(icmppkt->icmp6_type)); | 702 | icmp6_type_name(icmppkt->icmp6_type)); |
696 | } | 703 | } |
704 | return 0; | ||
697 | } | 705 | } |
698 | #endif | 706 | #endif |
699 | 707 | ||
@@ -726,6 +734,7 @@ static void ping4(len_and_sockaddr *lsa) | |||
726 | signal(SIGINT, print_stats_and_exit); | 734 | signal(SIGINT, print_stats_and_exit); |
727 | 735 | ||
728 | /* start the ping's going ... */ | 736 | /* start the ping's going ... */ |
737 | send_ping: | ||
729 | sendping4(0); | 738 | sendping4(0); |
730 | 739 | ||
731 | /* listen for replies */ | 740 | /* listen for replies */ |
@@ -741,9 +750,12 @@ static void ping4(len_and_sockaddr *lsa) | |||
741 | bb_perror_msg("recvfrom"); | 750 | bb_perror_msg("recvfrom"); |
742 | continue; | 751 | continue; |
743 | } | 752 | } |
744 | unpack4(G.rcv_packet, c, &from); | 753 | c = unpack4(G.rcv_packet, c, &from); |
745 | if (pingcount && G.nreceived >= pingcount) | 754 | if (pingcount && G.nreceived >= pingcount) |
746 | break; | 755 | break; |
756 | if (c && (option_mask32 & OPT_A)) { | ||
757 | goto send_ping; | ||
758 | } | ||
747 | } | 759 | } |
748 | } | 760 | } |
749 | #if ENABLE_PING6 | 761 | #if ENABLE_PING6 |
@@ -794,10 +806,6 @@ static void ping6(len_and_sockaddr *lsa) | |||
794 | 806 | ||
795 | signal(SIGINT, print_stats_and_exit); | 807 | signal(SIGINT, print_stats_and_exit); |
796 | 808 | ||
797 | /* start the ping's going ... */ | ||
798 | sendping6(0); | ||
799 | |||
800 | /* listen for replies */ | ||
801 | msg.msg_name = &from; | 809 | msg.msg_name = &from; |
802 | msg.msg_namelen = sizeof(from); | 810 | msg.msg_namelen = sizeof(from); |
803 | msg.msg_iov = &iov; | 811 | msg.msg_iov = &iov; |
@@ -805,12 +813,18 @@ static void ping6(len_and_sockaddr *lsa) | |||
805 | msg.msg_control = control_buf; | 813 | msg.msg_control = control_buf; |
806 | iov.iov_base = G.rcv_packet; | 814 | iov.iov_base = G.rcv_packet; |
807 | iov.iov_len = G.sizeof_rcv_packet; | 815 | iov.iov_len = G.sizeof_rcv_packet; |
816 | |||
817 | /* start the ping's going ... */ | ||
818 | send_ping: | ||
819 | sendping6(0); | ||
820 | |||
821 | /* listen for replies */ | ||
808 | while (1) { | 822 | while (1) { |
809 | int c; | 823 | int c; |
810 | struct cmsghdr *mp; | 824 | struct cmsghdr *mp; |
811 | int hoplimit = -1; | 825 | int hoplimit = -1; |
812 | msg.msg_controllen = sizeof(control_buf); | ||
813 | 826 | ||
827 | msg.msg_controllen = sizeof(control_buf); | ||
814 | c = recvmsg(pingsock, &msg, 0); | 828 | c = recvmsg(pingsock, &msg, 0); |
815 | if (c < 0) { | 829 | if (c < 0) { |
816 | if (errno != EINTR) | 830 | if (errno != EINTR) |
@@ -827,9 +841,12 @@ static void ping6(len_and_sockaddr *lsa) | |||
827 | move_from_unaligned_int(hoplimit, CMSG_DATA(mp)); | 841 | move_from_unaligned_int(hoplimit, CMSG_DATA(mp)); |
828 | } | 842 | } |
829 | } | 843 | } |
830 | unpack6(G.rcv_packet, c, &from, hoplimit); | 844 | c = unpack6(G.rcv_packet, c, &from, hoplimit); |
831 | if (pingcount && G.nreceived >= pingcount) | 845 | if (pingcount && G.nreceived >= pingcount) |
832 | break; | 846 | break; |
847 | if (c && (option_mask32 & OPT_A)) { | ||
848 | goto send_ping; | ||
849 | } | ||
833 | } | 850 | } |
834 | } | 851 | } |
835 | #endif | 852 | #endif |
@@ -875,7 +892,7 @@ static int common_ping_main(int opt, char **argv) | |||
875 | OPT_STRING | 892 | OPT_STRING |
876 | /* exactly one arg; -v and -q don't mix */ | 893 | /* exactly one arg; -v and -q don't mix */ |
877 | "\0" "=1:q--v:v--q", | 894 | "\0" "=1:q--v:v--q", |
878 | &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I, &str_p | 895 | &pingcount, &str_s, &opt_ttl, &G.deadline_us, &timeout, &str_I, &str_p |
879 | ); | 896 | ); |
880 | if (opt & OPT_s) | 897 | if (opt & OPT_s) |
881 | datalen = xatou16(str_s); // -s | 898 | datalen = xatou16(str_s); // -s |
@@ -889,6 +906,10 @@ static int common_ping_main(int opt, char **argv) | |||
889 | } | 906 | } |
890 | if (opt & OPT_p) | 907 | if (opt & OPT_p) |
891 | G.pattern = xstrtou_range(str_p, 16, 0, 255); | 908 | G.pattern = xstrtou_range(str_p, 16, 0, 255); |
909 | if (G.deadline_us) { | ||
910 | unsigned d = G.deadline_us < INT_MAX/1000000 ? G.deadline_us : INT_MAX/1000000; | ||
911 | G.deadline_us = 1 | ((d * 1000000) + monotonic_us()); | ||
912 | } | ||
892 | 913 | ||
893 | myid = (uint16_t) getpid(); | 914 | myid = (uint16_t) getpid(); |
894 | hostname = argv[optind]; | 915 | hostname = argv[optind]; |
@@ -911,7 +932,7 @@ static int common_ping_main(int opt, char **argv) | |||
911 | 932 | ||
912 | dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); | 933 | dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); |
913 | ping(lsa); | 934 | ping(lsa); |
914 | print_stats_and_exit(EXIT_SUCCESS); | 935 | print_stats_and_exit(0); |
915 | /*return EXIT_SUCCESS;*/ | 936 | /*return EXIT_SUCCESS;*/ |
916 | } | 937 | } |
917 | #endif /* FEATURE_FANCY_PING */ | 938 | #endif /* FEATURE_FANCY_PING */ |