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;
|
|
27 |
|
|
28 |
import jdk.dns.client.ex.DnsResolverException;
|
|
29 |
import jdk.dns.client.internal.DnsName;
|
|
30 |
import jdk.dns.client.internal.HostsFileResolver;
|
|
31 |
import jdk.dns.client.internal.Resolver;
|
|
32 |
import jdk.dns.client.internal.ResourceClassType;
|
|
33 |
import jdk.dns.client.internal.ResourceRecord;
|
|
34 |
import jdk.dns.client.internal.ResourceRecords;
|
|
35 |
import jdk.dns.conf.DnsResolverConfiguration;
|
|
36 |
import sun.net.util.IPAddressUtil;
|
|
37 |
|
|
38 |
import java.net.InetAddress;
|
|
39 |
import java.net.ProtocolFamily;
|
|
40 |
import java.net.UnknownHostException;
|
|
41 |
import java.security.PrivilegedAction;
|
|
42 |
import java.util.ArrayList;
|
|
43 |
import java.util.Collections;
|
|
44 |
import java.util.LinkedHashSet;
|
|
45 |
import java.util.List;
|
|
46 |
import java.util.Set;
|
|
47 |
import java.util.stream.Collectors;
|
|
48 |
|
|
49 |
public class NetworkNamesResolver {
|
|
50 |
|
|
51 |
private final AddressFamily addressFamily;
|
|
52 |
|
|
53 |
public static NetworkNamesResolver open() throws UnknownHostException {
|
|
54 |
return new NetworkNamesResolver(AddressFamily.ANY);
|
|
55 |
}
|
|
56 |
|
|
57 |
public static NetworkNamesResolver open(ProtocolFamily protocolFamily) throws UnknownHostException {
|
|
58 |
return new NetworkNamesResolver(AddressFamily.fromProtocolFamily(protocolFamily));
|
|
59 |
}
|
|
60 |
|
|
61 |
private NetworkNamesResolver(AddressFamily addressFamily) throws UnknownHostException {
|
|
62 |
this.addressFamily = addressFamily;
|
|
63 |
this.dnsResolver = new Resolver(dnsResolverConfiguration.nameservers(), 1000, 4);
|
|
64 |
}
|
|
65 |
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Lookup the IP address of a host.
|
|
69 |
* The family of required address needed can be specified with {@code addressFamily} parameter.
|
|
70 |
*
|
|
71 |
* @param hostname the specified host name
|
|
72 |
* @return first IP address that matches requested {@code addressFamily}
|
|
73 |
* @throws UnknownHostException if no IP address found for the specified host name and the specified address family
|
|
74 |
*/
|
|
75 |
public InetAddress lookupHostAddr(String hostname) throws UnknownHostException {
|
|
76 |
|
|
77 |
// First try hosts file
|
|
78 |
// TODO: Add nsswitch.conf to select proper order
|
|
79 |
try {
|
|
80 |
return hostsFileResolver.getHostAddress(hostname, addressFamily);
|
|
81 |
} catch (UnknownHostException uhe) {
|
|
82 |
if (DEBUG) {
|
|
83 |
System.err.printf("Hosts file doesn't know '%s' host with '%s' address family%n",
|
|
84 |
hostname, addressFamily.toString());
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
// If no luck - try to ask name servers
|
|
89 |
try {
|
|
90 |
var results = lookup(hostname, addressFamily, true, true, 0);
|
|
91 |
if (results.isEmpty()) {
|
|
92 |
throw new UnknownHostException(hostname);
|
|
93 |
}
|
|
94 |
return results.get(0);
|
|
95 |
} catch (DnsResolverException e) {
|
|
96 |
UnknownHostException uhe = new UnknownHostException(hostname);
|
|
97 |
uhe.initCause(e);
|
|
98 |
throw uhe;
|
|
99 |
}
|
|
100 |
}
|
|
101 |
|
|
102 |
/**
|
|
103 |
* <p>Lookup a host mapping by name. Retrieve the IP addresses
|
|
104 |
* associated with a host.
|
|
105 |
*
|
|
106 |
* @param hostname the specified hostname
|
|
107 |
* @return array of IP addresses for the requested host
|
|
108 |
* @throws UnknownHostException if no IP address for the {@code hostname} could be found
|
|
109 |
*/
|
|
110 |
public List<InetAddress> lookupAllHostAddr(String hostname) throws UnknownHostException {
|
|
111 |
// First try hosts file
|
|
112 |
// TODO: Add nsswitch.conf ReloadTracker and parser to select proper order
|
|
113 |
try {
|
|
114 |
return List.of(hostsFileResolver.getHostAddress(hostname));
|
|
115 |
} catch (UnknownHostException uhe) {
|
|
116 |
if (DEBUG) {
|
|
117 |
System.err.printf("Resolver API: Hosts file doesn't know '%s' host%n", hostname);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
try {
|
|
121 |
var results = lookup(hostname, AddressFamily.ANY, false, true, 0);
|
|
122 |
if (results.isEmpty()) {
|
|
123 |
throw new UnknownHostException(hostname + " unknown host name");
|
|
124 |
}
|
|
125 |
return results;
|
|
126 |
} catch (DnsResolverException e) {
|
|
127 |
UnknownHostException uhe = new UnknownHostException(hostname);
|
|
128 |
uhe.initCause(e);
|
|
129 |
throw uhe;
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* Lookup the host name corresponding to the IP address provided.
|
|
135 |
*
|
|
136 |
* @param address the specified IP address
|
|
137 |
* @return {@code String} representing the host name
|
|
138 |
* @throws UnknownHostException if no host found for the specified IP address
|
|
139 |
*/
|
|
140 |
public String getHostByAddr(InetAddress address) throws UnknownHostException {
|
|
141 |
var results = getAllHostsByAddr(address);
|
|
142 |
return results.get(0);
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Lookup all known host names which correspond to the IP address provided
|
|
147 |
*
|
|
148 |
* @param address the specified IP address
|
|
149 |
* @return array of {@code String} representing the host names
|
|
150 |
* @throws UnknownHostException if no host found for the specified IP address
|
|
151 |
*/
|
|
152 |
public List<String> getAllHostsByAddr(InetAddress address) throws UnknownHostException {
|
|
153 |
// First try hosts file
|
|
154 |
// TODO: Add nsswitch.conf to select proper order
|
|
155 |
try {
|
|
156 |
return List.of(hostsFileResolver.getByAddress(address));
|
|
157 |
} catch (UnknownHostException uhe) {
|
|
158 |
if (DEBUG) {
|
|
159 |
System.err.printf("Resolver API: No host in hosts file with %s address%n", address);
|
|
160 |
}
|
|
161 |
}
|
|
162 |
try {
|
|
163 |
var literalIP = addressToLiteralIP(address);
|
|
164 |
var family = AddressFamily.fromInetAddress(address);
|
|
165 |
var results = rlookup(literalIP, family);
|
|
166 |
if (results.isEmpty()) {
|
|
167 |
throw new UnknownHostException();
|
|
168 |
}
|
|
169 |
// remove trailing dot
|
|
170 |
return results.stream()
|
|
171 |
.map(host -> host.endsWith(".") ? host.substring(0, host.length() - 1) : host)
|
|
172 |
.collect(Collectors.toList());
|
|
173 |
} catch (DnsResolverException dre) {
|
|
174 |
UnknownHostException uhe = new UnknownHostException();
|
|
175 |
uhe.initCause(dre);
|
|
176 |
throw uhe;
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
private Set<String> prepareDomainsSearchList() {
|
|
181 |
var domainsToSearch = new LinkedHashSet<String>();
|
|
182 |
var domain = dnsResolverConfiguration.domain();
|
|
183 |
// First will try the domain
|
|
184 |
if (!domain.isBlank()) {
|
|
185 |
domainsToSearch.add(domain);
|
|
186 |
}
|
|
187 |
// Then will iterate over search list
|
|
188 |
domainsToSearch.addAll(dnsResolverConfiguration.searchlist());
|
|
189 |
if (DEBUG) {
|
|
190 |
System.out.printf("Domains search list:%s%n", domainsToSearch);
|
|
191 |
}
|
|
192 |
return domainsToSearch;
|
|
193 |
}
|
|
194 |
|
|
195 |
private List<InetAddress> lookup(String host, AddressFamily addressFamily, boolean oneIsEnough, boolean checkDomains, int depth) throws UnknownHostException, DnsResolverException {
|
|
196 |
|
|
197 |
if (DEBUG) {
|
|
198 |
System.out.printf("Resolver API: internal lookup call - %s%n", host);
|
|
199 |
}
|
|
200 |
|
|
201 |
// Will remain the same during DNS queries execution
|
|
202 |
ResourceClassType ct = ResourceClassType.fromAddressFamily(addressFamily);
|
|
203 |
ResourceRecords rrs = null;
|
|
204 |
|
|
205 |
// First try to get the resource records with the requested DNS host name if it is FQDN
|
|
206 |
if (host.contains(".")) {
|
|
207 |
rrs = execAddrResolutionQuery(new DnsName(host), ct);
|
|
208 |
}
|
|
209 |
// Non-FQDN or not known host address and domains still can be checked
|
|
210 |
if ((rrs == null || rrs.answer.isEmpty()) && checkDomains) {
|
|
211 |
for (String domain : prepareDomainsSearchList()) {
|
|
212 |
String hostWithSuffix = host + "." + domain;
|
|
213 |
if (DEBUG) {
|
|
214 |
System.err.printf("Resolver API: Trying to lookup:'%s'%n", hostWithSuffix);
|
|
215 |
}
|
|
216 |
rrs = execAddrResolutionQuery(new DnsName(hostWithSuffix), ct);
|
|
217 |
if (rrs != null && !rrs.answer.isEmpty()) {
|
|
218 |
if (DEBUG) {
|
|
219 |
System.err.printf("Resolver API: Found host in '%s' domain%n", domain);
|
|
220 |
}
|
|
221 |
break;
|
|
222 |
}
|
|
223 |
}
|
|
224 |
}
|
|
225 |
|
|
226 |
// If no answers then host is not known in registered domain
|
|
227 |
if (rrs == null || rrs.answer.isEmpty()) {
|
|
228 |
throw new UnknownHostException(host);
|
|
229 |
}
|
|
230 |
|
|
231 |
// Parse answers
|
|
232 |
List<InetAddress> results = new ArrayList<>();
|
|
233 |
for (int i = 0; i < rrs.answer.size(); i++) {
|
|
234 |
ResourceRecord rr = rrs.answer.elementAt(i);
|
|
235 |
int answerType = rr.getType();
|
|
236 |
String recordData = rr.getRdata().toString();
|
|
237 |
if (DEBUG) {
|
|
238 |
System.err.printf("Resolver API: Got %s type: %s%n", ResourceRecord.getTypeName(rr.getType()), rr.getRdata());
|
|
239 |
}
|
|
240 |
if (answerType == ResourceRecord.TYPE_CNAME) {
|
|
241 |
// We've got CNAME entry - issue another request to resolve the canonical host name
|
|
242 |
if (depth > MAX_CNAME_RESOLUTION_DEPTH) {
|
|
243 |
throw new UnknownHostException(host);
|
|
244 |
}
|
|
245 |
List<InetAddress> cnameResults = lookup(recordData, addressFamily, oneIsEnough, false, depth + 1);
|
|
246 |
if (oneIsEnough) {
|
|
247 |
return cnameResults;
|
|
248 |
} else {
|
|
249 |
results.addAll(cnameResults);
|
|
250 |
}
|
|
251 |
} else {
|
|
252 |
byte[] addrBytes;
|
|
253 |
if (answerType == ResourceRecord.TYPE_A && (addressFamily == AddressFamily.IPv4 || addressFamily == AddressFamily.ANY)) {
|
|
254 |
addrBytes = IPAddressUtil.textToNumericFormatV4(recordData);
|
|
255 |
if (addrBytes == null) {
|
|
256 |
if (DEBUG) {
|
|
257 |
System.err.println("Incorrect A resource record answer");
|
|
258 |
}
|
|
259 |
return Collections.emptyList();
|
|
260 |
}
|
|
261 |
} else if (answerType == ResourceRecord.TYPE_AAAA && (addressFamily == AddressFamily.IPv6 || addressFamily == AddressFamily.ANY)) {
|
|
262 |
addrBytes = IPAddressUtil.textToNumericFormatV6(recordData);
|
|
263 |
if (addrBytes == null) {
|
|
264 |
if (DEBUG) {
|
|
265 |
System.err.println("Incorrect AAAA resource record answer");
|
|
266 |
}
|
|
267 |
return Collections.emptyList();
|
|
268 |
}
|
|
269 |
} else {
|
|
270 |
continue;
|
|
271 |
}
|
|
272 |
// IPAddressUtil.textToNumeric can return null
|
|
273 |
if (addrBytes == null) {
|
|
274 |
throw new RuntimeException("Internal error");
|
|
275 |
}
|
|
276 |
InetAddress address = InetAddress.getByAddress(host, addrBytes);
|
|
277 |
results.add(address);
|
|
278 |
if (oneIsEnough) {
|
|
279 |
break;
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
return results;
|
|
285 |
}
|
|
286 |
|
|
287 |
// Can return null if query failed
|
|
288 |
private ResourceRecords execAddrResolutionQuery(DnsName name, ResourceClassType ct) {
|
|
289 |
// Not supporting authoritative requests [for now?]
|
|
290 |
try {
|
|
291 |
ResourceRecords rrs = dnsResolver.query(name, ct.getRecordClass(), ct.getRecordType(),
|
|
292 |
true, false);
|
|
293 |
return rrs.hasAddressOrAlias() ? rrs : null;
|
|
294 |
} catch (DnsResolverException e) {
|
|
295 |
if (DEBUG) {
|
|
296 |
System.err.println("Query failed:" + e.getMessage());
|
|
297 |
e.printStackTrace();
|
|
298 |
}
|
|
299 |
return null;
|
|
300 |
}
|
|
301 |
}
|
|
302 |
|
|
303 |
private List<String> rlookup(String literalIP, AddressFamily family) throws DnsResolverException {
|
|
304 |
switch (family) {
|
|
305 |
case IPv4:
|
|
306 |
return ipv4rlookup(literalIP);
|
|
307 |
case IPv6:
|
|
308 |
return ipv6rlookup(literalIP);
|
|
309 |
default:
|
|
310 |
throw new RuntimeException("Only IPv4 and IPv6 addresses are supported");
|
|
311 |
}
|
|
312 |
}
|
|
313 |
|
|
314 |
private List<String> ipv4rlookup(String literalIP) throws DnsResolverException {
|
|
315 |
var request = literalIP + "IN-ADDR.ARPA.";
|
|
316 |
var hostNames = new ArrayList<String>();
|
|
317 |
var name = new DnsName(request);
|
|
318 |
var rrs = dnsResolver.query(name, ResourceRecord.CLASS_INTERNET,
|
|
319 |
ResourceRecord.TYPE_PTR, true, false);
|
|
320 |
for (int i = 0; i < rrs.answer.size(); i++) {
|
|
321 |
ResourceRecord rr = rrs.answer.elementAt(i);
|
|
322 |
hostNames.add(rr.getRdata().toString());
|
|
323 |
}
|
|
324 |
return hostNames;
|
|
325 |
}
|
|
326 |
|
|
327 |
private List<String> ipv6rlookup(String literalIP) throws DnsResolverException {
|
|
328 |
List<String> hostNames = new ArrayList<>();
|
|
329 |
DnsName name = new DnsName(literalIP + "IP6.ARPA.");
|
|
330 |
ResourceRecords rrs = dnsResolver.query(name, ResourceRecord.CLASS_INTERNET,
|
|
331 |
ResourceRecord.TYPE_PTR, true, false);
|
|
332 |
/**
|
|
333 |
* Because RFC 3152 changed the root domain name for reverse
|
|
334 |
* lookups from IP6.INT. to IP6.ARPA., we need to check
|
|
335 |
* both. I.E. first the new one, IP6.ARPA, then if it fails
|
|
336 |
* the older one, IP6.INT
|
|
337 |
*/
|
|
338 |
if (rrs.answer.isEmpty()) {
|
|
339 |
name = new DnsName(literalIP + "IP6.INT.");
|
|
340 |
rrs = dnsResolver.query(name, ResourceRecord.CLASS_INTERNET,
|
|
341 |
ResourceRecord.TYPE_PTR, true, false);
|
|
342 |
}
|
|
343 |
for (int i = 0; i < rrs.answer.size(); i++) {
|
|
344 |
ResourceRecord rr = rrs.answer.elementAt(i);
|
|
345 |
hostNames.add(rr.getRdata().toString());
|
|
346 |
}
|
|
347 |
return hostNames;
|
|
348 |
}
|
|
349 |
|
|
350 |
private static String addressToLiteralIP(InetAddress address) {
|
|
351 |
byte[] bytes = address.getAddress();
|
|
352 |
StringBuilder addressBuff = new StringBuilder();
|
|
353 |
// IPv4 address
|
|
354 |
if (bytes.length == 4) {
|
|
355 |
for (int i = bytes.length - 1; i >= 0; i--) {
|
|
356 |
addressBuff.append(bytes[i] & 0xff);
|
|
357 |
addressBuff.append(".");
|
|
358 |
}
|
|
359 |
// IPv6 address
|
|
360 |
} else if (bytes.length == 16) {
|
|
361 |
for (int i = bytes.length - 1; i >= 0; i--) {
|
|
362 |
addressBuff.append(Integer.toHexString((bytes[i] & 0x0f)));
|
|
363 |
addressBuff.append(".");
|
|
364 |
addressBuff.append(Integer.toHexString((bytes[i] & 0xf0) >> 4));
|
|
365 |
addressBuff.append(".");
|
|
366 |
}
|
|
367 |
} else {
|
|
368 |
return null;
|
|
369 |
}
|
|
370 |
return addressBuff.toString();
|
|
371 |
}
|
|
372 |
|
|
373 |
private static DnsResolverConfiguration dnsResolverConfiguration = new DnsResolverConfiguration();
|
|
374 |
private static HostsFileResolver hostsFileResolver = new HostsFileResolver();
|
|
375 |
|
|
376 |
// TODO: Add system property with the sequence of resolution OR nsswitch.conf location to get the order:
|
|
377 |
// file, name
|
|
378 |
private final Resolver dnsResolver;
|
|
379 |
private static final boolean DEBUG = java.security.AccessController.doPrivileged(
|
|
380 |
(PrivilegedAction<Boolean>) () -> Boolean.getBoolean("jdk.dns.client.debug"));
|
|
381 |
private static final int MAX_CNAME_RESOLUTION_DEPTH = 4;
|
|
382 |
}
|