Class: Mongo::Socket::SSL

Inherits:
Mongo::Socket show all
Includes:
OpenSSL
Defined in:
lib/mongo/socket/ssl.rb

Overview

Wrapper for SSL sockets.

Since:

  • 2.0.0

Constant Summary

Constants inherited from Mongo::Socket

SSL_ERROR, TIMEOUT_ERROR, TIMEOUT_PACK

Instance Attribute Summary collapse

Attributes inherited from Mongo::Socket

#family, #socket

Instance Method Summary collapse

Methods inherited from Mongo::Socket

#alive?, #close, #eof?, #gets, #read, #write

Constructor Details

#initialize(host, port, host_name, timeout, family, options = {}) ⇒ SSL

Initializes a new SSL socket.

Examples:

Create the SSL socket.

SSL.new('::1', 27017, 30)

Parameters:

  • host (String)

    The hostname or IP address.

  • port (Integer)

    The port number.

  • timeout (Float)

    The socket timeout value.

  • family (Integer)

    The socket family.

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

    The ssl options.

Since:

  • 2.0.0



79
80
81
82
83
84
85
86
# File 'lib/mongo/socket/ssl.rb', line 79

def initialize(host, port, host_name, timeout, family, options = {})
  @host, @port, @host_name, @timeout, @options = host, port, host_name, timeout, options
  @context = create_context(options)
  @family = family
  @tcp_socket = ::Socket.new(family, SOCK_STREAM, 0)
  @tcp_socket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)
  set_socket_options(@tcp_socket)
end

Instance Attribute Details

#contextSSLContext (readonly)

Returns context The ssl context.

Returns:

  • (SSLContext)

    context The ssl context.

Since:

  • 2.0.0



27
28
29
# File 'lib/mongo/socket/ssl.rb', line 27

def context
  @context
end

#hostString (readonly)

Returns host The host to connect to.

Returns:

  • (String)

    host The host to connect to.

Since:

  • 2.0.0



30
31
32
# File 'lib/mongo/socket/ssl.rb', line 30

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



33
34
35
# File 'lib/mongo/socket/ssl.rb', line 33

def host_name
  @host_name
end

#optionsHash (readonly)

Returns The ssl options.

Returns:

  • (Hash)

    The ssl options.

Since:

  • 2.0.0



36
37
38
# File 'lib/mongo/socket/ssl.rb', line 36

def options
  @options
end

#portInteger (readonly)

Returns port The port to connect to.

Returns:

  • (Integer)

    port The port to connect to.

Since:

  • 2.0.0



39
40
41
# File 'lib/mongo/socket/ssl.rb', line 39

def port
  @port
end

#timeoutFloat (readonly)

Returns timeout The socket timeout.

Returns:

  • (Float)

    timeout The socket timeout.

Since:

  • 2.0.0



42
43
44
# File 'lib/mongo/socket/ssl.rb', line 42

def timeout
  @timeout
end

Instance Method Details

#connect!(connect_timeout = nil) ⇒ SSL

Note:

This method mutates the object by setting the socket internally.

Establishes a socket connection.

Examples:

Connect the socket.

sock.connect!

Returns:

  • (SSL)

    The connected socket instance.

Since:

  • 2.0.0



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mongo/socket/ssl.rb', line 55

def connect!(connect_timeout = nil)
  Timeout.timeout(connect_timeout, Error::SocketTimeoutError) do
    handle_errors { @tcp_socket.connect(::Socket.pack_sockaddr_in(port, host)) }
    @socket = OpenSSL::SSL::SSLSocket.new(@tcp_socket, context)
    @socket.hostname = @host_name unless BSON::Environment.jruby?
    @socket.sync_close = true
    handle_errors { @socket.connect }
    verify_certificate!(@socket)
    self
  end
end

#connectable?true, false

This socket can only be used if the ssl socket (@socket) has been created.

Examples:

Is the socket connectable?

socket.connectable?

Returns:

  • (true, false)

    If the socket is connectable.

Since:

  • 2.2.5



111
112
113
# File 'lib/mongo/socket/ssl.rb', line 111

def connectable?
  !!@socket
end

#readbyteObject

Read a single byte from the socket.

Examples:

Read a single byte.

socket.readbyte

Returns:

  • (Object)

    The read byte.

Since:

  • 2.0.0



96
97
98
99
100
101
# File 'lib/mongo/socket/ssl.rb', line 96

def readbyte
  handle_errors do
    byte = socket.read(1).bytes.to_a[0]
    byte.nil? ? raise(EOFError) : byte
  end
end