View Javadoc
1   /*
2    * Copyright 2018 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.resolver.dns;
17  
18  import io.netty.util.internal.SocketUtils;
19  import io.netty.util.internal.SystemPropertyUtil;
20  import io.netty.util.internal.logging.InternalLogger;
21  import io.netty.util.internal.logging.InternalLoggerFactory;
22  
23  import javax.naming.Context;
24  import javax.naming.directory.DirContext;
25  import javax.naming.directory.InitialDirContext;
26  import java.net.InetSocketAddress;
27  import java.net.URI;
28  import java.net.URISyntaxException;
29  import java.util.Hashtable;
30  import java.util.List;
31  
32  final class DirContextUtils {
33      private static final InternalLogger logger =
34              InternalLoggerFactory.getInstance(DirContextUtils.class);
35  
36      private DirContextUtils() { }
37  
38      /**
39       * On IBM i the JNDI lookup ends up in {@code LoadDnsConfig}, which needs special authority
40       * ({@code *IOSYSCFG}) and can terminate the whole JVM when it is missing. See
41       * <a href="https://github.com/netty/netty/issues/17396">#17396</a>.
42       */
43      static boolean isIbmI(String osName) {
44          return "OS/400".equalsIgnoreCase(osName);
45      }
46  
47      static void addNameServers(List<InetSocketAddress> defaultNameServers, int defaultPort) {
48          if (isIbmI(SystemPropertyUtil.get("os.name", ""))) {
49              logger.debug("Not obtaining nameservers via InitialDirContext on IBM i");
50              return;
51          }
52          // Using jndi-dns to obtain the default name servers.
53          //
54          // See:
55          // - https://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-dns.html
56          // - https://mail.openjdk.java.net/pipermail/net-dev/2017-March/010695.html
57          Hashtable<String, String> env = new Hashtable<String, String>();
58          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
59          env.put("java.naming.provider.url", "dns://");
60  
61          try {
62              DirContext ctx = new InitialDirContext(env);
63              String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url");
64              // Only try if not empty as otherwise we will produce an exception
65              if (dnsUrls != null && !dnsUrls.isEmpty()) {
66                  String[] servers = dnsUrls.split(" ");
67                  for (String server : servers) {
68                      try {
69                          URI uri = new URI(server);
70                          String host = uri.getHost();
71                          if (host == null || host.isEmpty()) {
72                              logger.debug(
73                                      "Skipping a nameserver URI as host portion could not be extracted: {}", server);
74                              // If the host portion can not be parsed we should just skip this entry.
75                              continue;
76                          }
77                          int port  = uri.getPort();
78                          defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ?
79                                  defaultPort : port));
80                      } catch (URISyntaxException e) {
81                          logger.debug("Skipping a malformed nameserver URI: {}", server, e);
82                      }
83                  }
84              }
85          } catch (Exception ex) {
86              // Will try reflection if this fails.
87              logger.debug("Unable to obtain nameservers via InitialDirContext", ex);
88          }
89      }
90  }