Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8173451

com.sun.jndi.ldap.connect.timeout parameter does not work for ldaps (ldap over ssl)

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.8.0_101"
      Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
      Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

      I have also tried the same with the latest java version, as well

      ADDITIONAL OS VERSION INFORMATION :
      Linux Cent OS
      Mac OSX

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Just add the parameter com.sun.jndi.ldap.connect.timeout for ldaps (ldap over ssl), i am getting the error, java.net.SocketException: Unconnected sockets not implemented.

      It works fine for ldap without ssl.

      A DESCRIPTION OF THE PROBLEM :
      When i add the parameter com.sun.jndi.ldap.connect.timeout for ldaps (ldap over ssl), i am getting the error, java.net.SocketException: Unconnected sockets not implemented.

      The error is hit when calling the function, InitialLdapContext
      javax.naming.ldap.InitialLdapContext#InitialLdapContext(java.util.Hashtable<?,?>, javax.naming.ldap.Control[])

      It works fine for ldap without ssl.

      I believe the root cause is because the createSocket() method is invoked without any parameter when a connect.timeout is specified.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Set the parameter, com.sun.jndi.ldap.connect.timeout to a value like 30000
      Create ldap connection over ssl

      <refer to source code for more details>

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      successful ldaps (ldap over ssl) connection should created.

      I am expecting the same behavior as ldap without ssl
      ACTUAL -
      Got exception : Caused by: java.net.SocketException: Unconnected sockets not implemented

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      exc: Caused by: javax.naming.CommunicationException: <ip>:<port> [Root exception is java.net.SocketException: Unconnected sockets not implemented] │
        exc: at com.sun.jndi.ldap.Connection.<init>(Connection.java:216) │
        exc: at com.sun.jndi.ldap.LdapClient.<init>(LdapClient.java:137) │
        exc: at com.sun.jndi.ldap.LdapClient.getInstance(LdapClient.java:1614) │
        exc: at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2746) │
        exc: at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:319) │
        exc: at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:192) │
        exc: at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:210) │
        exc: at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:153) │
        exc: at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:83) │
        exc: at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684) │
        exc: at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) │
        exc: at javax.naming.InitialContext.init(InitialContext.java:244) │
        exc: at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:154) │
        exc: at com.snaplogic.snaps.ad.ADBasicAuth.connect(ADBasicAuth.java:93) │
        exc: ... 46 more │
        exc: Caused by: java.net.SocketException: Unconnected sockets not implemented │
        exc: at javax.net.SocketFactory.createSocket(SocketFactory.java:125) │
        exc: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) │
        exc: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) │
        exc: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) │
        exc: at java.lang.reflect.Method.invoke(Method.java:498) │
        exc: at com.sun.jndi.ldap.Connection.createSocket(Connection.java:303) │
        exc: at com.sun.jndi.ldap.Connection.<init>(Connection.java:203) │
        exc: ... 59 more │
        exc: Caused by: java.lang.UnsupportedOperationException │
        exc: at javax.net.SocketFactory.createSocket(SocketFactory.java:123) │
        exc: ... 65 more │
        exc:

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.net.Socket;
      import java.net.ServerSocket;
      import javax.net.ServerSocketFactory;
      import javax.net.ssl.SSLServerSocketFactory;
      import java.io.*;
      import javax.naming.*;
      import javax.naming.directory.*;
      import java.util.Hashtable;

      public class ReadTimeoutTest {
        
      static boolean ssl = false;

          public static void main(String[] args) throws Exception {
        
      boolean passed = false;

      if (args.length > 0 && args[0].equalsIgnoreCase("SSL")) {
      System.out.println("SSL on");
      ssl = true;
      } else {
      System.out.println("SSL off");
      ssl = false;
      }

      // Set up the environment for creating the initial context
      Hashtable env = new Hashtable(11);
      env.put(Context.INITIAL_CONTEXT_FACTORY,
      "com.sun.jndi.ldap.LdapCtxFactory");
      env.put("com.sun.jndi.ldap.connect.timeout", "2000");
      if (ssl) {
              env.put(Context.PROVIDER_URL, "ldaps://localhost:2001");
        } else {
              env.put(Context.PROVIDER_URL, "ldap://localhost:2001");
        }

      Server s = new Server();

      try {

      // start the server
      s.start();

      // Create initial context
      DirContext ctx = new InitialDirContext(env);
      System.out.println("LDAP Client: Connected to the Server");

      // Close the context when we're done
      ctx.close();
      } catch (NamingException e) {
      e.printStackTrace();
      }
      s.interrupt();
          }

          static class Server extends Thread {

              static int serverPort = 2001;

      Server() {
      }

      public void run() {
      try {
              ServerSocket serverSock;
              
              if (ssl) {
                SSLServerSocketFactory ssocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
                serverSock = ssocketFactory.createServerSocket(serverPort);
              } else {
                serverSock = new ServerSocket(serverPort);
              }
                 Socket socket = serverSock.accept();
                   System.out.println("Server: Connection accepted");

                   BufferedInputStream bin = new BufferedInputStream(socket.
                                      getInputStream());
                   while (true) {
      // bin.read(); // If uncommented, bug does not appear.
                   }
      } catch (IOException e) {
      e.printStackTrace();
      // ignore
      }
             }
          }
      }
      ---------- END SOURCE ----------
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None.

      I am not setting connection timeout to avoid the issue.

      SUPPORT :
      YES

            wxiao Weibing Xiao
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: