Class: DNSTraverse::InfoCache

Inherits:
Object
  • Object
show all
Defined in:
lib/dnstraverse/info_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ InfoCache

Returns a new instance of InfoCache.



31
32
33
34
35
# File 'lib/dnstraverse/info_cache.rb', line 31

def initialize(parent = nil)
  @parent = parent
  @data = Hash.new
  self
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



21
22
23
# File 'lib/dnstraverse/info_cache.rb', line 21

def parent
  @parent
end

Instance Method Details

#add(rrs) ⇒ Object

adds the resource records, clearing out any existing entries with the same details



39
40
41
42
43
44
45
46
# File 'lib/dnstraverse/info_cache.rb', line 39

def add(rrs)
  rrs.each {|rr| @data[key(rr)] = Array.new } # clear out
  for rr in rrs do
    @data[key(rr)].push rr
    Log.debug { "Adding to infocache: #{rr}" }
  end
  return nil
end

#add_hints(domain, ns) ⇒ Object

array of hashes containing :name (server name) and :ips (array of strings) set domain to ” for setting root hints



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dnstraverse/info_cache.rb', line 50

def add_hints(domain, ns)
  rrs = Array.new
  for server in ns do
    rrs.push Dnsruby::RR.create(:name => domain, :ttl => 0,
                                :type => 'NS', :domainname => server[:name])
    for ip in server[:ips] do
      type = (ip.to_s =~ /\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/) ? 'A' : 'AAAA'
      rrs.push Dnsruby::RR.create(:type => type, :ttl => 0,
                                  :name => server[:name], :address => ip)
    end
  end
  return add(rrs)
end

#get?(args) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/dnstraverse/info_cache.rb', line 64

def get?(args)
  qclass = args[:qclass] || 'IN'
  gkey = "#{args[:qname]}:#{qclass}:#{args[:qtype]}".downcase
  if @data.has_key?(gkey) then
    Log.debug { "Infocache recall: " + @data[gkey].join(', ')}
    return @data[gkey] # returns Array
  end
  return nil unless parent
  return parent.get?(args)
end

#get_ns?(domain) ⇒ Boolean

get an appropriate ns based on domain

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dnstraverse/info_cache.rb', line 75

def get_ns?(domain) # get an appropriate ns based on domain
  domain = domain.to_s
  while true do
    Log.debug { "Infocache get_ns? checking NS records for '#{domain}'" }
    rrs = get?(:qname => domain, :qtype => 'NS')
    return rrs if rrs
    if domain == '' then
      raise "No nameservers available for #{domain} -- no root hints set??"
    end
    domain = (i = domain.index('.')) ? domain[i+1..-1] : ''
  end
end

#get_startservers(domain, nsatype = 'A') ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/dnstraverse/info_cache.rb', line 88

def get_startservers(domain, nsatype = 'A')
  Log.debug { "Getting startservers for #{domain}/#{nsatype}" }
  newbailiwick = nil
  # search for best NS records in authority cache based on this domain name
  ns = get_ns?(domain)
  starters = Array.new
  # look up in additional cache corresponding IP addresses if we know them
  for rr in ns do
    nameserver = rr.domainname.to_s
    iprrs = get?(:qname => nameserver, :qtype => nsatype)
    ips = iprrs ? iprrs.map {|iprr| iprr.address.to_s } : nil
    starters.push({ :name => nameserver, :ips => ips })
  end
  newbailiwick = ns[0].name.to_s
  newbailiwick = nil if newbailiwick == '' # back to root
  Log.debug { "For domain #{domain} using start servers: " +
    starters.map { |x| x[:name] }.join(', ') }
  return starters, newbailiwick
end