|
| 1 | +# frozen_string_literal: false |
| 2 | +=begin |
| 3 | += Win32 DNS and DHCP I/F |
| 4 | +
|
| 5 | +=end |
| 6 | + |
| 7 | +require 'win32/registry' |
| 8 | + |
| 9 | +module Win32 |
| 10 | + module Resolv |
| 11 | + API = Registry::API |
| 12 | + Error = Registry::Error |
| 13 | + |
| 14 | + def self.get_hosts_path |
| 15 | + path = get_hosts_dir |
| 16 | + path = File.expand_path('hosts', path) |
| 17 | + File.exist?(path) ? path : nil |
| 18 | + end |
| 19 | + |
| 20 | + def self.get_resolv_info |
| 21 | + search, nameserver = get_info |
| 22 | + if search.empty? |
| 23 | + search = nil |
| 24 | + else |
| 25 | + search.delete("") |
| 26 | + search.uniq! |
| 27 | + end |
| 28 | + if nameserver.empty? |
| 29 | + nameserver = nil |
| 30 | + else |
| 31 | + nameserver.delete("") |
| 32 | + nameserver.delete("0.0.0.0") |
| 33 | + nameserver.uniq! |
| 34 | + end |
| 35 | + [ search, nameserver ] |
| 36 | + end |
| 37 | + end |
| 38 | +end |
| 39 | + |
| 40 | +begin |
| 41 | + require 'win32/resolv.so' |
| 42 | +rescue LoadError |
| 43 | +end |
| 44 | + |
| 45 | +module Win32 |
| 46 | +#==================================================================== |
| 47 | +# Windows NT |
| 48 | +#==================================================================== |
| 49 | + module Resolv |
| 50 | + module SZ |
| 51 | + refine Registry do |
| 52 | + # ad hoc workaround for broken registry |
| 53 | + def read_s(key) |
| 54 | + type, str = read(key) |
| 55 | + unless type == Registry::REG_SZ |
| 56 | + warn "Broken registry, #{name}\\#{key} was #{Registry.type2name(type)}, ignored" |
| 57 | + return String.new |
| 58 | + end |
| 59 | + str |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + using SZ |
| 64 | + |
| 65 | + TCPIP_NT = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters' |
| 66 | + |
| 67 | + class << self |
| 68 | + private |
| 69 | + def get_hosts_dir |
| 70 | + Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg| |
| 71 | + reg.read_s_expand('DataBasePath') |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + def get_info |
| 76 | + search = nil |
| 77 | + nameserver = get_dns_server_list |
| 78 | + Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg| |
| 79 | + begin |
| 80 | + slist = reg.read_s('SearchList') |
| 81 | + search = slist.split(/,\s*/) unless slist.empty? |
| 82 | + rescue Registry::Error |
| 83 | + end |
| 84 | + |
| 85 | + if add_search = search.nil? |
| 86 | + search = [] |
| 87 | + begin |
| 88 | + nvdom = reg.read_s('NV Domain') |
| 89 | + unless nvdom.empty? |
| 90 | + @search = [ nvdom ] |
| 91 | + if reg.read_i('UseDomainNameDevolution') != 0 |
| 92 | + if /^\w+\./ =~ nvdom |
| 93 | + devo = $' |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | + rescue Registry::Error |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + reg.open('Interfaces') do |h| |
| 102 | + h.each_key do |iface, | |
| 103 | + h.open(iface) do |regif| |
| 104 | + next unless ns = %w[NameServer DhcpNameServer].find do |key| |
| 105 | + begin |
| 106 | + ns = regif.read_s(key) |
| 107 | + rescue Registry::Error |
| 108 | + else |
| 109 | + break ns.split(/[,\s]\s*/) unless ns.empty? |
| 110 | + end |
| 111 | + end |
| 112 | + next if (nameserver & ns).empty? |
| 113 | + |
| 114 | + if add_search |
| 115 | + begin |
| 116 | + [ 'Domain', 'DhcpDomain' ].each do |key| |
| 117 | + dom = regif.read_s(key) |
| 118 | + unless dom.empty? |
| 119 | + search.concat(dom.split(/,\s*/)) |
| 120 | + break |
| 121 | + end |
| 122 | + end |
| 123 | + rescue Registry::Error |
| 124 | + end |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | + end |
| 129 | + search << devo if add_search and devo |
| 130 | + end |
| 131 | + [ search.uniq, nameserver.uniq ] |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments