blob: 658c015180dde346f32e4fa3f515db695f92ecb2 [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
Rob Landleybc68cd12006-03-10 19:22:06 +000034enum {
35 DEFDATALEN = 56,
36 MAXIPLEN = 60,
37 MAXICMPLEN = 76,
38 MAXPACKET = 65468,
39 MAX_DUP_CHK = (8 * 128),
40 MAXWAIT = 10,
41 PINGINTERVAL = 1 /* second */
42};
Eric Andersen485b9551999-12-07 23:14:59 +000043
Eric Andersene90e7412002-06-06 11:47:00 +000044static void ping(const char *host);
Pavel Roskin0024abc2000-06-07 20:38:15 +000045
Eric Andersen19db07b1999-12-11 08:41:28 +000046/* common routines */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000047
Eric Andersen485b9551999-12-07 23:14:59 +000048static int in_cksum(unsigned short *buf, int sz)
49{
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 int nleft = sz;
51 int sum = 0;
52 unsigned short *w = buf;
53 unsigned short ans = 0;
Eric Andersen485b9551999-12-07 23:14:59 +000054
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 while (nleft > 1) {
56 sum += *w++;
57 nleft -= 2;
58 }
Eric Andersen485b9551999-12-07 23:14:59 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 if (nleft == 1) {
61 *(unsigned char *) (&ans) = *(unsigned char *) w;
62 sum += ans;
63 }
Eric Andersen485b9551999-12-07 23:14:59 +000064
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 sum = (sum >> 16) + (sum & 0xFFFF);
66 sum += (sum >> 16);
67 ans = ~sum;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000068 return ans;
Erik Andersene49d5ec2000-02-08 19:58:47 +000069}
Eric Andersen485b9551999-12-07 23:14:59 +000070
Eric Andersenbdfd0d72001-10-24 05:00:29 +000071#ifndef CONFIG_FEATURE_FANCY_PING
Denis Vlasenko4a5cf162006-11-20 00:48:22 +000072
73/* simple version */
74
Denis Vlasenkocb6874c2006-09-02 16:13:36 +000075static char *hostname;
76
Eric Andersenb8886822002-03-21 14:04:43 +000077static void noresp(int ign)
Eric Andersenb5474c42002-03-20 11:59:28 +000078{
Eric Andersenb8886822002-03-21 14:04:43 +000079 printf("No response from %s\n", hostname);
Eric Andersen4e486a52003-01-12 06:08:33 +000080 exit(EXIT_FAILURE);
Eric Andersenb5474c42002-03-20 11:59:28 +000081}
Eric Andersen19db07b1999-12-11 08:41:28 +000082
Pavel Roskin0024abc2000-06-07 20:38:15 +000083static void ping(const char *host)
Eric Andersen19db07b1999-12-11 08:41:28 +000084{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 struct hostent *h;
86 struct sockaddr_in pingaddr;
87 struct icmp *pkt;
88 int pingsock, c;
89 char packet[DEFDATALEN + MAXIPLEN + MAXICMPLEN];
Eric Andersen19db07b1999-12-11 08:41:28 +000090
Matt Kraai06ef1652001-07-13 20:56:27 +000091 pingsock = create_icmp_socket();
Erik Andersene49d5ec2000-02-08 19:58:47 +000092
93 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
94
95 pingaddr.sin_family = AF_INET;
Matt Kraaic55b8d42001-05-16 15:40:51 +000096 h = xgethostbyname(host);
Erik Andersene49d5ec2000-02-08 19:58:47 +000097 memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
Eric Andersenb5474c42002-03-20 11:59:28 +000098 hostname = h->h_name;
Erik Andersene49d5ec2000-02-08 19:58:47 +000099
100 pkt = (struct icmp *) packet;
101 memset(pkt, 0, sizeof(packet));
102 pkt->icmp_type = ICMP_ECHO;
103 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
104
Rob Landley07a637d2006-04-01 17:28:11 +0000105 c = sendto(pingsock, packet, DEFDATALEN + ICMP_MINLEN, 0,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
107
Rob Landley9b1857f2006-05-31 23:54:50 +0000108 if (c < 0) {
109 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110 bb_perror_msg_and_die("sendto");
Rob Landley9b1857f2006-05-31 23:54:50 +0000111 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112
113 signal(SIGALRM, noresp);
114 alarm(5); /* give the host 5000ms to respond */
115 /* listen for replies */
116 while (1) {
117 struct sockaddr_in from;
Mike Frysinger03e827a2005-07-26 23:00:59 +0000118 socklen_t fromlen = sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119
120 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
121 (struct sockaddr *) &from, &fromlen)) < 0) {
122 if (errno == EINTR)
123 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000125 continue;
126 }
127 if (c >= 76) { /* ip + icmp */
128 struct iphdr *iphdr = (struct iphdr *) packet;
129
130 pkt = (struct icmp *) (packet + (iphdr->ihl << 2)); /* skip ip hdr */
131 if (pkt->icmp_type == ICMP_ECHOREPLY)
132 break;
133 }
134 }
Rob Landley9b1857f2006-05-31 23:54:50 +0000135 if (ENABLE_FEATURE_CLEAN_UP) close(pingsock);
Eric Andersenb8886822002-03-21 14:04:43 +0000136 printf("%s is alive!\n", hostname);
Pavel Roskin0024abc2000-06-07 20:38:15 +0000137 return;
Eric Andersen19db07b1999-12-11 08:41:28 +0000138}
139
Rob Landleydfba7412006-03-06 20:47:33 +0000140int ping_main(int argc, char **argv)
Eric Andersen19db07b1999-12-11 08:41:28 +0000141{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000142 argc--;
143 argv++;
144 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 ping(*argv);
Matt Kraai3e856ce2000-12-01 02:55:13 +0000147 return EXIT_SUCCESS;
Eric Andersen19db07b1999-12-11 08:41:28 +0000148}
149
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000150#else /* ! CONFIG_FEATURE_FANCY_PING */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000151
Eric Andersen19db07b1999-12-11 08:41:28 +0000152/* full(er) version */
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000153
154#define OPT_STRING "qc:s:I:"
155enum {
156 OPT_QUIET = 1 << 0,
157};
158
Eric Andersen19db07b1999-12-11 08:41:28 +0000159static struct sockaddr_in pingaddr;
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000160static struct sockaddr_in sourceaddr;
Eric Andersen19db07b1999-12-11 08:41:28 +0000161static int pingsock = -1;
Denis Vlasenko13858992006-10-08 12:49:22 +0000162static unsigned datalen; /* intentionally uninitialized to work around gcc bug */
Eric Andersen19db07b1999-12-11 08:41:28 +0000163
Denis Vlasenko13858992006-10-08 12:49:22 +0000164static unsigned long ntransmitted, nreceived, nrepeats, pingcount;
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000165static int myid;
Matt Kraai369da772002-02-01 16:54:00 +0000166static unsigned long tmin = ULONG_MAX, tmax, tsum;
Eric Andersen19db07b1999-12-11 08:41:28 +0000167static char rcvd_tbl[MAX_DUP_CHK / 8];
168
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000169static 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
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000175#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
176#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
177#define SET(bit) (A(bit) |= B(bit))
178#define CLR(bit) (A(bit) &= (~B(bit)))
179#define TST(bit) (A(bit) & B(bit))
180
Eric Andersen19db07b1999-12-11 08:41:28 +0000181/**************************************************************************/
182
Eric Andersenfad04fd2000-07-14 06:49:52 +0000183static void pingstats(int junk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184{
Matt Kraaib938e2f2000-09-20 04:33:30 +0000185 int status;
186
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 signal(SIGINT, SIG_IGN);
188
Matt Kraai369da772002-02-01 16:54:00 +0000189 printf("\n--- %s ping statistics ---\n", hostent->h_name);
Denis Vlasenko13858992006-10-08 12:49:22 +0000190 printf("%lu packets transmitted, ", ntransmitted);
191 printf("%lu packets received, ", nreceived);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 if (nrepeats)
Denis Vlasenko13858992006-10-08 12:49:22 +0000193 printf("%lu duplicates, ", nrepeats);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000194 if (ntransmitted)
Denis Vlasenko13858992006-10-08 12:49:22 +0000195 printf("%lu%% packet loss\n",
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 (ntransmitted - nreceived) * 100 / ntransmitted);
197 if (nreceived)
198 printf("round-trip min/avg/max = %lu.%lu/%lu.%lu/%lu.%lu ms\n",
199 tmin / 10, tmin % 10,
200 (tsum / (nreceived + nrepeats)) / 10,
201 (tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
Matt Kraaib938e2f2000-09-20 04:33:30 +0000202 if (nreceived != 0)
203 status = EXIT_SUCCESS;
204 else
205 status = EXIT_FAILURE;
206 exit(status);
Eric Andersen485b9551999-12-07 23:14:59 +0000207}
208
Eric Andersenfad04fd2000-07-14 06:49:52 +0000209static void sendping(int junk)
Eric Andersen485b9551999-12-07 23:14:59 +0000210{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 struct icmp *pkt;
212 int i;
Rob Landley07a637d2006-04-01 17:28:11 +0000213 char packet[datalen + ICMP_MINLEN];
Eric Andersen485b9551999-12-07 23:14:59 +0000214
Erik Andersene49d5ec2000-02-08 19:58:47 +0000215 pkt = (struct icmp *) packet;
Eric Andersen485b9551999-12-07 23:14:59 +0000216
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 pkt->icmp_type = ICMP_ECHO;
218 pkt->icmp_code = 0;
219 pkt->icmp_cksum = 0;
Denis Vlasenkoe0b7f712006-09-02 16:57:59 +0000220 pkt->icmp_seq = htons(ntransmitted++);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000221 pkt->icmp_id = myid;
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000222 CLR(ntohs(pkt->icmp_seq) % MAX_DUP_CHK);
Eric Andersen485b9551999-12-07 23:14:59 +0000223
Rob Landleybbf4e162006-01-11 03:44:11 +0000224 gettimeofday((struct timeval *) &pkt->icmp_dun, NULL);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 pkt->icmp_cksum = in_cksum((unsigned short *) pkt, sizeof(packet));
Eric Andersen485b9551999-12-07 23:14:59 +0000226
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227 i = sendto(pingsock, packet, sizeof(packet), 0,
228 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
229
Pavel Roskin0024abc2000-06-07 20:38:15 +0000230 if (i < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000231 bb_perror_msg_and_die("sendto");
Eric Andersenfad04fd2000-07-14 06:49:52 +0000232 else if ((size_t)i != sizeof(packet))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
Pavel Roskin0024abc2000-06-07 20:38:15 +0000234 (int)sizeof(packet));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235
236 signal(SIGALRM, sendping);
237 if (pingcount == 0 || ntransmitted < pingcount) { /* schedule next in 1s */
238 alarm(PINGINTERVAL);
239 } else { /* done, wait for the last ping to come back */
240 /* todo, don't necessarily need to wait so long... */
241 signal(SIGALRM, pingstats);
242 alarm(MAXWAIT);
243 }
Eric Andersen485b9551999-12-07 23:14:59 +0000244}
Erik Andersene49d5ec2000-02-08 19:58:47 +0000245
Denis Vlasenkoe0b7f712006-09-02 16:57:59 +0000246static char *icmp_type_name(int id)
Erik Andersen227a59b2000-04-25 23:24:55 +0000247{
248 switch (id) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000249 case ICMP_ECHOREPLY: return "Echo Reply";
250 case ICMP_DEST_UNREACH: return "Destination Unreachable";
251 case ICMP_SOURCE_QUENCH: return "Source Quench";
252 case ICMP_REDIRECT: return "Redirect (change route)";
253 case ICMP_ECHO: return "Echo Request";
254 case ICMP_TIME_EXCEEDED: return "Time Exceeded";
255 case ICMP_PARAMETERPROB: return "Parameter Problem";
256 case ICMP_TIMESTAMP: return "Timestamp Request";
257 case ICMP_TIMESTAMPREPLY: return "Timestamp Reply";
258 case ICMP_INFO_REQUEST: return "Information Request";
259 case ICMP_INFO_REPLY: return "Information Reply";
260 case ICMP_ADDRESS: return "Address Mask Request";
261 case ICMP_ADDRESSREPLY: return "Address Mask Reply";
262 default: return "unknown ICMP type";
Erik Andersen227a59b2000-04-25 23:24:55 +0000263 }
264}
265
Eric Andersen485b9551999-12-07 23:14:59 +0000266static void unpack(char *buf, int sz, struct sockaddr_in *from)
267{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000268 struct icmp *icmppkt;
269 struct iphdr *iphdr;
270 struct timeval tv, *tp;
271 int hlen, dupflag;
272 unsigned long triptime;
Eric Andersen485b9551999-12-07 23:14:59 +0000273
Erik Andersene49d5ec2000-02-08 19:58:47 +0000274 gettimeofday(&tv, NULL);
Eric Andersen485b9551999-12-07 23:14:59 +0000275
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 /* discard if too short */
Pavel Roskin0024abc2000-06-07 20:38:15 +0000277 if (sz < (datalen + ICMP_MINLEN))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 return;
Eric Andersen485b9551999-12-07 23:14:59 +0000279
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000280 /* check IP header */
281 iphdr = (struct iphdr *) buf;
282 hlen = iphdr->ihl << 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000283 sz -= hlen;
284 icmppkt = (struct icmp *) (buf + hlen);
Erik Andersen227a59b2000-04-25 23:24:55 +0000285 if (icmppkt->icmp_id != myid)
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000286 return; /* not our ping */
Erik Andersen227a59b2000-04-25 23:24:55 +0000287
Erik Andersene49d5ec2000-02-08 19:58:47 +0000288 if (icmppkt->icmp_type == ICMP_ECHOREPLY) {
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000289 u_int16_t recv_seq = ntohs(icmppkt->icmp_seq);
Denis Vlasenkocb6874c2006-09-02 16:13:36 +0000290 ++nreceived;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000291 tp = (struct timeval *) icmppkt->icmp_data;
292
293 if ((tv.tv_usec -= tp->tv_usec) < 0) {
294 --tv.tv_sec;
295 tv.tv_usec += 1000000;
296 }
297 tv.tv_sec -= tp->tv_sec;
298
299 triptime = tv.tv_sec * 10000 + (tv.tv_usec / 100);
300 tsum += triptime;
301 if (triptime < tmin)
302 tmin = triptime;
303 if (triptime > tmax)
304 tmax = triptime;
305
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000306 if (TST(recv_seq % MAX_DUP_CHK)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000307 ++nrepeats;
308 --nreceived;
309 dupflag = 1;
310 } else {
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000311 SET(recv_seq % MAX_DUP_CHK);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 dupflag = 0;
313 }
314
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000315 if (option_mask32 & OPT_QUIET)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000316 return;
317
318 printf("%d bytes from %s: icmp_seq=%u", sz,
319 inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
Mike Frysinger887a1ad2005-09-15 01:32:48 +0000320 recv_seq);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 printf(" ttl=%d", iphdr->ttl);
322 printf(" time=%lu.%lu ms", triptime / 10, triptime % 10);
323 if (dupflag)
324 printf(" (DUP!)");
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000325 puts("");
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000326 } else
Erik Andersen227a59b2000-04-25 23:24:55 +0000327 if (icmppkt->icmp_type != ICMP_ECHO)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000328 bb_error_msg("warning: got ICMP %d (%s)",
Denis Vlasenkoe0b7f712006-09-02 16:57:59 +0000329 icmppkt->icmp_type, icmp_type_name(icmppkt->icmp_type));
Rob Landley42eddba2005-12-15 08:04:17 +0000330 fflush(stdout);
Eric Andersen485b9551999-12-07 23:14:59 +0000331}
332
Eric Andersene90e7412002-06-06 11:47:00 +0000333static void ping(const char *host)
Eric Andersen485b9551999-12-07 23:14:59 +0000334{
Pavel Roskin0024abc2000-06-07 20:38:15 +0000335 char packet[datalen + MAXIPLEN + MAXICMPLEN];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000336 int sockopt;
337
Matt Kraai06ef1652001-07-13 20:56:27 +0000338 pingsock = create_icmp_socket();
Eric Andersen485b9551999-12-07 23:14:59 +0000339
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000340 if (sourceaddr.sin_addr.s_addr) {
Denis Vlasenkobcf49082006-09-02 17:53:16 +0000341 xbind(pingsock, (struct sockaddr*)&sourceaddr, sizeof(sourceaddr));
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000342 }
343
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 memset(&pingaddr, 0, sizeof(struct sockaddr_in));
Eric Andersen19db07b1999-12-11 08:41:28 +0000345
Erik Andersene49d5ec2000-02-08 19:58:47 +0000346 pingaddr.sin_family = AF_INET;
Matt Kraai369da772002-02-01 16:54:00 +0000347 hostent = xgethostbyname(host);
348 if (hostent->h_addrtype != AF_INET)
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000349 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350
Matt Kraai369da772002-02-01 16:54:00 +0000351 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000352
353 /* enable broadcast pings */
Denis Vlasenko48237b02006-11-22 23:22:06 +0000354 setsockopt_broadcast(pingsock);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355
356 /* set recv buf for broadcast pings */
357 sockopt = 48 * 1024;
358 setsockopt(pingsock, SOL_SOCKET, SO_RCVBUF, (char *) &sockopt,
359 sizeof(sockopt));
360
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000361 printf("PING %s (%s)",
362 hostent->h_name,
363 inet_ntoa(*(struct in_addr *) &pingaddr.sin_addr.s_addr));
364 if (sourceaddr.sin_addr.s_addr) {
365 printf(" from %s",
366 inet_ntoa(*(struct in_addr *) &sourceaddr.sin_addr.s_addr));
367 }
368 printf(": %d data bytes\n", datalen);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000369
370 signal(SIGINT, pingstats);
371
372 /* start the ping's going ... */
373 sendping(0);
374
375 /* listen for replies */
376 while (1) {
377 struct sockaddr_in from;
Erik Andersen1d1d9502000-04-21 01:26:49 +0000378 socklen_t fromlen = (socklen_t) sizeof(from);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 int c;
380
381 if ((c = recvfrom(pingsock, packet, sizeof(packet), 0,
382 (struct sockaddr *) &from, &fromlen)) < 0) {
383 if (errno == EINTR)
384 continue;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000385 bb_perror_msg("recvfrom");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000386 continue;
387 }
388 unpack(packet, c, &from);
389 if (pingcount > 0 && nreceived >= pingcount)
390 break;
391 }
392 pingstats(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000393}
394
Denis Vlasenko2cbe6e62006-09-02 16:17:30 +0000395/* TODO: consolidate ether-wake.c, dnsd.c, ifupdown.c, nslookup.c
396 * versions of below thing. BTW we have far too many "%u.%u.%u.%u" too...
397*/
398static int parse_nipquad(const char *str, struct sockaddr_in* addr)
399{
400 char dummy;
401 unsigned i1, i2, i3, i4;
402 if (sscanf(str, "%u.%u.%u.%u%c",
403 &i1, &i2, &i3, &i4, &dummy) == 4
404 && ( (i1|i2|i3|i4) <= 0xff )
405 ) {
406 uint8_t* ptr = (uint8_t*)&addr->sin_addr;
407 ptr[0] = i1;
408 ptr[1] = i2;
409 ptr[2] = i3;
410 ptr[3] = i4;
411 return 0;
412 }
413 return 1; /* error */
414}
415
Rob Landleydfba7412006-03-06 20:47:33 +0000416int ping_main(int argc, char **argv)
Eric Andersen485b9551999-12-07 23:14:59 +0000417{
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000418 char *opt_c, *opt_s, *opt_I;
Eric Andersen485b9551999-12-07 23:14:59 +0000419
Mark Whitley59ab0252001-01-23 22:30:04 +0000420 datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
421
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000422 /* exactly one argument needed */
423 opt_complementary = "=1";
424 getopt32(argc, argv, OPT_STRING, &opt_c, &opt_s, &opt_I);
425 if (option_mask32 & 2) pingcount = xatoul(opt_c); // -c
426 if (option_mask32 & 4) datalen = xatou16(opt_s); // -s
427 if (option_mask32 & 8) { // -I
428/* TODO: ping6 accepts iface too:
429 if_index = if_nametoindex(*argv);
430 if (!if_index) ...
431make it true for ping. */
432 if (parse_nipquad(opt_I, &sourceaddr))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000433 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000434 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000435
Denis Vlasenkodb7f2e52006-09-02 16:16:23 +0000436 myid = (int16_t) getpid();
Denis Vlasenko4a5cf162006-11-20 00:48:22 +0000437 ping(argv[optind]);
Eric Andersene90e7412002-06-06 11:47:00 +0000438 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +0000439}
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000440#endif /* ! CONFIG_FEATURE_FANCY_PING */