Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 3 | * Mini ping implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 6 | * |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 7 | * Adapted from the ping in netkit-base 0.10: |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 8 | * Copyright (c) 1989 The Regents of the University of California. |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 9 | * All rights reserved. |
| 10 | * |
| 11 | * This code is derived from software contributed to Berkeley by |
| 12 | * Mike Muuss. |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 13 | * |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 15 | */ |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 16 | /* from ping6.c: |
| 17 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 18 | * |
| 19 | * This version of ping is adapted from the ping in netkit-base 0.10, |
| 20 | * which is: |
| 21 | * |
| 22 | * Original copyright notice is retained at the end of this file. |
| 23 | * |
| 24 | * This version is an adaptation of ping.c from busybox. |
| 25 | * The code was modified by Bart Visscher <magick@linux-fan.com> |
| 26 | */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 27 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 28 | #include <net/if.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 29 | #include <netinet/ip_icmp.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 30 | #include "busybox.h" |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 31 | #if ENABLE_PING6 |
| 32 | #include <netinet/icmp6.h> |
| 33 | /* I see RENUMBERED constants in bits/in.h - !!? |
| 34 | * What a fuck is going on with libc? Is it a glibc joke? */ |
| 35 | #ifdef IPV6_2292HOPLIMIT |
| 36 | #undef IPV6_HOPLIMIT |
| 37 | #define IPV6_HOPLIMIT IPV6_2292HOPLIMIT |
| 38 | #endif |
| 39 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 40 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 41 | enum { |
| 42 | DEFDATALEN = 56, |
| 43 | MAXIPLEN = 60, |
| 44 | MAXICMPLEN = 76, |
| 45 | MAXPACKET = 65468, |
| 46 | MAX_DUP_CHK = (8 * 128), |
| 47 | MAXWAIT = 10, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 48 | PINGINTERVAL = 1, /* 1 second */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 49 | }; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 50 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 51 | /* common routines */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 52 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 53 | static int in_cksum(unsigned short *buf, int sz) |
| 54 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | int nleft = sz; |
| 56 | int sum = 0; |
| 57 | unsigned short *w = buf; |
| 58 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | while (nleft > 1) { |
| 61 | sum += *w++; |
| 62 | nleft -= 2; |
| 63 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 64 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | if (nleft == 1) { |
| 66 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 67 | sum += ans; |
| 68 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 69 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 70 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 71 | sum += (sum >> 16); |
| 72 | ans = ~sum; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 73 | return ans; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 76 | #if !ENABLE_FEATURE_FANCY_PING |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 77 | |
| 78 | /* simple version */ |
| 79 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 80 | static char *hostname; |
| 81 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 82 | static void noresp(int ign ATTRIBUTE_UNUSED) |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 83 | { |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 84 | printf("No response from %s\n", hostname); |
Eric Andersen | 4e486a5 | 2003-01-12 06:08:33 +0000 | [diff] [blame] | 85 | exit(EXIT_FAILURE); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 86 | } |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 88 | static void ping(len_and_sockaddr *lsa) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 89 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | struct sockaddr_in pingaddr; |
| 91 | struct icmp *pkt; |
| 92 | int pingsock, c; |
| 93 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 94 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 95 | pingsock = create_icmp_socket(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 96 | pingaddr = lsa->sin; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | |
| 98 | pkt = (struct icmp *) packet; |
| 99 | memset(pkt, 0, sizeof(packet)); |
| 100 | pkt->icmp_type = ICMP_ECHO; |
| 101 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 102 | |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 103 | c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 104 | (struct sockaddr *) &pingaddr, sizeof(pingaddr)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | |
Rob Landley | 9b1857f | 2006-05-31 23:54:50 +0000 | [diff] [blame] | 106 | if (c < 0) { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 107 | if (ENABLE_FEATURE_CLEAN_UP) |
| 108 | close(pingsock); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 109 | bb_perror_msg_and_die("sendto"); |
Rob Landley | 9b1857f | 2006-05-31 23:54:50 +0000 | [diff] [blame] | 110 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | /* listen for replies */ |
| 113 | while (1) { |
| 114 | struct sockaddr_in from; |
Mike Frysinger | 03e827a | 2005-07-26 23:00:59 +0000 | [diff] [blame] | 115 | socklen_t fromlen = sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 806116b | 2006-12-31 12:14:16 +0000 | [diff] [blame] | 117 | c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 118 | (struct sockaddr *) &from, &fromlen); |
| 119 | if (c < 0) { |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 120 | if (errno != EINTR) |
| 121 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | continue; |
| 123 | } |
| 124 | if (c >= 76) { /* ip + icmp */ |
| 125 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 126 | |
| 127 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 128 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 129 | break; |
| 130 | } |
| 131 | } |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 132 | if (ENABLE_FEATURE_CLEAN_UP) |
| 133 | close(pingsock); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 136 | #if ENABLE_PING6 |
| 137 | static void ping6(len_and_sockaddr *lsa) |
| 138 | { |
| 139 | struct sockaddr_in6 pingaddr; |
| 140 | struct icmp6_hdr *pkt; |
| 141 | int pingsock, c; |
| 142 | int sockopt; |
| 143 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
| 144 | |
| 145 | pingsock = create_icmp6_socket(); |
| 146 | pingaddr = lsa->sin6; |
| 147 | |
| 148 | pkt = (struct icmp6_hdr *) packet; |
| 149 | memset(pkt, 0, sizeof(packet)); |
| 150 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
| 151 | |
| 152 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 153 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); |
| 154 | |
| 155 | c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0, |
| 156 | (struct sockaddr *) &pingaddr, sizeof(pingaddr)); |
| 157 | |
| 158 | if (c < 0) { |
| 159 | if (ENABLE_FEATURE_CLEAN_UP) |
| 160 | close(pingsock); |
| 161 | bb_perror_msg_and_die("sendto"); |
| 162 | } |
| 163 | |
| 164 | /* listen for replies */ |
| 165 | while (1) { |
| 166 | struct sockaddr_in6 from; |
| 167 | socklen_t fromlen = sizeof(from); |
| 168 | |
| 169 | c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 170 | (struct sockaddr *) &from, &fromlen); |
| 171 | if (c < 0) { |
| 172 | if (errno != EINTR) |
| 173 | bb_perror_msg("recvfrom"); |
| 174 | continue; |
| 175 | } |
| 176 | if (c >= 8) { /* icmp6_hdr */ |
| 177 | pkt = (struct icmp6_hdr *) packet; |
| 178 | if (pkt->icmp6_type == ICMP6_ECHO_REPLY) |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | if (ENABLE_FEATURE_CLEAN_UP) |
| 183 | close(pingsock); |
| 184 | } |
| 185 | #endif |
| 186 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 187 | int ping_main(int argc, char **argv) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 188 | { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 189 | len_and_sockaddr *lsa; |
| 190 | #if ENABLE_PING6 |
| 191 | sa_family_t af = AF_UNSPEC; |
| 192 | while (++argv[0][0]) == '-') { |
| 193 | if (argv[0][1] == '4') { |
| 194 | af = AF_INET; |
| 195 | continue; |
| 196 | } |
| 197 | if (argv[0][1] == '6') { |
| 198 | af = AF_INET6; |
| 199 | continue; |
| 200 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 201 | bb_show_usage(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 202 | } |
| 203 | #else |
| 204 | argv++; |
| 205 | #endif |
| 206 | |
| 207 | hostname = *argv; |
| 208 | if (!hostname) |
| 209 | bb_show_usage(); |
| 210 | |
| 211 | #if ENABLE_PING6 |
| 212 | lsa = host_and_af2sockaddr(hostname, 0, af); |
| 213 | #else |
| 214 | lsa = host_and_af2sockaddr(hostname, 0, AF_INET); |
| 215 | #endif |
| 216 | /* Set timer _after_ DNS resolution */ |
| 217 | signal(SIGALRM, noresp); |
| 218 | alarm(5); /* give the host 5000ms to respond */ |
| 219 | |
| 220 | #if ENABLE_PING6 |
| 221 | if (lsa->sa.sa_family == AF_INET6) |
| 222 | ping6(lsa); |
| 223 | else |
| 224 | #endif |
| 225 | ping(lsa); |
| 226 | printf("%s is alive!\n", hostname); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 227 | return EXIT_SUCCESS; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 230 | |
| 231 | #else /* FEATURE_FANCY_PING */ |
| 232 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 233 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 234 | /* full(er) version */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 235 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 236 | #define OPT_STRING ("qvc:s:I:4" USE_PING6("6")) |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 237 | enum { |
| 238 | OPT_QUIET = 1 << 0, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 239 | OPT_VERBOSE = 1 << 1, |
| 240 | OPT_c = 1 << 2, |
| 241 | OPT_s = 1 << 3, |
| 242 | OPT_I = 1 << 4, |
| 243 | OPT_IPV4 = 1 << 5, |
| 244 | OPT_IPV6 = (1 << 6) * ENABLE_PING6, |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 247 | |
| 248 | static union { |
| 249 | struct sockaddr sa; |
| 250 | struct sockaddr_in sin; |
| 251 | #if ENABLE_PING6 |
| 252 | struct sockaddr_in sin6; |
| 253 | #endif |
| 254 | } pingaddr; |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 255 | static struct sockaddr_in sourceaddr; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 256 | static int pingsock = -1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 257 | static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 258 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 259 | static int if_index; |
| 260 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 261 | static unsigned long ntransmitted, nreceived, nrepeats, pingcount; |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 262 | static int myid; |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 263 | static unsigned long tmin = ULONG_MAX, tmax, tsum; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 264 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 265 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 266 | static const char *hostname; |
| 267 | static const char *dotted; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 268 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 269 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 270 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 271 | #define SET(bit) (A(bit) |= B(bit)) |
| 272 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 273 | #define TST(bit) (A(bit) & B(bit)) |
| 274 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 275 | /**************************************************************************/ |
| 276 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 277 | static void pingstats(void) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | { |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 279 | int status; |
| 280 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 281 | signal(SIGINT, SIG_IGN); |
| 282 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 283 | printf("\n--- %s ping statistics ---\n", hostname); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 284 | printf("%lu packets transmitted, ", ntransmitted); |
| 285 | printf("%lu packets received, ", nreceived); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 286 | if (nrepeats) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 287 | printf("%lu duplicates, ", nrepeats); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 288 | if (ntransmitted) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 289 | ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted); |
| 290 | printf("%lu%% packet loss\n", ntransmitted); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 291 | if (nreceived) |
| 292 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 293 | tmin / 10, tmin % 10, |
| 294 | (tsum / (nreceived + nrepeats)) / 10, |
| 295 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 296 | if (nreceived != 0) |
| 297 | status = EXIT_SUCCESS; |
| 298 | else |
| 299 | status = EXIT_FAILURE; |
| 300 | exit(status); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 303 | static void sendping(int junk ATTRIBUTE_UNUSED) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 304 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 305 | struct icmp *pkt; |
| 306 | int i; |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 307 | char packet[datalen + ICMP_MINLEN]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 308 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 309 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 310 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 311 | pkt->icmp_type = ICMP_ECHO; |
| 312 | pkt->icmp_code = 0; |
| 313 | pkt->icmp_cksum = 0; |
Denis Vlasenko | 919c10d | 2007-01-03 22:14:18 +0000 | [diff] [blame] | 314 | pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 315 | pkt->icmp_id = myid; |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 316 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); |
Denis Vlasenko | 919c10d | 2007-01-03 22:14:18 +0000 | [diff] [blame] | 317 | ntransmitted++; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 318 | |
Rob Landley | bbf4e16 | 2006-01-11 03:44:11 +0000 | [diff] [blame] | 319 | gettimeofday((struct timeval *) &pkt->icmp_dun, NULL); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 320 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 321 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 322 | i = sendto(pingsock, packet, sizeof(packet), 0, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 323 | &pingaddr.sa, sizeof(pingaddr.sin)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 324 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 325 | if (i < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 326 | bb_perror_msg_and_die("sendto"); |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 327 | if ((size_t)i != sizeof(packet)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 328 | bb_error_msg_and_die("ping wrote %d chars; %d expected", i, |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 329 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | |
| 331 | signal(SIGALRM, sendping); |
| 332 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 333 | alarm(PINGINTERVAL); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 334 | } else { /* done, wait for the last ping to come back */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 335 | /* todo, don't necessarily need to wait so long... */ |
| 336 | signal(SIGALRM, pingstats); |
| 337 | alarm(MAXWAIT); |
| 338 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 339 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 340 | #if ENABLE_PING6 |
| 341 | static void sendping6(int junk ATTRIBUTE_UNUSED) |
| 342 | { |
| 343 | struct icmp6_hdr *pkt; |
| 344 | int i; |
| 345 | char packet[datalen + sizeof (struct icmp6_hdr)]; |
| 346 | |
| 347 | pkt = (struct icmp6_hdr *) packet; |
| 348 | |
| 349 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
| 350 | pkt->icmp6_code = 0; |
| 351 | pkt->icmp6_cksum = 0; |
| 352 | pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ |
| 353 | pkt->icmp6_id = myid; |
| 354 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); |
| 355 | ntransmitted++; |
| 356 | |
| 357 | gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL); |
| 358 | |
| 359 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 360 | &pingaddr.sa, sizeof(pingaddr.sin6)); |
| 361 | |
| 362 | if (i < 0) |
| 363 | bb_perror_msg_and_die("sendto"); |
| 364 | if ((size_t)i != sizeof(packet)) |
| 365 | bb_error_msg_and_die("ping wrote %d chars; %d expected", i, |
| 366 | (int)sizeof(packet)); |
| 367 | |
| 368 | signal(SIGALRM, sendping6); |
| 369 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 370 | alarm(PINGINTERVAL); |
| 371 | } else { /* done, wait for the last ping to come back */ |
| 372 | /* todo, don't necessarily need to wait so long... */ |
| 373 | signal(SIGALRM, pingstats); |
| 374 | alarm(MAXWAIT); |
| 375 | } |
| 376 | } |
| 377 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 379 | static char *icmp_type_name(int id) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 380 | { |
| 381 | switch (id) { |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 382 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 383 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 384 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 385 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 386 | case ICMP_ECHO: return "Echo Request"; |
| 387 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 388 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 389 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 390 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 391 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 392 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 393 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 394 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 395 | default: return "unknown ICMP type"; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 398 | #if ENABLE_PING6 |
| 399 | /* RFC3542 changed some definitions from RFC2292 for no good reason, whee! |
| 400 | * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */ |
| 401 | #ifndef MLD_LISTENER_QUERY |
| 402 | # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY |
| 403 | #endif |
| 404 | #ifndef MLD_LISTENER_REPORT |
| 405 | # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT |
| 406 | #endif |
| 407 | #ifndef MLD_LISTENER_REDUCTION |
| 408 | # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION |
| 409 | #endif |
| 410 | static char *icmp6_type_name(int id) |
| 411 | { |
| 412 | switch (id) { |
| 413 | case ICMP6_DST_UNREACH: return "Destination Unreachable"; |
| 414 | case ICMP6_PACKET_TOO_BIG: return "Packet too big"; |
| 415 | case ICMP6_TIME_EXCEEDED: return "Time Exceeded"; |
| 416 | case ICMP6_PARAM_PROB: return "Parameter Problem"; |
| 417 | case ICMP6_ECHO_REPLY: return "Echo Reply"; |
| 418 | case ICMP6_ECHO_REQUEST: return "Echo Request"; |
| 419 | case MLD_LISTENER_QUERY: return "Listener Query"; |
| 420 | case MLD_LISTENER_REPORT: return "Listener Report"; |
| 421 | case MLD_LISTENER_REDUCTION: return "Listener Reduction"; |
| 422 | default: return "unknown ICMP type"; |
| 423 | } |
| 424 | } |
| 425 | #endif |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 426 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 427 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 428 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | struct icmp *icmppkt; |
| 430 | struct iphdr *iphdr; |
| 431 | struct timeval tv, *tp; |
| 432 | int hlen, dupflag; |
| 433 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 434 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 435 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 436 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 437 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 438 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 439 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 440 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 441 | /* check IP header */ |
| 442 | iphdr = (struct iphdr *) buf; |
| 443 | hlen = iphdr->ihl << 2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 444 | sz -= hlen; |
| 445 | icmppkt = (struct icmp *) (buf + hlen); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 446 | if (icmppkt->icmp_id != myid) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 447 | return; /* not our ping */ |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 450 | uint16_t recv_seq = ntohs(icmppkt->icmp_seq); |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 451 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | tp = (struct timeval *) icmppkt->icmp_data; |
| 453 | |
| 454 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 455 | --tv.tv_sec; |
| 456 | tv.tv_usec += 1000000; |
| 457 | } |
| 458 | tv.tv_sec -= tp->tv_sec; |
| 459 | |
| 460 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 461 | tsum += triptime; |
| 462 | if (triptime < tmin) |
| 463 | tmin = triptime; |
| 464 | if (triptime > tmax) |
| 465 | tmax = triptime; |
| 466 | |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 467 | if (TST(recv_seq % MAX_DUP_CHK)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 468 | ++nrepeats; |
| 469 | --nreceived; |
| 470 | dupflag = 1; |
| 471 | } else { |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 472 | SET(recv_seq % MAX_DUP_CHK); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 473 | dupflag = 0; |
| 474 | } |
| 475 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 476 | if (option_mask32 & OPT_QUIET) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 477 | return; |
| 478 | |
| 479 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 480 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 481 | recv_seq); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 482 | printf(" ttl=%d", iphdr->ttl); |
| 483 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 484 | if (dupflag) |
| 485 | printf(" (DUP!)"); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 486 | puts(""); |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 487 | } else { |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 488 | if (icmppkt->icmp_type != ICMP_ECHO) |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 489 | bb_error_msg("warning: got ICMP %d (%s)", |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 490 | icmppkt->icmp_type, |
| 491 | icmp_type_name(icmppkt->icmp_type)); |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 492 | } |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 493 | fflush(stdout); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 494 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 495 | #if ENABLE_PING6 |
| 496 | static void unpack6(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit) |
| 497 | { |
| 498 | struct icmp6_hdr *icmppkt; |
| 499 | struct timeval tv, *tp; |
| 500 | int dupflag; |
| 501 | unsigned long triptime; |
| 502 | char buf[INET6_ADDRSTRLEN]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 503 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 504 | gettimeofday(&tv, NULL); |
| 505 | |
| 506 | /* discard if too short */ |
| 507 | if (sz < (datalen + sizeof(struct icmp6_hdr))) |
| 508 | return; |
| 509 | |
| 510 | icmppkt = (struct icmp6_hdr *) packet; |
| 511 | if (icmppkt->icmp6_id != myid) |
| 512 | return; /* not our ping */ |
| 513 | |
| 514 | if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { |
| 515 | uint16_t recv_seq = ntohs(icmppkt->icmp6_seq); |
| 516 | ++nreceived; |
| 517 | tp = (struct timeval *) &icmppkt->icmp6_data8[4]; |
| 518 | |
| 519 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 520 | --tv.tv_sec; |
| 521 | tv.tv_usec += 1000000; |
| 522 | } |
| 523 | tv.tv_sec -= tp->tv_sec; |
| 524 | |
| 525 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 526 | tsum += triptime; |
| 527 | if (triptime < tmin) |
| 528 | tmin = triptime; |
| 529 | if (triptime > tmax) |
| 530 | tmax = triptime; |
| 531 | |
| 532 | if (TST(recv_seq % MAX_DUP_CHK)) { |
| 533 | ++nrepeats; |
| 534 | --nreceived; |
| 535 | dupflag = 1; |
| 536 | } else { |
| 537 | SET(recv_seq % MAX_DUP_CHK); |
| 538 | dupflag = 0; |
| 539 | } |
| 540 | |
| 541 | if (option_mask32 & OPT_QUIET) |
| 542 | return; |
| 543 | |
| 544 | printf("%d bytes from %s: icmp6_seq=%u", sz, |
| 545 | inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr, |
| 546 | buf, sizeof(buf)), |
| 547 | recv_seq); |
| 548 | printf(" ttl=%d time=%lu.%lu ms", hoplimit, |
| 549 | triptime / 10, triptime % 10); |
| 550 | if (dupflag) |
| 551 | printf(" (DUP!)"); |
| 552 | puts(""); |
| 553 | } else { |
| 554 | if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) |
| 555 | bb_error_msg("warning: got ICMP %d (%s)", |
| 556 | icmppkt->icmp6_type, |
| 557 | icmp6_type_name(icmppkt->icmp6_type)); |
| 558 | } |
| 559 | fflush(stdout); |
| 560 | } |
| 561 | #endif |
| 562 | |
| 563 | static void ping(len_and_sockaddr *lsa) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 564 | { |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 565 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 566 | int sockopt; |
| 567 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 568 | pingsock = create_icmp_socket(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 569 | pingaddr.sin = lsa->sin; |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 570 | if (sourceaddr.sin_addr.s_addr) { |
Denis Vlasenko | bcf4908 | 2006-09-02 17:53:16 +0000 | [diff] [blame] | 571 | xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr)); |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 574 | /* enable broadcast pings */ |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 575 | setsockopt_broadcast(pingsock); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 576 | |
| 577 | /* set recv buf for broadcast pings */ |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 578 | sockopt = 48 * 1024; /* explain why 48k? */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 579 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 580 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 581 | printf("PING %s (%s)", hostname, dotted); |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 582 | if (sourceaddr.sin_addr.s_addr) { |
| 583 | printf(" from %s", |
| 584 | inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr)); |
| 585 | } |
| 586 | printf(": %d data bytes\n", datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 587 | |
| 588 | signal(SIGINT, pingstats); |
| 589 | |
| 590 | /* start the ping's going ... */ |
| 591 | sendping(0); |
| 592 | |
| 593 | /* listen for replies */ |
| 594 | while (1) { |
| 595 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 596 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 597 | int c; |
| 598 | |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 599 | c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 600 | (struct sockaddr *) &from, &fromlen); |
| 601 | if (c < 0) { |
| 602 | if (errno != EINTR) |
| 603 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 604 | continue; |
| 605 | } |
| 606 | unpack(packet, c, &from); |
| 607 | if (pingcount > 0 && nreceived >= pingcount) |
| 608 | break; |
| 609 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 610 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 611 | #if ENABLE_PING6 |
| 612 | extern int BUG_bad_offsetof_icmp6_cksum(void); |
| 613 | static void ping6(len_and_sockaddr *lsa) |
| 614 | { |
| 615 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
| 616 | char buf[INET6_ADDRSTRLEN]; |
| 617 | int sockopt; |
| 618 | struct msghdr msg; |
| 619 | struct sockaddr_in6 from; |
| 620 | struct iovec iov; |
| 621 | char control_buf[CMSG_SPACE(36)]; |
| 622 | |
| 623 | pingsock = create_icmp6_socket(); |
| 624 | pingaddr.sin6 = lsa->sin6; |
| 625 | //if (sourceaddr.sin_addr.s_addr) { |
| 626 | // xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr)); |
| 627 | //} |
| 628 | |
| 629 | #ifdef ICMP6_FILTER |
| 630 | { |
| 631 | struct icmp6_filter filt; |
| 632 | if (!(option_mask32 & OPT_VERBOSE)) { |
| 633 | ICMP6_FILTER_SETBLOCKALL(&filt); |
| 634 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); |
| 635 | } else { |
| 636 | ICMP6_FILTER_SETPASSALL(&filt); |
| 637 | } |
| 638 | if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, |
| 639 | sizeof(filt)) < 0) |
| 640 | bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); |
| 641 | } |
| 642 | #endif /*ICMP6_FILTER*/ |
| 643 | |
| 644 | /* enable broadcast pings */ |
| 645 | setsockopt_broadcast(pingsock); |
| 646 | |
| 647 | /* set recv buf for broadcast pings */ |
| 648 | sockopt = 48 * 1024; /* explain why 48k? */ |
| 649 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); |
| 650 | |
| 651 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 652 | if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) |
| 653 | BUG_bad_offsetof_icmp6_cksum(); |
| 654 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); |
| 655 | |
| 656 | /* request ttl info to be returned in ancillary data */ |
| 657 | setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1)); |
| 658 | |
| 659 | if (if_index) |
| 660 | pingaddr.sin6_scope_id = if_index; |
| 661 | |
| 662 | printf("PING %s (%s): %d data bytes\n", hostname, dotted, datalen); |
| 663 | |
| 664 | signal(SIGINT, pingstats); |
| 665 | |
| 666 | /* start the ping's going ... */ |
| 667 | sendping6(0); |
| 668 | |
| 669 | /* listen for replies */ |
| 670 | msg.msg_name = &from; |
| 671 | msg.msg_namelen = sizeof(from); |
| 672 | msg.msg_iov = &iov; |
| 673 | msg.msg_iovlen = 1; |
| 674 | msg.msg_control = control_buf; |
| 675 | iov.iov_base = packet; |
| 676 | iov.iov_len = sizeof(packet); |
| 677 | while (1) { |
| 678 | int c; |
| 679 | struct cmsghdr *mp; |
| 680 | int hoplimit = -1; |
| 681 | msg.msg_controllen = sizeof(control_buf); |
| 682 | |
| 683 | c = recvmsg(pingsock, &msg, 0); |
| 684 | if (c < 0) { |
| 685 | if (errno != EINTR) |
| 686 | bb_perror_msg("recvfrom"); |
| 687 | continue; |
| 688 | } |
| 689 | for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) { |
| 690 | if (mp->cmsg_level == SOL_IPV6 |
| 691 | && mp->cmsg_type == IPV6_HOPLIMIT |
| 692 | /* don't check len - we trust the kernel: */ |
| 693 | /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */ |
| 694 | ) { |
| 695 | hoplimit = *(int*)CMSG_DATA(mp); |
| 696 | } |
| 697 | } |
| 698 | unpack6(packet, c, &from, hoplimit); |
| 699 | if (pingcount > 0 && nreceived >= pingcount) |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 704 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 705 | /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c |
| 706 | * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too... |
| 707 | */ |
| 708 | static int parse_nipquad(const char *str, struct sockaddr_in* addr) |
| 709 | { |
| 710 | char dummy; |
| 711 | unsigned i1, i2, i3, i4; |
| 712 | if (sscanf(str, "%u.%u.%u.%u%c", |
| 713 | &i1, &i2, &i3, &i4, &dummy) == 4 |
| 714 | && ( (i1|i2|i3|i4) <= 0xff ) |
| 715 | ) { |
| 716 | uint8_t* ptr = (uint8_t*)&addr->sin_addr; |
| 717 | ptr[0] = i1; |
| 718 | ptr[1] = i2; |
| 719 | ptr[2] = i3; |
| 720 | ptr[3] = i4; |
| 721 | return 0; |
| 722 | } |
| 723 | return 1; /* error */ |
| 724 | } |
| 725 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 726 | int ping_main(int argc, char **argv) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 727 | { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 728 | const char *hostname; |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 729 | char *opt_c, *opt_s, *opt_I; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 730 | USE_PING6(sa_family_t af = AF_UNSPEC;) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 731 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 732 | datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ |
| 733 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 734 | /* exactly one argument needed, -v and -q don't mix. So do 4, 6 */ |
Denis Vlasenko | 581930c | 2007-01-24 23:55:34 +0000 | [diff] [blame^] | 735 | opt_complementary = "=1:q--v:v--q"; |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 736 | getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 737 | if (option_mask32 & OPT_c) pingcount = xatoul(opt_c); // -c |
| 738 | if (option_mask32 & OPT_s) datalen = xatou16(opt_s); // -s |
| 739 | if (option_mask32 & OPT_I) { // -I |
| 740 | if_index = if_nametoindex(opt_I); |
| 741 | if (!if_index) |
| 742 | if (parse_nipquad(opt_I, &sourceaddr)) |
| 743 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 744 | } |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 745 | myid = (int16_t) getpid(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 746 | hostname = argv[optind]; |
| 747 | #if ENABLE_PING6 |
| 748 | if (option_mask32 & OPT_IPV4) |
| 749 | af = AF_INET; |
| 750 | if (option_mask32 & OPT_IPV6) |
| 751 | af = AF_INET6; |
| 752 | lsa = host_and_af2sockaddr(hostname, 0, af); |
| 753 | #else |
| 754 | lsa = host_and_af2sockaddr(hostname, 0, AF_INET); |
| 755 | #endif |
| 756 | dotted = xmalloc_sockaddr2dotted_noport(lsa->sa, lsa->len); |
| 757 | #if ENABLE_PING6 |
| 758 | if (lsa->sa.sa_family == AF_INET6) |
| 759 | ping6(lsa); |
| 760 | else |
| 761 | #endif |
| 762 | ping(lsa); |
| 763 | pingstats(); |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 764 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 765 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 766 | #endif /* FEATURE_FANCY_PING */ |
| 767 | |
| 768 | |
| 769 | #if ENABLE_PING6 |
| 770 | int ping6_main(int argc, char **argv) |
| 771 | { |
| 772 | argv[0] = "-6"; |
| 773 | return ping_main(argc + 1, argv - 1); |
| 774 | } |
| 775 | #endif |
| 776 | |
| 777 | /* from ping6.c: |
| 778 | * Copyright (c) 1989 The Regents of the University of California. |
| 779 | * All rights reserved. |
| 780 | * |
| 781 | * This code is derived from software contributed to Berkeley by |
| 782 | * Mike Muuss. |
| 783 | * |
| 784 | * Redistribution and use in source and binary forms, with or without |
| 785 | * modification, are permitted provided that the following conditions |
| 786 | * are met: |
| 787 | * 1. Redistributions of source code must retain the above copyright |
| 788 | * notice, this list of conditions and the following disclaimer. |
| 789 | * 2. Redistributions in binary form must reproduce the above copyright |
| 790 | * notice, this list of conditions and the following disclaimer in the |
| 791 | * documentation and/or other materials provided with the distribution. |
| 792 | * |
| 793 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 794 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 795 | * |
| 796 | * 4. Neither the name of the University nor the names of its contributors |
| 797 | * may be used to endorse or promote products derived from this software |
| 798 | * without specific prior written permission. |
| 799 | * |
| 800 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 801 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 802 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 803 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 804 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 805 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 806 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 807 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 808 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 809 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 810 | * SUCH DAMAGE. |
| 811 | */ |