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); /* don't ++ here, it can be a macro */
223 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
226 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
227 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
229 i = sendto(pingsock, packet, sizeof(packet), 0,
230 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
233 bb_perror_msg_and_die("sendto");
234 else if ((size_t)i != sizeof(packet))
235 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
236 (int)sizeof(packet));
238 signal(SIGALRM, sendping);
239 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
241 } else { /* done, wait for the last ping to come back */
242 /* todo, don't necessarily need to wait so long... */
243 signal(SIGALRM, pingstats);
248 static char *icmp_type_name(int id)
251 case ICMP_ECHOREPLY: return "Echo Reply";
252 case ICMP_DEST_UNREACH: return "Destination Unreachable";
253 case ICMP_SOURCE_QUENCH: return "Source Quench";
254 case ICMP_REDIRECT: return "Redirect (change route)";
255 case ICMP_ECHO: return "Echo Request";
256 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
257 case ICMP_PARAMETERPROB: return "Parameter Problem";
258 case ICMP_TIMESTAMP: return "Timestamp Request";
259 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
260 case ICMP_INFO_REQUEST: return "Information Request";
261 case ICMP_INFO_REPLY: return "Information Reply";
262 case ICMP_ADDRESS: return "Address Mask Request";
263 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
264 default: return "unknown ICMP type";
268 static void unpack(char *buf, int sz, struct sockaddr_in *from)
270 struct icmp *icmppkt;
272 struct timeval tv, *tp;
274 unsigned long triptime;
276 gettimeofday(&tv, NULL);
278 /* discard if too short */
279 if (sz < (datalen + ICMP_MINLEN))
282 /* check IP header */
283 iphdr = (struct iphdr *) buf;
284 hlen = iphdr->ihl << 2;
286 icmppkt = (struct icmp *) (buf + hlen);
287 if (icmppkt->icmp_id != myid)
288 return; /* not our ping */
290 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
291 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
293 tp = (struct timeval *) icmppkt->icmp_data;
295 if ((tv.tv_usec -= tp->tv_usec) < 0) {
297 tv.tv_usec += 1000000;
299 tv.tv_sec -= tp->tv_sec;
301 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
308 if (TST(recv_seq % MAX_DUP_CHK)) {
313 SET(recv_seq % MAX_DUP_CHK);
317 if (option_mask32 & OPT_QUIET)
320 printf("%d bytes from %s: icmp_seq=%u", sz,
321 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
323 printf(" ttl=%d", iphdr->ttl);
324 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
329 if (icmppkt->icmp_type != ICMP_ECHO)
330 bb_error_msg("warning: got ICMP %d (%s)",
331 icmppkt->icmp_type, icmp_type_name(icmppkt->icmp_type));
335 static void ping(const char *host)
337 char packet[datalen + MAXIPLEN + MAXICMPLEN];
340 pingsock = create_icmp_socket();
342 if (sourceaddr.sin_addr.s_addr) {
343 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
346 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
348 pingaddr.sin_family = AF_INET;
349 hostent = xgethostbyname(host);
350 if (hostent->h_addrtype != AF_INET)
351 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
353 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
355 /* enable broadcast pings */
356 setsockopt_broadcast(pingsock);
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 */