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>
19 #include <sys/times.h>
20 #include <sys/signal.h>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip_icmp.h>
25 #include <arpa/inet.h>
36 static const int DEFDATALEN = 56;
37 static const int MAXIPLEN = 60;
38 static const int MAXICMPLEN = 76;
39 static const int MAXPACKET = 65468;
40 #define MAX_DUP_CHK (8 * 128)
41 static const int MAXWAIT = 10;
42 static const int PINGINTERVAL = 1; /* second */
44 #define O_QUIET (1 << 0)
46 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
47 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
48 #define SET(bit) (A(bit) |= B(bit))
49 #define CLR(bit) (A(bit) &= (~B(bit)))
50 #define TST(bit) (A(bit) & B(bit))
52 static void ping(const char *host);
55 static int in_cksum(unsigned short *buf, int sz)
59 unsigned short *w = buf;
60 unsigned short ans = 0;
68 *(unsigned char *) (&ans) = *(unsigned char *) w;
72 sum = (sum >> 16) + (sum & 0xFFFF);
79 #ifndef CONFIG_FEATURE_FANCY_PING
80 static char *hostname = NULL;
81 static void noresp(int ign)
83 printf("No response from %s\n", hostname);
87 static void ping(const char *host)
90 struct sockaddr_in pingaddr;
93 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
95 pingsock = create_icmp_socket();
97 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
99 pingaddr.sin_family = AF_INET;
100 h = xgethostbyname(host);
101 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
102 hostname = h->h_name;
104 pkt = (struct icmp *) packet;
105 memset(pkt, 0, sizeof(packet));
106 pkt->icmp_type = ICMP_ECHO;
107 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
109 c = sendto(pingsock, packet, sizeof(packet), 0,
110 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
112 if (c < 0 || c != sizeof(packet))
113 bb_perror_msg_and_die("sendto");
115 signal(SIGALRM, noresp);
116 alarm(5); /* give the host 5000ms to respond */
117 /* listen for replies */
119 struct sockaddr_in from;
120 socklen_t fromlen = sizeof(from);
122 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
123 (struct sockaddr *) &from, &fromlen)) < 0) {
126 bb_perror_msg("recvfrom");
129 if (c >= 76) { /* ip + icmp */
130 struct iphdr *iphdr = (struct iphdr *) packet;
132 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
133 if (pkt->icmp_type == ICMP_ECHOREPLY)
137 printf("%s is alive!\n", hostname);
141 extern int ping_main(int argc, char **argv)
151 #else /* ! CONFIG_FEATURE_FANCY_PING */
152 /* full(er) version */
153 static struct sockaddr_in pingaddr;
154 static int pingsock = -1;
155 static int datalen; /* intentionally uninitialized to work around gcc bug */
157 static long ntransmitted, nreceived, nrepeats, pingcount;
158 static int myid, options;
159 static unsigned long tmin = ULONG_MAX, tmax, tsum;
160 static char rcvd_tbl[MAX_DUP_CHK / 8];
162 #ifndef CONFIG_FEATURE_FANCY_PING6
165 struct hostent *hostent;
167 static void sendping(int);
168 static void pingstats(int);
169 static void unpack(char *, int, struct sockaddr_in *);
171 /**************************************************************************/
173 static void pingstats(int junk)
177 signal(SIGINT, SIG_IGN);
179 printf("\n--- %s ping statistics ---\n", hostent->h_name);
180 printf("%ld packets transmitted, ", ntransmitted);
181 printf("%ld packets received, ", nreceived);
183 printf("%ld duplicates, ", nrepeats);
185 printf("%ld%% packet loss\n",
186 (ntransmitted - nreceived) * 100 / ntransmitted);
188 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
189 tmin / 10, tmin % 10,
190 (tsum / (nreceived + nrepeats)) / 10,
191 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
193 status = EXIT_SUCCESS;
195 status = EXIT_FAILURE;
199 static void sendping(int junk)
203 char packet[datalen + sizeof(struct icmp)];
205 pkt = (struct icmp *) packet;
207 pkt->icmp_type = ICMP_ECHO;
210 pkt->icmp_seq = htons(ntransmitted++);
212 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
214 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
215 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
217 i = sendto(pingsock, packet, sizeof(packet), 0,
218 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
221 bb_perror_msg_and_die("sendto");
222 else if ((size_t)i != sizeof(packet))
223 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
224 (int)sizeof(packet));
226 signal(SIGALRM, sendping);
227 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
229 } else { /* done, wait for the last ping to come back */
230 /* todo, don't necessarily need to wait so long... */
231 signal(SIGALRM, pingstats);
236 static char *icmp_type_name (int id)
239 case ICMP_ECHOREPLY: return "Echo Reply";
240 case ICMP_DEST_UNREACH: return "Destination Unreachable";
241 case ICMP_SOURCE_QUENCH: return "Source Quench";
242 case ICMP_REDIRECT: return "Redirect (change route)";
243 case ICMP_ECHO: return "Echo Request";
244 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
245 case ICMP_PARAMETERPROB: return "Parameter Problem";
246 case ICMP_TIMESTAMP: return "Timestamp Request";
247 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
248 case ICMP_INFO_REQUEST: return "Information Request";
249 case ICMP_INFO_REPLY: return "Information Reply";
250 case ICMP_ADDRESS: return "Address Mask Request";
251 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
252 default: return "unknown ICMP type";
256 static void unpack(char *buf, int sz, struct sockaddr_in *from)
258 struct icmp *icmppkt;
260 struct timeval tv, *tp;
262 unsigned long triptime;
264 gettimeofday(&tv, NULL);
266 /* check IP header */
267 iphdr = (struct iphdr *) buf;
268 hlen = iphdr->ihl << 2;
269 /* discard if too short */
270 if (sz < (datalen + ICMP_MINLEN))
274 icmppkt = (struct icmp *) (buf + hlen);
276 if (icmppkt->icmp_id != myid)
277 return; /* not our ping */
279 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
280 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
282 tp = (struct timeval *) icmppkt->icmp_data;
284 if ((tv.tv_usec -= tp->tv_usec) < 0) {
286 tv.tv_usec += 1000000;
288 tv.tv_sec -= tp->tv_sec;
290 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
297 if (TST(recv_seq % MAX_DUP_CHK)) {
302 SET(recv_seq % MAX_DUP_CHK);
306 if (options & O_QUIET)
309 printf("%d bytes from %s: icmp_seq=%u", sz,
310 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
312 printf(" ttl=%d", iphdr->ttl);
313 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
318 if (icmppkt->icmp_type != ICMP_ECHO)
319 bb_error_msg("Warning: Got ICMP %d (%s)",
320 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
324 static void ping(const char *host)
326 char packet[datalen + MAXIPLEN + MAXICMPLEN];
329 pingsock = create_icmp_socket();
331 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
333 pingaddr.sin_family = AF_INET;
334 hostent = xgethostbyname(host);
335 if (hostent->h_addrtype != AF_INET)
336 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
338 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
340 /* enable broadcast pings */
342 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
345 /* set recv buf for broadcast pings */
347 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
350 printf("PING %s (%s): %d data bytes\n",
352 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
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 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
367 (struct sockaddr *) &from, &fromlen)) < 0) {
370 bb_perror_msg("recvfrom");
373 unpack(packet, c, &from);
374 if (pingcount > 0 && nreceived >= pingcount)
380 extern int ping_main(int argc, char **argv)
384 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
389 /* Parse any options */
390 while (argc >= 1 && **argv == '-') {
401 pingcount = atoi(*argv);
407 datalen = atoi(*argv);
418 myid = getpid() & 0xFFFF;
422 #endif /* ! CONFIG_FEATURE_FANCY_PING */