1#  You may distribute under the terms of either the GNU General Public License
2#  or the Artistic License (the same terms as Perl itself)
3#
4#  (C) Paul Evans, 2011-2015 -- leonerd@leonerd.org.uk
5
6package IO::Async::Resolver::DNS::LibResolvImpl;
7
8use strict;
9use warnings;
10
11our $VERSION = '0.06';
12
13use Net::LibResolv 0.03 qw( res_query res_search class_name2value type_name2value $h_errno );
14
15use IO::Async::Resolver;
16use constant HAVE_IO_ASYNC_RESOLVER_EXTENDED_ERROR => ( $IO::Async::Resolver::VERSION >= '0.68' );
17
18use IO::Async::Resolver::DNS::Constants qw( /^ERR_/ );
19
20my %errmap = (
21   Net::LibResolv::HOST_NOT_FOUND => ERR_NO_HOST,
22   Net::LibResolv::NO_ADDRESS     => ERR_NO_ADDRESS,
23   Net::LibResolv::NO_DATA        => ERR_NO_ADDRESS,
24   Net::LibResolv::NO_RECOVERY    => ERR_UNRECOVERABLE,
25   Net::LibResolv::TRY_AGAIN      => ERR_TEMPORARY,
26);
27
28sub _resolve
29{
30   my ( $func, $dname, $class, $type ) = @_;
31   my $pkt = $func->( $dname, class_name2value($class), type_name2value($type) );
32   if( !defined $pkt ) {
33      # We can't easily detect NODATA errors here, so we'll have to let the
34      # higher-level function do it
35      die HAVE_IO_ASYNC_RESOLVER_EXTENDED_ERROR
36         ? [ "$h_errno", $errmap{$h_errno+0} ]
37         : "$h_errno\n";
38   }
39
40   return $pkt;
41}
42
43sub IO::Async::Resolver::DNS::res_query  { _resolve( \&res_query,  @_ ) }
44sub IO::Async::Resolver::DNS::res_search { _resolve( \&res_search, @_ ) }
45
460x55AA;
47