need init of fromlen once per loop
[unix-history] / usr / src / sbin / ping / ping.c
index 69275d2..827c6e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
+ * Copyright (c) 1987, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
 
 #ifndef lint
 char copyright[] =
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1987 Regents of the University of California.\n\
+"@(#) Copyright (c) 1987, 1988 Regents of the University of California.\n\
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)ping.c     4.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)ping.c     4.12 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -84,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;
 
@@ -135,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 )
@@ -189,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;
@@ -248,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)];
 
@@ -270,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);
+               }
        }
 }
 
        }
 }
 
@@ -456,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",