blob: e9242e9b7f349228dd2c7f4a3e15bc7eae743f63 [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/*
Matt Kraaia9819b22000-12-22 01:48:07 +00003 * $Id: ping.c,v 1.30 2000/12/22 01:48:07 kraai 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 Andersen3570a342000-09-25 21:45:58 +000034#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000035#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 */
Eric Andersen999bf722000-07-09 06:59:58 +000054#if ! defined __GLIBC__ && ! defined __UCLIBC__
Eric Andersen9ca57d32000-06-19 18:51:53 +000055typedef 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 Andersene49d5ec2000-02-08 19:58:47 +0000177static char *hostname = NULL;
Eric Andersen19db07b1999-12-11 08:41:28 +0000178
179static void noresp(int ign)
180{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 printf("No response from %s\n", hostname);
182 exit(0);
Eric Andersen19db07b1999-12-11 08:41:28 +0000183}
184
Pavel Roskin0024abc2000-06-07 20:38:15 +0000185static void ping(const char *host)
Eric Andersen19db07b1999-12-11 08:41:28 +0000186{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 struct hostent *h;
188 struct sockaddr_in pingaddr;
189 struct icmp *pkt;
190 int pingsock, c;
191 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +0000192
Matt Kraaia9819b22000-12-22 01:48:07 +0000193 if ((pingsock = socket(AF_INET, SOCK_RAW, 1)) < 0) /* 1 == ICMP */
194 perror_msg_and_die("creating a raw socket");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195
196 /* drop root privs if running setuid */
197 setuid(getuid());
198
199 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
200
201 pingaddr.sin_family = AF_INET;
202 if (!(h = gethostbyname(host))) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000203 error_msg("unknown host %s\n", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000204 exit(1);
205 }
206 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
207 hostname = h->h_name;
208
209 pkt = (struct icmp *) packet;
210 memset(pkt, 0, sizeof(packet));
211 pkt->icmp_type = ICMP_ECHO;
212 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
213
214 c = sendto(pingsock, packet, sizeof(packet), 0,
215 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
216
Matt Kraaia9819b22000-12-22 01:48:07 +0000217 if (c < 0 || c != sizeof(packet))
218 perror_msg_and_die("sendto");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219
220 signal(SIGALRM, noresp);
221 alarm(5); /* give the host 5000ms to respond */
222 /* listen for replies */
223 while (1) {
224 struct sockaddr_in from;
225 size_t fromlen = sizeof(from);
226
227 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
228 (struct sockaddr *) &from, &fromlen)) < 0) {
229 if (errno == EINTR)
230 continue;
Matt Kraaia9819b22000-12-22 01:48:07 +0000231 perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 continue;
233 }
234 if (c >= 76) { /* ip + icmp */
235 struct iphdr *iphdr = (struct iphdr *) packet;
236
237 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
238 if (pkt->icmp_type == ICMP_ECHOREPLY)
239 break;
240 }
241 }
242 printf("%s is alive!\n", hostname);
Pavel Roskin0024abc2000-06-07 20:38:15 +0000243 return;
Eric Andersen19db07b1999-12-11 08:41:28 +0000244}
245
246extern int ping_main(int argc, char **argv)
247{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 argc--;
249 argv++;
250 if (argc < 1)
251 usage(ping_usage);
252 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000253 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000254}
255
Eric Andersen03f4c272000-07-06 23:10:29 +0000256#else /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen19db07b1999-12-11 08:41:28 +0000257/* full(er) version */
Eric Andersen19db07b1999-12-11 08:41:28 +0000258static char *hostname = NULL;
259static struct sockaddr_in pingaddr;
260static int pingsock = -1;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000261static int datalen = DEFDATALEN;
Eric Andersen19db07b1999-12-11 08:41:28 +0000262
263static long ntransmitted = 0, nreceived = 0, nrepeats = 0, pingcount = 0;
264static int myid = 0, options = 0;
265static unsigned long tmin = ULONG_MAX, tmax = 0, tsum = 0;
266static char rcvd_tbl[MAX_DUP_CHK / 8];
267
268static void sendping(int);
269static void pingstats(int);
270static void unpack(char *, int, struct sockaddr_in *);
271
Eric Andersen19db07b1999-12-11 08:41:28 +0000272/**************************************************************************/
273
Eric Andersenfad04fd2000-07-14 06:49:52 +0000274static void pingstats(int junk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275{
Matt Kraaib938e2f2000-09-20 04:33:30 +0000276 int status;
277
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 signal(SIGINT, SIG_IGN);
279
280 printf("\n--- %s ping statistics ---\n", hostname);
281 printf("%ld packets transmitted, ", ntransmitted);
282 printf("%ld packets received, ", nreceived);
283 if (nrepeats)
284 printf("%ld duplicates, ", nrepeats);
285 if (ntransmitted)
286 printf("%ld%% packet loss\n",
287 (ntransmitted - nreceived) * 100 / ntransmitted);
288 if (nreceived)
289 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
290 tmin / 10, tmin % 10,
291 (tsum / (nreceived + nrepeats)) / 10,
292 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Matt Kraaib938e2f2000-09-20 04:33:30 +0000293 if (nreceived != 0)
294 status = EXIT_SUCCESS;
295 else
296 status = EXIT_FAILURE;
297 exit(status);
Eric Andersen485b9551999-12-07 23:14:59 +0000298}
299
Eric Andersenfad04fd2000-07-14 06:49:52 +0000300static void sendping(int junk)
Eric Andersen485b9551999-12-07 23:14:59 +0000301{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 struct icmp *pkt;
303 int i;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000304 char packet[datalen + 8];
Eric Andersen485b9551999-12-07 23:14:59 +0000305
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000307
Erik Andersene49d5ec2000-02-08 19:58:47 +0000308 pkt->icmp_type = ICMP_ECHO;
309 pkt->icmp_code = 0;
310 pkt->icmp_cksum = 0;
311 pkt->icmp_seq = ntransmitted++;
312 pkt->icmp_id = myid;
313 CLR(pkt->icmp_seq % MAX_DUP_CHK);
Eric Andersen485b9551999-12-07 23:14:59 +0000314
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 gettimeofday((struct timeval *) &packet[8], NULL);
316 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000317
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 i = sendto(pingsock, packet, sizeof(packet), 0,
319 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
320
Pavel Roskin0024abc2000-06-07 20:38:15 +0000321 if (i < 0)
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000322 perror_msg_and_die("sendto");
Eric Andersenfad04fd2000-07-14 06:49:52 +0000323 else if ((size_t)i != sizeof(packet))
Mark Whitleyf57c9442000-12-07 19:56:48 +0000324 error_msg_and_die("ping wrote %d chars; %d expected\n", i,
Pavel Roskin0024abc2000-06-07 20:38:15 +0000325 (int)sizeof(packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326
327 signal(SIGALRM, sendping);
328 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
329 alarm(PINGINTERVAL);
330 } else { /* done, wait for the last ping to come back */
331 /* todo, don't necessarily need to wait so long... */
332 signal(SIGALRM, pingstats);
333 alarm(MAXWAIT);
334 }
Eric Andersen485b9551999-12-07 23:14:59 +0000335}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336
Erik Andersen227a59b2000-04-25 23:24:55 +0000337static char *icmp_type_name (int id)
338{
339 switch (id) {
340 case ICMP_ECHOREPLY: return "Echo Reply";
341 case ICMP_DEST_UNREACH: return "Destination Unreachable";
342 case ICMP_SOURCE_QUENCH: return "Source Quench";
343 case ICMP_REDIRECT: return "Redirect (change route)";
344 case ICMP_ECHO: return "Echo Request";
345 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
346 case ICMP_PARAMETERPROB: return "Parameter Problem";
347 case ICMP_TIMESTAMP: return "Timestamp Request";
348 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
349 case ICMP_INFO_REQUEST: return "Information Request";
350 case ICMP_INFO_REPLY: return "Information Reply";
351 case ICMP_ADDRESS: return "Address Mask Request";
352 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
353 default: return "unknown ICMP type";
354 }
355}
356
Eric Andersen485b9551999-12-07 23:14:59 +0000357static void unpack(char *buf, int sz, struct sockaddr_in *from)
358{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 struct icmp *icmppkt;
360 struct iphdr *iphdr;
361 struct timeval tv, *tp;
362 int hlen, dupflag;
363 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000364
Erik Andersene49d5ec2000-02-08 19:58:47 +0000365 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000366
Erik Andersene49d5ec2000-02-08 19:58:47 +0000367 /* check IP header */
368 iphdr = (struct iphdr *) buf;
369 hlen = iphdr->ihl << 2;
370 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000371 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000372 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000373
Erik Andersene49d5ec2000-02-08 19:58:47 +0000374 sz -= hlen;
375 icmppkt = (struct icmp *) (buf + hlen);
376
Erik Andersen227a59b2000-04-25 23:24:55 +0000377 if (icmppkt->icmp_id != myid)
378 return; /* not our ping */
379
Erik Andersene49d5ec2000-02-08 19:58:47 +0000380 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Erik Andersen227a59b2000-04-25 23:24:55 +0000381 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000382 tp = (struct timeval *) icmppkt->icmp_data;
383
384 if ((tv.tv_usec -= tp->tv_usec) < 0) {
385 --tv.tv_sec;
386 tv.tv_usec += 1000000;
387 }
388 tv.tv_sec -= tp->tv_sec;
389
390 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
391 tsum += triptime;
392 if (triptime < tmin)
393 tmin = triptime;
394 if (triptime > tmax)
395 tmax = triptime;
396
397 if (TST(icmppkt->icmp_seq % MAX_DUP_CHK)) {
398 ++nrepeats;
399 --nreceived;
400 dupflag = 1;
401 } else {
402 SET(icmppkt->icmp_seq % MAX_DUP_CHK);
403 dupflag = 0;
404 }
405
406 if (options & O_QUIET)
407 return;
408
409 printf("%d bytes from %s: icmp_seq=%u", sz,
410 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
411 icmppkt->icmp_seq);
412 printf(" ttl=%d", iphdr->ttl);
413 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
414 if (dupflag)
415 printf(" (DUP!)");
416 printf("\n");
Erik Andersen227a59b2000-04-25 23:24:55 +0000417 } else
418 if (icmppkt->icmp_type != ICMP_ECHO)
Mark Whitleyf57c9442000-12-07 19:56:48 +0000419 error_msg("Warning: Got ICMP %d (%s)\n",
Erik Andersen227a59b2000-04-25 23:24:55 +0000420 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
Eric Andersen485b9551999-12-07 23:14:59 +0000421}
422
Pavel Roskin0024abc2000-06-07 20:38:15 +0000423static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000424{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000425 struct protoent *proto;
426 struct hostent *h;
427 char buf[MAXHOSTNAMELEN];
Pavel Roskin0024abc2000-06-07 20:38:15 +0000428 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000429 int sockopt;
430
431 proto = getprotobyname("icmp");
432 /* if getprotobyname failed, just silently force
433 * proto->p_proto to have the correct value for "icmp" */
434 if ((pingsock = socket(AF_INET, SOCK_RAW,
435 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
Matt Kraaia9819b22000-12-22 01:48:07 +0000436 if (errno == EPERM)
437 error_msg_and_die("permission denied. (are you root?)\n");
438 else
439 perror_msg_and_die("creating a raw socket");
Eric Andersen485b9551999-12-07 23:14:59 +0000440 }
Eric Andersen485b9551999-12-07 23:14:59 +0000441
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 /* drop root privs if running setuid */
443 setuid(getuid());
Eric Andersen485b9551999-12-07 23:14:59 +0000444
Erik Andersene49d5ec2000-02-08 19:58:47 +0000445 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000446
Erik Andersene49d5ec2000-02-08 19:58:47 +0000447 pingaddr.sin_family = AF_INET;
448 if (!(h = gethostbyname(host))) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000449 error_msg("unknown host %s\n", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000451 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000452
453 if (h->h_addrtype != AF_INET) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000454 error_msg("unknown address type; only AF_INET is currently supported.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 exit(1);
456 }
457
458 pingaddr.sin_family = AF_INET; /* h->h_addrtype */
459 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
460 strncpy(buf, h->h_name, sizeof(buf) - 1);
461 hostname = buf;
462
463 /* enable broadcast pings */
464 sockopt = 1;
465 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
466 sizeof(sockopt));
467
468 /* set recv buf for broadcast pings */
469 sockopt = 48 * 1024;
470 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
471 sizeof(sockopt));
472
473 printf("PING %s (%s): %d data bytes\n",
474 hostname,
475 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
Pavel Roskin0024abc2000-06-07 20:38:15 +0000476 datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000477
478 signal(SIGINT, pingstats);
479
480 /* start the ping's going ... */
481 sendping(0);
482
483 /* listen for replies */
484 while (1) {
485 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000486 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 int c;
488
489 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
490 (struct sockaddr *) &from, &fromlen)) < 0) {
491 if (errno == EINTR)
492 continue;
Matt Kraaia9819b22000-12-22 01:48:07 +0000493 perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 continue;
495 }
496 unpack(packet, c, &from);
497 if (pingcount > 0 && nreceived >= pingcount)
498 break;
499 }
500 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000501}
502
503extern int ping_main(int argc, char **argv)
504{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000505 char *thisarg;
Eric Andersen485b9551999-12-07 23:14:59 +0000506
Erik Andersene49d5ec2000-02-08 19:58:47 +0000507 argc--;
508 argv++;
509 options = 0;
510 /* Parse any options */
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000511 while (argc >= 1 && **argv == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000512 thisarg = *argv;
513 thisarg++;
514 switch (*thisarg) {
515 case 'q':
516 options |= O_QUIET;
517 break;
518 case 'c':
Pavel Roskin0024abc2000-06-07 20:38:15 +0000519 if (--argc <= 0)
520 usage(ping_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000521 argv++;
522 pingcount = atoi(*argv);
523 break;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000524 case 's':
525 if (--argc <= 0)
526 usage(ping_usage);
527 argv++;
528 datalen = atoi(*argv);
529 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000530 default:
531 usage(ping_usage);
532 }
533 argc--;
534 argv++;
535 }
536 if (argc < 1)
537 usage(ping_usage);
538
539 myid = getpid() & 0xFFFF;
540 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000541 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000542}
Eric Andersen03f4c272000-07-06 23:10:29 +0000543#endif /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen485b9551999-12-07 23:14:59 +0000544
545/*
546 * Copyright (c) 1989 The Regents of the University of California.
547 * All rights reserved.
548 *
549 * This code is derived from software contributed to Berkeley by
550 * Mike Muuss.
551 *
552 * Redistribution and use in source and binary forms, with or without
553 * modification, are permitted provided that the following conditions
554 * are met:
555 * 1. Redistributions of source code must retain the above copyright
556 * notice, this list of conditions and the following disclaimer.
557 * 2. Redistributions in binary form must reproduce the above copyright
558 * notice, this list of conditions and the following disclaimer in the
559 * documentation and/or other materials provided with the distribution.
Eric Andersen4e573f42000-11-14 23:29:24 +0000560 *
561 * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
562 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
563 *
Eric Andersen485b9551999-12-07 23:14:59 +0000564 * 4. Neither the name of the University nor the names of its contributors
565 * may be used to endorse or promote products derived from this software
566 * without specific prior written permission.
567 *
568 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
569 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
570 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
571 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
572 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
573 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
574 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
575 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
576 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
577 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
578 * SUCH DAMAGE.
579 */