123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Address::IPv6

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/address/ipv6.rb

Overview

Sets up resolution with IPv6 support if the address is an ip address.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(host, port, host_name = nil) ⇒ IPv6

Initialize the IPv6 resolver.

Examples:

Initialize the resolver.

IPv6.new("::1", 28011, 'localhost')

Parameters:

  • host (String)

    The host.

  • port (Integer)

    The port.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 86

def initialize(host, port, host_name=nil)
  @host = host
  @port = port
  @host_name = host_name
end

Class Method Details

.parse(address) ⇒ Array<String, Integer>

Parse an IPv6 address into its host and port.

Examples:

Parse the address.

IPv6.parse("[::1]:28011")

Parameters:

  • address (String)

    The address to parse.

Returns:

  • (Array<String, Integer>)

    The host and port pair.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 51

def self.parse(address)
  # IPAddr's parser handles IP address only, not port.
  # Therefore we need to handle the port ourselves
  if address =~ /[\[\]]/
    parts = address.match(/\A\[(.+)\](?::(\d+))?\z/)
    if parts.nil?
      raise ArgumentError, "Invalid IPv6 address: #{address}"
    end
    host = parts[1]
    port = (parts[2] || 27017).to_i
  else
    host = address
    port = 27017
  end
  # Validate host.
  # This will raise IPAddr::InvalidAddressError
  # on newer rubies which is a subclass of ArgumentError
  # if host is invalid
  begin
    IPAddr.new(host)
  rescue ArgumentError
    raise ArgumentError, "Invalid IPv6 address: #{address}"
  end
  [ host, port ]
end

Instance Attribute Details

#hostString (readonly)

Returns:

  • (String)

    host The host.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 28

attr_reader :host

#host_nameString (readonly)

Returns:

  • (String)

    host_name The original host name.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 31

attr_reader :host_name

#portInteger (readonly)

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 34

attr_reader :port

Instance Method Details

#socket(socket_timeout, options = {}) ⇒ Mongo::Socket::SSL, Mongo::Socket::TCP

This method is for internal use only.

Get a socket for the provided address type, given the options.

Examples:

Get an IPv6 socket.

ipv4.socket(5, :ssl => true)

Parameters:

  • socket_timeout (Float)

    The socket timeout.

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

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

  • :ssl (true | false)

    Whether to use TLS.

  • :ssl_ca_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_object (Array<OpenSSL::X509::Certificate>)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_ca_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_object (OpenSSL::X509::Certificate)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_cert_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_object (OpenSSL::PKey)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_pass_phrase (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_key_string (String)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_certificate (true, false)

    Same as the corresponding Client/Socket::SSL option.

  • :ssl_verify_hostname (true, false)

    Same as the corresponding Client/Socket::SSL option.

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/address/ipv6.rb', line 133

def socket(socket_timeout, options = {})
  if options[:ssl]
    Socket::SSL.new(host, port, host_name, socket_timeout, Socket::PF_INET6, options)
  else
    Socket::TCP.new(host, port, socket_timeout, Socket::PF_INET6, options)
  end
end