Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * $Id: ping6.c,v 1.6 2004/03/15 08:28:48 andersen Exp $ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 4 | * Mini ping implementation for busybox |
| 5 | * |
| 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 7 | * |
"Robert P. J. Day" | 2819f75 | 2006-07-11 11:32:31 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 9 | * |
| 10 | * This version of ping is adapted from the ping in netkit-base 0.10, |
| 11 | * which is: |
| 12 | * |
| 13 | * Copyright (c) 1989 The Regents of the University of California. |
| 14 | * All rights reserved. |
| 15 | * |
| 16 | * This code is derived from software contributed to Berkeley by |
| 17 | * Mike Muuss. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 18 | * |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 19 | * Original copyright notice is retained at the end of this file. |
| 20 | * |
| 21 | * This version is an adaptation of ping.c from busybox. |
| 22 | * The code was modified by Bart Visscher <magick@linux-fan.com> |
| 23 | */ |
| 24 | |
| 25 | #include <sys/param.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <sys/file.h> |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 28 | #include <sys/times.h> |
Mike Frysinger | 06adf5f | 2006-03-22 00:25:07 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 30 | |
| 31 | #include <netinet/in.h> |
| 32 | #include <netinet/ip6.h> |
| 33 | #include <netinet/icmp6.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <net/if.h> |
| 36 | #include <netdb.h> |
| 37 | #include <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <errno.h> |
| 40 | #include <unistd.h> |
| 41 | #include <string.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <stddef.h> /* offsetof */ |
| 44 | #include "busybox.h" |
| 45 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 46 | enum { |
| 47 | DEFDATALEN = 56, |
| 48 | MAXIPLEN = 60, |
| 49 | MAXICMPLEN = 76, |
| 50 | MAXPACKET = 65468, |
| 51 | MAX_DUP_CHK = (8 * 128), |
| 52 | MAXWAIT = 10, |
| 53 | PINGINTERVAL = 1 /* second */ |
| 54 | }; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 55 | |
| 56 | #define O_QUIET (1 << 0) |
| 57 | #define O_VERBOSE (1 << 1) |
| 58 | |
| 59 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 60 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 61 | #define SET(bit) (A(bit) |= B(bit)) |
| 62 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 63 | #define TST(bit) (A(bit) & B(bit)) |
| 64 | |
| 65 | static void ping(const char *host); |
| 66 | |
| 67 | /* simple version */ |
| 68 | #ifndef CONFIG_FEATURE_FANCY_PING6 |
Eric Andersen | 787ff55 | 2003-05-22 07:10:22 +0000 | [diff] [blame] | 69 | static struct hostent *h; |
| 70 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 71 | static void noresp(int ign) |
Eric Andersen | 4e486a5 | 2003-01-12 06:08:33 +0000 | [diff] [blame] | 72 | { |
| 73 | printf("No response from %s\n", h->h_name); |
| 74 | exit(EXIT_FAILURE); |
| 75 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 76 | |
| 77 | static void ping(const char *host) |
| 78 | { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 79 | struct sockaddr_in6 pingaddr; |
| 80 | struct icmp6_hdr *pkt; |
| 81 | int pingsock, c; |
| 82 | int sockopt; |
| 83 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
| 84 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 85 | pingsock = create_icmp6_socket(); |
| 86 | |
| 87 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 88 | |
| 89 | pingaddr.sin6_family = AF_INET6; |
| 90 | h = xgethostbyname2(host, AF_INET6); |
| 91 | memcpy(&pingaddr.sin6_addr, h->h_addr, sizeof(pingaddr.sin6_addr)); |
| 92 | |
| 93 | pkt = (struct icmp6_hdr *) packet; |
| 94 | memset(pkt, 0, sizeof(packet)); |
| 95 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
| 96 | |
| 97 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 98 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt, |
| 99 | sizeof(sockopt)); |
| 100 | |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 101 | c = sendto(pingsock, packet, DEFDATALEN + sizeof (struct icmp6_hdr), 0, |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 102 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); |
| 103 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 104 | if (c < 0 || c != sizeof(packet)) { |
| 105 | if (ENABLE_FEATURE_CLEAN_UP) close(pingsock); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 106 | bb_perror_msg_and_die("sendto"); |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 107 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 108 | |
| 109 | signal(SIGALRM, noresp); |
| 110 | alarm(5); /* give the host 5000ms to respond */ |
| 111 | /* listen for replies */ |
| 112 | while (1) { |
| 113 | struct sockaddr_in6 from; |
| 114 | size_t fromlen = sizeof(from); |
| 115 | |
| 116 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 117 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 118 | if (errno == EINTR) |
| 119 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 120 | bb_perror_msg("recvfrom"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 121 | continue; |
| 122 | } |
| 123 | if (c >= 8) { /* icmp6_hdr */ |
| 124 | pkt = (struct icmp6_hdr *) packet; |
| 125 | if (pkt->icmp6_type == ICMP6_ECHO_REPLY) |
| 126 | break; |
| 127 | } |
| 128 | } |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 129 | if (ENABLE_FEATURE_CLEAN_UP) close(pingsock); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 130 | printf("%s is alive!\n", h->h_name); |
| 131 | return; |
| 132 | } |
| 133 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 134 | int ping6_main(int argc, char **argv) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 135 | { |
| 136 | argc--; |
| 137 | argv++; |
| 138 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 140 | ping(*argv); |
| 141 | return EXIT_SUCCESS; |
| 142 | } |
| 143 | |
| 144 | #else /* ! CONFIG_FEATURE_FANCY_PING6 */ |
| 145 | /* full(er) version */ |
| 146 | static struct sockaddr_in6 pingaddr; |
| 147 | static int pingsock = -1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 148 | static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 149 | static int if_index; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 151 | static unsigned long ntransmitted, nreceived, nrepeats, pingcount; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 152 | static int myid, options; |
| 153 | static unsigned long tmin = ULONG_MAX, tmax, tsum; |
| 154 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 155 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 156 | static struct hostent *hostent; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 157 | |
| 158 | static void sendping(int); |
| 159 | static void pingstats(int); |
| 160 | static void unpack(char *, int, struct sockaddr_in6 *, int); |
| 161 | |
| 162 | /**************************************************************************/ |
| 163 | |
| 164 | static void pingstats(int junk) |
| 165 | { |
| 166 | int status; |
| 167 | |
| 168 | signal(SIGINT, SIG_IGN); |
| 169 | |
| 170 | printf("\n--- %s ping statistics ---\n", hostent->h_name); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 171 | printf("%lu packets transmitted, ", ntransmitted); |
| 172 | printf("%lu packets received, ", nreceived); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 173 | if (nrepeats) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 174 | printf("%lu duplicates, ", nrepeats); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 175 | if (ntransmitted) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 176 | printf("%lu%% packet loss\n", |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 177 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 178 | if (nreceived) |
| 179 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 180 | tmin / 10, tmin % 10, |
| 181 | (tsum / (nreceived + nrepeats)) / 10, |
| 182 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
| 183 | if (nreceived != 0) |
| 184 | status = EXIT_SUCCESS; |
| 185 | else |
| 186 | status = EXIT_FAILURE; |
| 187 | exit(status); |
| 188 | } |
| 189 | |
| 190 | static void sendping(int junk) |
| 191 | { |
| 192 | struct icmp6_hdr *pkt; |
| 193 | int i; |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 194 | char packet[datalen + sizeof (struct icmp6_hdr)]; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 195 | |
| 196 | pkt = (struct icmp6_hdr *) packet; |
| 197 | |
| 198 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
| 199 | pkt->icmp6_code = 0; |
| 200 | pkt->icmp6_cksum = 0; |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 201 | pkt->icmp6_seq = htons(ntransmitted++); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 202 | pkt->icmp6_id = myid; |
| 203 | CLR(pkt->icmp6_seq % MAX_DUP_CHK); |
| 204 | |
| 205 | gettimeofday((struct timeval *) &pkt->icmp6_data8[4], NULL); |
| 206 | |
| 207 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 208 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); |
| 209 | |
| 210 | if (i < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 211 | bb_perror_msg_and_die("sendto"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 212 | else if ((size_t)i != sizeof(packet)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 213 | bb_error_msg_and_die("ping wrote %d chars; %d expected", i, |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 214 | (int)sizeof(packet)); |
| 215 | |
| 216 | signal(SIGALRM, sendping); |
| 217 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 218 | alarm(PINGINTERVAL); |
| 219 | } else { /* done, wait for the last ping to come back */ |
| 220 | /* todo, don't necessarily need to wait so long... */ |
| 221 | signal(SIGALRM, pingstats); |
| 222 | alarm(MAXWAIT); |
| 223 | } |
| 224 | } |
| 225 | |
Mike Frysinger | 2f135fc | 2006-03-13 23:48:18 +0000 | [diff] [blame] | 226 | /* RFC3542 changed some definitions from RFC2292 for no good reason, whee ! |
| 227 | * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */ |
| 228 | #ifndef MLD_LISTENER_QUERY |
| 229 | # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY |
Mike Frysinger | 9e09455 | 2006-03-10 23:41:29 +0000 | [diff] [blame] | 230 | #endif |
Mike Frysinger | 2f135fc | 2006-03-13 23:48:18 +0000 | [diff] [blame] | 231 | #ifndef MLD_LISTENER_REPORT |
| 232 | # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT |
Mike Frysinger | 9e09455 | 2006-03-10 23:41:29 +0000 | [diff] [blame] | 233 | #endif |
Mike Frysinger | 2f135fc | 2006-03-13 23:48:18 +0000 | [diff] [blame] | 234 | #ifndef MLD_LISTENER_REDUCTION |
| 235 | # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION |
Mike Frysinger | 9e09455 | 2006-03-10 23:41:29 +0000 | [diff] [blame] | 236 | #endif |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 237 | static char *icmp6_type_name(int id) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 238 | { |
| 239 | switch (id) { |
| 240 | case ICMP6_DST_UNREACH: return "Destination Unreachable"; |
| 241 | case ICMP6_PACKET_TOO_BIG: return "Packet too big"; |
| 242 | case ICMP6_TIME_EXCEEDED: return "Time Exceeded"; |
| 243 | case ICMP6_PARAM_PROB: return "Parameter Problem"; |
| 244 | case ICMP6_ECHO_REPLY: return "Echo Reply"; |
| 245 | case ICMP6_ECHO_REQUEST: return "Echo Request"; |
Mike Frysinger | 2f135fc | 2006-03-13 23:48:18 +0000 | [diff] [blame] | 246 | case MLD_LISTENER_QUERY: return "Listener Query"; |
| 247 | case MLD_LISTENER_REPORT: return "Listener Report"; |
| 248 | case MLD_LISTENER_REDUCTION: return "Listener Reduction"; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 249 | default: return "unknown ICMP type"; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
| 253 | static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit) |
| 254 | { |
| 255 | struct icmp6_hdr *icmppkt; |
| 256 | struct timeval tv, *tp; |
| 257 | int dupflag; |
| 258 | unsigned long triptime; |
| 259 | char buf[INET6_ADDRSTRLEN]; |
| 260 | |
| 261 | gettimeofday(&tv, NULL); |
| 262 | |
| 263 | /* discard if too short */ |
| 264 | if (sz < (datalen + sizeof(struct icmp6_hdr))) |
| 265 | return; |
| 266 | |
| 267 | icmppkt = (struct icmp6_hdr *) packet; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 268 | if (icmppkt->icmp6_id != myid) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 269 | return; /* not our ping */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 270 | |
| 271 | if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 272 | ++nreceived; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 273 | tp = (struct timeval *) &icmppkt->icmp6_data8[4]; |
| 274 | |
| 275 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 276 | --tv.tv_sec; |
| 277 | tv.tv_usec += 1000000; |
| 278 | } |
| 279 | tv.tv_sec -= tp->tv_sec; |
| 280 | |
| 281 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 282 | tsum += triptime; |
| 283 | if (triptime < tmin) |
| 284 | tmin = triptime; |
| 285 | if (triptime > tmax) |
| 286 | tmax = triptime; |
| 287 | |
| 288 | if (TST(icmppkt->icmp6_seq % MAX_DUP_CHK)) { |
| 289 | ++nrepeats; |
| 290 | --nreceived; |
| 291 | dupflag = 1; |
| 292 | } else { |
| 293 | SET(icmppkt->icmp6_seq % MAX_DUP_CHK); |
| 294 | dupflag = 0; |
| 295 | } |
| 296 | |
| 297 | if (options & O_QUIET) |
| 298 | return; |
| 299 | |
| 300 | printf("%d bytes from %s: icmp6_seq=%u", sz, |
Rob Landley | 7a260f0 | 2006-06-19 03:20:03 +0000 | [diff] [blame] | 301 | inet_ntop(AF_INET6, &pingaddr.sin6_addr, |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 302 | buf, sizeof(buf)), |
| 303 | icmppkt->icmp6_seq); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 304 | printf(" ttl=%d time=%lu.%lu ms", hoplimit, |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 305 | triptime / 10, triptime % 10); |
| 306 | if (dupflag) |
| 307 | printf(" (DUP!)"); |
| 308 | printf("\n"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 309 | } else |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 310 | if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame^] | 311 | bb_error_msg("warning: got ICMP %d (%s)", |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 312 | icmppkt->icmp6_type, icmp6_type_name(icmppkt->icmp6_type)); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static void ping(const char *host) |
| 316 | { |
| 317 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
| 318 | char buf[INET6_ADDRSTRLEN]; |
| 319 | int sockopt; |
| 320 | struct msghdr msg; |
| 321 | struct sockaddr_in6 from; |
| 322 | struct iovec iov; |
| 323 | char control_buf[CMSG_SPACE(36)]; |
| 324 | |
| 325 | pingsock = create_icmp6_socket(); |
| 326 | |
| 327 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 328 | |
| 329 | pingaddr.sin6_family = AF_INET6; |
| 330 | hostent = xgethostbyname2(host, AF_INET6); |
| 331 | if (hostent->h_addrtype != AF_INET6) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 332 | bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported."); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 333 | |
| 334 | memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr)); |
| 335 | |
| 336 | #ifdef ICMP6_FILTER |
| 337 | { |
| 338 | struct icmp6_filter filt; |
| 339 | if (!(options & O_VERBOSE)) { |
| 340 | ICMP6_FILTER_SETBLOCKALL(&filt); |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 341 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 342 | } else { |
| 343 | ICMP6_FILTER_SETPASSALL(&filt); |
| 344 | } |
| 345 | if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, |
| 346 | sizeof(filt)) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 347 | bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 348 | } |
| 349 | #endif /*ICMP6_FILTER*/ |
| 350 | |
| 351 | /* enable broadcast pings */ |
| 352 | sockopt = 1; |
| 353 | setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, |
| 354 | sizeof(sockopt)); |
| 355 | |
| 356 | /* set recv buf for broadcast pings */ |
| 357 | sockopt = 48 * 1024; |
| 358 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 359 | sizeof(sockopt)); |
| 360 | |
| 361 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 362 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, (char *) &sockopt, |
| 363 | sizeof(sockopt)); |
| 364 | |
| 365 | sockopt = 1; |
| 366 | setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, (char *) &sockopt, |
| 367 | sizeof(sockopt)); |
| 368 | |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 369 | if (if_index) |
| 370 | pingaddr.sin6_scope_id = if_index; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 371 | |
| 372 | printf("PING %s (%s): %d data bytes\n", |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 373 | hostent->h_name, |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 374 | inet_ntop(AF_INET6, &pingaddr.sin6_addr, |
| 375 | buf, sizeof(buf)), |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 376 | datalen); |
| 377 | |
| 378 | signal(SIGINT, pingstats); |
| 379 | |
| 380 | /* start the ping's going ... */ |
| 381 | sendping(0); |
| 382 | |
| 383 | /* listen for replies */ |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 384 | msg.msg_name = &from; |
| 385 | msg.msg_namelen = sizeof(from); |
| 386 | msg.msg_iov = &iov; |
| 387 | msg.msg_iovlen = 1; |
| 388 | msg.msg_control = control_buf; |
| 389 | iov.iov_base = packet; |
| 390 | iov.iov_len = sizeof(packet); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 391 | while (1) { |
| 392 | int c; |
| 393 | struct cmsghdr *cmsgptr = NULL; |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 394 | int hoplimit = -1; |
| 395 | msg.msg_controllen = sizeof(control_buf); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 396 | |
| 397 | if ((c = recvmsg(pingsock, &msg, 0)) < 0) { |
| 398 | if (errno == EINTR) |
| 399 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 400 | bb_perror_msg("recvfrom"); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; |
| 404 | cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) { |
| 405 | if (cmsgptr->cmsg_level == SOL_IPV6 && |
| 406 | cmsgptr->cmsg_type == IPV6_HOPLIMIT ) { |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 407 | hoplimit = *(int*)CMSG_DATA(cmsgptr); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | unpack(packet, c, &from, hoplimit); |
| 411 | if (pingcount > 0 && nreceived >= pingcount) |
| 412 | break; |
| 413 | } |
| 414 | pingstats(0); |
| 415 | } |
| 416 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 417 | int ping6_main(int argc, char **argv) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 418 | { |
| 419 | char *thisarg; |
| 420 | |
| 421 | datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ |
| 422 | |
| 423 | argc--; |
| 424 | argv++; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 425 | /* Parse any options */ |
| 426 | while (argc >= 1 && **argv == '-') { |
| 427 | thisarg = *argv; |
| 428 | thisarg++; |
| 429 | switch (*thisarg) { |
| 430 | case 'v': |
| 431 | options &= ~O_QUIET; |
| 432 | options |= O_VERBOSE; |
| 433 | break; |
| 434 | case 'q': |
| 435 | options &= ~O_VERBOSE; |
| 436 | options |= O_QUIET; |
| 437 | break; |
| 438 | case 'c': |
| 439 | if (--argc <= 0) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 440 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 441 | argv++; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 442 | pingcount = xatoul(*argv); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 443 | break; |
| 444 | case 's': |
| 445 | if (--argc <= 0) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 446 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 447 | argv++; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 448 | datalen = xatou16(*argv); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 449 | break; |
| 450 | case 'I': |
| 451 | if (--argc <= 0) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 452 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 453 | argv++; |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 454 | if_index = if_nametoindex(*argv); |
| 455 | if (!if_index) |
| 456 | bb_error_msg_and_die( |
| 457 | "%s: invalid interface name", *argv); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 458 | break; |
| 459 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 460 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 461 | } |
| 462 | argc--; |
| 463 | argv++; |
| 464 | } |
| 465 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 466 | bb_show_usage(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 467 | |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 468 | myid = (int16_t) getpid(); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 469 | ping(*argv); |
| 470 | return EXIT_SUCCESS; |
| 471 | } |
| 472 | #endif /* ! CONFIG_FEATURE_FANCY_PING6 */ |
| 473 | |
| 474 | /* |
| 475 | * Copyright (c) 1989 The Regents of the University of California. |
| 476 | * All rights reserved. |
| 477 | * |
| 478 | * This code is derived from software contributed to Berkeley by |
| 479 | * Mike Muuss. |
| 480 | * |
| 481 | * Redistribution and use in source and binary forms, with or without |
| 482 | * modification, are permitted provided that the following conditions |
| 483 | * are met: |
| 484 | * 1. Redistributions of source code must retain the above copyright |
| 485 | * notice, this list of conditions and the following disclaimer. |
| 486 | * 2. Redistributions in binary form must reproduce the above copyright |
| 487 | * notice, this list of conditions and the following disclaimer in the |
| 488 | * documentation and/or other materials provided with the distribution. |
| 489 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 490 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 491 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 492 | * |
| 493 | * 4. Neither the name of the University nor the names of its contributors |
| 494 | * may be used to endorse or promote products derived from this software |
| 495 | * without specific prior written permission. |
| 496 | * |
| 497 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 498 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 499 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 500 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 501 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 502 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 503 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 504 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 505 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 506 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 507 | * SUCH DAMAGE. |
| 508 | */ |