blob: b2b4880edd47be11a7b5a16d971ff04fdc46d06f [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 Andersenfad04fd2000-07-14 06:49:52 +00003 * $Id: ping.c,v 1.22 2000/07/14 06:49:52 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 */
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 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))) {
Matt Kraaid537a952000-07-14 01:51:25 +0000211 errorMsg("unknown host %s\n", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 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");
Matt Kraaid537a952000-07-14 01:51:25 +0000228 errorMsg("write incomplete\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 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
Eric Andersenfad04fd2000-07-14 06:49:52 +0000297static void pingstats(int junk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000298{
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
Eric Andersenfad04fd2000-07-14 06:49:52 +0000317static void sendping(int junk)
Eric Andersen485b9551999-12-07 23:14:59 +0000318{
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)
Matt Kraaibe84cd42000-07-12 17:02:35 +0000339 fatalError("sendto: %s\n", strerror(errno));
Eric Andersenfad04fd2000-07-14 06:49:52 +0000340 else if ((size_t)i != sizeof(packet))
Pavel Roskin0024abc2000-06-07 20:38:15 +0000341 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)
Matt Kraaid537a952000-07-14 01:51:25 +0000436 errorMsg("Warning: Got ICMP %d (%s)\n",
Erik Andersen227a59b2000-04-25 23:24:55 +0000437 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
Eric Andersen485b9551999-12-07 23:14:59 +0000438}
439
Pavel Roskin0024abc2000-06-07 20:38:15 +0000440static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000441{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000442 struct protoent *proto;
443 struct hostent *h;
444 char buf[MAXHOSTNAMELEN];
Pavel Roskin0024abc2000-06-07 20:38:15 +0000445 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000446 int sockopt;
447
448 proto = getprotobyname("icmp");
449 /* if getprotobyname failed, just silently force
450 * proto->p_proto to have the correct value for "icmp" */
451 if ((pingsock = socket(AF_INET, SOCK_RAW,
452 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
453 if (errno == EPERM) {
Matt Kraaid537a952000-07-14 01:51:25 +0000454 errorMsg("permission denied. (are you root?)\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000455 } else {
Pavel Roskin0024abc2000-06-07 20:38:15 +0000456 perror("ping: creating a raw socket");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457 }
458 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000459 }
Eric Andersen485b9551999-12-07 23:14:59 +0000460
Erik Andersene49d5ec2000-02-08 19:58:47 +0000461 /* drop root privs if running setuid */
462 setuid(getuid());
Eric Andersen485b9551999-12-07 23:14:59 +0000463
Erik Andersene49d5ec2000-02-08 19:58:47 +0000464 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000465
Erik Andersene49d5ec2000-02-08 19:58:47 +0000466 pingaddr.sin_family = AF_INET;
467 if (!(h = gethostbyname(host))) {
Matt Kraaid537a952000-07-14 01:51:25 +0000468 errorMsg("unknown host %s\n", host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000469 exit(1);
Eric Andersen485b9551999-12-07 23:14:59 +0000470 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471
472 if (h->h_addrtype != AF_INET) {
Matt Kraaid537a952000-07-14 01:51:25 +0000473 errorMsg("unknown address type; only AF_INET is currently supported.\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000474 exit(1);
475 }
476
477 pingaddr.sin_family = AF_INET; /* h->h_addrtype */
478 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
479 strncpy(buf, h->h_name, sizeof(buf) - 1);
480 hostname = buf;
481
482 /* enable broadcast pings */
483 sockopt = 1;
484 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
485 sizeof(sockopt));
486
487 /* set recv buf for broadcast pings */
488 sockopt = 48 * 1024;
489 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
490 sizeof(sockopt));
491
492 printf("PING %s (%s): %d data bytes\n",
493 hostname,
494 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
Pavel Roskin0024abc2000-06-07 20:38:15 +0000495 datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496
497 signal(SIGINT, pingstats);
498
499 /* start the ping's going ... */
500 sendping(0);
501
502 /* listen for replies */
503 while (1) {
504 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000505 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 int c;
507
508 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
509 (struct sockaddr *) &from, &fromlen)) < 0) {
510 if (errno == EINTR)
511 continue;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000512 perror("ping: recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000513 continue;
514 }
515 unpack(packet, c, &from);
516 if (pingcount > 0 && nreceived >= pingcount)
517 break;
518 }
519 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000520}
521
522extern int ping_main(int argc, char **argv)
523{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000524 char *thisarg;
Eric Andersen485b9551999-12-07 23:14:59 +0000525
Erik Andersene49d5ec2000-02-08 19:58:47 +0000526 argc--;
527 argv++;
528 options = 0;
529 /* Parse any options */
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000530 while (argc >= 1 && **argv == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000531 thisarg = *argv;
532 thisarg++;
533 switch (*thisarg) {
534 case 'q':
535 options |= O_QUIET;
536 break;
537 case 'c':
Pavel Roskin0024abc2000-06-07 20:38:15 +0000538 if (--argc <= 0)
539 usage(ping_usage);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 argv++;
541 pingcount = atoi(*argv);
542 break;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000543 case 's':
544 if (--argc <= 0)
545 usage(ping_usage);
546 argv++;
547 datalen = atoi(*argv);
548 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 default:
550 usage(ping_usage);
551 }
552 argc--;
553 argv++;
554 }
555 if (argc < 1)
556 usage(ping_usage);
557
558 myid = getpid() & 0xFFFF;
559 ping(*argv);
Eric Andersen9ca57d32000-06-19 18:51:53 +0000560 return(TRUE);
Eric Andersen485b9551999-12-07 23:14:59 +0000561}
Eric Andersen03f4c272000-07-06 23:10:29 +0000562#endif /* ! BB_FEATURE_SIMPLE_PING */
Eric Andersen485b9551999-12-07 23:14:59 +0000563
564/*
565 * Copyright (c) 1989 The Regents of the University of California.
566 * All rights reserved.
567 *
568 * This code is derived from software contributed to Berkeley by
569 * Mike Muuss.
570 *
571 * Redistribution and use in source and binary forms, with or without
572 * modification, are permitted provided that the following conditions
573 * are met:
574 * 1. Redistributions of source code must retain the above copyright
575 * notice, this list of conditions and the following disclaimer.
576 * 2. Redistributions in binary form must reproduce the above copyright
577 * notice, this list of conditions and the following disclaimer in the
578 * documentation and/or other materials provided with the distribution.
579 * 3. All advertising materials mentioning features or use of this software
580 * must display the following acknowledgement:
581 * This product includes software developed by the University of
582 * California, Berkeley and its contributors.
583 * 4. Neither the name of the University nor the names of its contributors
584 * may be used to endorse or promote products derived from this software
585 * without specific prior written permission.
586 *
587 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
588 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
589 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
590 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
591 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
592 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
593 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
594 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
595 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
596 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
597 * SUCH DAMAGE.
598 */