Module Name:    src
Committed By:   christos
Date:           Sat Nov 29 14:48:42 UTC 2014

Modified Files:
        src/sbin/ping: ping.c

Log Message:
Adjust default packet size to 56 data bytes (64 total).
Make error messages consistent.


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/sbin/ping/ping.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sbin/ping/ping.c
diff -u src/sbin/ping/ping.c:1.108 src/sbin/ping/ping.c:1.109
--- src/sbin/ping/ping.c:1.108	Thu Nov 27 14:43:58 2014
+++ src/sbin/ping/ping.c	Sat Nov 29 09:48:42 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: ping.c,v 1.108 2014/11/27 19:43:58 christos Exp $	*/
+/*	$NetBSD: ping.c,v 1.109 2014/11/29 14:48:42 christos Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -58,7 +58,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: ping.c,v 1.108 2014/11/27 19:43:58 christos Exp $");
+__RCSID("$NetBSD: ping.c,v 1.109 2014/11/29 14:48:42 christos Exp $");
 #endif
 
 #include <stdio.h>
@@ -252,12 +252,12 @@ main(int argc, char *argv[])
 #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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 			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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 			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 @@ main(int argc, char *argv[])
 		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 @@ main(int argc, char *argv[])
 	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;
@@ -470,7 +480,7 @@ main(int argc, char *argv[])
 
 	packlen = datalen + 60 + 76;	/* MAXIP + MAXICMP */
 	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();
@@ -525,9 +535,9 @@ main(int argc, char *argv[])
 				 - 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;
@@ -544,25 +554,25 @@ main(int argc, char *argv[])
 			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
@@ -572,27 +582,29 @@ main(int argc, char *argv[])
 		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
@@ -646,7 +658,7 @@ main(int argc, char *argv[])
 	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
@@ -736,7 +748,7 @@ doit(void)
 				if (errno == EINTR)
 					continue;
 				jiggle_flush(1);
-				err(1, "poll");
+				err(EXIT_FAILURE, "poll");
 			}
 			continue;
 		}
@@ -852,9 +864,9 @@ pinger(void)
 		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,
@@ -869,9 +881,9 @@ pinger(void)
 				warn("failed to clear cached route");
 		}
 		sw = 1;
-		if (prog_setsockopt(sloop,IPPROTO_IP,IP_HDRINCL,
+		if (prog_setsockopt(sloop, IPPROTO_IP, IP_HDRINCL,
 			       (char *)&sw, sizeof(sw)) < 0)
-			err(1, "Can't set special IP header");
+			err(EXIT_FAILURE, "Can't set special IP header");
 		
 		(void)clock_gettime(CLOCK_MONOTONIC, &clear_cache);
 	}
@@ -1797,7 +1809,7 @@ fill(void)
 	}
 	if (cp == fill_pat || *cp != '\0' || (cp-fill_pat) > 16*2) {
 		(void)fflush(stdout);
-		errx(1, "\"-p %s\": patterns must be specified with"
+		errx(EXIT_FAILURE, "\"-p %s\": patterns must be specified with"
 		     " 1-32 hex digits\n",
 		     fill_pat);
 	}
@@ -1861,8 +1873,7 @@ gethost(const char *arg,
 				hp = 0;
 			else
 				hp = gethostbyaddr((char *)&sa->sin_addr,
-						   sizeof(sa->sin_addr),
-						   AF_INET);
+				    sizeof(sa->sin_addr), AF_INET);
 			(void)strlcpy(realname, hp ? hp->h_name : name,
 			    realname_len);
 		}
@@ -1871,10 +1882,11 @@ gethost(const char *arg,
 	
 	hp = gethostbyname(name);
 	if (!hp)
-		errx(1, "Cannot resolve \"%s\" (%s)",name,hstrerror(h_errno));
+		errx(EXIT_FAILURE, "Cannot resolve \"%s\" (%s)",
+		    name, hstrerror(h_errno));
 
 	if (hp->h_addrtype != AF_INET)
-		errx(1, "%s only supported with IP", arg);
+		errx(EXIT_FAILURE, "%s only supported with IP", arg);
 
 	(void)memcpy(&sa->sin_addr, hp->h_addr, sizeof(sa->sin_addr));
 

Reply via email to