A non-blocking getaddrinfo() resolver
version 0.920
#!/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, idle_timeout => 5, af_order => [ AF_INET6, AF_INET ], # sidecar_program => $path_to_program, ); 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();
POE::Component::Resolver performs Socket::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 getaddrinfo() returns them. These defaults can be overridden with constructor parameters. getaddrinfo() delegates to the operating system's resolver, which may be reconfigured according to the usual conventions.
new
Create a new resolver. Returns an object that must be held and used to make requests. See the synopsis.
Accepts up to four optional named parameters.
\*(L"af_order\*(R" may contain an arrayref with the address families to permit, in the order in which they're preferred. Without \*(L"af_order\*(R", the component will prefer IPv4 addresses over IPv6 for legacy compatibility. This may change in the future as IPv6 gains more widespread acceptance. See \*(L"\s-1ENVIRONMENT\s0 \s-1VARIABLES\s0\*(R" for a way to override the default without hacking modules.
# Prefer IPv6 addresses, but also return IPv4 ones. my $r1 = POE::Component::Resolver->new( af_order => [ AF_INET6, AF_INET ] );
# Only return IPv6 addresses, # or nothing in cases where only IPv4 addresses exist. my $r2 = POE::Component::Resolver->new( af_order => [ AF_INET6 ] );
\*(L"idle_timeout\*(R" determines how long to keep idle resolver subprocesses before cleaning them up, in seconds. It defaults to 15.0 seconds.
\*(L"max_resolvers\*(R" 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 the resources you have available and the amount of parallelism you require.
# One at a time, but without the pesky blocking. my $r3 = POE::Component::Resolver->new( max_resolvers => 1 );
\*(L"sidecar_program\*(R" contains the disk location of a program that will perform blocking lookups on standard input and print the results on standard output. The sidecar program is needed only in special environments where the bundling and execution of extra utilities is tricky. \s-1PAR\s0 is one such environment.
The sidecar program needs to contain at least two statements:
use POE::Component::Resolver::Sidecar; POE::Component::Resover::Sidecar->main();
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() returns a request \s-1ID\s0 that may be used to cancel() a request before it has completed (or undef if the request couldn't begin, such as during shutdown). Resolve requires two parameters and accepts some additional optional ones.
\*(L"host\*(R" and \*(L"service\*(R" 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 for details.
\*(L"event\*(R" is optional; it contains the name of the event that will contain the resolver response. If omitted, it will default to \*(L"resolver_response\*(R"; you may want to specify a shorter event name.
\*(L"hints\*(R" is optional. If specified, it must contain a hashref of hints exactly as getaddrinfo() expects them. See Socket for details.
\*(L"misc\*(R" is optional continuation data that will be passed back in the response. It may contain any type of data the application requires.
cancel
Cancel a request, given the request's \s-1ID\s0.
my $request_id = $resolver->resolve("poe.dyndns.org", "http"); $resolver->cancel($request_id);
shutdown
Shut down the resolver. POE::Component::Resolver retains resources including child processes for up to \*(L"idle_timeout\*(R" seconds. This may keep programs running up to \*(L"idle_timeout\*(R" seconds longer than they should.
POE::Component::Resolver will release its resources (including child processes) when its shutdown() method is called.
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. 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.
resolver_response
The resolver response event includes three parameters.
$_[\s-1ARG0\s0] and $_[\s-1ARG1\s0] contain the retrn values from Socket'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.
$_[\s-1ARG2\s0] contains a hashref of information provided to the resolve() method. Specifically, the values of resolve()'s \*(L"host\*(R", \*(L"service\*(R" and \*(L"misc\*(R" parameters.
The \s-1POCO_RESOLVER_IPV\s0 environment variable sets this component's default Internet Protocol Version search order. If the variable exists, it should contain a string with the numbers 4 and/or 6. POE::Component::Resolver will treate these as Internet Protocol versions to consider, in the order they are preferred.
POE::Component::Resolver's new() method accepts an \*(L"af_order\*(R" parameter that overrides this environment variable.
Default to IPv4 addresses only:
export POCO_RESOLVER_IPV=4
Default to IPv6 addresses only:
export POCO_RESOLVER_IPV=6
Prefer IPv6, but accept IPv4 if needed:
export POCO_RESOLVER_IPV=64
Prefer IPv4, but accept IPv6 if needed:
export POCO_RESOLVER_IPV=46
This module requires \*(L"Microsoft \s-1TCP/IP\s0 version 6\*(R" to be installed. Steps for Windows \s-1XP\s0 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 \*(L"Microsoft \s-1TCP/IP\s0 version 6\*(R" is listed as an item being used, you are done.
Otherwise click Install...
Choose to add a Protocol
And install \*(L"Microsoft \s-1TCP/IP\s0 version 6\*(R" from the list of network protocols.
There is no timeout on requests.
There is no way to cancel a pending request.
Programs should shutdown() their POE::Component::Resolver objects when they are through needing asynchronous \s-1DNS\s0 resolution. Programs should additionally destroy their resolvers if they intend to run awhile and want to reuse the memory they consume.
In some cases, it may be necessary to shutdown components that perform asynchronous \s-1DNS\s0 using POE::Component::Resolver... such as POE::Component::IRC, POE::Component::Client::Keepalive and POE::Component::Client::HTTP.
By default, the resolver subprocesses hang around for idle_timeout, which defaults to 15.0 seconds. Destroying the Resolver object will clean up the process pool. Assuming only that is keeping the event loop active, the program will then exit cleanly.
Alternatively, reduce idle_timeout to a more manageable number, such as 5.0 seconds.
Otherwise something else may also be keeping the event loop active.
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.