Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/win32/resolv/extconf.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'mkmf'
if RUBY_ENGINE == "ruby" and have_library('iphlpapi', 'GetNetworkParams', ['windows.h', 'iphlpapi.h'])
have_library('advapi32', 'RegGetValueW', ['windows.h'])
create_makefile('win32/resolv')
else
File.write('Makefile', "all clean install:\n\t@echo Done: $(@)\n")
Expand Down
138 changes: 47 additions & 91 deletions ext/win32/resolv/lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@

=end

require 'win32/resolv.so'

module Win32
module Resolv
# Error at Win32 API
class Error < StandardError
# +code+ Win32 Error code
# +message+ Formatted message for +code+
def initialize(code, message)
super(message)
@code = code
end

# Win32 error code
attr_reader :code
end

def self.get_hosts_path
path = get_hosts_dir
path = File.expand_path('hosts', path)
Expand All @@ -29,121 +44,62 @@ def self.get_resolv_info
end
[ search, nameserver ]
end
end
end

begin
require 'win32/resolv.so'
rescue LoadError
end

module Win32
#====================================================================
# Windows NT
#====================================================================
module Resolv
begin
require 'win32/registry'
module SZ
refine Registry do
# ad hoc workaround for broken registry
def read_s(key)
type, str = read(key)
unless type == Registry::REG_SZ
warn "Broken registry, #{name}\\#{key} was #{Registry.type2name(type)}, ignored"
return String.new
end
str
end
end
end
using SZ
rescue LoadError, Gem::LoadError
require "open3"
end

TCPIP_NT = 'SYSTEM\CurrentControlSet\Services\Tcpip\Parameters'

class << self
private
def get_hosts_dir
get_item_property(TCPIP_NT, 'DataBasePath', expand: true)
tcpip_params do |params|
params.value('DataBasePath')
end
end

def get_info
search = nil
nameserver = get_dns_server_list

slist = get_item_property(TCPIP_NT, 'SearchList')
search = slist.split(/,\s*/) unless slist.empty?
tcpip_params do |params|
slist = params.value('SearchList')
search = slist.split(/,\s*/) if slist and !slist.empty?

if add_search = search.nil?
search = []
nvdom = get_item_property(TCPIP_NT, 'NV Domain')
if add_search = search.nil?
search = []
nvdom = params.value('NV Domain')

unless nvdom.empty?
search = [ nvdom ]
udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution', dword: true)
if udmnd != 0
if /^\w+\./ =~ nvdom
devo = $'
if nvdom and !nvdom.empty?
search = [ nvdom ]
udmnd = params.value('UseDomainNameDevolution')
if udmnd&.nonzero?
if /^\w+\./ =~ nvdom
devo = $'
end
end
end
end
end

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

ifs.each do |iface|
next unless ns = %w[NameServer DhcpNameServer].find do |key|
ns = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
break ns.split(/[,\s]\s*/) unless ns.empty?
end

next if (nameserver & ns).empty?
next if (nameserver & ns).empty?

if add_search
[ 'Domain', 'DhcpDomain' ].each do |key|
dom = get_item_property(TCPIP_NT + '\Interfaces' + "\\#{iface}", key)
unless dom.empty?
search.concat(dom.split(/,\s*/))
break
if add_search
[ 'Domain', 'DhcpDomain' ].each do |key|
dom = iface.value(key)
if dom and !dom.empty?
search.concat(dom.split(/,\s*/))
break
end
end
end
end
end
end
search << devo if add_search and devo
[ search.uniq, nameserver.uniq ]
end

def get_item_property(path, name, expand: false, dword: false)
if defined?(Win32::Registry)
begin
Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
if dword
reg.read_i(name)
else
expand ? reg.read_s_expand(name) : reg.read_s(name)
end
end
rescue Registry::Error
dword ? 0 : ""
end
else
cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty '#{name}'"
output, _ = Open3.capture2('powershell', '-Command', cmd)
dword ? output.strip.to_i : output.strip
search << devo if add_search and devo
end
[ search.uniq, nameserver.uniq ]
end
end
end
Expand Down
Loading