need init of fromlen once per loop
[unix-history] / usr / src / sbin / ping / ping.c
index e829aef..827c6e7 100644 (file)
@@ -1,6 +1,29 @@
+/*
+ * Copyright (c) 1987, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ping.c     4.6 (Berkeley) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1987, 1988 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)ping.c     4.12 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  *                     P I N G . C
 
 /*
  *                     P I N G . C
@@ -61,6 +84,7 @@ char hnamebuf[MAXHOSTNAMELEN];
 char *inet_ntoa();
 
 int npackets;
 char *inet_ntoa();
 
 int npackets;
+int burst = 1;
 int ntransmitted = 0;          /* sequence # for outbound packets = #sent */
 int ident;
 
 int ntransmitted = 0;          /* sequence # for outbound packets = #sent */
 int ident;
 
@@ -112,15 +136,15 @@ char *argv[];
                hostname = hnamebuf;
        } else {
                hp = gethostbyname(av[0]);
                hostname = hnamebuf;
        } else {
                hp = gethostbyname(av[0]);
-               if (hp) {
-                       to->sin_family = hp->h_addrtype;
-                       bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
-                       hostname = hp->h_name;
-                       toaddr = inet_ntoa(to->sin_addr.s_addr);
-               } else {
-                       printf("%s: unknown host %s\n", argv[0], av[0]);
+               if (!hp) {
+                       fprintf(stderr, "ping: %s: ", av[0]);
+                       herror((char *)NULL);
                        exit(1);
                }
                        exit(1);
                }
+               to->sin_family = hp->h_addrtype;
+               bcopy(hp->h_addr, (caddr_t)&to->sin_addr, hp->h_length);
+               hostname = hp->h_name;
+               toaddr = inet_ntoa(to->sin_addr.s_addr);
        }
 
        if( argc >= 2 )
        }
 
        if( argc >= 2 )
@@ -166,9 +190,10 @@ char *argv[];
 
        for (;;) {
                int len = sizeof (packet);
 
        for (;;) {
                int len = sizeof (packet);
-               int fromlen = sizeof (from);
+               int fromlen;
                int cc;
 
                int cc;
 
+               fromlen = sizeof (from);
                if ( (cc=recvfrom(s, packet, len, 0, &from, &fromlen)) < 0) {
                        if( errno == EINTR )
                                continue;
                if ( (cc=recvfrom(s, packet, len, 0, &from, &fromlen)) < 0) {
                        if( errno == EINTR )
                                continue;
@@ -225,7 +250,7 @@ pinger()
 {
        static u_char outpack[MAXPACKET];
        register struct icmp *icp = (struct icmp *) outpack;
 {
        static u_char outpack[MAXPACKET];
        register struct icmp *icp = (struct icmp *) outpack;
-       int i, cc;
+       int i, cc, n;
        register struct timeval *tp = (struct timeval *) &outpack[8];
        register u_char *datap = &outpack[8+sizeof(struct timeval)];
 
        register struct timeval *tp = (struct timeval *) &outpack[8];
        register u_char *datap = &outpack[8+sizeof(struct timeval)];
 
@@ -247,13 +272,15 @@ pinger()
        icp->icmp_cksum = in_cksum( icp, cc );
 
        /* cc = sendto(s, msg, len, flags, to, tolen) */
        icp->icmp_cksum = in_cksum( icp, cc );
 
        /* cc = sendto(s, msg, len, flags, to, tolen) */
-       i = sendto( s, outpack, cc, 0, &whereto, sizeof(struct sockaddr) );
-
-       if( i < 0 || i != cc )  {
-               if( i<0 )  perror("sendto");
-               printf("ping: wrote %s %d chars, ret=%d\n",
-                       hostname, cc, i );
-               fflush(stdout);
+       for (n = 0; n < burst; n++) {
+               i = sendto(s, outpack, cc, 0, &whereto, sizeof(whereto));
+
+               if( i < 0 || i != cc )  {
+                       if( i<0 )  perror("sendto");
+                       printf("ping: wrote %s %d chars, ret=%d\n",
+                               hostname, cc, i );
+                       fflush(stdout);
+               }
        }
 }
 
        }
 }
 
@@ -374,6 +401,7 @@ int len;
        register u_short *w = addr;
        register u_short answer;
        register int sum = 0;
        register u_short *w = addr;
        register u_short answer;
        register int sum = 0;
+       u_short odd_byte = 0;
 
        /*
         *  Our algorithm is simple, using a 32 bit accumulator (sum),
 
        /*
         *  Our algorithm is simple, using a 32 bit accumulator (sum),
@@ -387,8 +415,10 @@ int len;
        }
 
        /* mop up an odd byte, if necessary */
        }
 
        /* mop up an odd byte, if necessary */
-       if( nleft == 1 )
-               sum += *(u_char *)w;
+       if( nleft == 1 ) {
+               *(u_char *)(&odd_byte) = *(u_char *)w;
+               sum += odd_byte;
+       }
 
        /*
         * add back carry outs from top 16 bits to low 16 bits
 
        /*
         * add back carry outs from top 16 bits to low 16 bits
@@ -430,9 +460,14 @@ finish()
        printf("\n----%s PING Statistics----\n", hostname );
        printf("%d packets transmitted, ", ntransmitted );
        printf("%d packets received, ", nreceived );
        printf("\n----%s PING Statistics----\n", hostname );
        printf("%d packets transmitted, ", ntransmitted );
        printf("%d packets received, ", nreceived );
-       if (ntransmitted)
-           printf("%d%% packet loss",
-               (int) (((ntransmitted-nreceived)*100) / ntransmitted ) );
+       if (ntransmitted) {
+               if (nreceived <= ntransmitted)
+                   printf("%d%% packet loss",
+                       (int) (((ntransmitted-nreceived)*100) / ntransmitted));
+               else
+                   printf("%.2f responses per request",
+                       (float) nreceived / (float) ntransmitted);
+       }
        printf("\n");
        if (nreceived && timing)
            printf("round-trip (ms)  min/avg/max = %d/%d/%d\n",
        printf("\n");
        if (nreceived && timing)
            printf("round-trip (ms)  min/avg/max = %d/%d/%d\n",