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