The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Socket::GetAddrInfo - a wrapper for Socket6's getaddrinfo and getnameinfo, or emulation for platforms that do not support it

SYNOPSIS

 use Socket::GetAddrInfo qw( getaddrinfo getnameinfo );
 use IO::Socket;

 my $sock;

 my @res = getaddrinfo( "www.google.com", "www" );

 while( @res >= 5 ) {
    my ( $family, $socktype, $proto, $addr, $canonname ) = splice @res, 0, 5;

    $sock = IO::Socket->new();
    $sock->socket( $family, $socktype, $proto ) or undef $sock, next;
    $sock->connect( $addr ) or undef $sock, next;

    last;
 }

 if( $sock ) {
    my ( $host, $service ) = getnameinfo( $sock->peername );
    print "Connected to $host:$service\n";
 }

DESCRIPTION

The intention of this module is that any code wishing to perform name-to-address or address-to-name resolutions should use this instead of using Socket6 directly. If the underlying platform has Socket6 installed, then it will be used, and the complete range of features it provides can be used. If the platform does not support it, then this module will instead provide emulations of the relevant functions, using the legacy resolver functions of gethostbyname(), etc...

These emulations support the same interface as the real Socket6 functions, and behave as close as is resonably possible to emulate using the legacy functions. See below for details on the limits of this emulation.

Any existing code that already uses Socket6 to do this can simply change

 use Socket6 qw( getaddrinfo );

into

 use Socket::GetAddrInfo qw( getaddrinfo );

and require no further changes, in order to be backward-compatible with older machines that do not or cannot support Socket6.

LIMITS OF EMULATION

These emulations are not a complete replacement of Socket6, because they only support IPv4 (the AF_INET socket family).

@res = getaddrinfo( $node, $service, $family, $socktype, $protocol, $flags )

  • If $family is supplied, it must be AF_INET. Any other value will result in an error thrown by croak.

  • The only supported $flags values are AI_PASSIVE, AI_CANONNAME and AI_NUMERICHOST.

( $node, $service ) = getnameinfo( $addr, $flags )

  • If the sockaddr family of $addr is anything other than AF_INET, an error will be thrown with croak.

  • The only supported $flags values are NI_NUMERICHOST, NI_NUMERICSERV, NI_NAMEREQD and NI_DGRAM.

SEE ALSO

  • Socket6 - IPv6 related part of the C socket.h defines and structure manipulators

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>