Hello again,

there is an old bug report [1] filed with Debian, which makes much sense.
It is a wish that the call

    ping 10

should return an error, since no host can consist of a single octet,
and neither is an IPv4 address with initial octet 0 possible.

The following patch addresses this natural behaviour, which the default
`ping' of Debian GNU/Linux also complies with. I have by intention left
an outer conditional in place, which while not strictly necessary,
improves the readability of the code intentions.


Best regards,
Mats


[1] http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=323165
From bd64b0d72a5796a7617a9aa2ee67498b48221abd Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <[email protected]>
Date: Sat, 27 Nov 2010 19:09:24 +0100
Subject: [PATCH] ping: Discard single octets as host name.

---
 ChangeLog      |    5 +++++
 ping/libping.c |    9 +++++++++
 2 files changed, 14 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 9623bc7..edcbc68 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2010-11-27  Mats Erik Andersson <[email protected]>
 
+	* ping/libping.c (ping_set_dest): Detect a numerical host and
+	abort if it consists of a single octet.
+
+2010-11-27  Mats Erik Andersson <[email protected]>
+
 	* src/inetd.c (pidfile_option): New variable.
 	(pid_file): New variable.
 	(argp_options): Add an option for replacing path of PID file.
diff --git a/ping/libping.c b/ping/libping.c
index 52590ba..3bff875 100644
--- a/ping/libping.c
+++ b/ping/libping.c
@@ -260,6 +260,15 @@ ping_set_dest (PING * ping, char *host)
 {
   struct sockaddr_in *s_in = &ping->ping_dest.ping_sockaddr;
   s_in->sin_family = AF_INET;
+  if (strspn (host, ".0123456789") == strlen (host))
+    {
+      /* Numerical IPv4 address.  */
+      if (strchr (host, '.') == NULL)
+	/* Only one octet.  Discard.  */
+	return 1;
+      /* The resolver expands `10.6' to the address 10.0.0.6,
+       * which is productive and sensible.  */
+    }
   if (inet_aton (host, &s_in->sin_addr))
     ping->ping_hostname = strdup (host);
   else
-- 
1.7.2.3

Attachment: signature.asc
Description: Digital signature

Reply via email to