1 /* vi: set sw=4 ts=4: */
3 * Mini ping implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * Adapted from the ping in netkit-base 0.10:
8 * Copyright (c) 1989 The Regents of the University of California.
11 * This code is derived from software contributed to Berkeley by
14 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 #include <netinet/ip_icmp.h>
25 MAX_DUP_CHK = (8 * 128),
27 PINGINTERVAL = 1 /* second */
30 static void ping(const char *host);
34 static int in_cksum(unsigned short *buf, int sz)
38 unsigned short *w = buf;
39 unsigned short ans = 0;
47 *(unsigned char *) (&ans) = *(unsigned char *) w;
51 sum = (sum >> 16) + (sum & 0xFFFF);
57 #ifndef CONFIG_FEATURE_FANCY_PING
61 static char *hostname;
63 static void noresp(int ign)
65 printf("No response from %s\n", hostname);
69 static void ping(const char *host)
72 struct sockaddr_in pingaddr;
75 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
77 pingsock = create_icmp_socket();
79 memset(&pingaddr, 0, sizeof(pingaddr));
81 pingaddr.sin_family = AF_INET;
82 h = xgethostbyname(host);
83 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
86 pkt = (struct icmp *) packet;
87 memset(pkt, 0, sizeof(packet));
88 pkt->icmp_type = ICMP_ECHO;
89 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
91 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
92 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
95 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
96 bb_perror_msg_and_die("sendto");
99 signal(SIGALRM, noresp);
100 alarm(5); /* give the host 5000ms to respond */
101 /* listen for replies */
103 struct sockaddr_in from;
104 socklen_t fromlen = sizeof(from);
106 c = recvfrom(pingsock, packet, sizeof(packet), 0,
107 (struct sockaddr *) &from, &fromlen);
110 bb_perror_msg("recvfrom");
113 if (c >= 76) { /* ip + icmp */
114 struct iphdr *iphdr = (struct iphdr *) packet;
116 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
117 if (pkt->icmp_type == ICMP_ECHOREPLY)
121 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
122 printf("%s is alive!\n", hostname);
125 int ping_main(int argc, char **argv)
135 #else /* ! CONFIG_FEATURE_FANCY_PING */
137 /* full(er) version */
139 #define OPT_STRING "qc:s:I:"
144 static struct sockaddr_in pingaddr;
145 static struct sockaddr_in sourceaddr;
146 static int pingsock = -1;
147 static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
149 static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
151 static unsigned long tmin = ULONG_MAX, tmax, tsum;
152 static char rcvd_tbl[MAX_DUP_CHK / 8];
154 static struct hostent *hostent;
156 static void sendping(int);
157 static void pingstats(int);
158 static void unpack(char *, int, struct sockaddr_in *);
160 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
161 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
162 #define SET(bit) (A(bit) |= B(bit))
163 #define CLR(bit) (A(bit) &= (~B(bit)))
164 #define TST(bit) (A(bit) & B(bit))
166 /**************************************************************************/
168 static void pingstats(int junk)
172 signal(SIGINT, SIG_IGN);
174 printf("\n--- %s ping statistics ---\n", hostent->h_name);
175 printf("%lu packets transmitted, ", ntransmitted);
176 printf("%lu packets received, ", nreceived);
178 printf("%lu duplicates, ", nrepeats);
180 printf("%lu%% packet loss\n",
181 (ntransmitted - nreceived) * 100 / ntransmitted);
183 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
184 tmin / 10, tmin % 10,
185 (tsum / (nreceived + nrepeats)) / 10,
186 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
188 status = EXIT_SUCCESS;
190 status = EXIT_FAILURE;
194 static void sendping(int junk)
198 char packet[datalen + ICMP_MINLEN];
200 pkt = (struct icmp *) packet;
202 pkt->icmp_type = ICMP_ECHO;
205 pkt->icmp_seq = htons(ntransmitted); /* don't ++ here, it can be a macro */
207 CLR((uint16_t)ntransmitted % MAX_DUP_CHK);
210 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
211 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
213 i = sendto(pingsock, packet, sizeof(packet), 0,
214 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
217 bb_perror_msg_and_die("sendto");
218 if ((size_t)i != sizeof(packet))
219 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
220 (int)sizeof(packet));
222 signal(SIGALRM, sendping);
223 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
225 } else { /* done, wait for the last ping to come back */
226 /* todo, don't necessarily need to wait so long... */
227 signal(SIGALRM, pingstats);
232 static char *icmp_type_name(int id)
235 case ICMP_ECHOREPLY: return "Echo Reply";
236 case ICMP_DEST_UNREACH: return "Destination Unreachable";
237 case ICMP_SOURCE_QUENCH: return "Source Quench";
238 case ICMP_REDIRECT: return "Redirect (change route)";
239 case ICMP_ECHO: return "Echo Request";
240 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
241 case ICMP_PARAMETERPROB: return "Parameter Problem";
242 case ICMP_TIMESTAMP: return "Timestamp Request";
243 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
244 case ICMP_INFO_REQUEST: return "Information Request";
245 case ICMP_INFO_REPLY: return "Information Reply";
246 case ICMP_ADDRESS: return "Address Mask Request";
247 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
248 default: return "unknown ICMP type";
252 static void unpack(char *buf, int sz, struct sockaddr_in *from)
254 struct icmp *icmppkt;
256 struct timeval tv, *tp;
258 unsigned long triptime;
260 gettimeofday(&tv, NULL);
262 /* discard if too short */
263 if (sz < (datalen + ICMP_MINLEN))
266 /* check IP header */
267 iphdr = (struct iphdr *) buf;
268 hlen = iphdr->ihl << 2;
270 icmppkt = (struct icmp *) (buf + hlen);
271 if (icmppkt->icmp_id != myid)
272 return; /* not our ping */
274 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
275 uint16_t recv_seq = ntohs(icmppkt->icmp_seq);
277 tp = (struct timeval *) icmppkt->icmp_data;
279 if ((tv.tv_usec -= tp->tv_usec) < 0) {
281 tv.tv_usec += 1000000;
283 tv.tv_sec -= tp->tv_sec;
285 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
292 if (TST(recv_seq % MAX_DUP_CHK)) {
297 SET(recv_seq % MAX_DUP_CHK);
301 if (option_mask32 & OPT_QUIET)
304 printf("%d bytes from %s: icmp_seq=%u", sz,
305 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
307 printf(" ttl=%d", iphdr->ttl);
308 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
313 if (icmppkt->icmp_type != ICMP_ECHO)
314 bb_error_msg("warning: got ICMP %d (%s)",
316 icmp_type_name(icmppkt->icmp_type));
321 static void ping(const char *host)
323 char packet[datalen + MAXIPLEN + MAXICMPLEN];
326 pingsock = create_icmp_socket();
328 if (sourceaddr.sin_addr.s_addr) {
329 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
332 memset(&pingaddr, 0, sizeof(pingaddr));
334 pingaddr.sin_family = AF_INET;
335 hostent = xgethostbyname(host);
336 if (hostent->h_addrtype != AF_INET)
337 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
339 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
341 /* enable broadcast pings */
342 setsockopt_broadcast(pingsock);
344 /* set recv buf for broadcast pings */
345 sockopt = 48 * 1024; /* explain why 48k? */
346 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
349 printf("PING %s (%s)",
351 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
352 if (sourceaddr.sin_addr.s_addr) {
354 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
356 printf(": %d data bytes\n", datalen);
358 signal(SIGINT, pingstats);
360 /* start the ping's going ... */
363 /* listen for replies */
365 struct sockaddr_in from;
366 socklen_t fromlen = (socklen_t) sizeof(from);
369 c = recvfrom(pingsock, packet, sizeof(packet), 0,
370 (struct sockaddr *) &from, &fromlen);
373 bb_perror_msg("recvfrom");
376 unpack(packet, c, &from);
377 if (pingcount > 0 && nreceived >= pingcount)
383 /* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
384 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
386 static int parse_nipquad(const char *str, struct sockaddr_in* addr)
389 unsigned i1, i2, i3, i4;
390 if (sscanf(str, "%u.%u.%u.%u%c",
391 &i1, &i2, &i3, &i4, &dummy) == 4
392 && ( (i1|i2|i3|i4) <= 0xff )
394 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
401 return 1; /* error */
404 int ping_main(int argc, char **argv)
406 char *opt_c, *opt_s, *opt_I;
408 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
410 /* exactly one argument needed */
411 opt_complementary = "=1";
412 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
413 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
414 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
415 if (option_mask32 & 8) { // -I
416 /* TODO: ping6 accepts iface too:
417 if_index = if_nametoindex(*argv);
419 make it true for ping. */
420 if (parse_nipquad(opt_I, &sourceaddr))
424 myid = (int16_t) getpid();
428 #endif /* ! CONFIG_FEATURE_FANCY_PING */