| 1 | package Net::DNS::Resolver::cygwin; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | our $VERSION = (qw$Id: cygwin.pm 2002 2025-01-07 09:57:46Z willem $)[2]; | ||
| 6 | |||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::DNS::Resolver::cygwin - Cygwin resolver class | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | |||
| 15 | use IO::File; | ||
| 16 | |||
| 17 | |||
| 18 | sub _getregkey { | ||
| 19 | my @key = @_; | ||
| 20 | |||
| 21 | my $handle = IO::File->new( join( '/', @key ), '<' ) or return ''; | ||
| 22 | my $value = <$handle> || ''; | ||
| 23 | close($handle); | ||
| 24 | |||
| 25 | $value =~ s/\0+$//; | ||
| 26 | return $value; | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | sub _init { | ||
| 31 | my $defaults = shift->_defaults; | ||
| 32 | |||
| 33 | my $dirhandle; | ||
| 34 | |||
| 35 | my $root = '/proc/registry/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters'; | ||
| 36 | |||
| 37 | unless ( -d $root ) { | ||
| 38 | |||
| 39 | # Doesn't exist, maybe we are on 95/98/Me? | ||
| 40 | $root = '/proc/registry/HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/VxD/MSTCP'; | ||
| 41 | -d $root || Carp::croak "can't read registry: $!"; | ||
| 42 | } | ||
| 43 | |||
| 44 | # Best effort to find a useful domain name for the current host | ||
| 45 | # if domain ends up blank, we're probably (?) not connected anywhere | ||
| 46 | # a DNS server is interesting either... | ||
| 47 | my $domain = _getregkey( $root, 'Domain' ) || _getregkey( $root, 'DhcpDomain' ); | ||
| 48 | |||
| 49 | # If nothing else, the searchlist should probably contain our own domain | ||
| 50 | # also see below for domain name devolution if so configured | ||
| 51 | # (also remove any duplicates later) | ||
| 52 | my $devolution = _getregkey( $root, 'UseDomainNameDevolution' ); | ||
| 53 | my $searchlist = _getregkey( $root, 'SearchList' ); | ||
| 54 | my @searchlist = ( $domain, split m/[\s,]+/, $searchlist ); | ||
| 55 | |||
| 56 | |||
| 57 | # This is (probably) adequate on NT4 | ||
| 58 | my @nt4nameservers; | ||
| 59 | foreach ( grep {length} _getregkey( $root, 'NameServer' ), _getregkey( $root, 'DhcpNameServer' ) ) { | ||
| 60 | push @nt4nameservers, split m/[\s,]+/; | ||
| 61 | last; | ||
| 62 | } | ||
| 63 | |||
| 64 | |||
| 65 | # but on W2K/XP the registry layout is more advanced due to dynamically | ||
| 66 | # appearing connections. So we attempt to handle them, too... | ||
| 67 | # opt to silently fail if something isn't ok (maybe we're on NT4) | ||
| 68 | # If this doesn't fail override any NT4 style result we found, as it | ||
| 69 | # may be there but is not valid. | ||
| 70 | # drop any duplicates later | ||
| 71 | my @nameservers; | ||
| 72 | |||
| 73 | my $dnsadapters = join '/', $root, 'DNSRegisteredAdapters'; | ||
| 74 | if ( opendir( $dirhandle, $dnsadapters ) ) { | ||
| 75 | my @adapters = grep { !/^\.\.?$/ } readdir($dirhandle); | ||
| 76 | closedir($dirhandle); | ||
| 77 | foreach my $adapter (@adapters) { | ||
| 78 | my $ns = _getregkey( $dnsadapters, $adapter, 'DNSServerAddresses' ); | ||
| 79 | until ( length($ns) < 4 ) { | ||
| 80 | push @nameservers, join '.', unpack( 'C4', $ns ); | ||
| 81 | substr( $ns, 0, 4 ) = ''; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | my $interfaces = join '/', $root, 'Interfaces'; | ||
| 87 | if ( opendir( $dirhandle, $interfaces ) ) { | ||
| 88 | my @ifacelist = grep { !/^\.\.?$/ } readdir($dirhandle); | ||
| 89 | closedir($dirhandle); | ||
| 90 | foreach my $iface (@ifacelist) { | ||
| 91 | my $ip = _getregkey( $interfaces, $iface, 'DhcpIPAddress' ) | ||
| 92 | || _getregkey( $interfaces, $iface, 'IPAddress' ); | ||
| 93 | next unless $ip; | ||
| 94 | next if $ip eq '0.0.0.0'; | ||
| 95 | |||
| 96 | foreach ( | ||
| 97 | grep {length} _getregkey( $interfaces, $iface, 'NameServer' ), | ||
| 98 | _getregkey( $interfaces, $iface, 'DhcpNameServer' ) | ||
| 99 | ) { | ||
| 100 | push @nameservers, split m/[\s,]+/; | ||
| 101 | last; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | @nameservers = @nt4nameservers unless @nameservers; | ||
| 107 | $defaults->nameservers(@nameservers); | ||
| 108 | |||
| 109 | |||
| 110 | # fix devolution if configured, and simultaneously | ||
| 111 | # eliminate duplicate entries (but keep the order) | ||
| 112 | my @list; | ||
| 113 | my %seen; | ||
| 114 | foreach (@searchlist) { | ||
| 115 | s/\.+$//; | ||
| 116 | push( @list, $_ ) unless $seen{lc $_}++; | ||
| 117 | |||
| 118 | next unless $devolution; | ||
| 119 | |||
| 120 | # while there are more than two labels, cut | ||
| 121 | while (s#^[^.]+\.(.+\..+)$#$1#) { | ||
| 122 | push( @list, $_ ) unless $seen{lc $_}++; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | $defaults->searchlist(@list); | ||
| 126 | |||
| 127 | %$defaults = Net::DNS::Resolver::Base::_untaint(%$defaults); | ||
| 128 | |||
| 129 | $defaults->_read_env; | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | 1; | ||
| 135 | __END__ |