Net::DNS::Native - non-blocking system DNS resolver
use Net::DNS::Native; use IO::Select; use Socket; my $dns = Net::DNS::Native->new(); my $sock = $dns->getaddrinfo("google.com"); my $sel = IO::Select->new($sock); $sel->can_read(); # wait until resolving done my ($err, @res) = $dns->get_result($sock); die "Resolving failed: ", $err if ($err); for my $r (@res) { warn "google.com has ip ", $r->{family} == AF_INET ? inet_ntoa((unpack_sockaddr_in($r->{addr}))[1]) : # IPv4 Socket::inet_ntop(AF_INET6, (unpack_sockaddr_in6($r->{addr}))[1]); # IPv6 }
use Net::DNS::Native; use AnyEvent; use Socket; my $dns = Net::DNS::Native->new; my $cv = AnyEvent->condvar; $cv->begin; for my $host ('google.com', 'google.ru', 'google.cy') { my $fh = $dns->inet_aton($host); $cv->begin; my $w; $w = AnyEvent->io( fh => $fh, poll => 'r', cb => sub { my $ip = $dns->get_result($fh); warn $host, $ip ? " has ip " . inet_ntoa($ip) : " has no ip"; $cv->end; undef $w; } ) } $cv->end; $cv->recv;
This class provides several methods for host name resolution. It is designed to be used with event loops. All resolving are done by getaddrinfo(3) implemented in your system library. Since getaddrinfo() is blocking function and we don't want to block, call to this function will be done in separate thread. This class uses system native threads and not perl threads. So overhead shouldn't bee too big. Disadvantages of this method is that we can't provide timeout for resolving. And default timeout for getaddrinfo() on my system is about 40 sec.
To support threaded extensions like this one your perl should be linked with threads library. One of the possible solution is to build your perl with perl threads support using -Dusethreads for Configure script. But it is not necessary to build threaded perl. So, other solution is to not use -Dusethreads and instead use -A prepend:libswanted="pthread ". This will link your perl executable with libpthread.
-Dusethreads
Configure
-A prepend:libswanted="pthread "
If this conditions are not met you may get segfault. To check it run this oneliner:
perl -MConfig -le 'print $Config{usethreads}||$Config{libs}=~/-l?pthread\b/ ? "this perl may use threaded library" : "this perl may segfault with threaded library"'
This is a class constructor. Doesn't accept any arguments for now.
This is the most powerfull method. May resolve host to both IPv4 and IPv6 addresses. For full documentation see getaddrinfo(). This method accepts same parameters but instead of result returns handle on which you need to wait for availability to read.
This method will resolve $host accordingly to $family, which may be AF_INET to resolve to IPv4 or AF_INET6 to resolve to IPv6. For full documentation see inet_pton(). This method accepts same parameters but instead of result returns handle on which you need to wait for availability to read.
This method may be used only for resolving to IPv4. For full documentation see inet_aton(). This method accepts same parameters but instead of result returns handle on which you need to wait for availability to read.
This method may be used only for resolving to IPv4. For full documentation see gethostbyname(). This method accepts same parameters but instead of result returns handle on which you need to wait for availability to read.
After handle returned by methods above will became ready for read you should call this method with handle as argument. It will return results appropriate to the method which returned this handle. For getaddrinfo this will be ($err, @res) list. For inet_pton and inet_aton $packed_address or undef. For gethostbyname() $packed_address or undef in scalar context and ($name,$aliases,$addrtype,$length,@addrs) in list context.
getaddrinfo
($err, @res)
inet_pton
inet_aton
$packed_address
undef
gethostbyname()
($name,$aliases,$addrtype,$length,@addrs)
NOTE: it is important to call get_result() on returned handle when it will become ready for read. Because this method destroys resources associated with this handle. Otherwise you will get memory leaks.
Oleg G, <oleg@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself
To install Net::DNS::Native, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Net::DNS::Native
CPAN shell
perl -MCPAN -e shell install Net::DNS::Native
For more information on module installation, please visit the detailed CPAN module installation guide.