author | aefimov |
Thu, 14 Nov 2019 23:16:40 +0000 | |
branch | aefimov-dns-client-branch |
changeset 59101 | 258033faefc9 |
parent 58971 | 465a15dd6bed |
permissions | -rw-r--r-- |
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.client.internal; |
|
27 |
||
28 |
import jdk.dns.client.internal.util.ReloadTracker; |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58971
diff
changeset
|
29 |
import jdk.dns.conf.DnsResolverConfiguration; |
58870 | 30 |
import sun.net.util.IPAddressUtil; |
31 |
||
32 |
import java.net.InetAddress; |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
33 |
import java.net.ProtocolFamily; |
58870 | 34 |
import java.net.UnknownHostException; |
35 |
import java.nio.charset.StandardCharsets; |
|
36 |
import java.nio.file.Files; |
|
37 |
import java.nio.file.Path; |
|
38 |
import java.nio.file.Paths; |
|
39 |
import java.security.AccessController; |
|
40 |
import java.security.PrivilegedAction; |
|
41 |
import java.security.PrivilegedActionException; |
|
42 |
import java.security.PrivilegedExceptionAction; |
|
43 |
import java.util.Arrays; |
|
44 |
import java.util.Collections; |
|
45 |
import java.util.HashMap; |
|
46 |
import java.util.List; |
|
47 |
import java.util.Map; |
|
48 |
import java.util.Objects; |
|
49 |
import java.util.concurrent.locks.ReadWriteLock; |
|
50 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
|
51 |
import java.util.function.Predicate; |
|
52 |
import java.util.stream.Stream; |
|
53 |
||
54 |
public class HostsFileResolver { |
|
55 |
private static final String HOSTS_FILE_LOCATION_PROPERTY_VALUE = |
|
56 |
AccessController.doPrivileged((PrivilegedAction<String>) |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58971
diff
changeset
|
57 |
() -> System.getProperty("jdk.dns.client.hosts.file", |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58971
diff
changeset
|
58 |
DnsResolverConfiguration.getDefaultHostsFileLocation()) |
58870 | 59 |
); |
60 |
private static final ReadWriteLock LOCK = new ReentrantReadWriteLock(); |
|
61 |
||
62 |
// 300 seconds, similar to DnsResolverConfiguration in millis since Epoch |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58971
diff
changeset
|
63 |
private static final long REFRESH_TIMEOUT_NANOS = 300_000_000_000L; |
58870 | 64 |
private static final ReloadTracker HOSTS_FILE_TRACKER; |
65 |
private static volatile Map<String, HostFileEntry> HOST_ADDRESSES = Collections.emptyMap(); |
|
66 |
||
67 |
void loadHostsAddresses() { |
|
68 |
LOCK.readLock().lock(); |
|
69 |
var rsf = HOSTS_FILE_TRACKER.getReloadStatus(); |
|
70 |
try { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
71 |
if (!rsf.isReloadNeeded()) { |
58870 | 72 |
return; |
73 |
} |
|
74 |
} finally { |
|
75 |
LOCK.readLock().unlock(); |
|
76 |
} |
|
77 |
||
78 |
LOCK.writeLock().lock(); |
|
79 |
try { |
|
80 |
var rs = HOSTS_FILE_TRACKER.getReloadStatus(); |
|
81 |
// Check if reload is still needed |
|
82 |
if (rs.isReloadNeeded()) { |
|
83 |
if (rs.isFileExists()) { |
|
84 |
HOST_ADDRESSES = parseHostsFile(); |
|
85 |
HOSTS_FILE_TRACKER.updateTimestamps(rs); |
|
86 |
} else { |
|
87 |
HOST_ADDRESSES = Collections.emptyMap(); |
|
88 |
} |
|
89 |
} |
|
90 |
} finally { |
|
91 |
LOCK.writeLock().unlock(); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
private static String removeComments(String hostsEntry) { |
|
96 |
String filteredEntry = hostsEntry; |
|
97 |
int hashIndex; |
|
98 |
||
99 |
if ((hashIndex = hostsEntry.indexOf("#")) != -1) { |
|
100 |
filteredEntry = hostsEntry.substring(0, hashIndex); |
|
101 |
} |
|
102 |
return filteredEntry; |
|
103 |
} |
|
104 |
||
105 |
private static class HostFileEntry { |
|
106 |
final List<String> names; // Might need to split into aliases and name |
|
107 |
final InetAddress address; |
|
108 |
final boolean isValid; |
|
109 |
final boolean isHostname; |
|
110 |
||
111 |
HostFileEntry(String[] data) { |
|
112 |
assert data.length > 1; |
|
113 |
List<String> ln = List.of(Arrays.copyOfRange(data, 1, data.length)); |
|
114 |
String addressString = data[0]; |
|
115 |
names = ln; |
|
116 |
address = parseAddress(ln.isEmpty() ? null : ln.get(0), addressString); |
|
117 |
isValid = address != null; |
|
118 |
isHostname = false; |
|
119 |
} |
|
120 |
||
121 |
@Override |
|
122 |
public String toString() { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
123 |
return names + "/" + address; |
58870 | 124 |
} |
125 |
||
126 |
private HostFileEntry(String name, InetAddress address, boolean isHostname) { |
|
127 |
this.names = List.of(name); |
|
128 |
this.address = address; |
|
129 |
this.isValid = address != null; |
|
130 |
this.isHostname = isHostname; |
|
131 |
} |
|
132 |
||
133 |
boolean isValid() { |
|
134 |
return isValid; |
|
135 |
} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
136 |
|
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
137 |
boolean isHostname() { |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
138 |
return isHostname; |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
139 |
} |
58870 | 140 |
|
141 |
String getHostName() { |
|
142 |
return names.get(0); |
|
143 |
} |
|
144 |
||
145 |
Stream<HostFileEntry> oneNameStream() { |
|
146 |
HostFileEntry hostName = new HostFileEntry(names.get(0), address, true); |
|
147 |
Stream<HostFileEntry> aliases = names.stream() |
|
148 |
.skip(1) |
|
149 |
.map(n -> new HostFileEntry(n, address, false)); |
|
150 |
return Stream.concat(Stream.of(hostName), aliases); |
|
151 |
} |
|
152 |
||
153 |
private InetAddress parseAddress(String hostName, String addressString) { |
|
154 |
// TODO: Revisit |
|
155 |
Objects.requireNonNull(hostName); |
|
156 |
||
157 |
// IPAddressUtil is from |
|
158 |
var pa = (PrivilegedAction<byte[]>) () -> { |
|
159 |
if (IPAddressUtil.isIPv4LiteralAddress(addressString)) { |
|
160 |
return IPAddressUtil.textToNumericFormatV4(addressString); |
|
161 |
} else if (IPAddressUtil.isIPv6LiteralAddress(addressString)) { |
|
162 |
return IPAddressUtil.textToNumericFormatV6(addressString); |
|
163 |
} |
|
164 |
return null; |
|
165 |
}; |
|
166 |
byte[] addr = System.getSecurityManager() == null ? |
|
167 |
pa.run() : AccessController.doPrivileged(pa); |
|
168 |
||
169 |
if (addr != null) { |
|
170 |
try { |
|
171 |
// if (hostName == null) hostName = addressString |
|
172 |
return InetAddress.getByAddress(hostName, addr); |
|
173 |
} catch (UnknownHostException e) { |
|
174 |
} |
|
175 |
} |
|
176 |
return null; |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
private Map<String, HostFileEntry> parseHostsFile() { |
|
181 |
Path hf = Paths.get(HOSTS_FILE_LOCATION_PROPERTY_VALUE); |
|
182 |
try { |
|
183 |
// TODO: Revisit |
|
184 |
var pea = (PrivilegedExceptionAction<Boolean>) () -> Files.isRegularFile(hf); |
|
185 |
boolean isRegularFile = System.getSecurityManager() == null ? pea.run() |
|
186 |
: AccessController.doPrivileged(pea); |
|
187 |
||
188 |
if (isRegularFile) { |
|
189 |
var result = new HashMap<String, HostFileEntry>(); |
|
190 |
var pea2 = (PrivilegedExceptionAction<List<String>>) () -> Files.readAllLines(hf, StandardCharsets.UTF_8); |
|
191 |
var lines = System.getSecurityManager() == null ? pea2.run() |
|
192 |
: AccessController.doPrivileged(pea2); |
|
193 |
||
194 |
lines.stream() |
|
195 |
.map(HostsFileResolver::removeComments) |
|
196 |
.filter(Predicate.not(String::isBlank)) |
|
197 |
.map(s -> s.split("\\s+")) |
|
198 |
.filter(a -> a.length > 1) |
|
199 |
.map(HostFileEntry::new) |
|
200 |
.filter(HostFileEntry::isValid) |
|
201 |
.flatMap(HostFileEntry::oneNameStream) |
|
202 |
.forEachOrdered( |
|
203 |
// If the same host name is listed multiple times then |
|
204 |
// use the first encountered line |
|
205 |
hfe -> result.putIfAbsent(hfe.names.get(0), hfe) |
|
206 |
); |
|
207 |
return Map.copyOf(result); |
|
208 |
} |
|
209 |
} catch (PrivilegedActionException pae) { |
|
210 |
throw new RuntimeException("Can't read hosts file", pae.getCause()); |
|
211 |
} catch (Exception e) { |
|
212 |
throw new RuntimeException("Can't read hosts file", e); |
|
213 |
} |
|
214 |
return Collections.emptyMap(); |
|
215 |
} |
|
216 |
||
217 |
public InetAddress getHostAddress(String hostName) throws UnknownHostException { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
218 |
return getHostAddress(hostName, null); |
58870 | 219 |
} |
220 |
||
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
221 |
public InetAddress getHostAddress(String hostName, ProtocolFamily family) throws UnknownHostException { |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
222 |
var af = AddressFamily.fromProtocolFamily(family); |
58870 | 223 |
loadHostsAddresses(); |
224 |
var map = HOST_ADDRESSES; |
|
225 |
var he = map.get(hostName); |
|
226 |
if (he == null) { |
|
227 |
throw new UnknownHostException(hostName); |
|
228 |
} |
|
229 |
var addr = he.address; |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
230 |
if (!af.sameFamily(addr)) { |
58870 | 231 |
throw new UnknownHostException(hostName); |
232 |
} |
|
233 |
return addr; |
|
234 |
} |
|
235 |
||
236 |
public String getByAddress(final InetAddress ha) throws UnknownHostException { |
|
237 |
loadHostsAddresses(); |
|
238 |
var map = HOST_ADDRESSES; |
|
239 |
var entry = map.values().stream() |
|
240 |
.filter(HostFileEntry::isHostname) |
|
241 |
.filter(e -> isAddressBytesTheSame(ha.getAddress(), e.address.getAddress())) |
|
242 |
.findFirst(); |
|
243 |
if (entry.isEmpty()) { |
|
244 |
throw new UnknownHostException(ha.toString()); |
|
245 |
} |
|
246 |
return entry.get().getHostName(); |
|
247 |
} |
|
248 |
||
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
249 |
private static boolean isAddressBytesTheSame(byte[] addr1, byte[] addr2) { |
58870 | 250 |
if (addr1 == null || addr2 == null) { |
251 |
return false; |
|
252 |
} |
|
253 |
if (addr1.length != addr2.length) { |
|
254 |
return false; |
|
255 |
} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
256 |
for (int i = 0; i < addr1.length; i++) { |
58870 | 257 |
if (addr1[i] != addr2[i]) |
258 |
return false; |
|
259 |
} |
|
260 |
return true; |
|
261 |
} |
|
262 |
||
263 |
static { |
|
264 |
// TODO: Revisit |
|
265 |
try { |
|
266 |
var pea = (PrivilegedExceptionAction<ReloadTracker>) () -> |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58971
diff
changeset
|
267 |
ReloadTracker.newInstance(Paths.get(HOSTS_FILE_LOCATION_PROPERTY_VALUE), REFRESH_TIMEOUT_NANOS); |
58870 | 268 |
HOSTS_FILE_TRACKER = System.getSecurityManager() == null ? pea.run() : |
269 |
AccessController.doPrivileged(pea); |
|
270 |
} catch (PrivilegedActionException pae) { |
|
271 |
throw new RuntimeException("Error registering hosts file watch service", pae.getCause()); |
|
272 |
} catch (Exception e) { |
|
273 |
throw new RuntimeException("Error registering hosts file watch service", e); |
|
274 |
} |
|
275 |
} |
|
276 |
} |