Class: Mongo::Address::IPv6

Inherits:
Object
  • Object
show all
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 collapse

MATCH =

The regular expression to use to match an IPv6 ip address.

Since:

  • 2.0.0

Regexp.new('::').freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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



83
84
85
86
87
# File 'lib/mongo/address/ipv6.rb', line 83

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

Instance Attribute Details

#hostString (readonly)

Returns host The host.

Returns:

  • (String)

    host The host.

Since:

  • 2.0.0



25
26
27
# File 'lib/mongo/address/ipv6.rb', line 25

def host
  @host
end

#host_nameString (readonly)

Returns host_name The original host name.

Returns:

  • (String)

    host_name The original host name.

Since:

  • 2.0.0



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

def host_name
  @host_name
end

#portInteger (readonly)

Returns port The port.

Returns:

  • (Integer)

    port The port.

Since:

  • 2.0.0



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

def port
  @port
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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongo/address/ipv6.rb', line 48

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 Method Details

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

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.

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

    SSL options.

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

    The options.

Options Hash (options):

  • :connect_timeout (Float)

    Connect timeout.

Returns:

Since:

  • 2.0.0



103
104
105
106
107
108
109
# File 'lib/mongo/address/ipv6.rb', line 103

def socket(socket_timeout, ssl_options = {}, options = {})
  unless ssl_options.empty?
    Socket::SSL.new(host, port, host_name, socket_timeout, Socket::PF_INET6, ssl_options.merge(options))
  else
    Socket::TCP.new(host, port, socket_timeout, Socket::PF_INET6, options)
  end
end