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