Path: blob/master/src/java.base/unix/classes/sun/net/dns/ResolverConfigurationImpl.java
67773 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<>();6364try {65BufferedReader in =66new BufferedReader(new FileReader("/etc/resolv.conf"));67String line;68while ((line = in.readLine()) != null) {69int maxvalues = maxperkeyword;70if (line.isEmpty())71continue;72if (line.charAt(0) == '#' || line.charAt(0) == ';')73continue;74if (!line.startsWith(keyword))75continue;76String value = line.substring(keyword.length());77if (value.isEmpty())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 OS118119@SuppressWarnings("removal")120private void loadConfig() {121assert Thread.holdsLock(lock);122123// check if cached settings have expired.124if (lastRefresh >= 0) {125long currTime = System.currentTimeMillis();126if ((currTime - lastRefresh) < TIMEOUT) {127return;128}129}130131// get the name servers from /etc/resolv.conf132nameservers =133java.security.AccessController.doPrivileged(134new java.security.PrivilegedAction<>() {135public LinkedList<String> run() {136// typically MAXNS is 3 but we've picked 5 here137// to allow for additional servers if required.138return resolvconf("nameserver", 1, 5);139} /* run */140});141142// get the search list (or domain)143searchlist = getSearchList();144145// update the timestamp on the configuration146lastRefresh = System.currentTimeMillis();147}148149150// obtain search list or local domain151152@SuppressWarnings("removal")153private LinkedList<String> getSearchList() {154155LinkedList<String> sl;156157// first try the search keyword in /etc/resolv.conf158159sl = java.security.AccessController.doPrivileged(160new java.security.PrivilegedAction<>() {161public LinkedList<String> run() {162LinkedList<String> ll;163164// first try search keyword (max 6 domains)165ll = resolvconf("search", 6, 1);166if (ll.size() > 0) {167return ll;168}169170return null;171172} /* run */173174});175if (sl != null) {176return sl;177}178179// No search keyword so use local domain180181// try domain keyword in /etc/resolv.conf182183sl = java.security.AccessController.doPrivileged(184new java.security.PrivilegedAction<>() {185public LinkedList<String> run() {186LinkedList<String> ll;187188ll = resolvconf("domain", 1, 1);189if (ll.size() > 0) {190return ll;191}192return null;193194} /* run */195});196if (sl != null) {197return sl;198}199200// no local domain so try fallback (RPC) domain or201// hostName202203sl = new LinkedList<>();204String domain = fallbackDomain0();205if (domain != null && !domain.isEmpty()) {206sl.add(domain);207}208209return sl;210}211212213// ----214215ResolverConfigurationImpl() {216opts = new OptionsImpl();217}218219@SuppressWarnings("unchecked")220public List<String> searchlist() {221synchronized (lock) {222loadConfig();223224// List is mutable so return a shallow copy225return (List<String>)searchlist.clone();226}227}228229@SuppressWarnings("unchecked")230public List<String> nameservers() {231synchronized (lock) {232loadConfig();233234// List is mutable so return a shallow copy235236return (List<String>)nameservers.clone();237238}239}240241public Options options() {242return opts;243}244245246// --- Native methods --247248static native String fallbackDomain0();249250static {251jdk.internal.loader.BootLoader.loadLibrary("net");252}253254}255256/**257* Implementation of {@link ResolverConfiguration.Options}258*/259class OptionsImpl extends ResolverConfiguration.Options {260}261262263