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/in.h>
16 //#include <netinet/ip.h>
17 #include <netinet/ip_icmp.h>
18 //#include <arpa/inet.h>
27 MAX_DUP_CHK = (8 * 128),
29 PINGINTERVAL = 1 /* second */
32 static void ping(const char *host);
36 static int in_cksum(unsigned short *buf, int sz)
40 unsigned short *w = buf;
41 unsigned short ans = 0;
49 *(unsigned char *) (&ans) = *(unsigned char *) w;
53 sum = (sum >> 16) + (sum & 0xFFFF);
59 #ifndef CONFIG_FEATURE_FANCY_PING
63 static char *hostname;
65 static void noresp(int ign)
67 printf("No response from %s\n", hostname);
71 static void ping(const char *host)
74 struct sockaddr_in pingaddr;
77 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
79 pingsock = create_icmp_socket();
81 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
83 pingaddr.sin_family = AF_INET;
84 h = xgethostbyname(host);
85 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
88 pkt = (struct icmp *) packet;
89 memset(pkt, 0, sizeof(packet));
90 pkt->icmp_type = ICMP_ECHO;
91 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
93 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
94 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
97 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
98 bb_perror_msg_and_die("sendto");
101 signal(SIGALRM, noresp);
102 alarm(5); /* give the host 5000ms to respond */
103 /* listen for replies */
105 struct sockaddr_in from;
106 socklen_t fromlen = sizeof(from);
108 c = recvfrom(pingsock, packet, sizeof(packet), 0,
109 (struct sockaddr *) &from, &fromlen);
112 bb_perror_msg("recvfrom");
115 if (c >= 76) { /* ip + icmp */
116 struct iphdr *iphdr = (struct iphdr *) packet;
118 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
119 if (pkt->icmp_type == ICMP_ECHOREPLY)
123 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
124 printf("%s is alive!\n", hostname);
127 int ping_main(int argc, char **argv)
137 #else /* ! CONFIG_FEATURE_FANCY_PING */
139 /* full(er) version */
141 #define OPT_STRING "qc:s:I:"
146 static struct sockaddr_in pingaddr;
147 static struct sockaddr_in sourceaddr;
148 static int pingsock = -1;
149 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
151 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
153 static unsigned long tmin = ULONG_MAX, tmax, tsum;
154 static char rcvd_tbl[MAX_DUP_CHK / 8];
156 static struct hostent *hostent;
158 static void sendping(int);
159 static void pingstats(int);
160 static void unpack(char *, int, struct sockaddr_in *);
162 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
163 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
164 #define SET(bit) (A(bit) |= B(bit))
165 #define CLR(bit) (A(bit) &= (~B(bit)))
166 #define TST(bit) (A(bit) & B(bit))
168 /**************************************************************************/
170 static void pingstats(int junk)
174 signal(SIGINT, SIG_IGN);
176 printf("\n--- %s ping statistics ---\n", hostent->h_name);
177 printf("%lu packets transmitted, ", ntransmitted);
178 printf("%lu packets received, ", nreceived);
180 printf("%lu duplicates, ", nrepeats);
182 printf("%lu%% packet loss\n",
183 (ntransmitted - nreceived) * 100 / ntransmitted);
185 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
186 tmin / 10, tmin % 10,
187 (tsum / (nreceived + nrepeats)) / 10,
188 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
190 status = EXIT_SUCCESS;
192 status = EXIT_FAILURE;
196 static void sendping(int junk)
200 char packet[datalen + ICMP_MINLEN];
202 pkt = (struct icmp *) packet;
204 pkt->icmp_type = ICMP_ECHO;
207 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
209 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
212 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
213 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
215 i = sendto(pingsock, packet, sizeof(packet), 0,
216 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
219 bb_perror_msg_and_die("sendto");
220 if ((size_t)i != sizeof(packet))
221 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
222 (int)sizeof(packet));
224 signal(SIGALRM, sendping);
225 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
227 } else { /* done, wait for the last ping to come back */
228 /* todo, don't necessarily need to wait so long... */
229 signal(SIGALRM, pingstats);
234 static char *icmp_type_name(int id)
237 case ICMP_ECHOREPLY: return "Echo Reply";
238 case ICMP_DEST_UNREACH: return "Destination Unreachable";
239 case ICMP_SOURCE_QUENCH: return "Source Quench";
240 case ICMP_REDIRECT: return "Redirect (change route)";
241 case ICMP_ECHO: return "Echo Request";
242 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
243 case ICMP_PARAMETERPROB: return "Parameter Problem";
244 case ICMP_TIMESTAMP: return "Timestamp Request";
245 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
246 case ICMP_INFO_REQUEST: return "Information Request";
247 case ICMP_INFO_REPLY: return "Information Reply";
248 case ICMP_ADDRESS: return "Address Mask Request";
249 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
250 default: return "unknown ICMP type";
254 static void unpack(char *buf, int sz, struct sockaddr_in *from)
256 struct icmp *icmppkt;
258 struct timeval tv, *tp;
260 unsigned long triptime;
262 gettimeofday(&tv, NULL);
264 /* discard if too short */
265 if (sz < (datalen + ICMP_MINLEN))
268 /* check IP header */
269 iphdr = (struct iphdr *) buf;
270 hlen = iphdr->ihl << 2;
272 icmppkt = (struct icmp *) (buf + hlen);
273 if (icmppkt->icmp_id != myid)
274 return; /* not our ping */
276 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
277 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
279 tp = (struct timeval *) icmppkt->icmp_data;
281 if ((tv.tv_usec -= tp->tv_usec) < 0) {
283 tv.tv_usec += 1000000;
285 tv.tv_sec -= tp->tv_sec;
287 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
294 if (TST(recv_seq % MAX_DUP_CHK)) {
299 SET(recv_seq % MAX_DUP_CHK);
303 if (option_mask32 & OPT_QUIET)
306 printf("%d bytes from %s: icmp_seq=%u", sz,
307 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
309 printf(" ttl=%d", iphdr->ttl);
310 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
315 if (icmppkt->icmp_type != ICMP_ECHO)
316 bb_error_msg("warning: got ICMP %d (%s)",
318 icmp_type_name(icmppkt->icmp_type));
322 static void ping(const char *host)
324 char packet[datalen + MAXIPLEN + MAXICMPLEN];
327 pingsock = create_icmp_socket();
329 if (sourceaddr.sin_addr.s_addr) {
330 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
333 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
335 pingaddr.sin_family = AF_INET;
336 hostent = xgethostbyname(host);
337 if (hostent->h_addrtype != AF_INET)
338 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
340 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
342 /* enable broadcast pings */
343 setsockopt_broadcast(pingsock);
345 /* set recv buf for broadcast pings */
347 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
350 printf("PING %s (%s)",
352 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
353 if (sourceaddr.sin_addr.s_addr) {
355 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
357 printf(": %d data bytes\n", datalen);
359 signal(SIGINT, pingstats);
361 /* start the ping's going ... */
364 /* listen for replies */
366 struct sockaddr_in from;
367 socklen_t fromlen = (socklen_t) sizeof(from);
370 c = recvfrom(pingsock, packet, sizeof(packet), 0,
371 (struct sockaddr *) &from, &fromlen);
374 bb_perror_msg("recvfrom");
377 unpack(packet, c, &from);
378 if (pingcount > 0 && nreceived >= pingcount)
384 /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
385 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
387 static int parse_nipquad(const char *str, struct sockaddr_in* addr)
390 unsigned i1, i2, i3, i4;
391 if (sscanf(str, "%u.%u.%u.%u%c",
392 &i1, &i2, &i3, &i4, &dummy) == 4
393 && ( (i1|i2|i3|i4) <= 0xff )
395 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
402 return 1; /* error */
405 int ping_main(int argc, char **argv)
407 char *opt_c, *opt_s, *opt_I;
409 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
411 /* exactly one argument needed */
412 opt_complementary = "=1";
413 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
414 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
415 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
416 if (option_mask32 & 8) { // -I
417 /* TODO: ping6 accepts iface too:
418 if_index = if_nametoindex(*argv);
420 make it true for ping. */
421 if (parse_nipquad(opt_I, &sourceaddr))
425 myid = (int16_t) getpid();
429 #endif /* ! CONFIG_FEATURE_FANCY_PING */