@@ -8,24 +8,56 @@ class Net::LDAP::Connection #:nodoc:
88
99 def initialize ( server )
1010 @instrumentation_service = server [ :instrumentation_service ]
11+ server [ :hosts ] = [ [ server [ :host ] , server [ :port ] ] ] if server [ :hosts ] . nil?
1112
13+ if server [ :socket ]
14+ prepare_socket ( server )
15+ else
16+ open_connection ( server )
17+ end
18+
19+ yield self if block_given?
20+ end
21+
22+ def prepare_socket ( server )
23+ @conn = server [ :socket ]
24+
25+ if server [ :encryption ]
26+ setup_encryption server [ :encryption ]
27+ end
28+ end
29+
30+ def open_connection ( server )
31+ errors = [ ]
32+ server [ :hosts ] . each do |host , port |
33+ begin
34+ return connect_to_host ( host , port , server )
35+ rescue Net ::LDAP ::Error
36+ errors << $!
37+ end
38+ end
39+
40+ raise errors . first if errors . size == 1
41+ raise Net ::LDAP ::Error ,
42+ "Unable to connect to any given server: \n #{ errors . join ( "\n " ) } "
43+ end
44+
45+ def connect_to_host ( host , port , server )
1246 begin
13- @conn = server [ :socket ] || TCPSocket . new ( server [ : host] , server [ : port] )
47+ @conn = TCPSocket . new ( host , port )
1448 rescue SocketError
1549 raise Net ::LDAP ::Error , "No such address or other socket error."
1650 rescue Errno ::ECONNREFUSED
17- raise Net ::LDAP ::ConnectionRefusedError , "Server #{ server [ : host] } refused connection on port #{ server [ : port] } ."
51+ raise Net ::LDAP ::ConnectionRefusedError , "Server #{ host } refused connection on port #{ port } ."
1852 rescue Errno ::EHOSTUNREACH => error
19- raise Net ::LDAP ::Error , "Host #{ server [ : host] } was unreachable (#{ error . message } )"
53+ raise Net ::LDAP ::Error , "Host #{ host } was unreachable (#{ error . message } )"
2054 rescue Errno ::ETIMEDOUT
21- raise Net ::LDAP ::Error , "Connection to #{ server [ : host] } timed out."
55+ raise Net ::LDAP ::Error , "Connection to #{ host } timed out."
2256 end
2357
2458 if server [ :encryption ]
2559 setup_encryption server [ :encryption ]
2660 end
27-
28- yield self if block_given?
2961 end
3062
3163 module GetbyteForSSLSocket
@@ -63,18 +95,18 @@ def self.wrap_with_ssl(io, tls_options = {})
6395 end
6496
6597 #--
66- # Helper method called only from new , and only after we have a
67- # successfully-opened @conn instance variable, which is a TCP connection.
68- # Depending on the received arguments, we establish SSL, potentially
69- # replacing the value of @conn accordingly. Don't generate any errors here
70- # if no encryption is requested. DO raise Net::LDAP::Error objects if encryption
71- # is requested and we have trouble setting it up. That includes if OpenSSL
72- # is not set up on the machine. (Question: how does the Ruby OpenSSL
73- # wrapper react in that case?) DO NOT filter exceptions raised by the
74- # OpenSSL library. Let them pass back to the user. That should make it
75- # easier for us to debug the problem reports. Presumably (hopefully?) that
76- # will also produce recognizable errors if someone tries to use this on a
77- # machine without OpenSSL.
98+ # Helper method called only from prepare_socket or open_connection , and only
99+ # after we have a successfully-opened @conn instance variable, which is a TCP
100+ # connection. Depending on the received arguments, we establish SSL,
101+ # potentially replacing the value of @conn accordingly. Don't generate any
102+ # errors here if no encryption is requested. DO raise Net::LDAP::Error objects
103+ # if encryption is requested and we have trouble setting it up. That includes
104+ # if OpenSSL is not set up on the machine. (Question: how does the Ruby
105+ # OpenSSL wrapper react in that case?) DO NOT filter exceptions raised by the
106+ # OpenSSL library. Let them pass back to the user. That should make it easier
107+ # for us to debug the problem reports. Presumably (hopefully?) that will also
108+ # produce recognizable errors if someone tries to use this on a machine
109+ # without OpenSSL.
78110 #
79111 # The simple_tls method is intended as the simplest, stupidest, easiest
80112 # solution for people who want nothing more than encrypted comms with the
0 commit comments