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