1
1
/*
2
- * Copyright (c) 1999, 2023 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 1999, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
120
120
public final class Connection implements Runnable {
121
121
122
122
private static final boolean debug = false ;
123
- private static final int dump = 0 ; // > 0 r, > 1 rw
124
-
125
123
126
124
final private Thread worker ; // Initialized in constructor
127
125
128
- private boolean v3 = true ; // Set in setV3()
126
+ private boolean v3 = true ; // Set in setV3()
129
127
130
- final public String host ; // used by LdapClient for generating exception messages
131
- // used by StartTlsResponse when creating an SSL socket
132
- final public int port ; // used by LdapClient for generating exception messages
133
- // used by StartTlsResponse when creating an SSL socket
128
+ public final String host ; // used by LdapClient for generating exception messages
129
+ // used by StartTlsResponse when creating an SSL socket
130
+ public final int port ; // used by LdapClient for generating exception messages
131
+ // used by StartTlsResponse when creating an SSL socket
134
132
135
133
private boolean bound = false ; // Set in setBound()
136
134
@@ -319,30 +317,37 @@ private SocketFactory getSocketFactory(String socketFactoryName) throws Exceptio
319
317
}
320
318
321
319
private Socket createConnectionSocket (String host , int port , SocketFactory factory ,
322
- int connectTimeout ) throws Exception {
320
+ int connectTimeout ) throws IOException {
323
321
Socket socket = null ;
324
322
323
+ // if timeout is supplied, try to use unconnected socket for connecting with timeout
325
324
if (connectTimeout > 0 ) {
326
- // create unconnected socket and then connect it if timeout
327
- // is supplied
328
- InetSocketAddress endpoint =
329
- createInetSocketAddress (host , port );
330
- // unconnected socket
331
- socket = factory .createSocket ();
332
- // connect socket with a timeout
333
- socket .connect (endpoint , connectTimeout );
334
325
if (debug ) {
335
- System .err .println ("Connection: creating socket with " +
336
- "a connect timeout" );
326
+ System .err .println ("Connection: creating socket with a connect timeout" );
327
+ }
328
+ try {
329
+ // unconnected socket
330
+ socket = factory .createSocket ();
331
+ } catch (IOException e ) {
332
+ // unconnected socket is likely not supported by the SocketFactory
333
+ if (debug ) {
334
+ System .err .println ("Connection: unconnected socket not supported by SocketFactory" );
335
+ }
336
+ }
337
+ if (socket != null ) {
338
+ InetSocketAddress endpoint = createInetSocketAddress (host , port );
339
+ // connect socket with a timeout
340
+ socket .connect (endpoint , connectTimeout );
337
341
}
338
342
}
343
+
344
+ // either no timeout was supplied or unconnected socket did not work
339
345
if (socket == null ) {
340
346
// create connected socket
341
- socket = factory .createSocket (host , port );
342
347
if (debug ) {
343
- System .err .println ("Connection: creating connected socket with" +
344
- " no connect timeout" );
348
+ System .err .println ("Connection: creating connected socket with no connect timeout" );
345
349
}
350
+ socket = factory .createSocket (host , port );
346
351
}
347
352
return socket ;
348
353
}
@@ -351,7 +356,7 @@ private Socket createConnectionSocket(String host, int port, SocketFactory facto
351
356
// the SSL handshake following socket connection as part of the timeout.
352
357
// So explicitly set a socket read timeout, trigger the SSL handshake,
353
358
// then reset the timeout.
354
- private void initialSSLHandshake (SSLSocket sslSocket , int connectTimeout ) throws Exception {
359
+ private void initialSSLHandshake (SSLSocket sslSocket , int connectTimeout ) throws Exception {
355
360
356
361
if (!IS_HOSTNAME_VERIFICATION_DISABLED ) {
357
362
SSLParameters param = sslSocket .getSSLParameters ();
0 commit comments