Blame README

Packit 8bbd3c
NAME
Packit 8bbd3c
Packit 8bbd3c
    IO::Socket::IP - Family-neutral IP socket supporting both IPv4 and IPv6
Packit 8bbd3c
Packit 8bbd3c
SYNOPSIS
Packit 8bbd3c
Packit 8bbd3c
     use IO::Socket::IP;
Packit 8bbd3c
    
Packit 8bbd3c
     my $sock = IO::Socket::IP->new(
Packit 8bbd3c
        PeerHost => "www.google.com",
Packit 8bbd3c
        PeerPort => "http",
Packit 8bbd3c
        Type     => SOCK_STREAM,
Packit 8bbd3c
     ) or die "Cannot construct socket - $@";
Packit 8bbd3c
    
Packit 8bbd3c
     my $familyname = ( $sock->sockdomain == PF_INET6 ) ? "IPv6" :
Packit 8bbd3c
                      ( $sock->sockdomain == PF_INET  ) ? "IPv4" :
Packit 8bbd3c
                                                          "unknown";
Packit 8bbd3c
    
Packit 8bbd3c
     printf "Connected to google via %s\n", $familyname;
Packit 8bbd3c
Packit 8bbd3c
DESCRIPTION
Packit 8bbd3c
Packit 8bbd3c
    This module provides a protocol-independent way to use IPv4 and IPv6
Packit 8bbd3c
    sockets, intended as a replacement for IO::Socket::INET. Most
Packit 8bbd3c
    constructor arguments and methods are provided in a backward-compatible
Packit 8bbd3c
    way. For a list of known differences, see the IO::Socket::INET
Packit 8bbd3c
    INCOMPATIBILITES section below.
Packit 8bbd3c
Packit 8bbd3c
    It uses the getaddrinfo(3) function to convert hostnames and service
Packit 8bbd3c
    names or port numbers into sets of possible addresses to connect to or
Packit 8bbd3c
    listen on. This allows it to work for IPv6 where the system supports
Packit 8bbd3c
    it, while still falling back to IPv4-only on systems which don't.
Packit 8bbd3c
Packit 8bbd3c
REPLACING IO::Socket DEFAULT BEHAVIOUR
Packit 8bbd3c
Packit 8bbd3c
    By placing -register in the import list, IO::Socket uses IO::Socket::IP
Packit 8bbd3c
    rather than IO::Socket::INET as the class that handles PF_INET.
Packit 8bbd3c
    IO::Socket will also use IO::Socket::IP rather than IO::Socket::INET6
Packit 8bbd3c
    to handle PF_INET6, provided that the AF_INET6 constant is available.
Packit 8bbd3c
Packit 8bbd3c
    Changing IO::Socket's default behaviour means that calling the
Packit 8bbd3c
    IO::Socket constructor with either PF_INET or PF_INET6 as the Domain
Packit 8bbd3c
    parameter will yield an IO::Socket::IP object.
Packit 8bbd3c
Packit 8bbd3c
     use IO::Socket::IP -register;
Packit 8bbd3c
    
Packit 8bbd3c
     my $sock = IO::Socket->new(
Packit 8bbd3c
        Domain    => PF_INET6,
Packit 8bbd3c
        LocalHost => "::1",
Packit 8bbd3c
        Listen    => 1,
Packit 8bbd3c
     ) or die "Cannot create socket - $@\n";
Packit 8bbd3c
    
Packit 8bbd3c
     print "Created a socket of type " . ref($sock) . "\n";
Packit 8bbd3c
Packit 8bbd3c
    Note that -register is a global setting that applies to the entire
Packit 8bbd3c
    program; it cannot be applied only for certain callers, removed, or
Packit 8bbd3c
    limited by lexical scope.
Packit 8bbd3c
Packit 8bbd3c
CONSTRUCTORS
Packit 8bbd3c
Packit 8bbd3c
 $sock = IO::Socket::IP->new( %args )
Packit 8bbd3c
Packit 8bbd3c
    Creates a new IO::Socket::IP object, containing a newly created socket
Packit 8bbd3c
    handle according to the named arguments passed. The recognised
Packit 8bbd3c
    arguments are:
Packit 8bbd3c
Packit 8bbd3c
    PeerHost => STRING
Packit 8bbd3c
Packit 8bbd3c
    PeerService => STRING
Packit 8bbd3c
Packit 8bbd3c
      Hostname and service name for the peer to connect() to. The service
Packit 8bbd3c
      name may be given as a port number, as a decimal string.
Packit 8bbd3c
Packit 8bbd3c
    PeerAddr => STRING
Packit 8bbd3c
Packit 8bbd3c
    PeerPort => STRING
Packit 8bbd3c
Packit 8bbd3c
      For symmetry with the accessor methods and compatibility with
Packit 8bbd3c
      IO::Socket::INET, these are accepted as synonyms for PeerHost and
Packit 8bbd3c
      PeerService respectively.
Packit 8bbd3c
Packit 8bbd3c
    PeerAddrInfo => ARRAY
Packit 8bbd3c
Packit 8bbd3c
      Alternate form of specifying the peer to connect() to. This should be
Packit 8bbd3c
      an array of the form returned by Socket::getaddrinfo.
Packit 8bbd3c
Packit 8bbd3c
      This parameter takes precedence over the Peer*, Family, Type and
Packit 8bbd3c
      Proto arguments.
Packit 8bbd3c
Packit 8bbd3c
    LocalHost => STRING
Packit 8bbd3c
Packit 8bbd3c
    LocalService => STRING
Packit 8bbd3c
Packit 8bbd3c
      Hostname and service name for the local address to bind() to.
Packit 8bbd3c
Packit 8bbd3c
    LocalAddr => STRING
Packit 8bbd3c
Packit 8bbd3c
    LocalPort => STRING
Packit 8bbd3c
Packit 8bbd3c
      For symmetry with the accessor methods and compatibility with
Packit 8bbd3c
      IO::Socket::INET, these are accepted as synonyms for LocalHost and
Packit 8bbd3c
      LocalService respectively.
Packit 8bbd3c
Packit 8bbd3c
    LocalAddrInfo => ARRAY
Packit 8bbd3c
Packit 8bbd3c
      Alternate form of specifying the local address to bind() to. This
Packit 8bbd3c
      should be an array of the form returned by Socket::getaddrinfo.
Packit 8bbd3c
Packit 8bbd3c
      This parameter takes precedence over the Local*, Family, Type and
Packit 8bbd3c
      Proto arguments.
Packit 8bbd3c
Packit 8bbd3c
    Family => INT
Packit 8bbd3c
Packit 8bbd3c
      The address family to pass to getaddrinfo (e.g. AF_INET, AF_INET6).
Packit 8bbd3c
      Normally this will be left undefined, and getaddrinfo will search
Packit 8bbd3c
      using any address family supported by the system.
Packit 8bbd3c
Packit 8bbd3c
    Type => INT
Packit 8bbd3c
Packit 8bbd3c
      The socket type to pass to getaddrinfo (e.g. SOCK_STREAM,
Packit 8bbd3c
      SOCK_DGRAM). Normally defined by the caller; if left undefined
Packit 8bbd3c
      getaddrinfo may attempt to infer the type from the service name.
Packit 8bbd3c
Packit 8bbd3c
    Proto => STRING or INT
Packit 8bbd3c
Packit 8bbd3c
      The IP protocol to use for the socket (e.g. 'tcp', IPPROTO_TCP,
Packit 8bbd3c
      'udp',IPPROTO_UDP). Normally this will be left undefined, and either
Packit 8bbd3c
      getaddrinfo or the kernel will choose an appropriate value. May be
Packit 8bbd3c
      given either in string name or numeric form.
Packit 8bbd3c
Packit 8bbd3c
    GetAddrInfoFlags => INT
Packit 8bbd3c
Packit 8bbd3c
      More flags to pass to the getaddrinfo() function. If not supplied, a
Packit 8bbd3c
      default of AI_ADDRCONFIG will be used.
Packit 8bbd3c
Packit 8bbd3c
      These flags will be combined with AI_PASSIVE if the Listen argument
Packit 8bbd3c
      is given. For more information see the documentation about
Packit 8bbd3c
      getaddrinfo() in the Socket module.
Packit 8bbd3c
Packit 8bbd3c
    Listen => INT
Packit 8bbd3c
Packit 8bbd3c
      If defined, puts the socket into listening mode where new connections
Packit 8bbd3c
      can be accepted using the accept method. The value given is used as
Packit 8bbd3c
      the listen(2) queue size.
Packit 8bbd3c
Packit 8bbd3c
    ReuseAddr => BOOL
Packit 8bbd3c
Packit 8bbd3c
      If true, set the SO_REUSEADDR sockopt
Packit 8bbd3c
Packit 8bbd3c
    ReusePort => BOOL
Packit 8bbd3c
Packit 8bbd3c
      If true, set the SO_REUSEPORT sockopt (not all OSes implement this
Packit 8bbd3c
      sockopt)
Packit 8bbd3c
Packit 8bbd3c
    Broadcast => BOOL
Packit 8bbd3c
Packit 8bbd3c
      If true, set the SO_BROADCAST sockopt
Packit 8bbd3c
Packit 8bbd3c
    Sockopts => ARRAY
Packit 8bbd3c
Packit 8bbd3c
      An optional array of other socket options to apply after the three
Packit 8bbd3c
      listed above. The value is an ARRAY containing 2- or 3-element
Packit 8bbd3c
      ARRAYrefs. Each inner array relates to a single option, giving the
Packit 8bbd3c
      level and option name, and an optional value. If the value element is
Packit 8bbd3c
      missing, it will be given the value of a platform-sized integer 1
Packit 8bbd3c
      constant (i.e. suitable to enable most of the common boolean
Packit 8bbd3c
      options).
Packit 8bbd3c
Packit 8bbd3c
      For example, both options given below are equivalent to setting
Packit 8bbd3c
      ReuseAddr.
Packit 8bbd3c
Packit 8bbd3c
       Sockopts => [
Packit 8bbd3c
          [ SOL_SOCKET, SO_REUSEADDR ],
Packit 8bbd3c
          [ SOL_SOCKET, SO_REUSEADDR, pack( "i", 1 ) ],
Packit 8bbd3c
       ]
Packit 8bbd3c
Packit 8bbd3c
    V6Only => BOOL
Packit 8bbd3c
Packit 8bbd3c
      If defined, set the IPV6_V6ONLY sockopt when creating PF_INET6
Packit 8bbd3c
      sockets to the given value. If true, a listening-mode socket will
Packit 8bbd3c
      only listen on the AF_INET6 addresses; if false it will also accept
Packit 8bbd3c
      connections from AF_INET addresses.
Packit 8bbd3c
Packit 8bbd3c
      If not defined, the socket option will not be changed, and default
Packit 8bbd3c
      value set by the operating system will apply. For repeatable
Packit 8bbd3c
      behaviour across platforms it is recommended this value always be
Packit 8bbd3c
      defined for listening-mode sockets.
Packit 8bbd3c
Packit 8bbd3c
      Note that not all platforms support disabling this option. Some, at
Packit 8bbd3c
      least OpenBSD and MirBSD, will fail with EINVAL if you attempt to
Packit 8bbd3c
      disable it. To determine whether it is possible to disable, you may
Packit 8bbd3c
      use the class method
Packit 8bbd3c
Packit 8bbd3c
       if( IO::Socket::IP->CAN_DISABLE_V6ONLY ) {
Packit 8bbd3c
          ...
Packit 8bbd3c
       }
Packit 8bbd3c
       else {
Packit 8bbd3c
          ...
Packit 8bbd3c
       }
Packit 8bbd3c
Packit 8bbd3c
      If your platform does not support disabling this option but you still
Packit 8bbd3c
      want to listen for both AF_INET and AF_INET6 connections you will
Packit 8bbd3c
      have to create two listening sockets, one bound to each protocol.
Packit 8bbd3c
Packit 8bbd3c
    MultiHomed
Packit 8bbd3c
Packit 8bbd3c
      This IO::Socket::INET-style argument is ignored, except if it is
Packit 8bbd3c
      defined but false. See the IO::Socket::INET INCOMPATIBILITES section
Packit 8bbd3c
      below.
Packit 8bbd3c
Packit 8bbd3c
      However, the behaviour it enables is always performed by
Packit 8bbd3c
      IO::Socket::IP.
Packit 8bbd3c
Packit 8bbd3c
    Blocking => BOOL
Packit 8bbd3c
Packit 8bbd3c
      If defined but false, the socket will be set to non-blocking mode.
Packit 8bbd3c
      Otherwise it will default to blocking mode. See the NON-BLOCKING
Packit 8bbd3c
      section below for more detail.
Packit 8bbd3c
Packit 8bbd3c
    Timeout => NUM
Packit 8bbd3c
Packit 8bbd3c
      If defined, gives a maximum time in seconds to block per connect()
Packit 8bbd3c
      call when in blocking mode. If missing, no timeout is applied other
Packit 8bbd3c
      than that provided by the underlying operating system. When in
Packit 8bbd3c
      non-blocking mode this parameter is ignored.
Packit 8bbd3c
Packit 8bbd3c
      Note that if the hostname resolves to multiple address candidates,
Packit 8bbd3c
      the same timeout will apply to each connection attempt individually,
Packit 8bbd3c
      rather than to the operation as a whole. Further note that the
Packit 8bbd3c
      timeout does not apply to the initial hostname resolve operation, if
Packit 8bbd3c
      connecting by hostname.
Packit 8bbd3c
Packit 8bbd3c
      This behviour is copied inspired by IO::Socket::INET; for more fine
Packit 8bbd3c
      grained control over connection timeouts, consider performing a
Packit 8bbd3c
      nonblocking connect directly.
Packit 8bbd3c
Packit 8bbd3c
    If neither Type nor Proto hints are provided, a default of SOCK_STREAM
Packit 8bbd3c
    and IPPROTO_TCP respectively will be set, to maintain compatibility
Packit 8bbd3c
    with IO::Socket::INET. Other named arguments that are not recognised
Packit 8bbd3c
    are ignored.
Packit 8bbd3c
Packit 8bbd3c
    If neither Family nor any hosts or addresses are passed, nor any
Packit 8bbd3c
    *AddrInfo, then the constructor has no information on which to decide a
Packit 8bbd3c
    socket family to create. In this case, it performs a getaddinfo call
Packit 8bbd3c
    with the AI_ADDRCONFIG flag, no host name, and a service name of "0",
Packit 8bbd3c
    and uses the family of the first returned result.
Packit 8bbd3c
Packit 8bbd3c
    If the constructor fails, it will set $@ to an appropriate error
Packit 8bbd3c
    message; this may be from $! or it may be some other string; not every
Packit 8bbd3c
    failure necessarily has an associated errno value.
Packit 8bbd3c
Packit 8bbd3c
 $sock = IO::Socket::IP->new( $peeraddr )
Packit 8bbd3c
Packit 8bbd3c
    As a special case, if the constructor is passed a single argument (as
Packit 8bbd3c
    opposed to an even-sized list of key/value pairs), it is taken to be
Packit 8bbd3c
    the value of the PeerAddr parameter. This is parsed in the same way,
Packit 8bbd3c
    according to the behaviour given in the PeerHost AND LocalHost PARSING
Packit 8bbd3c
    section below.
Packit 8bbd3c
Packit 8bbd3c
METHODS
Packit 8bbd3c
Packit 8bbd3c
    As well as the following methods, this class inherits all the methods
Packit 8bbd3c
    in IO::Socket and IO::Handle.
Packit 8bbd3c
Packit 8bbd3c
 ( $host, $service ) = $sock->sockhost_service( $numeric )
Packit 8bbd3c
Packit 8bbd3c
    Returns the hostname and service name of the local address (that is,
Packit 8bbd3c
    the socket address given by the sockname method).
Packit 8bbd3c
Packit 8bbd3c
    If $numeric is true, these will be given in numeric form rather than
Packit 8bbd3c
    being resolved into names.
Packit 8bbd3c
Packit 8bbd3c
    The following four convenience wrappers may be used to obtain one of
Packit 8bbd3c
    the two values returned here. If both host and service names are
Packit 8bbd3c
    required, this method is preferable to the following wrappers, because
Packit 8bbd3c
    it will call getnameinfo(3) only once.
Packit 8bbd3c
Packit 8bbd3c
 $addr = $sock->sockhost
Packit 8bbd3c
Packit 8bbd3c
    Return the numeric form of the local address as a textual
Packit 8bbd3c
    representation
Packit 8bbd3c
Packit 8bbd3c
 $port = $sock->sockport
Packit 8bbd3c
Packit 8bbd3c
    Return the numeric form of the local port number
Packit 8bbd3c
Packit 8bbd3c
 $host = $sock->sockhostname
Packit 8bbd3c
Packit 8bbd3c
    Return the resolved name of the local address
Packit 8bbd3c
Packit 8bbd3c
 $service = $sock->sockservice
Packit 8bbd3c
Packit 8bbd3c
    Return the resolved name of the local port number
Packit 8bbd3c
Packit 8bbd3c
 $addr = $sock->sockaddr
Packit 8bbd3c
Packit 8bbd3c
    Return the local address as a binary octet string
Packit 8bbd3c
Packit 8bbd3c
 ( $host, $service ) = $sock->peerhost_service( $numeric )
Packit 8bbd3c
Packit 8bbd3c
    Returns the hostname and service name of the peer address (that is, the
Packit 8bbd3c
    socket address given by the peername method), similar to the
Packit 8bbd3c
    sockhost_service method.
Packit 8bbd3c
Packit 8bbd3c
    The following four convenience wrappers may be used to obtain one of
Packit 8bbd3c
    the two values returned here. If both host and service names are
Packit 8bbd3c
    required, this method is preferable to the following wrappers, because
Packit 8bbd3c
    it will call getnameinfo(3) only once.
Packit 8bbd3c
Packit 8bbd3c
 $addr = $sock->peerhost
Packit 8bbd3c
Packit 8bbd3c
    Return the numeric form of the peer address as a textual representation
Packit 8bbd3c
Packit 8bbd3c
 $port = $sock->peerport
Packit 8bbd3c
Packit 8bbd3c
    Return the numeric form of the peer port number
Packit 8bbd3c
Packit 8bbd3c
 $host = $sock->peerhostname
Packit 8bbd3c
Packit 8bbd3c
    Return the resolved name of the peer address
Packit 8bbd3c
Packit 8bbd3c
 $service = $sock->peerservice
Packit 8bbd3c
Packit 8bbd3c
    Return the resolved name of the peer port number
Packit 8bbd3c
Packit 8bbd3c
 $addr = $peer->peeraddr
Packit 8bbd3c
Packit 8bbd3c
    Return the peer address as a binary octet string
Packit 8bbd3c
Packit 8bbd3c
 $inet = $sock->as_inet
Packit 8bbd3c
Packit 8bbd3c
    Returns a new IO::Socket::INET instance wrapping the same filehandle.
Packit 8bbd3c
    This may be useful in cases where it is required, for
Packit 8bbd3c
    backward-compatibility, to have a real object of IO::Socket::INET type
Packit 8bbd3c
    instead of IO::Socket::IP. The new object will wrap the same underlying
Packit 8bbd3c
    socket filehandle as the original, so care should be taken not to
Packit 8bbd3c
    continue to use both objects concurrently. Ideally the original $sock
Packit 8bbd3c
    should be discarded after this method is called.
Packit 8bbd3c
Packit 8bbd3c
    This method checks that the socket domain is PF_INET and will throw an
Packit 8bbd3c
    exception if it isn't.
Packit 8bbd3c
Packit 8bbd3c
NON-BLOCKING
Packit 8bbd3c
Packit 8bbd3c
    If the constructor is passed a defined but false value for the Blocking
Packit 8bbd3c
    argument then the socket is put into non-blocking mode. When in
Packit 8bbd3c
    non-blocking mode, the socket will not be set up by the time the
Packit 8bbd3c
    constructor returns, because the underlying connect(2) syscall would
Packit 8bbd3c
    otherwise have to block.
Packit 8bbd3c
Packit 8bbd3c
    The non-blocking behaviour is an extension of the IO::Socket::INET API,
Packit 8bbd3c
    unique to IO::Socket::IP, because the former does not support
Packit 8bbd3c
    multi-homed non-blocking connect.
Packit 8bbd3c
Packit 8bbd3c
    When using non-blocking mode, the caller must repeatedly check for
Packit 8bbd3c
    writeability on the filehandle (for instance using select or IO::Poll).
Packit 8bbd3c
    Each time the filehandle is ready to write, the connect method must be
Packit 8bbd3c
    called, with no arguments. Note that some operating systems, most
Packit 8bbd3c
    notably MSWin32 do not report a connect() failure using write-ready; so
Packit 8bbd3c
    you must also select() for exceptional status.
Packit 8bbd3c
Packit 8bbd3c
    While connect returns false, the value of $! indicates whether it
Packit 8bbd3c
    should be tried again (by being set to the value EINPROGRESS, or
Packit 8bbd3c
    EWOULDBLOCK on MSWin32), or whether a permanent error has occurred
Packit 8bbd3c
    (e.g. ECONNREFUSED).
Packit 8bbd3c
Packit 8bbd3c
    Once the socket has been connected to the peer, connect will return
Packit 8bbd3c
    true and the socket will now be ready to use.
Packit 8bbd3c
Packit 8bbd3c
    Note that calls to the platform's underlying getaddrinfo(3) function
Packit 8bbd3c
    may block. If IO::Socket::IP has to perform this lookup, the
Packit 8bbd3c
    constructor will block even when in non-blocking mode.
Packit 8bbd3c
Packit 8bbd3c
    To avoid this blocking behaviour, the caller should pass in the result
Packit 8bbd3c
    of such a lookup using the PeerAddrInfo or LocalAddrInfo arguments.
Packit 8bbd3c
    This can be achieved by using Net::LibAsyncNS, or the getaddrinfo(3)
Packit 8bbd3c
    function can be called in a child process.
Packit 8bbd3c
Packit 8bbd3c
     use IO::Socket::IP;
Packit 8bbd3c
     use Errno qw( EINPROGRESS EWOULDBLOCK );
Packit 8bbd3c
    
Packit 8bbd3c
     my @peeraddrinfo = ... # Caller must obtain the getaddinfo result here
Packit 8bbd3c
    
Packit 8bbd3c
     my $socket = IO::Socket::IP->new(
Packit 8bbd3c
        PeerAddrInfo => \@peeraddrinfo,
Packit 8bbd3c
        Blocking     => 0,
Packit 8bbd3c
     ) or die "Cannot construct socket - $@";
Packit 8bbd3c
    
Packit 8bbd3c
     while( !$socket->connect and ( $! == EINPROGRESS || $! == EWOULDBLOCK ) ) {
Packit 8bbd3c
        my $wvec = '';
Packit 8bbd3c
        vec( $wvec, fileno $socket, 1 ) = 1;
Packit 8bbd3c
        my $evec = '';
Packit 8bbd3c
        vec( $evec, fileno $socket, 1 ) = 1;
Packit 8bbd3c
    
Packit 8bbd3c
        select( undef, $wvec, $evec, undef ) or die "Cannot select - $!";
Packit 8bbd3c
     }
Packit 8bbd3c
    
Packit 8bbd3c
     die "Cannot connect - $!" if $!;
Packit 8bbd3c
    
Packit 8bbd3c
     ...
Packit 8bbd3c
Packit 8bbd3c
    The example above uses select(), but any similar mechanism should work
Packit 8bbd3c
    analogously. IO::Socket::IP takes care when creating new socket
Packit 8bbd3c
    filehandles to preserve the actual file descriptor number, so such
Packit 8bbd3c
    techniques as poll or epoll should be transparent to its reallocation
Packit 8bbd3c
    of a different socket underneath, perhaps in order to switch protocol
Packit 8bbd3c
    family between PF_INET and PF_INET6.
Packit 8bbd3c
Packit 8bbd3c
    For another example using IO::Poll and Net::LibAsyncNS, see the
Packit 8bbd3c
    examples/nonblocking_libasyncns.pl file in the module distribution.
Packit 8bbd3c
Packit 8bbd3c
PeerHost AND LocalHost PARSING
Packit 8bbd3c
Packit 8bbd3c
    To support the IO::Socket::INET API, the host and port information may
Packit 8bbd3c
    be passed in a single string rather than as two separate arguments.
Packit 8bbd3c
Packit 8bbd3c
    If either LocalHost or PeerHost (or their ...Addr synonyms) have any of
Packit 8bbd3c
    the following special forms then special parsing is applied.
Packit 8bbd3c
Packit 8bbd3c
    The value of the ...Host argument will be split to give both the
Packit 8bbd3c
    hostname and port (or service name):
Packit 8bbd3c
Packit 8bbd3c
     hostname.example.org:http    # Host name
Packit 8bbd3c
     192.0.2.1:80                 # IPv4 address
Packit 8bbd3c
     [2001:db8::1]:80             # IPv6 address
Packit 8bbd3c
Packit 8bbd3c
    In each case, the port or service name (e.g. 80) is passed as the
Packit 8bbd3c
    LocalService or PeerService argument.
Packit 8bbd3c
Packit 8bbd3c
    Either of LocalService or PeerService (or their ...Port synonyms) can
Packit 8bbd3c
    be either a service name, a decimal number, or a string containing both
Packit 8bbd3c
    a service name and number, in a form such as
Packit 8bbd3c
Packit 8bbd3c
     http(80)
Packit 8bbd3c
Packit 8bbd3c
    In this case, the name (http) will be tried first, but if the resolver
Packit 8bbd3c
    does not understand it then the port number (80) will be used instead.
Packit 8bbd3c
Packit 8bbd3c
    If the ...Host argument is in this special form and the corresponding
Packit 8bbd3c
    ...Service or ...Port argument is also defined, the one parsed from the
Packit 8bbd3c
    ...Host argument will take precedence and the other will be ignored.
Packit 8bbd3c
Packit 8bbd3c
 ( $host, $port ) = IO::Socket::IP->split_addr( $addr )
Packit 8bbd3c
Packit 8bbd3c
    Utility method that provides the parsing functionality described above.
Packit 8bbd3c
    Returns a 2-element list, containing either the split hostname and port
Packit 8bbd3c
    description if it could be parsed, or the given address and undef if it
Packit 8bbd3c
    was not recognised.
Packit 8bbd3c
Packit 8bbd3c
     IO::Socket::IP->split_addr( "hostname:http" )
Packit 8bbd3c
                                  # ( "hostname",  "http" )
Packit 8bbd3c
    
Packit 8bbd3c
     IO::Socket::IP->split_addr( "192.0.2.1:80" )
Packit 8bbd3c
                                  # ( "192.0.2.1", "80"   )
Packit 8bbd3c
    
Packit 8bbd3c
     IO::Socket::IP->split_addr( "[2001:db8::1]:80" )
Packit 8bbd3c
                                  # ( "2001:db8::1", "80" )
Packit 8bbd3c
    
Packit 8bbd3c
     IO::Socket::IP->split_addr( "something.else" )
Packit 8bbd3c
                                  # ( "something.else", undef )
Packit 8bbd3c
Packit 8bbd3c
 $addr = IO::Socket::IP->join_addr( $host, $port )
Packit 8bbd3c
Packit 8bbd3c
    Utility method that performs the reverse of split_addr, returning a
Packit 8bbd3c
    string formed by joining the specified host address and port number.
Packit 8bbd3c
    The host address will be wrapped in [] brackets if required (because it
Packit 8bbd3c
    is a raw IPv6 numeric address).
Packit 8bbd3c
Packit 8bbd3c
    This can be especially useful when combined with the sockhost_service
Packit 8bbd3c
    or peerhost_service methods.
Packit 8bbd3c
Packit 8bbd3c
     say "Connected to ", IO::Socket::IP->join_addr( $sock->peerhost_service );
Packit 8bbd3c
Packit 8bbd3c
IO::Socket::INET INCOMPATIBILITES
Packit 8bbd3c
Packit 8bbd3c
      * The behaviour enabled by MultiHomed is in fact implemented by
Packit 8bbd3c
      IO::Socket::IP as it is required to correctly support searching for a
Packit 8bbd3c
      useable address from the results of the getaddrinfo(3) call. The
Packit 8bbd3c
      constructor will ignore the value of this argument, except if it is
Packit 8bbd3c
      defined but false. An exception is thrown in this case, because that
Packit 8bbd3c
      would request it disable the getaddrinfo(3) search behaviour in the
Packit 8bbd3c
      first place.
Packit 8bbd3c
Packit 8bbd3c
      * IO::Socket::IP implements both the Blocking and Timeout parameters,
Packit 8bbd3c
      but it implements the interaction of both in a different way.
Packit 8bbd3c
Packit 8bbd3c
      In ::INET, supplying a timeout overrides the non-blocking behaviour,
Packit 8bbd3c
      meaning that the connect() operation will still block despite that
Packit 8bbd3c
      the caller asked for a non-blocking socket. This is not explicitly
Packit 8bbd3c
      specified in its documentation, nor does this author believe that is
Packit 8bbd3c
      a useful behaviour - it appears to come from a quirk of
Packit 8bbd3c
      implementation.
Packit 8bbd3c
Packit 8bbd3c
      In ::IP therefore, the Blocking parameter takes precedence - if a
Packit 8bbd3c
      non-blocking socket is requested, no operation will block. The
Packit 8bbd3c
      Timeout parameter here simply defines the maximum time that a
Packit 8bbd3c
      blocking connect() call will wait, if it blocks at all.
Packit 8bbd3c
Packit 8bbd3c
      In order to specifically obtain the "blocking connect then
Packit 8bbd3c
      non-blocking send and receive" behaviour of specifying this
Packit 8bbd3c
      combination of options to ::INET when using ::IP, perform first a
Packit 8bbd3c
      blocking connect, then afterwards turn the socket into nonblocking
Packit 8bbd3c
      mode.
Packit 8bbd3c
Packit 8bbd3c
       my $sock = IO::Socket::IP->new(
Packit 8bbd3c
          PeerHost => $peer,
Packit 8bbd3c
          Timeout => 20,
Packit 8bbd3c
       ) or die "Cannot connect - $@";
Packit 8bbd3c
      
Packit 8bbd3c
       $sock->blocking( 0 );
Packit 8bbd3c
Packit 8bbd3c
      This code will behave identically under both IO::Socket::INET and
Packit 8bbd3c
      IO::Socket::IP.
Packit 8bbd3c
Packit 8bbd3c
TODO
Packit 8bbd3c
Packit 8bbd3c
      * Investigate whether POSIX::dup2 upsets BSD's kqueue watchers, and
Packit 8bbd3c
      if so, consider what possible workarounds might be applied.
Packit 8bbd3c
Packit 8bbd3c
AUTHOR
Packit 8bbd3c
Packit 8bbd3c
    Paul Evans <leonerd@leonerd.org.uk>
Packit 8bbd3c