Module: RightSupport::Net::DNS

Defined in:
lib/right_support/net/dns.rb

Constant Summary collapse

DEFAULT_RESOLVE_OPTIONS =
{
  :address_family => Socket::AF_INET,
  :socket_type    => Socket::SOCK_STREAM,
  :protocol       => Socket::IPPROTO_TCP,
  :retry          => 3,
  :uri            => nil,
}.freeze
STATIC_HOSTNAME_TRANSLATIONS =
{
  # This list of CIDR blocks comes directly from Amazon and
  # can be found here: https://forums.aws.amazon.com/ann.jspa?annID=2051
  'cf-mirror.rightscale.com' => [ '54.192.0.0/16',
                                  '54.230.0.0/16',
                                  '54.239.128.0/18',
                                  '54.240.128.0/18',
                                  '204.246.164.0/22',
                                  '204.246.168.0/22',
                                  '204.246.174.0/23',
                                  '204.246.176.0/20',
                                  '205.251.192.0/19',
                                  '205.251.249.0/24',
                                  '205.251.250.0/23',
                                  '205.251.252.0/23',
                                  '205.251.254.0/24',
                                  '216.137.32.0/19' ],
}.freeze

Class Method Summary collapse

Class Method Details

.resolve(endpoints, opts = {}) ⇒ Array<String>

Perform DNS resolution on a set of endpoints, where the endpoints may be hostnames or URIs. Expand out the list to include one entry per distinct address that is assigned to a given hostname, but preserve other aspects of the endpoints –. URIs will remain URIs with the same protocol, path-info, and so forth, but the hostname component will be resolved to IP addresses and the URI will be duplicated in the output, once for each distinct IP address.

Although this method does accept IPv4 dotted-quad addresses as input, it does not accept IPv6 addresses. However, given hostnames or URIs as input, one can resolve the hostnames to IPv6 addresses by specifying the appropriate address_family in the options.

It should never be necessary to specify a different :socket_type or :protocol, but these options are exposed just in case.

Parameters:

  • endpoints (Array<String>)

    a mixed list of hostnames, IPv4 addresses or URIs that contain them

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :retry (Integer)

    number of times to retry SocketError; default is 3

  • :address_family (Integer)

    what kind of IP addresses to resolve; default is Socket::AF_INET (IPv4)

  • :socket_type (Integer)

    socket-type context to pass to getaddrinfo, default is Socket::SOCK_STREAM

  • :protocol (Integer)

    protocol context to pass to getaddrinfo, default is Socket::IPPROTO_TCP

Returns:

  • (Array<String>)

    larger list of endpoints with all hostnames resolved to IP addresses

Raises:

  • URI::InvalidURIError if endpoints contains an invalid or URI

  • SocketError if endpoints contains an invalid or unresolvable hostname



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/right_support/net/dns.rb', line 165

def self.resolve(endpoints, opts={})
  opts = DEFAULT_RESOLVE_OPTIONS.merge(opts)
  endpoints = Array(endpoints)

  retries = 0
  resolved_endpoints = []

  endpoints.each do |endpoint|
    begin
      resolved_endpoint = nil
      if endpoint.include?(':')
        # It contains a colon, therefore it must be a URI -- we don't support IPv6
        uri = URI.parse(endpoint)
        hostname = uri.host
        raise URI::InvalidURIError, "Could not parse host component of URI" unless hostname

        resolved_endpoint = resolve_endpoint(hostname, opts.merge(:uri=>uri))
      else
        resolved_endpoint = resolve_endpoint(endpoint, opts)
      end
      resolved_endpoints << resolved_endpoint
    rescue SocketError => e
      retries += 1
      if retries < opts[:retry]
        retry
      else
        raise SocketError.new("hostname/endpoint: #{hostname ? hostname : endpoint} opts: #{opts} - #{e.message}")
      end
    end
  end

  resolved_endpoints
end

.resolve_all_ip_addresses(hostnames) ⇒ Object

Deprecated.

due to broken error handling - do not use; please use #resolve instead!

Resolve a set of DNS hostnames to the individual IP addresses to which they map. Only handles IPv4 addresses.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/right_support/net/dns.rb', line 121

def self.resolve_all_ip_addresses(hostnames)
  ips       = []
  hostnames = [hostnames] unless hostnames.respond_to?(:each)
  hostnames.each do |hostname|
    infos = nil
    begin
      infos = Socket.getaddrinfo(hostname, 443, Socket::AF_INET, Socket::SOCK_STREAM, Socket::IPPROTO_TCP)
    rescue Exception => e
      # NOTE: Need to figure out, which logger can we use here?
      # Log.error "Rescued #{e.class.name} resolving Repose hostnames: #{e.message}; retrying"
      retry
    end

    #Randomly permute the addrinfos of each hostname to help spread load.
    infos.shuffle.each do |info|
      ips << info[3]
    end
  end
  ips
end

.resolve_with_hostnames(endpoints, opts = {}) ⇒ Hash<hostnames => [endpoints]>

Similar to resolve, but return a hash of { hostnames => [endpoints] }

Perform DNS resolution on a set of endpoints, where the endpoints may be hostnames or URIs. Expand out the list to include one entry per distinct address that is assigned to a given hostname, but preserve other aspects of the endpoints –. URIs will remain URIs with the same protocol, path-info, and so forth, but the hostname component will be resolved to IP addresses and the URI will be duplicated in the output, once for each distinct IP address.

Although this method does accept IPv4 dotted-quad addresses as input, it does not accept IPv6 addresses. However, given hostnames or URIs as input, one can resolve the hostnames to IPv6 addresses by specifying the appropriate address_family in the options.

It should never be necessary to specify a different :socket_type or :protocol, but these options are exposed just in case.

Parameters:

  • endpoints (Array<String>)

    a mixed list of hostnames, IPv4 addresses or URIs that contain them

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :retry (Integer)

    number of times to retry SocketError; default is 3

  • :address_family (Integer)

    what kind of IP addresses to resolve; default is Socket::AF_INET (IPv4)

  • :socket_type (Integer)

    socket-type context to pass to getaddrinfo, default is Socket::SOCK_STREAM

  • :protocol (Integer)

    protocol context to pass to getaddrinfo, default is Socket::IPPROTO_TCP

Returns:

  • (Hash<hostnames => [endpoints]>)

    Hash with keys of hostnames and values of arrays of all associated IP addresses

Raises:

  • URI::InvalidURIError if endpoints contains an invalid or URI

  • SocketError if endpoints contains an invalid or unresolvable hostname



224
225
226
227
228
# File 'lib/right_support/net/dns.rb', line 224

def self.resolve_with_hostnames(endpoints, opts={})
  hostname_hash = {}
  endpoints.each {|endpoint| hostname_hash[endpoint] = resolve(endpoint, opts).first}
  hostname_hash
end