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