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 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
121 (struct sockaddr *) &from, &fromlen)) < 0) {
124 bb_perror_msg("recvfrom");
127 if (c >= 76) { /* ip + icmp */
128 struct iphdr *iphdr = (struct iphdr *) packet;
130 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
131 if (pkt->icmp_type == ICMP_ECHOREPLY)
135 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
136 printf("%s is alive!\n", hostname);
140 int ping_main(int argc, char **argv)
150 #else /* ! CONFIG_FEATURE_FANCY_PING */
152 /* full(er) version */
154 #define OPT_STRING "qc:s:I:"
159 static struct sockaddr_in pingaddr;
160 static struct sockaddr_in sourceaddr;
161 static int pingsock = -1;
162 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
164 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
166 static unsigned long tmin = ULONG_MAX, tmax, tsum;
167 static char rcvd_tbl[MAX_DUP_CHK / 8];
169 static struct hostent *hostent;
171 static void sendping(int);
172 static void pingstats(int);
173 static void unpack(char *, int, struct sockaddr_in *);
175 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
176 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
177 #define SET(bit) (A(bit) |= B(bit))
178 #define CLR(bit) (A(bit) &= (~B(bit)))
179 #define TST(bit) (A(bit) & B(bit))
181 /**************************************************************************/
183 static void pingstats(int junk)
187 signal(SIGINT, SIG_IGN);
189 printf("\n--- %s ping statistics ---\n", hostent->h_name);
190 printf("%lu packets transmitted, ", ntransmitted);
191 printf("%lu packets received, ", nreceived);
193 printf("%lu duplicates, ", nrepeats);
195 printf("%lu%% packet loss\n",
196 (ntransmitted - nreceived) * 100 / ntransmitted);
198 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
199 tmin / 10, tmin % 10,
200 (tsum / (nreceived + nrepeats)) / 10,
201 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
203 status = EXIT_SUCCESS;
205 status = EXIT_FAILURE;
209 static void sendping(int junk)
213 char packet[datalen + ICMP_MINLEN];
215 pkt = (struct icmp *) packet;
217 pkt->icmp_type = ICMP_ECHO;
220 pkt->icmp_seq = htons(ntransmitted++);
222 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
224 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
225 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
227 i = sendto(pingsock, packet, sizeof(packet), 0,
228 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
231 bb_perror_msg_and_die("sendto");
232 else if ((size_t)i != sizeof(packet))
233 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
234 (int)sizeof(packet));
236 signal(SIGALRM, sendping);
237 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
239 } else { /* done, wait for the last ping to come back */
240 /* todo, don't necessarily need to wait so long... */
241 signal(SIGALRM, pingstats);
246 static char *icmp_type_name(int id)
249 case ICMP_ECHOREPLY: return "Echo Reply";
250 case ICMP_DEST_UNREACH: return "Destination Unreachable";
251 case ICMP_SOURCE_QUENCH: return "Source Quench";
252 case ICMP_REDIRECT: return "Redirect (change route)";
253 case ICMP_ECHO: return "Echo Request";
254 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
255 case ICMP_PARAMETERPROB: return "Parameter Problem";
256 case ICMP_TIMESTAMP: return "Timestamp Request";
257 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
258 case ICMP_INFO_REQUEST: return "Information Request";
259 case ICMP_INFO_REPLY: return "Information Reply";
260 case ICMP_ADDRESS: return "Address Mask Request";
261 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
262 default: return "unknown ICMP type";
266 static void unpack(char *buf, int sz, struct sockaddr_in *from)
268 struct icmp *icmppkt;
270 struct timeval tv, *tp;
272 unsigned long triptime;
274 gettimeofday(&tv, NULL);
276 /* discard if too short */
277 if (sz < (datalen + ICMP_MINLEN))
280 /* check IP header */
281 iphdr = (struct iphdr *) buf;
282 hlen = iphdr->ihl << 2;
284 icmppkt = (struct icmp *) (buf + hlen);
285 if (icmppkt->icmp_id != myid)
286 return; /* not our ping */
288 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
289 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
291 tp = (struct timeval *) icmppkt->icmp_data;
293 if ((tv.tv_usec -= tp->tv_usec) < 0) {
295 tv.tv_usec += 1000000;
297 tv.tv_sec -= tp->tv_sec;
299 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
306 if (TST(recv_seq % MAX_DUP_CHK)) {
311 SET(recv_seq % MAX_DUP_CHK);
315 if (option_mask32 & OPT_QUIET)
318 printf("%d bytes from %s: icmp_seq=%u", sz,
319 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
321 printf(" ttl=%d", iphdr->ttl);
322 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
327 if (icmppkt->icmp_type != ICMP_ECHO)
328 bb_error_msg("warning: got ICMP %d (%s)",
329 icmppkt->icmp_type, icmp_type_name(icmppkt->icmp_type));
333 static void ping(const char *host)
335 char packet[datalen + MAXIPLEN + MAXICMPLEN];
338 pingsock = create_icmp_socket();
340 if (sourceaddr.sin_addr.s_addr) {
341 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
344 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
346 pingaddr.sin_family = AF_INET;
347 hostent = xgethostbyname(host);
348 if (hostent->h_addrtype != AF_INET)
349 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
351 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
353 /* enable broadcast pings */
355 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
358 /* set recv buf for broadcast pings */
360 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
363 printf("PING %s (%s)",
365 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
366 if (sourceaddr.sin_addr.s_addr) {
368 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
370 printf(": %d data bytes\n", datalen);
372 signal(SIGINT, pingstats);
374 /* start the ping's going ... */
377 /* listen for replies */
379 struct sockaddr_in from;
380 socklen_t fromlen = (socklen_t) sizeof(from);
383 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
384 (struct sockaddr *) &from, &fromlen)) < 0) {
387 bb_perror_msg("recvfrom");
390 unpack(packet, c, &from);
391 if (pingcount > 0 && nreceived >= pingcount)
397 /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
398 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
400 static int parse_nipquad(const char *str, struct sockaddr_in* addr)
403 unsigned i1, i2, i3, i4;
404 if (sscanf(str, "%u.%u.%u.%u%c",
405 &i1, &i2, &i3, &i4, &dummy) == 4
406 && ( (i1|i2|i3|i4) <= 0xff )
408 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
415 return 1; /* error */
418 int ping_main(int argc, char **argv)
420 char *opt_c, *opt_s, *opt_I;
422 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
424 /* exactly one argument needed */
425 opt_complementary = "=1";
426 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
427 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
428 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
429 if (option_mask32 & 8) { // -I
430 /* TODO: ping6 accepts iface too:
431 if_index = if_nametoindex(*argv);
433 make it true for ping. */
434 if (parse_nipquad(opt_I, &sourceaddr))
438 myid = (int16_t) getpid();
442 #endif /* ! CONFIG_FEATURE_FANCY_PING */