File: address.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (315 lines) | stat: -rw-r--r-- 10,727 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# frozen_string_literal: true
# rubocop:todo all

# Copyright (C) 2014-2020 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'mongo/address/ipv4'
require 'mongo/address/ipv6'
require 'mongo/address/unix'
require 'mongo/address/validator'

module Mongo

  # Represents an address to a server, either with an IP address or socket
  # path.
  #
  # @since 2.0.0
  class Address
    extend Forwardable

    # Mapping from socket family to resolver class.
    #
    # @since 2.0.0
    FAMILY_MAP = {
      ::Socket::PF_UNIX => Unix,
      ::Socket::AF_INET6 => IPv6,
      ::Socket::AF_INET => IPv4
    }.freeze

    # The localhost constant.
    #
    # @since 2.1.0
    LOCALHOST = 'localhost'.freeze

    # Initialize the address.
    #
    # @example Initialize the address with a DNS entry and port.
    #   Mongo::Address.new("app.example.com:27017")
    #
    # @example Initialize the address with a DNS entry and no port.
    #   Mongo::Address.new("app.example.com")
    #
    # @example Initialize the address with an IPV4 address and port.
    #   Mongo::Address.new("127.0.0.1:27017")
    #
    # @example Initialize the address with an IPV4 address and no port.
    #   Mongo::Address.new("127.0.0.1")
    #
    # @example Initialize the address with an IPV6 address and port.
    #   Mongo::Address.new("[::1]:27017")
    #
    # @example Initialize the address with an IPV6 address and no port.
    #   Mongo::Address.new("[::1]")
    #
    # @example Initialize the address with a unix socket.
    #   Mongo::Address.new("/path/to/socket.sock")
    #
    # @param [ String ] seed The provided address.
    # @param [ Hash ] options The address options.
    #
    # @option options [ Float ] :connect_timeout Connect timeout.
    #
    # @since 2.0.0
    def initialize(seed, options = {})
      if seed.nil?
        raise ArgumentError, "address must be not nil"
      end
      @seed = seed
      @host, @port = parse_host_port
      @options = Hash[options.map { |k, v| [k.to_sym, v] }]
    end

    # @return [ String ] seed The seed address.
    attr_reader :seed

    # @return [ String ] host The original host name.
    attr_reader :host

    # @return [ Integer ] port The port.
    attr_reader :port

    # @api private
    attr_reader :options

    # Check equality of the address to another.
    #
    # @example Check address equality.
    #   address == other
    #
    # @param [ Object ] other The other object.
    #
    # @return [ true, false ] If the objects are equal.
    #
    # @since 2.0.0
    def ==(other)
      return false unless other.is_a?(Address)
      host == other.host && port == other.port
    end

    # Check equality for hashing.
    #
    # @example Check hashing equality.
    #   address.eql?(other)
    #
    # @param [ Object ] other The other object.
    #
    # @return [ true, false ] If the objects are equal.
    #
    # @since 2.2.0
    def eql?(other)
      self == other
    end

    # Calculate the hash value for the address.
    #
    # @example Calculate the hash value.
    #   address.hash
    #
    # @return [ Integer ] The hash value.
    #
    # @since 2.0.0
    def hash
      [ host, port ].hash
    end

    # Get a pretty printed address inspection.
    #
    # @example Get the address inspection.
    #   address.inspect
    #
    # @return [ String ] The nice inspection string.
    #
    # @since 2.0.0
    def inspect
      "#<Mongo::Address:0x#{object_id} address=#{to_s}>"
    end

    # Get a socket for the address stored in this object, given the options.
    #
    # If the address stored in this object looks like a Unix path, this method
    # returns a Unix domain socket for this path.
    #
    # Otherwise, this method attempts to resolve the address stored in
    # this object to IPv4 and IPv6 addresses using +Socket#getaddrinfo+, then
    # connects to the resulting addresses and returns the socket of the first
    # successful connection. The order in which address families (IPv4/IPV6)
    # are tried is the same order in which the addresses are returned by
    # +getaddrinfo+, and is determined by the host system.
    #
    # Name resolution is performed on each +socket+ call. This is done so that
    # any changes to which addresses the host names used as seeds or in
    # server configuration resolve to are immediately noticed by the driver,
    # even if a socket has been connected to the affected host name/address
    # before. However, note that DNS TTL values may still affect when a change
    # to a host address is noticed by the driver.
    #
    # This method propagates any exceptions raised during DNS resolution and
    # subsequent connection attempts. In case of a host name resolving to
    # multiple IP addresses, the error raised by the last attempt is propagated
    # to the caller. This method does not map exceptions to Mongo::Error
    # subclasses, and may raise any subclass of Exception.
    #
    # @example Get a socket.
    #   address.socket(5, :ssl => true)
    #
    # @param [ Float ] socket_timeout The socket timeout.
    # @param [ Hash ] opts The options.
    #
    # @option opts [ Float ] :connect_timeout Connect timeout.
    # @option opts [ Boolean ] :csot Whether the client-side operation timeout
    #   should be considered when connecting the socket. This option influences
    #   only what errors will be raised if timeout expires.
    # @option opts [ true | false ] :ssl Whether to use SSL.
    # @option opts [ String ] :ssl_ca_cert
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ Array<OpenSSL::X509::Certificate> ] :ssl_ca_cert_object
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_ca_cert_string
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_cert
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ OpenSSL::X509::Certificate ] :ssl_cert_object
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_cert_string
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_key
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ OpenSSL::PKey ] :ssl_key_object
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_key_pass_phrase
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ String ] :ssl_key_string
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ true, false ] :ssl_verify
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ true, false ] :ssl_verify_certificate
    #   Same as the corresponding Client/Socket::SSL option.
    # @option opts [ true, false ] :ssl_verify_hostname
    #   Same as the corresponding Client/Socket::SSL option.
    #
    # @return [ Mongo::Socket::SSL | Mongo::Socket::TCP | Mongo::Socket::Unix ]
    #   The socket.
    #
    # @raise [ Mongo::Error ] If network connection failed.
    #
    # @since 2.0.0
    # @api private
    def socket(socket_timeout, opts = {})
      csot = !!opts[:csot]
      opts = {
        connect_timeout: Server::CONNECT_TIMEOUT,
      }.update(options).update(Hash[opts.map { |k, v| [k.to_sym, v] }])

      map_exceptions(csot) do
        if seed.downcase =~ Unix::MATCH
          specific_address = Unix.new(seed.downcase)
          return specific_address.socket(socket_timeout, opts)
        end

        # When the driver connects to "localhost", it only attempts IPv4
        # connections. When the driver connects to other hosts, it will
        # attempt both IPv4 and IPv6 connections.
        family = (host == LOCALHOST) ? ::Socket::AF_INET : ::Socket::AF_UNSPEC
        error = nil
        # Sometimes Socket#getaddrinfo returns the same info more than once
        # (multiple identical items in the returned array). It does not make
        # sense to try to connect to the same address more than once, thus
        # eliminate duplicates here.
        infos = ::Socket.getaddrinfo(host, nil, family, ::Socket::SOCK_STREAM)
        results = infos.map do |info|
          [info[4], info[3]]
        end.uniq
        results.each do |family, address_str|
          begin
            specific_address = FAMILY_MAP[family].new(address_str, port, host)
            socket = specific_address.socket(socket_timeout, opts)
            return socket
          rescue IOError, SystemCallError, Error::SocketTimeoutError, Error::SocketError => e
            error = e
          end
        end
        raise error
      end
    end

    # Get the address as a string.
    #
    # @example Get the address as a string.
    #   address.to_s
    #
    # @return [ String ] The nice string.
    #
    # @since 2.0.0
    def to_s
      if port
        if host.include?(':')
          "[#{host}]:#{port}"
        else
          "#{host}:#{port}"
        end
      else
        host
      end
    end

    private

    def parse_host_port
      address = seed.downcase
      case address
        when Unix::MATCH then Unix.parse(address)
        when IPv6::MATCH then IPv6.parse(address)
        else IPv4.parse(address)
      end
    end

    # Maps some errors to different ones, mostly low-level errors to driver
    # level errors
    #
    # @param [ Boolean ] csot Whether the client-side operation timeout
    #   should be considered when connecting the socket.
    def map_exceptions(csot)
      begin
        yield
      rescue Errno::ETIMEDOUT => e
        if csot
          raise Error::TimeoutError, "#{e.class}: #{e} (for #{self})"
        else
          raise Error::SocketTimeoutError, "#{e.class}: #{e} (for #{self})"
        end
      rescue Error::SocketTimeoutError => e
        if csot
          raise Error::TimeoutError, "#{e.class}: #{e} (for #{self})"
        else
          raise e
        end
      rescue IOError, SystemCallError => e
        raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
      rescue OpenSSL::SSL::SSLError => e
        raise Error::SocketError, "#{e.class}: #{e} (for #{self})"
      end
    end
  end
end