Class: FTW::DNS

Inherits:
Object
  • Object
show all
Extended by:
Singleton
Defined in:
lib/ftw/dns.rb

Overview

I wrap whatever Ruby provides because it is historically very inconsistent in implementation behavior across ruby platforms and versions. In the future, this will probably implement the DNS protocol, but for now chill in the awkward, but already-written, ruby stdlib.

I didn’t really want to write a DNS library, but a consistent API and behavior is necessary for my continued sanity :)

Constant Summary collapse

V4_IN_V6_PREFIX =

TODO(sissel): Switch to using Resolv::DNS since it lets you (the programmer) choose dns configuration (servers, etc)

"0:" * 12

Instance Method Summary collapse

Methods included from Singleton

included, singleton

Instance Method Details

#resolve(hostname) ⇒ Object

Resolve a hostname.

It will return an array of all known addresses for the host.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ftw/dns.rb', line 24

def resolve(hostname)
  official, aliases, family, *addresses = Socket.gethostbyname(hostname)
  # We ignore family, here. Ruby will return v6 *and* v4 addresses in
  # the same gethostbyname() call. It is confusing. 
  #
  # Let's just rely entirely on the length of the address string.
  return addresses.collect do |address|
    if address.length == 16
      unpack_v6(address)
    else
      unpack_v4(address)
    end
  end
end

#resolve_random(hostname) ⇒ Object

Resolve hostname and choose one of the results at random.

Use this method if you are connecting to a hostname that resolves to multiple addresses.



43
44
45
46
# File 'lib/ftw/dns.rb', line 43

def resolve_random(hostname)
  addresses = resolve(hostname)
  return addresses[rand(addresses.size)]
end