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 static void ping(const char *host);
48 static int in_cksum(unsigned short *buf, int sz)
52 unsigned short *w = buf;
53 unsigned short ans = 0;
61 *(unsigned char *) (&ans) = *(unsigned char *) w;
65 sum = (sum >> 16) + (sum & 0xFFFF);
71 #ifndef CONFIG_FEATURE_FANCY_PING
75 static char *hostname;
77 static void noresp(int ign)
79 printf("No response from %s\n", hostname);
83 static void ping(const char *host)
86 struct sockaddr_in pingaddr;
89 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
91 pingsock = create_icmp_socket();
93 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
95 pingaddr.sin_family = AF_INET;
96 h = xgethostbyname(host);
97 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
100 pkt = (struct icmp *) packet;
101 memset(pkt, 0, sizeof(packet));
102 pkt->icmp_type = ICMP_ECHO;
103 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
105 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
106 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
109 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
110 bb_perror_msg_and_die("sendto");
113 signal(SIGALRM, noresp);
114 alarm(5); /* give the host 5000ms to respond */
115 /* listen for replies */
117 struct sockaddr_in from;
118 socklen_t fromlen = sizeof(from);
120 c = recvfrom(pingsock, packet, sizeof(packet), 0,
121 (struct sockaddr *) &from, &fromlen);
125 bb_perror_msg("recvfrom");
128 if (c >= 76) { /* ip + icmp */
129 struct iphdr *iphdr = (struct iphdr *) packet;
131 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
132 if (pkt->icmp_type == ICMP_ECHOREPLY)
136 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
137 printf("%s is alive!\n", hostname);
141 int ping_main(int argc, char **argv)
151 #else /* ! CONFIG_FEATURE_FANCY_PING */
153 /* full(er) version */
155 #define OPT_STRING "qc:s:I:"
160 static struct sockaddr_in pingaddr;
161 static struct sockaddr_in sourceaddr;
162 static int pingsock = -1;
163 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
165 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
167 static unsigned long tmin = ULONG_MAX, tmax, tsum;
168 static char rcvd_tbl[MAX_DUP_CHK / 8];
170 static struct hostent *hostent;
172 static void sendping(int);
173 static void pingstats(int);
174 static void unpack(char *, int, struct sockaddr_in *);
176 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
177 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
178 #define SET(bit) (A(bit) |= B(bit))
179 #define CLR(bit) (A(bit) &= (~B(bit)))
180 #define TST(bit) (A(bit) & B(bit))
182 /**************************************************************************/
184 static void pingstats(int junk)
188 signal(SIGINT, SIG_IGN);
190 printf("\n--- %s ping statistics ---\n", hostent->h_name);
191 printf("%lu packets transmitted, ", ntransmitted);
192 printf("%lu packets received, ", nreceived);
194 printf("%lu duplicates, ", nrepeats);
196 printf("%lu%% packet loss\n",
197 (ntransmitted - nreceived) * 100 / ntransmitted);
199 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
200 tmin / 10, tmin % 10,
201 (tsum / (nreceived + nrepeats)) / 10,
202 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
204 status = EXIT_SUCCESS;
206 status = EXIT_FAILURE;
210 static void sendping(int junk)
214 char packet[datalen + ICMP_MINLEN];
216 pkt = (struct icmp *) packet;
218 pkt->icmp_type = ICMP_ECHO;
221 pkt->icmp_seq = htons(ntransmitted++);
223 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
225 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
226 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
228 i = sendto(pingsock, packet, sizeof(packet), 0,
229 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
232 bb_perror_msg_and_die("sendto");
233 else if ((size_t)i != sizeof(packet))
234 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
235 (int)sizeof(packet));
237 signal(SIGALRM, sendping);
238 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
240 } else { /* done, wait for the last ping to come back */
241 /* todo, don't necessarily need to wait so long... */
242 signal(SIGALRM, pingstats);
247 static char *icmp_type_name(int id)
250 case ICMP_ECHOREPLY: return "Echo Reply";
251 case ICMP_DEST_UNREACH: return "Destination Unreachable";
252 case ICMP_SOURCE_QUENCH: return "Source Quench";
253 case ICMP_REDIRECT: return "Redirect (change route)";
254 case ICMP_ECHO: return "Echo Request";
255 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
256 case ICMP_PARAMETERPROB: return "Parameter Problem";
257 case ICMP_TIMESTAMP: return "Timestamp Request";
258 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
259 case ICMP_INFO_REQUEST: return "Information Request";
260 case ICMP_INFO_REPLY: return "Information Reply";
261 case ICMP_ADDRESS: return "Address Mask Request";
262 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
263 default: return "unknown ICMP type";
267 static void unpack(char *buf, int sz, struct sockaddr_in *from)
269 struct icmp *icmppkt;
271 struct timeval tv, *tp;
273 unsigned long triptime;
275 gettimeofday(&tv, NULL);
277 /* discard if too short */
278 if (sz < (datalen + ICMP_MINLEN))
281 /* check IP header */
282 iphdr = (struct iphdr *) buf;
283 hlen = iphdr->ihl << 2;
285 icmppkt = (struct icmp *) (buf + hlen);
286 if (icmppkt->icmp_id != myid)
287 return; /* not our ping */
289 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
290 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
292 tp = (struct timeval *) icmppkt->icmp_data;
294 if ((tv.tv_usec -= tp->tv_usec) < 0) {
296 tv.tv_usec += 1000000;
298 tv.tv_sec -= tp->tv_sec;
300 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
307 if (TST(recv_seq % MAX_DUP_CHK)) {
312 SET(recv_seq % MAX_DUP_CHK);
316 if (option_mask32 & OPT_QUIET)
319 printf("%d bytes from %s: icmp_seq=%u", sz,
320 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
322 printf(" ttl=%d", iphdr->ttl);
323 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
328 if (icmppkt->icmp_type != ICMP_ECHO)
329 bb_error_msg("warning: got ICMP %d (%s)",
330 icmppkt->icmp_type, icmp_type_name(icmppkt->icmp_type));
334 static void ping(const char *host)
336 char packet[datalen + MAXIPLEN + MAXICMPLEN];
339 pingsock = create_icmp_socket();
341 if (sourceaddr.sin_addr.s_addr) {
342 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
345 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
347 pingaddr.sin_family = AF_INET;
348 hostent = xgethostbyname(host);
349 if (hostent->h_addrtype != AF_INET)
350 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
352 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
354 /* enable broadcast pings */
355 setsockopt_broadcast(pingsock);
357 /* set recv buf for broadcast pings */
359 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
362 printf("PING %s (%s)",
364 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
365 if (sourceaddr.sin_addr.s_addr) {
367 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
369 printf(": %d data bytes\n", datalen);
371 signal(SIGINT, pingstats);
373 /* start the ping's going ... */
376 /* listen for replies */
378 struct sockaddr_in from;
379 socklen_t fromlen = (socklen_t) sizeof(from);
382 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
383 (struct sockaddr *) &from, &fromlen)) < 0) {
386 bb_perror_msg("recvfrom");
389 unpack(packet, c, &from);
390 if (pingcount > 0 && nreceived >= pingcount)
396 /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
397 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
399 static int parse_nipquad(const char *str, struct sockaddr_in* addr)
402 unsigned i1, i2, i3, i4;
403 if (sscanf(str, "%u.%u.%u.%u%c",
404 &i1, &i2, &i3, &i4, &dummy) == 4
405 && ( (i1|i2|i3|i4) <= 0xff )
407 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
414 return 1; /* error */
417 int ping_main(int argc, char **argv)
419 char *opt_c, *opt_s, *opt_I;
421 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
423 /* exactly one argument needed */
424 opt_complementary = "=1";
425 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
426 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
427 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
428 if (option_mask32 & 8) { // -I
429 /* TODO: ping6 accepts iface too:
430 if_index = if_nametoindex(*argv);
432 make it true for ping. */
433 if (parse_nipquad(opt_I, &sourceaddr))
437 myid = (int16_t) getpid();
441 #endif /* ! CONFIG_FEATURE_FANCY_PING */