author | redestad |
Thu, 13 Dec 2018 15:31:05 +0100 | |
changeset 53018 | 8bf9268df0e2 |
parent 47216 | 71c04702a3d5 |
child 55693 | 9a97b1393e72 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
12559
diff
changeset
|
2 |
* Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.dns; |
|
27 |
||
28 |
import java.util.List; |
|
29 |
import java.util.LinkedList; |
|
30 |
import java.util.StringTokenizer; |
|
31 |
import java.io.BufferedReader; |
|
32 |
import java.io.FileReader; |
|
33 |
import java.io.IOException; |
|
34 |
||
35 |
/* |
|
36 |
* An implementation of ResolverConfiguration for Solaris |
|
37 |
* and Linux. |
|
38 |
*/ |
|
39 |
||
40 |
public class ResolverConfigurationImpl |
|
41 |
extends ResolverConfiguration |
|
42 |
{ |
|
43 |
// Lock helds whilst loading configuration or checking |
|
44 |
private static Object lock = new Object(); |
|
45 |
||
46 |
// Time of last refresh. |
|
47 |
private static long lastRefresh = -1; |
|
48 |
||
49 |
// Cache timeout (300 seconds) - should be converted into property |
|
50 |
// or configured as preference in the future. |
|
51 |
private static final int TIMEOUT = 300000; |
|
52 |
||
53 |
// Resolver options |
|
54 |
private final Options opts; |
|
55 |
||
56 |
// Parse /etc/resolv.conf to get the values for a particular |
|
57 |
// keyword. |
|
58 |
// |
|
6112 | 59 |
private LinkedList<String> resolvconf(String keyword, |
60 |
int maxperkeyword, |
|
61 |
int maxkeywords) |
|
62 |
{ |
|
63 |
LinkedList<String> ll = new LinkedList<>(); |
|
2 | 64 |
|
65 |
try { |
|
66 |
BufferedReader in = |
|
67 |
new BufferedReader(new FileReader("/etc/resolv.conf")); |
|
68 |
String line; |
|
69 |
while ((line = in.readLine()) != null) { |
|
70 |
int maxvalues = maxperkeyword; |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
71 |
if (line.isEmpty()) |
2 | 72 |
continue; |
73 |
if (line.charAt(0) == '#' || line.charAt(0) == ';') |
|
74 |
continue; |
|
75 |
if (!line.startsWith(keyword)) |
|
76 |
continue; |
|
77 |
String value = line.substring(keyword.length()); |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
78 |
if (value.isEmpty()) |
2 | 79 |
continue; |
80 |
if (value.charAt(0) != ' ' && value.charAt(0) != '\t') |
|
81 |
continue; |
|
82 |
StringTokenizer st = new StringTokenizer(value, " \t"); |
|
83 |
while (st.hasMoreTokens()) { |
|
84 |
String val = st.nextToken(); |
|
85 |
if (val.charAt(0) == '#' || val.charAt(0) == ';') { |
|
86 |
break; |
|
87 |
} |
|
29978
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
88 |
if ("nameserver".equals(keyword)) { |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
89 |
if (val.indexOf(':') >= 0 && |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
90 |
val.indexOf('.') < 0 && // skip for IPv4 literals with port |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
91 |
val.indexOf('[') < 0 && |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
92 |
val.indexOf(']') < 0 ) { |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
93 |
// IPv6 literal, in non-BSD-style. |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
94 |
val = "[" + val + "]"; |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
95 |
} |
2a735486f812
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
25859
diff
changeset
|
96 |
} |
2 | 97 |
ll.add(val); |
98 |
if (--maxvalues == 0) { |
|
99 |
break; |
|
100 |
} |
|
101 |
} |
|
102 |
if (--maxkeywords == 0) { |
|
103 |
break; |
|
104 |
} |
|
105 |
} |
|
106 |
in.close(); |
|
107 |
} catch (IOException ioe) { |
|
108 |
// problem reading value |
|
109 |
} |
|
110 |
||
111 |
return ll; |
|
112 |
} |
|
113 |
||
6112 | 114 |
private LinkedList<String> searchlist; |
115 |
private LinkedList<String> nameservers; |
|
2 | 116 |
|
117 |
||
118 |
// Load DNS configuration from OS |
|
119 |
||
120 |
private void loadConfig() { |
|
121 |
assert Thread.holdsLock(lock); |
|
122 |
||
123 |
// check if cached settings have expired. |
|
124 |
if (lastRefresh >= 0) { |
|
125 |
long currTime = System.currentTimeMillis(); |
|
126 |
if ((currTime - lastRefresh) < TIMEOUT) { |
|
127 |
return; |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
// get the name servers from /etc/resolv.conf |
|
132 |
nameservers = |
|
6112 | 133 |
java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29978
diff
changeset
|
134 |
new java.security.PrivilegedAction<>() { |
6112 | 135 |
public LinkedList<String> run() { |
2 | 136 |
// typically MAXNS is 3 but we've picked 5 here |
137 |
// to allow for additional servers if required. |
|
138 |
return resolvconf("nameserver", 1, 5); |
|
139 |
} /* run */ |
|
140 |
}); |
|
141 |
||
142 |
// get the search list (or domain) |
|
143 |
searchlist = getSearchList(); |
|
144 |
||
145 |
// update the timestamp on the configuration |
|
146 |
lastRefresh = System.currentTimeMillis(); |
|
147 |
} |
|
148 |
||
149 |
||
150 |
// obtain search list or local domain |
|
151 |
||
6112 | 152 |
private LinkedList<String> getSearchList() { |
2 | 153 |
|
6112 | 154 |
LinkedList<String> sl; |
2 | 155 |
|
156 |
// first try the search keyword in /etc/resolv.conf |
|
157 |
||
6112 | 158 |
sl = java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29978
diff
changeset
|
159 |
new java.security.PrivilegedAction<>() { |
6112 | 160 |
public LinkedList<String> run() { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
161 |
LinkedList<String> ll; |
2 | 162 |
|
163 |
// first try search keyword (max 6 domains) |
|
164 |
ll = resolvconf("search", 6, 1); |
|
165 |
if (ll.size() > 0) { |
|
166 |
return ll; |
|
167 |
} |
|
168 |
||
169 |
return null; |
|
170 |
||
171 |
} /* run */ |
|
172 |
||
173 |
}); |
|
174 |
if (sl != null) { |
|
175 |
return sl; |
|
176 |
} |
|
177 |
||
178 |
// No search keyword so use local domain |
|
179 |
||
180 |
||
181 |
// LOCALDOMAIN has absolute priority on Solaris |
|
182 |
||
183 |
String localDomain = localDomain0(); |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
184 |
if (localDomain != null && !localDomain.isEmpty()) { |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29978
diff
changeset
|
185 |
sl = new LinkedList<>(); |
2 | 186 |
sl.add(localDomain); |
187 |
return sl; |
|
188 |
} |
|
189 |
||
190 |
// try domain keyword in /etc/resolv.conf |
|
191 |
||
6112 | 192 |
sl = java.security.AccessController.doPrivileged( |
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29978
diff
changeset
|
193 |
new java.security.PrivilegedAction<>() { |
6112 | 194 |
public LinkedList<String> run() { |
195 |
LinkedList<String> ll; |
|
2 | 196 |
|
197 |
ll = resolvconf("domain", 1, 1); |
|
198 |
if (ll.size() > 0) { |
|
199 |
return ll; |
|
200 |
} |
|
201 |
return null; |
|
202 |
||
203 |
} /* run */ |
|
204 |
}); |
|
205 |
if (sl != null) { |
|
206 |
return sl; |
|
207 |
} |
|
208 |
||
209 |
// no local domain so try fallback (RPC) domain or |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
210 |
// hostName |
2 | 211 |
|
6112 | 212 |
sl = new LinkedList<>(); |
2 | 213 |
String domain = fallbackDomain0(); |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
214 |
if (domain != null && !domain.isEmpty()) { |
2 | 215 |
sl.add(domain); |
216 |
} |
|
217 |
||
218 |
return sl; |
|
219 |
} |
|
220 |
||
221 |
||
222 |
// ---- |
|
223 |
||
224 |
ResolverConfigurationImpl() { |
|
225 |
opts = new OptionsImpl(); |
|
226 |
} |
|
227 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
228 |
@SuppressWarnings("unchecked") |
6112 | 229 |
public List<String> searchlist() { |
2 | 230 |
synchronized (lock) { |
231 |
loadConfig(); |
|
232 |
||
233 |
// List is mutable so return a shallow copy |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
234 |
return (List<String>)searchlist.clone(); |
2 | 235 |
} |
236 |
} |
|
237 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
238 |
@SuppressWarnings("unchecked") |
6112 | 239 |
public List<String> nameservers() { |
2 | 240 |
synchronized (lock) { |
241 |
loadConfig(); |
|
242 |
||
243 |
// List is mutable so return a shallow copy |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
244 |
|
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
245 |
return (List<String>)nameservers.clone(); |
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
246 |
|
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
6112
diff
changeset
|
247 |
} |
2 | 248 |
} |
249 |
||
250 |
public Options options() { |
|
251 |
return opts; |
|
252 |
} |
|
253 |
||
254 |
||
255 |
// --- Native methods -- |
|
256 |
||
257 |
static native String localDomain0(); |
|
258 |
||
259 |
static native String fallbackDomain0(); |
|
260 |
||
261 |
static { |
|
262 |
java.security.AccessController.doPrivileged( |
|
29986
97167d851fc4
8078467: Update core libraries to use diamond with anonymous classes
darcy
parents:
29978
diff
changeset
|
263 |
new java.security.PrivilegedAction<>() { |
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10596
diff
changeset
|
264 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10596
diff
changeset
|
265 |
System.loadLibrary("net"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10596
diff
changeset
|
266 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10596
diff
changeset
|
267 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10596
diff
changeset
|
268 |
}); |
2 | 269 |
} |
270 |
||
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Implementation of {@link ResolverConfiguration.Options} |
|
275 |
*/ |
|
276 |
class OptionsImpl extends ResolverConfiguration.Options { |
|
277 |
} |