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 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | * This version of ping is adapted from the ping in netkit-base 0.10, |
| 23 | * which is: |
| 24 | * |
| 25 | * Copyright (c) 1989 The Regents of the University of California. |
| 26 | * All rights reserved. |
| 27 | * |
| 28 | * This code is derived from software contributed to Berkeley by |
| 29 | * Mike Muuss. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | * |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 31 | * Original copyright notice is retained at the end of this file. |
| 32 | */ |
| 33 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 34 | #include <sys/param.h> |
| 35 | #include <sys/socket.h> |
| 36 | #include <sys/file.h> |
| 37 | #include <sys/time.h> |
| 38 | #include <sys/times.h> |
| 39 | #include <sys/signal.h> |
| 40 | |
| 41 | #include <netinet/in.h> |
| 42 | #include <netinet/ip.h> |
| 43 | #include <netinet/ip_icmp.h> |
| 44 | #include <arpa/inet.h> |
| 45 | #include <netdb.h> |
| 46 | #include <stdio.h> |
| 47 | #include <stdlib.h> |
| 48 | #include <errno.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 49 | #include <unistd.h> |
| 50 | #include <string.h> |
| 51 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 52 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 53 | |
Eric Andersen | 9ca57d3 | 2000-06-19 18:51:53 +0000 | [diff] [blame] | 54 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 55 | static const int DEFDATALEN = 56; |
| 56 | static const int MAXIPLEN = 60; |
| 57 | static const int MAXICMPLEN = 76; |
| 58 | static const int MAXPACKET = 65468; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 59 | #define MAX_DUP_CHK (8 * 128) |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 60 | static const int MAXWAIT = 10; |
| 61 | static const int PINGINTERVAL = 1; /* second */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 62 | |
| 63 | #define O_QUIET (1 << 0) |
| 64 | |
| 65 | #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */ |
| 66 | #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */ |
| 67 | #define SET(bit) (A(bit) |= B(bit)) |
| 68 | #define CLR(bit) (A(bit) &= (~B(bit))) |
| 69 | #define TST(bit) (A(bit) & B(bit)) |
| 70 | |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 71 | static void ping(const char *host); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 72 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 73 | /* common routines */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 74 | static int in_cksum(unsigned short *buf, int sz) |
| 75 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | int nleft = sz; |
| 77 | int sum = 0; |
| 78 | unsigned short *w = buf; |
| 79 | unsigned short ans = 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 80 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | while (nleft > 1) { |
| 82 | sum += *w++; |
| 83 | nleft -= 2; |
| 84 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 85 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | if (nleft == 1) { |
| 87 | *(unsigned char *) (&ans) = *(unsigned char *) w; |
| 88 | sum += ans; |
| 89 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 90 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | sum = (sum >> 16) + (sum & 0xFFFF); |
| 92 | sum += (sum >> 16); |
| 93 | ans = ~sum; |
| 94 | return (ans); |
| 95 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 96 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 97 | /* simple version */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 98 | #ifndef CONFIG_FEATURE_FANCY_PING |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 99 | static char *hostname = NULL; |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 100 | static void noresp(int ign) |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 101 | { |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 102 | printf("No response from %s\n", hostname); |
Eric Andersen | 4e486a5 | 2003-01-12 06:08:33 +0000 | [diff] [blame] | 103 | exit(EXIT_FAILURE); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 104 | } |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 105 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 106 | static void ping(const char *host) |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 107 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | struct hostent *h; |
| 109 | struct sockaddr_in pingaddr; |
| 110 | struct icmp *pkt; |
| 111 | int pingsock, c; |
| 112 | char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN]; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 113 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 114 | pingsock = create_icmp_socket(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | |
| 116 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
| 117 | |
| 118 | pingaddr.sin_family = AF_INET; |
Matt Kraai | c55b8d4 | 2001-05-16 15:40:51 +0000 | [diff] [blame] | 119 | h = xgethostbyname(host); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 120 | memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr)); |
Eric Andersen | b5474c4 | 2002-03-20 11:59:28 +0000 | [diff] [blame] | 121 | hostname = h->h_name; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | |
| 123 | pkt = (struct icmp *) packet; |
| 124 | memset(pkt, 0, sizeof(packet)); |
| 125 | pkt->icmp_type = ICMP_ECHO; |
| 126 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
| 127 | |
| 128 | c = sendto(pingsock, packet, sizeof(packet), 0, |
| 129 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 130 | |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 131 | if (c < 0 || c != sizeof(packet)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | bb_perror_msg_and_die("sendto"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 133 | |
| 134 | signal(SIGALRM, noresp); |
| 135 | alarm(5); /* give the host 5000ms to respond */ |
| 136 | /* listen for replies */ |
| 137 | while (1) { |
| 138 | struct sockaddr_in from; |
| 139 | size_t fromlen = sizeof(from); |
| 140 | |
| 141 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 142 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 143 | if (errno == EINTR) |
| 144 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 145 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | continue; |
| 147 | } |
| 148 | if (c >= 76) { /* ip + icmp */ |
| 149 | struct iphdr *iphdr = (struct iphdr *) packet; |
| 150 | |
| 151 | pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */ |
| 152 | if (pkt->icmp_type == ICMP_ECHOREPLY) |
| 153 | break; |
| 154 | } |
| 155 | } |
Eric Andersen | b888682 | 2002-03-21 14:04:43 +0000 | [diff] [blame] | 156 | printf("%s is alive!\n", hostname); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 157 | return; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | extern int ping_main(int argc, char **argv) |
| 161 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | argc--; |
| 163 | argv++; |
| 164 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 166 | ping(*argv); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 167 | return EXIT_SUCCESS; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 170 | #else /* ! CONFIG_FEATURE_FANCY_PING */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 171 | /* full(er) version */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 172 | static struct sockaddr_in pingaddr; |
| 173 | static int pingsock = -1; |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 174 | static int datalen; /* intentionally uninitialized to work around gcc bug */ |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 175 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 176 | static long ntransmitted, nreceived, nrepeats, pingcount; |
| 177 | static int myid, options; |
| 178 | static unsigned long tmin = ULONG_MAX, tmax, tsum; |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 179 | static char rcvd_tbl[MAX_DUP_CHK / 8]; |
| 180 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 181 | struct hostent *hostent; |
| 182 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 183 | static void sendping(int); |
| 184 | static void pingstats(int); |
| 185 | static void unpack(char *, int, struct sockaddr_in *); |
| 186 | |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 187 | /**************************************************************************/ |
| 188 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 189 | static void pingstats(int junk) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 190 | { |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 191 | int status; |
| 192 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 193 | signal(SIGINT, SIG_IGN); |
| 194 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 195 | printf("\n--- %s ping statistics ---\n", hostent->h_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | printf("%ld packets transmitted, ", ntransmitted); |
| 197 | printf("%ld packets received, ", nreceived); |
| 198 | if (nrepeats) |
| 199 | printf("%ld duplicates, ", nrepeats); |
| 200 | if (ntransmitted) |
| 201 | printf("%ld%% packet loss\n", |
| 202 | (ntransmitted - nreceived) * 100 / ntransmitted); |
| 203 | if (nreceived) |
| 204 | printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n", |
| 205 | tmin / 10, tmin % 10, |
| 206 | (tsum / (nreceived + nrepeats)) / 10, |
| 207 | (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10); |
Matt Kraai | b938e2f | 2000-09-20 04:33:30 +0000 | [diff] [blame] | 208 | if (nreceived != 0) |
| 209 | status = EXIT_SUCCESS; |
| 210 | else |
| 211 | status = EXIT_FAILURE; |
| 212 | exit(status); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 215 | static void sendping(int junk) |
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 | struct icmp *pkt; |
| 218 | int i; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 219 | char packet[datalen + 8]; |
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 | pkt = (struct icmp *) packet; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 222 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | pkt->icmp_type = ICMP_ECHO; |
| 224 | pkt->icmp_code = 0; |
| 225 | pkt->icmp_cksum = 0; |
| 226 | pkt->icmp_seq = ntransmitted++; |
| 227 | pkt->icmp_id = myid; |
| 228 | CLR(pkt->icmp_seq % MAX_DUP_CHK); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 229 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 230 | gettimeofday((struct timeval *) &packet[8], NULL); |
| 231 | pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | i = sendto(pingsock, packet, sizeof(packet), 0, |
| 234 | (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); |
| 235 | |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 236 | if (i < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 237 | bb_perror_msg_and_die("sendto"); |
Eric Andersen | fad04fd | 2000-07-14 06:49:52 +0000 | [diff] [blame] | 238 | else if ((size_t)i != sizeof(packet)) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 239 | bb_error_msg_and_die("ping wrote %d chars; %d expected", i, |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 240 | (int)sizeof(packet)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 241 | |
| 242 | signal(SIGALRM, sendping); |
| 243 | if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */ |
| 244 | alarm(PINGINTERVAL); |
| 245 | } else { /* done, wait for the last ping to come back */ |
| 246 | /* todo, don't necessarily need to wait so long... */ |
| 247 | signal(SIGALRM, pingstats); |
| 248 | alarm(MAXWAIT); |
| 249 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 250 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 252 | static char *icmp_type_name (int id) |
| 253 | { |
| 254 | switch (id) { |
| 255 | case ICMP_ECHOREPLY: return "Echo Reply"; |
| 256 | case ICMP_DEST_UNREACH: return "Destination Unreachable"; |
| 257 | case ICMP_SOURCE_QUENCH: return "Source Quench"; |
| 258 | case ICMP_REDIRECT: return "Redirect (change route)"; |
| 259 | case ICMP_ECHO: return "Echo Request"; |
| 260 | case ICMP_TIME_EXCEEDED: return "Time Exceeded"; |
| 261 | case ICMP_PARAMETERPROB: return "Parameter Problem"; |
| 262 | case ICMP_TIMESTAMP: return "Timestamp Request"; |
| 263 | case ICMP_TIMESTAMPREPLY: return "Timestamp Reply"; |
| 264 | case ICMP_INFO_REQUEST: return "Information Request"; |
| 265 | case ICMP_INFO_REPLY: return "Information Reply"; |
| 266 | case ICMP_ADDRESS: return "Address Mask Request"; |
| 267 | case ICMP_ADDRESSREPLY: return "Address Mask Reply"; |
| 268 | default: return "unknown ICMP type"; |
| 269 | } |
| 270 | } |
| 271 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 272 | static void unpack(char *buf, int sz, struct sockaddr_in *from) |
| 273 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 274 | struct icmp *icmppkt; |
| 275 | struct iphdr *iphdr; |
| 276 | struct timeval tv, *tp; |
| 277 | int hlen, dupflag; |
| 278 | unsigned long triptime; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 279 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 280 | gettimeofday(&tv, NULL); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 281 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 282 | /* check IP header */ |
| 283 | iphdr = (struct iphdr *) buf; |
| 284 | hlen = iphdr->ihl << 2; |
| 285 | /* discard if too short */ |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 286 | if (sz < (datalen + ICMP_MINLEN)) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 287 | return; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 288 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 289 | sz -= hlen; |
| 290 | icmppkt = (struct icmp *) (buf + hlen); |
| 291 | |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 292 | if (icmppkt->icmp_id != myid) |
| 293 | return; /* not our ping */ |
| 294 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 295 | if (icmppkt->icmp_type == ICMP_ECHOREPLY) { |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 296 | ++nreceived; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 297 | tp = (struct timeval *) icmppkt->icmp_data; |
| 298 | |
| 299 | if ((tv.tv_usec -= tp->tv_usec) < 0) { |
| 300 | --tv.tv_sec; |
| 301 | tv.tv_usec += 1000000; |
| 302 | } |
| 303 | tv.tv_sec -= tp->tv_sec; |
| 304 | |
| 305 | triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100); |
| 306 | tsum += triptime; |
| 307 | if (triptime < tmin) |
| 308 | tmin = triptime; |
| 309 | if (triptime > tmax) |
| 310 | tmax = triptime; |
| 311 | |
| 312 | if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) { |
| 313 | ++nrepeats; |
| 314 | --nreceived; |
| 315 | dupflag = 1; |
| 316 | } else { |
| 317 | SET(icmppkt->icmp_seq % MAX_DUP_CHK); |
| 318 | dupflag = 0; |
| 319 | } |
| 320 | |
| 321 | if (options & O_QUIET) |
| 322 | return; |
| 323 | |
| 324 | printf("%d bytes from %s: icmp_seq=%u", sz, |
| 325 | inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr), |
| 326 | icmppkt->icmp_seq); |
| 327 | printf(" ttl=%d", iphdr->ttl); |
| 328 | printf(" time=%lu.%lu ms", triptime / 10, triptime % 10); |
| 329 | if (dupflag) |
| 330 | printf(" (DUP!)"); |
| 331 | printf("\n"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 332 | } else |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 333 | if (icmppkt->icmp_type != ICMP_ECHO) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 334 | bb_error_msg("Warning: Got ICMP %d (%s)", |
Erik Andersen | 227a59b | 2000-04-25 23:24:55 +0000 | [diff] [blame] | 335 | icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 338 | static void ping(const char *host) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 339 | { |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 340 | char packet[datalen + MAXIPLEN + MAXICMPLEN]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 341 | int sockopt; |
| 342 | |
Matt Kraai | 06ef165 | 2001-07-13 20:56:27 +0000 | [diff] [blame] | 343 | pingsock = create_icmp_socket(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 344 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 345 | memset(&pingaddr, 0, sizeof(struct sockaddr_in)); |
Eric Andersen | 19db07b | 1999-12-11 08:41:28 +0000 | [diff] [blame] | 346 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 347 | pingaddr.sin_family = AF_INET; |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 348 | hostent = xgethostbyname(host); |
| 349 | if (hostent->h_addrtype != AF_INET) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 350 | 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] | 351 | |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 352 | memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 353 | |
| 354 | /* enable broadcast pings */ |
| 355 | sockopt = 1; |
| 356 | setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt, |
| 357 | sizeof(sockopt)); |
| 358 | |
| 359 | /* set recv buf for broadcast pings */ |
| 360 | sockopt = 48 * 1024; |
| 361 | setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt, |
| 362 | sizeof(sockopt)); |
| 363 | |
| 364 | printf("PING %s (%s): %d data bytes\n", |
Matt Kraai | 369da77 | 2002-02-01 16:54:00 +0000 | [diff] [blame] | 365 | hostent->h_name, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 366 | inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr), |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 367 | datalen); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 368 | |
| 369 | signal(SIGINT, pingstats); |
| 370 | |
| 371 | /* start the ping's going ... */ |
| 372 | sendping(0); |
| 373 | |
| 374 | /* listen for replies */ |
| 375 | while (1) { |
| 376 | struct sockaddr_in from; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 377 | socklen_t fromlen = (socklen_t) sizeof(from); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 378 | int c; |
| 379 | |
| 380 | if ((c = recvfrom(pingsock, packet, sizeof(packet), 0, |
| 381 | (struct sockaddr *) &from, &fromlen)) < 0) { |
| 382 | if (errno == EINTR) |
| 383 | continue; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 384 | bb_perror_msg("recvfrom"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | continue; |
| 386 | } |
| 387 | unpack(packet, c, &from); |
| 388 | if (pingcount > 0 && nreceived >= pingcount) |
| 389 | break; |
| 390 | } |
| 391 | pingstats(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | extern int ping_main(int argc, char **argv) |
| 395 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 396 | char *thisarg; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 397 | |
Mark Whitley | 59ab025 | 2001-01-23 22:30:04 +0000 | [diff] [blame] | 398 | datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */ |
| 399 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 400 | argc--; |
| 401 | argv++; |
| 402 | options = 0; |
| 403 | /* Parse any options */ |
Erik Andersen | 9cf3bfa | 2000-04-13 18:49:43 +0000 | [diff] [blame] | 404 | while (argc >= 1 && **argv == '-') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 405 | thisarg = *argv; |
| 406 | thisarg++; |
| 407 | switch (*thisarg) { |
| 408 | case 'q': |
| 409 | options |= O_QUIET; |
| 410 | break; |
| 411 | case 'c': |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 412 | if (--argc <= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 413 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 414 | argv++; |
| 415 | pingcount = atoi(*argv); |
| 416 | break; |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 417 | case 's': |
| 418 | if (--argc <= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 419 | bb_show_usage(); |
Pavel Roskin | 0024abc | 2000-06-07 20:38:15 +0000 | [diff] [blame] | 420 | argv++; |
| 421 | datalen = atoi(*argv); |
| 422 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 424 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 425 | } |
| 426 | argc--; |
| 427 | argv++; |
| 428 | } |
| 429 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 430 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 431 | |
| 432 | myid = getpid() & 0xFFFF; |
Eric Andersen | e90e741 | 2002-06-06 11:47:00 +0000 | [diff] [blame] | 433 | ping(*argv); |
| 434 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 435 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 436 | #endif /* ! CONFIG_FEATURE_FANCY_PING */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 437 | |
| 438 | /* |
| 439 | * Copyright (c) 1989 The Regents of the University of California. |
| 440 | * All rights reserved. |
| 441 | * |
| 442 | * This code is derived from software contributed to Berkeley by |
| 443 | * Mike Muuss. |
| 444 | * |
| 445 | * Redistribution and use in source and binary forms, with or without |
| 446 | * modification, are permitted provided that the following conditions |
| 447 | * are met: |
| 448 | * 1. Redistributions of source code must retain the above copyright |
| 449 | * notice, this list of conditions and the following disclaimer. |
| 450 | * 2. Redistributions in binary form must reproduce the above copyright |
| 451 | * notice, this list of conditions and the following disclaimer in the |
| 452 | * documentation and/or other materials provided with the distribution. |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 453 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 454 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 455 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
Eric Andersen | 4e573f4 | 2000-11-14 23:29:24 +0000 | [diff] [blame] | 456 | * |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 457 | * 4. Neither the name of the University nor the names of its contributors |
| 458 | * may be used to endorse or promote products derived from this software |
| 459 | * without specific prior written permission. |
| 460 | * |
| 461 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 462 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 463 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 464 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 465 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 466 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 467 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 468 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 469 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 470 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 471 | * SUCH DAMAGE. |
| 472 | */ |