blob: 5cd6f4a1a56e75a042a460ec53b1893edd449c8a [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 Andersenc7bda1c2004-03-15 08:29:22 +00003 * $Id: ping.c,v 1.56 2004/03/15 08:28:48 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 *
Rob Landley42eddba2005-12-15 08:04:17 +00008 * Adapted from the ping in netkit-base 0.10:
Eric Andersen485b9551999-12-07 23:14:59 +00009 * Copyright (c) 1989 The Regents of the University of California.
Rob Landley42eddba2005-12-15 08:04:17 +000010 * Derived from software contributed to Berkeley by Mike Muuss.
Eric Andersen485b9551999-12-07 23:14:59 +000011 *
Rob Landley42eddba2005-12-15 08:04:17 +000012 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen485b9551999-12-07 23:14:59 +000013 */
14
Eric Andersen485b9551999-12-07 23:14:59 +000015#include <sys/param.h>
16#include <sys/socket.h>
17#include <sys/file.h>
Eric Andersen485b9551999-12-07 23:14:59 +000018#include <sys/times.h>
Mike Frysinger06adf5f2006-03-22 00:25:07 +000019#include <signal.h>
Eric Andersen485b9551999-12-07 23:14:59 +000020
21#include <netinet/in.h>
22#include <netinet/ip.h>
23#include <netinet/ip_icmp.h>
24#include <arpa/inet.h>
25#include <netdb.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <unistd.h>
30#include <string.h>
31#include <stdlib.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000033
Eric Andersen9ca57d32000-06-19 18:51:53 +000034
Rob Landleybc68cd12006-03-10 19:22:06 +000035enum {
36 DEFDATALEN = 56,
37 MAXIPLEN = 60,
38 MAXICMPLEN = 76,
39 MAXPACKET = 65468,
40 MAX_DUP_CHK = (8 * 128),
41 MAXWAIT = 10,
42 PINGINTERVAL = 1 /* second */
43};
Eric Andersen485b9551999-12-07 23:14:59 +000044
45#define O_QUIET (1 << 0)
46
47#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
48#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
49#define SET(bit) (A(bit) |= B(bit))
50#define CLR(bit) (A(bit) &= (~B(bit)))
51#define TST(bit) (A(bit) & B(bit))
52
Eric Andersene90e7412002-06-06 11:47:00 +000053static void ping(const char *host);
Pavel Roskin0024abc2000-06-07 20:38:15 +000054
Eric Andersen19db07b1999-12-11 08:41:28 +000055/* common routines */
Eric Andersen485b9551999-12-07 23:14:59 +000056static int in_cksum(unsigned short *buf, int sz)
57{
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 int nleft = sz;
59 int sum = 0;
60 unsigned short *w = buf;
61 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +000062
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 while (nleft > 1) {
64 sum += *w++;
65 nleft -= 2;
66 }
Eric Andersen485b9551999-12-07 23:14:59 +000067
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 if (nleft == 1) {
69 *(unsigned char *) (&ans) = *(unsigned char *) w;
70 sum += ans;
71 }
Eric Andersen485b9551999-12-07 23:14:59 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 sum = (sum >> 16) + (sum & 0xFFFF);
74 sum += (sum >> 16);
75 ans = ~sum;
76 return (ans);
77}
Eric Andersen485b9551999-12-07 23:14:59 +000078
Eric Andersen19db07b1999-12-11 08:41:28 +000079/* simple version */
Eric Andersenbdfd0d72001-10-24 05:00:29 +000080#ifndef CONFIG_FEATURE_FANCY_PING
Eric Andersenb5474c42002-03-20 11:59:28 +000081static char *hostname = NULL;
Eric Andersenb8886822002-03-21 14:04:43 +000082static void noresp(int ign)
Eric Andersenb5474c42002-03-20 11:59:28 +000083{
Eric Andersenb8886822002-03-21 14:04:43 +000084 printf("No response from %s\n", hostname);
Eric Andersen4e486a52003-01-12 06:08:33 +000085 exit(EXIT_FAILURE);
Eric Andersenb5474c42002-03-20 11:59:28 +000086}
Eric Andersen19db07b1999-12-11 08:41:28 +000087
Pavel Roskin0024abc2000-06-07 20:38:15 +000088static void ping(const char *host)
Eric Andersen19db07b1999-12-11 08:41:28 +000089{
Erik Andersene49d5ec2000-02-08 19:58:47 +000090 struct hostent *h;
91 struct sockaddr_in pingaddr;
92 struct icmp *pkt;
93 int pingsock, c;
94 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +000095
Matt Kraai06ef1652001-07-13 20:56:27 +000096 pingsock = create_icmp_socket();
Erik Andersene49d5ec2000-02-08 19:58:47 +000097
98 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
99
100 pingaddr.sin_family = AF_INET;
Matt Kraaic55b8d42001-05-16 15:40:51 +0000101 h = xgethostbyname(host);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
Eric Andersenb5474c42002-03-20 11:59:28 +0000103 hostname = h->h_name;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104
105 pkt = (struct icmp *) packet;
106 memset(pkt, 0, sizeof(packet));
107 pkt->icmp_type = ICMP_ECHO;
108 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
109
Rob Landley07a637d2006-04-01 17:28:11 +0000110 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
112
Rob Landley9b1857f2006-05-31 23:54:50 +0000113 if (c < 0) {
114 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000115 bb_perror_msg_and_die("sendto");
Rob Landley9b1857f2006-05-31 23:54:50 +0000116 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000117
118 signal(SIGALRM, noresp);
119 alarm(5); /* give the host 5000ms to respond */
120 /* listen for replies */
121 while (1) {
122 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000123 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124
125 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
126 (struct sockaddr *) &from, &fromlen)) < 0) {
127 if (errno == EINTR)
128 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000129 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000130 continue;
131 }
132 if (c >= 76) { /* ip + icmp */
133 struct iphdr *iphdr = (struct iphdr *) packet;
134
135 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
136 if (pkt->icmp_type == ICMP_ECHOREPLY)
137 break;
138 }
139 }
Rob Landley9b1857f2006-05-31 23:54:50 +0000140 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Eric Andersenb8886822002-03-21 14:04:43 +0000141 printf("%s is alive!\n", hostname);
Pavel Roskin0024abc2000-06-07 20:38:15 +0000142 return;
Eric Andersen19db07b1999-12-11 08:41:28 +0000143}
144
Rob Landleydfba7412006-03-06 20:47:33 +0000145int ping_main(int argc, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000146{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 argc--;
148 argv++;
149 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000150 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000151 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000152 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000153}
154
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000155#else /* ! CONFIG_FEATURE_FANCY_PING */
Eric Andersen19db07b1999-12-11 08:41:28 +0000156/* full(er) version */
Eric Andersen19db07b1999-12-11 08:41:28 +0000157static struct sockaddr_in pingaddr;
158static int pingsock = -1;
Mark Whitley59ab0252001-01-23 22:30:04 +0000159static int datalen; /* intentionally uninitialized to work around gcc bug */
Eric Andersen19db07b1999-12-11 08:41:28 +0000160
Matt Kraai369da772002-02-01 16:54:00 +0000161static long ntransmitted, nreceived, nrepeats, pingcount;
162static int myid, options;
163static unsigned long tmin = ULONG_MAX, tmax, tsum;
Eric Andersen19db07b1999-12-11 08:41:28 +0000164static char rcvd_tbl[MAX_DUP_CHK / 8];
165
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000166#ifndef CONFIG_FEATURE_FANCY_PING6
167static
168#endif
169 struct hostent *hostent;
Matt Kraai369da772002-02-01 16:54:00 +0000170
Eric Andersen19db07b1999-12-11 08:41:28 +0000171static void sendping(int);
172static void pingstats(int);
173static void unpack(char *, int, struct sockaddr_in *);
174
Eric Andersen19db07b1999-12-11 08:41:28 +0000175/**************************************************************************/
176
Eric Andersenfad04fd2000-07-14 06:49:52 +0000177static void pingstats(int junk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000178{
Matt Kraaib938e2f2000-09-20 04:33:30 +0000179 int status;
180
Erik Andersene49d5ec2000-02-08 19:58:47 +0000181 signal(SIGINT, SIG_IGN);
182
Matt Kraai369da772002-02-01 16:54:00 +0000183 printf("\n--- %s ping statistics ---\n", hostent->h_name);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 printf("%ld packets transmitted, ", ntransmitted);
185 printf("%ld packets received, ", nreceived);
186 if (nrepeats)
187 printf("%ld duplicates, ", nrepeats);
188 if (ntransmitted)
189 printf("%ld%% packet loss\n",
190 (ntransmitted - nreceived) * 100 / ntransmitted);
191 if (nreceived)
192 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
193 tmin / 10, tmin % 10,
194 (tsum / (nreceived + nrepeats)) / 10,
195 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Matt Kraaib938e2f2000-09-20 04:33:30 +0000196 if (nreceived != 0)
197 status = EXIT_SUCCESS;
198 else
199 status = EXIT_FAILURE;
200 exit(status);
Eric Andersen485b9551999-12-07 23:14:59 +0000201}
202
Eric Andersenfad04fd2000-07-14 06:49:52 +0000203static void sendping(int junk)
Eric Andersen485b9551999-12-07 23:14:59 +0000204{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 struct icmp *pkt;
206 int i;
Rob Landley07a637d2006-04-01 17:28:11 +0000207 char packet[datalen + ICMP_MINLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000208
Erik Andersene49d5ec2000-02-08 19:58:47 +0000209 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000210
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 pkt->icmp_type = ICMP_ECHO;
212 pkt->icmp_code = 0;
213 pkt->icmp_cksum = 0;
Rob Landleycae61e72006-08-01 00:30:26 +0000214 pkt->icmp_seq = SWAP_BE16(ntransmitted++);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 pkt->icmp_id = myid;
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000216 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
Eric Andersen485b9551999-12-07 23:14:59 +0000217
Rob Landleybbf4e162006-01-11 03:44:11 +0000218 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000219 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000220
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 i = sendto(pingsock, packet, sizeof(packet), 0,
222 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
223
Pavel Roskin0024abc2000-06-07 20:38:15 +0000224 if (i < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000225 bb_perror_msg_and_die("sendto");
Eric Andersenfad04fd2000-07-14 06:49:52 +0000226 else if ((size_t)i != sizeof(packet))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
Pavel Roskin0024abc2000-06-07 20:38:15 +0000228 (int)sizeof(packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229
230 signal(SIGALRM, sendping);
231 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
232 alarm(PINGINTERVAL);
233 } else { /* done, wait for the last ping to come back */
234 /* todo, don't necessarily need to wait so long... */
235 signal(SIGALRM, pingstats);
236 alarm(MAXWAIT);
237 }
Eric Andersen485b9551999-12-07 23:14:59 +0000238}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239
Erik Andersen227a59b2000-04-25 23:24:55 +0000240static char *icmp_type_name (int id)
241{
242 switch (id) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000243 case ICMP_ECHOREPLY: return "Echo Reply";
244 case ICMP_DEST_UNREACH: return "Destination Unreachable";
245 case ICMP_SOURCE_QUENCH: return "Source Quench";
246 case ICMP_REDIRECT: return "Redirect (change route)";
247 case ICMP_ECHO: return "Echo Request";
248 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
249 case ICMP_PARAMETERPROB: return "Parameter Problem";
250 case ICMP_TIMESTAMP: return "Timestamp Request";
251 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
252 case ICMP_INFO_REQUEST: return "Information Request";
253 case ICMP_INFO_REPLY: return "Information Reply";
254 case ICMP_ADDRESS: return "Address Mask Request";
255 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
256 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000257 }
258}
259
Eric Andersen485b9551999-12-07 23:14:59 +0000260static void unpack(char *buf, int sz, struct sockaddr_in *from)
261{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262 struct icmp *icmppkt;
263 struct iphdr *iphdr;
264 struct timeval tv, *tp;
265 int hlen, dupflag;
266 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000267
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000269
Erik Andersene49d5ec2000-02-08 19:58:47 +0000270 /* check IP header */
271 iphdr = (struct iphdr *) buf;
272 hlen = iphdr->ihl << 2;
273 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000274 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000275 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000276
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 sz -= hlen;
278 icmppkt = (struct icmp *) (buf + hlen);
279
Erik Andersen227a59b2000-04-25 23:24:55 +0000280 if (icmppkt->icmp_id != myid)
281 return; /* not our ping */
282
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000284 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
Erik Andersen227a59b2000-04-25 23:24:55 +0000285 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 tp = (struct timeval *) icmppkt->icmp_data;
287
288 if ((tv.tv_usec -= tp->tv_usec) < 0) {
289 --tv.tv_sec;
290 tv.tv_usec += 1000000;
291 }
292 tv.tv_sec -= tp->tv_sec;
293
294 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
295 tsum += triptime;
296 if (triptime < tmin)
297 tmin = triptime;
298 if (triptime > tmax)
299 tmax = triptime;
300
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000301 if (TST(recv_seq % MAX_DUP_CHK)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000302 ++nrepeats;
303 --nreceived;
304 dupflag = 1;
305 } else {
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000306 SET(recv_seq % MAX_DUP_CHK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 dupflag = 0;
308 }
309
310 if (options & O_QUIET)
311 return;
312
313 printf("%d bytes from %s: icmp_seq=%u", sz,
314 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000315 recv_seq);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 printf(" ttl=%d", iphdr->ttl);
317 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
318 if (dupflag)
319 printf(" (DUP!)");
320 printf("\n");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000321 } else
Erik Andersen227a59b2000-04-25 23:24:55 +0000322 if (icmppkt->icmp_type != ICMP_ECHO)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000323 bb_error_msg("Warning: Got ICMP %d (%s)",
Erik Andersen227a59b2000-04-25 23:24:55 +0000324 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
Rob Landley42eddba2005-12-15 08:04:17 +0000325 fflush(stdout);
Eric Andersen485b9551999-12-07 23:14:59 +0000326}
327
Eric Andersene90e7412002-06-06 11:47:00 +0000328static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000329{
Pavel Roskin0024abc2000-06-07 20:38:15 +0000330 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000331 int sockopt;
332
Matt Kraai06ef1652001-07-13 20:56:27 +0000333 pingsock = create_icmp_socket();
Eric Andersen485b9551999-12-07 23:14:59 +0000334
Erik Andersene49d5ec2000-02-08 19:58:47 +0000335 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000336
Erik Andersene49d5ec2000-02-08 19:58:47 +0000337 pingaddr.sin_family = AF_INET;
Matt Kraai369da772002-02-01 16:54:00 +0000338 hostent = xgethostbyname(host);
339 if (hostent->h_addrtype != AF_INET)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000340 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341
Matt Kraai369da772002-02-01 16:54:00 +0000342 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000343
344 /* enable broadcast pings */
345 sockopt = 1;
346 setsockopt(pingsock, SOL_SOCKET, SO_BROADCAST, (char *) &sockopt,
347 sizeof(sockopt));
348
349 /* set recv buf for broadcast pings */
350 sockopt = 48 * 1024;
351 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
352 sizeof(sockopt));
353
354 printf("PING %s (%s): %d data bytes\n",
Matt Kraai369da772002-02-01 16:54:00 +0000355 hostent->h_name,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000356 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr),
Pavel Roskin0024abc2000-06-07 20:38:15 +0000357 datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000358
359 signal(SIGINT, pingstats);
360
361 /* start the ping's going ... */
362 sendping(0);
363
364 /* listen for replies */
365 while (1) {
366 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000367 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000368 int c;
369
370 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
371 (struct sockaddr *) &from, &fromlen)) < 0) {
372 if (errno == EINTR)
373 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000374 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000375 continue;
376 }
377 unpack(packet, c, &from);
378 if (pingcount > 0 && nreceived >= pingcount)
379 break;
380 }
381 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000382}
383
Rob Landleydfba7412006-03-06 20:47:33 +0000384int ping_main(int argc, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000385{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 char *thisarg;
Eric Andersen485b9551999-12-07 23:14:59 +0000387
Mark Whitley59ab0252001-01-23 22:30:04 +0000388 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
389
Erik Andersene49d5ec2000-02-08 19:58:47 +0000390 argc--;
391 argv++;
392 options = 0;
393 /* Parse any options */
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000394 while (argc >= 1 && **argv == '-') {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000395 thisarg = *argv;
396 thisarg++;
397 switch (*thisarg) {
398 case 'q':
399 options |= O_QUIET;
400 break;
401 case 'c':
Pavel Roskin0024abc2000-06-07 20:38:15 +0000402 if (--argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000403 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 argv++;
405 pingcount = atoi(*argv);
406 break;
Pavel Roskin0024abc2000-06-07 20:38:15 +0000407 case 's':
408 if (--argc <= 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000409 bb_show_usage();
Pavel Roskin0024abc2000-06-07 20:38:15 +0000410 argv++;
411 datalen = atoi(*argv);
412 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000413 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000414 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000415 }
416 argc--;
417 argv++;
418 }
419 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000420 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421
422 myid = getpid() & 0xFFFF;
Eric Andersene90e7412002-06-06 11:47:00 +0000423 ping(*argv);
424 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000425}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000426#endif /* ! CONFIG_FEATURE_FANCY_PING */