/* vi: set sw=4 ts=4: */
/*
- * $Id: ping.c,v 1.17 2000/06/19 18:51:53 andersen Exp $
+ * $Id: ping.c,v 1.26 2000/11/14 23:29:24 andersen Exp $
* Mini ping implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
* Original copyright notice is retained at the end of this file.
*/
-#include "internal.h"
+#include "busybox.h"
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/file.h>
/* It turns out that libc5 doesn't have proper icmp support
* built into it header files, so we have to supplement it */
-#if __GNU_LIBRARY__ < 5
+#if ! defined __GLIBC__ && ! defined __UCLIBC__
typedef unsigned int socklen_t;
#define ICMP_MINLEN 8 /* abs minimum */
}
/* simple version */
-#ifdef BB_SIMPLE_PING
-static const char *ping_usage = "ping host\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
- "\nSend ICMP ECHO_REQUEST packets to network hosts\n"
-#endif
- ;
-
+#ifdef BB_FEATURE_SIMPLE_PING
static char *hostname = NULL;
static void noresp(int ign)
pingaddr.sin_family = AF_INET;
if (!(h = gethostbyname(host))) {
- fprintf(stderr, "ping: unknown host %s\n", host);
+ errorMsg("unknown host %s\n", host);
exit(1);
}
memcpy(&pingaddr.sin_addr, h->h_addr, sizeof(pingaddr.sin_addr));
if (c < 0 || c != sizeof(packet)) {
if (c < 0)
perror("ping: sendto");
- fprintf(stderr, "ping: write incomplete\n");
+ errorMsg("write incomplete\n");
exit(1);
}
exit(TRUE);
}
-#else /* ! BB_SIMPLE_PING */
+#else /* ! BB_FEATURE_SIMPLE_PING */
/* full(er) version */
-static const char *ping_usage = "ping [OPTION]... host\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
- "\nSend ICMP ECHO_REQUEST packets to network hosts.\n\n"
- "Options:\n"
- "\t-c COUNT\tSend only COUNT pings.\n"
- "\t-s SIZE\t\tSend SIZE data bytes in packets (default=56).\n"
- "\t-q\t\tQuiet mode, only displays output at start\n"
- "\t\t\tand when finished.\n"
-#endif
- ;
-
static char *hostname = NULL;
static struct sockaddr_in pingaddr;
static int pingsock = -1;
/**************************************************************************/
-static void pingstats(int ign)
+static void pingstats(int junk)
{
+ int status;
+
signal(SIGINT, SIG_IGN);
printf("\n--- %s ping statistics ---\n", hostname);
tmin / 10, tmin % 10,
(tsum / (nreceived + nrepeats)) / 10,
(tsum / (nreceived + nrepeats)) % 10, tmax / 10, tmax % 10);
- exit(0);
+ if (nreceived != 0)
+ status = EXIT_SUCCESS;
+ else
+ status = EXIT_FAILURE;
+ exit(status);
}
-static void sendping(int ign)
+static void sendping(int junk)
{
struct icmp *pkt;
int i;
(struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
if (i < 0)
- fatalError("ping: sendto: %s\n", strerror(errno));
- else if (i != sizeof(packet))
+ fatalError("sendto: %s\n", strerror(errno));
+ else if ((size_t)i != sizeof(packet))
fatalError("ping wrote %d chars; %d expected\n", i,
(int)sizeof(packet));
printf("\n");
} else
if (icmppkt->icmp_type != ICMP_ECHO)
- fprintf(stderr,
- "Warning: Got ICMP %d (%s)\n",
+ errorMsg("Warning: Got ICMP %d (%s)\n",
icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
}
if ((pingsock = socket(AF_INET, SOCK_RAW,
(proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
if (errno == EPERM) {
- fprintf(stderr, "ping: permission denied. (are you root?)\n");
+ errorMsg("permission denied. (are you root?)\n");
} else {
perror("ping: creating a raw socket");
}
pingaddr.sin_family = AF_INET;
if (!(h = gethostbyname(host))) {
- fprintf(stderr, "ping: unknown host %s\n", host);
+ errorMsg("unknown host %s\n", host);
exit(1);
}
if (h->h_addrtype != AF_INET) {
- fprintf(stderr,
- "ping: unknown address type; only AF_INET is currently supported.\n");
+ errorMsg("unknown address type; only AF_INET is currently supported.\n");
exit(1);
}
ping(*argv);
return(TRUE);
}
-#endif /* ! BB_SIMPLE_PING */
+#endif /* ! BB_FEATURE_SIMPLE_PING */
/*
* Copyright (c) 1989 The Regents of the University of California.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
+ *
+ * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change
+ * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change>
+ *
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.