Module: Win32::Resolv

Defined in:
lib/win32/resolv.rb,
lib/win32/resolv.rb,
lib/win32/resolv9x.rb,
resolv/resolv.c

Overview

Windows NT

Defined Under Namespace

Modules: SZ, WsControl

Constant Summary collapse

API =
Registry::API
Error =
Registry::Error
TCPIP_NT =
'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'
TCPIP_9X =
'SYSTEM\CurrentControlSet\Services\VxD\MSTCP'
DHCP_9X =
'SYSTEM\CurrentControlSet\Services\VxD\DHCP'
WINDOWS =
'Software\Microsoft\Windows\CurrentVersion'

Class Method Summary collapse

Class Method Details

.get_dhcpinfoObject


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/win32/resolv9x.rb', line 42

def get_dhcpinfo
  macaddrs = {}
  ipaddrs = {}
  WsControl.get_iflist.each do |index, macaddr, *ipaddr|
    macaddrs[macaddr] = 1
    ipaddr.each { |ipaddr| ipaddrs[ipaddr] = 1 }
  end
  iflist = [ macaddrs, ipaddrs ]

  search = []
  nameserver = []
  version = -1
  Registry::HKEY_LOCAL_MACHINE.open(DHCP_9X) do |reg|
    begin
      version = API.unpackdw(reg.read_bin("Version"))
    rescue Registry::Error
    end

    reg.each_key do |key, |
      catch(:not_used) do
        reg.open(key) do |regdi|
          dom, ns = get_dhcpinfo_key(version, regdi, iflist)
          search << dom if dom
          nameserver.concat(ns) if ns
        end
      end
    end
  end
  [ search, nameserver ]
end

.get_dhcpinfo_95(reg) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'lib/win32/resolv9x.rb', line 73

def get_dhcpinfo_95(reg)
  dhcp = reg.read_bin("DhcpInfo")
  [
    API.unpackdw(dhcp[4..7]),
    API.unpackdw(dhcp[8..11]),
    1,
    dhcp[45..50],
    reg.read_bin("OptionInfo"),
  ]
end

.get_dhcpinfo_98(reg) ⇒ Object


84
85
86
87
88
89
90
91
92
# File 'lib/win32/resolv9x.rb', line 84

def get_dhcpinfo_98(reg)
  [
    API.unpackdw(reg.read_bin("DhcpIPAddress")),
    API.unpackdw(reg.read_bin("DhcpSubnetMask")),
    API.unpackdw(reg.read_bin("HardwareType")),
    reg.read_bin("HardwareAddress"),
    reg.read_bin("OptionInfo"),
  ]
end

.get_dhcpinfo_key(version, reg, iflist) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/win32/resolv9x.rb', line 94

def get_dhcpinfo_key(version, reg, iflist)
  info = case version
         when 1
           get_dhcpinfo_95(reg)
         when 2
           get_dhcpinfo_98(reg)
         else
           begin
             get_dhcpinfo_98(reg)
           rescue Registry::Error
             get_dhcpinfo_95(reg)
           end
         end
  ipaddr, netmask, hwtype, macaddr, opt = info
  throw :not_used unless
    ipaddr and ipaddr != 0 and
    netmask and netmask != 0 and
    macaddr and macaddr.size == 6 and
    hwtype == 1 and
    iflist[0][macaddr] and iflist[1][ipaddr]

  size = opt.size
  idx = 0
  while idx <= size
    opttype = opt[idx]
    optsize = opt[idx + 1]
    optval  = opt[idx + 2, optsize]
    case opttype
    when 0xFF    ## term
      break
    when 0x0F    ## domain
      domain = optval.chomp("\0")
    when 0x06    ## dns
      nameserver = optval.scan(/..../).collect { |addr|
        "%d.%d.%d.%d" % addr.unpack('C4')
      }
    end
    idx += optsize + 2
  end
  [ domain, nameserver ]
rescue Registry::Error
  throw :not_used
end

.get_hosts_dirObject

private


13
14
15
16
17
# File 'lib/win32/resolv9x.rb', line 13

def get_hosts_dir
  Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
    reg.read_s_expand('DataBasePath')
  end
end

.get_hosts_pathObject


14
15
16
17
18
# File 'lib/win32/resolv.rb', line 14

def self.get_hosts_path
  path = get_hosts_dir
  path = File.expand_path('hosts', path)
  File.exist?(path) ? path : nil
end

.get_infoObject


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/win32/resolv.rb', line 88

def get_info
  search = nil
  nameserver = get_dns_server_list
  Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT) do |reg|
    begin
      slist = reg.read_s('SearchList')
      search = slist.split(/,\s*/) unless slist.empty?
    rescue Registry::Error
    end

    if add_search = search.nil?
      search = []
      begin
        nvdom = reg.read_s('NV Domain')
        unless nvdom.empty?
          @search = [ nvdom ]
          if reg.read_i('UseDomainNameDevolution') != 0
            if /^\w+\./ =~ nvdom
              devo = $'
            end
          end
        end
      rescue Registry::Error
      end
    end

    reg.open('Interfaces') do |h|
      h.each_key do |iface, |
        h.open(iface) do |regif|
          next unless ns = %w[NameServer DhcpNameServer].find do |key|
            begin
              ns = regif.read_s(key)
            rescue Registry::Error
            else
              break ns.split(/[,\s]\s*/) unless ns.empty?
            end
          end
          next if (nameserver & ns).empty?

          if add_search
            begin
              [ 'Domain', 'DhcpDomain' ].each do |key|
                dom = regif.read_s(key)
                unless dom.empty?
                  search.concat(dom.split(/,\s*/))
                  break
                end
              end
            rescue Registry::Error
            end
          end
        end
      end
    end
    search << devo if add_search and devo
  end
  [ search.uniq, nameserver.uniq ]
end

.get_resolv_infoObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/win32/resolv.rb', line 20

def self.get_resolv_info
  search, nameserver = get_info
  if search.empty?
    search = nil
  else
    search.delete("")
    search.uniq!
  end
  if nameserver.empty?
    nameserver = nil
  else
    nameserver.delete("")
    nameserver.delete("0.0.0.0")
    nameserver.uniq!
  end
  [ search, nameserver ]
end