78f5e7
From 067faffb8e596a53c9ac2ed7e571472f7a163681 Mon Sep 17 00:00:00 2001
78f5e7
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
78f5e7
Date: Mon, 16 Jan 2017 16:13:08 +0100
78f5e7
Subject: [PATCH] Add IPv6 support
78f5e7
MIME-Version: 1.0
78f5e7
Content-Type: text/plain; charset=UTF-8
78f5e7
Content-Transfer-Encoding: 8bit
78f5e7
78f5e7
This patch ports the code from IO::Socket::INET to IO::Socket::IP in
78f5e7
order to support IPv6.
78f5e7
78f5e7
CPAN RT #91699, #71395.
78f5e7
78f5e7
Signed-off-by: Petr Písař <ppisar@redhat.com>
78f5e7
---
78f5e7
 Makefile.PL        |  1 +
78f5e7
 README             | 24 ++++++++++++------------
78f5e7
 lib/HTTP/Daemon.pm | 43 ++++++++++++++++++++++++++++---------------
78f5e7
 t/chunked.t        | 34 +++++++++++++++++++++++-----------
78f5e7
 4 files changed, 64 insertions(+), 38 deletions(-)
78f5e7
78f5e7
diff --git a/Makefile.PL b/Makefile.PL
78f5e7
index 09c7e86..85d5712 100644
78f5e7
--- a/Makefile.PL
78f5e7
+++ b/Makefile.PL
78f5e7
@@ -14,6 +14,7 @@ WriteMakefile(
78f5e7
     PREREQ_PM => {
78f5e7
 	'Sys::Hostname' => 0,
78f5e7
 	'IO::Socket' => 0,
78f5e7
+	'IO::Socket::IP' => 0,
78f5e7
 	'HTTP::Request' => 6,
78f5e7
 	'HTTP::Response' => 6,
78f5e7
 	'HTTP::Status' => 6,
78f5e7
diff --git a/README b/README
78f5e7
index be5a20a..ddb3b6e 100644
78f5e7
--- a/README
78f5e7
+++ b/README
78f5e7
@@ -24,12 +24,12 @@ SYNOPSIS
78f5e7
 DESCRIPTION
78f5e7
     Instances of the `HTTP::Daemon' class are HTTP/1.1 servers that listen
78f5e7
     on a socket for incoming requests. The `HTTP::Daemon' is a subclass of
78f5e7
-    `IO::Socket::INET', so you can perform socket operations directly on it
78f5e7
+    `IO::Socket::IP', so you can perform socket operations directly on it
78f5e7
     too.
78f5e7
 
78f5e7
     The accept() method will return when a connection from a client is
78f5e7
     available. The returned value will be an `HTTP::Daemon::ClientConn'
78f5e7
-    object which is another `IO::Socket::INET' subclass. Calling the
78f5e7
+    object which is another `IO::Socket::IP' subclass. Calling the
78f5e7
     get_request() method on this object will read data from the client and
78f5e7
     return an `HTTP::Request' object. The ClientConn object also provide
78f5e7
     methods to send back various responses.
78f5e7
@@ -40,13 +40,13 @@ DESCRIPTION
78f5e7
     responses that conform to the HTTP/1.1 protocol.
78f5e7
 
78f5e7
     The following methods of `HTTP::Daemon' are new (or enhanced) relative
78f5e7
-    to the `IO::Socket::INET' base class:
78f5e7
+    to the `IO::Socket::IP' base class:
78f5e7
 
78f5e7
     $d = HTTP::Daemon->new
78f5e7
     $d = HTTP::Daemon->new( %opts )
78f5e7
         The constructor method takes the same arguments as the
78f5e7
-        `IO::Socket::INET' constructor, but unlike its base class it can
78f5e7
-        also be called without any arguments. The daemon will then set up a
78f5e7
+        `IO::Socket::IP' constructor, but unlike its base class it can also
78f5e7
+        be called without any arguments. The daemon will then set up a
78f5e7
         listen queue of 5 connections and allocate some random port number.
78f5e7
 
78f5e7
         A server that wants to bind to some specific address on the standard
78f5e7
@@ -57,8 +57,8 @@ DESCRIPTION
78f5e7
                    LocalPort => 80,
78f5e7
                );
78f5e7
 
78f5e7
-        See IO::Socket::INET for a description of other arguments that can
78f5e7
-        be used configure the daemon during construction.
78f5e7
+        See IO::Socket::IP for a description of other arguments that can be
78f5e7
+        used configure the daemon during construction.
78f5e7
 
78f5e7
     $c = $d->accept
78f5e7
     $c = $d->accept( $pkg )
78f5e7
@@ -71,7 +71,7 @@ DESCRIPTION
78f5e7
 
78f5e7
         The accept method will return `undef' if timeouts have been enabled
78f5e7
         and no connection is made within the given time. The timeout()
78f5e7
-        method is described in IO::Socket.
78f5e7
+        method is described in IO::Socket::IP.
78f5e7
 
78f5e7
         In list context both the client object and the peer address will be
78f5e7
         returned; see the description of the accept method IO::Socket for
78f5e7
@@ -89,9 +89,9 @@ DESCRIPTION
78f5e7
         The default is the string "libwww-perl-daemon/#.##" where "#.##" is
78f5e7
         replaced with the version number of this module.
78f5e7
 
78f5e7
-    The `HTTP::Daemon::ClientConn' is a `IO::Socket::INET' subclass.
78f5e7
-    Instances of this class are returned by the accept() method of
78f5e7
-    `HTTP::Daemon'. The following methods are provided:
78f5e7
+    The `HTTP::Daemon::ClientConn' is a `IO::Socket::IP' subclass. Instances
78f5e7
+    of this class are returned by the accept() method of `HTTP::Daemon'. The
78f5e7
+    following methods are provided:
78f5e7
 
78f5e7
     $c->get_request
78f5e7
     $c->get_request( $headers_only )
78f5e7
@@ -227,7 +227,7 @@ DESCRIPTION
78f5e7
 SEE ALSO
78f5e7
     RFC 2616
78f5e7
 
78f5e7
-    IO::Socket::INET, IO::Socket
78f5e7
+    IO::Socket::IP, IO::Socket
78f5e7
 
78f5e7
 COPYRIGHT
78f5e7
     Copyright 1996-2003, Gisle Aas
78f5e7
diff --git a/lib/HTTP/Daemon.pm b/lib/HTTP/Daemon.pm
78f5e7
index 27a7bf4..0e22b77 100644
78f5e7
--- a/lib/HTTP/Daemon.pm
78f5e7
+++ b/lib/HTTP/Daemon.pm
78f5e7
@@ -5,8 +5,10 @@ use vars qw($VERSION @ISA $PROTO $DEBUG);
78f5e7
 
78f5e7
 $VERSION = "6.01";
78f5e7
 
78f5e7
-use IO::Socket qw(AF_INET INADDR_ANY INADDR_LOOPBACK inet_ntoa);
78f5e7
-@ISA=qw(IO::Socket::INET);
78f5e7
+use Socket qw(AF_INET AF_INET6 INADDR_ANY IN6ADDR_ANY
78f5e7
+	    INADDR_LOOPBACK IN6ADDR_LOOPBACK inet_ntop sockaddr_family);
78f5e7
+use IO::Socket::IP;
78f5e7
+@ISA=qw(IO::Socket::IP);
78f5e7
 
78f5e7
 $PROTO = "HTTP/1.1";
78f5e7
 
78f5e7
@@ -40,15 +42,26 @@ sub url
78f5e7
     my $self = shift;
78f5e7
     my $url = $self->_default_scheme . "://";
78f5e7
     my $addr = $self->sockaddr;
78f5e7
-    if (!$addr || $addr eq INADDR_ANY) {
78f5e7
+    if (!$addr || $addr eq INADDR_ANY || $addr eq IN6ADDR_ANY) {
78f5e7
  	require Sys::Hostname;
78f5e7
  	$url .= lc Sys::Hostname::hostname();
78f5e7
     }
78f5e7
     elsif ($addr eq INADDR_LOOPBACK) {
78f5e7
-	$url .= inet_ntoa($addr);
78f5e7
+	$url .= inet_ntop(AF_INET, $addr);
78f5e7
+    }
78f5e7
+    elsif ($addr eq IN6ADDR_LOOPBACK) {
78f5e7
+	$url .= '[' . inet_ntop(AF_INET6, $addr) . ']';
78f5e7
     }
78f5e7
     else {
78f5e7
-	$url .= gethostbyaddr($addr, AF_INET) || inet_ntoa($addr);
78f5e7
+	my $host = $addr->sockhostname;
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
+	    }
78f5e7
+	}
78f5e7
+	$url .= $host;
78f5e7
     }
78f5e7
     my $port = $self->sockport;
78f5e7
     $url .= ":$port" if $port != $self->_default_port;
78f5e7
@@ -77,8 +90,8 @@ sub product_tokens
78f5e7
 package HTTP::Daemon::ClientConn;
78f5e7
 
78f5e7
 use vars qw(@ISA $DEBUG);
78f5e7
-use IO::Socket ();
78f5e7
-@ISA=qw(IO::Socket::INET);
78f5e7
+use IO::Socket::IP ();
78f5e7
+@ISA=qw(IO::Socket::IP);
78f5e7
 *DEBUG = \$HTTP::Daemon::DEBUG;
78f5e7
 
78f5e7
 use HTTP::Request  ();
78f5e7
@@ -645,12 +658,12 @@ HTTP::Daemon - a simple http server class
78f5e7
 
78f5e7
 Instances of the C<HTTP::Daemon> class are HTTP/1.1 servers that
78f5e7
 listen on a socket for incoming requests. The C<HTTP::Daemon> is a
78f5e7
-subclass of C<IO::Socket::INET>, so you can perform socket operations
78f5e7
+subclass of C<IO::Socket::IP>, so you can perform socket operations
78f5e7
 directly on it too.
78f5e7
 
78f5e7
 The accept() method will return when a connection from a client is
78f5e7
 available.  The returned value will be an C<HTTP::Daemon::ClientConn>
78f5e7
-object which is another C<IO::Socket::INET> subclass.  Calling the
78f5e7
+object which is another C<IO::Socket::IP> subclass.  Calling the
78f5e7
 get_request() method on this object will read data from the client and
78f5e7
 return an C<HTTP::Request> object.  The ClientConn object also provide
78f5e7
 methods to send back various responses.
78f5e7
@@ -661,7 +674,7 @@ desirable.  Also note that the user is responsible for generating
78f5e7
 responses that conform to the HTTP/1.1 protocol.
78f5e7
 
78f5e7
 The following methods of C<HTTP::Daemon> are new (or enhanced) relative
78f5e7
-to the C<IO::Socket::INET> base class:
78f5e7
+to the C<IO::Socket::IP> base class:
78f5e7
 
78f5e7
 =over 4
78f5e7
 
78f5e7
@@ -670,7 +683,7 @@ to the C<IO::Socket::INET> base class:
78f5e7
 =item $d = HTTP::Daemon->new( %opts )
78f5e7
 
78f5e7
 The constructor method takes the same arguments as the
78f5e7
-C<IO::Socket::INET> constructor, but unlike its base class it can also
78f5e7
+C<IO::Socket::IP> constructor, but unlike its base class it can also
78f5e7
 be called without any arguments.  The daemon will then set up a listen
78f5e7
 queue of 5 connections and allocate some random port number.
78f5e7
 
78f5e7
@@ -682,7 +695,7 @@ HTTP port will be constructed like this:
78f5e7
            LocalPort => 80,
78f5e7
        );
78f5e7
 
78f5e7
-See L<IO::Socket::INET> for a description of other arguments that can
78f5e7
+See L<IO::Socket::IP> for a description of other arguments that can
78f5e7
 be used configure the daemon during construction.
78f5e7
 
78f5e7
 =item $c = $d->accept
78f5e7
@@ -699,7 +712,7 @@ class a subclass of C<HTTP::Daemon::ClientConn>.
78f5e7
 
78f5e7
 The accept method will return C<undef> if timeouts have been enabled
78f5e7
 and no connection is made within the given time.  The timeout() method
78f5e7
-is described in L<IO::Socket>.
78f5e7
+is described in L<IO::Socket::IP>.
78f5e7
 
78f5e7
 In list context both the client object and the peer address will be
78f5e7
 returned; see the description of the accept method L<IO::Socket> for
78f5e7
@@ -721,7 +734,7 @@ replaced with the version number of this module.
78f5e7
 
78f5e7
 =back
78f5e7
 
78f5e7
-The C<HTTP::Daemon::ClientConn> is a C<IO::Socket::INET>
78f5e7
+The C<HTTP::Daemon::ClientConn> is a C<IO::Socket::IP>
78f5e7
 subclass. Instances of this class are returned by the accept() method
78f5e7
 of C<HTTP::Daemon>.  The following methods are provided:
78f5e7
 
78f5e7
@@ -895,7 +908,7 @@ Return a reference to the corresponding C<HTTP::Daemon> object.
78f5e7
 
78f5e7
 RFC 2616
78f5e7
 
78f5e7
-L<IO::Socket::INET>, L<IO::Socket>
78f5e7
+L<IO::Socket::IP>, L<IO::Socket>
78f5e7
 
78f5e7
 =head1 COPYRIGHT
78f5e7
 
78f5e7
diff --git a/t/chunked.t b/t/chunked.t
78f5e7
index e11799f..c274b11 100644
78f5e7
--- a/t/chunked.t
78f5e7
+++ b/t/chunked.t
78f5e7
@@ -95,18 +95,30 @@ my $can_fork = $Config{d_fork} ||
78f5e7
 my $tests = @TESTS;
78f5e7
 my $tport = 8333;
78f5e7
 
78f5e7
-my $tsock = IO::Socket::INET->new(LocalAddr => '0.0.0.0',
78f5e7
-                                  LocalPort => $tport,
78f5e7
-                                  Listen    => 1,
78f5e7
-                                  ReuseAddr => 1);
78f5e7
+my @addresses = (
78f5e7
+    { server => '::', client => '::1' },
78f5e7
+    { server => '0.0.0.0', client => '127.0.0.1' }
78f5e7
+);
78f5e7
+my $family;
78f5e7
+for my $id (0..$#addresses) {
78f5e7
+    my $tsock = IO::Socket::IP->new(LocalAddr => $addresses[$id]->{server},
78f5e7
+                                    LocalPort => $tport,
78f5e7
+                                    Listen    => 1,
78f5e7
+                                    ReuseAddr => 1);
78f5e7
+    if ($tsock) {
78f5e7
+        close $tsock;
78f5e7
+        $family = $id;
78f5e7
+        last;
78f5e7
+    }
78f5e7
+}
78f5e7
+
78f5e7
 if (!$can_fork) {
78f5e7
   plan skip_all => "This system cannot fork";
78f5e7
 }
78f5e7
-elsif (!$tsock) {
78f5e7
-  plan skip_all => "Cannot listen on 0.0.0.0:$tport";
78f5e7
+elsif (!defined $family) {
78f5e7
+  plan skip_all => "Cannot listen on unspecifed address and port $tport";
78f5e7
 }
78f5e7
 else {
78f5e7
-  close $tsock;
78f5e7
   plan tests => $tests;
78f5e7
 }
78f5e7
 
78f5e7
@@ -132,9 +144,9 @@ if ($pid = fork) {
78f5e7
       open my $fh, "| socket localhost $tport" or die;
78f5e7
       print $fh $test;
78f5e7
     }
78f5e7
-    use IO::Socket::INET;
78f5e7
-    my $sock = IO::Socket::INET->new(
78f5e7
-                                     PeerAddr => "127.0.0.1",
78f5e7
+    use IO::Socket::IP;
78f5e7
+    my $sock = IO::Socket::IP->new(
78f5e7
+                                     PeerAddr => $addresses[$family]->{client},
78f5e7
                                      PeerPort => $tport,
78f5e7
                                     ) or die;
78f5e7
     if (0) {
78f5e7
@@ -158,7 +170,7 @@ if ($pid = fork) {
78f5e7
 } else {
78f5e7
   die "cannot fork: $!" unless defined $pid;
78f5e7
   my $d = HTTP::Daemon->new(
78f5e7
-                            LocalAddr => '0.0.0.0',
78f5e7
+                            LocalAddr => $addresses[$family]->{server},
78f5e7
                             LocalPort => $tport,
78f5e7
                             ReuseAddr => 1,
78f5e7
                            ) or die;
78f5e7
-- 
78f5e7
2.7.4
78f5e7