Path: blob/master/src/java.base/unix/classes/sun/net/dns/ResolverConfigurationImpl.java
41137 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.dns;2627import java.util.List;28import java.util.LinkedList;29import java.util.StringTokenizer;30import java.io.BufferedReader;31import java.io.FileReader;32import java.io.IOException;3334/*35* An implementation of ResolverConfiguration for Solaris36* and Linux.37*/3839public class ResolverConfigurationImpl40extends ResolverConfiguration41{42// Lock helds whilst loading configuration or checking43private static Object lock = new Object();4445// Time of last refresh.46private static long lastRefresh = -1;4748// Cache timeout (300 seconds) - should be converted into property49// or configured as preference in the future.50private static final int TIMEOUT = 300000;5152// Resolver options53private final Options opts;5455// Parse /etc/resolv.conf to get the values for a particular56// keyword.57//58private LinkedList<String> resolvconf(String keyword,59int maxperkeyword,60int maxkeywords)61{62LinkedList<String> ll = new LinkedList<>();6364String resolvPath = System.getProperty("ext.net.resolvPath", "/etc/resolv.conf");6566try {67BufferedReader in =68new BufferedReader(new FileReader(resolvPath));69String line;70while ((line = in.readLine()) != null) {71int maxvalues = maxperkeyword;72if (line.isEmpty())73continue;74if (line.charAt(0) == '#' || line.charAt(0) == ';')75continue;76if (!line.startsWith(keyword))77continue;78String value = line.substring(keyword.length());79if (value.isEmpty())80continue;81if (value.charAt(0) != ' ' && value.charAt(0) != '\t')82continue;83StringTokenizer st = new StringTokenizer(value, " \t");84while (st.hasMoreTokens()) {85String val = st.nextToken();86if (val.charAt(0) == '#' || val.charAt(0) == ';') {87break;88}89if ("nameserver".equals(keyword)) {90if (val.indexOf(':') >= 0 &&91val.indexOf('.') < 0 && // skip for IPv4 literals with port92val.indexOf('[') < 0 &&93val.indexOf(']') < 0 ) {94// IPv6 literal, in non-BSD-style.95val = "[" + val + "]";96}97}98ll.add(val);99if (--maxvalues == 0) {100break;101}102}103if (--maxkeywords == 0) {104break;105}106}107in.close();108} catch (IOException ioe) {109// problem reading value110}111112return ll;113}114115private LinkedList<String> searchlist;116private LinkedList<String> nameservers;117118119// Load DNS configuration from OS120121@SuppressWarnings("removal")122private void loadConfig() {123assert Thread.holdsLock(lock);124125// check if cached settings have expired.126if (lastRefresh >= 0) {127long currTime = System.currentTimeMillis();128if ((currTime - lastRefresh) < TIMEOUT) {129return;130}131}132133// get the name servers from /etc/resolv.conf134nameservers =135java.security.AccessController.doPrivileged(136new java.security.PrivilegedAction<>() {137public LinkedList<String> run() {138// typically MAXNS is 3 but we've picked 5 here139// to allow for additional servers if required.140return resolvconf("nameserver", 1, 5);141} /* run */142});143144// get the search list (or domain)145searchlist = getSearchList();146147// update the timestamp on the configuration148lastRefresh = System.currentTimeMillis();149}150151152// obtain search list or local domain153154@SuppressWarnings("removal")155private LinkedList<String> getSearchList() {156157LinkedList<String> sl;158159// first try the search keyword in /etc/resolv.conf160161sl = java.security.AccessController.doPrivileged(162new java.security.PrivilegedAction<>() {163public LinkedList<String> run() {164LinkedList<String> ll;165166// first try search keyword (max 6 domains)167ll = resolvconf("search", 6, 1);168if (ll.size() > 0) {169return ll;170}171172return null;173174} /* run */175176});177if (sl != null) {178return sl;179}180181// No search keyword so use local domain182183// try domain keyword in /etc/resolv.conf184185sl = java.security.AccessController.doPrivileged(186new java.security.PrivilegedAction<>() {187public LinkedList<String> run() {188LinkedList<String> ll;189190ll = resolvconf("domain", 1, 1);191if (ll.size() > 0) {192return ll;193}194return null;195196} /* run */197});198if (sl != null) {199return sl;200}201202// no local domain so try fallback (RPC) domain or203// hostName204205sl = new LinkedList<>();206String domain = fallbackDomain0();207if (domain != null && !domain.isEmpty()) {208sl.add(domain);209}210211return sl;212}213214215// ----216217ResolverConfigurationImpl() {218opts = new OptionsImpl();219}220221@SuppressWarnings("unchecked")222public List<String> searchlist() {223synchronized (lock) {224loadConfig();225226// List is mutable so return a shallow copy227return (List<String>)searchlist.clone();228}229}230231@SuppressWarnings("unchecked")232public List<String> nameservers() {233synchronized (lock) {234loadConfig();235236// List is mutable so return a shallow copy237238return (List<String>)nameservers.clone();239240}241}242243public Options options() {244return opts;245}246247248// --- Native methods --249250static native String fallbackDomain0();251252static {253jdk.internal.loader.BootLoader.loadLibrary("net");254}255256}257258/**259* Implementation of {@link ResolverConfiguration.Options}260*/261class OptionsImpl extends ResolverConfiguration.Options {262}263264265