Author: Gunnar Wolf <gwolf@debian.org>
Forwarded: https://github.com/mla/ip2host/pull/1
Bug-Debian: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=659801
Last-Update: 2012-02-15
Description: Add support for resolving IPv6 addresses

Index: ip2host/ip2host
===================================================================
--- ip2host.orig/ip2host	2012-02-15 21:20:16.000000000 -0600
+++ ip2host/ip2host	2012-02-15 21:31:19.000000000 -0600
@@ -17,11 +17,11 @@
 our $CACHE    = '';        # Optional disk cache file to use
 our $TTL      = 86400 * 7; # Seconds until disk cached ips are expired
 our $DEBUG    = 0;
-# Regular expression for matching an IP address
+# Regular expression for matching either an IPv4 or an IPv6 address
 # $1 should be the IP
-# If IP is always in first column then this would also work:
-# q/^([\d.]+)/s;
-our $REGEX    = '\b (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) \b';
+# IPv6 regex from http://people.spodhuis.org/phil.pennock/software/emit_ipv6_regexp-0.304
+# Just added the IPv4 bit at the beginning.
+our $REGEX = '\b (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|(?^x:(?:(?:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})):){6})(?^:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})):(?^:(?:[0-9a-fA-F]{1,4})))|(?^:(?:(?:(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:::(?:(?^:(?:[0-9a-fA-F]{1,4})):){5})(?^:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})):(?^:(?:[0-9a-fA-F]{1,4})))|(?^:(?:(?:(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})))?::(?:(?^:(?:[0-9a-fA-F]{1,4})):){4})(?^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a-fA-F]{1,4})):(?^:(?:[0-9a-fA-F]{1,4})))|(?^:(?:(?:(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9]))\.){3}(?^:(?:25[0-5]|(?:[1-9]|1[0-9]|2[0-4])?[0-9])))))))|(?:(?:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})):){0,5}(?^:(?:[0-9a-fA-F]{1,4})))?::)(?^:(?:[0-9a-fA-F]{1,4})))|(?:(?:(?:(?:(?^:(?:[0-9a-fA-F]{1,4})):){0,6}(?^:(?:[0-9a-fA-F]{1,4})))?::))))) \b';
 
 use strict;
 use vars qw( %Opt %Buffer %Pending %Cache $Output_Line $Input_Line );
@@ -115,9 +115,13 @@
     chomp($ip);
     my $host = undef;
     eval { # Try to resolve, but give up after $TIMEOUT seconds
+      my ($ip_struct);
       alarm($Opt{timeout});
-      my $ip_struct = inet_aton $ip;
-      $host = gethostbyaddr $ip_struct, AF_INET;
+      if ($ip_struct = inet_aton($ip)) {
+	  $host = gethostbyaddr $ip_struct, AF_INET;
+      } elsif ($ip_struct = Socket::inet_pton(AF_INET6, $ip)) {
+	  $host = gethostbyaddr $ip_struct, AF_INET6;
+      }
       alarm(0);
     };
     # XXX Debug
