@SuppressWarnings("unchecked") @Test public void testSingleRecordQuery() throws IOException { DnsClient client = new DnsClient(new LruCache(0)); applyStubRecords(client, record("www.example.com", a("127.0.0.1"))); DnsQueryResult result = client.query("www.example.com", TYPE.A); DnsMessage response = result.response; assertNotNull(response); assertEquals(1, response.answerSection.size()); assertEquals(TYPE.A, response.answerSection.get(0).type); assertArrayEquals(new byte[] {127, 0, 0, 1}, ((A) response.answerSection.get(0).payloadData).getIp()); result = client.query("www2.example.com", TYPE.A); assertEquals(RESPONSE_CODE.NX_DOMAIN, result.response.responseCode); result = client.query("www.example.com", TYPE.CNAME); assertEquals(RESPONSE_CODE.NX_DOMAIN, result.response.responseCode); }
@Override public DnsQueryResult query(DnsMessage.Builder queryBuilder) throws IOException { DnsMessage q = newQuestion(queryBuilder).build(); DnsQueryResult dnsQueryResult = (cache == null) ? null : cache.get(q); if (dnsQueryResult != null) { return dnsQueryResult; } List<InetAddress> dnsServerAddresses = getServerAddresses(); List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size()); for (InetAddress dns : dnsServerAddresses) { if (nonRaServers.contains(dns)) { LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\""); continue; } try { dnsQueryResult = query(q, dns); } catch (IOException ioe) { ioExceptions.add(ioe); continue; } DnsMessage responseMessage = dnsQueryResult.response; if (!responseMessage.recursionAvailable) { boolean newRaServer = nonRaServers.add(dns); if (newRaServer) { LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution"); } continue; } if (disableResultFilter) { return dnsQueryResult; } switch (responseMessage.responseCode) { case NO_ERROR: case NX_DOMAIN: break; default: String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.'; if (!LOGGER.isLoggable(Level.FINE)) { warning += "\n" + responseMessage; } LOGGER.warning(warning); ErrorResponseException exception = new ErrorResponseException(q, dnsQueryResult); ioExceptions.add(exception); continue; } return dnsQueryResult; } MultipleIoException.throwIfRequired(ioExceptions); throw new NoQueryPossibleException(q); }
DnsClient extends AbstractDnsClient { @Override public DnsQueryResult query(DnsMessage.Builder queryBuilder) throws IOException { DnsMessage q = newQuestion(queryBuilder).build(); DnsQueryResult dnsQueryResult = (cache == null) ? null : cache.get(q); if (dnsQueryResult != null) { return dnsQueryResult; } List<InetAddress> dnsServerAddresses = getServerAddresses(); List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size()); for (InetAddress dns : dnsServerAddresses) { if (nonRaServers.contains(dns)) { LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\""); continue; } try { dnsQueryResult = query(q, dns); } catch (IOException ioe) { ioExceptions.add(ioe); continue; } DnsMessage responseMessage = dnsQueryResult.response; if (!responseMessage.recursionAvailable) { boolean newRaServer = nonRaServers.add(dns); if (newRaServer) { LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution"); } continue; } if (disableResultFilter) { return dnsQueryResult; } switch (responseMessage.responseCode) { case NO_ERROR: case NX_DOMAIN: break; default: String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.'; if (!LOGGER.isLoggable(Level.FINE)) { warning += "\n" + responseMessage; } LOGGER.warning(warning); ErrorResponseException exception = new ErrorResponseException(q, dnsQueryResult); ioExceptions.add(exception); continue; } return dnsQueryResult; } MultipleIoException.throwIfRequired(ioExceptions); throw new NoQueryPossibleException(q); } }
DnsClient extends AbstractDnsClient { @Override public DnsQueryResult query(DnsMessage.Builder queryBuilder) throws IOException { DnsMessage q = newQuestion(queryBuilder).build(); DnsQueryResult dnsQueryResult = (cache == null) ? null : cache.get(q); if (dnsQueryResult != null) { return dnsQueryResult; } List<InetAddress> dnsServerAddresses = getServerAddresses(); List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size()); for (InetAddress dns : dnsServerAddresses) { if (nonRaServers.contains(dns)) { LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\""); continue; } try { dnsQueryResult = query(q, dns); } catch (IOException ioe) { ioExceptions.add(ioe); continue; } DnsMessage responseMessage = dnsQueryResult.response; if (!responseMessage.recursionAvailable) { boolean newRaServer = nonRaServers.add(dns); if (newRaServer) { LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution"); } continue; } if (disableResultFilter) { return dnsQueryResult; } switch (responseMessage.responseCode) { case NO_ERROR: case NX_DOMAIN: break; default: String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.'; if (!LOGGER.isLoggable(Level.FINE)) { warning += "\n" + responseMessage; } LOGGER.warning(warning); ErrorResponseException exception = new ErrorResponseException(q, dnsQueryResult); ioExceptions.add(exception); continue; } return dnsQueryResult; } MultipleIoException.throwIfRequired(ioExceptions); throw new NoQueryPossibleException(q); } DnsClient();  DnsClient(DnsCache dnsCache); }
DnsClient extends AbstractDnsClient { @Override public DnsQueryResult query(DnsMessage.Builder queryBuilder) throws IOException { DnsMessage q = newQuestion(queryBuilder).build(); DnsQueryResult dnsQueryResult = (cache == null) ? null : cache.get(q); if (dnsQueryResult != null) { return dnsQueryResult; } List<InetAddress> dnsServerAddresses = getServerAddresses(); List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size()); for (InetAddress dns : dnsServerAddresses) { if (nonRaServers.contains(dns)) { LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\""); continue; } try { dnsQueryResult = query(q, dns); } catch (IOException ioe) { ioExceptions.add(ioe); continue; } DnsMessage responseMessage = dnsQueryResult.response; if (!responseMessage.recursionAvailable) { boolean newRaServer = nonRaServers.add(dns); if (newRaServer) { LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution"); } continue; } if (disableResultFilter) { return dnsQueryResult; } switch (responseMessage.responseCode) { case NO_ERROR: case NX_DOMAIN: break; default: String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.'; if (!LOGGER.isLoggable(Level.FINE)) { warning += "\n" + responseMessage; } LOGGER.warning(warning); ErrorResponseException exception = new ErrorResponseException(q, dnsQueryResult); ioExceptions.add(exception); continue; } return dnsQueryResult; } MultipleIoException.throwIfRequired(ioExceptions); throw new NoQueryPossibleException(q); } DnsClient();  DnsClient(DnsCache dnsCache); @Override DnsQueryResult query(DnsMessage.Builder queryBuilder); static List<String> findDNS(); static List<InetAddress> findDnsAddresses(); static void addDnsServerLookupMechanism(DnsServerLookupMechanism dnsServerLookup); static boolean removeDNSServerLookupMechanism(DnsServerLookupMechanism dnsServerLookup); static boolean addBlacklistedDnsServer(String dnsServer); static boolean removeBlacklistedDnsServer(String dnsServer); boolean isAskForDnssec(); void setAskForDnssec(boolean askForDnssec); boolean isDisableResultFilter(); void setDisableResultFilter(boolean disableResultFilter); boolean isUseHardcodedDnsServersEnabled(); void setUseHardcodedDnsServers(boolean useHardcodedDnsServers); InetAddress getRandomHardcodedIpv4DnsServer(); InetAddress getRandomHarcodedIpv6DnsServer(); }
DnsClient extends AbstractDnsClient { @Override public DnsQueryResult query(DnsMessage.Builder queryBuilder) throws IOException { DnsMessage q = newQuestion(queryBuilder).build(); DnsQueryResult dnsQueryResult = (cache == null) ? null : cache.get(q); if (dnsQueryResult != null) { return dnsQueryResult; } List<InetAddress> dnsServerAddresses = getServerAddresses(); List<IOException> ioExceptions = new ArrayList<>(dnsServerAddresses.size()); for (InetAddress dns : dnsServerAddresses) { if (nonRaServers.contains(dns)) { LOGGER.finer("Skipping " + dns + " because it was marked as \"recursion not available\""); continue; } try { dnsQueryResult = query(q, dns); } catch (IOException ioe) { ioExceptions.add(ioe); continue; } DnsMessage responseMessage = dnsQueryResult.response; if (!responseMessage.recursionAvailable) { boolean newRaServer = nonRaServers.add(dns); if (newRaServer) { LOGGER.warning("The DNS server " + dns + " returned a response without the \"recursion available\" (RA) flag set. This likely indicates a misconfiguration because the server is not suitable for DNS resolution"); } continue; } if (disableResultFilter) { return dnsQueryResult; } switch (responseMessage.responseCode) { case NO_ERROR: case NX_DOMAIN: break; default: String warning = "Response from " + dns + " asked for " + q.getQuestion() + " with error code: " + responseMessage.responseCode + '.'; if (!LOGGER.isLoggable(Level.FINE)) { warning += "\n" + responseMessage; } LOGGER.warning(warning); ErrorResponseException exception = new ErrorResponseException(q, dnsQueryResult); ioExceptions.add(exception); continue; } return dnsQueryResult; } MultipleIoException.throwIfRequired(ioExceptions); throw new NoQueryPossibleException(q); } DnsClient();  DnsClient(DnsCache dnsCache); @Override DnsQueryResult query(DnsMessage.Builder queryBuilder); static List<String> findDNS(); static List<InetAddress> findDnsAddresses(); static void addDnsServerLookupMechanism(DnsServerLookupMechanism dnsServerLookup); static boolean removeDNSServerLookupMechanism(DnsServerLookupMechanism dnsServerLookup); static boolean addBlacklistedDnsServer(String dnsServer); static boolean removeBlacklistedDnsServer(String dnsServer); boolean isAskForDnssec(); void setAskForDnssec(boolean askForDnssec); boolean isDisableResultFilter(); void setDisableResultFilter(boolean disableResultFilter); boolean isUseHardcodedDnsServersEnabled(); void setUseHardcodedDnsServers(boolean useHardcodedDnsServers); InetAddress getRandomHardcodedIpv4DnsServer(); InetAddress getRandomHarcodedIpv6DnsServer();  }