]> 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 7227bd42a8c9298cfcc6cd10e9218571d0dda168..fa79b60c58e089cb1d15f953b6c39ff60891675c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 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>
-#ifndef __APPLE__
-__FBSDID("$FreeBSD: src/sbin/ping/ping.c,v 1.112 2007/07/01 12:08:06 gnn Exp $");
-#endif
-       
 /*
  *                     P I N G . C
  *
@@ -208,7 +200,7 @@ char boundifname[IFNAMSIZ];
 #endif /* IP_FORCE_OUT_IFP */
 int nocell;
 int how_traffic_class = 0;
-int traffic_class = -1;
+int traffic_class = SO_TC_CTL; /* use control class, by default */
 int no_dup = 0;
 
 /* counters */
@@ -226,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 */
@@ -250,13 +243,12 @@ static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *, int);
 static void pr_retip(struct ip *);
 static void status(int);
 static void stopit(int);
-static void tvsub(struct timeval *, struct timeval *);
+static void tvsub(struct timeval *, const struct timeval *);
+static uint32_t str2svc(const char *);
 static void usage(void) __dead2;
 
 int
-main(argc, argv)
-       int argc;
-       char *const *argv;
+main(int argc, char *const *argv)
 {
        struct sockaddr_in from, sock_in;
        struct in_addr ifaddr;
@@ -266,7 +258,7 @@ main(argc, argv)
        struct msghdr msg;
        struct sigaction si_sa;
        size_t sz;
-       u_char *datap, packet[IP_MAXPACKET];
+       u_char *datap, packet[IP_MAXPACKET] __attribute__((aligned(4)));
        char *ep, *source, *target, *payload;
        struct hostent *hp;
 #ifdef IPSEC_POLICY_IPSEC
@@ -275,7 +267,7 @@ main(argc, 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];
@@ -300,7 +292,8 @@ main(argc, argv)
                s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
        sockerrno = errno;
 
-       setuid(getuid());
+       if (setuid(getuid()) != 0)
+               err(EX_NOPERM, "setuid() failed");
        uid = getuid();
 
        alarmtimeout = df = preload = tos = 0;
@@ -422,14 +415,17 @@ main(argc, argv)
                                    optarg);
                        options |= F_INTERVAL;
                        interval = (int)t;
-                       if (uid && interval < 1000) {
+                       if (uid && interval < 100) {
                                errno = EPERM;
                                err(EX_NOPERM, "-i interval too short");
                        }
                        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;
@@ -686,7 +682,7 @@ main(argc, 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;
@@ -696,6 +692,8 @@ main(argc, argv)
                err(EX_OSERR, "socket");
        }
        hold = 1;
+       (void) setsockopt(s, SOL_SOCKET, SO_RECV_ANYIF, (char *)&hold,
+           sizeof(hold));
        if (ifscope != 0) {
                if (setsockopt(s, IPPROTO_IP, IP_BOUND_IF,
                    (char *)&ifscope, sizeof (ifscope)) != 0)
@@ -719,14 +717,14 @@ main(argc, argv)
        if (options & F_SO_DONTROUTE)
                (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold,
                    sizeof(hold));
-       if (how_traffic_class == 1 && traffic_class > 0) {
-               (void)setsockopt(s, SOL_SOCKET, SO_TRAFFIC_CLASS, (void *)&traffic_class,
-                                                sizeof(traffic_class));
-       } 
+       if (how_traffic_class < 2 && traffic_class >= 0) {
+               (void) setsockopt(s, SOL_SOCKET, SO_TRAFFIC_CLASS,
+                   (void *)&traffic_class, sizeof (traffic_class));
+       }
        if (how_traffic_class > 0) {
                int on = 1;
-               (void)setsockopt(s, SOL_SOCKET, SO_RECV_TRAFFIC_CLASS, (void *)&on,
-                                                sizeof(on));
+               (void) setsockopt(s, SOL_SOCKET, SO_RECV_TRAFFIC_CLASS,
+                   (void *)&on, sizeof (on));
        }
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
@@ -894,17 +892,21 @@ main(argc, argv)
        if (sigaction(SIGINT, &si_sa, 0) == -1) {
                err(EX_OSERR, "sigaction SIGINT");
        }
-
+       si_sa.sa_handler = stopit;
+       if (sigaction(SIGQUIT, &si_sa, 0) == -1) {
+               err(EX_OSERR, "sigaction SIGQUIT");
+       }
+    
        si_sa.sa_handler = status;
        if (sigaction(SIGINFO, &si_sa, 0) == -1) {
-               err(EX_OSERR, "sigaction");
+               err(EX_OSERR, "sigaction SIGINFO");
        }
 
-        if (alarmtimeout > 0) {
+       if (alarmtimeout > 0) {
                si_sa.sa_handler = stopit;
                if (sigaction(SIGALRM, &si_sa, 0) == -1)
                        err(EX_OSERR, "sigaction SIGALRM");
-        }
+       }
 
        bzero(&msg, sizeof(msg));
        msg.msg_name = (caddr_t)&from;
@@ -977,7 +979,7 @@ main(argc, argv)
                                continue;
                        }
                        for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
-       #ifdef SO_TIMESTAMP
+#ifdef SO_TIMESTAMP
                                if (cmsg->cmsg_level == SOL_SOCKET &&
                                        cmsg->cmsg_type == SCM_TIMESTAMP &&
                                        cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) {
@@ -985,7 +987,7 @@ main(argc, argv)
                                        memcpy(&now, CMSG_DATA(cmsg), sizeof(now));
                                        tv = &now;
                                }
-       #endif
+#endif
                                if (cmsg->cmsg_level == SOL_SOCKET &&
                                        cmsg->cmsg_type == SO_TRAFFIC_CLASS &&
                                        cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
@@ -1004,8 +1006,6 @@ main(argc, 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;
@@ -1033,8 +1033,12 @@ main(argc, 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);
+                               }
                        }
                }
        }
@@ -1050,8 +1054,7 @@ main(argc, argv)
  * to be called from a signal handler.
  */
 void
-stopit(sig)
-       int sig __unused;
+stopit(int sig __unused)
 {
 
        /*
@@ -1117,7 +1120,7 @@ pinger(void)
                ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
                packet = outpackhdr;
        }
-       if (how_traffic_class == 2) {
+       if (how_traffic_class > 1 && traffic_class >= 0) {
                struct msghdr msg;
                struct iovec iov;
                char *cmbuf[CMSG_SPACE(sizeof(int))];
@@ -1171,12 +1174,8 @@ pinger(void)
  * program to be run without having intermingled output (or statistics!).
  */
 static void
-pr_pack(buf, cc, from, tv, tc)
-       char *buf;
-       int cc;
-       struct sockaddr_in *from;
-       struct timeval *tv;
-       int tc;
+pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv,
+    int tc)
 {
        struct in_addr ina;
        u_char *cp, *dp;
@@ -1257,6 +1256,17 @@ pr_pack(buf, cc, from, tv, tc)
                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);
@@ -1281,10 +1291,10 @@ pr_pack(buf, cc, from, tv, tc)
                                (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];
@@ -1297,21 +1307,21 @@ pr_pack(buf, cc, from, tv, tc)
                                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);
                                        }
@@ -1444,9 +1454,7 @@ pr_pack(buf, cc, from, tv, tc)
  *     Checksum routine for Internet Protocol family headers (C Version)
  */
 u_short
-in_cksum(addr, len)
-       u_short *addr;
-       int len;
+in_cksum(u_short *addr, int len)
 {
        int nleft, sum;
        u_short *w;
@@ -1490,8 +1498,7 @@ in_cksum(addr, len)
  * be >= in.
  */
 static void
-tvsub(out, in)
-       struct timeval *out, *in;
+tvsub(struct timeval *out, const struct timeval *in)
 {
 
        if ((out->tv_usec -= in->tv_usec) < 0) {
@@ -1507,15 +1514,14 @@ tvsub(out, in)
  */
 
 static void
-status(sig)
-       int sig __unused;
+status(int sig __unused)
 {
 
        siginfo_p = 1;
 }
 
 static void
-check_status()
+check_status(void)
 {
 
        if (siginfo_p) {
@@ -1535,7 +1541,7 @@ check_status()
  *     Print out statistics, and give up.
  */
 static void
-finish()
+finish(void)
 {
 
        (void)signal(SIGINT, SIG_IGN);
@@ -1594,8 +1600,7 @@ static char *ttab[] = {
  *     Print a descriptive string about an ICMP header.
  */
 static void
-pr_icmph(icp)
-       struct icmp *icp;
+pr_icmph(struct icmp *icp)
 {
 
        switch(icp->icmp_type) {
@@ -1742,8 +1747,7 @@ pr_icmph(icp)
  *     Print an IP header with options.
  */
 static void
-pr_iph(ip)
-       struct ip *ip;
+pr_iph(struct ip *ip)
 {
        u_char *cp;
        int hlen;
@@ -1775,8 +1779,7 @@ pr_iph(ip)
  * a hostname.
  */
 static char *
-pr_addr(ina)
-       struct in_addr ina;
+pr_addr(struct in_addr ina)
 {
        struct hostent *hp;
        static char buf[16 + 3 + MAXHOSTNAMELEN];
@@ -1795,8 +1798,7 @@ pr_addr(ina)
  *     Dump some info on a returned (via ICMP) IP packet.
  */
 static void
-pr_retip(ip)
-       struct ip *ip;
+pr_retip(struct ip *ip)
 {
        u_char *cp;
        int hlen;
@@ -1814,7 +1816,7 @@ pr_retip(ip)
 }
 
 static char *
-pr_ntime (n_time timestamp)
+pr_ntime(n_time timestamp)
 {
        static char buf[10];
        int hour, min, sec;
@@ -1830,8 +1832,7 @@ pr_ntime (n_time timestamp)
 }
 
 static void
-fill(bp, patp)
-       char *bp, *patp;
+fill(char *bp, char *patp)
 {
        char *cp;
        int pat[16];
@@ -1861,22 +1862,59 @@ fill(bp, 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
 #define        SECOPT          ""
 #endif
 static void
-usage()
+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);