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>
22 #include <netinet/in.h>
23 #include <netinet/ip.h>
24 #include <netinet/ip_icmp.h>
25 #include <arpa/inet.h>
41 MAX_DUP_CHK = (8 * 128),
43 PINGINTERVAL = 1 /* second */
46 #define O_QUIET (1 << 0)
48 #define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
49 #define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
50 #define SET(bit) (A(bit) |= B(bit))
51 #define CLR(bit) (A(bit) &= (~B(bit)))
52 #define TST(bit) (A(bit) & B(bit))
54 static void ping(const char *host);
57 static int in_cksum(unsigned short *buf, int sz)
61 unsigned short *w = buf;
62 unsigned short ans = 0;
70 *(unsigned char *) (&ans) = *(unsigned char *) w;
74 sum = (sum >> 16) + (sum & 0xFFFF);
81 #ifndef CONFIG_FEATURE_FANCY_PING
82 static char *hostname = NULL;
83 static void noresp(int ign)
85 printf("No response from %s\n", hostname);
89 static void ping(const char *host)
92 struct sockaddr_in pingaddr;
95 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
97 pingsock = create_icmp_socket();
99 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
101 pingaddr.sin_family = AF_INET;
102 h = xgethostbyname(host);
103 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
104 hostname = h->h_name;
106 pkt = (struct icmp *) packet;
107 memset(pkt, 0, sizeof(packet));
108 pkt->icmp_type = ICMP_ECHO;
109 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
111 c = sendto(pingsock, packet, sizeof(packet), 0,
112 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
114 if (c < 0 || c != sizeof(packet))
115 bb_perror_msg_and_die("sendto");
117 signal(SIGALRM, noresp);
118 alarm(5); /* give the host 5000ms to respond */
119 /* listen for replies */
121 struct sockaddr_in from;
122 socklen_t fromlen = sizeof(from);
124 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
125 (struct sockaddr *) &from, &fromlen)) < 0) {
128 bb_perror_msg("recvfrom");
131 if (c >= 76) { /* ip + icmp */
132 struct iphdr *iphdr = (struct iphdr *) packet;
134 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
135 if (pkt->icmp_type == ICMP_ECHOREPLY)
139 printf("%s is alive!\n", hostname);
143 int ping_main(int argc, char **argv)
153 #else /* ! CONFIG_FEATURE_FANCY_PING */
154 /* full(er) version */
155 static struct sockaddr_in pingaddr;
156 static int pingsock = -1;
157 static int datalen; /* intentionally uninitialized to work around gcc bug */
159 static long ntransmitted, nreceived, nrepeats, pingcount;
160 static int myid, options;
161 static unsigned long tmin = ULONG_MAX, tmax, tsum;
162 static char rcvd_tbl[MAX_DUP_CHK / 8];
164 #ifndef CONFIG_FEATURE_FANCY_PING6
167 struct hostent *hostent;
169 static void sendping(int);
170 static void pingstats(int);
171 static void unpack(char *, int, struct sockaddr_in *);
173 /**************************************************************************/
175 static void pingstats(int junk)
179 signal(SIGINT, SIG_IGN);
181 printf("\n--- %s ping statistics ---\n", hostent->h_name);
182 printf("%ld packets transmitted, ", ntransmitted);
183 printf("%ld packets received, ", nreceived);
185 printf("%ld duplicates, ", nrepeats);
187 printf("%ld%% packet loss\n",
188 (ntransmitted - nreceived) * 100 / ntransmitted);
190 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
191 tmin / 10, tmin % 10,
192 (tsum / (nreceived + nrepeats)) / 10,
193 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
195 status = EXIT_SUCCESS;
197 status = EXIT_FAILURE;
201 static void sendping(int junk)
205 char packet[datalen + sizeof(struct icmp)];
207 pkt = (struct icmp *) packet;
209 pkt->icmp_type = ICMP_ECHO;
212 pkt->icmp_seq = htons(ntransmitted++);
214 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
216 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
217 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
219 i = sendto(pingsock, packet, sizeof(packet), 0,
220 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
223 bb_perror_msg_and_die("sendto");
224 else if ((size_t)i != sizeof(packet))
225 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
226 (int)sizeof(packet));
228 signal(SIGALRM, sendping);
229 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
231 } else { /* done, wait for the last ping to come back */
232 /* todo, don't necessarily need to wait so long... */
233 signal(SIGALRM, pingstats);
238 static char *icmp_type_name (int id)
241 case ICMP_ECHOREPLY: return "Echo Reply";
242 case ICMP_DEST_UNREACH: return "Destination Unreachable";
243 case ICMP_SOURCE_QUENCH: return "Source Quench";
244 case ICMP_REDIRECT: return "Redirect (change route)";
245 case ICMP_ECHO: return "Echo Request";
246 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
247 case ICMP_PARAMETERPROB: return "Parameter Problem";
248 case ICMP_TIMESTAMP: return "Timestamp Request";
249 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
250 case ICMP_INFO_REQUEST: return "Information Request";
251 case ICMP_INFO_REPLY: return "Information Reply";
252 case ICMP_ADDRESS: return "Address Mask Request";
253 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
254 default: return "unknown ICMP type";
258 static void unpack(char *buf, int sz, struct sockaddr_in *from)
260 struct icmp *icmppkt;
262 struct timeval tv, *tp;
264 unsigned long triptime;
266 gettimeofday(&tv, NULL);
268 /* check IP header */
269 iphdr = (struct iphdr *) buf;
270 hlen = iphdr->ihl << 2;
271 /* discard if too short */
272 if (sz < (datalen + ICMP_MINLEN))
276 icmppkt = (struct icmp *) (buf + hlen);
278 if (icmppkt->icmp_id != myid)
279 return; /* not our ping */
281 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
282 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
284 tp = (struct timeval *) icmppkt->icmp_data;
286 if ((tv.tv_usec -= tp->tv_usec) < 0) {
288 tv.tv_usec += 1000000;
290 tv.tv_sec -= tp->tv_sec;
292 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
299 if (TST(recv_seq % MAX_DUP_CHK)) {
304 SET(recv_seq % MAX_DUP_CHK);
308 if (options & O_QUIET)
311 printf("%d bytes from %s: icmp_seq=%u", sz,
312 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
314 printf(" ttl=%d", iphdr->ttl);
315 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
320 if (icmppkt->icmp_type != ICMP_ECHO)
321 bb_error_msg("Warning: Got ICMP %d (%s)",
322 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
326 static void ping(const char *host)
328 char packet[datalen + MAXIPLEN + MAXICMPLEN];
331 pingsock = create_icmp_socket();
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 */
344 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
347 /* set recv buf for broadcast pings */
349 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
352 printf("PING %s (%s): %d data bytes\n",
354 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
357 signal(SIGINT, pingstats);
359 /* start the ping's going ... */
362 /* listen for replies */
364 struct sockaddr_in from;
365 socklen_t fromlen = (socklen_t) sizeof(from);
368 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
369 (struct sockaddr *) &from, &fromlen)) < 0) {
372 bb_perror_msg("recvfrom");
375 unpack(packet, c, &from);
376 if (pingcount > 0 && nreceived >= pingcount)
382 int ping_main(int argc, char **argv)
386 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
391 /* Parse any options */
392 while (argc >= 1 && **argv == '-') {
403 pingcount = atoi(*argv);
409 datalen = atoi(*argv);
420 myid = getpid() & 0xFFFF;
424 #endif /* ! CONFIG_FEATURE_FANCY_PING */