www.usr.com/support/gpl/USR9107_release.1.4.tar.gz
[bcm963xx.git] / userapps / opensource / busybox / networking / ping.c
index a63dcf0..00fa7ca 100755 (executable)
@@ -1,6 +1,6 @@
 /* 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>
@@ -61,6 +61,7 @@ static const int MAXWAIT = 10;
 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 */
@@ -68,6 +69,12 @@ static const int PINGINTERVAL = 1;           /* second */
 #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 */
@@ -173,7 +180,8 @@ static struct sockaddr_in pingaddr;
 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];
@@ -184,6 +192,34 @@ static void sendping(int);
 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)
@@ -191,7 +227,6 @@ 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);
@@ -209,6 +244,15 @@ static void pingstats(int junk)
                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);
 }
 
@@ -317,11 +361,19 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from)
                        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);
@@ -346,6 +398,7 @@ static void ping(const char *host)
 
        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.");
 
@@ -393,6 +446,7 @@ static void ping(const char *host)
 
 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 */
@@ -420,6 +474,16 @@ extern int ping_main(int argc, char **argv)
                        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();
                }
@@ -431,6 +495,12 @@ extern int ping_main(int argc, char **argv)
 
        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 */