blob: b9e8d0fd2c1cd08aa04829f54ee38e442978a908 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen03f4c272000-07-06 23:10:29 +00003 * $Id: ping.c,v 1.18 2000/07/06 23:10:29 andersen Exp $
Eric Andersen485b9551999-12-07 23:14:59 +00004 * Mini ping implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This version of ping is adapted from the ping in netkit-base 0.10,
23 * which is:
24 *
25 * Copyright (c) 1989 The Regents of the University of California.
26 * All rights reserved.
27 *
28 * This code is derived from software contributed to Berkeley by
29 * Mike Muuss.
30 *
31 * Original copyright notice is retained at the end of this file.
32 */
33
34#include "internal.h"
35#include <sys/param.h>
36#include <sys/socket.h>
37#include <sys/file.h>
38#include <sys/time.h>
39#include <sys/times.h>
40#include <sys/signal.h>
41
42#include <netinet/in.h>
43#include <netinet/ip.h>
44#include <netinet/ip_icmp.h>
45#include <arpa/inet.h>
46#include <netdb.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <errno.h>
50
Eric Andersen9ca57d32000-06-19 18:51:53 +000051
52/* It turns out that libc5 doesn't have proper icmp support
53 * built into it header files, so we have to supplement it */
54#if __GNU_LIBRARY__ < 5
55typedef unsigned int socklen_t;
56
57#define ICMP_MINLEN 8 /* abs minimum */
58
59struct icmp_ra_addr
60{
61 u_int32_t ira_addr;
62 u_int32_t ira_preference;
63};
64
65
66struct icmp
67{
68 u_int8_t icmp_type; /* type of message, see below */
69 u_int8_t icmp_code; /* type sub code */
70 u_int16_t icmp_cksum; /* ones complement checksum of struct */
71 union
72 {
73 u_char ih_pptr; /* ICMP_PARAMPROB */
74 struct in_addr ih_gwaddr; /* gateway address */
75 struct ih_idseq /* echo datagram */
76 {
77 u_int16_t icd_id;
78 u_int16_t icd_seq;
79 } ih_idseq;
80 u_int32_t ih_void;
81
82 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
83 struct ih_pmtu
84 {
85 u_int16_t ipm_void;
86 u_int16_t ipm_nextmtu;
87 } ih_pmtu;
88
89 struct ih_rtradv
90 {
91 u_int8_t irt_num_addrs;
92 u_int8_t irt_wpa;
93 u_int16_t irt_lifetime;
94 } ih_rtradv;
95 } icmp_hun;
96#define icmp_pptr icmp_hun.ih_pptr
97#define icmp_gwaddr icmp_hun.ih_gwaddr
98#define icmp_id icmp_hun.ih_idseq.icd_id
99#define icmp_seq icmp_hun.ih_idseq.icd_seq
100#define icmp_void icmp_hun.ih_void
101#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
102#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
103#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
104#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
105#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
106 union
107 {
108 struct
109 {
110 u_int32_t its_otime;
111 u_int32_t its_rtime;
112 u_int32_t its_ttime;
113 } id_ts;
114 struct
115 {
116 struct ip idi_ip;
117 /* options and then 64 bits of data */
118 } id_ip;
119 struct icmp_ra_addr id_radv;
120 u_int32_t id_mask;
121 u_int8_t id_data[1];
122 } icmp_dun;
123#define icmp_otime icmp_dun.id_ts.its_otime
124#define icmp_rtime icmp_dun.id_ts.its_rtime
125#define icmp_ttime icmp_dun.id_ts.its_ttime
126#define icmp_ip icmp_dun.id_ip.idi_ip
127#define icmp_radv icmp_dun.id_radv
128#define icmp_mask icmp_dun.id_mask
129#define icmp_data icmp_dun.id_data
130};
131#endif
132
Eric Andersen485b9551999-12-07 23:14:59 +0000133#define DEFDATALEN 56
134#define MAXIPLEN 60
135#define MAXICMPLEN 76
136#define MAXPACKET 65468
137#define MAX_DUP_CHK (8 * 128)
138#define MAXWAIT 10
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139#define PINGINTERVAL 1 /* second */
Eric Andersen485b9551999-12-07 23:14:59 +0000140
141#define O_QUIET (1 << 0)
142
143#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
144#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
145#define SET(bit) (A(bit) |= B(bit))
146#define CLR(bit) (A(bit) &= (~B(bit)))
147#define TST(bit) (A(bit) & B(bit))
148
Pavel Roskin0024abc2000-06-07 20:38:15 +0000149static void ping(const char *host);
150
Eric Andersen19db07b1999-12-11 08:41:28 +0000151/* common routines */
Eric Andersen485b9551999-12-07 23:14:59 +0000152static int in_cksum(unsigned short *buf, int sz)
153{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000154 int nleft = sz;
155 int sum = 0;
156 unsigned short *w = buf;
157 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000158
Erik Andersene49d5ec2000-02-08 19:58:47 +0000159 while (nleft > 1) {
160 sum += *w++;
161 nleft -= 2;
162 }
Eric Andersen485b9551999-12-07 23:14:59 +0000163
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 if (nleft == 1) {
165 *(unsigned char *) (&ans) = *(unsigned char *) w;
166 sum += ans;
167 }
Eric Andersen485b9551999-12-07 23:14:59 +0000168
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 sum = (sum >> 16) + (sum & 0xFFFF);
170 sum += (sum >> 16);
171 ans = ~sum;
172 return (ans);
173}
Eric Andersen485b9551999-12-07 23:14:59 +0000174
Eric Andersen19db07b1999-12-11 08:41:28 +0000175/* simple version */
Eric Andersen03f4c272000-07-06 23:10:29 +0000176#ifdef BB_FEATURE_SIMPLE_PING
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000177static const char *ping_usage = "ping host\n"
178#ifndef BB_FEATURE_TRIVIAL_HELP
179 "\nSend ICMP ECHO_REQUEST packets to network hosts\n"
180#endif
181 ;
Eric Andersen19db07b1999-12-11 08:41:28 +0000182
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183static char *hostname = NULL;
Eric Andersen19db07b1999-12-11 08:41:28 +0000184
185static void noresp(int ign)
186{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 printf("No response from %s\n", hostname);
188 exit(0);
Eric Andersen19db07b1999-12-11 08:41:28 +0000189}
190
Pavel Roskin0024abc2000-06-07 20:38:15 +0000191static void ping(const char *host)
Eric Andersen19db07b1999-12-11 08:41:28 +0000192{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193 struct hostent *h;
194 struct sockaddr_in pingaddr;
195 struct icmp *pkt;
196 int pingsock, c;
197 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +0000198
Erik Andersene49d5ec2000-02-08 19:58:47 +0000199 if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) { /* 1 == ICMP */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000200 perror("ping: creating a raw socket");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000201 exit(1);
Eric Andersen19db07b1999-12-11 08:41:28 +0000202 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203
204 /* drop root privs if running setuid */
205 setuid(getuid());
206
207 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
208
209 pingaddr.sin_family = AF_INET;
210 if (!(h = gethostbyname(host))) {
211 fprintf(stderr, "ping: unknown host %s\n", host);
212 exit(1);
213 }
214 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
215 hostname = h->h_name;
216
217 pkt = (struct icmp *) packet;
218 memset(pkt, 0, sizeof(packet));
219 pkt->icmp_type = ICMP_ECHO;
220 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
221
222 c = sendto(pingsock, packet, sizeof(packet), 0,
223 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
224
225 if (c < 0 || c != sizeof(packet)) {
226 if (c < 0)
Pavel Roskin0024abc2000-06-07 20:38:15 +0000227 perror("ping: sendto");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 fprintf(stderr, "ping: write incomplete\n");
229 exit(1);
230 }
231
232 signal(SIGALRM, noresp);
233 alarm(5); /* give the host 5000ms to respond */
234 /* listen for replies */
235 while (1) {
236 struct sockaddr_in from;
237 size_t fromlen = sizeof(from);
238
239 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
240 (struct sockaddr *) &from, &fromlen)) < 0) {
241 if (errno == EINTR)
242 continue;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000243 perror("ping: recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 continue;
245 }
246 if (c >= 76) { /* ip + icmp */
247 struct iphdr *iphdr = (struct iphdr *) packet;
248
249 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
250 if (pkt->icmp_type == ICMP_ECHOREPLY)
251 break;
252 }
253 }
254 printf("%s is alive!\n", hostname);
Pavel Roskin0024abc2000-06-07 20:38:15 +0000255 return;
Eric Andersen19db07b1999-12-11 08:41:28 +0000256}
257
258extern int ping_main(int argc, char **argv)
259{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260 argc--;
261 argv++;
262 if (argc < 1)
263 usage(ping_usage);
264 ping(*argv);
265 exit(TRUE);
Eric Andersen19db07b1999-12-11 08:41:28 +0000266}
267
Eric Andersen03f4c272000-07-06 23:10:29 +0000268#else /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen19db07b1999-12-11 08:41:28 +0000269/* full(er) version */
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000270static const char *ping_usage = "ping [OPTION]... host\n"
271#ifndef BB_FEATURE_TRIVIAL_HELP
272 "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000273 "Options:\n"
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000274 "\t-c COUNT\tSend only COUNT pings.\n"
Pavel Roskin0024abc2000-06-07 20:38:15 +0000275 "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n"
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000276 "\t-q\t\tQuiet mode, only displays output at start\n"
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000277 "\t\t\tand when finished.\n"
278#endif
279 ;
Eric Andersen19db07b1999-12-11 08:41:28 +0000280
281static char *hostname = NULL;
282static struct sockaddr_in pingaddr;
283static int pingsock = -1;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000284static int datalen = DEFDATALEN;
Eric Andersen19db07b1999-12-11 08:41:28 +0000285
286static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
287static int myid = 0, options = 0;
288static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
289static char rcvd_tbl[MAX_DUP_CHK / 8];
290
291static void sendping(int);
292static void pingstats(int);
293static void unpack(char *, int, struct sockaddr_in *);
294
Eric Andersen19db07b1999-12-11 08:41:28 +0000295/**************************************************************************/
296
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297static void pingstats(int ign)
298{
299 signal(SIGINT, SIG_IGN);
300
301 printf("\n--- %s ping statistics ---\n", hostname);
302 printf("%ld packets transmitted, ", ntransmitted);
303 printf("%ld packets received, ", nreceived);
304 if (nrepeats)
305 printf("%ld duplicates, ", nrepeats);
306 if (ntransmitted)
307 printf("%ld%% packet loss\n",
308 (ntransmitted - nreceived) * 100 / ntransmitted);
309 if (nreceived)
310 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
311 tmin / 10, tmin % 10,
312 (tsum / (nreceived + nrepeats)) / 10,
313 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
314 exit(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000315}
316
317static void sendping(int ign)
318{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 struct icmp *pkt;
320 int i;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000321 char packet[datalen + 8];
Eric Andersen485b9551999-12-07 23:14:59 +0000322
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000324
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 pkt->icmp_type = ICMP_ECHO;
326 pkt->icmp_code = 0;
327 pkt->icmp_cksum = 0;
328 pkt->icmp_seq = ntransmitted++;
329 pkt->icmp_id = myid;
330 CLR(pkt->icmp_seq % MAX_DUP_CHK);
Eric Andersen485b9551999-12-07 23:14:59 +0000331
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332 gettimeofday((struct timeval *) &packet[8], NULL);
333 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000334
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 i = sendto(pingsock, packet, sizeof(packet), 0,
336 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
337
Pavel Roskin0024abc2000-06-07 20:38:15 +0000338 if (i < 0)
339 fatalError("ping: sendto: %s\n", strerror(errno));
340 else if (i != sizeof(packet))
341 fatalError("ping wrote %d chars; %d expected\n", i,
342 (int)sizeof(packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343
344 signal(SIGALRM, sendping);
345 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
346 alarm(PINGINTERVAL);
347 } else { /* done, wait for the last ping to come back */
348 /* todo, don't necessarily need to wait so long... */
349 signal(SIGALRM, pingstats);
350 alarm(MAXWAIT);
351 }
Eric Andersen485b9551999-12-07 23:14:59 +0000352}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353
Erik Andersen227a59b2000-04-25 23:24:55 +0000354static char *icmp_type_name (int id)
355{
356 switch (id) {
357 case ICMP_ECHOREPLY: return "Echo Reply";
358 case ICMP_DEST_UNREACH: return "Destination Unreachable";
359 case ICMP_SOURCE_QUENCH: return "Source Quench";
360 case ICMP_REDIRECT: return "Redirect (change route)";
361 case ICMP_ECHO: return "Echo Request";
362 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
363 case ICMP_PARAMETERPROB: return "Parameter Problem";
364 case ICMP_TIMESTAMP: return "Timestamp Request";
365 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
366 case ICMP_INFO_REQUEST: return "Information Request";
367 case ICMP_INFO_REPLY: return "Information Reply";
368 case ICMP_ADDRESS: return "Address Mask Request";
369 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
370 default: return "unknown ICMP type";
371 }
372}
373
Eric Andersen485b9551999-12-07 23:14:59 +0000374static void unpack(char *buf, int sz, struct sockaddr_in *from)
375{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 struct icmp *icmppkt;
377 struct iphdr *iphdr;
378 struct timeval tv, *tp;
379 int hlen, dupflag;
380 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000381
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000383
Erik Andersene49d5ec2000-02-08 19:58:47 +0000384 /* check IP header */
385 iphdr = (struct iphdr *) buf;
386 hlen = iphdr->ihl << 2;
387 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000388 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000390
Erik Andersene49d5ec2000-02-08 19:58:47 +0000391 sz -= hlen;
392 icmppkt = (struct icmp *) (buf + hlen);
393
Erik Andersen227a59b2000-04-25 23:24:55 +0000394 if (icmppkt->icmp_id != myid)
395 return; /* not our ping */
396
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Erik Andersen227a59b2000-04-25 23:24:55 +0000398 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000399 tp = (struct timeval *) icmppkt->icmp_data;
400
401 if ((tv.tv_usec -= tp->tv_usec) < 0) {
402 --tv.tv_sec;
403 tv.tv_usec += 1000000;
404 }
405 tv.tv_sec -= tp->tv_sec;
406
407 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
408 tsum += triptime;
409 if (triptime < tmin)
410 tmin = triptime;
411 if (triptime > tmax)
412 tmax = triptime;
413
414 if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
415 ++nrepeats;
416 --nreceived;
417 dupflag = 1;
418 } else {
419 SET(icmppkt->icmp_seq % MAX_DUP_CHK);
420 dupflag = 0;
421 }
422
423 if (options & O_QUIET)
424 return;
425
426 printf("%d bytes from %s: icmp_seq=%u", sz,
427 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
428 icmppkt->icmp_seq);
429 printf(" ttl=%d", iphdr->ttl);
430 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
431 if (dupflag)
432 printf(" (DUP!)");
433 printf("\n");
Erik Andersen227a59b2000-04-25 23:24:55 +0000434 } else
435 if (icmppkt->icmp_type != ICMP_ECHO)
436 fprintf(stderr,
437 "Warning: Got ICMP %d (%s)\n",
438 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
Eric Andersen485b9551999-12-07 23:14:59 +0000439}
440
Pavel Roskin0024abc2000-06-07 20:38:15 +0000441static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000442{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 struct protoent *proto;
444 struct hostent *h;
445 char buf[MAXHOSTNAMELEN];
Pavel Roskin0024abc2000-06-07 20:38:15 +0000446 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 int sockopt;
448
449 proto = getprotobyname("icmp");
450 /* if getprotobyname failed, just silently force
451 * proto->p_proto to have the correct value for "icmp" */
452 if ((pingsock = socket(AF_INET, SOCK_RAW,
453 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
454 if (errno == EPERM) {
455 fprintf(stderr, "ping: permission denied. (are you root?)\n");
456 } else {
Pavel Roskin0024abc2000-06-07 20:38:15 +0000457 perror("ping: creating a raw socket");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000458 }
459 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000460 }
Eric Andersen485b9551999-12-07 23:14:59 +0000461
Erik Andersene49d5ec2000-02-08 19:58:47 +0000462 /* drop root privs if running setuid */
463 setuid(getuid());
Eric Andersen485b9551999-12-07 23:14:59 +0000464
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000466
Erik Andersene49d5ec2000-02-08 19:58:47 +0000467 pingaddr.sin_family = AF_INET;
468 if (!(h = gethostbyname(host))) {
469 fprintf(stderr, "ping: unknown host %s\n", host);
470 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000471 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000472
473 if (h->h_addrtype != AF_INET) {
474 fprintf(stderr,
475 "ping: unknown address type; only AF_INET is currently supported.\n");
476 exit(1);
477 }
478
479 pingaddr.sin_family = AF_INET; /* h->h_addrtype */
480 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
481 strncpy(buf, h->h_name, sizeof(buf) - 1);
482 hostname = buf;
483
484 /* enable broadcast pings */
485 sockopt = 1;
486 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
487 sizeof(sockopt));
488
489 /* set recv buf for broadcast pings */
490 sockopt = 48 * 1024;
491 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
492 sizeof(sockopt));
493
494 printf("PING %s (%s): %d data bytes\n",
495 hostname,
496 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
Pavel Roskin0024abc2000-06-07 20:38:15 +0000497 datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498
499 signal(SIGINT, pingstats);
500
501 /* start the ping's going ... */
502 sendping(0);
503
504 /* listen for replies */
505 while (1) {
506 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000507 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 int c;
509
510 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
511 (struct sockaddr *) &from, &fromlen)) < 0) {
512 if (errno == EINTR)
513 continue;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000514 perror("ping: recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 continue;
516 }
517 unpack(packet, c, &from);
518 if (pingcount > 0 && nreceived >= pingcount)
519 break;
520 }
521 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000522}
523
524extern int ping_main(int argc, char **argv)
525{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 char *thisarg;
Eric Andersen485b9551999-12-07 23:14:59 +0000527
Erik Andersene49d5ec2000-02-08 19:58:47 +0000528 argc--;
529 argv++;
530 options = 0;
531 /* Parse any options */
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000532 while (argc >= 1 && **argv == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 thisarg = *argv;
534 thisarg++;
535 switch (*thisarg) {
536 case 'q':
537 options |= O_QUIET;
538 break;
539 case 'c':
Pavel Roskin0024abc2000-06-07 20:38:15 +0000540 if (--argc <= 0)
541 usage(ping_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 argv++;
543 pingcount = atoi(*argv);
544 break;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000545 case 's':
546 if (--argc <= 0)
547 usage(ping_usage);
548 argv++;
549 datalen = atoi(*argv);
550 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000551 default:
552 usage(ping_usage);
553 }
554 argc--;
555 argv++;
556 }
557 if (argc < 1)
558 usage(ping_usage);
559
560 myid = getpid() & 0xFFFF;
561 ping(*argv);
Eric Andersen9ca57d32000-06-19 18:51:53 +0000562 return(TRUE);
Eric Andersen485b9551999-12-07 23:14:59 +0000563}
Eric Andersen03f4c272000-07-06 23:10:29 +0000564#endif /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen485b9551999-12-07 23:14:59 +0000565
566/*
567 * Copyright (c) 1989 The Regents of the University of California.
568 * All rights reserved.
569 *
570 * This code is derived from software contributed to Berkeley by
571 * Mike Muuss.
572 *
573 * Redistribution and use in source and binary forms, with or without
574 * modification, are permitted provided that the following conditions
575 * are met:
576 * 1. Redistributions of source code must retain the above copyright
577 * notice, this list of conditions and the following disclaimer.
578 * 2. Redistributions in binary form must reproduce the above copyright
579 * notice, this list of conditions and the following disclaimer in the
580 * documentation and/or other materials provided with the distribution.
581 * 3. All advertising materials mentioning features or use of this software
582 * must display the following acknowledgement:
583 * This product includes software developed by the University of
584 * California, Berkeley and its contributors.
585 * 4. Neither the name of the University nor the names of its contributors
586 * may be used to endorse or promote products derived from this software
587 * without specific prior written permission.
588 *
589 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
590 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
591 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
592 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
593 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
594 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
595 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
596 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
597 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
598 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
599 * SUCH DAMAGE.
600 */