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