NAME

POE::Component::Resolver - A non-blocking getaddrinfo() resolver

VERSION

version 0.900_161

SYNOPSIS

#!/usr/bin/perl

use warnings;
use strict;

use POE;
use POE::Component::Resolver qw(AF_INET AF_INET6);

my $r = POE::Component::Resolver->new(
	max_resolvers => 8,
	af_order => [ AF_INET6, AF_INET ],
);

my @hosts = qw( ipv6-test.com );
my $tcp   = getprotobyname("tcp");

POE::Session->create(
	inline_states => {
		_start => sub {
			foreach my $host (@hosts) {
				$r->resolve(
					host    => $host,
					service => "http",
					event   => "got_response",
					hints   => { protocol => $tcp },
				) or die $!;
			}
		},

		_stop => sub { print "client session stopped\n" },

		got_response => sub {
			my ($error, $addresses, $request) = @_[ARG0..ARG2];
			use YAML; print YAML::Dump(
				{
					error => $error,
					addr => $addresses,
					req => $request,
				}
			);
		},
	}
);

POE::Kernel->run();

DESCRIPTION

POE::Component::Resolver performs Socket::GetAddrInfo::getaddrinfo() calls in subprocesses where they're permitted to block as long as necessary.

By default it will run eight subprocesses and prefer address families in whatever order Socket::GetAddrInfo returns them. These defaults can be overridden with constructor parameters.

PUBLIC METHODS

new

Create a new resolver. Returns an object that must be held and used to make requests. See the synopsis.

Accepts up to two optional named parameters.

"af_order" may contain an arrayref with the address families to permit, in the order in which they're preferred. Without "af_order", the component will return addresses in the order in which Socket::GetAddrInfo provides them.

# Prefer IPv6 addresses, but also return IPv4 ones.
my $r1 = POE::Component::Resolver->new(
	af_order => [ AF_INET6, AF_INET ]
);

# Only return AF_INET6 addresses.
my $r2 = POE::Component::Resolver->new(
	af_order => [ AF_INET6 ]
);

"max_resolvers" controls the component's parallelism by defining the maximum number of sidecar processes to manage. It defaults to 8, but fewer or more processes can be configured depending on usage requirements.

# One at a time, but without the pesky blocking.
my $r3 = POE::Component::Resolver->new( max_resolvers => 1 );

resolve

resolve() begins a new request to resolve a domain. The request will be enqueued in the component until a sidecar process can service it. Resolve requires two parameters and accepts some additional optional ones.

"host" and "service" are required and contain the host (name or Internet address) and service (name or numeric port) that will be passed verbatim to getaddrinfo(). See Socket::GetAddrInfo for details.

"event" is optional; it contains the name of the event that will contain the resolver response. If omitted, it will default to "resolver_response"; you may want to specify a shorter event name.

"hints" is optional. If specified, it must contain a hashref of hints exactly as getaddrinfo() expects them. See Socket::GetAddrInfo for details.

"misc" is optional continuation data that will be passed back in the response. It may contain any type of data the application requires.

unpack_addr

In scalar context, unpack_addr($response_addr_hashref) returns the addr element of $response_addr_hashref in a numeric form appropriate for the address family of the address.

sub handle_resolver_response {
	my ($error, $addresses, $request) = @_[ARG0..ARG2];

	foreach my $a (@$addresses) {
		my $numeric_addr = $resolver->unpack_addr($a);
		print "$request->{host} = $numeric_addr\n";
	}
}

In list context, it returns the numeric port and address.

sub handle_resolver_response {
	my ($error, $addresses, $request) = @_[ARG0..ARG2];

	foreach my $a (@$addresses) {
		my ($$numeric_addr, $port) = $resolver->unpack_addr($a);
		print "$request->{host} = $numeric_addr\n";
	}
}

unpack_addr() is a convenience wrapper around getnameinfo() from Socket::GetAddrInfo. You're certainly welcome to use the discrete function instead.

unpack_addr() returns bleak emptiness on failure, regardless of context. You can check for undef return.

DESTROY

This component is shut down when it's destroyed, following Perl's rules for object destruction. Any pending requests are canceled, and their responses will be errors.

PUBLIC EVENTS

resolver_response

The resolver response event includes three parameters.

$_[ARG0] and $_[ARG1] contain the retrn values from Socket::GetAddrInfo's getaddrinfo() call. These are an error message (if the call failed), and an arrayref of address structures if the call succeeded.

The component provides its own error message, 'component shut down'. This response is given for every pending request at the time the user shuts down the component.

$_[ARG2] contains a hashref of information provided to the resolve() method. Specifically, the values of resolve()'s "host", "service" and "misc" parameters.

COMPATIBILITY ISSUES

Microsoft Windows

This module requires "Microsoft TCP/IP version 6" to be installed. Steps for Windows XP Pro (the steps for your particular version of Windows may be subtly or drastically different):

  • Open your Control Panel

  • Open your Network Connections

  • Select your network connection from the available one(s)

  • In the Local Area Connection Status dialog, click the Properties button

  • If "Microsoft TCP/IP version 6" is listed as an item being used, you are done.

  • Otherwise click Install...

  • Choose to add a Protocol

  • And install "Microsoft TCP/IP version 6" from the list of network protocols.

BUGS

There is no timeout on requests.

There is no way to cancel a pending request.

LICENSE

Except where otherwise noted, this distribution is Copyright 2011 by Rocco Caputo. All rights reserved. This distribution is free software; you may redistribute it and/or modify it under the same terms as Perl itself.