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