123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Srv::Result Private

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongo/srv/result.rb

Overview

SRV record lookup result.

Contains server addresses that the query resolved to, and minimum TTL of the DNS records.

Constant Summary

  • MISMATCHED_DOMAINNAME =

    Returns:

    • (String)

      MISMATCHED_DOMAINNAME Error message format string indicating that an SRV record found does not match the domain of a hostname.

    # File 'lib/mongo/srv/result.rb', line 30
    'Parent domain name in SRV record result (%s) does not match ' +
    'that of the hostname (%s)'

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Mongo::Address::Validator - Included

#validate_address_str!

Takes an address string in ipv4/ipv6/hostname/socket path format and validates its format.

#validate_hostname!

Validates format of the hostname, in particular for further use as the origin in same origin verification.

#validate_port_str!

Instance Attribute Details

#address_strsArray<String> (readonly)

Returns:

  • (Array<String>)

    address_strs The host strings of the SRV records for the query hostname.

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 38

attr_reader :address_strs

#empty?Boolean (readonly)

Checks whether there are any records.

Returns:

  • (Boolean)

    Whether or not there are any records.

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 56

def empty?
  @address_strs.empty?
end

#min_ttlInteger | nil (rw)

Returns:

  • (Integer | nil)

    min_ttl The smallest TTL found among the records (or nil if no records have been added).

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 42

attr_accessor :min_ttl

#query_hostnameString (readonly)

Returns:

  • (String)

    query_hostname The hostname pointing to the DNS records.

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 34

attr_reader :query_hostname

Instance Method Details

#add_record(record)

Adds a new record.

Parameters:

  • record (Resolv::DNS::Resource)

    An SRV record found for the hostname.

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 63

def add_record(record)
  record_host = normalize_hostname(record.target.to_s)
  port = record.port
  validate_hostname!(record_host)
  validate_same_origin!(record_host)
  address_str = if record_host.index(':')
                  # IPV6 address
                  "[#{record_host}]:#{port}"
                else
                  "#{record_host}:#{port}"
                end
  @address_strs << address_str

  @min_ttl = if @min_ttl.nil?
               record.ttl
             else
               [ @min_ttl, record.ttl ].min
             end

  nil
end

#normalize_hostname(host) (private)

Transforms the provided hostname to simplify its validation later on.

This method is safe to call during both initial DNS seed list discovery and during SRV monitoring, in that it does not convert invalid hostnames into valid ones.

  • Converts the hostname to lower case.

  • Removes one trailing dot, if there is exactly one. If the hostname has multiple trailing dots, it is unchanged.

Parameters:

  • host (String)

    Hostname to transform.

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 98

def normalize_hostname(host)
  host = host.downcase
  host = host.delete_suffix('.') unless host.end_with?('..')
  host
end

#validate_same_origin!(record_host) (private)

Ensures that a record’s domain name matches that of the hostname.

A hostname’s domain name consists of each of the ‘.’ delineated parts after the first. For example, the hostname ‘foo.bar.baz’ has the domain name ‘bar.baz’.

If the hostname has less than three parts, its domain name is the hostname itself.

Parameters:

  • record_host (String)

    The host of the SRV record.

Raises:

[ GitHub ]

  
# File 'lib/mongo/srv/result.rb', line 116

def validate_same_origin!(record_host)
  srv_host_domain = query_hostname.split('.')
  srv_is_less_than_three_parts = srv_host_domain.length < 3
  srv_host_domain = srv_host_domain[1..-1] unless srv_is_less_than_three_parts
  record_host_parts = record_host.split('.')

  if srv_is_less_than_three_parts && record_host_parts.length <= srv_host_domain.length
    raise Error::MismatchedDomain.new(format(MISMATCHED_DOMAINNAME, record_host, srv_host_domain))
  end

  unless (record_host_parts.size > srv_host_domain.size) && (srv_host_domain == record_host_parts[-srv_host_domain.size..-1])
    raise Error::MismatchedDomain.new(format(MISMATCHED_DOMAINNAME, record_host, srv_host_domain))
  end
end