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