Package org.minidns
Class AbstractDnsClient
- java.lang.Object
-
- org.minidns.AbstractDnsClient
-
- Direct Known Subclasses:
DnsClient,IterativeDnsClient,ReliableDnsClient
public abstract class AbstractDnsClient extends java.lang.Object
A minimal DNS client for SRV/A/AAAA/NS and CNAME lookups, with IDN support. This circumvents the missing javax.naming package on android.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractDnsClient.IpVersionSetting
-
Field Summary
Fields Modifier and Type Field Description protected DnsCachecacheThe internal DNS cache.protected DnsDataSourcedataSourceprotected static LruCacheDEFAULT_CACHEprotected static AbstractDnsClient.IpVersionSettingDEFAULT_IP_VERSION_SETTINGprotected java.util.RandominsecureRandomprotected AbstractDnsClient.IpVersionSettingipVersionSettingprotected static java.util.logging.LoggerLOGGERprotected java.util.RandomrandomThe internal random class for sequence generation.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractDnsClient()Create a new DNS client using the global default cache.protectedAbstractDnsClient(DnsCache cache)Create a new DNS client with the given DNS cache.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description DnsCachegetCache()Get the cache used by this DNS client.java.util.Set<A>getCachedIPv4AddressesFor(DnsName dnsName)java.util.Set<A>getCachedIPv4NameserverAddressesFor(DnsName dnsName)java.util.Set<AAAA>getCachedIPv6AddressesFor(DnsName dnsName)java.util.Set<AAAA>getCachedIPv6NameserverAddressesFor(DnsName dnsName)java.util.Set<NS>getCachedNameserverRecordsFor(DnsName dnsName)DnsDataSourcegetDataSource()Returns the currently usedDnsDataSource.AbstractDnsClient.IpVersionSettinggetPreferedIpVersion()protected DnsMessagegetQueryFor(Question q)protected booleanisResponseCacheable(Question q, DnsQueryResult result)Whether a response from the DNS system should be cached or not.protected abstract DnsMessage.BuildernewQuestion(DnsMessage.Builder questionMessage)DnsQueryResultquery(java.lang.CharSequence name, Record.TYPE type)Query the system nameservers for a single entry of the class IN (which is used for MX, SRV, A, AAAA and most other RRs).DnsQueryResultquery(java.lang.String name, Record.TYPE type, java.net.InetAddress address)Query a nameserver for a single entry of class IN.DnsQueryResultquery(java.lang.String name, Record.TYPE type, Record.CLASS clazz)Query the system nameservers for a single entry of any class.DnsQueryResultquery(java.lang.String name, Record.TYPE type, Record.CLASS clazz, java.net.InetAddress address)Query a nameserver for a single entry.DnsQueryResultquery(java.lang.String name, Record.TYPE type, Record.CLASS clazz, java.net.InetAddress address, int port)Query a nameserver for a single entry.protected abstract DnsQueryResultquery(DnsMessage.Builder query)Send a query request to the DNS system.DnsQueryResultquery(DnsMessage query, java.net.InetAddress host)DnsQueryResultquery(DnsMessage requestMessage, java.net.InetAddress address, int port)DnsQueryResultquery(Question q)DnsQueryResultquery(Question q, java.net.InetAddress address)Query a specific server for one entry.DnsQueryResultquery(Question q, java.net.InetAddress server, int port)DnsQueryResultquery(DnsName name, Record.TYPE type)Query the system nameservers for a single entry of the class IN (which is used for MX, SRV, A, AAAA and most other RRs).MiniDnsFuture<DnsQueryResult,java.io.IOException>queryAsync(java.lang.CharSequence name, Record.TYPE type)protected MiniDnsFuture<DnsQueryResult,java.io.IOException>queryAsync(DnsMessage.Builder query)Default implementation of an asynchronous DNS query which just wraps the synchronous case.MiniDnsFuture<DnsQueryResult,java.io.IOException>queryAsync(DnsMessage query, java.net.InetAddress dnsServer)MiniDnsFuture<DnsQueryResult,java.io.IOException>queryAsync(DnsMessage requestMessage, java.net.InetAddress address, int port)MiniDnsFuture<DnsQueryResult,java.io.IOException>queryAsync(Question q)voidsetDataSource(DnsDataSource dataSource)Set aDnsDataSourceto be used by the DnsClient.static voidsetDefaultIpVersion(AbstractDnsClient.IpVersionSetting preferedIpVersion)voidsetPreferedIpVersion(AbstractDnsClient.IpVersionSetting preferedIpVersion)
-
-
-
Field Detail
-
DEFAULT_CACHE
protected static final LruCache DEFAULT_CACHE
-
LOGGER
protected static final java.util.logging.Logger LOGGER
-
random
protected final java.util.Random random
The internal random class for sequence generation.
-
insecureRandom
protected final java.util.Random insecureRandom
-
dataSource
protected DnsDataSource dataSource
-
DEFAULT_IP_VERSION_SETTING
protected static AbstractDnsClient.IpVersionSetting DEFAULT_IP_VERSION_SETTING
-
ipVersionSetting
protected AbstractDnsClient.IpVersionSetting ipVersionSetting
-
-
Constructor Detail
-
AbstractDnsClient
protected AbstractDnsClient(DnsCache cache)
Create a new DNS client with the given DNS cache.- Parameters:
cache- The backend DNS cache.
-
AbstractDnsClient
protected AbstractDnsClient()
Create a new DNS client using the global default cache.
-
-
Method Detail
-
setDefaultIpVersion
public static void setDefaultIpVersion(AbstractDnsClient.IpVersionSetting preferedIpVersion)
-
setPreferedIpVersion
public void setPreferedIpVersion(AbstractDnsClient.IpVersionSetting preferedIpVersion)
-
getPreferedIpVersion
public AbstractDnsClient.IpVersionSetting getPreferedIpVersion()
-
query
public final DnsQueryResult query(java.lang.String name, Record.TYPE type, Record.CLASS clazz) throws java.io.IOException
Query the system nameservers for a single entry of any class. This can be used to determine the name server version, if name is version.bind, type is TYPE.TXT and clazz is CLASS.CH.- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).clazz- The class of the request (usually IN for Internet).- Returns:
- The response (or null on timeout/error).
- Throws:
java.io.IOException- if an IO error occurs.
-
query
public final DnsQueryResult query(DnsName name, Record.TYPE type) throws java.io.IOException
Query the system nameservers for a single entry of the class IN (which is used for MX, SRV, A, AAAA and most other RRs).- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).- Returns:
- The response (or null on timeout/error).
- Throws:
java.io.IOException- if an IO error occurs.
-
query
public final DnsQueryResult query(java.lang.CharSequence name, Record.TYPE type) throws java.io.IOException
Query the system nameservers for a single entry of the class IN (which is used for MX, SRV, A, AAAA and most other RRs).- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).- Returns:
- The response (or null on timeout/error).
- Throws:
java.io.IOException- if an IO error occurs.
-
query
public DnsQueryResult query(Question q) throws java.io.IOException
- Throws:
java.io.IOException
-
query
protected abstract DnsQueryResult query(DnsMessage.Builder query) throws java.io.IOException
Send a query request to the DNS system.- Parameters:
query- The query to send to the server.- Returns:
- The response (or null).
- Throws:
java.io.IOException- if an IO error occurs.
-
queryAsync
public final MiniDnsFuture<DnsQueryResult,java.io.IOException> queryAsync(java.lang.CharSequence name, Record.TYPE type)
-
queryAsync
public final MiniDnsFuture<DnsQueryResult,java.io.IOException> queryAsync(Question q)
-
queryAsync
protected MiniDnsFuture<DnsQueryResult,java.io.IOException> queryAsync(DnsMessage.Builder query)
Default implementation of an asynchronous DNS query which just wraps the synchronous case.Subclasses override this method to support true asynchronous queries.
- Parameters:
query- the query.- Returns:
- a future for this query.
-
query
public final DnsQueryResult query(Question q, java.net.InetAddress server, int port) throws java.io.IOException
- Throws:
java.io.IOException
-
query
public final DnsQueryResult query(DnsMessage requestMessage, java.net.InetAddress address, int port) throws java.io.IOException
- Throws:
java.io.IOException
-
queryAsync
public final MiniDnsFuture<DnsQueryResult,java.io.IOException> queryAsync(DnsMessage requestMessage, java.net.InetAddress address, int port)
-
isResponseCacheable
protected boolean isResponseCacheable(Question q, DnsQueryResult result)
Whether a response from the DNS system should be cached or not.- Parameters:
q- The question the response message should answer.result- The DNS query result.- Returns:
- True, if the response should be cached, false otherwise.
-
newQuestion
protected abstract DnsMessage.Builder newQuestion(DnsMessage.Builder questionMessage)
-
query
public DnsQueryResult query(java.lang.String name, Record.TYPE type, Record.CLASS clazz, java.net.InetAddress address, int port) throws java.io.IOException
Query a nameserver for a single entry.- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).clazz- The class of the request (usually IN for Internet).address- The DNS server address.port- The DNS server port.- Returns:
- The response (or null on timeout / failure).
- Throws:
java.io.IOException- On IO Errors.
-
query
public DnsQueryResult query(java.lang.String name, Record.TYPE type, Record.CLASS clazz, java.net.InetAddress address) throws java.io.IOException
Query a nameserver for a single entry.- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).clazz- The class of the request (usually IN for Internet).address- The DNS server host.- Returns:
- The response (or null on timeout / failure).
- Throws:
java.io.IOException- On IO Errors.
-
query
public DnsQueryResult query(java.lang.String name, Record.TYPE type, java.net.InetAddress address) throws java.io.IOException
Query a nameserver for a single entry of class IN.- Parameters:
name- The DNS name to request.type- The DNS type to request (SRV, A, AAAA, ...).address- The DNS server host.- Returns:
- The response (or null on timeout / failure).
- Throws:
java.io.IOException- On IO Errors.
-
query
public final DnsQueryResult query(DnsMessage query, java.net.InetAddress host) throws java.io.IOException
- Throws:
java.io.IOException
-
query
public DnsQueryResult query(Question q, java.net.InetAddress address) throws java.io.IOException
Query a specific server for one entry.- Parameters:
q- The question section of the DNS query.address- The dns server address.- Returns:
- The a DNS query result.
- Throws:
java.io.IOException- On IOErrors.
-
queryAsync
public final MiniDnsFuture<DnsQueryResult,java.io.IOException> queryAsync(DnsMessage query, java.net.InetAddress dnsServer)
-
getDataSource
public DnsDataSource getDataSource()
Returns the currently usedDnsDataSource. SeesetDataSource(DnsDataSource)for details.- Returns:
- The currently used
DnsDataSource
-
setDataSource
public void setDataSource(DnsDataSource dataSource)
Set aDnsDataSourceto be used by the DnsClient. The default implementation will direct all queries directly to the Internet. This can be used to define a non-default handling for outgoing data. This can be useful to redirect the requests to a proxy or to modify requests after or responses before they are handled by the DnsClient implementation.- Parameters:
dataSource- An implementation of DNSDataSource that shall be used.
-
getCache
public DnsCache getCache()
Get the cache used by this DNS client.- Returns:
- the cached used by this DNS client or
null.
-
getQueryFor
protected DnsMessage getQueryFor(Question q)
-
getCachedNameserverRecordsFor
public java.util.Set<NS> getCachedNameserverRecordsFor(DnsName dnsName)
-
getCachedIPv4AddressesFor
public java.util.Set<A> getCachedIPv4AddressesFor(DnsName dnsName)
-
getCachedIPv6AddressesFor
public java.util.Set<AAAA> getCachedIPv6AddressesFor(DnsName dnsName)
-
getCachedIPv4NameserverAddressesFor
public java.util.Set<A> getCachedIPv4NameserverAddressesFor(DnsName dnsName)
-
getCachedIPv6NameserverAddressesFor
public java.util.Set<AAAA> getCachedIPv6NameserverAddressesFor(DnsName dnsName)
-
-