Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/classes/sun/net/dns/ResolverConfigurationImpl.java
41137 views
1
/*
2
* Copyright (c) 2002, 2021, 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 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
//
59
private LinkedList<String> resolvconf(String keyword,
60
int maxperkeyword,
61
int maxkeywords)
62
{
63
LinkedList<String> ll = new LinkedList<>();
64
65
String resolvPath = System.getProperty("ext.net.resolvPath", "/etc/resolv.conf");
66
67
try {
68
BufferedReader in =
69
new BufferedReader(new FileReader(resolvPath));
70
String line;
71
while ((line = in.readLine()) != null) {
72
int maxvalues = maxperkeyword;
73
if (line.isEmpty())
74
continue;
75
if (line.charAt(0) == '#' || line.charAt(0) == ';')
76
continue;
77
if (!line.startsWith(keyword))
78
continue;
79
String value = line.substring(keyword.length());
80
if (value.isEmpty())
81
continue;
82
if (value.charAt(0) != ' ' && value.charAt(0) != '\t')
83
continue;
84
StringTokenizer st = new StringTokenizer(value, " \t");
85
while (st.hasMoreTokens()) {
86
String val = st.nextToken();
87
if (val.charAt(0) == '#' || val.charAt(0) == ';') {
88
break;
89
}
90
if ("nameserver".equals(keyword)) {
91
if (val.indexOf(':') >= 0 &&
92
val.indexOf('.') < 0 && // skip for IPv4 literals with port
93
val.indexOf('[') < 0 &&
94
val.indexOf(']') < 0 ) {
95
// IPv6 literal, in non-BSD-style.
96
val = "[" + val + "]";
97
}
98
}
99
ll.add(val);
100
if (--maxvalues == 0) {
101
break;
102
}
103
}
104
if (--maxkeywords == 0) {
105
break;
106
}
107
}
108
in.close();
109
} catch (IOException ioe) {
110
// problem reading value
111
}
112
113
return ll;
114
}
115
116
private LinkedList<String> searchlist;
117
private LinkedList<String> nameservers;
118
119
120
// Load DNS configuration from OS
121
122
@SuppressWarnings("removal")
123
private void loadConfig() {
124
assert Thread.holdsLock(lock);
125
126
// check if cached settings have expired.
127
if (lastRefresh >= 0) {
128
long currTime = System.currentTimeMillis();
129
if ((currTime - lastRefresh) < TIMEOUT) {
130
return;
131
}
132
}
133
134
// get the name servers from /etc/resolv.conf
135
nameservers =
136
java.security.AccessController.doPrivileged(
137
new java.security.PrivilegedAction<>() {
138
public LinkedList<String> run() {
139
// typically MAXNS is 3 but we've picked 5 here
140
// to allow for additional servers if required.
141
return resolvconf("nameserver", 1, 5);
142
} /* run */
143
});
144
145
// get the search list (or domain)
146
searchlist = getSearchList();
147
148
// update the timestamp on the configuration
149
lastRefresh = System.currentTimeMillis();
150
}
151
152
153
// obtain search list or local domain
154
155
@SuppressWarnings("removal")
156
private LinkedList<String> getSearchList() {
157
158
LinkedList<String> sl;
159
160
// first try the search keyword in /etc/resolv.conf
161
162
sl = java.security.AccessController.doPrivileged(
163
new java.security.PrivilegedAction<>() {
164
public LinkedList<String> run() {
165
LinkedList<String> ll;
166
167
// first try search keyword (max 6 domains)
168
ll = resolvconf("search", 6, 1);
169
if (ll.size() > 0) {
170
return ll;
171
}
172
173
return null;
174
175
} /* run */
176
177
});
178
if (sl != null) {
179
return sl;
180
}
181
182
// No search keyword so use local domain
183
184
// try domain keyword in /etc/resolv.conf
185
186
sl = java.security.AccessController.doPrivileged(
187
new java.security.PrivilegedAction<>() {
188
public LinkedList<String> run() {
189
LinkedList<String> ll;
190
191
ll = resolvconf("domain", 1, 1);
192
if (ll.size() > 0) {
193
return ll;
194
}
195
return null;
196
197
} /* run */
198
});
199
if (sl != null) {
200
return sl;
201
}
202
203
// no local domain so try fallback (RPC) domain or
204
// hostName
205
206
sl = new LinkedList<>();
207
String domain = fallbackDomain0();
208
if (domain != null && !domain.isEmpty()) {
209
sl.add(domain);
210
}
211
212
return sl;
213
}
214
215
216
// ----
217
218
ResolverConfigurationImpl() {
219
opts = new OptionsImpl();
220
}
221
222
@SuppressWarnings("unchecked")
223
public List<String> searchlist() {
224
synchronized (lock) {
225
loadConfig();
226
227
// List is mutable so return a shallow copy
228
return (List<String>)searchlist.clone();
229
}
230
}
231
232
@SuppressWarnings("unchecked")
233
public List<String> nameservers() {
234
synchronized (lock) {
235
loadConfig();
236
237
// List is mutable so return a shallow copy
238
239
return (List<String>)nameservers.clone();
240
241
}
242
}
243
244
public Options options() {
245
return opts;
246
}
247
248
249
// --- Native methods --
250
251
static native String fallbackDomain0();
252
253
static {
254
jdk.internal.loader.BootLoader.loadLibrary("net");
255
}
256
257
}
258
259
/**
260
* Implementation of {@link ResolverConfiguration.Options}
261
*/
262
class OptionsImpl extends ResolverConfiguration.Options {
263
}
264
265