Debian Codegraph @

minidns-hla/src/main/java/org/minidns/hla/ResolverApi.java

raw
1 /*
2 * Copyright 2015-2020 the original author or authors
3 *
4 * This software is licensed under the Apache License, Version 2.0,
5 * the GNU Lesser General Public License version 2 or later ("LGPL")
6 * and the WTFPL.
7 * You may choose either license to govern your use of this software only
8 * upon the condition that you accept all of the terms of either
9 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
10 */
11 package org.minidns.hla;
12
13 import java.io.IOException;
14 import java.net.Inet4Address;
15 import java.net.Inet6Address;
16 import java.net.InetAddress;
17
18 import org.minidns.AbstractDnsClient;
19 import org.minidns.dnslabel.DnsLabel;
20 import org.minidns.dnsmessage.Question;
21 import org.minidns.dnsname.DnsName;
22 import org.minidns.dnsqueryresult.DnsQueryResult;
23 import org.minidns.hla.srv.SrvProto;
24 import org.minidns.hla.srv.SrvService;
25 import org.minidns.hla.srv.SrvServiceProto;
26 import org.minidns.hla.srv.SrvType;
27 import org.minidns.iterative.ReliableDnsClient;
28 import org.minidns.record.Data;
29 import org.minidns.record.PTR;
30 import org.minidns.record.SRV;
31 import org.minidns.record.Record.TYPE;
32 import org.minidns.util.InetAddressUtil;
33
34 /**
35 * The high-level MiniDNS resolving API. It is designed to be easy to use.
36 * <p>
37 * A simple exammple how to resolve the IPv4 address of a given domain:
38 * </p>
39 * <pre>
40 * {@code
41 * ResolverResult<A> result = DnssecResolverApi.INSTANCE.resolve("verteiltesysteme.net", A.class);
42 * if (!result.wasSuccessful()) {
43 * RESPONSE_CODE responseCode = result.getResponseCode();
44 * // Perform error handling.
45 * …
46 * return;
47 * }
48 * if (!result.isAuthenticData()) {
49 * // Response was not secured with DNSSEC.
50 * …
51 * return;
52 * }
53 * Set<A> answers = result.getAnswers();
54 * for (A a : answers) {
55 * InetAddress inetAddress = a.getInetAddress();
56 * // Do someting with the InetAddress, e.g. connect to.
57 * …
58 * }
59 * }
60 * </pre>
61 * <p>
62 * MiniDNS also supports SRV resource records as first class citizens:
63 * </p>
64 * <pre>
65 * {@code
66 * SrvResolverResult result = DnssecResolverApi.INSTANCE.resolveSrv(SrvType.xmpp_client, "example.org")
67 * if (!result.wasSuccessful()) {
68 * RESPONSE_CODE responseCode = result.getResponseCode();
69 * // Perform error handling.
70 * …
71 * return;
72 * }
73 * if (!result.isAuthenticData()) {
74 * // Response was not secured with DNSSEC.
75 * …
76 * return;
77 * }
78 * List<ResolvedSrvRecord> srvRecords = result.getSortedSrvResolvedAddresses();
79 * // Loop over the domain names pointed by the SRV RR. MiniDNS will return the list
80 * // correctly sorted by the priority and weight of the related SRV RR.
81 * for (ResolvedSrvRecord srvRecord : srvRecord) {
82 * // Loop over the Internet Address RRs resolved for the SRV RR. The order of
83 * // the list depends on the prefered IP version setting of MiniDNS.
84 * for (InternetAddressRR inetAddressRR : srvRecord.addresses) {
85 * InetAddress inetAddress = inetAddressRR.getInetAddress();
86 * int port = srvAddresses.port;
87 * // Try to connect to inetAddress at port.
88 * …
89 * }
90 * }
91 * }
92 * </pre>
93 *
94 * @author Florian Schmaus
95 *
96 */
97 public class ResolverApi {
98
99 public static final ResolverApi INSTANCE = new ResolverApi(new ReliableDnsClient());
100
101 private final AbstractDnsClient dnsClient;
102
103 public ResolverApi(AbstractDnsClient dnsClient) {
104 this.dnsClient = dnsClient;
105 }
106
107 public final <D extends Data> ResolverResult<D> resolve(String name, Class<D> type) throws IOException {
108 return resolve(DnsName.from(name), type);
109 }
110
111 public final <D extends Data> ResolverResult<D> resolve(DnsName name, Class<D> type) throws IOException {
112 TYPE t = TYPE.getType(type);
113 Question q = new Question(name, t);
114 return resolve(q);
115 }
116
117 public <D extends Data> ResolverResult<D> resolve(Question question) throws IOException {
118 DnsQueryResult dnsQueryResult = dnsClient.query(question);
119
120 return new ResolverResult<D>(question, dnsQueryResult, null);
121 }
122
123 public SrvResolverResult resolveSrv(SrvType type, String serviceName) throws IOException {
124 return resolveSrv(type.service, type.proto, DnsName.from(serviceName));
125 }
126
127 public SrvResolverResult resolveSrv(SrvType type, DnsName serviceName) throws IOException {
128 return resolveSrv(type.service, type.proto, serviceName);
129 }
130
131 public SrvResolverResult resolveSrv(SrvService service, SrvProto proto, String name) throws IOException {
132 return resolveSrv(service.dnsLabel, proto.dnsLabel, DnsName.from(name));
133 }
134
135 public SrvResolverResult resolveSrv(SrvService service, SrvProto proto, DnsName name) throws IOException {
136 return resolveSrv(service.dnsLabel, proto.dnsLabel, name);
137 }
138
139 public SrvResolverResult resolveSrv(DnsLabel service, DnsLabel proto, DnsName name) throws IOException {
140 SrvServiceProto srvServiceProto = new SrvServiceProto(service, proto);
141 return resolveSrv(name, srvServiceProto);
142 }
143
144 public SrvResolverResult resolveSrv(String name) throws IOException {
145 return resolveSrv(DnsName.from(name));
146 }
147
148 public ResolverResult<PTR> reverseLookup(CharSequence inetAddressCs) throws IOException {
149 InetAddress inetAddress = InetAddress.getByName(inetAddressCs.toString());
150 return reverseLookup(inetAddress);
151 }
152
153 public ResolverResult<PTR> reverseLookup(InetAddress inetAddress) throws IOException {
154 if (inetAddress instanceof Inet4Address) {
155 return reverseLookup((Inet4Address) inetAddress);
156 } else if (inetAddress instanceof Inet6Address) {
157 return reverseLookup((Inet6Address) inetAddress);
158 } else {
159 throw new IllegalArgumentException("The given InetAddress '" + inetAddress + "' is neither of type Inet4Address or Inet6Address");
160 }
161 }
162
163 public ResolverResult<PTR> reverseLookup(Inet4Address inet4Address) throws IOException {
164 DnsName reversedIpAddress = InetAddressUtil.reverseIpAddressOf(inet4Address);
165 DnsName dnsName = DnsName.from(reversedIpAddress, DnsName.IN_ADDR_ARPA);
166 return resolve(dnsName, PTR.class);
167 }
168
169 public ResolverResult<PTR> reverseLookup(Inet6Address inet6Address) throws IOException {
170 DnsName reversedIpAddress = InetAddressUtil.reverseIpAddressOf(inet6Address);
171 DnsName dnsName = DnsName.from(reversedIpAddress, DnsName.IP6_ARPA);
172 return resolve(dnsName, PTR.class);
173 }
174
175 /**
176 * Resolve the {@link SRV} resource record for the given name. After ensuring that the resolution was successful
177 * with {@link SrvResolverResult#wasSuccessful()} , and, if DNSSEC was used, that the results could be verified with
178 * {@link SrvResolverResult#isAuthenticData()}, simply use {@link SrvResolverResult#getSortedSrvResolvedAddresses()} to
179 * retrieve the resolved IP addresses.
180 * <p>
181 * The name of SRV records is "_[service]._[protocol].[serviceDomain]", for example "_xmpp-client._tcp.example.org".
182 * </p>
183 *
184 * @param srvDnsName the name to resolve.
185 * @return a <code>SrvResolverResult</code> instance which can be used to retrieve the IP addresses.
186 * @throws IOException if an IO exception occurs.
187 */
188 public SrvResolverResult resolveSrv(DnsName srvDnsName) throws IOException {
189 final int labelCount = srvDnsName.getLabelCount();
190 if (labelCount < 3) {
191 throw new IllegalArgumentException();
192 }
193
194 DnsLabel service = srvDnsName.getLabel(labelCount - 1);
195 DnsLabel proto = srvDnsName.getLabel(labelCount - 2);
196 DnsName name = srvDnsName.stripToLabels(labelCount - 2);
197
198 SrvServiceProto srvServiceProto = new SrvServiceProto(service, proto);
199
200 return resolveSrv(name, srvServiceProto);
201 }
202
203 /**
204 * Resolve the {@link SRV} resource record for the given service name, service and protcol. After ensuring that the
205 * resolution was successful with {@link SrvResolverResult#wasSuccessful()} , and, if DNSSEC was used, that the
206 * results could be verified with {@link SrvResolverResult#isAuthenticData()}, simply use
207 * {@link SrvResolverResult#getSortedSrvResolvedAddresses()} to retrieve the resolved IP addresses.
208 *
209 * @param name the DNS name of the service.
210 * @param srvServiceProto the service and protocol to lookup.
211 * @return a <code>SrvResolverResult</code> instance which can be used to retrieve the IP addresses.
212 * @throws IOException if an I/O error occurs.
213 */
214 public SrvResolverResult resolveSrv(DnsName name, SrvServiceProto srvServiceProto) throws IOException {
215 DnsName srvDnsName = DnsName.from(srvServiceProto.service, srvServiceProto.proto, name);
216 ResolverResult<SRV> result = resolve(srvDnsName, SRV.class);
217
218 return new SrvResolverResult(result, srvServiceProto, this);
219 }
220
221 public final AbstractDnsClient getClient() {
222 return dnsClient;
223 }
224 }