1#!/usr/bin/perl 2 3use v5; 4use strict; 5use warnings; 6 7use Test::More; 8 9use IO::Socket::IP; 10use Socket qw( inet_pton inet_ntop pack_sockaddr_in6 unpack_sockaddr_in6 IN6ADDR_LOOPBACK ); 11 12my $AF_INET6 = eval { Socket::AF_INET6() } or 13 plan skip_all => "No AF_INET6"; 14 15# Some odd locations like BSD jails might not like IN6ADDR_LOOPBACK. We'll 16# establish a baseline first to test against 17my $IN6ADDR_LOOPBACK = eval { 18 socket my $sockh, Socket::PF_INET6(), SOCK_STREAM, 0 or die "Cannot socket(PF_INET6) - $!"; 19 bind $sockh, pack_sockaddr_in6( 0, inet_pton( $AF_INET6, "::1" ) ) or die "Cannot bind() - $!"; 20 ( unpack_sockaddr_in6( getsockname $sockh ) )[1]; 21} or plan skip_all => "Unable to bind to ::1 - $@"; 22my $IN6ADDR_LOOPBACK_HOST = inet_ntop( $AF_INET6, $IN6ADDR_LOOPBACK ); 23if( $IN6ADDR_LOOPBACK ne IN6ADDR_LOOPBACK ) { 24 diag( "Testing with IN6ADDR_LOOPBACK=$IN6ADDR_LOOPBACK_HOST; this may be because of odd networking" ); 25} 26my $IN6ADDR_LOOPBACK_HEX = unpack "H*", $IN6ADDR_LOOPBACK; 27 28# Unpack just ip6_addr and port because other fields might not match end to end 29sub unpack_sockaddr_in6_addrport { 30 return ( Socket::unpack_sockaddr_in6( shift ) )[0,1]; 31} 32 33foreach my $socktype (qw( SOCK_STREAM SOCK_DGRAM )) { 34 my $testserver = IO::Socket->new; 35 $testserver->socket( $AF_INET6, Socket->$socktype, 0 ) 36 or die "Cannot socket() - $!"; 37 38 my ( $err, $ai ) = Socket::getaddrinfo( "::1", 0, { family => $AF_INET6, socktype => Socket->$socktype } ); 39 die "getaddrinfo() - $err" if $err; 40 41 $testserver->bind( $ai->{addr} ) or die "Cannot bind() - $!"; 42 43 if( $socktype eq "SOCK_STREAM" ) { 44 $testserver->listen( 1 ) or die "Cannot listen() - $!"; 45 } 46 47 my $testport = ( Socket::unpack_sockaddr_in6 $testserver->sockname )[0]; 48 49 my $socket = IO::Socket::IP->new( 50 PeerHost => "::1", 51 PeerService => $testport, 52 Type => Socket->$socktype, 53 GetAddrInfoFlags => 0, # disable AI_ADDRCONFIG 54 ); 55 56 ok( defined $socket, "IO::Socket::IP->new constructs a $socktype socket" ) or 57 diag( " error was $@" ); 58 59 is( $socket->sockdomain, $AF_INET6, "\$socket->sockdomain for $socktype" ); 60 is( $socket->socktype, Socket->$socktype, "\$socket->socktype for $socktype" ); 61 62 my $testclient = ( $socktype eq "SOCK_STREAM" ) ? 63 $testserver->accept : 64 do { $testserver->connect( $socket->sockname ); $testserver }; 65 66 ok( defined $testclient, "accepted test $socktype client" ); 67 68 ok( $socket->connected, "\$socket is connected for $socktype" ); 69 70 is_deeply( [ unpack_sockaddr_in6_addrport( $socket->sockname ) ], 71 [ unpack_sockaddr_in6_addrport( $testclient->peername ) ], 72 "\$socket->sockname for $socktype" ); 73 74 is_deeply( [ unpack_sockaddr_in6_addrport( $socket->peername ) ], 75 [ unpack_sockaddr_in6_addrport( $testclient->sockname ) ], 76 "\$socket->peername for $socktype" ); 77 78 is( $socket->peerhost, $IN6ADDR_LOOPBACK_HOST, "\$socket->peerhost for $socktype" ); 79 is( $socket->peerport, $testport, "\$socket->peerport for $socktype" ); 80 81 # Unpack just so it pretty prints without wrecking the terminal if it fails 82 is( unpack("H*", $socket->peeraddr), $IN6ADDR_LOOPBACK_HEX, "\$testclient->peeraddr for $socktype" ); 83 if( $socktype eq "SOCK_STREAM" ) { 84 # Some OSes don't update sockaddr with a local bind() on SOCK_DGRAM sockets 85 is( unpack("H*", $socket->sockaddr), $IN6ADDR_LOOPBACK_HEX, "\$testclient->sockaddr for $socktype" ); 86 } 87 88 # Can't easily test the non-numeric versions without relying on the system's 89 # ability to resolve the name "localhost" 90 91 $socket->close; 92 ok( !$socket->connected, "\$socket not connected after close for $socktype" ); 93} 94 95done_testing; 96