The branch main has been updated by markj:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=03d4d1c778091a08277d070185a9c60c7a6d57e0

commit 03d4d1c778091a08277d070185a9c60c7a6d57e0
Author:     Jose Luis Duran <[email protected]>
AuthorDate: 2022-11-21 01:18:43 +0000
Commit:     Mark Johnston <[email protected]>
CommitDate: 2023-10-11 17:48:28 +0000

    ping: Unify ping/ping6 statistics section
    
    This is a first step towards a unification/simplification of ping/ping6
    (internally).  The end goal is to produce a standardized user-facing
    output.
    
    Before (ping6):
    
        PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
        16 bytes from ::1, icmp_seq=0 hlim=64 time=0.168 ms
        16 bytes from ::1, icmp_seq=1 hlim=64 time=0.068 ms
    
        --- 2001:db8::2 ping6 statistics ---
        round-trip min/avg/max/std-dev = 0.068/0.118/0.168/0.050 ms
    
    After (ping6):
    
        PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
        16 bytes from ::1, icmp_seq=0 hlim=64 time=0.168 ms
        16 bytes from ::1, icmp_seq=1 hlim=64 time=0.068 ms
    
        --- 2001:db8::2 ping statistics ---
        round-trip min/avg/max/stddev = 0.068/0.118/0.168/0.050 ms
    
    This has the nice side-effect of adding units to SIGINFO's statistics,
    as printing numbers without units may not be of much help.  Also
    mentions the fact that these times are round-trip.
    
    Before (ping/ping6 SIGINFO):
    
        2/2 packets received (100.0%) 0.068 min / 0.118 avg / 0.168 max
    
    After (ping/ping6 SIGINFO):
    
        --- <ipv4/ipv6 address> ping statistics ---
        2 packets transmitted, 2 packets received, 0.0% packet loss
        round-trip min/avg/max/stddev = 0.068/0.118/0.168/0.050 ms
    
    In the case of a SIGINFO, the output will be printed to stderr, for both
    ping and ping6.
    
    Reviewed by:    markj
    MFC after:      1 week
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/863
    Differential Revision:  https://reviews.freebsd.org/D39126
---
 sbin/ping/main.c                     |  83 +++++++++++++++++
 sbin/ping/main.h                     |  25 ++++++
 sbin/ping/ping.c                     | 169 +++++++----------------------------
 sbin/ping/ping6.c                    | 123 +++----------------------
 sbin/ping/tests/ping_6_c1_s8_t1.out  |   6 +-
 sbin/ping/tests/ping_c1_s8_t1_S1.out |   6 +-
 sbin/ping/tests/test_ping.py         |  72 +++++++--------
 7 files changed, 193 insertions(+), 291 deletions(-)

diff --git a/sbin/ping/main.c b/sbin/ping/main.c
index e07b30952199..686b412a19b7 100644
--- a/sbin/ping/main.c
+++ b/sbin/ping/main.c
@@ -35,6 +35,8 @@
 #include <netinet/in.h>
 
 #include <err.h>
+#include <math.h>
+#include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -59,6 +61,28 @@
 #error At least one of INET and INET6 is required
 #endif
 
+/* various options */
+u_int options;
+
+char *hostname;
+
+/* counters */
+long nreceived;                /* # of packets we got back */
+long nrepeats;         /* number of duplicates */
+long ntransmitted;     /* sequence # for outbound packets = #sent */
+long nrcvtimeout = 0;  /* # of packets we got back after waittime */
+
+/* nonzero if we've been told to finish up */
+volatile sig_atomic_t seenint;
+volatile sig_atomic_t seeninfo;
+
+/* timing */
+int timing;            /* flag to do timing */
+double tmin = 999999999.0;     /* minimum round trip time */
+double tmax = 0.0;     /* maximum round trip time */
+double tsum = 0.0;     /* sum of all times, for doing average */
+double tsumsq = 0.0;   /* sum of all times squared, for std. dev. */
+
 int
 main(int argc, char *argv[])
 {
@@ -157,6 +181,65 @@ ping6:
 #endif
 }
 
+/*
+ * onsignal --
+ *     Set the global bit that causes the main loop to quit.
+ */
+void
+onsignal(int sig)
+{
+       switch (sig) {
+       case SIGALRM:
+       case SIGINT:
+               /*
+                * When doing reverse DNS lookups, the seenint flag might not
+                * be noticed for a while.  Just exit if we get a second SIGINT.
+                */
+               if (!(options & F_HOSTNAME) && seenint != 0)
+                       _exit(nreceived ? 0 : 2);
+               seenint++;
+               break;
+       case SIGINFO:
+               seeninfo++;
+               break;
+       }
+}
+
+/*
+ * pr_summary --
+ *     Print out summary statistics to the given output stream.
+ */
+void
+pr_summary(FILE * restrict stream)
+{
+       fprintf(stream, "\n--- %s ping statistics ---\n", hostname);
+       fprintf(stream, "%ld packets transmitted, ", ntransmitted);
+       fprintf(stream, "%ld packets received, ", nreceived);
+       if (nrepeats)
+               fprintf(stream, "+%ld duplicates, ", nrepeats);
+       if (ntransmitted) {
+               if (nreceived > ntransmitted)
+                       fprintf(stream, "-- somebody's duplicating packets!");
+               else
+                       fprintf(stream, "%.1f%% packet loss",
+                           ((((double)ntransmitted - nreceived) * 100.0) /
+                           ntransmitted));
+       }
+       if (nrcvtimeout)
+               fprintf(stream, ", %ld packets out of wait time", nrcvtimeout);
+       fputc('\n', stream);
+       if (nreceived && timing) {
+               /* Only display average to microseconds */
+               double num = nreceived + nrepeats;
+               double avg = tsum / num;
+               double stddev = sqrt(fmax(0, tsumsq / num - avg * avg));
+               fprintf(stream,
+                   "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
+                   tmin, avg, tmax, stddev);
+       }
+       fflush(stream);
+}
+
 void
 usage(void)
 {
diff --git a/sbin/ping/main.h b/sbin/ping/main.h
index 7084585e6bb0..9a883b61a350 100644
--- a/sbin/ping/main.h
+++ b/sbin/ping/main.h
@@ -49,6 +49,31 @@
 #endif
 #define PING6OPTS ".::6Aab:C:c:Dde:fHI:i:k:l:m:nNoOp:qS:s:t:uvyYW:z:" 
PING6ADDOPTS
 
+/* various options */
+extern u_int options;
+#define        F_HOSTNAME      0x0004
+
+extern char *hostname;
+
+/* counters */
+extern long nreceived;         /* # of packets we got back */
+extern long nrepeats;          /* number of duplicates */
+extern long ntransmitted;      /* sequence # for outbound packets = #sent */
+extern long nrcvtimeout;       /* # of packets we got back after waittime */
+
+/* nonzero if we've been told to finish up */
+extern volatile sig_atomic_t seenint;
+extern volatile sig_atomic_t seeninfo;
+
+/* timing */
+extern int timing;             /* flag to do timing */
+extern double tmin;            /* minimum round trip time */
+extern double tmax;            /* maximum round trip time */
+extern double tsum;            /* sum of all times, for doing average */
+extern double tsumsq;          /* sum of all times squared, for std. dev. */
+
+void onsignal(int);
+void pr_summary(FILE * __restrict);
 void usage(void) __dead2;
 
 #endif
diff --git a/sbin/ping/ping.c b/sbin/ping/ping.c
index ad16ff5a5ee9..bbb103a7361f 100644
--- a/sbin/ping/ping.c
+++ b/sbin/ping/ping.c
@@ -87,7 +87,6 @@ static char sccsid[] = "@(#)ping.c    8.1 (Berkeley) 6/5/93";
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
-#include <math.h>
 #include <netdb.h>
 #include <stddef.h>
 #include <signal.h>
@@ -127,10 +126,8 @@ struct tv32 {
 };
 
 /* various options */
-static int options;
 #define        F_FLOOD         0x0001
 #define        F_INTERVAL      0x0002
-#define        F_NUMERIC       0x0004
 #define        F_PINGFILLED    0x0008
 #define        F_QUIET         0x0010
 #define        F_RROUTE        0x0020
@@ -178,7 +175,6 @@ static char BSPACE = '\b';  /* characters written for flood 
*/
 static const char *DOT = ".";
 static size_t DOTlen = 1;
 static size_t DOTidx = 0;
-static char *hostname;
 static char *shostname;
 static int ident;              /* process id to identify our packets */
 static int uid;                        /* cached uid for micro-optimization */
@@ -190,9 +186,6 @@ static int send_len;
 /* counters */
 static long nmissedmax;                /* max value of ntransmitted - 
nreceived - 1 */
 static long npackets;          /* max packets to transmit */
-static long nreceived;         /* # of packets we got back */
-static long nrepeats;          /* number of duplicates */
-static long ntransmitted;      /* sequence # for outbound packets = #sent */
 static long snpackets;                 /* max packets to transmit in one sweep 
*/
 static long sntransmitted;     /* # of packets we sent in this sweep */
 static int sweepmax;           /* max value of payload in sweep */
@@ -200,33 +193,17 @@ static int sweepmin = 0;  /* start value of payload in 
sweep */
 static int sweepincr = 1;      /* payload increment in sweep */
 static int interval = 1000;    /* interval between packets, ms */
 static int waittime = MAXWAIT; /* timeout for each packet */
-static long nrcvtimeout = 0;   /* # of packets we got back after waittime */
-
-/* timing */
-static int timing;             /* flag to do timing */
-static double tmin = 999999999.0;      /* minimum round trip time */
-static double tmax = 0.0;      /* maximum round trip time */
-static double tsum = 0.0;      /* sum of all times, for doing average */
-static double tsumsq = 0.0;    /* sum of all times squared, for std. dev. */
-
-/* nonzero if we've been told to finish up */
-static volatile sig_atomic_t finish_up;
-static volatile sig_atomic_t siginfo_p;
 
 static cap_channel_t *capdns;
 
 static void fill(char *, char *);
 static cap_channel_t *capdns_setup(void);
-static void check_status(void);
-static void finish(void) __dead2;
 static void pinger(void);
 static char *pr_addr(struct in_addr);
 static char *pr_ntime(n_time);
 static void pr_icmph(struct icmp *, struct ip *, const u_char *const);
 static void pr_iph(struct ip *, const u_char *);
 static void pr_pack(char *, ssize_t, struct sockaddr_in *, struct timespec *);
-static void status(int);
-static void stopit(int);
 
 int
 ping(int argc, char *const *argv)
@@ -264,8 +241,6 @@ ping(int argc, char *const *argv)
 #endif
        cap_rights_t rights;
 
-       options |= F_NUMERIC;
-
        /*
         * Do the stuff that we need root priv's for *first*, and
         * then drop our setuid bit.  Save error reporting for
@@ -379,7 +354,7 @@ ping(int argc, char *const *argv)
                        options |= F_SWEEP;
                        break;
                case 'H':
-                       options &= ~F_NUMERIC;
+                       options |= F_HOSTNAME;
                        break;
                case 'h': /* Packet size increment for ping sweep */
                        ltmp = strtonum(optarg, 1, INT_MAX, &errstr);
@@ -452,7 +427,7 @@ ping(int argc, char *const *argv)
                        options |= F_TTL;
                        break;
                case 'n':
-                       options |= F_NUMERIC;
+                       options &= ~F_HOSTNAME;
                        break;
                case 'o':
                        options |= F_ONCE;
@@ -882,22 +857,17 @@ ping(int argc, char *const *argv)
 
        sigemptyset(&si_sa.sa_mask);
        si_sa.sa_flags = 0;
-
-       si_sa.sa_handler = stopit;
-       if (sigaction(SIGINT, &si_sa, 0) == -1) {
+       si_sa.sa_handler = onsignal;
+       if (sigaction(SIGINT, &si_sa, 0) == -1)
                err(EX_OSERR, "sigaction SIGINT");
-       }
-
-       si_sa.sa_handler = status;
-       if (sigaction(SIGINFO, &si_sa, 0) == -1) {
+       seenint = 0;
+       if (sigaction(SIGINFO, &si_sa, 0) == -1)
                err(EX_OSERR, "sigaction SIGINFO");
-       }
-
-        if (alarmtimeout > 0) {
-               si_sa.sa_handler = stopit;
+       seeninfo = 0;
+       if (alarmtimeout > 0) {
                if (sigaction(SIGALRM, &si_sa, 0) == -1)
                        err(EX_OSERR, "sigaction SIGALRM");
-        }
+       }
 
        bzero(&msg, sizeof(msg));
        msg.msg_name = (caddr_t)&from;
@@ -929,13 +899,18 @@ ping(int argc, char *const *argv)
        }
 
        almost_done = 0;
-       while (!finish_up) {
+       while (seenint == 0) {
                struct timespec now, timeout;
                fd_set rfds;
                int n;
                ssize_t cc;
 
-               check_status();
+               /* signal handling */
+               if (seeninfo) {
+                       pr_summary(stderr);
+                       seeninfo = 0;
+                       continue;
+               }
                if ((unsigned)srecv >= FD_SETSIZE)
                        errx(EX_OSERR, "descriptor too large");
                FD_ZERO(&rfds);
@@ -945,9 +920,10 @@ ping(int argc, char *const *argv)
                timespecsub(&timeout, &now, &timeout);
                if (timeout.tv_sec < 0)
                        timespecclear(&timeout);
+
                n = pselect(srecv + 1, &rfds, NULL, NULL, &timeout, NULL);
                if (n < 0)
-                       continue;       /* Must be EINTR. */
+                       continue;       /* EINTR */
                if (n == 1) {
                        struct timespec *tv = NULL;
 #ifdef SO_TIMESTAMP
@@ -982,7 +958,7 @@ ping(int argc, char *const *argv)
                            (npackets && nreceived >= npackets))
                                break;
                }
-               if (n == 0 || options & F_FLOOD) {
+               if (n == 0 || (options & F_FLOOD)) {
                        if (sweepmax && sntransmitted == snpackets) {
                                if (datalen + sweepincr > sweepmax)
                                        break;
@@ -998,14 +974,21 @@ ping(int argc, char *const *argv)
                                if (almost_done)
                                        break;
                                almost_done = 1;
+                               /*
+                                * If we're not transmitting any more packets,
+                                * change the timer to wait two round-trip times
+                                * if we've received any packets or (waittime)
+                                * milliseconds if we haven't.
+                                */
                                intvl.tv_nsec = 0;
                                if (nreceived) {
                                        intvl.tv_sec = 2 * tmax / 1000;
-                                       if (!intvl.tv_sec)
+                                       if (intvl.tv_sec == 0)
                                                intvl.tv_sec = 1;
                                } else {
                                        intvl.tv_sec = waittime / 1000;
-                                       intvl.tv_nsec = waittime % 1000 * 
1000000;
+                                       intvl.tv_nsec =
+                                           waittime % 1000 * 1000000;
                                }
                        }
                        (void)clock_gettime(CLOCK_MONOTONIC, &last);
@@ -1016,28 +999,9 @@ ping(int argc, char *const *argv)
                        }
                }
        }
-       finish();
-       /* NOTREACHED */
-       exit(0);        /* Make the compiler happy */
-}
-
-/*
- * stopit --
- *     Set the global bit that causes the main loop to quit.
- * Do NOT call finish() from here, since finish() does far too much
- * to be called from a signal handler.
- */
-void
-stopit(int sig __unused)
-{
+       pr_summary(stdout);
 
-       /*
-        * When doing reverse DNS lookups, the finish_up flag might not
-        * be noticed for a while.  Just exit if we get a second SIGINT.
-        */
-       if (!(options & F_NUMERIC) && finish_up)
-               _exit(nreceived ? 0 : 2);
-       finish_up = 1;
+       exit(nreceived ? 0 : 2);
 }
 
 /*
@@ -1463,77 +1427,6 @@ pr_pack(char *buf, ssize_t cc, struct sockaddr_in *from, 
struct timespec *tv)
        }
 }
 
-/*
- * status --
- *     Print out statistics when SIGINFO is received.
- */
-
-static void
-status(int sig __unused)
-{
-
-       siginfo_p = 1;
-}
-
-static void
-check_status(void)
-{
-
-       if (siginfo_p) {
-               siginfo_p = 0;
-               (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)",
-                   nreceived, ntransmitted,
-                   ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0);
-               if (nreceived && timing)
-                       (void)fprintf(stderr, " %.3f min / %.3f avg / %.3f max",
-                           tmin, tsum / (nreceived + nrepeats), tmax);
-               (void)fprintf(stderr, "\n");
-       }
-}
-
-/*
- * finish --
- *     Print out statistics, and give up.
- */
-static void
-finish(void)
-{
-
-       (void)signal(SIGINT, SIG_IGN);
-       (void)signal(SIGALRM, SIG_IGN);
-       (void)putchar('\n');
-       (void)fflush(stdout);
-       (void)printf("--- %s ping statistics ---\n", hostname);
-       (void)printf("%ld packets transmitted, ", ntransmitted);
-       (void)printf("%ld packets received, ", nreceived);
-       if (nrepeats)
-               (void)printf("+%ld duplicates, ", nrepeats);
-       if (ntransmitted) {
-               if (nreceived > ntransmitted)
-                       (void)printf("-- somebody's printing up packets!");
-               else
-                       (void)printf("%.1f%% packet loss",
-                           ((ntransmitted - nreceived) * 100.0) /
-                           ntransmitted);
-       }
-       if (nrcvtimeout)
-               (void)printf(", %ld packets out of wait time", nrcvtimeout);
-       (void)putchar('\n');
-       if (nreceived && timing) {
-               double n = nreceived + nrepeats;
-               double avg = tsum / n;
-               double stddev = sqrt(fmax(0, tsumsq / n - avg * avg));
-               (void)printf(
-                   "round-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms\n",
-                   tmin, avg, tmax, stddev);
-       }
-
-       if (nreceived)
-               exit(0);
-       else
-               exit(2);
-}
-
 /*
  * pr_icmph --
  *     Print a descriptive string about an ICMP header.
@@ -1705,7 +1598,7 @@ pr_addr(struct in_addr ina)
        struct hostent *hp;
        static char buf[16 + 3 + MAXHOSTNAMELEN];
 
-       if (options & F_NUMERIC)
+       if (!(options & F_HOSTNAME))
                return inet_ntoa(ina);
 
        hp = cap_gethostbyaddr(capdns, (char *)&ina, sizeof(ina), AF_INET);
diff --git a/sbin/ping/ping6.c b/sbin/ping/ping6.c
index d14da9c67a52..a71ef8a84aee 100644
--- a/sbin/ping/ping6.c
+++ b/sbin/ping/ping6.c
@@ -124,7 +124,6 @@ static char sccsid[] = "@(#)ping.c  8.1 (Berkeley) 6/5/93";
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <math.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -188,7 +187,6 @@ struct tv32 {
 #define F_FQDN         0x1000
 #define F_INTERFACE    0x2000
 #define F_SRCADDR      0x4000
-#define F_HOSTNAME     0x10000
 #define F_FQDNOLD      0x20000
 #define F_NIGROUP      0x40000
 #define F_SUPTYPES     0x80000
@@ -200,7 +198,6 @@ struct tv32 {
 #define F_NOUSERDATA   (F_NODEADDR | F_FQDN | F_FQDNOLD | F_SUPTYPES)
 #define        F_WAITTIME      0x2000000
 #define        F_DOT           0x4000000
-static u_int options;
 
 #define IN6LEN         sizeof(struct in6_addr)
 #define SA6LEN         sizeof(struct sockaddr_in6)
@@ -229,7 +226,6 @@ static char BBELL = '\a';   /* characters written for 
AUDIBLE */
 static const char *DOT = ".";
 static size_t DOTlen = 1;
 static size_t DOTidx = 0;
-static char *hostname;
 static int ident;              /* process id to identify our packets */
 static u_int8_t nonce[8];      /* nonce field for node information */
 static int hoplimit = -1;      /* hoplimit */
@@ -241,20 +237,9 @@ static cap_channel_t *capdns;
 /* counters */
 static long nmissedmax;                /* max value of ntransmitted - 
nreceived - 1 */
 static long npackets;          /* max packets to transmit */
-static long nreceived;         /* # of packets we got back */
-static long nrepeats;          /* number of duplicates */
-static long ntransmitted;      /* sequence # for outbound packets = #sent */
 static long ntransmitfailures; /* number of transmit failures */
 static int interval = 1000;    /* interval between packets in ms */
 static int waittime = MAXWAIT; /* timeout for each packet */
-static long nrcvtimeout = 0;   /* # of packets we got back after waittime */
-
-/* timing */
-static int timing;             /* flag to do timing */
-static double tmin = 999999999.0;      /* minimum round trip time */
-static double tmax = 0.0;      /* maximum round trip time */
-static double tsum = 0.0;      /* sum of all times, for doing average */
-static double tsumsq = 0.0;    /* sum of all times squared, for std. dev. */
 
 /* for node addresses */
 static u_short naflags;
@@ -264,18 +249,11 @@ static struct msghdr smsghdr;
 static struct iovec smsgiov;
 static char *scmsg = 0;
 
-static volatile sig_atomic_t seenint;
-#ifdef SIGINFO
-static volatile sig_atomic_t seeninfo;
-#endif
-
 static cap_channel_t *capdns_setup(void);
 static void     fill(char *, char *);
 static int      get_hoplim(struct msghdr *);
 static int      get_pathmtu(struct msghdr *);
 static struct in6_pktinfo *get_rcvpktinfo(struct msghdr *);
-static void     onsignal(int);
-static void     onint(int);
 static size_t   pingerlen(void);
 static int      pinger(void);
 static const char *pr_addr(struct sockaddr *, int);
@@ -293,7 +271,6 @@ static void  pr_ip6opt(void *, size_t);
 static void     pr_rthdr(void *, size_t);
 static int      pr_bitrange(u_int32_t, int, int);
 static void     pr_retip(struct ip6_hdr *, u_char *);
-static void     summary(void);
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
 static int      setpolicy(int, char *);
@@ -1142,7 +1119,7 @@ ping6(int argc, char *argv[])
        if (caph_rights_limit(ssend, &rights_ssend) < 0)
                err(1, "caph_rights_limit ssend setsockopt");
 
-       printf("PING6(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
+       printf("PING(%lu=40+8+%lu bytes) ", (unsigned long)(40 + pingerlen()),
            (unsigned long)(pingerlen() - 8));
        printf("%s --> ", pr_addr((struct sockaddr *)&src, sizeof(src)));
        printf("%s\n", pr_addr((struct sockaddr *)&dst, sizeof(dst)));
@@ -1163,11 +1140,9 @@ ping6(int argc, char *argv[])
        if (sigaction(SIGINT, &si_sa, 0) == -1)
                err(EX_OSERR, "sigaction SIGINT");
        seenint = 0;
-#ifdef SIGINFO
        if (sigaction(SIGINFO, &si_sa, 0) == -1)
                err(EX_OSERR, "sigaction SIGINFO");
        seeninfo = 0;
-#endif
        if (alarmtimeout > 0) {
                if (sigaction(SIGALRM, &si_sa, 0) == -1)
                        err(EX_OSERR, "sigaction SIGALRM");
@@ -1186,15 +1161,11 @@ ping6(int argc, char *argv[])
                int n;
 
                /* signal handling */
-               if (seenint)
-                       onint(SIGINT);
-#ifdef SIGINFO
                if (seeninfo) {
-                       summary();
+                       pr_summary(stderr);
                        seeninfo = 0;
                        continue;
                }
-#endif
                FD_ZERO(&rfds);
                FD_SET(srecv, &rfds);
                clock_gettime(CLOCK_MONOTONIC, &now);
@@ -1258,12 +1229,12 @@ ping6(int argc, char *argv[])
                                if (almost_done)
                                        break;
                                almost_done = 1;
-                       /*
-                        * If we're not transmitting any more packets,
-                        * change the timer to wait two round-trip times
-                        * if we've received any packets or (waittime)
-                        * milliseconds if we haven't.
-                        */
+                               /*
+                                * If we're not transmitting any more packets,
+                                * change the timer to wait two round-trip times
+                                * if we've received any packets or (waittime)
+                                * milliseconds if we haven't.
+                                */
                                intvl.tv_nsec = 0;
                                if (nreceived) {
                                        intvl.tv_sec = 2 * tmax / 1000;
@@ -1272,7 +1243,7 @@ ping6(int argc, char *argv[])
                                } else {
                                        intvl.tv_sec = waittime / 1000;
                                        intvl.tv_nsec =
-                                               waittime % 1000 * 1000000;
+                                           waittime % 1000 * 1000000;
                                }
                        }
                        clock_gettime(CLOCK_MONOTONIC, &last);
@@ -1288,7 +1259,7 @@ ping6(int argc, char *argv[])
        si_sa.sa_handler = SIG_IGN;
        sigaction(SIGINT, &si_sa, 0);
        sigaction(SIGALRM, &si_sa, 0);
-       summary();
+       pr_summary(stdout);
 
         if(packet != NULL)
                 free(packet);
@@ -1301,23 +1272,6 @@ ping6(int argc, char *argv[])
                exit(EX_OSERR);
 }
 
-static void
-onsignal(int sig)
-{
-
-       switch (sig) {
-       case SIGINT:
-       case SIGALRM:
-               seenint++;
-               break;
-#ifdef SIGINFO
-       case SIGINFO:
-               seeninfo++;
-               break;
-#endif
-       }
-}
-
 /*
  * pinger --
  *     Compose and transmit an ICMP ECHO REQUEST packet.  The IP packet
@@ -1469,7 +1423,7 @@ pinger(void)
                        ntransmitfailures++;
                        warn("sendmsg");
                }
-               (void)printf("ping6: wrote %s %d chars, ret=%d\n",
+               (void)printf("ping: wrote %s %d chars, ret=%d\n",
                    hostname, cc, i);
        }
        if (!(options & F_QUIET) && options & F_DOT)
@@ -2305,59 +2259,6 @@ get_pathmtu(struct msghdr *mhdr)
        return(0);
 }
 
-/*
- * onint --
- *     SIGINT handler.
- */
-/* ARGSUSED */
-static void
-onint(int notused __unused)
-{
-       /*
-        * When doing reverse DNS lookups, the seenint flag might not
-        * be noticed for a while.  Just exit if we get a second SIGINT.
-        */
-       if ((options & F_HOSTNAME) && seenint != 0)
-               _exit(nreceived ? 0 : 2);
-}
-
-/*
- * summary --
- *     Print out statistics.
- */
-static void
-summary(void)
-{
-
-       (void)printf("\n--- %s ping6 statistics ---\n", hostname);
-       (void)printf("%ld packets transmitted, ", ntransmitted);
-       (void)printf("%ld packets received, ", nreceived);
-       if (nrepeats)
-               (void)printf("+%ld duplicates, ", nrepeats);
-       if (ntransmitted) {
-               if (nreceived > ntransmitted)
-                       (void)printf("-- somebody's duplicating packets!");
-               else
-                       (void)printf("%.1f%% packet loss",
-                           ((((double)ntransmitted - nreceived) * 100.0) /
-                           ntransmitted));
-       }
-       if (nrcvtimeout)
-               printf(", %ld packets out of wait time", nrcvtimeout);
-       (void)putchar('\n');
-       if (nreceived && timing) {
-               /* Only display average to microseconds */
-               double num = nreceived + nrepeats;
-               double avg = tsum / num;
-               double stddev = sqrt(fmax(0, tsumsq / num - avg * avg));
-               (void)printf(
-                   "round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
-                   tmin, avg, tmax, stddev);
-               (void)fflush(stdout);
-       }
-       (void)fflush(stdout);
-}
-
 /*subject type*/
 static const char *niqcode[] = {
        "IPv6 address",
@@ -2640,7 +2541,7 @@ pr_addr(struct sockaddr *addr, int addrlen)
        static char buf[NI_MAXHOST];
        int flag = 0;
 
-       if ((options & F_HOSTNAME) == 0)
+       if (!(options & F_HOSTNAME))
                flag |= NI_NUMERICHOST;
 
        if (cap_getnameinfo(capdns, addr, addrlen, buf, sizeof(buf), NULL, 0,
diff --git a/sbin/ping/tests/ping_6_c1_s8_t1.out 
b/sbin/ping/tests/ping_6_c1_s8_t1.out
index 81c56e6cf586..0d207e74bc3e 100644
--- a/sbin/ping/tests/ping_6_c1_s8_t1.out
+++ b/sbin/ping/tests/ping_6_c1_s8_t1.out
@@ -1,6 +1,6 @@
-PING6(56=40+8+8 bytes) ::1 --> ::1
+PING(56=40+8+8 bytes) ::1 --> ::1
 16 bytes from ::1, icmp_seq=0 hlim= time= ms
 
---- localhost ping6 statistics ---
+--- localhost ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
diff --git a/sbin/ping/tests/ping_c1_s8_t1_S1.out 
b/sbin/ping/tests/ping_c1_s8_t1_S1.out
index 81c56e6cf586..0d207e74bc3e 100644
--- a/sbin/ping/tests/ping_c1_s8_t1_S1.out
+++ b/sbin/ping/tests/ping_c1_s8_t1_S1.out
@@ -1,6 +1,6 @@
-PING6(56=40+8+8 bytes) ::1 --> ::1
+PING(56=40+8+8 bytes) ::1 --> ::1
 16 bytes from ::1, icmp_seq=0 hlim= time= ms
 
---- localhost ping6 statistics ---
+--- localhost ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
diff --git a/sbin/ping/tests/test_ping.py b/sbin/ping/tests/test_ping.py
index 1c2540cb6d15..c51823103133 100644
--- a/sbin/ping/tests/test_ping.py
+++ b/sbin/ping/tests/test_ping.py
@@ -311,12 +311,12 @@ round-trip min/avg/max/stddev = /// ms
                 "args": "ping -6 -c1 -s8 -t1 localhost",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) ::1 --> ::1
+PING(56=40+8+8 bytes) ::1 --> ::1
 16 bytes from ::1, icmp_seq=0 hlim= time= ms
 
---- localhost ping6 statistics ---
+--- localhost ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -357,12 +357,12 @@ PING 192.0.2.2 (192.0.2.2): 56 data bytes
                 "args": "ping -A -c1 2001:db8::1",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
 16 bytes from 2001:db8::1, icmp_seq=0 hlim= time= ms
 
---- 2001:db8::1 ping6 statistics ---
+--- 2001:db8::1 ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -373,9 +373,9 @@ round-trip min/avg/max/std-dev = /// ms
                 "args": "ping -A -c1 2001:db8::2",
                 "returncode": 2,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
 
---- 2001:db8::2 ping6 statistics ---
+--- 2001:db8::2 ping statistics ---
 1 packets transmitted, 0 packets received, 100.0% packet loss
 """,
                 "stderr": "",
@@ -419,14 +419,14 @@ round-trip min/avg/max/stddev = /// ms
                 "args": "ping -A -c3 2001:db8::1",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
 16 bytes from 2001:db8::1, icmp_seq=0 hlim= time= ms
 16 bytes from 2001:db8::1, icmp_seq=1 hlim= time= ms
 16 bytes from 2001:db8::1, icmp_seq=2 hlim= time= ms
 
---- 2001:db8::1 ping6 statistics ---
+--- 2001:db8::1 ping statistics ---
 3 packets transmitted, 3 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -437,9 +437,9 @@ round-trip min/avg/max/std-dev = /// ms
                 "args": "ping -A -c3 2001:db8::2",
                 "returncode": 2,
                 "stdout": """\
-\x07\x07PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
+\x07\x07PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
 
---- 2001:db8::2 ping6 statistics ---
+--- 2001:db8::2 ping statistics ---
 3 packets transmitted, 0 packets received, 100.0% packet loss
 """,
                 "stderr": "",
@@ -481,12 +481,12 @@ PING 192.0.2.2 (192.0.2.2): 56 data bytes
                 "args": "ping -c1 2001:db8::1",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
 16 bytes from 2001:db8::1, icmp_seq=0 hlim= time= ms
 
---- 2001:db8::1 ping6 statistics ---
+--- 2001:db8::1 ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -497,9 +497,9 @@ round-trip min/avg/max/std-dev = /// ms
                 "args": "ping -c1 2001:db8::2",
                 "returncode": 2,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
 
---- 2001:db8::2 ping6 statistics ---
+--- 2001:db8::2 ping statistics ---
 1 packets transmitted, 0 packets received, 100.0% packet loss
 """,
                 "stderr": "",
@@ -527,12 +527,12 @@ round-trip min/avg/max/stddev = /// ms
                 "args": "ping -c1 -S::1 -s8 -t1 localhost",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) ::1 --> ::1
+PING(56=40+8+8 bytes) ::1 --> ::1
 16 bytes from ::1, icmp_seq=0 hlim= time= ms
 
---- localhost ping6 statistics ---
+--- localhost ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -575,14 +575,14 @@ PING 192.0.2.2 (192.0.2.2): 56 data bytes
                 "args": "ping -c3 2001:db8::1",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
 16 bytes from 2001:db8::1, icmp_seq=0 hlim= time= ms
 16 bytes from 2001:db8::1, icmp_seq=1 hlim= time= ms
 16 bytes from 2001:db8::1, icmp_seq=2 hlim= time= ms
 
---- 2001:db8::1 ping6 statistics ---
+--- 2001:db8::1 ping statistics ---
 3 packets transmitted, 3 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -593,9 +593,9 @@ round-trip min/avg/max/std-dev = /// ms
                 "args": "ping -c3 2001:db8::2",
                 "returncode": 2,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
 
---- 2001:db8::2 ping6 statistics ---
+--- 2001:db8::2 ping statistics ---
 3 packets transmitted, 0 packets received, 100.0% packet loss
 """,
                 "stderr": "",
@@ -636,11 +636,11 @@ PING 192.0.2.2 (192.0.2.2): 56 data bytes
                 "args": "ping -q -c1 2001:db8::1",
                 "returncode": 0,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
+PING(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::1
 
---- 2001:db8::1 ping6 statistics ---
+--- 2001:db8::1 ping statistics ---
 1 packets transmitted, 1 packets received, 0.0% packet loss
-round-trip min/avg/max/std-dev = /// ms
+round-trip min/avg/max/stddev = /// ms
 """,
                 "stderr": "",
             },
@@ -651,9 +651,9 @@ round-trip min/avg/max/std-dev = /// ms
                 "args": "ping -q -c1 2001:db8::2",
                 "returncode": 2,
                 "stdout": """\
-PING6(56=40+8+8 bytes) 2001:db8::1 --> 2001:db8::2
*** 34 LINES SKIPPED ***

Reply via email to