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