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 <netinet/ip_icmp.h>
23 MAX_DUP_CHK = (8 * 128),
25 PINGINTERVAL = 1 /* second */
28 static void ping(const char *host);
32 static int in_cksum(unsigned short *buf, int sz)
36 unsigned short *w = buf;
37 unsigned short ans = 0;
45 *(unsigned char *) (&ans) = *(unsigned char *) w;
49 sum = (sum >> 16) + (sum & 0xFFFF);
55 #ifndef CONFIG_FEATURE_FANCY_PING
59 static char *hostname;
61 static void noresp(int ign)
63 printf("No response from %s\n", hostname);
67 static void ping(const char *host)
70 struct sockaddr_in pingaddr;
73 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
75 pingsock = create_icmp_socket();
77 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
79 pingaddr.sin_family = AF_INET;
80 h = xgethostbyname(host);
81 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
84 pkt = (struct icmp *) packet;
85 memset(pkt, 0, sizeof(packet));
86 pkt->icmp_type = ICMP_ECHO;
87 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
89 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
90 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
93 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
94 bb_perror_msg_and_die("sendto");
97 signal(SIGALRM, noresp);
98 alarm(5); /* give the host 5000ms to respond */
99 /* listen for replies */
101 struct sockaddr_in from;
102 socklen_t fromlen = sizeof(from);
104 c = recvfrom(pingsock, packet, sizeof(packet), 0,
105 (struct sockaddr *) &from, &fromlen);
108 bb_perror_msg("recvfrom");
111 if (c >= 76) { /* ip + icmp */
112 struct iphdr *iphdr = (struct iphdr *) packet;
114 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
115 if (pkt->icmp_type == ICMP_ECHOREPLY)
119 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
120 printf("%s is alive!\n", hostname);
123 int ping_main(int argc, char **argv)
133 #else /* ! CONFIG_FEATURE_FANCY_PING */
135 /* full(er) version */
137 #define OPT_STRING "qc:s:I:"
142 static struct sockaddr_in pingaddr;
143 static struct sockaddr_in sourceaddr;
144 static int pingsock = -1;
145 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
147 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
149 static unsigned long tmin = ULONG_MAX, tmax, tsum;
150 static char rcvd_tbl[MAX_DUP_CHK / 8];
152 static struct hostent *hostent;
154 static void sendping(int);
155 static void pingstats(int);
156 static void unpack(char *, int, struct sockaddr_in *);
158 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
159 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
160 #define SET(bit) (A(bit) |= B(bit))
161 #define CLR(bit) (A(bit) &= (~B(bit)))
162 #define TST(bit) (A(bit) & B(bit))
164 /**************************************************************************/
166 static void pingstats(int junk)
170 signal(SIGINT, SIG_IGN);
172 printf("\n--- %s ping statistics ---\n", hostent->h_name);
173 printf("%lu packets transmitted, ", ntransmitted);
174 printf("%lu packets received, ", nreceived);
176 printf("%lu duplicates, ", nrepeats);
178 printf("%lu%% packet loss\n",
179 (ntransmitted - nreceived) * 100 / ntransmitted);
181 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
182 tmin / 10, tmin % 10,
183 (tsum / (nreceived + nrepeats)) / 10,
184 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
186 status = EXIT_SUCCESS;
188 status = EXIT_FAILURE;
192 static void sendping(int junk)
196 char packet[datalen + ICMP_MINLEN];
198 pkt = (struct icmp *) packet;
200 pkt->icmp_type = ICMP_ECHO;
203 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
205 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
208 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
209 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
211 i = sendto(pingsock, packet, sizeof(packet), 0,
212 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
215 bb_perror_msg_and_die("sendto");
216 if ((size_t)i != sizeof(packet))
217 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
218 (int)sizeof(packet));
220 signal(SIGALRM, sendping);
221 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
223 } else { /* done, wait for the last ping to come back */
224 /* todo, don't necessarily need to wait so long... */
225 signal(SIGALRM, pingstats);
230 static char *icmp_type_name(int id)
233 case ICMP_ECHOREPLY: return "Echo Reply";
234 case ICMP_DEST_UNREACH: return "Destination Unreachable";
235 case ICMP_SOURCE_QUENCH: return "Source Quench";
236 case ICMP_REDIRECT: return "Redirect (change route)";
237 case ICMP_ECHO: return "Echo Request";
238 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
239 case ICMP_PARAMETERPROB: return "Parameter Problem";
240 case ICMP_TIMESTAMP: return "Timestamp Request";
241 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
242 case ICMP_INFO_REQUEST: return "Information Request";
243 case ICMP_INFO_REPLY: return "Information Reply";
244 case ICMP_ADDRESS: return "Address Mask Request";
245 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
246 default: return "unknown ICMP type";
250 static void unpack(char *buf, int sz, struct sockaddr_in *from)
252 struct icmp *icmppkt;
254 struct timeval tv, *tp;
256 unsigned long triptime;
258 gettimeofday(&tv, NULL);
260 /* discard if too short */
261 if (sz < (datalen + ICMP_MINLEN))
264 /* check IP header */
265 iphdr = (struct iphdr *) buf;
266 hlen = iphdr->ihl << 2;
268 icmppkt = (struct icmp *) (buf + hlen);
269 if (icmppkt->icmp_id != myid)
270 return; /* not our ping */
272 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
273 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
275 tp = (struct timeval *) icmppkt->icmp_data;
277 if ((tv.tv_usec -= tp->tv_usec) < 0) {
279 tv.tv_usec += 1000000;
281 tv.tv_sec -= tp->tv_sec;
283 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
290 if (TST(recv_seq % MAX_DUP_CHK)) {
295 SET(recv_seq % MAX_DUP_CHK);
299 if (option_mask32 & OPT_QUIET)
302 printf("%d bytes from %s: icmp_seq=%u", sz,
303 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
305 printf(" ttl=%d", iphdr->ttl);
306 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
311 if (icmppkt->icmp_type != ICMP_ECHO)
312 bb_error_msg("warning: got ICMP %d (%s)",
314 icmp_type_name(icmppkt->icmp_type));
318 static void ping(const char *host)
320 char packet[datalen + MAXIPLEN + MAXICMPLEN];
323 pingsock = create_icmp_socket();
325 if (sourceaddr.sin_addr.s_addr) {
326 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
329 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
331 pingaddr.sin_family = AF_INET;
332 hostent = xgethostbyname(host);
333 if (hostent->h_addrtype != AF_INET)
334 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
336 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
338 /* enable broadcast pings */
339 setsockopt_broadcast(pingsock);
341 /* set recv buf for broadcast pings */
343 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
346 printf("PING %s (%s)",
348 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
349 if (sourceaddr.sin_addr.s_addr) {
351 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
353 printf(": %d data bytes\n", datalen);
355 signal(SIGINT, pingstats);
357 /* start the ping's going ... */
360 /* listen for replies */
362 struct sockaddr_in from;
363 socklen_t fromlen = (socklen_t) sizeof(from);
366 c = recvfrom(pingsock, packet, sizeof(packet), 0,
367 (struct sockaddr *) &from, &fromlen);
370 bb_perror_msg("recvfrom");
373 unpack(packet, c, &from);
374 if (pingcount > 0 && nreceived >= pingcount)
380 /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
381 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
383 static int parse_nipquad(const char *str, struct sockaddr_in* addr)
386 unsigned i1, i2, i3, i4;
387 if (sscanf(str, "%u.%u.%u.%u%c",
388 &i1, &i2, &i3, &i4, &dummy) == 4
389 && ( (i1|i2|i3|i4) <= 0xff )
391 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
398 return 1; /* error */
401 int ping_main(int argc, char **argv)
403 char *opt_c, *opt_s, *opt_I;
405 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
407 /* exactly one argument needed */
408 opt_complementary = "=1";
409 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
410 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
411 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
412 if (option_mask32 & 8) { // -I
413 /* TODO: ping6 accepts iface too:
414 if_index = if_nametoindex(*argv);
416 make it true for ping. */
417 if (parse_nipquad(opt_I, &sourceaddr))
421 myid = (int16_t) getpid();
425 #endif /* ! CONFIG_FEATURE_FANCY_PING */