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.logging.InternalLogger;
20  import io.netty.util.internal.logging.InternalLoggerFactory;
21  
22  import javax.naming.Context;
23  import javax.naming.directory.DirContext;
24  import javax.naming.directory.InitialDirContext;
25  import java.net.InetSocketAddress;
26  import java.net.URI;
27  import java.net.URISyntaxException;
28  import java.util.Hashtable;
29  import java.util.List;
30  
31  final class DirContextUtils {
32      private static final InternalLogger logger =
33              InternalLoggerFactory.getInstance(DirContextUtils.class);
34  
35      private DirContextUtils() { }
36  
37      static void addNameServers(List<InetSocketAddress> defaultNameServers, int defaultPort) {
38          // Using jndi-dns to obtain the default name servers.
39          //
40          // See:
41          // - https://docs.oracle.com/javase/8/docs/technotes/guides/jndi/jndi-dns.html
42          // - https://mail.openjdk.java.net/pipermail/net-dev/2017-March/010695.html
43          Hashtable<String, String> env = new Hashtable<String, String>();
44          env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
45          env.put("java.naming.provider.url", "dns://");
46  
47          try {
48              DirContext ctx = new InitialDirContext(env);
49              String dnsUrls = (String) ctx.getEnvironment().get("java.naming.provider.url");
50              // Only try if not empty as otherwise we will produce an exception
51              if (dnsUrls != null && !dnsUrls.isEmpty()) {
52                  String[] servers = dnsUrls.split(" ");
53                  for (String server : servers) {
54                      try {
55                          URI uri = new URI(server);
56                          String host = uri.getHost();
57                          if (host == null || host.isEmpty()) {
58                              logger.debug(
59                                      "Skipping a nameserver URI as host portion could not be extracted: {}", server);
60                              // If the host portion can not be parsed we should just skip this entry.
61                              continue;
62                          }
63                          int port  = uri.getPort();
64                          defaultNameServers.add(SocketUtils.socketAddress(uri.getHost(), port == -1 ?
65                                  defaultPort : port));
66                      } catch (URISyntaxException e) {
67                          logger.debug("Skipping a malformed nameserver URI: {}", server, e);
68                      }
69                  }
70              }
71          } catch (Exception ex) {
72              // Will try reflection if this fails.
73              logger.debug("Unable to obtain nameservers via InitialDirContext", ex);
74          }
75      }
76  }