1 /* vi: set sw=4 ts=4: */
3 * $Id: ping.c,v 1.56 2004/03/15 08:28:48 andersen Exp $
4 * Mini ping implementation for busybox
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
8 * Adapted from the ping in netkit-base 0.10:
9 * Copyright (c) 1989 The Regents of the University of California.
10 * Derived from software contributed to Berkeley by Mike Muuss.
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/param.h>
16 #include <sys/socket.h>
18 #include <sys/times.h>
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <netinet/ip_icmp.h>
24 #include <arpa/inet.h>
39 MAX_DUP_CHK = (8 * 128),
41 PINGINTERVAL = 1 /* second */
44 #define O_QUIET (1 << 0)
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))
52 static void ping(const char *host);
55 static int in_cksum(unsigned short *buf, int sz)
59 unsigned short *w = buf;
60 unsigned short ans = 0;
68 *(unsigned char *) (&ans) = *(unsigned char *) w;
72 sum = (sum >> 16) + (sum & 0xFFFF);
79 #ifndef CONFIG_FEATURE_FANCY_PING
80 static char *hostname;
82 static void noresp(int ign)
84 printf("No response from %s\n", hostname);
88 static void ping(const char *host)
91 struct sockaddr_in pingaddr;
94 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
96 pingsock = create_icmp_socket();
98 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
100 pingaddr.sin_family = AF_INET;
101 h = xgethostbyname(host);
102 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
103 hostname = h->h_name;
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));
110 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
111 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
114 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
115 bb_perror_msg_and_die("sendto");
118 signal(SIGALRM, noresp);
119 alarm(5); /* give the host 5000ms to respond */
120 /* listen for replies */
122 struct sockaddr_in from;
123 socklen_t fromlen = sizeof(from);
125 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
126 (struct sockaddr *) &from, &fromlen)) < 0) {
129 bb_perror_msg("recvfrom");
132 if (c >= 76) { /* ip + icmp */
133 struct iphdr *iphdr = (struct iphdr *) packet;
135 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
136 if (pkt->icmp_type == ICMP_ECHOREPLY)
140 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
141 printf("%s is alive!\n", hostname);
145 int ping_main(int argc, char **argv)
155 #else /* ! CONFIG_FEATURE_FANCY_PING */
156 /* full(er) version */
157 static struct sockaddr_in pingaddr;
158 static int pingsock = -1;
159 static int datalen; /* intentionally uninitialized to work around gcc bug */
161 static long ntransmitted, nreceived, nrepeats, pingcount;
162 static int myid, options;
163 static unsigned long tmin = ULONG_MAX, tmax, tsum;
164 static char rcvd_tbl[MAX_DUP_CHK / 8];
166 static struct hostent *hostent;
168 static void sendping(int);
169 static void pingstats(int);
170 static void unpack(char *, int, struct sockaddr_in *);
172 /**************************************************************************/
174 static void pingstats(int junk)
178 signal(SIGINT, SIG_IGN);
180 printf("\n--- %s ping statistics ---\n", hostent->h_name);
181 printf("%ld packets transmitted, ", ntransmitted);
182 printf("%ld packets received, ", nreceived);
184 printf("%ld duplicates, ", nrepeats);
186 printf("%ld%% packet loss\n",
187 (ntransmitted - nreceived) * 100 / ntransmitted);
189 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
190 tmin / 10, tmin % 10,
191 (tsum / (nreceived + nrepeats)) / 10,
192 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
194 status = EXIT_SUCCESS;
196 status = EXIT_FAILURE;
200 static void sendping(int junk)
204 char packet[datalen + ICMP_MINLEN];
206 pkt = (struct icmp *) packet;
208 pkt->icmp_type = ICMP_ECHO;
211 pkt->icmp_seq = SWAP_BE16(ntransmitted++);
213 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
215 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
216 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
218 i = sendto(pingsock, packet, sizeof(packet), 0,
219 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
222 bb_perror_msg_and_die("sendto");
223 else if ((size_t)i != sizeof(packet))
224 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
225 (int)sizeof(packet));
227 signal(SIGALRM, sendping);
228 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
230 } else { /* done, wait for the last ping to come back */
231 /* todo, don't necessarily need to wait so long... */
232 signal(SIGALRM, pingstats);
237 static char *icmp_type_name (int id)
240 case ICMP_ECHOREPLY: return "Echo Reply";
241 case ICMP_DEST_UNREACH: return "Destination Unreachable";
242 case ICMP_SOURCE_QUENCH: return "Source Quench";
243 case ICMP_REDIRECT: return "Redirect (change route)";
244 case ICMP_ECHO: return "Echo Request";
245 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
246 case ICMP_PARAMETERPROB: return "Parameter Problem";
247 case ICMP_TIMESTAMP: return "Timestamp Request";
248 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
249 case ICMP_INFO_REQUEST: return "Information Request";
250 case ICMP_INFO_REPLY: return "Information Reply";
251 case ICMP_ADDRESS: return "Address Mask Request";
252 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
253 default: return "unknown ICMP type";
257 static void unpack(char *buf, int sz, struct sockaddr_in *from)
259 struct icmp *icmppkt;
261 struct timeval tv, *tp;
263 unsigned long triptime;
265 gettimeofday(&tv, NULL);
267 /* check IP header */
268 iphdr = (struct iphdr *) buf;
269 hlen = iphdr->ihl << 2;
270 /* discard if too short */
271 if (sz < (datalen + ICMP_MINLEN))
275 icmppkt = (struct icmp *) (buf + hlen);
277 if (icmppkt->icmp_id != myid)
278 return; /* not our ping */
280 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
281 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
283 tp = (struct timeval *) icmppkt->icmp_data;
285 if ((tv.tv_usec -= tp->tv_usec) < 0) {
287 tv.tv_usec += 1000000;
289 tv.tv_sec -= tp->tv_sec;
291 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
298 if (TST(recv_seq % MAX_DUP_CHK)) {
303 SET(recv_seq % MAX_DUP_CHK);
307 if (options & O_QUIET)
310 printf("%d bytes from %s: icmp_seq=%u", sz,
311 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
313 printf(" ttl=%d", iphdr->ttl);
314 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
319 if (icmppkt->icmp_type != ICMP_ECHO)
320 bb_error_msg("Warning: Got ICMP %d (%s)",
321 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
325 static void ping(const char *host)
327 char packet[datalen + MAXIPLEN + MAXICMPLEN];
330 pingsock = create_icmp_socket();
332 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
334 pingaddr.sin_family = AF_INET;
335 hostent = xgethostbyname(host);
336 if (hostent->h_addrtype != AF_INET)
337 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
339 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
341 /* enable broadcast pings */
343 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
346 /* set recv buf for broadcast pings */
348 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
351 printf("PING %s (%s): %d data bytes\n",
353 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
356 signal(SIGINT, pingstats);
358 /* start the ping's going ... */
361 /* listen for replies */
363 struct sockaddr_in from;
364 socklen_t fromlen = (socklen_t) sizeof(from);
367 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
368 (struct sockaddr *) &from, &fromlen)) < 0) {
371 bb_perror_msg("recvfrom");
374 unpack(packet, c, &from);
375 if (pingcount > 0 && nreceived >= pingcount)
381 int ping_main(int argc, char **argv)
385 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
390 /* Parse any options */
391 while (argc >= 1 && **argv == '-') {
402 pingcount = atoi(*argv);
408 datalen = atoi(*argv);
419 myid = getpid() & 0xFFFF;
423 #endif /* ! CONFIG_FEATURE_FANCY_PING */