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 | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 3 | * $Id: ping.c,v 1.24 2000/09/20 04:33:30 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 | |
| 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 | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 177 | static char *hostname = NULL; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 178 | |
| 179 | static void noresp(int ign) |
| 180 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 181 | printf("No response from %s\n", hostname); |
| 182 | exit(0); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 185 | static void ping(const char *host) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 186 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | struct hostent *h; |
| 188 | struct sockaddr_in pingaddr; |
| 189 | struct icmp *pkt; |
| 190 | int pingsock, c; |
| 191 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
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 | if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 194 | perror("ping: creating a raw socket"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 195 | exit(1); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 196 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 197 | |
| 198 | /* drop root privs if running setuid */ |
| 199 | setuid(getuid()); |
| 200 | |
| 201 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 202 | |
| 203 | pingaddr.sin_family = AF_INET; |
| 204 | if (!(h = gethostbyname(host))) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 205 | errorMsg("unknown host %s\n", host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 206 | exit(1); |
| 207 | } |
| 208 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 209 | hostname = h->h_name; |
| 210 | |
| 211 | pkt = (struct icmp *) packet; |
| 212 | memset(pkt, 0, sizeof(packet)); |
| 213 | pkt->icmp_type = ICMP_ECHO; |
| 214 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 215 | |
| 216 | c = sendto(pingsock, packet, sizeof(packet), 0, |
| 217 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 218 | |
| 219 | if (c < 0 || c != sizeof(packet)) { |
| 220 | if (c < 0) |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 221 | perror("ping: sendto"); |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 222 | errorMsg("write incomplete\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | exit(1); |
| 224 | } |
| 225 | |
| 226 | signal(SIGALRM, noresp); |
| 227 | alarm(5); /* give the host 5000ms to respond */ |
| 228 | /* listen for replies */ |
| 229 | while (1) { |
| 230 | struct sockaddr_in from; |
| 231 | size_t fromlen = sizeof(from); |
| 232 | |
| 233 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 234 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 235 | if (errno == EINTR) |
| 236 | continue; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 237 | perror("ping: recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | if (c >= 76) { /* ip + icmp */ |
| 241 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 242 | |
| 243 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 244 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | printf("%s is alive!\n", hostname); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 249 | return; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | extern int ping_main(int argc, char **argv) |
| 253 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 254 | argc--; |
| 255 | argv++; |
| 256 | if (argc < 1) |
| 257 | usage(ping_usage); |
| 258 | ping(*argv); |
| 259 | exit(TRUE); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 262 | #else /* ! BB_FEATURE_SIMPLE_PING */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 263 | /* full(er) version */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 264 | static char *hostname = NULL; |
| 265 | static struct sockaddr_in pingaddr; |
| 266 | static int pingsock = -1; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 267 | static int datalen = DEFDATALEN; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 268 | |
| 269 | static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0; |
| 270 | static int myid = 0, options = 0; |
| 271 | static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0; |
| 272 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 273 | |
| 274 | static void sendping(int); |
| 275 | static void pingstats(int); |
| 276 | static void unpack(char *, int, struct sockaddr_in *); |
| 277 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 278 | /**************************************************************************/ |
| 279 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 280 | static void pingstats(int junk) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 281 | { |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 282 | int status; |
| 283 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 284 | signal(SIGINT, SIG_IGN); |
| 285 | |
| 286 | printf("\n--- %s ping statistics ---\n", hostname); |
| 287 | printf("%ld packets transmitted, ", ntransmitted); |
| 288 | printf("%ld packets received, ", nreceived); |
| 289 | if (nrepeats) |
| 290 | printf("%ld duplicates, ", nrepeats); |
| 291 | if (ntransmitted) |
| 292 | printf("%ld%% packet loss\n", |
| 293 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 294 | if (nreceived) |
| 295 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 296 | tmin / 10, tmin % 10, |
| 297 | (tsum / (nreceived + nrepeats)) / 10, |
| 298 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 299 | if (nreceived != 0) |
| 300 | status = EXIT_SUCCESS; |
| 301 | else |
| 302 | status = EXIT_FAILURE; |
| 303 | exit(status); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 306 | static void sendping(int junk) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 307 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 308 | struct icmp *pkt; |
| 309 | int i; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 310 | char packet[datalen + 8]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 311 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 313 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 314 | pkt->icmp_type = ICMP_ECHO; |
| 315 | pkt->icmp_code = 0; |
| 316 | pkt->icmp_cksum = 0; |
| 317 | pkt->icmp_seq = ntransmitted++; |
| 318 | pkt->icmp_id = myid; |
| 319 | CLR(pkt->icmp_seq % MAX_DUP_CHK); |
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 | gettimeofday((struct timeval *) &packet[8], NULL); |
| 322 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 323 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 324 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 325 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 326 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 327 | if (i < 0) |
Matt Kraai | be84cd4 | 2000-07-12 17:02:35 +0000 | [diff] [blame] | 328 | fatalError("sendto: %s\n", strerror(errno)); |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 329 | else if ((size_t)i != sizeof(packet)) |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 330 | fatalError("ping wrote %d chars; %d expected\n", i, |
| 331 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 332 | |
| 333 | signal(SIGALRM, sendping); |
| 334 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 335 | alarm(PINGINTERVAL); |
| 336 | } else { /* done, wait for the last ping to come back */ |
| 337 | /* todo, don't necessarily need to wait so long... */ |
| 338 | signal(SIGALRM, pingstats); |
| 339 | alarm(MAXWAIT); |
| 340 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 341 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 342 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 343 | static char *icmp_type_name (int id) |
| 344 | { |
| 345 | switch (id) { |
| 346 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 347 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 348 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 349 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 350 | case ICMP_ECHO: return "Echo Request"; |
| 351 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 352 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 353 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 354 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 355 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 356 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 357 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 358 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 359 | default: return "unknown ICMP type"; |
| 360 | } |
| 361 | } |
| 362 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 363 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 364 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 365 | struct icmp *icmppkt; |
| 366 | struct iphdr *iphdr; |
| 367 | struct timeval tv, *tp; |
| 368 | int hlen, dupflag; |
| 369 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 370 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 371 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 372 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 373 | /* check IP header */ |
| 374 | iphdr = (struct iphdr *) buf; |
| 375 | hlen = iphdr->ihl << 2; |
| 376 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 377 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 378 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 379 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | sz -= hlen; |
| 381 | icmppkt = (struct icmp *) (buf + hlen); |
| 382 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 383 | if (icmppkt->icmp_id != myid) |
| 384 | return; /* not our ping */ |
| 385 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 387 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | tp = (struct timeval *) icmppkt->icmp_data; |
| 389 | |
| 390 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 391 | --tv.tv_sec; |
| 392 | tv.tv_usec += 1000000; |
| 393 | } |
| 394 | tv.tv_sec -= tp->tv_sec; |
| 395 | |
| 396 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 397 | tsum += triptime; |
| 398 | if (triptime < tmin) |
| 399 | tmin = triptime; |
| 400 | if (triptime > tmax) |
| 401 | tmax = triptime; |
| 402 | |
| 403 | if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) { |
| 404 | ++nrepeats; |
| 405 | --nreceived; |
| 406 | dupflag = 1; |
| 407 | } else { |
| 408 | SET(icmppkt->icmp_seq % MAX_DUP_CHK); |
| 409 | dupflag = 0; |
| 410 | } |
| 411 | |
| 412 | if (options & O_QUIET) |
| 413 | return; |
| 414 | |
| 415 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 416 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
| 417 | icmppkt->icmp_seq); |
| 418 | printf(" ttl=%d", iphdr->ttl); |
| 419 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 420 | if (dupflag) |
| 421 | printf(" (DUP!)"); |
| 422 | printf("\n"); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 423 | } else |
| 424 | if (icmppkt->icmp_type != ICMP_ECHO) |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 425 | errorMsg("Warning: Got ICMP %d (%s)\n", |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 426 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 429 | static void ping(const char *host) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 430 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 431 | struct protoent *proto; |
| 432 | struct hostent *h; |
| 433 | char buf[MAXHOSTNAMELEN]; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 434 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 435 | int sockopt; |
| 436 | |
| 437 | proto = getprotobyname("icmp"); |
| 438 | /* if getprotobyname failed, just silently force |
| 439 | * proto->p_proto to have the correct value for "icmp" */ |
| 440 | if ((pingsock = socket(AF_INET, SOCK_RAW, |
| 441 | (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */ |
| 442 | if (errno == EPERM) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 443 | errorMsg("permission denied. (are you root?)\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 444 | } else { |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 445 | perror("ping: creating a raw socket"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | } |
| 447 | exit(1); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 448 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 449 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 450 | /* drop root privs if running setuid */ |
| 451 | setuid(getuid()); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 452 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 453 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 454 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 455 | pingaddr.sin_family = AF_INET; |
| 456 | if (!(h = gethostbyname(host))) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 457 | errorMsg("unknown host %s\n", host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | exit(1); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 459 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 460 | |
| 461 | if (h->h_addrtype != AF_INET) { |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 462 | errorMsg("unknown address type; only AF_INET is currently supported.\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 463 | exit(1); |
| 464 | } |
| 465 | |
| 466 | pingaddr.sin_family = AF_INET; /* h->h_addrtype */ |
| 467 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
| 468 | strncpy(buf, h->h_name, sizeof(buf) - 1); |
| 469 | hostname = buf; |
| 470 | |
| 471 | /* enable broadcast pings */ |
| 472 | sockopt = 1; |
| 473 | setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, |
| 474 | sizeof(sockopt)); |
| 475 | |
| 476 | /* set recv buf for broadcast pings */ |
| 477 | sockopt = 48 * 1024; |
| 478 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 479 | sizeof(sockopt)); |
| 480 | |
| 481 | printf("PING %s (%s): %d data bytes\n", |
| 482 | hostname, |
| 483 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 484 | datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 485 | |
| 486 | signal(SIGINT, pingstats); |
| 487 | |
| 488 | /* start the ping's going ... */ |
| 489 | sendping(0); |
| 490 | |
| 491 | /* listen for replies */ |
| 492 | while (1) { |
| 493 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 494 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 495 | int c; |
| 496 | |
| 497 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 498 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 499 | if (errno == EINTR) |
| 500 | continue; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 501 | perror("ping: recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 502 | continue; |
| 503 | } |
| 504 | unpack(packet, c, &from); |
| 505 | if (pingcount > 0 && nreceived >= pingcount) |
| 506 | break; |
| 507 | } |
| 508 | pingstats(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | extern int ping_main(int argc, char **argv) |
| 512 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 513 | char *thisarg; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 514 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 515 | argc--; |
| 516 | argv++; |
| 517 | options = 0; |
| 518 | /* Parse any options */ |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 519 | while (argc >= 1 && **argv == '-') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 520 | thisarg = *argv; |
| 521 | thisarg++; |
| 522 | switch (*thisarg) { |
| 523 | case 'q': |
| 524 | options |= O_QUIET; |
| 525 | break; |
| 526 | case 'c': |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 527 | if (--argc <= 0) |
| 528 | usage(ping_usage); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | argv++; |
| 530 | pingcount = atoi(*argv); |
| 531 | break; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 532 | case 's': |
| 533 | if (--argc <= 0) |
| 534 | usage(ping_usage); |
| 535 | argv++; |
| 536 | datalen = atoi(*argv); |
| 537 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 538 | default: |
| 539 | usage(ping_usage); |
| 540 | } |
| 541 | argc--; |
| 542 | argv++; |
| 543 | } |
| 544 | if (argc < 1) |
| 545 | usage(ping_usage); |
| 546 | |
| 547 | myid = getpid() & 0xFFFF; |
| 548 | ping(*argv); |
Eric Andersen | 9ca57d3 | 2000-06-19 18:51:53 +0000 | [diff] [blame] | 549 | return(TRUE); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 550 | } |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 551 | #endif /* ! BB_FEATURE_SIMPLE_PING */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 552 | |
| 553 | /* |
| 554 | * Copyright (c) 1989 The Regents of the University of California. |
| 555 | * All rights reserved. |
| 556 | * |
| 557 | * This code is derived from software contributed to Berkeley by |
| 558 | * Mike Muuss. |
| 559 | * |
| 560 | * Redistribution and use in source and binary forms, with or without |
| 561 | * modification, are permitted provided that the following conditions |
| 562 | * are met: |
| 563 | * 1. Redistributions of source code must retain the above copyright |
| 564 | * notice, this list of conditions and the following disclaimer. |
| 565 | * 2. Redistributions in binary form must reproduce the above copyright |
| 566 | * notice, this list of conditions and the following disclaimer in the |
| 567 | * documentation and/or other materials provided with the distribution. |
| 568 | * 3. All advertising materials mentioning features or use of this software |
| 569 | * must display the following acknowledgement: |
| 570 | * This product includes software developed by the University of |
| 571 | * California, Berkeley and its contributors. |
| 572 | * 4. Neither the name of the University nor the names of its contributors |
| 573 | * may be used to endorse or promote products derived from this software |
| 574 | * without specific prior written permission. |
| 575 | * |
| 576 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 577 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 578 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 579 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 580 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 581 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 582 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 583 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 584 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 585 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 586 | * SUCH DAMAGE. |
| 587 | */ |