author | aefimov |
Thu, 14 Nov 2019 23:16:40 +0000 | |
branch | aefimov-dns-client-branch |
changeset 59101 | 258033faefc9 |
parent 58984 | 15e026239a6c |
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; |
|
27 |
||
28 |
import jdk.dns.client.ex.DnsResolverException; |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
29 |
import jdk.dns.client.internal.AddressResolutionQueue; |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
30 |
import jdk.dns.client.internal.DnsResolver; |
58870 | 31 |
import jdk.dns.client.internal.HostsFileResolver; |
32 |
||
33 |
import java.net.InetAddress; |
|
34 |
import java.net.ProtocolFamily; |
|
35 |
import java.net.UnknownHostException; |
|
36 |
import java.security.PrivilegedAction; |
|
37 |
import java.util.List; |
|
38 |
import java.util.stream.Collectors; |
|
39 |
||
40 |
public class NetworkNamesResolver { |
|
41 |
||
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
42 |
private final ProtocolFamily protocolFamily; |
58870 | 43 |
|
44 |
public static NetworkNamesResolver open() throws UnknownHostException { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
45 |
// null for any (IPv4+IPv6) addresses family |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
46 |
return new NetworkNamesResolver(null); |
58870 | 47 |
} |
48 |
||
49 |
public static NetworkNamesResolver open(ProtocolFamily protocolFamily) throws UnknownHostException { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
50 |
return new NetworkNamesResolver(protocolFamily); |
58870 | 51 |
} |
52 |
||
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
53 |
private NetworkNamesResolver(ProtocolFamily protocolFamily) throws UnknownHostException { |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
54 |
this.protocolFamily = protocolFamily; |
58870 | 55 |
} |
56 |
||
57 |
/** |
|
58 |
* Lookup the IP address of a host. |
|
59 |
* The family of required address needed can be specified with {@code addressFamily} parameter. |
|
60 |
* |
|
61 |
* @param hostname the specified host name |
|
62 |
* @return first IP address that matches requested {@code addressFamily} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
63 |
* @throws UnknownHostException if no IP address found for the specified host name and the specified address family |
58870 | 64 |
*/ |
65 |
public InetAddress lookupHostAddr(String hostname) throws UnknownHostException { |
|
66 |
// First try hosts file |
|
67 |
// TODO: Add nsswitch.conf to select proper order |
|
68 |
try { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
69 |
return hostsFileResolver.getHostAddress(hostname, protocolFamily); |
58870 | 70 |
} catch (UnknownHostException uhe) { |
71 |
if (DEBUG) { |
|
72 |
System.err.printf("Hosts file doesn't know '%s' host with '%s' address family%n", |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
73 |
hostname, protocolFamily == null ? "ANY" : protocolFamily.toString()); |
58870 | 74 |
} |
75 |
} |
|
76 |
||
77 |
// If no luck - try to ask name servers |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
78 |
try (DnsResolver dnsResolver = new DnsResolver()) { |
58984
15e026239a6c
aefimov-dns-client-branch: Add system property to control ANY requests
aefimov
parents:
58971
diff
changeset
|
79 |
var results = lookup(dnsResolver, hostname, protocolFamily, false); |
58870 | 80 |
return results.get(0); |
81 |
} |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* <p>Lookup a host mapping by name. Retrieve the IP addresses |
|
86 |
* associated with a host. |
|
87 |
* |
|
88 |
* @param hostname the specified hostname |
|
89 |
* @return array of IP addresses for the requested host |
|
90 |
* @throws UnknownHostException if no IP address for the {@code hostname} could be found |
|
91 |
*/ |
|
92 |
public List<InetAddress> lookupAllHostAddr(String hostname) throws UnknownHostException { |
|
93 |
// First try hosts file |
|
94 |
// TODO: Add nsswitch.conf ReloadTracker and parser to select proper order |
|
95 |
try { |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
96 |
return List.of(hostsFileResolver.getHostAddress(hostname, protocolFamily)); |
58870 | 97 |
} catch (UnknownHostException uhe) { |
98 |
if (DEBUG) { |
|
99 |
System.err.printf("Resolver API: Hosts file doesn't know '%s' host%n", hostname); |
|
100 |
} |
|
101 |
} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
102 |
|
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
103 |
try (var dnsResolver = new DnsResolver()) { |
58984
15e026239a6c
aefimov-dns-client-branch: Add system property to control ANY requests
aefimov
parents:
58971
diff
changeset
|
104 |
var results = lookup(dnsResolver, hostname, null, true); |
58870 | 105 |
if (results.isEmpty()) { |
106 |
throw new UnknownHostException(hostname + " unknown host name"); |
|
107 |
} |
|
108 |
return results; |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Lookup the host name corresponding to the IP address provided. |
|
114 |
* |
|
115 |
* @param address the specified IP address |
|
116 |
* @return {@code String} representing the host name |
|
117 |
* @throws UnknownHostException if no host found for the specified IP address |
|
118 |
*/ |
|
119 |
public String getHostByAddr(InetAddress address) throws UnknownHostException { |
|
120 |
var results = getAllHostsByAddr(address); |
|
121 |
return results.get(0); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Lookup all known host names which correspond to the IP address provided |
|
126 |
* |
|
127 |
* @param address the specified IP address |
|
128 |
* @return array of {@code String} representing the host names |
|
129 |
* @throws UnknownHostException if no host found for the specified IP address |
|
130 |
*/ |
|
131 |
public List<String> getAllHostsByAddr(InetAddress address) throws UnknownHostException { |
|
132 |
// First try hosts file |
|
133 |
// TODO: Add nsswitch.conf to select proper order |
|
59101
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58984
diff
changeset
|
134 |
if (DEBUG) { |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58984
diff
changeset
|
135 |
System.err.println("Resolver API: Reverse lookup call for address:"+address); |
258033faefc9
aefimov-dns-client-branch: Fix timeouts, add platform specific file locations
aefimov
parents:
58984
diff
changeset
|
136 |
} |
58870 | 137 |
try { |
138 |
return List.of(hostsFileResolver.getByAddress(address)); |
|
139 |
} catch (UnknownHostException uhe) { |
|
140 |
if (DEBUG) { |
|
141 |
System.err.printf("Resolver API: No host in hosts file with %s address%n", address); |
|
142 |
} |
|
143 |
} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
144 |
try (DnsResolver dnsResolver = new DnsResolver()) { |
58870 | 145 |
var literalIP = addressToLiteralIP(address); |
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
146 |
var results = dnsResolver.rlookup(literalIP, address); |
58870 | 147 |
if (results.isEmpty()) { |
148 |
throw new UnknownHostException(); |
|
149 |
} |
|
150 |
// remove trailing dot |
|
151 |
return results.stream() |
|
152 |
.map(host -> host.endsWith(".") ? host.substring(0, host.length() - 1) : host) |
|
153 |
.collect(Collectors.toList()); |
|
154 |
} catch (DnsResolverException dre) { |
|
155 |
UnknownHostException uhe = new UnknownHostException(); |
|
156 |
uhe.initCause(dre); |
|
157 |
throw uhe; |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
private static String addressToLiteralIP(InetAddress address) { |
|
162 |
byte[] bytes = address.getAddress(); |
|
163 |
StringBuilder addressBuff = new StringBuilder(); |
|
164 |
// IPv4 address |
|
165 |
if (bytes.length == 4) { |
|
166 |
for (int i = bytes.length - 1; i >= 0; i--) { |
|
167 |
addressBuff.append(bytes[i] & 0xff); |
|
168 |
addressBuff.append("."); |
|
169 |
} |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
170 |
// IPv6 address |
58870 | 171 |
} else if (bytes.length == 16) { |
172 |
for (int i = bytes.length - 1; i >= 0; i--) { |
|
173 |
addressBuff.append(Integer.toHexString((bytes[i] & 0x0f))); |
|
174 |
addressBuff.append("."); |
|
175 |
addressBuff.append(Integer.toHexString((bytes[i] & 0xf0) >> 4)); |
|
176 |
addressBuff.append("."); |
|
177 |
} |
|
178 |
} else { |
|
179 |
return null; |
|
180 |
} |
|
181 |
return addressBuff.toString(); |
|
182 |
} |
|
183 |
||
58984
15e026239a6c
aefimov-dns-client-branch: Add system property to control ANY requests
aefimov
parents:
58971
diff
changeset
|
184 |
private List<InetAddress> lookup(DnsResolver dnsResolver, String host, ProtocolFamily protocolFamily, boolean needAllAddresses) throws UnknownHostException { |
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
185 |
if (DEBUG) { |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
186 |
System.out.printf("Resolver API: internal lookup call - %s%n", host); |
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
187 |
} |
58984
15e026239a6c
aefimov-dns-client-branch: Add system property to control ANY requests
aefimov
parents:
58971
diff
changeset
|
188 |
return AddressResolutionQueue.resolve(dnsResolver, host, protocolFamily, needAllAddresses); |
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
189 |
} |
58870 | 190 |
|
58971
465a15dd6bed
aefimov-dns-client-branch: Cleanup and address ANY type failures
aefimov
parents:
58870
diff
changeset
|
191 |
private static HostsFileResolver hostsFileResolver = new HostsFileResolver(); |
58870 | 192 |
private static final boolean DEBUG = java.security.AccessController.doPrivileged( |
193 |
(PrivilegedAction<Boolean>) () -> Boolean.getBoolean("jdk.dns.client.debug")); |
|
194 |
} |