123456789_123456789_123456789_123456789_123456789_

Module: Win32::Resolv

Overview

Constant Summary

Class Method Summary

Class Method Details

.get_hosts_dir (private)

[ GitHub ]

  
# File 'ext/win32/lib/win32/resolv.rb', line 69

def get_hosts_dir
  get_item_property(TCPIP_NT, 'DataBasePath', expand: true)
end

.get_hosts_path

[ GitHub ]

  
# File 'ext/win32/lib/win32/resolv.rb', line 9

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

.get_info (private)

[ GitHub ]

  
# File 'ext/win32/lib/win32/resolv.rb', line 73

def get_info
  search = nil
  nameserver = get_dns_server_list

  slist = get_item_property(TCPIP_NT, 'SearchList')
  search = slist.split(/,\s*/) unless slist.empty?

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

    unless nvdom.empty?
      @search = [ nvdom ]
      udmnd = get_item_property(TCPIP_NT, 'UseDomainNameDevolution').to_i
      if udmnd != 0
        if /^\w+\./ =~ nvdom
          devo = $'
        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+/)
        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?

    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
        end
      end
    end
  end
  search << devo if add_search and devo
  [ search.uniq, nameserver.uniq ]
end

.get_item_property(path, name, expand: false) (private)

[ GitHub ]

  
# File 'ext/win32/lib/win32/resolv.rb', line 129

def get_item_property(path, name, expand: false)
  if defined?(Win32::Registry)
    Registry::HKEY_LOCAL_MACHINE.open(path) do |reg|
      expand ? reg.read_s_expand(name) : reg.read_s(name)
    rescue Registry::Error
      ""
    end
  else
    cmd = "Get-ItemProperty -Path 'HKLM:\\#{path}' -Name '#{name}' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty #{name}"
    output, _ = Open3.capture2('powershell', '-Command', cmd)
    output.strip
  end
end

.get_resolv_info

[ GitHub ]

  
# File 'ext/win32/lib/win32/resolv.rb', line 15

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