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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
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> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 30 | #include "libbb.h" |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 31 | |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 32 | //config:config PING |
| 33 | //config: bool "ping" |
| 34 | //config: default y |
Denys Vlasenko | e3b1a1f | 2011-02-26 22:24:08 +0100 | [diff] [blame] | 35 | //config: select PLATFORM_LINUX |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 36 | //config: help |
| 37 | //config: ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to |
| 38 | //config: elicit an ICMP ECHO_RESPONSE from a host or gateway. |
| 39 | //config: |
| 40 | //config:config PING6 |
| 41 | //config: bool "ping6" |
| 42 | //config: default y |
| 43 | //config: depends on FEATURE_IPV6 && PING |
| 44 | //config: help |
| 45 | //config: This will give you a ping that can talk IPv6. |
| 46 | //config: |
| 47 | //config:config FEATURE_FANCY_PING |
| 48 | //config: bool "Enable fancy ping output" |
| 49 | //config: default y |
| 50 | //config: depends on PING |
| 51 | //config: help |
| 52 | //config: Make the output from the ping applet include statistics, and at the |
| 53 | //config: same time provide full support for ICMP packets. |
| 54 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 55 | /* Needs socket(AF_INET, SOCK_RAW, IPPROTO_ICMP), therefore BB_SUID_MAYBE: */ |
| 56 | //applet:IF_PING(APPLET(ping, BB_DIR_BIN, BB_SUID_MAYBE)) |
| 57 | //applet:IF_PING6(APPLET(ping6, BB_DIR_BIN, BB_SUID_MAYBE)) |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 58 | |
| 59 | //kbuild:lib-$(CONFIG_PING) += ping.o |
| 60 | //kbuild:lib-$(CONFIG_PING6) += ping.o |
| 61 | |
| 62 | //usage:#if !ENABLE_FEATURE_FANCY_PING |
| 63 | //usage:# define ping_trivial_usage |
| 64 | //usage: "HOST" |
| 65 | //usage:# define ping_full_usage "\n\n" |
| 66 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts" |
| 67 | //usage:# define ping6_trivial_usage |
| 68 | //usage: "HOST" |
| 69 | //usage:# define ping6_full_usage "\n\n" |
| 70 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts" |
| 71 | //usage:#else |
| 72 | //usage:# define ping_trivial_usage |
| 73 | //usage: "[OPTIONS] HOST" |
| 74 | //usage:# define ping_full_usage "\n\n" |
| 75 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" |
| 76 | //usage: "\nOptions:" |
| 77 | //usage: "\n -4,-6 Force IP or IPv6 name resolution" |
| 78 | //usage: "\n -c CNT Send only CNT pings" |
| 79 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)" |
| 80 | //usage: "\n -t TTL Set TTL" |
| 81 | //usage: "\n -I IFACE/IP Use interface or IP address as source" |
| 82 | //usage: "\n -W SEC Seconds to wait for the first response (default:10)" |
| 83 | //usage: "\n (after all -c CNT packets are sent)" |
| 84 | //usage: "\n -w SEC Seconds until ping exits (default:infinite)" |
| 85 | //usage: "\n (can exit earlier with -c CNT)" |
| 86 | //usage: "\n -q Quiet, only displays output at start" |
| 87 | //usage: "\n and when finished" |
| 88 | //usage: |
| 89 | //usage:# define ping6_trivial_usage |
| 90 | //usage: "[OPTIONS] HOST" |
| 91 | //usage:# define ping6_full_usage "\n\n" |
| 92 | //usage: "Send ICMP ECHO_REQUEST packets to network hosts\n" |
| 93 | //usage: "\nOptions:" |
| 94 | //usage: "\n -c CNT Send only CNT pings" |
| 95 | //usage: "\n -s SIZE Send SIZE data bytes in packets (default:56)" |
| 96 | //usage: "\n -I IFACE/IP Use interface or IP address as source" |
| 97 | //usage: "\n -q Quiet, only displays output at start" |
| 98 | //usage: "\n and when finished" |
| 99 | //usage: |
| 100 | //usage:#endif |
| 101 | //usage: |
| 102 | //usage:#define ping_example_usage |
| 103 | //usage: "$ ping localhost\n" |
| 104 | //usage: "PING slag (127.0.0.1): 56 data bytes\n" |
| 105 | //usage: "64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=20.1 ms\n" |
| 106 | //usage: "\n" |
| 107 | //usage: "--- debian ping statistics ---\n" |
| 108 | //usage: "1 packets transmitted, 1 packets received, 0% packet loss\n" |
| 109 | //usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n" |
| 110 | //usage:#define ping6_example_usage |
| 111 | //usage: "$ ping6 ip6-localhost\n" |
| 112 | //usage: "PING ip6-localhost (::1): 56 data bytes\n" |
| 113 | //usage: "64 bytes from ::1: icmp6_seq=0 ttl=64 time=20.1 ms\n" |
| 114 | //usage: "\n" |
| 115 | //usage: "--- ip6-localhost ping statistics ---\n" |
| 116 | //usage: "1 packets transmitted, 1 packets received, 0% packet loss\n" |
| 117 | //usage: "round-trip min/avg/max = 20.1/20.1/20.1 ms\n" |
| 118 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 119 | #if ENABLE_PING6 |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 120 | # include <netinet/icmp6.h> |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 121 | /* I see RENUMBERED constants in bits/in.h - !!? |
| 122 | * What a fuck is going on with libc? Is it a glibc joke? */ |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 123 | # ifdef IPV6_2292HOPLIMIT |
| 124 | # undef IPV6_HOPLIMIT |
| 125 | # define IPV6_HOPLIMIT IPV6_2292HOPLIMIT |
| 126 | # endif |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 127 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 128 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 129 | enum { |
| 130 | DEFDATALEN = 56, |
| 131 | MAXIPLEN = 60, |
| 132 | MAXICMPLEN = 76, |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 133 | MAX_DUP_CHK = (8 * 128), |
| 134 | MAXWAIT = 10, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 135 | PINGINTERVAL = 1, /* 1 second */ |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 136 | }; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 137 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 138 | /* Common routines */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 139 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 140 | static int in_cksum(unsigned short *buf, int sz) |
| 141 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | int nleft = sz; |
| 143 | int sum = 0; |
| 144 | unsigned short *w = buf; |
| 145 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 146 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 147 | while (nleft > 1) { |
| 148 | sum += *w++; |
| 149 | nleft -= 2; |
| 150 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 151 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | if (nleft == 1) { |
| 153 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 154 | sum += ans; |
| 155 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 156 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 158 | sum += (sum >> 16); |
| 159 | ans = ~sum; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 160 | return ans; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 161 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 163 | #if !ENABLE_FEATURE_FANCY_PING |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 164 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 165 | /* Simple version */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 166 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 167 | struct globals { |
| 168 | char *hostname; |
| 169 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
| 170 | } FIX_ALIASING; |
| 171 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 172 | #define INIT_G() do { } while (0) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 174 | static void noresp(int ign UNUSED_PARAM) |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 175 | { |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 176 | printf("No response from %s\n", G.hostname); |
Eric Andersen | 4e486a5 | 2003-01-12 06:08:33 +0000 | [diff] [blame] | 177 | exit(EXIT_FAILURE); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 178 | } |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 179 | |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 180 | static void ping4(len_and_sockaddr *lsa) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 181 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 182 | struct icmp *pkt; |
| 183 | int pingsock, c; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 184 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 185 | pingsock = create_icmp_socket(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 186 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 187 | pkt = (struct icmp *) G.packet; |
| 188 | memset(pkt, 0, sizeof(G.packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 189 | pkt->icmp_type = ICMP_ECHO; |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 190 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(G.packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 192 | xsendto(pingsock, G.packet, DEFDATALEN + ICMP_MINLEN, &lsa->u.sa, lsa->len); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 193 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | /* listen for replies */ |
| 195 | while (1) { |
| 196 | struct sockaddr_in from; |
Mike Frysinger | 03e827a | 2005-07-26 23:00:59 +0000 | [diff] [blame] | 197 | socklen_t fromlen = sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 199 | c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, |
Denis Vlasenko | 806116b | 2006-12-31 12:14:16 +0000 | [diff] [blame] | 200 | (struct sockaddr *) &from, &fromlen); |
| 201 | if (c < 0) { |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 202 | if (errno != EINTR) |
| 203 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | continue; |
| 205 | } |
| 206 | if (c >= 76) { /* ip + icmp */ |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 207 | struct iphdr *iphdr = (struct iphdr *) G.packet; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 209 | pkt = (struct icmp *) (G.packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 210 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 211 | break; |
| 212 | } |
| 213 | } |
Denis Vlasenko | 9adc6ce | 2007-01-22 22:45:27 +0000 | [diff] [blame] | 214 | if (ENABLE_FEATURE_CLEAN_UP) |
| 215 | close(pingsock); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 218 | #if ENABLE_PING6 |
| 219 | static void ping6(len_and_sockaddr *lsa) |
| 220 | { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 221 | struct icmp6_hdr *pkt; |
| 222 | int pingsock, c; |
| 223 | int sockopt; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 224 | |
| 225 | pingsock = create_icmp6_socket(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 226 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 227 | pkt = (struct icmp6_hdr *) G.packet; |
| 228 | memset(pkt, 0, sizeof(G.packet)); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 229 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
| 230 | |
| 231 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 232 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); |
| 233 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 234 | xsendto(pingsock, G.packet, DEFDATALEN + sizeof(struct icmp6_hdr), &lsa->u.sa, lsa->len); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 235 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 236 | /* listen for replies */ |
| 237 | while (1) { |
| 238 | struct sockaddr_in6 from; |
| 239 | socklen_t fromlen = sizeof(from); |
| 240 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 241 | c = recvfrom(pingsock, G.packet, sizeof(G.packet), 0, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 242 | (struct sockaddr *) &from, &fromlen); |
| 243 | if (c < 0) { |
| 244 | if (errno != EINTR) |
| 245 | bb_perror_msg("recvfrom"); |
| 246 | continue; |
| 247 | } |
Bernhard Reutner-Fischer | dfd38a4 | 2010-01-23 12:52:40 +0100 | [diff] [blame] | 248 | if (c >= ICMP_MINLEN) { /* icmp6_hdr */ |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 249 | pkt = (struct icmp6_hdr *) G.packet; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 250 | if (pkt->icmp6_type == ICMP6_ECHO_REPLY) |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | if (ENABLE_FEATURE_CLEAN_UP) |
| 255 | close(pingsock); |
| 256 | } |
| 257 | #endif |
| 258 | |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 259 | #if !ENABLE_PING6 |
| 260 | # define common_ping_main(af, argv) common_ping_main(argv) |
| 261 | #endif |
| 262 | static int common_ping_main(sa_family_t af, char **argv) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 263 | { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 264 | len_and_sockaddr *lsa; |
Denis Vlasenko | 3483e85 | 2007-07-02 15:47:52 +0000 | [diff] [blame] | 265 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 266 | INIT_G(); |
| 267 | |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 268 | #if ENABLE_PING6 |
Denis Vlasenko | 3483e85 | 2007-07-02 15:47:52 +0000 | [diff] [blame] | 269 | while ((++argv)[0] && argv[0][0] == '-') { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 270 | if (argv[0][1] == '4') { |
| 271 | af = AF_INET; |
| 272 | continue; |
| 273 | } |
| 274 | if (argv[0][1] == '6') { |
| 275 | af = AF_INET6; |
| 276 | continue; |
| 277 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 278 | bb_show_usage(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 279 | } |
| 280 | #else |
| 281 | argv++; |
| 282 | #endif |
| 283 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 284 | G.hostname = *argv; |
| 285 | if (!G.hostname) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 286 | bb_show_usage(); |
| 287 | |
| 288 | #if ENABLE_PING6 |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 289 | lsa = xhost_and_af2sockaddr(G.hostname, 0, af); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 290 | #else |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 291 | lsa = xhost_and_af2sockaddr(G.hostname, 0, AF_INET); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 292 | #endif |
| 293 | /* Set timer _after_ DNS resolution */ |
| 294 | signal(SIGALRM, noresp); |
| 295 | alarm(5); /* give the host 5000ms to respond */ |
| 296 | |
| 297 | #if ENABLE_PING6 |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 298 | if (lsa->u.sa.sa_family == AF_INET6) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 299 | ping6(lsa); |
| 300 | else |
| 301 | #endif |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 302 | ping4(lsa); |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 303 | printf("%s is alive!\n", G.hostname); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 304 | return EXIT_SUCCESS; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 307 | |
| 308 | #else /* FEATURE_FANCY_PING */ |
| 309 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 310 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 311 | /* Full(er) version */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 312 | |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 313 | #define OPT_STRING ("qvc:s:t:w:W:I:4" IF_PING6("6")) |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 314 | enum { |
| 315 | OPT_QUIET = 1 << 0, |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 316 | OPT_VERBOSE = 1 << 1, |
| 317 | OPT_c = 1 << 2, |
| 318 | OPT_s = 1 << 3, |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 319 | OPT_t = 1 << 4, |
| 320 | OPT_w = 1 << 5, |
| 321 | OPT_W = 1 << 6, |
| 322 | OPT_I = 1 << 7, |
| 323 | OPT_IPV4 = 1 << 8, |
| 324 | OPT_IPV6 = (1 << 9) * ENABLE_PING6, |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 327 | |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 328 | struct globals { |
| 329 | int pingsock; |
Denis Vlasenko | 1a7afb4 | 2007-10-19 21:39:25 +0000 | [diff] [blame] | 330 | int if_index; |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 331 | char *str_I; |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 332 | len_and_sockaddr *source_lsa; |
| 333 | unsigned datalen; |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 334 | unsigned pingcount; /* must be int-sized */ |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 335 | unsigned opt_ttl; |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 336 | unsigned long ntransmitted, nreceived, nrepeats; |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 337 | uint16_t myid; |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 338 | unsigned tmin, tmax; /* in us */ |
| 339 | unsigned long long tsum; /* in us, sum of all times */ |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 340 | unsigned deadline; |
| 341 | unsigned timeout; |
| 342 | unsigned total_secs; |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 343 | unsigned sizeof_rcv_packet; |
| 344 | char *rcv_packet; /* [datalen + MAXIPLEN + MAXICMPLEN] */ |
| 345 | void *snd_packet; /* [datalen + ipv4/ipv6_const] */ |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 346 | const char *hostname; |
| 347 | const char *dotted; |
| 348 | union { |
| 349 | struct sockaddr sa; |
| 350 | struct sockaddr_in sin; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 351 | #if ENABLE_PING6 |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 352 | struct sockaddr_in6 sin6; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 353 | #endif |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 354 | } pingaddr; |
| 355 | char rcvd_tbl[MAX_DUP_CHK / 8]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 356 | } FIX_ALIASING; |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 357 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 358 | #define pingsock (G.pingsock ) |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 359 | #define if_index (G.if_index ) |
Denis Vlasenko | 1a7afb4 | 2007-10-19 21:39:25 +0000 | [diff] [blame] | 360 | #define source_lsa (G.source_lsa ) |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 361 | #define str_I (G.str_I ) |
Denis Vlasenko | 1a7afb4 | 2007-10-19 21:39:25 +0000 | [diff] [blame] | 362 | #define datalen (G.datalen ) |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 363 | #define ntransmitted (G.ntransmitted) |
| 364 | #define nreceived (G.nreceived ) |
| 365 | #define nrepeats (G.nrepeats ) |
| 366 | #define pingcount (G.pingcount ) |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 367 | #define opt_ttl (G.opt_ttl ) |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 368 | #define myid (G.myid ) |
| 369 | #define tmin (G.tmin ) |
| 370 | #define tmax (G.tmax ) |
| 371 | #define tsum (G.tsum ) |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 372 | #define deadline (G.deadline ) |
| 373 | #define timeout (G.timeout ) |
| 374 | #define total_secs (G.total_secs ) |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 375 | #define hostname (G.hostname ) |
| 376 | #define dotted (G.dotted ) |
| 377 | #define pingaddr (G.pingaddr ) |
| 378 | #define rcvd_tbl (G.rcvd_tbl ) |
| 379 | void BUG_ping_globals_too_big(void); |
| 380 | #define INIT_G() do { \ |
Denis Vlasenko | 6b40443 | 2008-01-07 16:13:14 +0000 | [diff] [blame] | 381 | if (sizeof(G) > COMMON_BUFSIZE) \ |
| 382 | BUG_ping_globals_too_big(); \ |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 383 | pingsock = -1; \ |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 384 | datalen = DEFDATALEN; \ |
| 385 | timeout = MAXWAIT; \ |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 386 | tmin = UINT_MAX; \ |
| 387 | } while (0) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 388 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 389 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 390 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 391 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 392 | #define SET(bit) (A(bit) |= B(bit)) |
| 393 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 394 | #define TST(bit) (A(bit) & B(bit)) |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 395 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 396 | /**************************************************************************/ |
| 397 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 398 | static void print_stats_and_exit(int junk) NORETURN; |
| 399 | static void print_stats_and_exit(int junk UNUSED_PARAM) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 400 | { |
| 401 | signal(SIGINT, SIG_IGN); |
| 402 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 403 | printf("\n--- %s ping statistics ---\n", hostname); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 404 | printf("%lu packets transmitted, ", ntransmitted); |
| 405 | printf("%lu packets received, ", nreceived); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 406 | if (nrepeats) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 407 | printf("%lu duplicates, ", nrepeats); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 408 | if (ntransmitted) |
Denis Vlasenko | aeb4bdd | 2007-01-25 00:00:02 +0000 | [diff] [blame] | 409 | ntransmitted = (ntransmitted - nreceived) * 100 / ntransmitted; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 410 | printf("%lu%% packet loss\n", ntransmitted); |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 411 | if (tmin != UINT_MAX) { |
| 412 | unsigned tavg = tsum / (nreceived + nrepeats); |
| 413 | printf("round-trip min/avg/max = %u.%03u/%u.%03u/%u.%03u ms\n", |
| 414 | tmin / 1000, tmin % 1000, |
| 415 | tavg / 1000, tavg % 1000, |
| 416 | tmax / 1000, tmax % 1000); |
| 417 | } |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 418 | /* if condition is true, exit with 1 -- 'failure' */ |
| 419 | exit(nreceived == 0 || (deadline && nreceived < pingcount)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Denys Vlasenko | 281e7b8 | 2011-02-06 17:51:45 +0100 | [diff] [blame] | 422 | static void sendping_tail(void (*sp)(int), int size_pkt) |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 423 | { |
Denis Vlasenko | c8e9993 | 2007-02-09 18:14:42 +0000 | [diff] [blame] | 424 | int sz; |
| 425 | |
| 426 | CLR((uint16_t)ntransmitted % MAX_DUP_CHK); |
| 427 | ntransmitted++; |
| 428 | |
Denys Vlasenko | 281e7b8 | 2011-02-06 17:51:45 +0100 | [diff] [blame] | 429 | size_pkt += datalen; |
| 430 | |
Denis Vlasenko | c8e9993 | 2007-02-09 18:14:42 +0000 | [diff] [blame] | 431 | /* sizeof(pingaddr) can be larger than real sa size, but I think |
| 432 | * it doesn't matter */ |
Denys Vlasenko | 281e7b8 | 2011-02-06 17:51:45 +0100 | [diff] [blame] | 433 | sz = xsendto(pingsock, G.snd_packet, size_pkt, &pingaddr.sa, sizeof(pingaddr)); |
Denis Vlasenko | c8e9993 | 2007-02-09 18:14:42 +0000 | [diff] [blame] | 434 | if (sz != size_pkt) |
Bernhard Reutner-Fischer | b290889 | 2007-04-12 11:34:39 +0000 | [diff] [blame] | 435 | bb_error_msg_and_die(bb_msg_write_error); |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 436 | |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 437 | if (pingcount == 0 || deadline || ntransmitted < pingcount) { |
| 438 | /* Didn't send all pings yet - schedule next in 1s */ |
| 439 | signal(SIGALRM, sp); |
| 440 | if (deadline) { |
| 441 | total_secs += PINGINTERVAL; |
| 442 | if (total_secs >= deadline) |
| 443 | signal(SIGALRM, print_stats_and_exit); |
| 444 | } |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 445 | alarm(PINGINTERVAL); |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 446 | } else { /* -c NN, and all NN are sent (and no deadline) */ |
| 447 | /* Wait for the last ping to come back. |
| 448 | * -W timeout: wait for a response in seconds. |
| 449 | * Affects only timeout in absense of any responses, |
| 450 | * otherwise ping waits for two RTTs. */ |
| 451 | unsigned expire = timeout; |
| 452 | |
| 453 | if (nreceived) { |
| 454 | /* approx. 2*tmax, in seconds (2 RTT) */ |
| 455 | expire = tmax / (512*1024); |
| 456 | if (expire == 0) |
| 457 | expire = 1; |
| 458 | } |
| 459 | signal(SIGALRM, print_stats_and_exit); |
| 460 | alarm(expire); |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 464 | static void sendping4(int junk UNUSED_PARAM) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 465 | { |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 466 | struct icmp *pkt = G.snd_packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 467 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 468 | //memset(pkt, 0, datalen + ICMP_MINLEN + 4); - G.snd_packet was xzalloced |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 469 | pkt->icmp_type = ICMP_ECHO; |
Denis Vlasenko | b34266b | 2008-04-29 12:31:53 +0000 | [diff] [blame] | 470 | /*pkt->icmp_code = 0;*/ |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 471 | pkt->icmp_cksum = 0; /* cksum is calculated with this field set to 0 */ |
Denis Vlasenko | 919c10d | 2007-01-03 22:14:18 +0000 | [diff] [blame] | 472 | 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] | 473 | pkt->icmp_id = myid; |
Denis Vlasenko | 7b72fc1 | 2007-06-16 13:37:59 +0000 | [diff] [blame] | 474 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 475 | /* If datalen < 4, we store timestamp _past_ the packet, |
| 476 | * but it's ok - we allocated 4 extra bytes in xzalloc() just in case. |
| 477 | */ |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 478 | /*if (datalen >= 4)*/ |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 479 | /* No hton: we'll read it back on the same machine */ |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 480 | *(uint32_t*)&pkt->icmp_dun = monotonic_us(); |
Denis Vlasenko | 7b72fc1 | 2007-06-16 13:37:59 +0000 | [diff] [blame] | 481 | |
Denis Vlasenko | c8e9993 | 2007-02-09 18:14:42 +0000 | [diff] [blame] | 482 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, datalen + ICMP_MINLEN); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 483 | |
Denys Vlasenko | 281e7b8 | 2011-02-06 17:51:45 +0100 | [diff] [blame] | 484 | sendping_tail(sendping4, ICMP_MINLEN); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 485 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 486 | #if ENABLE_PING6 |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 487 | static void sendping6(int junk UNUSED_PARAM) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 488 | { |
Denys Vlasenko | 1bb52a9 | 2011-02-05 03:58:43 +0100 | [diff] [blame] | 489 | struct icmp6_hdr *pkt = G.snd_packet; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 490 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 491 | //memset(pkt, 0, datalen + sizeof(struct icmp6_hdr) + 4); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 492 | pkt->icmp6_type = ICMP6_ECHO_REQUEST; |
Denis Vlasenko | b34266b | 2008-04-29 12:31:53 +0000 | [diff] [blame] | 493 | /*pkt->icmp6_code = 0;*/ |
| 494 | /*pkt->icmp6_cksum = 0;*/ |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 495 | pkt->icmp6_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */ |
| 496 | pkt->icmp6_id = myid; |
Denis Vlasenko | 7b72fc1 | 2007-06-16 13:37:59 +0000 | [diff] [blame] | 497 | |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 498 | /*if (datalen >= 4)*/ |
| 499 | *(uint32_t*)(&pkt->icmp6_data8[4]) = monotonic_us(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 500 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 501 | //TODO? pkt->icmp_cksum = in_cksum(...); |
| 502 | |
Denys Vlasenko | 281e7b8 | 2011-02-06 17:51:45 +0100 | [diff] [blame] | 503 | sendping_tail(sendping6, sizeof(struct icmp6_hdr)); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 504 | } |
| 505 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 506 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 507 | static const char *icmp_type_name(int id) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 508 | { |
| 509 | switch (id) { |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 510 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 511 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 512 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 513 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 514 | case ICMP_ECHO: return "Echo Request"; |
| 515 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 516 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 517 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 518 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 519 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 520 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 521 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 522 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 523 | default: return "unknown ICMP type"; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 526 | #if ENABLE_PING6 |
| 527 | /* RFC3542 changed some definitions from RFC2292 for no good reason, whee! |
| 528 | * the newer 3542 uses a MLD_ prefix where as 2292 uses ICMP6_ prefix */ |
| 529 | #ifndef MLD_LISTENER_QUERY |
| 530 | # define MLD_LISTENER_QUERY ICMP6_MEMBERSHIP_QUERY |
| 531 | #endif |
| 532 | #ifndef MLD_LISTENER_REPORT |
| 533 | # define MLD_LISTENER_REPORT ICMP6_MEMBERSHIP_REPORT |
| 534 | #endif |
| 535 | #ifndef MLD_LISTENER_REDUCTION |
| 536 | # define MLD_LISTENER_REDUCTION ICMP6_MEMBERSHIP_REDUCTION |
| 537 | #endif |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 538 | static const char *icmp6_type_name(int id) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 539 | { |
| 540 | switch (id) { |
| 541 | case ICMP6_DST_UNREACH: return "Destination Unreachable"; |
| 542 | case ICMP6_PACKET_TOO_BIG: return "Packet too big"; |
| 543 | case ICMP6_TIME_EXCEEDED: return "Time Exceeded"; |
| 544 | case ICMP6_PARAM_PROB: return "Parameter Problem"; |
| 545 | case ICMP6_ECHO_REPLY: return "Echo Reply"; |
| 546 | case ICMP6_ECHO_REQUEST: return "Echo Request"; |
| 547 | case MLD_LISTENER_QUERY: return "Listener Query"; |
| 548 | case MLD_LISTENER_REPORT: return "Listener Report"; |
| 549 | case MLD_LISTENER_REDUCTION: return "Listener Reduction"; |
| 550 | default: return "unknown ICMP type"; |
| 551 | } |
| 552 | } |
| 553 | #endif |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 554 | |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 555 | static void unpack_tail(int sz, uint32_t *tp, |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 556 | const char *from_str, |
| 557 | uint16_t recv_seq, int ttl) |
| 558 | { |
| 559 | const char *dupmsg = " (DUP!)"; |
| 560 | unsigned triptime = triptime; /* for gcc */ |
| 561 | |
| 562 | ++nreceived; |
| 563 | |
| 564 | if (tp) { |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 565 | /* (int32_t) cast is for hypothetical 64-bit unsigned */ |
| 566 | /* (doesn't hurt 32-bit real-world anyway) */ |
| 567 | triptime = (int32_t) ((uint32_t)monotonic_us() - *tp); |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 568 | tsum += triptime; |
| 569 | if (triptime < tmin) |
| 570 | tmin = triptime; |
| 571 | if (triptime > tmax) |
| 572 | tmax = triptime; |
| 573 | } |
| 574 | |
| 575 | if (TST(recv_seq % MAX_DUP_CHK)) { |
| 576 | ++nrepeats; |
| 577 | --nreceived; |
| 578 | } else { |
| 579 | SET(recv_seq % MAX_DUP_CHK); |
| 580 | dupmsg += 7; |
| 581 | } |
| 582 | |
| 583 | if (option_mask32 & OPT_QUIET) |
| 584 | return; |
| 585 | |
| 586 | printf("%d bytes from %s: seq=%u ttl=%d", sz, |
| 587 | from_str, recv_seq, ttl); |
| 588 | if (tp) |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 589 | printf(" time=%u.%03u ms", triptime / 1000, triptime % 1000); |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 590 | puts(dupmsg); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 591 | fflush_all(); |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 592 | } |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 593 | static void unpack4(char *buf, int sz, struct sockaddr_in *from) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 594 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 595 | struct icmp *icmppkt; |
| 596 | struct iphdr *iphdr; |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 597 | int hlen; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 598 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 599 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 600 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 601 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 602 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 603 | /* check IP header */ |
| 604 | iphdr = (struct iphdr *) buf; |
| 605 | hlen = iphdr->ihl << 2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 606 | sz -= hlen; |
| 607 | icmppkt = (struct icmp *) (buf + hlen); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 608 | if (icmppkt->icmp_id != myid) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 609 | return; /* not our ping */ |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 610 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 611 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 612 | uint16_t recv_seq = ntohs(icmppkt->icmp_seq); |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 613 | uint32_t *tp = NULL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 614 | |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 615 | if (sz >= ICMP_MINLEN + sizeof(uint32_t)) |
| 616 | tp = (uint32_t *) icmppkt->icmp_data; |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 617 | unpack_tail(sz, tp, |
| 618 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
| 619 | recv_seq, iphdr->ttl); |
| 620 | } else if (icmppkt->icmp_type != ICMP_ECHO) { |
| 621 | bb_error_msg("warning: got ICMP %d (%s)", |
| 622 | icmppkt->icmp_type, |
| 623 | icmp_type_name(icmppkt->icmp_type)); |
Denis Vlasenko | 35d4da0 | 2007-01-22 14:04:27 +0000 | [diff] [blame] | 624 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 625 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 626 | #if ENABLE_PING6 |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 627 | static void unpack6(char *packet, int sz, /*struct sockaddr_in6 *from,*/ int hoplimit) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 628 | { |
| 629 | struct icmp6_hdr *icmppkt; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 630 | char buf[INET6_ADDRSTRLEN]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 631 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 632 | /* discard if too short */ |
| 633 | if (sz < (datalen + sizeof(struct icmp6_hdr))) |
| 634 | return; |
| 635 | |
| 636 | icmppkt = (struct icmp6_hdr *) packet; |
| 637 | if (icmppkt->icmp6_id != myid) |
| 638 | return; /* not our ping */ |
| 639 | |
| 640 | if (icmppkt->icmp6_type == ICMP6_ECHO_REPLY) { |
| 641 | uint16_t recv_seq = ntohs(icmppkt->icmp6_seq); |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 642 | uint32_t *tp = NULL; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 643 | |
Denis Vlasenko | 7679145 | 2007-06-18 08:55:57 +0000 | [diff] [blame] | 644 | if (sz >= sizeof(struct icmp6_hdr) + sizeof(uint32_t)) |
| 645 | tp = (uint32_t *) &icmppkt->icmp6_data8[4]; |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 646 | unpack_tail(sz, tp, |
| 647 | inet_ntop(AF_INET6, &pingaddr.sin6.sin6_addr, |
| 648 | buf, sizeof(buf)), |
| 649 | recv_seq, hoplimit); |
| 650 | } else if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) { |
| 651 | bb_error_msg("warning: got ICMP %d (%s)", |
| 652 | icmppkt->icmp6_type, |
| 653 | icmp6_type_name(icmppkt->icmp6_type)); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 654 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 655 | } |
| 656 | #endif |
| 657 | |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 658 | static void ping4(len_and_sockaddr *lsa) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 659 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 660 | int sockopt; |
| 661 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 662 | pingsock = create_icmp_socket(); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 663 | pingaddr.sin = lsa->u.sin; |
Denis Vlasenko | 5ad1048 | 2007-06-19 22:54:21 +0000 | [diff] [blame] | 664 | if (source_lsa) { |
| 665 | if (setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_IF, |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 666 | &source_lsa->u.sa, source_lsa->len)) |
Denis Vlasenko | 5ad1048 | 2007-06-19 22:54:21 +0000 | [diff] [blame] | 667 | bb_error_msg_and_die("can't set multicast source interface"); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 668 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); |
Denis Vlasenko | 5ad1048 | 2007-06-19 22:54:21 +0000 | [diff] [blame] | 669 | } |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 670 | if (str_I) |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 671 | setsockopt_bindtodevice(pingsock, str_I); |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 672 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 673 | /* enable broadcast pings */ |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 674 | setsockopt_broadcast(pingsock); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 675 | |
Denis Vlasenko | 5770715 | 2008-08-24 00:02:18 +0000 | [diff] [blame] | 676 | /* set recv buf (needed if we can get lots of responses: flood ping, |
| 677 | * broadcast ping etc) */ |
| 678 | sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ |
Denis Vlasenko | 703e202 | 2007-01-22 14:12:08 +0000 | [diff] [blame] | 679 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 680 | |
Denys Vlasenko | 3c8799b | 2010-11-29 12:07:12 +0100 | [diff] [blame] | 681 | if (opt_ttl != 0) { |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 682 | setsockopt(pingsock, IPPROTO_IP, IP_TTL, &opt_ttl, sizeof(opt_ttl)); |
Denys Vlasenko | 3c8799b | 2010-11-29 12:07:12 +0100 | [diff] [blame] | 683 | /* above doesnt affect packets sent to bcast IP, so... */ |
| 684 | setsockopt(pingsock, IPPROTO_IP, IP_MULTICAST_TTL, &opt_ttl, sizeof(opt_ttl)); |
| 685 | } |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 686 | |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 687 | signal(SIGINT, print_stats_and_exit); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 688 | |
| 689 | /* start the ping's going ... */ |
Denis Vlasenko | e935602 | 2007-01-29 18:03:54 +0000 | [diff] [blame] | 690 | sendping4(0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 691 | |
| 692 | /* listen for replies */ |
| 693 | while (1) { |
| 694 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 695 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 696 | int c; |
| 697 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 698 | c = recvfrom(pingsock, G.rcv_packet, G.sizeof_rcv_packet, 0, |
Denis Vlasenko | 44c2eb2 | 2007-01-08 23:55:33 +0000 | [diff] [blame] | 699 | (struct sockaddr *) &from, &fromlen); |
| 700 | if (c < 0) { |
| 701 | if (errno != EINTR) |
| 702 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | continue; |
| 704 | } |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 705 | unpack4(G.rcv_packet, c, &from); |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 706 | if (pingcount && nreceived >= pingcount) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 707 | break; |
| 708 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 709 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 710 | #if ENABLE_PING6 |
| 711 | extern int BUG_bad_offsetof_icmp6_cksum(void); |
| 712 | static void ping6(len_and_sockaddr *lsa) |
| 713 | { |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 714 | int sockopt; |
| 715 | struct msghdr msg; |
| 716 | struct sockaddr_in6 from; |
| 717 | struct iovec iov; |
| 718 | char control_buf[CMSG_SPACE(36)]; |
| 719 | |
| 720 | pingsock = create_icmp6_socket(); |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 721 | pingaddr.sin6 = lsa->u.sin6; |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 722 | /* untested whether "-I addr" really works for IPv6: */ |
| 723 | if (source_lsa) |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 724 | xbind(pingsock, &source_lsa->u.sa, source_lsa->len); |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 725 | if (str_I) |
Denis Vlasenko | e537385 | 2008-12-10 11:12:16 +0000 | [diff] [blame] | 726 | setsockopt_bindtodevice(pingsock, str_I); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 727 | |
| 728 | #ifdef ICMP6_FILTER |
| 729 | { |
| 730 | struct icmp6_filter filt; |
| 731 | if (!(option_mask32 & OPT_VERBOSE)) { |
| 732 | ICMP6_FILTER_SETBLOCKALL(&filt); |
| 733 | ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt); |
| 734 | } else { |
| 735 | ICMP6_FILTER_SETPASSALL(&filt); |
| 736 | } |
| 737 | if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, |
| 738 | sizeof(filt)) < 0) |
| 739 | bb_error_msg_and_die("setsockopt(ICMP6_FILTER)"); |
| 740 | } |
| 741 | #endif /*ICMP6_FILTER*/ |
| 742 | |
| 743 | /* enable broadcast pings */ |
| 744 | setsockopt_broadcast(pingsock); |
| 745 | |
Denis Vlasenko | 5770715 | 2008-08-24 00:02:18 +0000 | [diff] [blame] | 746 | /* set recv buf (needed if we can get lots of responses: flood ping, |
| 747 | * broadcast ping etc) */ |
| 748 | sockopt = (datalen * 2) + 7 * 1024; /* giving it a bit of extra room */ |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 749 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, &sockopt, sizeof(sockopt)); |
| 750 | |
| 751 | sockopt = offsetof(struct icmp6_hdr, icmp6_cksum); |
| 752 | if (offsetof(struct icmp6_hdr, icmp6_cksum) != 2) |
| 753 | BUG_bad_offsetof_icmp6_cksum(); |
| 754 | setsockopt(pingsock, SOL_RAW, IPV6_CHECKSUM, &sockopt, sizeof(sockopt)); |
| 755 | |
| 756 | /* request ttl info to be returned in ancillary data */ |
| 757 | setsockopt(pingsock, SOL_IPV6, IPV6_HOPLIMIT, &const_int_1, sizeof(const_int_1)); |
| 758 | |
| 759 | if (if_index) |
Denis Vlasenko | aeb4bdd | 2007-01-25 00:00:02 +0000 | [diff] [blame] | 760 | pingaddr.sin6.sin6_scope_id = if_index; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 761 | |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 762 | signal(SIGINT, print_stats_and_exit); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 763 | |
| 764 | /* start the ping's going ... */ |
| 765 | sendping6(0); |
| 766 | |
| 767 | /* listen for replies */ |
| 768 | msg.msg_name = &from; |
| 769 | msg.msg_namelen = sizeof(from); |
| 770 | msg.msg_iov = &iov; |
| 771 | msg.msg_iovlen = 1; |
| 772 | msg.msg_control = control_buf; |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 773 | iov.iov_base = G.rcv_packet; |
| 774 | iov.iov_len = G.sizeof_rcv_packet; |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 775 | while (1) { |
| 776 | int c; |
| 777 | struct cmsghdr *mp; |
| 778 | int hoplimit = -1; |
| 779 | msg.msg_controllen = sizeof(control_buf); |
| 780 | |
| 781 | c = recvmsg(pingsock, &msg, 0); |
| 782 | if (c < 0) { |
| 783 | if (errno != EINTR) |
| 784 | bb_perror_msg("recvfrom"); |
| 785 | continue; |
| 786 | } |
| 787 | for (mp = CMSG_FIRSTHDR(&msg); mp; mp = CMSG_NXTHDR(&msg, mp)) { |
| 788 | if (mp->cmsg_level == SOL_IPV6 |
| 789 | && mp->cmsg_type == IPV6_HOPLIMIT |
| 790 | /* don't check len - we trust the kernel: */ |
| 791 | /* && mp->cmsg_len >= CMSG_LEN(sizeof(int)) */ |
| 792 | ) { |
Denys Vlasenko | 57be1ee | 2009-11-26 15:26:31 +0100 | [diff] [blame] | 793 | /*hoplimit = *(int*)CMSG_DATA(mp); - unaligned access */ |
| 794 | move_from_unaligned_int(hoplimit, CMSG_DATA(mp)); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 797 | unpack6(G.rcv_packet, c, /*&from,*/ hoplimit); |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 798 | if (pingcount && nreceived >= pingcount) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 799 | break; |
| 800 | } |
| 801 | } |
| 802 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 803 | |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 804 | static void ping(len_and_sockaddr *lsa) |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 805 | { |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 806 | printf("PING %s (%s)", hostname, dotted); |
| 807 | if (source_lsa) { |
| 808 | printf(" from %s", |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 809 | xmalloc_sockaddr2dotted_noport(&source_lsa->u.sa)); |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 810 | } |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 811 | printf(": %d data bytes\n", datalen); |
| 812 | |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 813 | G.sizeof_rcv_packet = datalen + MAXIPLEN + MAXICMPLEN; |
| 814 | G.rcv_packet = xzalloc(G.sizeof_rcv_packet); |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 815 | #if ENABLE_PING6 |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 816 | if (lsa->u.sa.sa_family == AF_INET6) { |
| 817 | /* +4 reserves a place for timestamp, which may end up sitting |
| 818 | * _after_ packet. Saves one if() - see sendping4/6() */ |
| 819 | G.snd_packet = xzalloc(datalen + sizeof(struct icmp6_hdr) + 4); |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 820 | ping6(lsa); |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 821 | } else |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 822 | #endif |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 823 | { |
| 824 | G.snd_packet = xzalloc(datalen + ICMP_MINLEN + 4); |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 825 | ping4(lsa); |
Denys Vlasenko | 9341fd2 | 2010-03-03 01:10:29 +0100 | [diff] [blame] | 826 | } |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 829 | static int common_ping_main(int opt, char **argv) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 830 | { |
Denis Vlasenko | aeb4bdd | 2007-01-25 00:00:02 +0000 | [diff] [blame] | 831 | len_and_sockaddr *lsa; |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 832 | char *str_s; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 833 | |
Denis Vlasenko | 821cc25 | 2007-06-04 10:33:48 +0000 | [diff] [blame] | 834 | INIT_G(); |
| 835 | |
Joachim Nilsson | 714e2b7 | 2010-11-28 23:01:18 +0100 | [diff] [blame] | 836 | /* exactly one argument needed; -v and -q don't mix; -c NUM, -t NUM, -w NUM, -W NUM */ |
| 837 | opt_complementary = "=1:q--v:v--q:c+:t+:w+:W+"; |
| 838 | opt |= getopt32(argv, OPT_STRING, &pingcount, &str_s, &opt_ttl, &deadline, &timeout, &str_I); |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 839 | if (opt & OPT_s) |
| 840 | datalen = xatou16(str_s); // -s |
| 841 | if (opt & OPT_I) { // -I |
| 842 | if_index = if_nametoindex(str_I); |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 843 | if (!if_index) { |
| 844 | /* TODO: I'm not sure it takes IPv6 unless in [XX:XX..] format */ |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 845 | source_lsa = xdotted2sockaddr(str_I, 0); |
| 846 | str_I = NULL; /* don't try to bind to device later */ |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 847 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 848 | } |
Denis Vlasenko | 1c9ad62 | 2007-05-27 00:53:41 +0000 | [diff] [blame] | 849 | myid = (uint16_t) getpid(); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 850 | hostname = argv[optind]; |
| 851 | #if ENABLE_PING6 |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 852 | { |
| 853 | sa_family_t af = AF_UNSPEC; |
| 854 | if (opt & OPT_IPV4) |
| 855 | af = AF_INET; |
| 856 | if (opt & OPT_IPV6) |
| 857 | af = AF_INET6; |
| 858 | lsa = xhost_and_af2sockaddr(hostname, 0, af); |
| 859 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 860 | #else |
Denis Vlasenko | 42823d5 | 2007-02-04 02:39:08 +0000 | [diff] [blame] | 861 | lsa = xhost_and_af2sockaddr(hostname, 0, AF_INET); |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 862 | #endif |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 863 | |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 864 | if (source_lsa && source_lsa->u.sa.sa_family != lsa->u.sa.sa_family) |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 865 | /* leaking it here... */ |
| 866 | source_lsa = NULL; |
| 867 | |
Bernhard Reutner-Fischer | 8c69afd | 2008-01-29 10:33:34 +0000 | [diff] [blame] | 868 | dotted = xmalloc_sockaddr2dotted_noport(&lsa->u.sa); |
Denis Vlasenko | 9ca26d3 | 2007-02-09 17:32:16 +0000 | [diff] [blame] | 869 | ping(lsa); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 870 | print_stats_and_exit(EXIT_SUCCESS); |
Denis Vlasenko | 90c31b3 | 2008-04-07 00:46:29 +0000 | [diff] [blame] | 871 | /*return EXIT_SUCCESS;*/ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 872 | } |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 873 | #endif /* FEATURE_FANCY_PING */ |
| 874 | |
| 875 | |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 876 | int ping_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 877 | int ping_main(int argc UNUSED_PARAM, char **argv) |
| 878 | { |
| 879 | #if !ENABLE_FEATURE_FANCY_PING |
| 880 | return common_ping_main(AF_UNSPEC, argv); |
| 881 | #else |
| 882 | return common_ping_main(0, argv); |
| 883 | #endif |
| 884 | } |
| 885 | |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 886 | #if ENABLE_PING6 |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 887 | int ping6_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 59f502b | 2008-10-27 11:54:45 +0000 | [diff] [blame] | 888 | int ping6_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 889 | { |
Denys Vlasenko | ad7d94b | 2009-11-20 18:12:12 +0100 | [diff] [blame] | 890 | # if !ENABLE_FEATURE_FANCY_PING |
| 891 | return common_ping_main(AF_INET6, argv); |
| 892 | # else |
| 893 | return common_ping_main(OPT_IPV6, argv); |
| 894 | # endif |
Denis Vlasenko | b9a279b | 2007-01-24 23:53:22 +0000 | [diff] [blame] | 895 | } |
| 896 | #endif |
| 897 | |
| 898 | /* from ping6.c: |
| 899 | * Copyright (c) 1989 The Regents of the University of California. |
| 900 | * All rights reserved. |
| 901 | * |
| 902 | * This code is derived from software contributed to Berkeley by |
| 903 | * Mike Muuss. |
| 904 | * |
| 905 | * Redistribution and use in source and binary forms, with or without |
| 906 | * modification, are permitted provided that the following conditions |
| 907 | * are met: |
| 908 | * 1. Redistributions of source code must retain the above copyright |
| 909 | * notice, this list of conditions and the following disclaimer. |
| 910 | * 2. Redistributions in binary form must reproduce the above copyright |
| 911 | * notice, this list of conditions and the following disclaimer in the |
| 912 | * documentation and/or other materials provided with the distribution. |
| 913 | * |
| 914 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 915 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 916 | * |
| 917 | * 4. Neither the name of the University nor the names of its contributors |
| 918 | * may be used to endorse or promote products derived from this software |
| 919 | * without specific prior written permission. |
| 920 | * |
| 921 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 922 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 923 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 924 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 925 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 926 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 927 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 928 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 929 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 930 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 931 | * SUCH DAMAGE. |
| 932 | */ |