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