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