Skip to content

Commit bd24870

Browse files
committed
Move minimal registry access to the extension
Now win32/registry depends on fiddle, and its conversion is complex and too generic for the purpose of resolv.
1 parent 599f78c commit bd24870

File tree

4 files changed

+260
-195
lines changed

4 files changed

+260
-195
lines changed

ext/win32/resolv/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'mkmf'
22
if RUBY_ENGINE == "ruby" and have_library('iphlpapi', 'GetNetworkParams', ['windows.h', 'iphlpapi.h'])
3+
have_library('advapi32', 'RegGetValueW', ['windows.h'])
34
create_makefile('win32/resolv')
45
else
56
File.write('Makefile', "all clean install:\n\t@echo Done: $(@)\n")

ext/win32/resolv/lib/resolv.rb

Lines changed: 47 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,23 @@
44
55
=end
66

7+
require 'win32/resolv.so'
8+
79
module Win32
810
module Resolv
11+
# Error at Win32 API
12+
class Error < StandardError
13+
# +code+ Win32 Error code
14+
# +message+ Formatted message for +code+
15+
def initialize(code, message)
16+
super(message)
17+
@code = code
18+
end
19+
20+
# Win32 error code
21+
attr_reader :code
22+
end
23+
924
def self.get_hosts_path
1025
path = get_hosts_dir
1126
path = File.expand_path('hosts', path)
@@ -29,121 +44,62 @@ def self.get_resolv_info
2944
end
3045
[ search, nameserver ]
3146
end
32-
end
33-
end
34-
35-
begin
36-
require 'win32/resolv.so'
37-
rescue LoadError
38-
end
39-
40-
module Win32
41-
#====================================================================
42-
# Windows NT
43-
#====================================================================
44-
module Resolv
45-
begin
46-
require 'win32/registry'
47-
module SZ
48-
refine Registry do
49-
# ad hoc workaround for broken registry
50-
def read_s(key)
51-
type, str = read(key)
52-
unless type == Registry::REG_SZ
53-
warn "Broken registry, #{name}\\#{key} was #{Registry.type2name(type)}, ignored"
54-
return String.new
55-
end
56-
str
57-
end
58-
end
59-
end
60-
using SZ
61-
rescue LoadError, Gem::LoadError
62-
require "open3"
63-
end
64-
65-
TCPIP_NT = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'
6647

6748
class << self
6849
private
6950
def get_hosts_dir
70-
get_item_property(TCPIP_NT, 'DataBasePath', expand: true)
51+
tcpip_params do |params|
52+
params.value('DataBasePath')
53+
end
7154
end
7255

7356
def get_info
7457
search = nil
7558
nameserver = get_dns_server_list
7659

77-
slist = get_item_property(TCPIP_NT, 'SearchList')
78-
search = slist.split(/,\s*/) unless slist.empty?
60+
tcpip_params do |params|
61+
slist = params.value('SearchList')
62+
search = slist.split(/,\s*/) if slist and !slist.empty?
7963

80-
if add_search = search.nil?
81-
search = []
82-
nvdom = get_item_property(TCPIP_NT, 'NV Domain')
64+
if add_search = search.nil?
65+
search = []
66+
nvdom = params.value('NV Domain')
8367

84-
unless nvdom.empty?
85-
search = [ nvdom ]
86-
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution', dword: true)
87-
if udmnd != 0
88-
if /^\w+\./ =~ nvdom
89-
devo = $'
68+
if nvdom and !nvdom.empty?
69+
search = [ nvdom ]
70+
udmnd = params.value('UseDomainNameDevolution')
71+
if udmnd&.nonzero?
72+
if /^\w+\./ =~ nvdom
73+
devo = $'
74+
end
9075
end
9176
end
9277
end
93-
end
9478

95-
ifs = if defined?(Win32::Registry)
96-
Registry::HKEY_LOCAL_MACHINE.open(TCPIP_NT + '\Interfaces') do |reg|
97-
reg.keys
98-
rescue Registry::Error
99-
[]
100-
end
101-
else
102-
cmd = "Get-ChildItem 'HKLM:\\#{TCPIP_NT}\\Interfaces' | ForEach-Object { $_.PSChildName }"
103-
output, _ = Open3.capture2('powershell', '-Command', cmd)
104-
output.split(/\n+/)
79+
params.open('Interfaces') do |reg|
80+
reg.each_key do |iface|
81+
next unless ns = %w[NameServer DhcpNameServer].find do |key|
82+
ns = iface.value(key)
83+
break ns.split(/[,\s]\s*/) if ns and !ns.empty?
10584
end
10685

107-
ifs.each do |iface|
108-
next unless ns = %w[NameServer DhcpNameServer].find do |key|
109-
ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
110-
break ns.split(/[,\s]\s*/) unless ns.empty?
111-
end
112-
113-
next if (nameserver & ns).empty?
86+
next if (nameserver & ns).empty?
11487

115-
if add_search
116-
[ 'Domain', 'DhcpDomain' ].each do |key|
117-
dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
118-
unless dom.empty?
119-
search.concat(dom.split(/,\s*/))
120-
break
88+
if add_search
89+
[ 'Domain', 'DhcpDomain' ].each do |key|
90+
dom = iface.value(key)
91+
if dom and !dom.empty?
92+
search.concat(dom.split(/,\s*/))
93+
break
94+
end
95+
end
12196
end
12297
end
12398
end
124-
end
125-
search << devo if add_search and devo
126-
[ search.uniq, nameserver.uniq ]
127-
end
12899

129-
def get_item_property(path, name, expand: false, dword: false)
130-
if defined?(Win32::Registry)
131-
begin
132-
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
133-
if dword
134-
reg.read_i(name)
135-
else
136-
expand ? reg.read_s_expand(name) : reg.read_s(name)
137-
end
138-
end
139-
rescue Registry::Error
140-
dword ? 0 : ""
141-
end
142-
else
143-
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty '#{name}'"
144-
output, _ = Open3.capture2('powershell', '-Command', cmd)
145-
dword ? output.strip.to_i : output.strip
100+
search << devo if add_search and devo
146101
end
102+
[ search.uniq, nameserver.uniq ]
147103
end
148104
end
149105
end

0 commit comments

Comments
 (0)