Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7]: src/sbin/ping Pull up following revision(s) (requested by chr...



details:   https://anonhg.NetBSD.org/src/rev/332a1ec19d90
branches:  netbsd-7
changeset: 799205:332a1ec19d90
user:      snj <snj%NetBSD.org@localhost>
date:      Tue Apr 14 05:26:20 2015 +0000

description:
Pull up following revision(s) (requested by christos in ticket #692):
        sbin/ping/ping.c: revisions 1.108, 1.109
PR/49423: Martin Husemann: ping for small packets does not work in -7 or
-current
--
Adjust default packet size to 56 data bytes (64 total).
Make error messages consistent.

diffstat:

 sbin/ping/ping.c |  119 ++++++++++++++++++++++++++++++------------------------
 1 files changed, 65 insertions(+), 54 deletions(-)

diffs (truncated from 381 to 300 lines):

diff -r 625e2359f85b -r 332a1ec19d90 sbin/ping/ping.c
--- a/sbin/ping/ping.c  Tue Apr 14 05:21:26 2015 +0000
+++ b/sbin/ping/ping.c  Tue Apr 14 05:26:20 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: ping.c,v 1.107 2013/10/19 01:08:25 christos Exp $      */
+/*     $NetBSD: ping.c,v 1.107.4.1 2015/04/14 05:26:20 snj Exp $       */
 
 /*
  * Copyright (c) 1989, 1993
@@ -58,7 +58,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ping.c,v 1.107 2013/10/19 01:08:25 christos Exp $");
+__RCSID("$NetBSD: ping.c,v 1.107.4.1 2015/04/14 05:26:20 snj Exp $");
 #endif
 
 #include <stdio.h>
@@ -252,12 +252,12 @@
 #endif
 
        if (prog_init && prog_init() == -1)
-               err(1, "init failed");
+               err(EXIT_FAILURE, "init failed");
 
        if ((s = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
-               err(1, "Cannot create socket");
+               err(EXIT_FAILURE, "Cannot create socket");
        if ((sloop = prog_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
-               err(1, "Cannot create socket");
+               err(EXIT_FAILURE, "Cannot create socket");
 
        /*
         * sloop is never read on.  This prevents packets from
@@ -267,7 +267,7 @@
                warn("Cannot shutdown for read");
 
        if (prog_setuid(prog_getuid()) == -1)
-               err(1, "setuid");
+               err(EXIT_FAILURE, "setuid");
 
        setprogname(argv[0]);
 
@@ -293,7 +293,9 @@
                case 'c':
                        npackets = strtol(optarg, &p, 0);
                        if (*p != '\0' || npackets <= 0)
-                               errx(1, "Bad/invalid number of packets");
+                               errx(EXIT_FAILURE,
+                                   "Bad/invalid number of packets: %s",
+                                   optarg);
                        break;
                case 'D':
                        pingflags |= F_DF;
@@ -310,13 +312,14 @@
                case 'i':               /* wait between sending packets */
                        interval = strtod(optarg, &p);
                        if (*p != '\0' || interval <= 0)
-                               errx(1, "Bad/invalid interval %s", optarg);
+                               errx(EXIT_FAILURE, "Bad/invalid interval: %s",
+                                   optarg);
                        break;
                case 'l':
                        preload = strtol(optarg, &p, 0);
                        if (*p != '\0' || preload < 0)
-                               errx(1, "Bad/invalid preload value %s",
-                                    optarg);
+                               errx(EXIT_FAILURE, "Bad/invalid preload value: "
+                                   "%s", optarg);
                        break;
                case 'n':
                        pingflags |= F_NUMERIC;
@@ -326,13 +329,15 @@
                        break;
                case 'p':               /* fill buffer with user pattern */
                        if (pingflags & F_PING_RANDOM)
-                               errx(1, "Only one of -P and -p allowed");
+                               errx(EXIT_FAILURE,
+                                   "Only one of -P and -p allowed");
                        pingflags |= F_PING_FILLED;
                        fill_pat = optarg;
                        break;
                case 'P':
                        if (pingflags & F_PING_FILLED)
-                               errx(1, "Only one of -P and -p allowed");
+                               errx(EXIT_FAILURE,
+                                   "Only one of -P and -p allowed");
                        pingflags |= F_PING_RANDOM;
                        break;
                case 'q':
@@ -347,9 +352,10 @@
                case 's':               /* size of packet to send */
                        l = strtol(optarg, &p, 0);
                        if (*p != '\0' || l < 0)
-                               errx(1, "Bad/invalid packet size %s", optarg);
+                               errx(EXIT_FAILURE,
+                                   "Bad/invalid packet size: %s", optarg);
                        if (l > MAXPACKET)
-                               errx(1, "packet size is too large");
+                               errx(EXIT_FAILURE, "packet size is too large");
                        len = (int)l;
                        break;
                case 'v':
@@ -364,12 +370,13 @@
                case 't':
                        tos = strtoul(optarg, &p, 0);
                        if (*p != '\0' ||  tos > 0xFF)
-                               errx(1, "bad tos value: %s", optarg);
+                               errx(EXIT_FAILURE, "bad tos value: %s", optarg);
                        break;
                case 'T':
                        l = strtol(optarg, &p, 0);
                        if (*p != '\0' || l > 255 || l <= 0)
-                               errx(1, "ttl out of range");
+                               errx(EXIT_FAILURE, "ttl out of range: %s",
+                                   optarg);
                        ttl = (u_char)l;    /* cannot check >255 otherwise */
                        break;
                case 'I':
@@ -383,7 +390,8 @@
                case 'w':
                        maxwait = strtod(optarg, &p);
                        if (*p != '\0' || maxwait <= 0)
-                               errx(1, "Bad/invalid maxwait time %s", optarg);
+                               errx(EXIT_FAILURE, "Bad/invalid maxwait time: "
+                                   "%s", optarg);
                        break;
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
@@ -392,13 +400,14 @@
                        if (!strncmp("in", optarg, 2)) {
                                policy_in = strdup(optarg);
                                if (!policy_in)
-                                       err(1, "strdup");
+                                       err(EXIT_FAILURE, "strdup");
                        } else if (!strncmp("out", optarg, 3)) {
                                policy_out = strdup(optarg);
                                if (!policy_out)
-                                       err(1, "strdup");
+                                       err(EXIT_FAILURE, "strdup");
                        } else
-                               errx(1, "invalid security policy");
+                               errx(EXIT_FAILURE, "invalid security policy: "
+                                   "%s", optarg);
                        break;
 #else
                case 'A':
@@ -419,11 +428,12 @@
                interval = (pingflags & F_FLOOD) ? FLOOD_INTVL : 1.0;
 #ifndef sgi
        if (pingflags & F_FLOOD && prog_getuid())
-               errx(1, "Must be superuser to use -f");
+               errx(EXIT_FAILURE, "Must be superuser to use -f");
        if (interval < 1.0 && prog_getuid())
-               errx(1, "Must be superuser to use < 1 sec ping interval");
+               errx(EXIT_FAILURE, "Must be superuser to use < 1 sec "
+                   "ping interval");
        if (preload > 0 && prog_getuid())
-               errx(1, "Must be superuser to use -l");
+               errx(EXIT_FAILURE, "Must be superuser to use -l");
 #endif
        sec_to_timespec(interval, &interval_tv);
 
@@ -458,7 +468,7 @@
        if (len != -1)
                datalen = len;
        else
-               datalen = 64;
+               datalen = 64 - PHDR_LEN;
        if (!compat && datalen >= (int)PHDR64_LEN) { /* can we time them? */
                pingflags |= F_TIMING64;
                phdrlen = PHDR64_LEN;
@@ -469,9 +479,8 @@
                phdrlen = 0;
 
        packlen = datalen + 60 + 76;    /* MAXIP + MAXICMP */
-       datalen -= phdrlen;
        if ((packet = malloc(packlen)) == NULL)
-               err(1, "Out of memory");
+               err(EXIT_FAILURE, "Can't allocate %d bytes", packlen);
 
        if (pingflags & F_PING_FILLED) {
                fill();
@@ -526,9 +535,9 @@
                                 - optlen);
        (void) memcpy(opack_ip + 1, optspace, optlen);
 
-       if (prog_setsockopt(s,IPPROTO_IP,IP_HDRINCL,
+       if (prog_setsockopt(s, IPPROTO_IP, IP_HDRINCL,
            (char *) &on, sizeof(on)) < 0)
-               err(1, "Can't set special IP header");
+               err(EXIT_FAILURE, "Can't set special IP header");
 
        opack_ip->ip_v = IPVERSION;
        opack_ip->ip_hl = (sizeof(struct ip)+optlen) >> 2;
@@ -545,25 +554,25 @@
                        if (prog_setsockopt(s, IPPROTO_IP,
                            IP_MULTICAST_LOOP,
                            (char *) &loop, 1) < 0)
-                               err(1, "Can't disable multicast loopback");
+                               err(EXIT_FAILURE, "Can't disable multicast loopback");
                }
 
                if (ttl != 0
                    && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
                    (char *) &ttl, 1) < 0)
-                       err(1, "Can't set multicast time-to-live");
+                       err(EXIT_FAILURE, "Can't set multicast time-to-live");
 
                if ((pingflags & F_SOURCE_ADDR)
                    && prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
                                  (char *) &src_addr.sin_addr,
                                  sizeof(src_addr.sin_addr)) < 0)
-                       err(1, "Can't set multicast source interface");
+                       err(EXIT_FAILURE, "Can't set multicast source interface");
 
        } else if (pingflags & F_SOURCE_ADDR) {
                if (prog_setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF,
                               (char *) &src_addr.sin_addr,
                               sizeof(src_addr.sin_addr)) < 0)
-                       err(1, "Can't set source interface/address");
+                       err(EXIT_FAILURE, "Can't set source interface/address");
        }
 #ifdef IPSEC
 #ifdef IPSEC_POLICY_IPSEC
@@ -573,27 +582,29 @@
                if (policy_in != NULL) {
                        buf = ipsec_set_policy(policy_in, strlen(policy_in));
                        if (buf == NULL)
-                               errx(1, "%s", ipsec_strerror());
+                               errx(EXIT_FAILURE, "%s", ipsec_strerror());
                        if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
                                        buf, ipsec_get_policylen(buf)) < 0) {
-                               err(1, "ipsec policy cannot be configured");
+                               err(EXIT_FAILURE, "ipsec policy cannot be "
+                                   "configured");
                        }
                        free(buf);
                }
                if (policy_out != NULL) {
                        buf = ipsec_set_policy(policy_out, strlen(policy_out));
                        if (buf == NULL)
-                               errx(1, "%s", ipsec_strerror());
+                               errx(EXIT_FAILURE, "%s", ipsec_strerror());
                        if (prog_setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY,
                                        buf, ipsec_get_policylen(buf)) < 0) {
-                               err(1, "ipsec policy cannot be configured");
+                               err(EXIT_FAILURE, "ipsec policy cannot be "
+                                   "configured");
                        }
                        free(buf);
                }
        }
        buf = ipsec_set_policy("out bypass", strlen("out bypass"));
        if (buf == NULL)
-               errx(1, "%s", ipsec_strerror());
+               errx(EXIT_FAILURE, "%s", ipsec_strerror());
        if (prog_setsockopt(sloop, IPPROTO_IP, IP_IPSEC_POLICY,
                        buf, ipsec_get_policylen(buf)) < 0) {
 #if 0
@@ -637,7 +648,7 @@
 #endif /*IPSEC*/
 
        (void)printf("PING %s (%s): %d data bytes\n", hostname,
-                    inet_ntoa(whereto.sin_addr), datalen + phdrlen);
+                    inet_ntoa(whereto.sin_addr), datalen);
 
        /* When pinging the broadcast address, you can get a lot
         * of answers.  Doing something so evil is useful if you
@@ -647,7 +658,7 @@
        while (0 > prog_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
                              (char*)&bufspace, sizeof(bufspace))) {
                if ((bufspace -= 4096) <= 0)
-                       err(1, "Cannot set the receive buffer size");
+                       err(EXIT_FAILURE, "Cannot set the receive buffer size");
        }
 
        /* make it possible to send giant probes, but do not worry now
@@ -737,7 +748,7 @@
                                if (errno == EINTR)
                                        continue;
                                jiggle_flush(1);
-                               err(1, "poll");
+                               err(EXIT_FAILURE, "poll");
                        }
                        continue;
                }
@@ -853,9 +864,9 @@
                opack_icmp.icmp_cksum = in_cksum((u_int16_t *)&opack_icmp,
                    phdrlen);
                sw = 0;
-               if (prog_setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
-                              (char *)&sw,sizeof(sw)) < 0)
-                       err(1, "Can't turn off special IP header");
+               if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
+                              (char *)&sw, sizeof(sw)) < 0)
+                       err(EXIT_FAILURE, "Can't turn off special IP header");
                if (prog_sendto(sloop, (char *) &opack_icmp,
                           ICMP_MINLEN, MSG_DONTROUTE,
                           (struct sockaddr *)&loc_addr,



Home | Main Index | Thread Index | Old Index