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