Class: Middleman::DnsResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-core/dns_resolver.rb,
lib/middleman-core/dns_resolver/hosts_resolver.rb,
lib/middleman-core/dns_resolver/network_resolver.rb,
lib/middleman-core/dns_resolver/local_link_resolver.rb,
lib/middleman-core/dns_resolver/basic_network_resolver.rb

Overview

This resolves IP address to names and vice versa

Defined Under Namespace

Classes: BasicNetworkResolver, HostsResolver, LocalLinkResolver, NetworkResolver

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DnsResolver

Create resolver

First the local resolver is used. If environment variable HOSTSRC is given this file is used for local name lookup.

First the local resolver is queried. If this raises an error or returns nil or [] the network resolver is queried.

Parameters:

  • network_resolver (#getnames, #getaddresses)

    The resolver which uses a network name server to resolve ip addresses and names.

  • local_resolver (#getnames, #getaddresses)

    The resolver uses /etc/hosts on POSIX-systems and C:\Windows\System32\drivers\etc\hosts on Windows-operating systems to resolve ip addresses and names.



30
31
32
33
34
35
36
37
38
# File 'lib/middleman-core/dns_resolver.rb', line 30

def initialize(opts={})
  @resolvers = []
  @resolvers << opts.fetch(:hosts_resolver, HostsResolver.new)

  require 'middleman-core/dns_resolver/local_link_resolver'
  @resolvers << opts.fetch(:local_link_resolver, LocalLinkResolver.new)

  @resolvers << opts.fetch(:network_resolver, NetworkResolver.new)
end

Instance Method Details

#ips_for(name) ⇒ Object

Get ips for given name

First the local resolver is used. On POSIX-systems /etc/hosts is used. On Windows C:\Windows\System32\drivers\etc\hosts is used.

Parameters:

  • name (String)

    The name which should be resolved.



61
62
63
64
65
66
67
68
69
# File 'lib/middleman-core/dns_resolver.rb', line 61

def ips_for(name)
  resolvers.each do |r|
    ips = r.getaddresses(name)

    return ips unless ips.nil? || ips.empty?
  end

  []
end

#names_for(ip) ⇒ Object

Get names for given ip

Parameters:

  • ip (String)

    The ip which should be resolved.



44
45
46
47
48
49
50
51
52
# File 'lib/middleman-core/dns_resolver.rb', line 44

def names_for(ip)
  resolvers.each do |r|
    names = r.getnames(ip)

    return names unless names.nil? || names.empty?
  end

  []
end