VinsWorldcom@C:\Users\VinsWorldcom> ver Microsoft Windows [Version 6.1.7601] VinsWorldcom@C:\Users\VinsWorldcom> perl -v This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x64-multi-thread [...] #### VinsWorldcom@C:\Users\VinsWorldcom> dig -v DiG 9.8.1 VinsWorldcom@C:\Users\VinsWorldcom> dig ipv6.google.com aaaa @8.8.8.8 ; <<>> DiG 9.8.1 <<>> ipv6.google.com aaaa @8.8.8.8 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 7089 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 ;; QUESTION SECTION: ;ipv6.google.com. IN AAAA ;; ANSWER SECTION: ipv6.google.com. 86399 IN CNAME ipv6.l.google.com. ipv6.l.google.com. 299 IN AAAA 2001:4860:800f::63 ;; Query time: 69 msec ;; SERVER: 8.8.8.8#53(8.8.8.8) ;; WHEN: Tue Nov 15 10:05:14 2011 ;; MSG SIZE rcvd: 82 #### #!/usr/bin/perl use strict; use warnings; use Getopt::Long qw(:config no_ignore_case); use Pod::Usage; use Socket; use Socket6; use Socket::GetAddrInfo qw(getaddrinfo); my %opt; GetOptions( 'new!' => \$opt{new}, 'old!' => \$opt{old} ) or pod2usage(-verbose => 0); if (!@ARGV) { pod2usage(-verbose => 0, -message => "$0: host required\n") } if ((!defined($opt{new})) && (!defined($opt{old}))) { $opt{new} = $opt{old} = 1 } if ($opt{old}) { my $gethost = gethostbyname($ARGV[0]); my $host = inet_ntoa((gethostbyname($ARGV[0]))[4]); # print "OLD: gethostbyname() = $gethost\n"; print "OLD: inet_ntoa() Address = $host\n"; } if ($opt{new}) { my ($err, @getaddr) = getaddrinfo($ARGV[0], 0); if (!@getaddr) { print "$0: $err\n"; exit } my $host = inet_ntop(AF_INET6, (getaddrinfo($ARGV[0], 0))[1]->{addr}); # printf "NEW: getaddrinfo() = %s\n", $getaddr[1]->{addr}; print "NEW: inet_ntop() Address = $host\n"; } __END__ =head1 SYNOPSIS program [--old|--new] IP[v6]_address | hostname #### VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl www.google.com OLD: inet_ntoa() Address = 72.14.204.105 NEW: inet_ntop() Address = 200:0:480e:cc69:: VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl 72.14.204.105 OLD: inet_ntoa() Address = 72.14.204.105 NEW: inet_ntop() Address = 200:0:480e:cc69:: #### VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl ipv6.google.com Usage: Socket::inet_ntoa(ip_address_sv) at C:\Users\VinsWorldcom\tmp\v4v6Lookup.pl line 29. VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl --new ipv6.google.com C:\Users\VinsWorldcom\tmp\v4v6Lookup.pl: The requested name is valid, but no data of the requested type was found. VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl 2001:4860:800f::63 Usage: Socket::inet_ntoa(ip_address_sv) at C:\Users\VinsWorldcom\tmp\v4v6Lookup.pl line 29. VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl --new 2001:4860:800f::63 Bad arg length for Socket6::inet_ntop, length is 28, should be 16 at C:\Users\VinsWorldcom\tmp\v4v6Lookup.pl line 40. #### #!/usr/bin/perl use strict; use warnings; use Getopt::Long qw(:config no_ignore_case); use Pod::Usage; use Socket; use Socket6; # Windows DOES support getaddrinfo and inet_ntop/pton functions # although it may *seem* like it doesn't from Strawberry's GCC # Don't need Socket6::GetAddrInfo my %opt; GetOptions( 'new!' => \$opt{new}, 'old!' => \$opt{old} ) or pod2usage(-verbose => 0); if (!@ARGV) { pod2usage(-verbose => 0, -message => "$0: host required\n") } if ((!defined($opt{new})) && (!defined($opt{old}))) { $opt{new} = $opt{old} = 1 } if ($opt{old}) { my $gethost = gethostbyname($ARGV[0]); my $host = inet_ntoa((gethostbyname($ARGV[0]))[4]); # print "OLD: gethostbyname() = $gethost\n"; print "OLD: inet_ntoa() Address = $host\n"; } if ($opt{new}) { my @getaddr = getaddrinfo($ARGV[0], 0); # IP[v6] address shows up at different locations in the [3] element # use substr to 'find' it - awful! my $host = inet_ntop($getaddr[0], substr($getaddr[3], (($getaddr[0] == AF_INET) ? 4 : 8), (($getaddr[0] == AF_INET) ? 4 : 16))); # printf "NEW: getaddrinfo() = %s\n", $getaddr[3]; print "NEW: inet_ntop() Address = $host\n"; } __END__ =head1 SYNOPSIS program [--old|--new] IP[v6]_address | hostname #### VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl --new www.google.com NEW: inet_ntop() Address = 72.14.204.147 VinsWorldcom@C:\Users\VinsWorldcom\tmp> v4v6Lookup.pl --new ipv6.google.com NEW: inet_ntop() Address = 2001:4860:800f::93 #### if ($opt{new}) { my @getaddr = getaddrinfo($ARGV[0], 0); my $address; # Get the pointer to the address itself, different fields in IPv4 and IPv6 if ($getaddr[0] == AF_INET) { $address = (unpack_sockaddr_in($getaddr[3]))[1] } else { $address = (unpack_sockaddr_in6($getaddr[3]))[1] } my $host = inet_ntop($getaddr[0], $address); # printf "NEW: getaddrinfo() = %s\n", $getaddr[3]; print "NEW: inet_ntop() Address = $host\n"; }