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 | /* |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 3 | * $Id: ping.c,v 1.32 2001/01/23 22:30:04 markw Exp $ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 4 | * Mini ping implementation for busybox |
| 5 | * |
| 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This version of ping is adapted from the ping in netkit-base 0.10, |
| 23 | * which is: |
| 24 | * |
| 25 | * Copyright (c) 1989 The Regents of the University of California. |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * This code is derived from software contributed to Berkeley by |
| 29 | * Mike Muuss. |
| 30 | * |
| 31 | * Original copyright notice is retained at the end of this file. |
| 32 | */ |
| 33 | |
Eric Andersen | 6705986 | 2001-01-22 22:48:42 +0000 | [diff] [blame] | 34 | #warning This applet has moved to netkit-tiny. After BusyBox 0.49, this |
| 35 | #warning applet will be removed from BusyBox. All maintainence efforts |
| 36 | #warning should be done in the netkit-tiny source tree. |
| 37 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 38 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 39 | #include <sys/param.h> |
| 40 | #include <sys/socket.h> |
| 41 | #include <sys/file.h> |
| 42 | #include <sys/time.h> |
| 43 | #include <sys/times.h> |
| 44 | #include <sys/signal.h> |
| 45 | |
| 46 | #include <netinet/in.h> |
| 47 | #include <netinet/ip.h> |
| 48 | #include <netinet/ip_icmp.h> |
| 49 | #include <arpa/inet.h> |
| 50 | #include <netdb.h> |
| 51 | #include <stdio.h> |
| 52 | #include <stdlib.h> |
| 53 | #include <errno.h> |
| 54 | |
Eric Andersen | 9ca57d3 | 2000-06-19 18:51:53 +0000 | [diff] [blame] | 55 | |
| 56 | /* It turns out that libc5 doesn't have proper icmp support |
| 57 | * built into it header files, so we have to supplement it */ |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 58 | #if ! defined __GLIBC__ && ! defined __UCLIBC__ |
Eric Andersen | 9ca57d3 | 2000-06-19 18:51:53 +0000 | [diff] [blame] | 59 | typedef unsigned int socklen_t; |
| 60 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 61 | static const int ICMP_MINLEN = 8; /* abs minimum */ |
Eric Andersen | 9ca57d3 | 2000-06-19 18:51:53 +0000 | [diff] [blame] | 62 | |
| 63 | struct icmp_ra_addr |
| 64 | { |
| 65 | u_int32_t ira_addr; |
| 66 | u_int32_t ira_preference; |
| 67 | }; |
| 68 | |
| 69 | |
| 70 | struct icmp |
| 71 | { |
| 72 | u_int8_t icmp_type; /* type of message, see below */ |
| 73 | u_int8_t icmp_code; /* type sub code */ |
| 74 | u_int16_t icmp_cksum; /* ones complement checksum of struct */ |
| 75 | union |
| 76 | { |
| 77 | u_char ih_pptr; /* ICMP_PARAMPROB */ |
| 78 | struct in_addr ih_gwaddr; /* gateway address */ |
| 79 | struct ih_idseq /* echo datagram */ |
| 80 | { |
| 81 | u_int16_t icd_id; |
| 82 | u_int16_t icd_seq; |
| 83 | } ih_idseq; |
| 84 | u_int32_t ih_void; |
| 85 | |
| 86 | /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */ |
| 87 | struct ih_pmtu |
| 88 | { |
| 89 | u_int16_t ipm_void; |
| 90 | u_int16_t ipm_nextmtu; |
| 91 | } ih_pmtu; |
| 92 | |
| 93 | struct ih_rtradv |
| 94 | { |
| 95 | u_int8_t irt_num_addrs; |
| 96 | u_int8_t irt_wpa; |
| 97 | u_int16_t irt_lifetime; |
| 98 | } ih_rtradv; |
| 99 | } icmp_hun; |
| 100 | #define icmp_pptr icmp_hun.ih_pptr |
| 101 | #define icmp_gwaddr icmp_hun.ih_gwaddr |
| 102 | #define icmp_id icmp_hun.ih_idseq.icd_id |
| 103 | #define icmp_seq icmp_hun.ih_idseq.icd_seq |
| 104 | #define icmp_void icmp_hun.ih_void |
| 105 | #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void |
| 106 | #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu |
| 107 | #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs |
| 108 | #define icmp_wpa icmp_hun.ih_rtradv.irt_wpa |
| 109 | #define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime |
| 110 | union |
| 111 | { |
| 112 | struct |
| 113 | { |
| 114 | u_int32_t its_otime; |
| 115 | u_int32_t its_rtime; |
| 116 | u_int32_t its_ttime; |
| 117 | } id_ts; |
| 118 | struct |
| 119 | { |
| 120 | struct ip idi_ip; |
| 121 | /* options and then 64 bits of data */ |
| 122 | } id_ip; |
| 123 | struct icmp_ra_addr id_radv; |
| 124 | u_int32_t id_mask; |
| 125 | u_int8_t id_data[1]; |
| 126 | } icmp_dun; |
| 127 | #define icmp_otime icmp_dun.id_ts.its_otime |
| 128 | #define icmp_rtime icmp_dun.id_ts.its_rtime |
| 129 | #define icmp_ttime icmp_dun.id_ts.its_ttime |
| 130 | #define icmp_ip icmp_dun.id_ip.idi_ip |
| 131 | #define icmp_radv icmp_dun.id_radv |
| 132 | #define icmp_mask icmp_dun.id_mask |
| 133 | #define icmp_data icmp_dun.id_data |
| 134 | }; |
| 135 | #endif |
| 136 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 137 | static const int DEFDATALEN = 56; |
| 138 | static const int MAXIPLEN = 60; |
| 139 | static const int MAXICMPLEN = 76; |
| 140 | static const int MAXPACKET = 65468; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 141 | #define MAX_DUP_CHK (8 * 128) |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 142 | static const int MAXWAIT = 10; |
| 143 | static const int PINGINTERVAL = 1; /* second */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 144 | |
| 145 | #define O_QUIET (1 << 0) |
| 146 | |
| 147 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 148 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 149 | #define SET(bit) (A(bit) |= B(bit)) |
| 150 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 151 | #define TST(bit) (A(bit) & B(bit)) |
| 152 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 153 | static void ping(const char *host); |
| 154 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 155 | /* common routines */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 156 | static int in_cksum(unsigned short *buf, int sz) |
| 157 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 158 | int nleft = sz; |
| 159 | int sum = 0; |
| 160 | unsigned short *w = buf; |
| 161 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 162 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 163 | while (nleft > 1) { |
| 164 | sum += *w++; |
| 165 | nleft -= 2; |
| 166 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 167 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 168 | if (nleft == 1) { |
| 169 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 170 | sum += ans; |
| 171 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 174 | sum += (sum >> 16); |
| 175 | ans = ~sum; |
| 176 | return (ans); |
| 177 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 178 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 179 | /* simple version */ |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 180 | #ifdef BB_FEATURE_SIMPLE_PING |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | static char *hostname = NULL; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 182 | |
| 183 | static void noresp(int ign) |
| 184 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 185 | printf("No response from %s\n", hostname); |
| 186 | exit(0); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 189 | static void ping(const char *host) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 190 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 191 | struct hostent *h; |
| 192 | struct sockaddr_in pingaddr; |
| 193 | struct icmp *pkt; |
| 194 | int pingsock, c; |
| 195 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 196 | |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 197 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) /* 1 == ICMP */ |
| 198 | perror_msg_and_die("creating a raw socket"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 199 | |
| 200 | /* drop root privs if running setuid */ |
| 201 | setuid(getuid()); |
| 202 | |
| 203 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 204 | |
| 205 | pingaddr.sin_family = AF_INET; |
| 206 | if (!(h = gethostbyname(host))) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 207 | error_msg("unknown host %s\n", host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 208 | exit(1); |
| 209 | } |
| 210 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 211 | hostname = h->h_name; |
| 212 | |
| 213 | pkt = (struct icmp *) packet; |
| 214 | memset(pkt, 0, sizeof(packet)); |
| 215 | pkt->icmp_type = ICMP_ECHO; |
| 216 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 217 | |
| 218 | c = sendto(pingsock, packet, sizeof(packet), 0, |
| 219 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 220 | |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 221 | if (c < 0 || c != sizeof(packet)) |
| 222 | perror_msg_and_die("sendto"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | |
| 224 | signal(SIGALRM, noresp); |
| 225 | alarm(5); /* give the host 5000ms to respond */ |
| 226 | /* listen for replies */ |
| 227 | while (1) { |
| 228 | struct sockaddr_in from; |
| 229 | size_t fromlen = sizeof(from); |
| 230 | |
| 231 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 232 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 233 | if (errno == EINTR) |
| 234 | continue; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 235 | perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | continue; |
| 237 | } |
| 238 | if (c >= 76) { /* ip + icmp */ |
| 239 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 240 | |
| 241 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 242 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | printf("%s is alive!\n", hostname); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 247 | return; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | extern int ping_main(int argc, char **argv) |
| 251 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 252 | argc--; |
| 253 | argv++; |
| 254 | if (argc < 1) |
| 255 | usage(ping_usage); |
| 256 | ping(*argv); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 257 | return EXIT_SUCCESS; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 260 | #else /* ! BB_FEATURE_SIMPLE_PING */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 261 | /* full(er) version */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 262 | static char *hostname = NULL; |
| 263 | static struct sockaddr_in pingaddr; |
| 264 | static int pingsock = -1; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 265 | static int datalen; /* intentionally uninitialized to work around gcc bug */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 266 | |
| 267 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; |
| 268 | static int myid = 0, options = 0; |
| 269 | static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0; |
| 270 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 271 | |
| 272 | static void sendping(int); |
| 273 | static void pingstats(int); |
| 274 | static void unpack(char *, int, struct sockaddr_in *); |
| 275 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 276 | /**************************************************************************/ |
| 277 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 278 | static void pingstats(int junk) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 279 | { |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 280 | int status; |
| 281 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 282 | signal(SIGINT, SIG_IGN); |
| 283 | |
| 284 | printf("\n--- %s ping statistics ---\n", hostname); |
| 285 | printf("%ld packets transmitted, ", ntransmitted); |
| 286 | printf("%ld packets received, ", nreceived); |
| 287 | if (nrepeats) |
| 288 | printf("%ld duplicates, ", nrepeats); |
| 289 | if (ntransmitted) |
| 290 | printf("%ld%% packet loss\n", |
| 291 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 292 | if (nreceived) |
| 293 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 294 | tmin / 10, tmin % 10, |
| 295 | (tsum / (nreceived + nrepeats)) / 10, |
| 296 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 297 | if (nreceived != 0) |
| 298 | status = EXIT_SUCCESS; |
| 299 | else |
| 300 | status = EXIT_FAILURE; |
| 301 | exit(status); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 304 | static void sendping(int junk) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 305 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 306 | struct icmp *pkt; |
| 307 | int i; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 308 | char packet[datalen + 8]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 309 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 310 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 311 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | pkt->icmp_type = ICMP_ECHO; |
| 313 | pkt->icmp_code = 0; |
| 314 | pkt->icmp_cksum = 0; |
| 315 | pkt->icmp_seq = ntransmitted++; |
| 316 | pkt->icmp_id = myid; |
| 317 | CLR(pkt->icmp_seq % MAX_DUP_CHK); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 318 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 319 | gettimeofday((struct timeval *) &packet[8], NULL); |
| 320 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 321 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 322 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 323 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 324 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 325 | if (i < 0) |
Matt Kraai | 1fa1ade | 2000-12-18 03:57:16 +0000 | [diff] [blame] | 326 | perror_msg_and_die("sendto"); |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 327 | else if ((size_t)i != sizeof(packet)) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 328 | error_msg_and_die("ping wrote %d chars; %d expected\n", i, |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 329 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | |
| 331 | signal(SIGALRM, sendping); |
| 332 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 333 | alarm(PINGINTERVAL); |
| 334 | } else { /* done, wait for the last ping to come back */ |
| 335 | /* todo, don't necessarily need to wait so long... */ |
| 336 | signal(SIGALRM, pingstats); |
| 337 | alarm(MAXWAIT); |
| 338 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 339 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 340 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 341 | static char *icmp_type_name (int id) |
| 342 | { |
| 343 | switch (id) { |
| 344 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 345 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 346 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 347 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 348 | case ICMP_ECHO: return "Echo Request"; |
| 349 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 350 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 351 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 352 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 353 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 354 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 355 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 356 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 357 | default: return "unknown ICMP type"; |
| 358 | } |
| 359 | } |
| 360 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 361 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 362 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 363 | struct icmp *icmppkt; |
| 364 | struct iphdr *iphdr; |
| 365 | struct timeval tv, *tp; |
| 366 | int hlen, dupflag; |
| 367 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 368 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 369 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 370 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 371 | /* check IP header */ |
| 372 | iphdr = (struct iphdr *) buf; |
| 373 | hlen = iphdr->ihl << 2; |
| 374 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 375 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 376 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 377 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 378 | sz -= hlen; |
| 379 | icmppkt = (struct icmp *) (buf + hlen); |
| 380 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 381 | if (icmppkt->icmp_id != myid) |
| 382 | return; /* not our ping */ |
| 383 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 384 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 385 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | tp = (struct timeval *) icmppkt->icmp_data; |
| 387 | |
| 388 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 389 | --tv.tv_sec; |
| 390 | tv.tv_usec += 1000000; |
| 391 | } |
| 392 | tv.tv_sec -= tp->tv_sec; |
| 393 | |
| 394 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 395 | tsum += triptime; |
| 396 | if (triptime < tmin) |
| 397 | tmin = triptime; |
| 398 | if (triptime > tmax) |
| 399 | tmax = triptime; |
| 400 | |
| 401 | if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) { |
| 402 | ++nrepeats; |
| 403 | --nreceived; |
| 404 | dupflag = 1; |
| 405 | } else { |
| 406 | SET(icmppkt->icmp_seq % MAX_DUP_CHK); |
| 407 | dupflag = 0; |
| 408 | } |
| 409 | |
| 410 | if (options & O_QUIET) |
| 411 | return; |
| 412 | |
| 413 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 414 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
| 415 | icmppkt->icmp_seq); |
| 416 | printf(" ttl=%d", iphdr->ttl); |
| 417 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 418 | if (dupflag) |
| 419 | printf(" (DUP!)"); |
| 420 | printf("\n"); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 421 | } else |
| 422 | if (icmppkt->icmp_type != ICMP_ECHO) |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 423 | error_msg("Warning: Got ICMP %d (%s)\n", |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 424 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 427 | static void ping(const char *host) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 428 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | struct protoent *proto; |
| 430 | struct hostent *h; |
| 431 | char buf[MAXHOSTNAMELEN]; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 432 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 433 | int sockopt; |
| 434 | |
| 435 | proto = getprotobyname("icmp"); |
| 436 | /* if getprotobyname failed, just silently force |
| 437 | * proto->p_proto to have the correct value for "icmp" */ |
| 438 | if ((pingsock = socket(AF_INET, SOCK_RAW, |
| 439 | (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */ |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 440 | if (errno == EPERM) |
| 441 | error_msg_and_die("permission denied. (are you root?)\n"); |
| 442 | else |
| 443 | perror_msg_and_die("creating a raw socket"); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 444 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 445 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | /* drop root privs if running setuid */ |
| 447 | setuid(getuid()); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 448 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 449 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 450 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | pingaddr.sin_family = AF_INET; |
| 452 | if (!(h = gethostbyname(host))) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 453 | error_msg("unknown host %s\n", host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 454 | exit(1); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 455 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 456 | |
| 457 | if (h->h_addrtype != AF_INET) { |
Mark Whitley | f57c944 | 2000-12-07 19:56:48 +0000 | [diff] [blame] | 458 | error_msg("unknown address type; only AF_INET is currently supported.\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 459 | exit(1); |
| 460 | } |
| 461 | |
| 462 | pingaddr.sin_family = AF_INET; /* h->h_addrtype */ |
| 463 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 464 | strncpy(buf, h->h_name, sizeof(buf) - 1); |
| 465 | hostname = buf; |
| 466 | |
| 467 | /* enable broadcast pings */ |
| 468 | sockopt = 1; |
| 469 | setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, |
| 470 | sizeof(sockopt)); |
| 471 | |
| 472 | /* set recv buf for broadcast pings */ |
| 473 | sockopt = 48 * 1024; |
| 474 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 475 | sizeof(sockopt)); |
| 476 | |
| 477 | printf("PING %s (%s): %d data bytes\n", |
| 478 | hostname, |
| 479 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 480 | datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 481 | |
| 482 | signal(SIGINT, pingstats); |
| 483 | |
| 484 | /* start the ping's going ... */ |
| 485 | sendping(0); |
| 486 | |
| 487 | /* listen for replies */ |
| 488 | while (1) { |
| 489 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 490 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 491 | int c; |
| 492 | |
| 493 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 494 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 495 | if (errno == EINTR) |
| 496 | continue; |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 497 | perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | continue; |
| 499 | } |
| 500 | unpack(packet, c, &from); |
| 501 | if (pingcount > 0 && nreceived >= pingcount) |
| 502 | break; |
| 503 | } |
| 504 | pingstats(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | extern int ping_main(int argc, char **argv) |
| 508 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 509 | char *thisarg; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 510 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame^] | 511 | datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ |
| 512 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | argc--; |
| 514 | argv++; |
| 515 | options = 0; |
| 516 | /* Parse any options */ |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 517 | while (argc >= 1 && **argv == '-') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 518 | thisarg = *argv; |
| 519 | thisarg++; |
| 520 | switch (*thisarg) { |
| 521 | case 'q': |
| 522 | options |= O_QUIET; |
| 523 | break; |
| 524 | case 'c': |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 525 | if (--argc <= 0) |
| 526 | usage(ping_usage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 527 | argv++; |
| 528 | pingcount = atoi(*argv); |
| 529 | break; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 530 | case 's': |
| 531 | if (--argc <= 0) |
| 532 | usage(ping_usage); |
| 533 | argv++; |
| 534 | datalen = atoi(*argv); |
| 535 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 536 | default: |
| 537 | usage(ping_usage); |
| 538 | } |
| 539 | argc--; |
| 540 | argv++; |
| 541 | } |
| 542 | if (argc < 1) |
| 543 | usage(ping_usage); |
| 544 | |
| 545 | myid = getpid() & 0xFFFF; |
| 546 | ping(*argv); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 547 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 548 | } |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 549 | #endif /* ! BB_FEATURE_SIMPLE_PING */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 550 | |
| 551 | /* |
| 552 | * Copyright (c) 1989 The Regents of the University of California. |
| 553 | * All rights reserved. |
| 554 | * |
| 555 | * This code is derived from software contributed to Berkeley by |
| 556 | * Mike Muuss. |
| 557 | * |
| 558 | * Redistribution and use in source and binary forms, with or without |
| 559 | * modification, are permitted provided that the following conditions |
| 560 | * are met: |
| 561 | * 1. Redistributions of source code must retain the above copyright |
| 562 | * notice, this list of conditions and the following disclaimer. |
| 563 | * 2. Redistributions in binary form must reproduce the above copyright |
| 564 | * notice, this list of conditions and the following disclaimer in the |
| 565 | * documentation and/or other materials provided with the distribution. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 566 | * |
| 567 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 568 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 569 | * |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 570 | * 4. Neither the name of the University nor the names of its contributors |
| 571 | * may be used to endorse or promote products derived from this software |
| 572 | * without specific prior written permission. |
| 573 | * |
| 574 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 575 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 576 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 577 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 578 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 579 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 580 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 581 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 582 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 583 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 584 | * SUCH DAMAGE. |
| 585 | */ |