bcbb20
From e49f553aa8be21e5df72452e50af2e9f0b82ecad Mon Sep 17 00:00:00 2001
bcbb20
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
bcbb20
Date: Wed, 23 May 2018 17:31:42 +0200
bcbb20
Subject: [PATCH] Resolve specific socket addresses correctly
bcbb20
MIME-Version: 1.0
bcbb20
Content-Type: text/plain; charset=UTF-8
bcbb20
Content-Transfer-Encoding: 8bit
bcbb20
bcbb20
Previous code did not formatted specific (not 0.0.0.0 or ::)
bcbb20
correctly:
bcbb20
bcbb20
$ perl -MHTTP::Daemon -e '$d=HTTP::Daemon->new(LocalAddr=>q{127.0.0.2}) or die; print $d->url, qq{\n}'
bcbb20
Can't call method "sockhostname" without a package or object reference at /usr/share/perl5/vendor_perl/HTTP/Daemon.pm line 64.
bcbb20
bcbb20
This patch also fixes formatting numerical IPv6 addresses. It seems
bcbb20
that IO::Socket::IP::sockhostname() formats unresolvable addresses too.
bcbb20
bcbb20
Signed-off-by: Petr Písař <ppisar@redhat.com>
bcbb20
---
bcbb20
 lib/HTTP/Daemon.pm | 15 +++++++++++++--
bcbb20
 1 file changed, 13 insertions(+), 2 deletions(-)
bcbb20
bcbb20
diff --git a/lib/HTTP/Daemon.pm b/lib/HTTP/Daemon.pm
bcbb20
index 1e9d48e..216c73f 100644
bcbb20
--- a/lib/HTTP/Daemon.pm
bcbb20
+++ b/lib/HTTP/Daemon.pm
bcbb20
@@ -61,12 +61,23 @@ sub url
bcbb20
 	$url .= '[' . inet_ntop(AF_INET6, $addr) . ']';
bcbb20
     }
bcbb20
     else {
bcbb20
-	my $host = $addr->sockhostname;
bcbb20
+	my $host = $self->sockhostname;
bcbb20
+	# sockhostname() seems to return a stringified IP address if not
bcbb20
+	# resolvable, then quote it for a port separator and an IPv6 zone separator.
bcbb20
+	# But be paranoid for a case when it already contains a bracket.
bcbb20
+	if (defined $host and $host =~ /:/) {
bcbb20
+	    if ($host =~ /[\[\]]/) {
bcbb20
+		$host = undef;
bcbb20
+	    } else {
bcbb20
+		$host =~ s/%/%25/g;
bcbb20
+		$host  = '[' . $host . ']';
bcbb20
+	    }
bcbb20
+	}
bcbb20
         if (!defined $host) {
bcbb20
 	    if (sockaddr_family($addr) eq AF_INET6) {
bcbb20
 		$host = '[' . inet_ntop(AF_INET6, $addr) . ']';
bcbb20
 	    } else {
bcbb20
-		$host = inet_ntop(AF_INET6, $addr);
bcbb20
+		$host = inet_ntop(AF_INET, $addr);
bcbb20
 	    }
bcbb20
 	}
bcbb20
 	$url .= $host;
bcbb20
-- 
bcbb20
2.14.3
bcbb20