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