Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/classes/sun/net/dns/ResolverConfigurationImpl.java
32288 views
/*1* Copyright (c) 2002, 2012, 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<>();6364try {65BufferedReader in =66new BufferedReader(new FileReader("/etc/resolv.conf"));67String line;68while ((line = in.readLine()) != null) {69int maxvalues = maxperkeyword;70if (line.length() == 0)71continue;72if (line.charAt(0) == '#' || line.charAt(0) == ';')73continue;74if (!line.startsWith(keyword))75continue;76String value = line.substring(keyword.length());77if (value.length() == 0)78continue;79if (value.charAt(0) != ' ' && value.charAt(0) != '\t')80continue;81StringTokenizer st = new StringTokenizer(value, " \t");82while (st.hasMoreTokens()) {83String val = st.nextToken();84if (val.charAt(0) == '#' || val.charAt(0) == ';') {85break;86}87if ("nameserver".equals(keyword)) {88if (val.indexOf(':') >= 0 &&89val.indexOf('.') < 0 && // skip for IPv4 literals with port90val.indexOf('[') < 0 &&91val.indexOf(']') < 0 ) {92// IPv6 literal, in non-BSD-style.93val = "[" + val + "]";94}95}96ll.add(val);97if (--maxvalues == 0) {98break;99}100}101if (--maxkeywords == 0) {102break;103}104}105in.close();106} catch (IOException ioe) {107// problem reading value108}109110return ll;111}112113private LinkedList<String> searchlist;114private LinkedList<String> nameservers;115116117// Load DNS configuration from OS118119private void loadConfig() {120assert Thread.holdsLock(lock);121122// check if cached settings have expired.123if (lastRefresh >= 0) {124long currTime = System.currentTimeMillis();125if ((currTime - lastRefresh) < TIMEOUT) {126return;127}128}129130// get the name servers from /etc/resolv.conf131nameservers =132java.security.AccessController.doPrivileged(133new java.security.PrivilegedAction<LinkedList<String>>() {134public LinkedList<String> run() {135// typically MAXNS is 3 but we've picked 5 here136// to allow for additional servers if required.137return resolvconf("nameserver", 1, 5);138} /* run */139});140141// get the search list (or domain)142searchlist = getSearchList();143144// update the timestamp on the configuration145lastRefresh = System.currentTimeMillis();146}147148149// obtain search list or local domain150151private LinkedList<String> getSearchList() {152153LinkedList<String> sl;154155// first try the search keyword in /etc/resolv.conf156157sl = java.security.AccessController.doPrivileged(158new java.security.PrivilegedAction<LinkedList<String>>() {159public LinkedList<String> run() {160LinkedList<String> ll;161162// first try search keyword (max 6 domains)163ll = resolvconf("search", 6, 1);164if (ll.size() > 0) {165return ll;166}167168return null;169170} /* run */171172});173if (sl != null) {174return sl;175}176177// No search keyword so use local domain178179180// LOCALDOMAIN has absolute priority on Solaris181182String localDomain = localDomain0();183if (localDomain != null && localDomain.length() > 0) {184sl = new LinkedList<String>();185sl.add(localDomain);186return sl;187}188189// try domain keyword in /etc/resolv.conf190191sl = java.security.AccessController.doPrivileged(192new java.security.PrivilegedAction<LinkedList<String>>() {193public LinkedList<String> run() {194LinkedList<String> ll;195196ll = resolvconf("domain", 1, 1);197if (ll.size() > 0) {198return ll;199}200return null;201202} /* run */203});204if (sl != null) {205return sl;206}207208// no local domain so try fallback (RPC) domain or209// hostName210211sl = new LinkedList<>();212String domain = fallbackDomain0();213if (domain != null && domain.length() > 0) {214sl.add(domain);215}216217return sl;218}219220221// ----222223ResolverConfigurationImpl() {224opts = new OptionsImpl();225}226227@SuppressWarnings("unchecked")228public List<String> searchlist() {229synchronized (lock) {230loadConfig();231232// List is mutable so return a shallow copy233return (List<String>)searchlist.clone();234}235}236237@SuppressWarnings("unchecked")238public List<String> nameservers() {239synchronized (lock) {240loadConfig();241242// List is mutable so return a shallow copy243244return (List<String>)nameservers.clone();245246}247}248249public Options options() {250return opts;251}252253254// --- Native methods --255256static native String localDomain0();257258static native String fallbackDomain0();259260static {261java.security.AccessController.doPrivileged(262new java.security.PrivilegedAction<Void>() {263public Void run() {264System.loadLibrary("net");265return null;266}267});268}269270}271272/**273* Implementation of {@link ResolverConfiguration.Options}274*/275class OptionsImpl extends ResolverConfiguration.Options {276}277278279