58870
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.dns.conf;
|
|
27 |
|
59100
|
28 |
import java.nio.file.Paths;
|
58870
|
29 |
import java.util.LinkedList;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.StringTokenizer;
|
59100
|
32 |
import java.util.concurrent.locks.ReentrantLock;
|
58870
|
33 |
|
|
34 |
public class DnsResolverConfiguration {
|
|
35 |
// Lock held whilst loading configuration or checking
|
|
36 |
private static ReentrantLock lock = new ReentrantLock();
|
|
37 |
|
|
38 |
// Addresses have changed
|
|
39 |
private static boolean changed = false;
|
|
40 |
|
|
41 |
// Time of last refresh.
|
|
42 |
private static long lastRefresh = -1;
|
|
43 |
|
59100
|
44 |
// Cache timeout (120 seconds in nanoseconds) - should be converted into property
|
58870
|
45 |
// or configured as preference in the future.
|
59100
|
46 |
private static final long TIMEOUT = 120_000_000_000L;
|
58870
|
47 |
|
|
48 |
// DNS suffix list and name servers populated by native method
|
|
49 |
private static String os_searchlist;
|
|
50 |
private static String os_nameservers;
|
|
51 |
|
|
52 |
// Cached lists
|
|
53 |
private static LinkedList<String> searchlist;
|
|
54 |
private static LinkedList<String> nameservers;
|
59100
|
55 |
private volatile String domain = "";
|
58870
|
56 |
|
|
57 |
// Parse string that consists of token delimited by space or commas
|
|
58 |
// and return LinkedHashMap
|
|
59 |
private LinkedList<String> stringToList(String str) {
|
|
60 |
LinkedList<String> ll = new LinkedList<>();
|
|
61 |
|
|
62 |
// comma and space are valid delimiters
|
|
63 |
StringTokenizer st = new StringTokenizer(str, ", ");
|
|
64 |
while (st.hasMoreTokens()) {
|
|
65 |
String s = st.nextToken();
|
|
66 |
if (!ll.contains(s)) {
|
|
67 |
ll.add(s);
|
|
68 |
}
|
|
69 |
}
|
|
70 |
return ll;
|
|
71 |
}
|
|
72 |
|
59100
|
73 |
public static String getDefaultHostsFileLocation() {
|
|
74 |
return Paths.get(System.getenv("SystemRoot"))
|
|
75 |
.resolve("System32")
|
|
76 |
.resolve("drivers")
|
|
77 |
.resolve("etc")
|
|
78 |
.resolve("hosts")
|
|
79 |
.toString();
|
|
80 |
}
|
|
81 |
|
58870
|
82 |
// Load DNS configuration from OS
|
|
83 |
|
|
84 |
private void loadConfig() {
|
|
85 |
assert lock.isHeldByCurrentThread();
|
|
86 |
|
|
87 |
// if address have changed then DNS probably changed as well;
|
|
88 |
// otherwise check if cached settings have expired.
|
|
89 |
//
|
|
90 |
if (changed) {
|
|
91 |
changed = false;
|
|
92 |
} else {
|
|
93 |
if (lastRefresh >= 0) {
|
59100
|
94 |
long currTime = System.nanoTime();
|
58870
|
95 |
if ((currTime - lastRefresh) < TIMEOUT) {
|
|
96 |
return;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
// load DNS configuration, update timestamp, create
|
|
102 |
// new HashMaps from the loaded configuration
|
|
103 |
//
|
|
104 |
loadDNSconfig0();
|
|
105 |
|
59100
|
106 |
lastRefresh = System.nanoTime();
|
58870
|
107 |
searchlist = stringToList(os_searchlist);
|
59100
|
108 |
if (searchlist.size() > 0) {
|
|
109 |
domain = searchlist.get(0);
|
|
110 |
} else {
|
|
111 |
domain = "";
|
|
112 |
}
|
58870
|
113 |
nameservers = stringToList(os_nameservers);
|
|
114 |
os_searchlist = null; // can be GC'ed
|
|
115 |
os_nameservers = null;
|
|
116 |
}
|
|
117 |
|
59100
|
118 |
public DnsResolverConfiguration() {
|
58870
|
119 |
}
|
|
120 |
|
|
121 |
@SuppressWarnings("unchecked") // clone()
|
|
122 |
public List<String> searchlist() {
|
|
123 |
lock.lock();
|
|
124 |
try {
|
|
125 |
loadConfig();
|
|
126 |
|
|
127 |
// List is mutable so return a shallow copy
|
|
128 |
return (List<String>) searchlist.clone();
|
|
129 |
} finally {
|
|
130 |
lock.unlock();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
|
|
134 |
@SuppressWarnings("unchecked") // clone()
|
|
135 |
public List<String> nameservers() {
|
59100
|
136 |
lock.lock();
|
58870
|
137 |
try {
|
|
138 |
loadConfig();
|
|
139 |
|
|
140 |
// List is mutable so return a shallow copy
|
|
141 |
return (List<String>) nameservers.clone();
|
|
142 |
} finally {
|
|
143 |
lock.unlock();
|
|
144 |
}
|
|
145 |
}
|
|
146 |
|
59100
|
147 |
public String domain() {
|
|
148 |
lock.lock();
|
|
149 |
try {
|
|
150 |
loadConfig();
|
|
151 |
return domain;
|
|
152 |
} finally {
|
|
153 |
lock.unlock();
|
|
154 |
}
|
|
155 |
}
|
|
156 |
|
58870
|
157 |
// --- Address Change Listener
|
|
158 |
|
|
159 |
static class AddressChangeListener extends Thread {
|
|
160 |
public void run() {
|
|
161 |
for (; ; ) {
|
|
162 |
// wait for configuration to change
|
|
163 |
if (notifyAddrChange0() != 0)
|
|
164 |
return;
|
|
165 |
lock.lock();
|
|
166 |
try {
|
|
167 |
changed = true;
|
|
168 |
} finally {
|
|
169 |
lock.unlock();
|
|
170 |
}
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
// --- Native methods --
|
|
177 |
|
|
178 |
static native void init0();
|
|
179 |
|
|
180 |
static native void loadDNSconfig0();
|
|
181 |
|
|
182 |
static native int notifyAddrChange0();
|
|
183 |
|
|
184 |
static {
|
|
185 |
System.loadLibrary("resolver");
|
|
186 |
init0();
|
|
187 |
|
|
188 |
// start the address listener thread
|
|
189 |
AddressChangeListener thr = new AddressChangeListener();
|
|
190 |
thr.setDaemon(true);
|
|
191 |
thr.start();
|
|
192 |
}
|
|
193 |
}
|