]> git.saurik.com Git - apple/network_cmds.git/blobdiff - ping.tproj/ping.c
network_cmds-480.tar.gz
[apple/network_cmds.git] / ping.tproj / ping.c
index 4480408c50179fb198796a2da7d4648e0d49c7bc..fa79b60c58e089cb1d15f953b6c39ff60891675c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999-2013 Apple Inc. All rights reserved.
+ * Copyright (c) 1999-2015 Apple Inc. All rights reserved.
  *
  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  *
  * SUCH DAMAGE.
  */
 
-#if 0
+#include <sys/cdefs.h>
+
 #ifndef lint
-static const char copyright[] =
+__unused static const char copyright[] =
 "@(#) Copyright (c) 1989, 1993\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
-#ifndef lint
-static char sccsid[] = "@(#)ping.c     8.1 (Berkeley) 6/5/93";
-#endif /* not lint */
-#endif
-#include <sys/cdefs.h>
-
 /*
  *                     P I N G . C
  *
@@ -223,6 +218,7 @@ int sweepincr = 1;          /* payload increment in sweep */
 int interval = 1000;           /* interval between packets, ms */
 int waittime = MAXWAIT;                /* timeout for each packet */
 long nrcvtimeout = 0;          /* # of packets we got back after waittime */
+int icmp_len = 0;              /* length of the ICMP header */
 
 /* timing */
 int timing;                    /* flag to do timing */
@@ -248,6 +244,7 @@ static void pr_retip(struct ip *);
 static void status(int);
 static void stopit(int);
 static void tvsub(struct timeval *, const struct timeval *);
+static uint32_t str2svc(const char *);
 static void usage(void) __dead2;
 
 int
@@ -270,7 +267,7 @@ main(int argc, char *const *argv)
        struct sockaddr_in *to;
        double t;
        u_long alarmtimeout, ultmp;
-       int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno,
+       int almost_done, ch, df, hold, i, mib[4], preload, sockerrno,
            tos, ttl;
        char ctrl[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(int))];
        char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN];
@@ -425,7 +422,10 @@ main(int argc, char *const *argv)
                        break;
                case 'k':
                        how_traffic_class++;
-                       traffic_class = atoi(optarg);
+                       traffic_class = str2svc(optarg);
+                       if (traffic_class == UINT32_MAX)
+                               errx(EX_USAGE, "bad traffic class: `%s'",
+                                    optarg);
                        break;
                case 'L':
                        options |= F_NOLOOP;
@@ -682,7 +682,7 @@ main(int argc, char *const *argv)
                timing = 1;
 
        if (!(options & F_PINGFILLED))
-               for (i = TIMEVAL_LEN; i < datalen; ++i)
+               for (i = TIMEVAL_LEN; i < MAX(datalen, sweepmax); ++i)
                        *datap++ = i;
 
        ident = getpid() & 0xFFFF;
@@ -1006,8 +1006,6 @@ main(int argc, char *const *argv)
                }
                if (n == 0 || options & F_FLOOD) {
                        if (sweepmax && sntransmitted == snpackets) {
-                               for (i = 0; i < sweepincr ; ++i) 
-                                       *datap++ = i;
                                datalen += sweepincr;
                                if (datalen > sweepmax)
                                        break;
@@ -1035,8 +1033,12 @@ main(int argc, char *const *argv)
                                nmissedmax = ntransmitted - nreceived - 1;
                                if (options & F_MISSED)
                                        (void)write(STDOUT_FILENO, &BBELL, 1);
-                               if (!(options & F_QUIET))
-                                       printf("Request timeout for icmp_seq %ld\n", ntransmitted - 2);
+                               if (!(options & F_QUIET)) {
+                                       printf("Request timeout for icmp_seq %u\n",
+                                              (uint16_t)(ntransmitted - 2));
+                                       if (!(options & F_FLOOD))
+                                               (void)fflush(stdout);
+                               }
                        }
                }
        }
@@ -1254,6 +1256,17 @@ pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv,
                if (options & F_FLOOD)
                        (void)write(STDOUT_FILENO, &BSPACE, 1);
                else {
+                       int seq_sent_len = send_len;
+                       int seq_datalen = datalen;
+                       
+                       if (sweepmax != 0) {
+                               /*
+                                * When sweeping take in account the length of that
+                                * was sent based on the sequence number
+                                */
+                               seq_datalen = sweepmin + (seq / snpackets) * sweepincr;
+                               seq_sent_len = icmp_len + seq_datalen;
+                       }
                        (void)printf("%d bytes from %s: icmp_seq=%u", cc,
                           inet_ntoa(*(struct in_addr *)&from->sin_addr.s_addr),
                           seq);
@@ -1278,10 +1291,10 @@ pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv,
                                (void)printf(" tsr=%s", pr_ntime(icp->icmp_rtime));
                                (void)printf(" tst=%s", pr_ntime(icp->icmp_ttime));
                        }
-                       if (recv_len != send_len) {
+                       if (recv_len != seq_sent_len) {
                                (void)printf(
                                     "\nwrong total length %d instead of %d",
-                                    recv_len, send_len);
+                                    recv_len, seq_sent_len);
                        }
                        /* check the data */
                        cp = (u_char*)&icp->icmp_data[phdr_len];
@@ -1294,21 +1307,21 @@ pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv,
                                cc -= TIMEVAL_LEN;
                                i += TIMEVAL_LEN;
                        }
-                       for (; i < datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
+                       for (; i < seq_datalen && cc > 0; ++i, ++cp, ++dp, --cc) {
                                if (*cp != *dp) {
        (void)printf("\nwrong data byte #%d should be 0x%x but was 0x%x",
            i, *dp, *cp);
                                        (void)printf("\ncp:");
                                        cp = (u_char*)&icp->icmp_data[0];
-                                       for (i = 0; i < datalen; ++i, ++cp) {
-                                               if ((i % 16) == 8)
+                                       for (i = 0; i < seq_datalen; ++i, ++cp) {
+                                               if ((i % 16) == 0)
                                                        (void)printf("\n\t");
                                                (void)printf("%2x ", *cp);
                                        }
                                        (void)printf("\ndp:");
                                        cp = &outpack[ICMP_MINLEN];
-                                       for (i = 0; i < datalen; ++i, ++cp) {
-                                               if ((i % 16) == 8)
+                                       for (i = 0; i < seq_datalen; ++i, ++cp) {
+                                               if ((i % 16) == 0)
                                                        (void)printf("\n\t");
                                                (void)printf("%2x ", *cp);
                                        }
@@ -1849,6 +1862,42 @@ fill(char *bp, char *patp)
        }
 }
 
+uint32_t
+str2svc(const char *str)
+{
+       uint32_t svc;
+       char *endptr;
+       
+       if (str == NULL || *str == '\0')
+               svc = UINT32_MAX;
+       else if (strcasecmp(str, "BK_SYS") == 0)
+               return SO_TC_BK_SYS;
+       else if (strcasecmp(str, "BK") == 0)
+               return SO_TC_BK;
+       else if (strcasecmp(str, "BE") == 0)
+               return SO_TC_BE;
+       else if (strcasecmp(str, "RD") == 0)
+               return SO_TC_RD;
+       else if (strcasecmp(str, "OAM") == 0)
+               return SO_TC_OAM;
+       else if (strcasecmp(str, "AV") == 0)
+               return SO_TC_AV;
+       else if (strcasecmp(str, "RV") == 0)
+               return SO_TC_RV;
+       else if (strcasecmp(str, "VI") == 0)
+               return SO_TC_VI;
+       else if (strcasecmp(str, "VO") == 0)
+               return SO_TC_VO;
+       else if (strcasecmp(str, "CTL") == 0)
+               return SO_TC_CTL;
+       else {
+               svc = strtoul(str, &endptr, 0);
+               if (*endptr != '\0')
+                       svc = UINT32_MAX;
+       }
+       return (svc);
+}
+
 #if defined(IPSEC) && defined(IPSEC_POLICY_IPSEC)
 #define        SECOPT          " [-P policy]"
 #else
@@ -1858,13 +1907,14 @@ static void
 usage(void)
 {
 
-       (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
-"usage: ping [-AaDdfnoQqRrv] [-b boundif] [-c count] [-G sweepmaxsize] [-g sweepminsize]",
-"            [-h sweepincrsize] [-i wait] [-l preload] [-M mask | time] [-m ttl]",
-"           " SECOPT " [-p pattern] [-S src_addr] [-s packetsize] [-t timeout]",
-"            [-W waittime] [-z tos] host",
-"       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]",
-"            [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
+       (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
+"usage: ping [-AaDdfnoQqRrv] [-b boundif] [-c count] [-G sweepmaxsize]",
+"            [-g sweepminsize] [-h sweepincrsize] [-i wait] [−k trafficclass]",
+"            [-l preload] [-M mask | time] [-m ttl]" SECOPT " [-p pattern]",
+"            [-S src_addr] [-s packetsize] [-t timeout][-W waittime] [-z tos]",
+"            host",
+"       ping [-AaDdfLnoQqRrv] [-b boundif] [-c count] [-I iface] [-i wait]",
+"            [−k trafficclass] [-l preload] [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]",
 "            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]",
 "            [-z tos] mcast-group");
        exit(EX_USAGE);