/* vi: set sw=4 ts=4: */
/*
- * $Id: ping.c,v 1.1.1.1 2005/04/29 01:44:57 echo Exp $
+ * $Id: ping.c,v 1.55 2003/07/22 08:56:51 andersen Exp $
* Mini ping implementation for busybox
*
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
static const int PINGINTERVAL = 1; /* second */
#define O_QUIET (1 << 0)
+#define O_LOG 2
#define A(bit) rcvd_tbl[(bit)>>3] /* identify byte in array */
#define B(bit) (1 << ((bit) & 0x07)) /* identify bit in byte */
#define CLR(bit) (A(bit) &= (~B(bit)))
#define TST(bit) (A(bit) & B(bit))
+//BRCM begin
+/* store ping statistics to a file */
+FILE *pingFile=NULL;
+FILE *pingPid=NULL;
+//BRCM end
+
static void ping(const char *host);
/* common routines */
static int pingsock = -1;
static int datalen; /* intentionally uninitialized to work around gcc bug */
-static long ntransmitted, nreceived, nrepeats, pingcount;
+// brcm: changed default value of pingcount from 0 to 4.
+static long ntransmitted, nreceived, nrepeats, pingcount=4;
static int myid, options;
static unsigned long tmin = ULONG_MAX, tmax, tsum;
static char rcvd_tbl[MAX_DUP_CHK / 8];
static void pingstats(int);
static void unpack(char *, int, struct sockaddr_in *);
+static void logStat(int finish)
+{
+ char ip[16];
+ struct in_addr addr;
+
+ pingFile = fopen ("/var/pingStats", "w");
+ if (pingFile == NULL)
+ return;
+
+ rewind(pingFile);
+ if ((finish) && (ntransmitted == 0))
+ fprintf(pingFile,"%d\n",-1);
+ else
+ fprintf(pingFile,"%d\n",finish);
+
+ memcpy(&addr, hostent->h_addr, sizeof(ip));
+ strncpy(ip,inet_ntoa(addr),16);
+ /* IP, sent, receive, lost, min, max, average */
+ fprintf(pingFile,"IP = %s Sent = %ld Receive = %ld Lost = %ld Min = %lu.%lu ms Max = %lu.%lu ms Average = %lu.%lu ms \n",
+ ip, ntransmitted, nreceived, (ntransmitted-nreceived),
+ (tmin / 10), (tmin % 10),
+ (tmax / 10), (tmax % 10),
+ ((tsum / (nreceived + nrepeats)) / 10),
+ ((tsum / (nreceived + nrepeats)) % 10));
+
+ fclose(pingFile);
+}
+
/**************************************************************************/
static void pingstats(int junk)
int status;
signal(SIGINT, SIG_IGN);
-
printf("\n--- %s ping statistics ---\n", hostent->h_name);
printf("%ld packets transmitted, ", ntransmitted);
printf("%ld packets received, ", nreceived);
status = EXIT_SUCCESS;
else
status = EXIT_FAILURE;
+
+ //BRCM begin
+ if (options & O_LOG) {
+ logStat(1);
+ if (access("/var/pingPid",F_OK) == 0)
+ unlink("/var/pingPid");
+ }
+ //BRCM end
+
exit(status);
}
SET(icmppkt->icmp_seq % MAX_DUP_CHK);
dupflag = 0;
}
+ //BRCM begin
+ if (options & O_LOG) {
+ logStat(0);
+ usleep(1);
+ } /* O_LOG */
+ //BRCM end
if (options & O_QUIET)
return;
- printf("%d bytes from %s: icmp_seq=%u", sz,
+ /*brcm: changed display message to show actually receive data bytes length
+ rather than ICMP packet length which is ICMP_MINLEN+dataLen */
+ printf("%d bytes from %s: icmp_seq=%u", (sz-ICMP_MINLEN),
inet_ntoa(*(struct in_addr *) &from->sin_addr.s_addr),
icmppkt->icmp_seq);
printf(" ttl=%d", iphdr->ttl);
pingaddr.sin_family = AF_INET;
hostent = xgethostbyname(host);
+
if (hostent->h_addrtype != AF_INET)
bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
extern int ping_main(int argc, char **argv)
{
+ FILE *fd;
char *thisarg;
datalen = DEFDATALEN; /* initialized here rather than in global scope to work around gcc bug */
argv++;
datalen = atoi(*argv);
break;
+ //BRCM begin
+ case 'l':
+ if ((pingPid = fopen ("/var/pingPid", "w")) != NULL) {
+ fprintf(pingPid,"%d\n",getpid());
+ (void)fclose(pingPid);
+ }
+ /* log statistics to file */
+ options |= O_LOG;
+ break;
+ //BRCM end
default:
bb_show_usage();
}
myid = getpid() & 0xFFFF;
ping(*argv);
+
+ if (options & O_LOG) {
+ if (access("/var/pingPid",F_OK) == 0) {
+ unlink("/var/pingPid");
+ }
+ }
return EXIT_SUCCESS;
}
#endif /* ! CONFIG_FEATURE_FANCY_PING */