X-Git-Url: https://git.saurik.com/apple/network_cmds.git/blobdiff_plain/2b484d24084b903459c5b416c06cd77b48c748b9..342c141ecf761e55ea0f23a89d835a8544df5f2e:/ping.tproj/ping.c diff --git a/ping.tproj/ping.c b/ping.tproj/ping.c index 3b1b89c..7701b7b 100644 --- a/ping.tproj/ping.c +++ b/ping.tproj/ping.c @@ -1,3 +1,30 @@ +/* + * Copyright (c) 1999-2013 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. @@ -30,22 +57,14 @@ * SUCH DAMAGE. */ -#if 0 +#include + #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 -#ifndef __APPLE__ -__FBSDID("$FreeBSD: /repoman/r/ncvs/src/sbin/ping/ping.c,v 1.105 2004/08/14 17:46:10 stefanf Exp $"); -#endif - /* * P I N G . C * @@ -76,6 +95,7 @@ __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sbin/ping/ping.c,v 1.105 2004/08/14 17:4 #include #include #include +#include #ifdef IPSEC #include @@ -92,9 +112,10 @@ __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sbin/ping/ping.c,v 1.105 2004/08/14 17:4 #include #include #include +#include #define INADDR_LEN ((int)sizeof(in_addr_t)) -#define TIMEVAL_LEN ((int)sizeof(struct timeval)) +#define TIMEVAL_LEN ((int)sizeof(struct tv32)) #define MASK_LEN (ICMP_MASKLEN - ICMP_MINLEN) #define TS_LEN (ICMP_TSLEN - ICMP_MINLEN) #define DEFDATALEN 56 /* default data length */ @@ -102,7 +123,7 @@ __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sbin/ping/ping.c,v 1.105 2004/08/14 17:4 /* runs out of buffer space */ #define MAXIPLEN (sizeof(struct ip) + MAX_IPOPTLEN) #define MAXICMPLEN (ICMP_ADVLENMIN + MAX_IPOPTLEN) -#define MAXWAIT 10 /* max seconds to wait for response */ +#define MAXWAIT 10000 /* max ms to wait for response */ #define MAXALARM (60 * 60) /* max seconds for alarm timeout */ #define MAXTOS 255 @@ -112,6 +133,11 @@ __FBSDID("$FreeBSD: /repoman/r/ncvs/src/sbin/ping/ping.c,v 1.105 2004/08/14 17:4 #define CLR(bit) (A(bit) &= (~B(bit))) #define TST(bit) (A(bit) & B(bit)) +struct tv32 { + u_int32_t tv32_sec; + u_int32_t tv32_usec; +}; + /* various options */ int options; #define F_FLOOD 0x0001 @@ -139,6 +165,8 @@ int options; #define F_HDRINCL 0x40000 #define F_MASK 0x80000 #define F_TIME 0x100000 +#define F_SWEEP 0x200000 +#define F_WAITTIME 0x400000 /* * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum @@ -165,6 +193,15 @@ u_char icmp_type = ICMP_ECHO; u_char icmp_type_rsp = ICMP_ECHOREPLY; int phdr_len = 0; int send_len; +char *boundif; +unsigned int ifscope; +#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED +char boundifname[IFNAMSIZ]; +#endif /* IP_FORCE_OUT_IFP */ +int nocell; +int how_traffic_class = 0; +int traffic_class = SO_TC_CTL; /* use control class, by default */ +int no_dup = 0; /* counters */ long nmissedmax; /* max value of ntransmitted - nreceived - 1 */ @@ -172,7 +209,15 @@ long npackets; /* max packets to transmit */ long nreceived; /* # of packets we got back */ long nrepeats; /* number of duplicates */ long ntransmitted; /* sequence # for outbound packets = #sent */ +long snpackets; /* max packets to transmit in one sweep */ +long snreceived; /* # of packets we got back in this sweep */ +long sntransmitted; /* # of packets we sent in this sweep */ +int sweepmax; /* max value of payload in sweep */ +int sweepmin = 0; /* start value of payload in sweep */ +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 */ /* timing */ int timing; /* flag to do timing */ @@ -193,17 +238,16 @@ static char *pr_addr(struct in_addr); static char *pr_ntime(n_time); static void pr_icmph(struct icmp *); static void pr_iph(struct ip *); -static void pr_pack(char *, int, struct sockaddr_in *, struct timeval *); +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; @@ -213,7 +257,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 @@ -224,7 +268,7 @@ main(argc, argv) u_long alarmtimeout, ultmp; int almost_done, ch, df, hold, i, icmp_len, mib[4], preload, sockerrno, tos, ttl; - char ctrl[CMSG_SPACE(sizeof(struct timeval))]; + char ctrl[CMSG_SPACE(sizeof(struct timeval)) + CMSG_SPACE(sizeof(int))]; char hnamebuf[MAXHOSTNAMELEN], snamebuf[MAXHOSTNAMELEN]; #ifdef IP_OPTIONS char rspace[MAX_IPOPTLEN]; /* record route space */ @@ -241,22 +285,29 @@ main(argc, argv) * then drop our setuid bit. Save error reporting for * after arg parsing. */ - s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + if (getuid()) + s = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP); + else + 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; outpack = outpackhdr + sizeof(struct ip); while ((ch = getopt(argc, argv, - "Aac:DdfI:i:Ll:M:m:nop:QqRrS:s:T:t:vz:" + "Aab:Cc:DdfG:g:h:I:i:k:Ll:M:m:nop:QqRrS:s:T:t:vW:z:" #ifdef IPSEC #ifdef IPSEC_POLICY_IPSEC "P:" #endif /*IPSEC_POLICY_IPSEC*/ #endif /*IPSEC*/ +#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED + "B:" +#endif /* IP_FORCE_OUT_IFP */ )) != -1) { switch(ch) { @@ -266,6 +317,18 @@ main(argc, argv) case 'a': options |= F_AUDIBLE; break; +#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED + case 'B': + (void) snprintf(boundifname, sizeof (boundifname), + "%s", optarg); + break; +#endif /* IP_FORCE_OUT_IFP */ + case 'b': + boundif = optarg; + break; + case 'C': + nocell++; + break; case 'c': ultmp = strtoul(optarg, &ep, 0); if (*ep || ep == optarg || ultmp > LONG_MAX || !ultmp) @@ -289,6 +352,54 @@ main(argc, argv) options |= F_FLOOD; setbuf(stdout, (char *)NULL); break; + case 'G': /* Maximum packet size for ping sweep */ + ultmp = strtoul(optarg, &ep, 0); + if (*ep || ep == optarg) + errx(EX_USAGE, "invalid packet size: `%s'", + optarg); +#ifndef __APPLE__ + if (uid != 0 && ultmp > DEFDATALEN) { + errno = EPERM; + err(EX_NOPERM, + "packet size too large: %lu > %u", + ultmp, DEFDATALEN); + } +#endif /* __APPLE__ */ + options |= F_SWEEP; + sweepmax = ultmp; + break; + case 'g': /* Minimum packet size for ping sweep */ + ultmp = strtoul(optarg, &ep, 0); + if (*ep || ep == optarg) + errx(EX_USAGE, "invalid packet size: `%s'", + optarg); +#ifndef __APPLE__ + if (uid != 0 && ultmp > DEFDATALEN) { + errno = EPERM; + err(EX_NOPERM, + "packet size too large: %lu > %u", + ultmp, DEFDATALEN); + } +#endif /* __APPLE__ */ + options |= F_SWEEP; + sweepmin = ultmp; + break; + case 'h': /* Packet size increment for ping sweep */ + ultmp = strtoul(optarg, &ep, 0); + if (*ep || ep == optarg || ultmp < 1) + errx(EX_USAGE, "invalid increment size: `%s'", + optarg); +#ifndef __APPLE__ + if (uid != 0 && ultmp > DEFDATALEN) { + errno = EPERM; + err(EX_NOPERM, + "packet size too large: %lu > %u", + ultmp, DEFDATALEN); + } +#endif /* __APPLE__ */ + options |= F_SWEEP; + sweepincr = ultmp; + break; case 'I': /* multicast interface */ if (inet_aton(optarg, &ifaddr) == 0) errx(EX_USAGE, @@ -303,11 +414,18 @@ 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 = str2svc(optarg); + if (traffic_class == UINT32_MAX) + errx(EX_USAGE, "bad traffic class: `%s'", + optarg); + break; case 'L': options |= F_NOLOOP; loop = 0; @@ -395,7 +513,7 @@ main(argc, argv) "packet size too large: %lu > %u", ultmp, DEFDATALEN); } -#endif +#endif /* __APPLE__ */ datalen = ultmp; break; case 'T': /* multicast TTL */ @@ -414,11 +532,19 @@ main(argc, argv) if (alarmtimeout > MAXALARM) errx(EX_USAGE, "invalid timeout: `%s' > %d", optarg, MAXALARM); - alarm((int)alarmtimeout); + alarm((unsigned int)alarmtimeout); break; case 'v': options |= F_VERBOSE; break; + case 'W': /* wait ms for answer */ + t = strtod(optarg, &ep); + if (*ep || ep == optarg || t > (double)INT_MAX) + errx(EX_USAGE, "invalid timing interval: `%s'", + optarg); + options |= F_WAITTIME; + waittime = (int)t; + break; case 'z': options |= F_HDRINCL; ultmp = strtoul(optarg, &ep, 0); @@ -431,6 +557,9 @@ main(argc, argv) } } + if (boundif != NULL && (ifscope = if_nametoindex(boundif)) == 0) + errx(1, "bad interface name"); + if (argc - optind != 1) usage(); target = argv[optind]; @@ -513,6 +642,30 @@ main(argc, argv) hostname = hnamebuf; } + do { + struct ifaddrs *ifa_list, *ifa; + + if (IN_MULTICAST(ntohl(whereto.sin_addr.s_addr)) || whereto.sin_addr.s_addr == INADDR_BROADCAST) { + no_dup = 1; + break; + } + + if (getifaddrs(&ifa_list) == -1) + break; + for (ifa = ifa_list; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_INET) + continue; + if ((ifa->ifa_flags & IFF_BROADCAST) == 0 || ifa->ifa_broadaddr == NULL) + continue; + if (whereto.sin_addr.s_addr != ((struct sockaddr_in*)ifa->ifa_broadaddr)->sin_addr.s_addr) + continue; + no_dup = 1; + break; + } + + freeifaddrs(ifa_list); + } while (0); + if (options & F_FLOOD && options & F_INTERVAL) errx(EX_USAGE, "-f and -i: incompatible options"); @@ -538,12 +691,40 @@ 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) + err(EX_OSERR, "setsockopt(IP_BOUND_IF)"); + } +#if defined(IP_FORCE_OUT_IFP) && TARGET_OS_EMBEDDED + else if (boundifname[0] != 0) { + if (setsockopt(s, IPPROTO_IP, IP_FORCE_OUT_IFP, boundifname, + sizeof (boundifname)) != 0) + err(EX_OSERR, "setsockopt(IP_FORCE_OUT_IFP)"); + } +#endif /* IP_FORCE_OUT_IFP */ + if (nocell) { + if (setsockopt(s, IPPROTO_IP, IP_NO_IFT_CELLULAR, + (char *)&nocell, sizeof (nocell)) != 0) + err(EX_OSERR, "setsockopt(IP_NO_IFT_CELLULAR)"); + } if (options & F_SO_DEBUG) (void)setsockopt(s, SOL_SOCKET, SO_DEBUG, (char *)&hold, sizeof(hold)); if (options & F_SO_DONTROUTE) (void)setsockopt(s, SOL_SOCKET, SO_DONTROUTE, (char *)&hold, sizeof(hold)); + 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)); + } #ifdef IPSEC #ifdef IPSEC_POLICY_IPSEC if (options & F_POLICY) { @@ -642,6 +823,23 @@ main(argc, argv) err(EX_OSERR, "setsockopt SO_TIMESTAMP"); } #endif + if (sweepmax) { + if (sweepmin >= sweepmax) + errx(EX_USAGE, "Maximum packet size must be greater than the minimum packet size"); + + if (datalen != DEFDATALEN) + errx(EX_USAGE, "Packet size and ping sweep are mutually exclusive"); + + if (npackets > 0) { + snpackets = npackets; + npackets = 0; + } else + snpackets = 1; + datalen = sweepmin; + send_len = icmp_len + sweepmin; + } + if (options & F_SWEEP && !sweepmax) + errx(EX_USAGE, "Maximum sweep size must be specified"); /* * When pinging the broadcast address, you can get a lot of answers. @@ -667,9 +865,19 @@ main(argc, argv) inet_ntoa(to->sin_addr)); if (source) (void)printf(" from %s", shostname); - (void)printf(": %d data bytes\n", datalen); - } else - (void)printf("PING %s: %d data bytes\n", hostname, datalen); + if (sweepmax) + (void)printf(": (%d ... %d) data bytes\n", + sweepmin, sweepmax); + else + (void)printf(": %d data bytes\n", datalen); + + } else { + if (sweepmax) + (void)printf("PING %s: (%d ... %d) data bytes\n", + hostname, sweepmin, sweepmax); + else + (void)printf("PING %s: %d data bytes\n", hostname, datalen); + } /* * Use sigaction() instead of signal() to get unambiguous semantics, @@ -683,17 +891,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; @@ -728,6 +940,7 @@ main(argc, argv) struct timeval now, timeout; fd_set rfds; int cc, n; + int tc = -1; check_status(); if ((unsigned)s >= FD_SETSIZE) @@ -764,25 +977,42 @@ main(argc, argv) warn("recvmsg"); continue; } + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) { #ifdef SO_TIMESTAMP - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_TIMESTAMP && - cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { - /* Copy to avoid alignment problems: */ - memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); - tv = &now; - } + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SCM_TIMESTAMP && + cmsg->cmsg_len == CMSG_LEN(sizeof *tv)) { + /* Copy to avoid alignment problems: */ + memcpy(&now, CMSG_DATA(cmsg), sizeof(now)); + tv = &now; + } #endif + if (cmsg->cmsg_level == SOL_SOCKET && + cmsg->cmsg_type == SO_TRAFFIC_CLASS && + cmsg->cmsg_len == CMSG_LEN(sizeof(int))) { + /* Copy to avoid alignment problems: */ + memcpy(&tc, CMSG_DATA(cmsg), sizeof(tc)); + } + } if (tv == NULL) { (void)gettimeofday(&now, NULL); tv = &now; } - pr_pack((char *)packet, cc, &from, tv); + pr_pack((char *)packet, cc, &from, tv, tc); if ((options & F_ONCE && nreceived) || (npackets && nreceived >= npackets)) break; } if (n == 0 || options & F_FLOOD) { + if (sweepmax && sntransmitted == snpackets) { + for (i = 0; i < sweepincr ; ++i) + *datap++ = i; + datalen += sweepincr; + if (datalen > sweepmax) + break; + send_len = icmp_len + datalen; + sntransmitted = 0; + } if (!npackets || ntransmitted < npackets) pinger(); else { @@ -794,14 +1024,18 @@ main(argc, argv) intvl.tv_sec = 2 * tmax / 1000; if (!intvl.tv_sec) intvl.tv_sec = 1; - } else - intvl.tv_sec = MAXWAIT; + } else { + intvl.tv_sec = waittime / 1000; + intvl.tv_usec = waittime % 1000 * 1000; + } } (void)gettimeofday(&last, NULL); if (ntransmitted - nreceived - 1 > nmissedmax) { 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); } } } @@ -817,8 +1051,7 @@ main(argc, argv) * to be called from a signal handler. */ void -stopit(sig) - int sig __unused; +stopit(int sig __unused) { /* @@ -842,6 +1075,7 @@ static void pinger(void) { struct timeval now; + struct tv32 tv32; struct ip *ip; struct icmp *icp; int cc, i; @@ -860,13 +1094,15 @@ pinger(void) if ((options & F_TIME) || timing) { (void)gettimeofday(&now, NULL); + tv32.tv32_sec = htonl(now.tv_sec); + tv32.tv32_usec = htonl(now.tv_usec); if (options & F_TIME) icp->icmp_otime = htonl((now.tv_sec % (24*60*60)) * 1000 + now.tv_usec / 1000); if (timing) - bcopy((void *)&now, + bcopy((void *)&tv32, (void *)&outpack[ICMP_MINLEN + phdr_len], - sizeof(struct timeval)); + sizeof(tv32)); } cc = ICMP_MINLEN + phdr_len + datalen; @@ -881,9 +1117,34 @@ pinger(void) ip->ip_sum = in_cksum((u_short *)outpackhdr, cc); packet = outpackhdr; } - i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, - sizeof(whereto)); - + if (how_traffic_class > 1 && traffic_class >= 0) { + struct msghdr msg; + struct iovec iov; + char *cmbuf[CMSG_SPACE(sizeof(int))]; + struct cmsghdr *cm = (struct cmsghdr *)cmbuf; + + msg.msg_name = &whereto; + msg.msg_namelen = sizeof(whereto); + + iov.iov_base = packet; + iov.iov_len = cc; + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + + cm->cmsg_len = CMSG_LEN(sizeof(int)); + cm->cmsg_level = SOL_SOCKET; + cm->cmsg_type = SO_TRAFFIC_CLASS; + *(int *)CMSG_DATA(cm) = traffic_class; + msg.msg_control = cm; + msg.msg_controllen = CMSG_SPACE(sizeof(int)); + + msg.msg_flags = 0; + + i = sendmsg(s, &msg, 0); + } else { + i = sendto(s, (char *)packet, cc, 0, (struct sockaddr *)&whereto, + sizeof(whereto)); + } if (i < 0 || i != cc) { if (i < 0) { if (options & F_FLOOD && errno == ENOBUFS) { @@ -897,6 +1158,7 @@ pinger(void) } } ntransmitted++; + sntransmitted++; if (!(options & F_QUIET) && options & F_FLOOD) (void)write(STDOUT_FILENO, &DOT, 1); } @@ -909,11 +1171,8 @@ pinger(void) * program to be run without having intermingled output (or statistics!). */ static void -pr_pack(buf, cc, from, tv) - char *buf; - int cc; - struct sockaddr_in *from; - struct timeval *tv; +pr_pack(char *buf, int cc, struct sockaddr_in *from, struct timeval *tv, + int tc) { struct in_addr ina; u_char *cp, *dp; @@ -946,6 +1205,7 @@ pr_pack(buf, cc, from, tv) triptime = 0.0; if (timing) { struct timeval tv1; + struct tv32 tv32; #ifndef icmp_data tp = &icp->icmp_ip; #else @@ -955,7 +1215,9 @@ pr_pack(buf, cc, from, tv) if (cc - ICMP_MINLEN - phdr_len >= sizeof(tv1)) { /* Copy to avoid alignment problems: */ - memcpy(&tv1, tp, sizeof(tv1)); + memcpy(&tv32, tp, sizeof(tv32)); + tv1.tv_sec = ntohl(tv32.tv32_sec); + tv1.tv_usec = ntohl(tv32.tv32_usec); tvsub(tv, &tv1); triptime = ((double)tv->tv_sec) * 1000.0 + ((double)tv->tv_usec) / 1000.0; @@ -982,6 +1244,11 @@ pr_pack(buf, cc, from, tv) if (options & F_QUIET) return; + + if (options & F_WAITTIME && triptime > waittime) { + ++nrcvtimeout; + return; + } if (options & F_FLOOD) (void)write(STDOUT_FILENO, &BSPACE, 1); @@ -992,8 +1259,12 @@ pr_pack(buf, cc, from, tv) (void)printf(" ttl=%d", ip->ip_ttl); if (timing) (void)printf(" time=%.3f ms", triptime); - if (dupflag) + if (tc != -1) { + (void)printf(" tc=%d", tc); + } + if (dupflag && no_dup == 0) { (void)printf(" (DUP!)"); + } if (options & F_AUDIBLE) (void)write(STDOUT_FILENO, &BBELL, 1); if (options & F_MASK) { @@ -1169,9 +1440,7 @@ pr_pack(buf, cc, from, tv) * 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; @@ -1215,8 +1484,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) { @@ -1232,20 +1500,19 @@ 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) { siginfo_p = 0; - (void)fprintf(stderr, "\r%ld/%ld packets received (%.0f%%)", + (void)fprintf(stderr, "\r%ld/%ld packets received (%.1f%%)", nreceived, ntransmitted, ntransmitted ? nreceived * 100.0 / ntransmitted : 0.0); if (nreceived && timing) @@ -1260,7 +1527,7 @@ check_status() * Print out statistics, and give up. */ static void -finish() +finish(void) { (void)signal(SIGINT, SIG_IGN); @@ -1276,10 +1543,12 @@ finish() if (nreceived > ntransmitted) (void)printf("-- somebody's printing up packets!"); else - (void)printf("%d%% packet loss", - (int)(((ntransmitted - nreceived) * 100) / - ntransmitted)); + (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; @@ -1317,8 +1586,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) { @@ -1465,8 +1733,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; @@ -1498,8 +1765,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]; @@ -1518,8 +1784,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; @@ -1537,7 +1802,7 @@ pr_retip(ip) } static char * -pr_ntime (n_time timestamp) +pr_ntime(n_time timestamp) { static char buf[10]; int hour, min, sec; @@ -1553,8 +1818,7 @@ pr_ntime (n_time timestamp) } static void -fill(bp, patp) - char *bp, *patp; +fill(char *bp, char *patp) { char *cp; int pat[16]; @@ -1584,21 +1848,60 @@ 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", -"usage: ping [-AaDdfnoQqRrv] [-c count] [-i wait] [-l preload] [-M mask | time]", -" [-m ttl]" SECOPT " [-p pattern] [-S src_addr] [-s packetsize]", -" [-t timeout] [-z tos] host", -" ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait] [-l preload]", -" [-M mask | time] [-m ttl]" SECOPT " [-p pattern] [-S src_addr]", -" [-s packetsize] [-T ttl] [-t timeout] [-z tos] mcast-group"); + (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); }