| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #  You may distribute under the terms of either the GNU General Public License | 
| 2 |  |  |  |  |  |  | #  or the Artistic License (the same terms as Perl itself) | 
| 3 |  |  |  |  |  |  | # | 
| 4 |  |  |  |  |  |  | #  (C) Paul Evans, 2010-2011 -- leonerd@leonerd.org.uk | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | package Socket::GetAddrInfo::Socket6api; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 23890 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 72 |  | 
| 9 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 51 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 | 2 |  |  | 2 |  | 9 | use Carp; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 167 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.22'; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 2 |  |  | 2 |  | 9 | use Exporter 'import'; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 69 |  | 
| 16 |  |  |  |  |  |  | our @EXPORT_OK = qw( | 
| 17 |  |  |  |  |  |  | getaddrinfo | 
| 18 |  |  |  |  |  |  | getnameinfo | 
| 19 |  |  |  |  |  |  | ); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 2 |  |  | 2 |  | 492 | use Socket::GetAddrInfo (); | 
|  | 2 |  |  |  |  | 5 |  | 
|  | 2 |  |  |  |  | 607 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | # Re-export all the AI_*, EAI_* and NI_* constants | 
| 24 |  |  |  |  |  |  | my @constants = @{ $Socket::GetAddrInfo::EXPORT_TAGS{constants} }; | 
| 25 |  |  |  |  |  |  | push @EXPORT_OK, @constants; | 
| 26 |  |  |  |  |  |  | Socket::GetAddrInfo->import( @constants ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 NAME | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | C - Provide L functions | 
| 31 |  |  |  |  |  |  | using L API | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | use Socket qw( AF_UNSPEC SOCK_STREAM ); | 
| 36 |  |  |  |  |  |  | use Socket::GetAddrInfo::Socket6api qw( getaddrinfo getnameinfo ); | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | my $sock; | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | my @res = getaddrinfo( "www.google.com", "www", AF_UNSPEC, SOCK_STREAM ); | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | die "Cannot resolve name - $res[0]" if @res == 1; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | while( @res >= 5 ) { | 
| 45 |  |  |  |  |  |  | my ( $family, $socktype, $protocol, $addr, undef ) = splice @res, 0, 5, (); | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | $sock = IO::Socket->new(); | 
| 48 |  |  |  |  |  |  | $sock->socket( $family, $socktype, $protocol ) or | 
| 49 |  |  |  |  |  |  | undef $sock, next; | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | $sock->connect( $addr ) or undef $sock, next; | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | last; | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | if( $sock ) { | 
| 57 |  |  |  |  |  |  | my ( $host, $service ) = getnameinfo( $sock->peername ); | 
| 58 |  |  |  |  |  |  | print "Connected to $host:$service\n" if defined $host; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | L provides the functions of C and | 
| 64 |  |  |  |  |  |  | C using a convenient interface where hints and address structures | 
| 65 |  |  |  |  |  |  | are represented as hashes. L also provides these functions, in a form | 
| 66 |  |  |  |  |  |  | taking and returning flat lists of values. | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | This module wraps the functions provided by C to provide | 
| 69 |  |  |  |  |  |  | them in an identical API to C. It is intended to stand as a utility | 
| 70 |  |  |  |  |  |  | for existing code written for the C API to use these functions | 
| 71 |  |  |  |  |  |  | instead. | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =cut | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | =cut | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head2 @res = getaddrinfo( $host, $service, $family, $socktype, $protocol, $flags ) | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | This version of the API takes the hints values as separate ordered parameters. | 
| 82 |  |  |  |  |  |  | Unspecified parameters should be passed as C<0>. | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | If successful, this function returns a flat list of values, five for each | 
| 85 |  |  |  |  |  |  | returned address structure. Each group of five elements will contain, in | 
| 86 |  |  |  |  |  |  | order, the C, C, C, C and C | 
| 87 |  |  |  |  |  |  | values of the address structure. | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | If unsuccessful, it will return a single value, containing the string error | 
| 90 |  |  |  |  |  |  | message. To remain compatible with the C interface, this value does | 
| 91 |  |  |  |  |  |  | not have the error integer part. | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | =cut | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub getaddrinfo | 
| 96 |  |  |  |  |  |  | { | 
| 97 | 2 | 50 | 33 | 2 | 1 | 5920 | @_ >= 2 and @_ <= 6 or | 
| 98 |  |  |  |  |  |  | croak "Usage: getaddrinfo(host, service, family=0, socktype=0, protocol=0, flags=0)"; | 
| 99 |  |  |  |  |  |  |  | 
| 100 | 2 |  |  |  |  | 8 | my ( $host, $service, $family, $socktype, $protocol, $flags ) = @_; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 2 |  | 50 |  |  | 1255 | my ( $err, @res ) = Socket::GetAddrInfo::getaddrinfo( $host, $service, { | 
|  |  |  | 50 |  |  |  |  | 
|  |  |  | 50 |  |  |  |  | 
|  |  |  | 50 |  |  |  |  | 
| 103 |  |  |  |  |  |  | flags    => $flags    || 0, | 
| 104 |  |  |  |  |  |  | family   => $family   || 0, | 
| 105 |  |  |  |  |  |  | socktype => $socktype || 0, | 
| 106 |  |  |  |  |  |  | protocol => $protocol || 0, | 
| 107 |  |  |  |  |  |  | } ); | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 2 | 100 |  |  |  | 15 | return "$err" if $err; | 
| 110 | 1 |  |  |  |  | 2 | return map { $_->{family}, $_->{socktype}, $_->{protocol}, $_->{addr}, $_->{canonname} } @res; | 
|  | 1 |  |  |  |  | 9 |  | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 ( $host, $service ) = getnameinfo( $addr, $flags ) | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | This version of the API returns only the host name and service name, if | 
| 116 |  |  |  |  |  |  | successfully resolved. On error, it will return an empty list. To remain | 
| 117 |  |  |  |  |  |  | compatible with the C interface, no error information will be | 
| 118 |  |  |  |  |  |  | supplied. | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | =cut | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | sub getnameinfo | 
| 123 |  |  |  |  |  |  | { | 
| 124 | 1 | 50 | 33 | 1 | 1 | 786 | @_ >= 1 and @_ <= 2 or | 
| 125 |  |  |  |  |  |  | croak "Usage: getnameinfo(addr, flags=0)"; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 1 |  |  |  |  | 3 | my ( $addr, $flags ) = @_; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 1 |  |  |  |  | 21 | my ( $err, $host, $service ) = Socket::GetAddrInfo::getnameinfo( $addr, $flags ); | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 1 | 50 |  |  |  | 4 | return () if $err; | 
| 132 | 1 |  |  |  |  | 4 | return ( $host, $service ); | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 AUTHOR | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | Paul Evans | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =cut | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | 0x55AA; |