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::IP->new(
35      ( $socktype eq "SOCK_STREAM" ? ( Listen => 1 ) : () ),
36      LocalHost => "::1",
37      LocalPort => "0",
38      Type      => Socket->$socktype,
39      GetAddrInfoFlags => 0, # disable AI_ADDRCONFIG
40   );
41
42   ok( defined $testserver, "IO::Socket::IP->new constructs a $socktype socket" ) or
43      diag( "  error was $@" );
44
45   is( $testserver->sockdomain, $AF_INET6,         "\$testserver->sockdomain for $socktype" );
46   is( $testserver->socktype,   Socket->$socktype, "\$testserver->socktype for $socktype" );
47
48   is( $testserver->sockhost, $IN6ADDR_LOOPBACK_HOST, "\$testserver->sockhost for $socktype" );
49   like( $testserver->sockport, qr/^\d+$/,            "\$testserver->sockport for $socktype" );
50
51   ok( eval { $testserver->peerport; 1 }, "\$testserver->peerport does not die for $socktype" )
52      or do { chomp( my $e = $@ ); diag( "Exception was: $e" ) };
53
54   my $socket = IO::Socket->new;
55   $socket->socket( $AF_INET6, Socket->$socktype, 0 )
56      or die "Cannot socket() - $!";
57
58   my ( $err, $ai ) = Socket::getaddrinfo( "::1", $testserver->sockport, { family => $AF_INET6, socktype => Socket->$socktype } );
59   die "getaddrinfo() - $err" if $err;
60
61   $socket->connect( $ai->{addr} ) or die "Cannot connect() - $!";
62
63   my $testclient = ( $socktype eq "SOCK_STREAM" ) ? 
64      $testserver->accept : 
65      do { $testserver->connect( $socket->sockname ); $testserver };
66
67   ok( defined $testclient, "accepted test $socktype client" );
68   isa_ok( $testclient, "IO::Socket::IP", "\$testclient for $socktype" );
69
70   is( $testclient->sockdomain, $AF_INET6,         "\$testclient->sockdomain for $socktype" );
71   is( $testclient->socktype,   Socket->$socktype, "\$testclient->socktype for $socktype" );
72
73   is_deeply( [ unpack_sockaddr_in6_addrport( $socket->sockname ) ],
74              [ unpack_sockaddr_in6_addrport( $testclient->peername ) ],
75              "\$socket->sockname for $socktype" );
76
77   is_deeply( [ unpack_sockaddr_in6_addrport( $socket->peername ) ],
78              [ unpack_sockaddr_in6_addrport( $testclient->sockname ) ],
79              "\$socket->peername for $socktype" );
80
81   my $peerport = ( Socket::unpack_sockaddr_in6 $socket->peername )[0];
82   my $sockport = ( Socket::unpack_sockaddr_in6 $socket->sockname )[0];
83
84   is( $testclient->sockport, $peerport, "\$testclient->sockport for $socktype" );
85   is( $testclient->peerport, $sockport, "\$testclient->peerport for $socktype" );
86
87   # Unpack just so it pretty prints without wrecking the terminal if it fails
88   is( unpack("H*", $testclient->peeraddr), $IN6ADDR_LOOPBACK_HEX, "\$testclient->peeraddr for $socktype" );
89   if( $socktype eq "SOCK_STREAM" ) {
90      # Some OSes don't update sockaddr with a local bind() on SOCK_DGRAM sockets
91      is( unpack("H*", $testclient->sockaddr), $IN6ADDR_LOOPBACK_HEX, "\$testclient->sockaddr for $socktype" );
92   }
93}
94
95done_testing;
96