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 | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3 | * $Id: ping.c,v 1.56 2004/03/15 08:28:48 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 | * |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 8 | * Adapted from the ping in netkit-base 0.10: |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 9 | * Copyright (c) 1989 The Regents of the University of California. |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 10 | * Derived from software contributed to Berkeley by Mike Muuss. |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 11 | * |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 12 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 15 | #include <sys/param.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <sys/file.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 18 | #include <sys/times.h> |
Mike Frysinger | 06adf5f | 2006-03-22 00:25:07 +0000 | [diff] [blame] | 19 | #include <signal.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 20 | |
| 21 | #include <netinet/in.h> |
| 22 | #include <netinet/ip.h> |
| 23 | #include <netinet/ip_icmp.h> |
| 24 | #include <arpa/inet.h> |
| 25 | #include <netdb.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 33 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 34 | enum { |
| 35 | DEFDATALEN = 56, |
| 36 | MAXIPLEN = 60, |
| 37 | MAXICMPLEN = 76, |
| 38 | MAXPACKET = 65468, |
| 39 | MAX_DUP_CHK = (8 * 128), |
| 40 | MAXWAIT = 10, |
| 41 | PINGINTERVAL = 1 /* second */ |
| 42 | }; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 43 | |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 44 | static void ping(const char *host); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 45 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 46 | /* common routines */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 47 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 48 | static int in_cksum(unsigned short *buf, int sz) |
| 49 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | int nleft = sz; |
| 51 | int sum = 0; |
| 52 | unsigned short *w = buf; |
| 53 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 54 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | while (nleft > 1) { |
| 56 | sum += *w++; |
| 57 | nleft -= 2; |
| 58 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | if (nleft == 1) { |
| 61 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 62 | sum += ans; |
| 63 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 64 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 66 | sum += (sum >> 16); |
| 67 | ans = ~sum; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame^] | 68 | return ans; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 69 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 70 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 71 | #ifndef CONFIG_FEATURE_FANCY_PING |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 72 | |
| 73 | /* simple version */ |
| 74 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 75 | static char *hostname; |
| 76 | |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 77 | static void noresp(int ign) |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 78 | { |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 79 | printf("No response from %s\n", hostname); |
Eric Andersen | 4e486a5 | 2003-01-12 06:08:33 +0000 | [diff] [blame] | 80 | exit(EXIT_FAILURE); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 81 | } |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 82 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 83 | static void ping(const char *host) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 84 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 85 | struct hostent *h; |
| 86 | struct sockaddr_in pingaddr; |
| 87 | struct icmp *pkt; |
| 88 | int pingsock, c; |
| 89 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 90 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 91 | pingsock = create_icmp_socket(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | |
| 93 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 94 | |
| 95 | pingaddr.sin_family = AF_INET; |
Matt Kraai | c55b8d4 | 2001-05-16 15:40:51 +0000 | [diff] [blame] | 96 | h = xgethostbyname(host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 98 | hostname = h->h_name; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 99 | |
| 100 | pkt = (struct icmp *) packet; |
| 101 | memset(pkt, 0, sizeof(packet)); |
| 102 | pkt->icmp_type = ICMP_ECHO; |
| 103 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 104 | |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 105 | c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 107 | |
Rob Landley | 9b1857f | 2006-05-31 23:54:50 +0000 | [diff] [blame] | 108 | if (c < 0) { |
| 109 | if (ENABLE_FEATURE_CLEAN_UP) close(pingsock); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | bb_perror_msg_and_die("sendto"); |
Rob Landley | 9b1857f | 2006-05-31 23:54:50 +0000 | [diff] [blame] | 111 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | |
| 113 | signal(SIGALRM, noresp); |
| 114 | alarm(5); /* give the host 5000ms to respond */ |
| 115 | /* listen for replies */ |
| 116 | while (1) { |
| 117 | struct sockaddr_in from; |
Mike Frysinger | 03e827a | 2005-07-26 23:00:59 +0000 | [diff] [blame] | 118 | socklen_t fromlen = sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 119 | |
| 120 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 121 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 122 | if (errno == EINTR) |
| 123 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 124 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | if (c >= 76) { /* ip + icmp */ |
| 128 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 129 | |
| 130 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 131 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 132 | break; |
| 133 | } |
| 134 | } |
Rob Landley | 9b1857f | 2006-05-31 23:54:50 +0000 | [diff] [blame] | 135 | if (ENABLE_FEATURE_CLEAN_UP) close(pingsock); |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 136 | printf("%s is alive!\n", hostname); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 137 | return; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 140 | int ping_main(int argc, char **argv) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 141 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | argc--; |
| 143 | argv++; |
| 144 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | ping(*argv); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 147 | return EXIT_SUCCESS; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 150 | #else /* ! CONFIG_FEATURE_FANCY_PING */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 151 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 152 | /* full(er) version */ |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 153 | |
| 154 | #define OPT_STRING "qc:s:I:" |
| 155 | enum { |
| 156 | OPT_QUIET = 1 << 0, |
| 157 | }; |
| 158 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 159 | static struct sockaddr_in pingaddr; |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 160 | static struct sockaddr_in sourceaddr; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 161 | static int pingsock = -1; |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 162 | static unsigned datalen; /* intentionally uninitialized to work around gcc bug */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 163 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 164 | static unsigned long ntransmitted, nreceived, nrepeats, pingcount; |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 165 | static int myid; |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 166 | static unsigned long tmin = ULONG_MAX, tmax, tsum; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 167 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 168 | |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 169 | static struct hostent *hostent; |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 170 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 171 | static void sendping(int); |
| 172 | static void pingstats(int); |
| 173 | static void unpack(char *, int, struct sockaddr_in *); |
| 174 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 175 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 176 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 177 | #define SET(bit) (A(bit) |= B(bit)) |
| 178 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 179 | #define TST(bit) (A(bit) & B(bit)) |
| 180 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 181 | /**************************************************************************/ |
| 182 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 183 | static void pingstats(int junk) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 184 | { |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 185 | int status; |
| 186 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 187 | signal(SIGINT, SIG_IGN); |
| 188 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 189 | printf("\n--- %s ping statistics ---\n", hostent->h_name); |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 190 | printf("%lu packets transmitted, ", ntransmitted); |
| 191 | printf("%lu packets received, ", nreceived); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 192 | if (nrepeats) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 193 | printf("%lu duplicates, ", nrepeats); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 194 | if (ntransmitted) |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 195 | printf("%lu%% packet loss\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 197 | if (nreceived) |
| 198 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 199 | tmin / 10, tmin % 10, |
| 200 | (tsum / (nreceived + nrepeats)) / 10, |
| 201 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 202 | if (nreceived != 0) |
| 203 | status = EXIT_SUCCESS; |
| 204 | else |
| 205 | status = EXIT_FAILURE; |
| 206 | exit(status); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 209 | static void sendping(int junk) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 210 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | struct icmp *pkt; |
| 212 | int i; |
Rob Landley | 07a637d | 2006-04-01 17:28:11 +0000 | [diff] [blame] | 213 | char packet[datalen + ICMP_MINLEN]; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 214 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 216 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | pkt->icmp_type = ICMP_ECHO; |
| 218 | pkt->icmp_code = 0; |
| 219 | pkt->icmp_cksum = 0; |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 220 | pkt->icmp_seq = htons(ntransmitted++); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | pkt->icmp_id = myid; |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 222 | CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 223 | |
Rob Landley | bbf4e16 | 2006-01-11 03:44:11 +0000 | [diff] [blame] | 224 | gettimeofday((struct timeval *) &pkt->icmp_dun, NULL); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 225 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 226 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 227 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 228 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 229 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 230 | if (i < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 231 | bb_perror_msg_and_die("sendto"); |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 232 | else if ((size_t)i != sizeof(packet)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 233 | bb_error_msg_and_die("ping wrote %d chars; %d expected", i, |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 234 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 235 | |
| 236 | signal(SIGALRM, sendping); |
| 237 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 238 | alarm(PINGINTERVAL); |
| 239 | } else { /* done, wait for the last ping to come back */ |
| 240 | /* todo, don't necessarily need to wait so long... */ |
| 241 | signal(SIGALRM, pingstats); |
| 242 | alarm(MAXWAIT); |
| 243 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 244 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 245 | |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 246 | static char *icmp_type_name(int id) |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 247 | { |
| 248 | switch (id) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 249 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 250 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 251 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 252 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 253 | case ICMP_ECHO: return "Echo Request"; |
| 254 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 255 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 256 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 257 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 258 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 259 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 260 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 261 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 262 | default: return "unknown ICMP type"; |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 266 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 267 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 268 | struct icmp *icmppkt; |
| 269 | struct iphdr *iphdr; |
| 270 | struct timeval tv, *tp; |
| 271 | int hlen, dupflag; |
| 272 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 273 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 275 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 276 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 277 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 278 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 280 | /* check IP header */ |
| 281 | iphdr = (struct iphdr *) buf; |
| 282 | hlen = iphdr->ihl << 2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 283 | sz -= hlen; |
| 284 | icmppkt = (struct icmp *) (buf + hlen); |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 285 | if (icmppkt->icmp_id != myid) |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 286 | return; /* not our ping */ |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 287 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 288 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 289 | u_int16_t recv_seq = ntohs(icmppkt->icmp_seq); |
Denis Vlasenko | cb6874c | 2006-09-02 16:13:36 +0000 | [diff] [blame] | 290 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 291 | tp = (struct timeval *) icmppkt->icmp_data; |
| 292 | |
| 293 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 294 | --tv.tv_sec; |
| 295 | tv.tv_usec += 1000000; |
| 296 | } |
| 297 | tv.tv_sec -= tp->tv_sec; |
| 298 | |
| 299 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 300 | tsum += triptime; |
| 301 | if (triptime < tmin) |
| 302 | tmin = triptime; |
| 303 | if (triptime > tmax) |
| 304 | tmax = triptime; |
| 305 | |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 306 | if (TST(recv_seq % MAX_DUP_CHK)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 307 | ++nrepeats; |
| 308 | --nreceived; |
| 309 | dupflag = 1; |
| 310 | } else { |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 311 | SET(recv_seq % MAX_DUP_CHK); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | dupflag = 0; |
| 313 | } |
| 314 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 315 | if (option_mask32 & OPT_QUIET) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | return; |
| 317 | |
| 318 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 319 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
Mike Frysinger | 887a1ad | 2005-09-15 01:32:48 +0000 | [diff] [blame] | 320 | recv_seq); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 321 | printf(" ttl=%d", iphdr->ttl); |
| 322 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 323 | if (dupflag) |
| 324 | printf(" (DUP!)"); |
Denis Vlasenko | c6f188d | 2006-10-26 00:37:00 +0000 | [diff] [blame] | 325 | puts(""); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 326 | } else |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 327 | if (icmppkt->icmp_type != ICMP_ECHO) |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 328 | bb_error_msg("warning: got ICMP %d (%s)", |
Denis Vlasenko | e0b7f71 | 2006-09-02 16:57:59 +0000 | [diff] [blame] | 329 | icmppkt->icmp_type, icmp_type_name(icmppkt->icmp_type)); |
Rob Landley | 42eddba | 2005-12-15 08:04:17 +0000 | [diff] [blame] | 330 | fflush(stdout); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 333 | static void ping(const char *host) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 334 | { |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 335 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 336 | int sockopt; |
| 337 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 338 | pingsock = create_icmp_socket(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 339 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 340 | if (sourceaddr.sin_addr.s_addr) { |
Denis Vlasenko | bcf4908 | 2006-09-02 17:53:16 +0000 | [diff] [blame] | 341 | xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr)); |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 344 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 345 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 346 | pingaddr.sin_family = AF_INET; |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 347 | hostent = xgethostbyname(host); |
| 348 | if (hostent->h_addrtype != AF_INET) |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 349 | bb_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] | 350 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 351 | memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 352 | |
| 353 | /* enable broadcast pings */ |
Denis Vlasenko | 48237b0 | 2006-11-22 23:22:06 +0000 | [diff] [blame] | 354 | setsockopt_broadcast(pingsock); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 355 | |
| 356 | /* set recv buf for broadcast pings */ |
| 357 | sockopt = 48 * 1024; |
| 358 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 359 | sizeof(sockopt)); |
| 360 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 361 | printf("PING %s (%s)", |
| 362 | hostent->h_name, |
| 363 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr)); |
| 364 | if (sourceaddr.sin_addr.s_addr) { |
| 365 | printf(" from %s", |
| 366 | inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr)); |
| 367 | } |
| 368 | printf(": %d data bytes\n", datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 369 | |
| 370 | signal(SIGINT, pingstats); |
| 371 | |
| 372 | /* start the ping's going ... */ |
| 373 | sendping(0); |
| 374 | |
| 375 | /* listen for replies */ |
| 376 | while (1) { |
| 377 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 378 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 379 | int c; |
| 380 | |
| 381 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 382 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 383 | if (errno == EINTR) |
| 384 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 385 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | continue; |
| 387 | } |
| 388 | unpack(packet, c, &from); |
| 389 | if (pingcount > 0 && nreceived >= pingcount) |
| 390 | break; |
| 391 | } |
| 392 | pingstats(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Denis Vlasenko | 2cbe6e6 | 2006-09-02 16:17:30 +0000 | [diff] [blame] | 395 | /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c |
| 396 | * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too... |
| 397 | */ |
| 398 | static int parse_nipquad(const char *str, struct sockaddr_in* addr) |
| 399 | { |
| 400 | char dummy; |
| 401 | unsigned i1, i2, i3, i4; |
| 402 | if (sscanf(str, "%u.%u.%u.%u%c", |
| 403 | &i1, &i2, &i3, &i4, &dummy) == 4 |
| 404 | && ( (i1|i2|i3|i4) <= 0xff ) |
| 405 | ) { |
| 406 | uint8_t* ptr = (uint8_t*)&addr->sin_addr; |
| 407 | ptr[0] = i1; |
| 408 | ptr[1] = i2; |
| 409 | ptr[2] = i3; |
| 410 | ptr[3] = i4; |
| 411 | return 0; |
| 412 | } |
| 413 | return 1; /* error */ |
| 414 | } |
| 415 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 416 | int ping_main(int argc, char **argv) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 417 | { |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 418 | char *opt_c, *opt_s, *opt_I; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 419 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 420 | datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ |
| 421 | |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 422 | /* exactly one argument needed */ |
| 423 | opt_complementary = "=1"; |
| 424 | getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I); |
| 425 | if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c |
| 426 | if (option_mask32 & 4) datalen = xatou16(opt_s); // -s |
| 427 | if (option_mask32 & 8) { // -I |
| 428 | /* TODO: ping6 accepts iface too: |
| 429 | if_index = if_nametoindex(*argv); |
| 430 | if (!if_index) ... |
| 431 | make it true for ping. */ |
| 432 | if (parse_nipquad(opt_I, &sourceaddr)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 433 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 434 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | db7f2e5 | 2006-09-02 16:16:23 +0000 | [diff] [blame] | 436 | myid = (int16_t) getpid(); |
Denis Vlasenko | 4a5cf16 | 2006-11-20 00:48:22 +0000 | [diff] [blame] | 437 | ping(argv[optind]); |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 438 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 439 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 440 | #endif /* ! CONFIG_FEATURE_FANCY_PING */ |