Class DnsClient
This class uses the configuration provided by DcvConfiguration
to initialize DNS servers, timeout and retries.
The DnsClient class is designed to be flexible and robust, allowing for easy integration with different DNS servers
and handling various DNS record types such as A, CNAME, TXT, and CAA. It also includes error handling mechanisms to
manage common DNS query issues, ensuring reliable DNS data retrieval.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final int
The number of retries for DNS queries.The list of DNS servers to query.private final int
The timeout for DNS queries in milliseconds. -
Constructor Summary
ConstructorsConstructorDescriptionDnsClient
(DcvContext dcvContext) Constructs a new DnsClient with the specified configuration. -
Method Summary
Modifier and TypeMethodDescriptionprotected org.xbill.DNS.Lookup
createLookup
(String domain, int type) Creates a new Lookup for the specified domain and DNS record type.protected org.xbill.DNS.ExtendedResolver
createResolver
(String server, Integer port) Creates a new SimpleResolver for the specified DNS server.private DnsData
getDnsData
(String server, String domain, DnsType type) Retrieves DNS data for a specific server, domain, and DNS record type.getDnsData
(List<String> domains, DnsType type) Retrieves DNS records of the specified type for the given domains.private int
mapToDnsIntType
(DnsType dnsRecordType) Maps the DnsType enum to the corresponding integer value used by the DNS library.private void
verifyInputData
(List<String> domains) Verifies the input data for the DNS query.
-
Field Details
-
dnsServers
The list of DNS servers to query.This list is initialized from the
DcvConfiguration
and contains the DNS servers that the client will query for DNS records. The order of the servers in the list can affect the query process, as the client will attempt to query each server in sequence until it receives a valid response or exhausts the list. -
dnsTimeout
private final int dnsTimeoutThe timeout for DNS queries in milliseconds.This timeout value, specified in milliseconds, determines how long the client will wait for a response from a DNS server before considering the query to have failed.
-
dnsRetries
private final int dnsRetriesThe number of retries for DNS queries.This value specifies the number of times the client will retry a DNS query if it does not receive a valid response within the specified timeout period.
-
-
Constructor Details
-
DnsClient
Constructs a new DnsClient with the specified configuration.This constructor initializes the DnsClient using the provided
DcvContext
, which contains the necessary configuration settings such as DNS servers, timeout, and retries.- Parameters:
dcvContext
- context where we can find the needed dependencies / configuration
-
-
Method Details
-
getDnsData
Retrieves DNS records of the specified type for the given domains.This method iterates over the configured DNS servers, requesting DNS records of the specified type for each domain in the list. When a valid non-empty response is received, the method returns the DNS data for that domain. Querying is done sequentially and will end as soon as a valid response is received, so it is recommended to carefully consider the order of DNS servers in your configuration. If no valid response is received, it aggregates the errors encountered during the query process and returns an empty result with the collected errors.
- Parameters:
domains
- The list of domains to query.type
- The type of DNS record to query.- Returns:
- The DNS data containing the first records found and any errors encountered.
-
verifyInputData
Verifies the input data for the DNS query.This method checks that the list of domains is not empty and that the DNS servers are configured. If the input data is invalid or missing, the method throws an
IllegalArgumentException
- Parameters:
domains
- The list of domains to query.
-
getDnsData
Retrieves DNS data for a specific server, domain, and DNS record type.This method performs a DNS query for the specified server, domain, and DNS record type. It creates a resolver for the DNS server and a lookup for the domain and record type. The method handles common exceptions such as
UnknownHostException
andTextParseException
, logging the errors and returning an empty result with the appropriate error codes. The provided server may include a port number separated by a colon. (e.g. 8.8.8.8:53). If no port is provided, the default DNS port (53) is used.- Parameters:
server
- The DNS server to query.domain
- The domain to query.type
- The type of DNS record to query.- Returns:
- The DNS data containing the records and any errors encountered.
-
createResolver
protected org.xbill.DNS.ExtendedResolver createResolver(String server, Integer port) throws UnknownHostException Creates a new SimpleResolver for the specified DNS server.This method creates and configures a
SimpleResolver
for the specified DNS server and port. If a port is provided, it creates a resolver using the specified port; otherwise, it uses the default DNS port (53). The resolver is then wrapped in anExtendedResolver
to provide additional functionality such as retries and timeout management.- Parameters:
server
- The DNS server to create a resolver for.port
- The port to use for the resolver. If not port is specified, the default port (53) is used.- Returns:
- The created ExtendedResolver.
- Throws:
UnknownHostException
- If the DNS server is unknown.
-
createLookup
protected org.xbill.DNS.Lookup createLookup(String domain, int type) throws org.xbill.DNS.TextParseException Creates a new Lookup for the specified domain and DNS record type.This method creates and configures a
Lookup
for the specified domain and DNS record type. The lookup is used to perform the actual DNS query, translating the domain name and record type into a DNS request. The method throwsTextParseException
if it is unable to parse the response.- Parameters:
domain
- The domain to create a lookup for.type
- The DNS record type to query.- Returns:
- The created Lookup.
- Throws:
org.xbill.DNS.TextParseException
- If the domain name is invalid.
-
mapToDnsIntType
Maps the DnsType enum to the corresponding integer value used by the DNS library.This method translates the
DnsType
enum values into the integer constants used by the DNS library. This mapping is necessary because the DNS library uses integer values to represent different DNS record types, while the client code uses the more readableDnsType
enum.- Parameters:
dnsRecordType
- The DnsType to map.- Returns:
- The corresponding integer value for the DNS record type.
-