Since rangecoder is just a bunch of C functions, move it into the one user
[oweals/busybox.git] / networking / ping.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * $Id: ping.c,v 1.56 2004/03/15 08:28:48 andersen Exp $
4  * Mini ping implementation for busybox
5  *
6  * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7  *
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.
11  *
12  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13  */
14
15 #include <sys/param.h>
16 #include <sys/socket.h>
17 #include <sys/file.h>
18 #include <sys/times.h>
19 #include <signal.h>
20
21 #include <netinet/in.h>
22 #include <netinet/ip.h>
23 #include <netinet/ip_icmp.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include "busybox.h"
33
34
35 enum {
36         DEFDATALEN = 56,
37         MAXIPLEN = 60,
38         MAXICMPLEN = 76,
39         MAXPACKET = 65468,
40         MAX_DUP_CHK = (8 * 128),
41         MAXWAIT = 10,
42         PINGINTERVAL = 1                /* second */
43 };
44
45 #define O_QUIET         (1 << 0)
46
47 #define A(bit)          rcvd_tbl[(bit)>>3]      /* identify byte in array */
48 #define B(bit)          (1 << ((bit) & 0x07))   /* identify bit in byte */
49 #define SET(bit)        (A(bit) |= B(bit))
50 #define CLR(bit)        (A(bit) &= (~B(bit)))
51 #define TST(bit)        (A(bit) & B(bit))
52
53 static void ping(const char *host);
54
55 /* common routines */
56 static int in_cksum(unsigned short *buf, int sz)
57 {
58         int nleft = sz;
59         int sum = 0;
60         unsigned short *w = buf;
61         unsigned short ans = 0;
62
63         while (nleft > 1) {
64                 sum += *w++;
65                 nleft -= 2;
66         }
67
68         if (nleft == 1) {
69                 *(unsigned char *) (&ans) = *(unsigned char *) w;
70                 sum += ans;
71         }
72
73         sum = (sum >> 16) + (sum & 0xFFFF);
74         sum += (sum >> 16);
75         ans = ~sum;
76         return (ans);
77 }
78
79 /* simple version */
80 #ifndef CONFIG_FEATURE_FANCY_PING
81 static char *hostname = NULL;
82 static void noresp(int ign)
83 {
84         printf("No response from %s\n", hostname);
85         exit(EXIT_FAILURE);
86 }
87
88 static void ping(const char *host)
89 {
90         struct hostent *h;
91         struct sockaddr_in pingaddr;
92         struct icmp *pkt;
93         int pingsock, c;
94         char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
95
96         pingsock = create_icmp_socket();
97
98         memset(&pingaddr, 0, sizeof(struct sockaddr_in));
99
100         pingaddr.sin_family = AF_INET;
101         h = xgethostbyname(host);
102         memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
103         hostname = h->h_name;
104
105         pkt = (struct icmp *) packet;
106         memset(pkt, 0, sizeof(packet));
107         pkt->icmp_type = ICMP_ECHO;
108         pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
109
110         c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
111                            (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
112
113         if (c < 0) {
114                 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
115                 bb_perror_msg_and_die("sendto");
116         }
117
118         signal(SIGALRM, noresp);
119         alarm(5);                                       /* give the host 5000ms to respond */
120         /* listen for replies */
121         while (1) {
122                 struct sockaddr_in from;
123                 socklen_t fromlen = sizeof(from);
124
125                 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
126                                                   (struct sockaddr *) &from, &fromlen)) < 0) {
127                         if (errno == EINTR)
128                                 continue;
129                         bb_perror_msg("recvfrom");
130                         continue;
131                 }
132                 if (c >= 76) {                  /* ip + icmp */
133                         struct iphdr *iphdr = (struct iphdr *) packet;
134
135                         pkt = (struct icmp *) (packet + (iphdr->ihl << 2));     /* skip ip hdr */
136                         if (pkt->icmp_type == ICMP_ECHOREPLY)
137                                 break;
138                 }
139         }
140         if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
141         printf("%s is alive!\n", hostname);
142         return;
143 }
144
145 int ping_main(int argc, char **argv)
146 {
147         argc--;
148         argv++;
149         if (argc < 1)
150                 bb_show_usage();
151         ping(*argv);
152         return EXIT_SUCCESS;
153 }
154
155 #else /* ! CONFIG_FEATURE_FANCY_PING */
156 /* full(er) version */
157 static struct sockaddr_in pingaddr;
158 static int pingsock = -1;
159 static int datalen; /* intentionally uninitialized to work around gcc bug */
160
161 static long ntransmitted, nreceived, nrepeats, pingcount;
162 static int myid, options;
163 static unsigned long tmin = ULONG_MAX, tmax, tsum;
164 static char rcvd_tbl[MAX_DUP_CHK / 8];
165
166 #ifndef CONFIG_FEATURE_FANCY_PING6
167 static
168 #endif
169         struct hostent *hostent;
170
171 static void sendping(int);
172 static void pingstats(int);
173 static void unpack(char *, int, struct sockaddr_in *);
174
175 /**************************************************************************/
176
177 static void pingstats(int junk)
178 {
179         int status;
180
181         signal(SIGINT, SIG_IGN);
182
183         printf("\n--- %s ping statistics ---\n", hostent->h_name);
184         printf("%ld packets transmitted, ", ntransmitted);
185         printf("%ld packets received, ", nreceived);
186         if (nrepeats)
187                 printf("%ld duplicates, ", nrepeats);
188         if (ntransmitted)
189                 printf("%ld%% packet loss\n",
190                            (ntransmitted - nreceived) * 100 / ntransmitted);
191         if (nreceived)
192                 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
193                            tmin / 10, tmin % 10,
194                            (tsum / (nreceived + nrepeats)) / 10,
195                            (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
196         if (nreceived != 0)
197                 status = EXIT_SUCCESS;
198         else
199                 status = EXIT_FAILURE;
200         exit(status);
201 }
202
203 static void sendping(int junk)
204 {
205         struct icmp *pkt;
206         int i;
207         char packet[datalen + ICMP_MINLEN];
208
209         pkt = (struct icmp *) packet;
210
211         pkt->icmp_type = ICMP_ECHO;
212         pkt->icmp_code = 0;
213         pkt->icmp_cksum = 0;
214         pkt->icmp_seq = htons(ntransmitted++);
215         pkt->icmp_id = myid;
216         CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
217
218         gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
219         pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
220
221         i = sendto(pingsock, packet, sizeof(packet), 0,
222                            (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
223
224         if (i < 0)
225                 bb_perror_msg_and_die("sendto");
226         else if ((size_t)i != sizeof(packet))
227                 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
228                            (int)sizeof(packet));
229
230         signal(SIGALRM, sendping);
231         if (pingcount == 0 || ntransmitted < pingcount) {       /* schedule next in 1s */
232                 alarm(PINGINTERVAL);
233         } else {                                        /* done, wait for the last ping to come back */
234                 /* todo, don't necessarily need to wait so long... */
235                 signal(SIGALRM, pingstats);
236                 alarm(MAXWAIT);
237         }
238 }
239
240 static char *icmp_type_name (int id)
241 {
242         switch (id) {
243         case ICMP_ECHOREPLY:            return "Echo Reply";
244         case ICMP_DEST_UNREACH:         return "Destination Unreachable";
245         case ICMP_SOURCE_QUENCH:        return "Source Quench";
246         case ICMP_REDIRECT:                     return "Redirect (change route)";
247         case ICMP_ECHO:                         return "Echo Request";
248         case ICMP_TIME_EXCEEDED:        return "Time Exceeded";
249         case ICMP_PARAMETERPROB:        return "Parameter Problem";
250         case ICMP_TIMESTAMP:            return "Timestamp Request";
251         case ICMP_TIMESTAMPREPLY:       return "Timestamp Reply";
252         case ICMP_INFO_REQUEST:         return "Information Request";
253         case ICMP_INFO_REPLY:           return "Information Reply";
254         case ICMP_ADDRESS:                      return "Address Mask Request";
255         case ICMP_ADDRESSREPLY:         return "Address Mask Reply";
256         default:                                        return "unknown ICMP type";
257         }
258 }
259
260 static void unpack(char *buf, int sz, struct sockaddr_in *from)
261 {
262         struct icmp *icmppkt;
263         struct iphdr *iphdr;
264         struct timeval tv, *tp;
265         int hlen, dupflag;
266         unsigned long triptime;
267
268         gettimeofday(&tv, NULL);
269
270         /* check IP header */
271         iphdr = (struct iphdr *) buf;
272         hlen = iphdr->ihl << 2;
273         /* discard if too short */
274         if (sz < (datalen + ICMP_MINLEN))
275                 return;
276
277         sz -= hlen;
278         icmppkt = (struct icmp *) (buf + hlen);
279
280         if (icmppkt->icmp_id != myid)
281             return;                             /* not our ping */
282
283         if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
284                 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
285             ++nreceived;
286                 tp = (struct timeval *) icmppkt->icmp_data;
287
288                 if ((tv.tv_usec -= tp->tv_usec) < 0) {
289                         --tv.tv_sec;
290                         tv.tv_usec += 1000000;
291                 }
292                 tv.tv_sec -= tp->tv_sec;
293
294                 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
295                 tsum += triptime;
296                 if (triptime < tmin)
297                         tmin = triptime;
298                 if (triptime > tmax)
299                         tmax = triptime;
300
301                 if (TST(recv_seq % MAX_DUP_CHK)) {
302                         ++nrepeats;
303                         --nreceived;
304                         dupflag = 1;
305                 } else {
306                         SET(recv_seq % MAX_DUP_CHK);
307                         dupflag = 0;
308                 }
309
310                 if (options & O_QUIET)
311                         return;
312
313                 printf("%d bytes from %s: icmp_seq=%u", sz,
314                            inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
315                            recv_seq);
316                 printf(" ttl=%d", iphdr->ttl);
317                 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
318                 if (dupflag)
319                         printf(" (DUP!)");
320                 printf("\n");
321         } else
322                 if (icmppkt->icmp_type != ICMP_ECHO)
323                         bb_error_msg("Warning: Got ICMP %d (%s)",
324                                         icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
325         fflush(stdout);
326 }
327
328 static void ping(const char *host)
329 {
330         char packet[datalen + MAXIPLEN + MAXICMPLEN];
331         int sockopt;
332
333         pingsock = create_icmp_socket();
334
335         memset(&pingaddr, 0, sizeof(struct sockaddr_in));
336
337         pingaddr.sin_family = AF_INET;
338         hostent = xgethostbyname(host);
339         if (hostent->h_addrtype != AF_INET)
340                 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
341
342         memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
343
344         /* enable broadcast pings */
345         sockopt = 1;
346         setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
347                            sizeof(sockopt));
348
349         /* set recv buf for broadcast pings */
350         sockopt = 48 * 1024;
351         setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
352                            sizeof(sockopt));
353
354         printf("PING %s (%s): %d data bytes\n",
355                    hostent->h_name,
356                    inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
357                    datalen);
358
359         signal(SIGINT, pingstats);
360
361         /* start the ping's going ... */
362         sendping(0);
363
364         /* listen for replies */
365         while (1) {
366                 struct sockaddr_in from;
367                 socklen_t fromlen = (socklen_t) sizeof(from);
368                 int c;
369
370                 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
371                                                   (struct sockaddr *) &from, &fromlen)) < 0) {
372                         if (errno == EINTR)
373                                 continue;
374                         bb_perror_msg("recvfrom");
375                         continue;
376                 }
377                 unpack(packet, c, &from);
378                 if (pingcount > 0 && nreceived >= pingcount)
379                         break;
380         }
381         pingstats(0);
382 }
383
384 int ping_main(int argc, char **argv)
385 {
386         char *thisarg;
387
388         datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
389
390         argc--;
391         argv++;
392         options = 0;
393         /* Parse any options */
394         while (argc >= 1 && **argv == '-') {
395                 thisarg = *argv;
396                 thisarg++;
397                 switch (*thisarg) {
398                 case 'q':
399                         options |= O_QUIET;
400                         break;
401                 case 'c':
402                         if (--argc <= 0)
403                                 bb_show_usage();
404                         argv++;
405                         pingcount = atoi(*argv);
406                         break;
407                 case 's':
408                         if (--argc <= 0)
409                                 bb_show_usage();
410                         argv++;
411                         datalen = atoi(*argv);
412                         break;
413                 default:
414                         bb_show_usage();
415                 }
416                 argc--;
417                 argv++;
418         }
419         if (argc < 1)
420                 bb_show_usage();
421
422         myid = getpid() & 0xFFFF;
423         ping(*argv);
424         return EXIT_SUCCESS;
425 }
426 #endif /* ! CONFIG_FEATURE_FANCY_PING */