blob: 8b82dcaace82bad227e43a6cea3767d52bb1db32 [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 Andersenb6b519b2001-04-09 23:52:18 +00003 * $Id: ping.c,v 1.40 2001/04/09 23:52:18 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
Eric Andersen485b9551999-12-07 23:14:59 +000034#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>
Eric Andersened3ef502001-01-27 08:24:39 +000049#include <unistd.h>
50#include <string.h>
51#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000052#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000053
Eric Andersen9ca57d32000-06-19 18:51:53 +000054
55/* It turns out that libc5 doesn't have proper icmp support
56 * built into it header files, so we have to supplement it */
Eric Andersenb6b519b2001-04-09 23:52:18 +000057#if __GNU_LIBRARY__ < 5
Mark Whitley59ab0252001-01-23 22:30:04 +000058static const int ICMP_MINLEN = 8; /* abs minimum */
Eric Andersen9ca57d32000-06-19 18:51:53 +000059
60struct icmp_ra_addr
61{
62 u_int32_t ira_addr;
63 u_int32_t ira_preference;
64};
65
66
67struct icmp
68{
69 u_int8_t icmp_type; /* type of message, see below */
70 u_int8_t icmp_code; /* type sub code */
71 u_int16_t icmp_cksum; /* ones complement checksum of struct */
72 union
73 {
74 u_char ih_pptr; /* ICMP_PARAMPROB */
75 struct in_addr ih_gwaddr; /* gateway address */
76 struct ih_idseq /* echo datagram */
77 {
78 u_int16_t icd_id;
79 u_int16_t icd_seq;
80 } ih_idseq;
81 u_int32_t ih_void;
82
83 /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
84 struct ih_pmtu
85 {
86 u_int16_t ipm_void;
87 u_int16_t ipm_nextmtu;
88 } ih_pmtu;
89
90 struct ih_rtradv
91 {
92 u_int8_t irt_num_addrs;
93 u_int8_t irt_wpa;
94 u_int16_t irt_lifetime;
95 } ih_rtradv;
96 } icmp_hun;
97#define icmp_pptr icmp_hun.ih_pptr
98#define icmp_gwaddr icmp_hun.ih_gwaddr
99#define icmp_id icmp_hun.ih_idseq.icd_id
100#define icmp_seq icmp_hun.ih_idseq.icd_seq
101#define icmp_void icmp_hun.ih_void
102#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
103#define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
104#define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
105#define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
106#define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
107 union
108 {
109 struct
110 {
111 u_int32_t its_otime;
112 u_int32_t its_rtime;
113 u_int32_t its_ttime;
114 } id_ts;
115 struct
116 {
117 struct ip idi_ip;
118 /* options and then 64 bits of data */
119 } id_ip;
120 struct icmp_ra_addr id_radv;
121 u_int32_t id_mask;
122 u_int8_t id_data[1];
123 } icmp_dun;
124#define icmp_otime icmp_dun.id_ts.its_otime
125#define icmp_rtime icmp_dun.id_ts.its_rtime
126#define icmp_ttime icmp_dun.id_ts.its_ttime
127#define icmp_ip icmp_dun.id_ip.idi_ip
128#define icmp_radv icmp_dun.id_radv
129#define icmp_mask icmp_dun.id_mask
130#define icmp_data icmp_dun.id_data
131};
132#endif
133
Mark Whitley59ab0252001-01-23 22:30:04 +0000134static const int DEFDATALEN = 56;
135static const int MAXIPLEN = 60;
136static const int MAXICMPLEN = 76;
137static const int MAXPACKET = 65468;
Eric Andersen485b9551999-12-07 23:14:59 +0000138#define MAX_DUP_CHK (8 * 128)
Mark Whitley59ab0252001-01-23 22:30:04 +0000139static const int MAXWAIT = 10;
140static const int PINGINTERVAL = 1; /* second */
Eric Andersen485b9551999-12-07 23:14:59 +0000141
142#define O_QUIET (1 << 0)
143
144#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
145#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
146#define SET(bit) (A(bit) |= B(bit))
147#define CLR(bit) (A(bit) &= (~B(bit)))
148#define TST(bit) (A(bit) & B(bit))
149
Pavel Roskin0024abc2000-06-07 20:38:15 +0000150static void ping(const char *host);
151
Eric Andersen19db07b1999-12-11 08:41:28 +0000152/* common routines */
Eric Andersen485b9551999-12-07 23:14:59 +0000153static int in_cksum(unsigned short *buf, int sz)
154{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155 int nleft = sz;
156 int sum = 0;
157 unsigned short *w = buf;
158 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000159
Erik Andersene49d5ec2000-02-08 19:58:47 +0000160 while (nleft > 1) {
161 sum += *w++;
162 nleft -= 2;
163 }
Eric Andersen485b9551999-12-07 23:14:59 +0000164
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 if (nleft == 1) {
166 *(unsigned char *) (&ans) = *(unsigned char *) w;
167 sum += ans;
168 }
Eric Andersen485b9551999-12-07 23:14:59 +0000169
Erik Andersene49d5ec2000-02-08 19:58:47 +0000170 sum = (sum >> 16) + (sum & 0xFFFF);
171 sum += (sum >> 16);
172 ans = ~sum;
173 return (ans);
174}
Eric Andersen485b9551999-12-07 23:14:59 +0000175
Eric Andersen19db07b1999-12-11 08:41:28 +0000176/* simple version */
Eric Andersen03f4c272000-07-06 23:10:29 +0000177#ifdef BB_FEATURE_SIMPLE_PING
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178static char *hostname = NULL;
Eric Andersen19db07b1999-12-11 08:41:28 +0000179
180static void noresp(int ign)
181{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 printf("No response from %s\n", hostname);
183 exit(0);
Eric Andersen19db07b1999-12-11 08:41:28 +0000184}
185
Pavel Roskin0024abc2000-06-07 20:38:15 +0000186static void ping(const char *host)
Eric Andersen19db07b1999-12-11 08:41:28 +0000187{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188 struct hostent *h;
189 struct sockaddr_in pingaddr;
190 struct icmp *pkt;
191 int pingsock, c;
192 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +0000193
Matt Kraaia9819b22000-12-22 01:48:07 +0000194 if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) /* 1 == ICMP */
195 perror_msg_and_die("creating a raw socket");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196
197 /* drop root privs if running setuid */
198 setuid(getuid());
199
200 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
201
202 pingaddr.sin_family = AF_INET;
203 if (!(h = gethostbyname(host))) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000204 error_msg("unknown host %s", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 exit(1);
206 }
207 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
208 hostname = h->h_name;
209
210 pkt = (struct icmp *) packet;
211 memset(pkt, 0, sizeof(packet));
212 pkt->icmp_type = ICMP_ECHO;
213 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
214
215 c = sendto(pingsock, packet, sizeof(packet), 0,
216 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
217
Matt Kraaia9819b22000-12-22 01:48:07 +0000218 if (c < 0 || c != sizeof(packet))
219 perror_msg_and_die("sendto");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000220
221 signal(SIGALRM, noresp);
222 alarm(5); /* give the host 5000ms to respond */
223 /* listen for replies */
224 while (1) {
225 struct sockaddr_in from;
226 size_t fromlen = sizeof(from);
227
228 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
229 (struct sockaddr *) &from, &fromlen)) < 0) {
230 if (errno == EINTR)
231 continue;
Matt Kraaia9819b22000-12-22 01:48:07 +0000232 perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000233 continue;
234 }
235 if (c >= 76) { /* ip + icmp */
236 struct iphdr *iphdr = (struct iphdr *) packet;
237
238 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
239 if (pkt->icmp_type == ICMP_ECHOREPLY)
240 break;
241 }
242 }
243 printf("%s is alive!\n", hostname);
Pavel Roskin0024abc2000-06-07 20:38:15 +0000244 return;
Eric Andersen19db07b1999-12-11 08:41:28 +0000245}
246
247extern int ping_main(int argc, char **argv)
248{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249 argc--;
250 argv++;
251 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000252 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000254 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000255}
256
Eric Andersen03f4c272000-07-06 23:10:29 +0000257#else /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen19db07b1999-12-11 08:41:28 +0000258/* full(er) version */
Eric Andersen19db07b1999-12-11 08:41:28 +0000259static char *hostname = NULL;
260static struct sockaddr_in pingaddr;
261static int pingsock = -1;
Mark Whitley59ab0252001-01-23 22:30:04 +0000262static int datalen; /* intentionally uninitialized to work around gcc bug */
Eric Andersen19db07b1999-12-11 08:41:28 +0000263
264static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
265static int myid = 0, options = 0;
266static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
267static char rcvd_tbl[MAX_DUP_CHK / 8];
268
269static void sendping(int);
270static void pingstats(int);
271static void unpack(char *, int, struct sockaddr_in *);
272
Eric Andersen19db07b1999-12-11 08:41:28 +0000273/**************************************************************************/
274
Eric Andersenfad04fd2000-07-14 06:49:52 +0000275static void pingstats(int junk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276{
Matt Kraaib938e2f2000-09-20 04:33:30 +0000277 int status;
278
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 signal(SIGINT, SIG_IGN);
280
281 printf("\n--- %s ping statistics ---\n", hostname);
282 printf("%ld packets transmitted, ", ntransmitted);
283 printf("%ld packets received, ", nreceived);
284 if (nrepeats)
285 printf("%ld duplicates, ", nrepeats);
286 if (ntransmitted)
287 printf("%ld%% packet loss\n",
288 (ntransmitted - nreceived) * 100 / ntransmitted);
289 if (nreceived)
290 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
291 tmin / 10, tmin % 10,
292 (tsum / (nreceived + nrepeats)) / 10,
293 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Matt Kraaib938e2f2000-09-20 04:33:30 +0000294 if (nreceived != 0)
295 status = EXIT_SUCCESS;
296 else
297 status = EXIT_FAILURE;
298 exit(status);
Eric Andersen485b9551999-12-07 23:14:59 +0000299}
300
Eric Andersenfad04fd2000-07-14 06:49:52 +0000301static void sendping(int junk)
Eric Andersen485b9551999-12-07 23:14:59 +0000302{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000303 struct icmp *pkt;
304 int i;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000305 char packet[datalen + 8];
Eric Andersen485b9551999-12-07 23:14:59 +0000306
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000308
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 pkt->icmp_type = ICMP_ECHO;
310 pkt->icmp_code = 0;
311 pkt->icmp_cksum = 0;
312 pkt->icmp_seq = ntransmitted++;
313 pkt->icmp_id = myid;
314 CLR(pkt->icmp_seq % MAX_DUP_CHK);
Eric Andersen485b9551999-12-07 23:14:59 +0000315
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 gettimeofday((struct timeval *) &packet[8], NULL);
317 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000318
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 i = sendto(pingsock, packet, sizeof(packet), 0,
320 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
321
Pavel Roskin0024abc2000-06-07 20:38:15 +0000322 if (i < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000323 perror_msg_and_die("sendto");
Eric Andersenfad04fd2000-07-14 06:49:52 +0000324 else if ((size_t)i != sizeof(packet))
Matt Kraaidd19c692001-01-31 19:00:21 +0000325 error_msg_and_die("ping wrote %d chars; %d expected", i,
Pavel Roskin0024abc2000-06-07 20:38:15 +0000326 (int)sizeof(packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327
328 signal(SIGALRM, sendping);
329 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
330 alarm(PINGINTERVAL);
331 } else { /* done, wait for the last ping to come back */
332 /* todo, don't necessarily need to wait so long... */
333 signal(SIGALRM, pingstats);
334 alarm(MAXWAIT);
335 }
Eric Andersen485b9551999-12-07 23:14:59 +0000336}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337
Erik Andersen227a59b2000-04-25 23:24:55 +0000338static char *icmp_type_name (int id)
339{
340 switch (id) {
341 case ICMP_ECHOREPLY: return "Echo Reply";
342 case ICMP_DEST_UNREACH: return "Destination Unreachable";
343 case ICMP_SOURCE_QUENCH: return "Source Quench";
344 case ICMP_REDIRECT: return "Redirect (change route)";
345 case ICMP_ECHO: return "Echo Request";
346 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
347 case ICMP_PARAMETERPROB: return "Parameter Problem";
348 case ICMP_TIMESTAMP: return "Timestamp Request";
349 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
350 case ICMP_INFO_REQUEST: return "Information Request";
351 case ICMP_INFO_REPLY: return "Information Reply";
352 case ICMP_ADDRESS: return "Address Mask Request";
353 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
354 default: return "unknown ICMP type";
355 }
356}
357
Eric Andersen485b9551999-12-07 23:14:59 +0000358static void unpack(char *buf, int sz, struct sockaddr_in *from)
359{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 struct icmp *icmppkt;
361 struct iphdr *iphdr;
362 struct timeval tv, *tp;
363 int hlen, dupflag;
364 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000365
Erik Andersene49d5ec2000-02-08 19:58:47 +0000366 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000367
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 /* check IP header */
369 iphdr = (struct iphdr *) buf;
370 hlen = iphdr->ihl << 2;
371 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000372 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000374
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 sz -= hlen;
376 icmppkt = (struct icmp *) (buf + hlen);
377
Erik Andersen227a59b2000-04-25 23:24:55 +0000378 if (icmppkt->icmp_id != myid)
379 return; /* not our ping */
380
Erik Andersene49d5ec2000-02-08 19:58:47 +0000381 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Erik Andersen227a59b2000-04-25 23:24:55 +0000382 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 tp = (struct timeval *) icmppkt->icmp_data;
384
385 if ((tv.tv_usec -= tp->tv_usec) < 0) {
386 --tv.tv_sec;
387 tv.tv_usec += 1000000;
388 }
389 tv.tv_sec -= tp->tv_sec;
390
391 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
392 tsum += triptime;
393 if (triptime < tmin)
394 tmin = triptime;
395 if (triptime > tmax)
396 tmax = triptime;
397
398 if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
399 ++nrepeats;
400 --nreceived;
401 dupflag = 1;
402 } else {
403 SET(icmppkt->icmp_seq % MAX_DUP_CHK);
404 dupflag = 0;
405 }
406
407 if (options & O_QUIET)
408 return;
409
410 printf("%d bytes from %s: icmp_seq=%u", sz,
411 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
412 icmppkt->icmp_seq);
413 printf(" ttl=%d", iphdr->ttl);
414 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
415 if (dupflag)
416 printf(" (DUP!)");
417 printf("\n");
Erik Andersen227a59b2000-04-25 23:24:55 +0000418 } else
419 if (icmppkt->icmp_type != ICMP_ECHO)
Matt Kraaidd19c692001-01-31 19:00:21 +0000420 error_msg("Warning: Got ICMP %d (%s)",
Erik Andersen227a59b2000-04-25 23:24:55 +0000421 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
Eric Andersen485b9551999-12-07 23:14:59 +0000422}
423
Pavel Roskin0024abc2000-06-07 20:38:15 +0000424static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000425{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000426 struct protoent *proto;
427 struct hostent *h;
428 char buf[MAXHOSTNAMELEN];
Pavel Roskin0024abc2000-06-07 20:38:15 +0000429 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000430 int sockopt;
431
432 proto = getprotobyname("icmp");
433 /* if getprotobyname failed, just silently force
434 * proto->p_proto to have the correct value for "icmp" */
435 if ((pingsock = socket(AF_INET, SOCK_RAW,
436 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
Matt Kraaia9819b22000-12-22 01:48:07 +0000437 if (errno == EPERM)
Matt Kraaidd19c692001-01-31 19:00:21 +0000438 error_msg_and_die("permission denied. (are you root?)");
Matt Kraaia9819b22000-12-22 01:48:07 +0000439 else
440 perror_msg_and_die("creating a raw socket");
Eric Andersen485b9551999-12-07 23:14:59 +0000441 }
Eric Andersen485b9551999-12-07 23:14:59 +0000442
Erik Andersene49d5ec2000-02-08 19:58:47 +0000443 /* drop root privs if running setuid */
444 setuid(getuid());
Eric Andersen485b9551999-12-07 23:14:59 +0000445
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000447
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 pingaddr.sin_family = AF_INET;
449 if (!(h = gethostbyname(host))) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000450 error_msg("unknown host %s", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000451 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000452 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000453
454 if (h->h_addrtype != AF_INET) {
Matt Kraaidd19c692001-01-31 19:00:21 +0000455 error_msg("unknown address type; only AF_INET is currently supported.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000456 exit(1);
457 }
458
459 pingaddr.sin_family = AF_INET; /* h->h_addrtype */
460 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
461 strncpy(buf, h->h_name, sizeof(buf) - 1);
462 hostname = buf;
463
464 /* enable broadcast pings */
465 sockopt = 1;
466 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
467 sizeof(sockopt));
468
469 /* set recv buf for broadcast pings */
470 sockopt = 48 * 1024;
471 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
472 sizeof(sockopt));
473
474 printf("PING %s (%s): %d data bytes\n",
475 hostname,
476 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
Pavel Roskin0024abc2000-06-07 20:38:15 +0000477 datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478
479 signal(SIGINT, pingstats);
480
481 /* start the ping's going ... */
482 sendping(0);
483
484 /* listen for replies */
485 while (1) {
486 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000487 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000488 int c;
489
490 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
491 (struct sockaddr *) &from, &fromlen)) < 0) {
492 if (errno == EINTR)
493 continue;
Matt Kraaia9819b22000-12-22 01:48:07 +0000494 perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000495 continue;
496 }
497 unpack(packet, c, &from);
498 if (pingcount > 0 && nreceived >= pingcount)
499 break;
500 }
501 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000502}
503
504extern int ping_main(int argc, char **argv)
505{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 char *thisarg;
Eric Andersen485b9551999-12-07 23:14:59 +0000507
Mark Whitley59ab0252001-01-23 22:30:04 +0000508 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
509
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 argc--;
511 argv++;
512 options = 0;
513 /* Parse any options */
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000514 while (argc >= 1 && **argv == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000515 thisarg = *argv;
516 thisarg++;
517 switch (*thisarg) {
518 case 'q':
519 options |= O_QUIET;
520 break;
521 case 'c':
Pavel Roskin0024abc2000-06-07 20:38:15 +0000522 if (--argc <= 0)
Eric Andersen67991cf2001-02-14 21:23:06 +0000523 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 argv++;
525 pingcount = atoi(*argv);
526 break;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000527 case 's':
528 if (--argc <= 0)
Eric Andersen67991cf2001-02-14 21:23:06 +0000529 show_usage();
Pavel Roskin0024abc2000-06-07 20:38:15 +0000530 argv++;
531 datalen = atoi(*argv);
532 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000533 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000534 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000535 }
536 argc--;
537 argv++;
538 }
539 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +0000540 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000541
542 myid = getpid() & 0xFFFF;
543 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000544 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000545}
Eric Andersen03f4c272000-07-06 23:10:29 +0000546#endif /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen485b9551999-12-07 23:14:59 +0000547
548/*
549 * Copyright (c) 1989 The Regents of the University of California.
550 * All rights reserved.
551 *
552 * This code is derived from software contributed to Berkeley by
553 * Mike Muuss.
554 *
555 * Redistribution and use in source and binary forms, with or without
556 * modification, are permitted provided that the following conditions
557 * are met:
558 * 1. Redistributions of source code must retain the above copyright
559 * notice, this list of conditions and the following disclaimer.
560 * 2. Redistributions in binary form must reproduce the above copyright
561 * notice, this list of conditions and the following disclaimer in the
562 * documentation and/or other materials provided with the distribution.
Eric Andersen4e573f42000-11-14 23:29:24 +0000563 *
564 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
565 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
566 *
Eric Andersen485b9551999-12-07 23:14:59 +0000567 * 4. Neither the name of the University nor the names of its contributors
568 * may be used to endorse or promote products derived from this software
569 * without specific prior written permission.
570 *
571 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
572 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
573 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
574 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
575 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
576 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
577 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
578 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
579 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
580 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
581 * SUCH DAMAGE.
582 */