lib/ftw/dns.rb in ftw-0.0.4 vs lib/ftw/dns.rb in ftw-0.0.5

- old
+ new

@@ -13,19 +13,19 @@ # choose dns configuration (servers, etc) V4_IN_V6_PREFIX = "0:" * 12 # Get a singleton instance of FTW::DNS - public def self.singleton @resolver ||= self.new end # def self.singleton + private + # Resolve a hostname. # # It will return an array of all known addresses for the host. - public 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. # @@ -41,22 +41,19 @@ # 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. - public def resolve_random(hostname) addresses = resolve(hostname) return addresses[rand(addresses.size)] end # def resolve_random - private def unpack_v4(address) return address.unpack("C4").join(".") end # def unpack_v4 - private def unpack_v6(address) if address.length == 16 # Unpack 16 bit chunks, convert to hex, join with ":" address.unpack("n8").collect { |p| p.to_s(16) } \ .join(":").sub(/(?:0:(?:0:)+)/, "::") @@ -66,6 +63,8 @@ # http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses # http://www.tcpipguide.com/free/t_IPv6IPv4AddressEmbedding.htm "::" + unpack_v4(address) end end # def unpack_v6 + + public(:resolve, :resolve_random) end # class FTW::DNS