async dns support
This commit is contained in:
		| @@ -4,11 +4,12 @@ use strict; | ||||
| use warnings FATAL => 'all'; | ||||
|  | ||||
| use Net::DNS; | ||||
| use AnyEvent::DNS; | ||||
|  | ||||
| use base 'Exporter'; | ||||
| our @EXPORT = (); | ||||
| our @EXPORT_OK = qw/ | ||||
|   hostname_from_ip ipv4_from_hostname | ||||
|   hostname_from_ip hostnames_resolve_async ipv4_from_hostname | ||||
| /; | ||||
| our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||||
|  | ||||
| @@ -75,5 +76,46 @@ sub ipv4_from_hostname { | ||||
|   return undef; | ||||
| } | ||||
|  | ||||
| =head2 hostnames_resolve_async( $ips ) | ||||
|  | ||||
| This method uses a fully asynchronous and high-performance pure-perl stub | ||||
| resolver C<AnyEvent::DNS>. | ||||
|  | ||||
| Given a reference to an array of hashes will resolve the C<IPv4> or C<IPv6> | ||||
| address in the C<ip> or C<alias> key of each hash into its hostname which | ||||
| will be inserted in the C<dns> key of the hash.  The resolver does also | ||||
| forward-lookups to verify that the resolved hostnames point to the | ||||
| address. | ||||
|  | ||||
| Returns the supplied reference to an array of hashes with dns values for | ||||
| addresses which resolved. | ||||
|  | ||||
| =cut | ||||
|  | ||||
| sub hostnames_resolve_async { | ||||
|   my $ips = shift; | ||||
|  | ||||
|   my $resolver = AnyEvent::DNS->new(); | ||||
|      | ||||
|   # Set up the condvar | ||||
|   my $done = AE::cv; | ||||
|   $done->begin( sub { shift->send } ); | ||||
|  | ||||
|   foreach my $hash_ref (@$ips) { | ||||
|     my $ip = $hash_ref->{'ip'} || $hash_ref->{'alias'}; | ||||
|     $done->begin; | ||||
|     AnyEvent::DNS::reverse_verify $ip, | ||||
|             sub { $hash_ref->{'dns'} = shift; $done->end; }; | ||||
|   } | ||||
|  | ||||
|   # Decrement the cv counter to cancel out the send declaration | ||||
|   $done->end; | ||||
|  | ||||
|   # Wait for the resolver to perform all resolutions | ||||
|   $done->recv; | ||||
|  | ||||
|   return $ips; | ||||
| } | ||||
|  | ||||
| 1; | ||||
|  | ||||
|   | ||||
| @@ -13,7 +13,6 @@ our @EXPORT_OK = qw/ | ||||
|   check_only | ||||
|   is_discoverable | ||||
|   is_arpnipable | ||||
|   can_nodenames | ||||
|   is_macsuckable | ||||
| /; | ||||
| our %EXPORT_TAGS = (all => \@EXPORT_OK); | ||||
| @@ -246,32 +245,6 @@ sub is_arpnipable { | ||||
|   return 1; | ||||
| } | ||||
|  | ||||
| =head2 can_nodenames( $ip ) | ||||
|  | ||||
| Given an IP address, returns C<true> if Netdisco on this host is permitted by | ||||
| the local configuration to resolve Node IPs to DNS names for the device. | ||||
|  | ||||
| The configuration items C<nodenames_no> and C<nodenames_only> are checked | ||||
| against the given IP. | ||||
|  | ||||
| Returns false if the host is not permitted to do this job for the target | ||||
| device. | ||||
|  | ||||
| =cut | ||||
|  | ||||
| sub can_nodenames { | ||||
|   my $ip = shift; | ||||
|   my $device = get_device($ip) or return 0; | ||||
|  | ||||
|   return _bail_msg("can_nodenames device matched nodenames_no") | ||||
|     if check_no($device, 'nodenames_no'); | ||||
|  | ||||
|   return _bail_msg("can_nodenames: device failed to match nodenames_only") | ||||
|     unless check_only($device, 'nodenames_only'); | ||||
|  | ||||
|   return 1; | ||||
| } | ||||
|  | ||||
| =head2 is_macsuckable( $ip ) | ||||
|  | ||||
| Given an IP address, returns C<true> if Netdisco on this host is permitted by | ||||
|   | ||||
		Reference in New Issue
	
	Block a user