Changeset 3232 in MondoRescue for branches/3.2/mindi-busybox/networking/ping.c
- Timestamp:
- Jan 1, 2014, 12:47:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3.2/mindi-busybox/networking/ping.c
r2725 r3232 29 29 #include <netinet/ip_icmp.h> 30 30 #include "libbb.h" 31 32 #ifdef __BIONIC__ 33 /* should be in netinet/ip_icmp.h */ 34 # define ICMP_DEST_UNREACH 3 /* Destination Unreachable */ 35 # define ICMP_SOURCE_QUENCH 4 /* Source Quench */ 36 # define ICMP_REDIRECT 5 /* Redirect (change route) */ 37 # define ICMP_ECHO 8 /* Echo Request */ 38 # define ICMP_TIME_EXCEEDED 11 /* Time Exceeded */ 39 # define ICMP_PARAMETERPROB 12 /* Parameter Problem */ 40 # define ICMP_TIMESTAMP 13 /* Timestamp Request */ 41 # define ICMP_TIMESTAMPREPLY 14 /* Timestamp Reply */ 42 # define ICMP_INFO_REQUEST 15 /* Information Request */ 43 # define ICMP_INFO_REPLY 16 /* Information Reply */ 44 # define ICMP_ADDRESS 17 /* Address Mask Request */ 45 # define ICMP_ADDRESSREPLY 18 /* Address Mask Reply */ 46 #endif 47 48 //config:config PING 49 //config: bool "ping" 50 //config: default y 51 //config: select PLATFORM_LINUX 52 //config: help 53 //config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to 54 //config: elicit an ICMP ECHO_RESPONSE from a host or gateway. 55 //config: 56 //config:config PING6 57 //config: bool "ping6" 58 //config: default y 59 //config: depends on FEATURE_IPV6 && PING 60 //config: help 61 //config: This will give you a ping that can talk IPv6. 62 //config: 63 //config:config FEATURE_FANCY_PING 64 //config: bool "Enable fancy ping output" 65 //config: default y 66 //config: depends on PING 67 //config: help 68 //config: Make the output from the ping applet include statistics, and at the 69 //config: same time provide full support for ICMP packets. 70 71 /* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */ 72 //applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE)) 73 //applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE)) 74 75 //kbuild:lib-$(CONFIG_PING) += ping.o 76 //kbuild:lib-$(CONFIG_PING6) += ping.o 77 78 //usage:#if !ENABLE_FEATURE_FANCY_PING 79 //usage:# define ping_trivial_usage 80 //usage: "HOST" 81 //usage:# define ping_full_usage "\n\n" 82 //usage: "Send ICMP ECHO_REQUEST packets to network hosts" 83 //usage:# define ping6_trivial_usage 84 //usage: "HOST" 85 //usage:# define ping6_full_usage "\n\n" 86 //usage: "Send ICMP ECHO_REQUEST packets to network hosts" 87 //usage:#else 88 //usage:# define ping_trivial_usage 89 //usage: "[OPTIONS] HOST" 90 //usage:# define ping_full_usage "\n\n" 91 //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" 92 //usage: "\n -4,-6 Force IP or IPv6 name resolution" 93 //usage: "\n -c CNT Send only CNT pings" 94 //usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)" 95 //usage: "\n -t TTL Set TTL" 96 //usage: "\n -I IFACE/IP Use interface or IP address as source" 97 //usage: "\n -W SEC Seconds to wait for the first response (default:10)" 98 //usage: "\n (after all -c CNT packets are sent)" 99 //usage: "\n -w SEC Seconds until ping exits (default:infinite)" 100 //usage: "\n (can exit earlier with -c CNT)" 101 //usage: "\n -q Quiet, only displays output at start" 102 //usage: "\n and when finished" 103 //usage: 104 //usage:# define ping6_trivial_usage 105 //usage: "[OPTIONS] HOST" 106 //usage:# define ping6_full_usage "\n\n" 107 //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" 108 //usage: "\n -c CNT Send only CNT pings" 109 //usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)" 110 //usage: "\n -I IFACE/IP Use interface or IP address as source" 111 //usage: "\n -q Quiet, only displays output at start" 112 //usage: "\n and when finished" 113 //usage: 114 //usage:#endif 115 //usage: 116 //usage:#define ping_example_usage 117 //usage: "$ ping localhost\n" 118 //usage: "PING slag (127.0.0.1): 56 data bytes\n" 119 //usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n" 120 //usage: "\n" 121 //usage: "--- debian ping statistics ---\n" 122 //usage: "1 packets transmitted, 1 packets received, 0% packet loss\n" 123 //usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n" 124 //usage:#define ping6_example_usage 125 //usage: "$ ping6 ip6-localhost\n" 126 //usage: "PING ip6-localhost (::1): 56 data bytes\n" 127 //usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n" 128 //usage: "\n" 129 //usage: "--- ip6-localhost ping statistics ---\n" 130 //usage: "1 packets transmitted, 1 packets received, 0% packet loss\n" 131 //usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n" 31 132 32 133 #if ENABLE_PING6 … … 49 150 }; 50 151 51 /* Common routines */52 53 static int in_cksum(unsigned short *buf, int sz)54 {55 int nleft = sz;56 int sum = 0;57 unsigned short *w = buf;58 unsigned short ans = 0;59 60 while (nleft > 1) {61 sum += *w++;62 nleft -= 2;63 }64 65 if (nleft == 1) {66 *(unsigned char *) (&ans) = *(unsigned char *) w;67 sum += ans;68 }69 70 sum = (sum >> 16) + (sum & 0xFFFF);71 sum += (sum >> 16);72 ans = ~sum;73 return ans;74 }75 76 152 #if !ENABLE_FEATURE_FANCY_PING 77 153 … … 101 177 memset(pkt, 0, sizeof(G.packet)); 102 178 pkt->icmp_type = ICMP_ECHO; 103 pkt->icmp_cksum = in _cksum((unsigned short *) pkt, sizeof(G.packet));179 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, sizeof(G.packet)); 104 180 105 181 xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); … … 224 300 /* Full(er) version */ 225 301 226 #define OPT_STRING ("qvc:s: w:W:I:4" IF_PING6("6"))302 #define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6")) 227 303 enum { 228 304 OPT_QUIET = 1 << 0, … … 230 306 OPT_c = 1 << 2, 231 307 OPT_s = 1 << 3, 232 OPT_w = 1 << 4, 233 OPT_W = 1 << 5, 234 OPT_I = 1 << 6, 235 OPT_IPV4 = 1 << 7, 236 OPT_IPV6 = (1 << 8) * ENABLE_PING6, 308 OPT_t = 1 << 4, 309 OPT_w = 1 << 5, 310 OPT_W = 1 << 6, 311 OPT_I = 1 << 7, 312 OPT_IPV4 = 1 << 8, 313 OPT_IPV6 = (1 << 9) * ENABLE_PING6, 237 314 }; 238 315 … … 245 322 unsigned datalen; 246 323 unsigned pingcount; /* must be int-sized */ 324 unsigned opt_ttl; 247 325 unsigned long ntransmitted, nreceived, nrepeats; 248 326 uint16_t myid; … … 276 354 #define nrepeats (G.nrepeats ) 277 355 #define pingcount (G.pingcount ) 356 #define opt_ttl (G.opt_ttl ) 278 357 #define myid (G.myid ) 279 358 #define tmin (G.tmin ) … … 330 409 } 331 410 332 static void sendping_tail(void (*sp)(int), const void *pkt,int size_pkt)411 static void sendping_tail(void (*sp)(int), int size_pkt) 333 412 { 334 413 int sz; … … 337 416 ntransmitted++; 338 417 418 size_pkt += datalen; 419 339 420 /* sizeof(pingaddr) can be larger than real sa size, but I think 340 421 * it doesn't matter */ 341 sz = xsendto(pingsock, pkt, size_pkt, &pingaddr.sa, sizeof(pingaddr));422 sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr)); 342 423 if (sz != size_pkt) 343 424 bb_error_msg_and_die(bb_msg_write_error); … … 388 469 *(uint32_t*)&pkt->icmp_dun = monotonic_us(); 389 470 390 pkt->icmp_cksum = in _cksum((unsigned short *) pkt, datalen + ICMP_MINLEN);391 392 sendping_tail(sendping4, pkt, datalen +ICMP_MINLEN);471 pkt->icmp_cksum = inet_cksum((uint16_t *) pkt, datalen + ICMP_MINLEN); 472 473 sendping_tail(sendping4, ICMP_MINLEN); 393 474 } 394 475 #if ENABLE_PING6 … … 407 488 *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); 408 489 409 //TODO? pkt->icmp_cksum = in _cksum(...);410 411 sendping_tail(sendping6, pkt, datalen +sizeof(struct icmp6_hdr));490 //TODO? pkt->icmp_cksum = inet_cksum(...); 491 492 sendping_tail(sendping6, sizeof(struct icmp6_hdr)); 412 493 } 413 494 #endif … … 533 614 } 534 615 #if ENABLE_PING6 535 static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/int hoplimit)616 static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit) 536 617 { 537 618 struct icmp6_hdr *icmppkt; … … 553 634 tp = (uint32_t *) &icmppkt->icmp6_data8[4]; 554 635 unpack_tail(sz, tp, 555 inet_ntop(AF_INET6, & pingaddr.sin6.sin6_addr,636 inet_ntop(AF_INET6, &from->sin6_addr, 556 637 buf, sizeof(buf)), 557 638 recv_seq, hoplimit); … … 586 667 sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ 587 668 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); 669 670 if (opt_ttl != 0) { 671 setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl)); 672 /* above doesnt affect packets sent to bcast IP, so... */ 673 setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl)); 674 } 588 675 589 676 signal(SIGINT, print_stats_and_exit); … … 638 725 } 639 726 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 640 727 sizeof(filt)) < 0) 641 728 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); 642 729 } … … 697 784 } 698 785 } 699 unpack6(G.rcv_packet, c, /*&from,*/hoplimit);786 unpack6(G.rcv_packet, c, &from, hoplimit); 700 787 if (pingcount && nreceived >= pingcount) 701 788 break; … … 736 823 INIT_G(); 737 824 738 /* exactly one argument needed; -v and -q don't mix; -c NUM, - w NUM, -W NUM */739 opt_complementary = "=1:q--v:v--q:c+: w+:W+";740 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, & deadline, &timeout, &str_I);825 /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */ 826 opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+"; 827 opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I); 741 828 if (opt & OPT_s) 742 829 datalen = xatou16(str_s); // -s
Note:
See TracChangeset
for help on using the changeset viewer.