blob: 5cadac3e8658672d7f43f96b04f98129c48ffe04 [file] [log] [blame]
Eric Andersen485b9551999-12-07 23:14:59 +00001/*
Erik Andersen8e759aa2000-01-29 05:52:40 +00002 * $Id: ping.c,v 1.8 2000/01/29 05:52:40 erik Exp $
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini ping implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * This version of ping is adapted from the ping in netkit-base 0.10,
22 * which is:
23 *
24 * Copyright (c) 1989 The Regents of the University of California.
25 * All rights reserved.
26 *
27 * This code is derived from software contributed to Berkeley by
28 * Mike Muuss.
29 *
30 * Original copyright notice is retained at the end of this file.
31 */
32
33#include "internal.h"
34#include <sys/param.h>
35#include <sys/socket.h>
36#include <sys/file.h>
37#include <sys/time.h>
38#include <sys/times.h>
39#include <sys/signal.h>
40
41#include <netinet/in.h>
42#include <netinet/ip.h>
43#include <netinet/ip_icmp.h>
44#include <arpa/inet.h>
45#include <netdb.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <errno.h>
49
50#define DEFDATALEN 56
51#define MAXIPLEN 60
52#define MAXICMPLEN 76
53#define MAXPACKET 65468
54#define MAX_DUP_CHK (8 * 128)
55#define MAXWAIT 10
Eric Andersen1792f8c1999-12-09 06:11:36 +000056#define PINGINTERVAL 1 /* second */
Eric Andersen485b9551999-12-07 23:14:59 +000057
58#define O_QUIET (1 << 0)
59
60#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
61#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
62#define SET(bit) (A(bit) |= B(bit))
63#define CLR(bit) (A(bit) &= (~B(bit)))
64#define TST(bit) (A(bit) & B(bit))
65
Eric Andersen19db07b1999-12-11 08:41:28 +000066/* common routines */
Eric Andersen485b9551999-12-07 23:14:59 +000067static int in_cksum(unsigned short *buf, int sz)
68{
69 int nleft = sz;
70 int sum = 0;
71 unsigned short *w = buf;
72 unsigned short ans = 0;
73
74 while (nleft > 1) {
75 sum += *w++;
76 nleft -= 2;
77 }
78
79 if (nleft == 1) {
80 *(unsigned char *)(&ans) = *(unsigned char *)w;
81 sum += ans;
82 }
83
84 sum = (sum >> 16) + (sum & 0xFFFF);
85 sum += (sum >> 16);
86 ans = ~sum;
87 return(ans);
88}
89
Eric Andersen19db07b1999-12-11 08:41:28 +000090/* simple version */
91#ifdef BB_SIMPLE_PING
92static const char* ping_usage = "ping host\n\n";
93
94static char* hostname = NULL;
95
96static void noresp(int ign)
97{
98 printf("No response from %s\n", hostname);
99 exit(0);
100}
101
102static int ping(const char *host)
103{
104 struct hostent *h;
105 struct sockaddr_in pingaddr;
106 struct icmp *pkt;
107 int pingsock, c;
108 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
109
110 if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */
111 perror("ping");
112 exit(1);
113 }
114
115 /* drop root privs if running setuid */
116 setuid(getuid());
117
118 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
119 pingaddr.sin_family = AF_INET;
120 if (!(h = gethostbyname(host))) {
121 fprintf(stderr, "ping: unknown host %s\n", host);
122 exit(1);
123 }
124 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
125 hostname = h->h_name;
126
127 pkt = (struct icmp *)packet;
128 memset(pkt, 0, sizeof(packet));
129 pkt->icmp_type = ICMP_ECHO;
130 pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet));
131
132 c = sendto(pingsock, packet, sizeof(packet), 0,
133 (struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in));
134
135 if (c < 0 || c != sizeof(packet)) {
136 if (c < 0) perror("ping");
137 fprintf(stderr, "ping: write incomplete\n");
138 exit(1);
139 }
140
141 signal(SIGALRM, noresp);
142 alarm(5); /* give the host 5000ms to respond */
143 /* listen for replies */
144 while (1) {
145 struct sockaddr_in from;
146 size_t fromlen = sizeof(from);
147
148 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
149 (struct sockaddr *)&from, &fromlen)) < 0) {
150 if (errno == EINTR) continue;
151 perror("ping");
152 continue;
153 }
154 if (c >= 76) { /* ip + icmp */
155 struct iphdr *iphdr = (struct iphdr *)packet;
156 pkt = (struct icmp *)(packet + (iphdr->ihl << 2)); /* skip ip hdr */
157 if (pkt->icmp_type == ICMP_ECHOREPLY) break;
158 }
159 }
160 printf("%s is alive!\n", hostname);
161 return(TRUE);
162}
163
164extern int ping_main(int argc, char **argv)
165{
166 argc--;
167 argv++;
168 if (argc < 1) usage(ping_usage);
169 ping(*argv);
170 exit(TRUE);
171}
172
173#else
174/* full(er) version */
175static const char* ping_usage = "ping [OPTION]... host\n\n"
176"Send ICMP ECHO_REQUEST packets to network hosts.\n\n"
177"Options:\n"
178"\t-q\t\tQuiet mode, only displays output at start and when finished.\n"
179"\t-c COUNT\tSend only COUNT pings.\n";
180
181static char *hostname = NULL;
182static struct sockaddr_in pingaddr;
183static int pingsock = -1;
184
185static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
186static int myid = 0, options = 0;
187static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
188static char rcvd_tbl[MAX_DUP_CHK / 8];
189
190static void sendping(int);
191static void pingstats(int);
192static void unpack(char *, int, struct sockaddr_in *);
193
194static void ping(char *);
195
196/**************************************************************************/
197
Eric Andersen485b9551999-12-07 23:14:59 +0000198static void pingstats(int ign) {
199 signal(SIGINT, SIG_IGN);
200
201 printf("\n--- %s ping statistics ---\n", hostname);
202 printf("%ld packets transmitted, ", ntransmitted);
203 printf("%ld packets received, ", nreceived);
204 if (nrepeats)
205 printf("%ld duplicates, ", nrepeats);
206 if (ntransmitted)
207 printf("%ld%% packet loss\n",
208 (ntransmitted - nreceived)*100/ntransmitted);
209 if (nreceived)
210 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
211 tmin/10, tmin%10,
212 (tsum/(nreceived+nrepeats))/10,
213 (tsum/(nreceived+nrepeats))%10,
214 tmax/10, tmax%10);
215 exit(0);
216}
217
218static void sendping(int ign)
219{
220 struct icmp *pkt;
221 int i;
222 char packet[DEFDATALEN + 8];
223
224 pkt = (struct icmp *)packet;
225
226 pkt->icmp_type = ICMP_ECHO;
227 pkt->icmp_code = 0;
228 pkt->icmp_cksum = 0;
229 pkt->icmp_seq = ntransmitted++;
230 pkt->icmp_id = myid;
231 CLR(pkt->icmp_seq % MAX_DUP_CHK);
232
233 gettimeofday((struct timeval *)&packet[8], NULL);
234 pkt->icmp_cksum = in_cksum((unsigned short *)pkt, sizeof(packet));
235
236 i = sendto(pingsock, packet, sizeof(packet), 0,
237 (struct sockaddr *)&pingaddr, sizeof(struct sockaddr_in));
238
239 if (i < 0 || i != sizeof(packet)) {
240 if (i < 0) perror("ping");
241 fprintf(stderr, "ping wrote %d chars; %d expected\n", i, sizeof(packet));
242 exit(1);
243 }
244
245 signal(SIGALRM, sendping);
Eric Andersen1792f8c1999-12-09 06:11:36 +0000246 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
247 alarm(PINGINTERVAL);
Eric Andersen485b9551999-12-07 23:14:59 +0000248 } else { /* done, wait for the last ping to come back */
249 /* todo, don't necessarily need to wait so long... */
250 signal(SIGALRM, pingstats);
251 alarm(MAXWAIT);
252 }
253}
254
255static void unpack(char *buf, int sz, struct sockaddr_in *from)
256{
257 struct icmp *icmppkt;
258 struct iphdr *iphdr;
259 struct timeval tv, *tp;
260 int hlen, dupflag;
261 unsigned long triptime;
262
263 gettimeofday(&tv, NULL);
264
265 /* check IP header */
266 iphdr = (struct iphdr *)buf;
267 hlen = iphdr->ihl << 2;
268 /* discard if too short */
269 if (sz < (DEFDATALEN + ICMP_MINLEN)) return;
270
271 sz -= hlen;
272 icmppkt = (struct icmp *)(buf + hlen);
273
274 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
275 if (icmppkt->icmp_id != myid) return; /* not our ping */
276 ++nreceived;
277 tp = (struct timeval *)icmppkt->icmp_data;
278
279 if ((tv.tv_usec -= tp->tv_usec) < 0) {
280 --tv.tv_sec;
281 tv.tv_usec += 1000000;
282 }
283 tv.tv_sec -= tp->tv_sec;
284
285 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
286 tsum += triptime;
287 if (triptime < tmin) tmin = triptime;
288 if (triptime > tmax) tmax = triptime;
289
290 if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
291 ++nrepeats;
292 --nreceived;
293 dupflag = 1;
294 } else {
295 SET(icmppkt->icmp_seq % MAX_DUP_CHK);
296 dupflag = 0;
297 }
298
299 if (options & O_QUIET) return;
300
301 printf("%d bytes from %s: icmp_seq=%u", sz,
302 inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
303 icmppkt->icmp_seq);
304 printf(" ttl=%d", iphdr->ttl);
305 printf(" time=%lu.%lu ms", triptime/10, triptime%10);
306 if (dupflag) printf(" (DUP!)");
307 printf("\n");
308 } else {
309 fprintf(stderr, "Warning: unknown ICMP packet received (not echo-reply)\n");
310 }
311}
312
313static void ping(char *host)
314{
Erik Andersen8e759aa2000-01-29 05:52:40 +0000315 struct protoent *proto=NULL;
Eric Andersen485b9551999-12-07 23:14:59 +0000316 struct hostent *h;
317 char buf[MAXHOSTNAMELEN];
318 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
319 int sockopt;
320
Erik Andersen8e759aa2000-01-29 05:52:40 +0000321 proto = getprotobyname("icmp");
322 if ((pingsock = socket(AF_INET, SOCK_RAW, (proto)? proto->p_proto : 1 )) < 0) { /* 1 == ICMP */
Eric Andersen485b9551999-12-07 23:14:59 +0000323 if (errno == EPERM) {
324 fprintf(stderr, "ping: permission denied. (are you root?)\n");
325 } else {
326 perror("ping");
327 }
328 exit(1);
329 }
330
Eric Andersen1792f8c1999-12-09 06:11:36 +0000331 /* drop root privs if running setuid */
Eric Andersen485b9551999-12-07 23:14:59 +0000332 setuid(getuid());
Eric Andersen485b9551999-12-07 23:14:59 +0000333
334 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
335 pingaddr.sin_family = AF_INET;
Eric Andersen19db07b1999-12-11 08:41:28 +0000336 if (!(h = gethostbyname(host))) {
337 fprintf(stderr, "ping: unknown host %s\n", host);
338 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000339 }
340
Eric Andersen19db07b1999-12-11 08:41:28 +0000341 if (h->h_addrtype != AF_INET) {
342 fprintf(stderr, "ping: unknown address type; only AF_INET is currently supported.\n");
343 exit(1);
344 }
345
346 pingaddr.sin_family = AF_INET; /* h->h_addrtype */
347 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
348 strncpy(buf, h->h_name, sizeof(buf)-1);
349 hostname = buf;
350
Eric Andersen485b9551999-12-07 23:14:59 +0000351 /* enable broadcast pings */
352 sockopt = 1;
353 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt, sizeof(sockopt));
354
355 /* set recv buf for broadcast pings */
356 sockopt = 48 * 1024;
357 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *)&sockopt, sizeof(sockopt));
358
359 printf("PING %s (%s): %d data bytes\n",
360 hostname, inet_ntoa(*(struct in_addr *)&pingaddr.sin_addr.s_addr),
361 DEFDATALEN);
362
363 signal(SIGINT, pingstats);
364
365 /* start the ping's going ... */
366 sendping(0);
367
368 /* listen for replies */
369 while (1) {
370 struct sockaddr_in from;
371 size_t fromlen = sizeof(from);
372 int c;
373
374 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
375 (struct sockaddr *)&from, &fromlen)) < 0) {
376 if (errno == EINTR) continue;
377 perror("ping");
378 continue;
379 }
380 unpack(packet, c, &from);
381 if (pingcount > 0 && nreceived >= pingcount) break;
382 }
383 pingstats(0);
384}
385
386extern int ping_main(int argc, char **argv)
387{
Eric Andersen1792f8c1999-12-09 06:11:36 +0000388 char *thisarg;
389
Eric Andersen485b9551999-12-07 23:14:59 +0000390 argc--;
391 argv++;
392 options = 0;
393 /* Parse any options */
Eric Andersen1792f8c1999-12-09 06:11:36 +0000394 while (argc > 1) {
395 if (**argv != '-') usage(ping_usage);
396 thisarg = *argv; thisarg++;
397 switch (*thisarg) {
398 case 'q': options |= O_QUIET; break;
399 case 'c':
400 argc--; argv++;
401 pingcount = atoi(*argv);
402 break;
403 default:
404 usage(ping_usage);
405 }
406 argc--; argv++;
Eric Andersen485b9551999-12-07 23:14:59 +0000407 }
Eric Andersen1792f8c1999-12-09 06:11:36 +0000408 if (argc < 1) usage(ping_usage);
Eric Andersen485b9551999-12-07 23:14:59 +0000409
410 myid = getpid() & 0xFFFF;
Eric Andersen1792f8c1999-12-09 06:11:36 +0000411 ping(*argv);
412 exit(TRUE);
Eric Andersen485b9551999-12-07 23:14:59 +0000413}
Eric Andersen19db07b1999-12-11 08:41:28 +0000414#endif
Eric Andersen485b9551999-12-07 23:14:59 +0000415
416/*
417 * Copyright (c) 1989 The Regents of the University of California.
418 * All rights reserved.
419 *
420 * This code is derived from software contributed to Berkeley by
421 * Mike Muuss.
422 *
423 * Redistribution and use in source and binary forms, with or without
424 * modification, are permitted provided that the following conditions
425 * are met:
426 * 1. Redistributions of source code must retain the above copyright
427 * notice, this list of conditions and the following disclaimer.
428 * 2. Redistributions in binary form must reproduce the above copyright
429 * notice, this list of conditions and the following disclaimer in the
430 * documentation and/or other materials provided with the distribution.
431 * 3. All advertising materials mentioning features or use of this software
432 * must display the following acknowledgement:
433 * This product includes software developed by the University of
434 * California, Berkeley and its contributors.
435 * 4. Neither the name of the University nor the names of its contributors
436 * may be used to endorse or promote products derived from this software
437 * without specific prior written permission.
438 *
439 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
440 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
441 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
442 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
443 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
444 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
445 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
446 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
447 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
448 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
449 * SUCH DAMAGE.
450 */