Search in sources :

Example 51 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class ReliableDNSClient method query.

@Override
protected DNSMessage query(DNSMessage.Builder q) throws IOException {
    DNSMessage dnsMessage = null;
    String unacceptableReason = null;
    List<IOException> ioExceptions = new LinkedList<>();
    if (mode != Mode.iterativeOnly) {
        // Try a recursive query.
        try {
            dnsMessage = dnsClient.query(q);
            if (dnsMessage != null) {
                unacceptableReason = isResponseAcceptable(dnsMessage);
                if (unacceptableReason == null) {
                    return dnsMessage;
                }
            }
        } catch (IOException ioException) {
            ioExceptions.add(ioException);
        }
    }
    // Abort if we the are in "recursive only" mode.
    if (mode == Mode.recursiveOnly)
        return dnsMessage;
    // Eventually log that we fall back to iterative mode.
    final Level FALLBACK_LOG_LEVEL = Level.FINE;
    if (LOGGER.isLoggable(FALLBACK_LOG_LEVEL) && mode != Mode.iterativeOnly) {
        String logString = "Resolution fall back to iterative mode because: ";
        if (!ioExceptions.isEmpty()) {
            logString += ioExceptions.get(0);
        } else if (dnsMessage == null) {
            logString += " DNSClient did not return a response";
        } else if (unacceptableReason != null) {
            logString += unacceptableReason + ". Response:\n" + dnsMessage;
        } else {
            throw new AssertionError("This should never been reached");
        }
        LOGGER.log(FALLBACK_LOG_LEVEL, logString);
    }
    try {
        dnsMessage = recursiveDnsClient.query(q);
    } catch (IOException ioException) {
        ioExceptions.add(ioException);
    }
    if (dnsMessage == null) {
        MultipleIoException.throwIfRequired(ioExceptions);
    }
    return dnsMessage;
}
Also used : Level(java.util.logging.Level) IOException(java.io.IOException) LinkedList(java.util.LinkedList) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 52 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AsyncApiTest method simpleAsyncApiTest.

public static void simpleAsyncApiTest() throws IOException {
    DNSClient client = new DNSClient();
    client.setDataSource(new AsyncNetworkDataSource());
    client.getDataSource().setTimeout(60 * 60 * 1000);
    MiniDnsFuture<DNSMessage, IOException> future = client.queryAsync("example.com", Record.TYPE.NS);
    DNSMessage response = future.getOrThrow();
    assertEquals(RESPONSE_CODE.NO_ERROR, response.responseCode);
}
Also used : DNSClient(org.minidns.DNSClient) IOException(java.io.IOException) AsyncNetworkDataSource(org.minidns.source.async.AsyncNetworkDataSource) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 53 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class AsyncApiTest method tcpAsyncApiTest.

public static void tcpAsyncApiTest() throws IOException {
    DNSDataSource dataSource = new AsyncNetworkDataSource();
    dataSource.setTimeout(60 * 60 * 1000);
    dataSource.setUdpPayloadSize(256);
    dataSource.setQueryMode(QueryMode.tcp);
    DNSClient client = new DNSClient();
    client.setDataSource(dataSource);
    client.setAskForDnssec(true);
    MiniDnsFuture<DNSMessage, IOException> future = client.queryAsync("google.com", Record.TYPE.AAAA);
    DNSMessage response = future.getOrThrow();
    assertEquals(RESPONSE_CODE.NO_ERROR, response.responseCode);
}
Also used : DNSDataSource(org.minidns.source.DNSDataSource) DNSClient(org.minidns.DNSClient) IOException(java.io.IOException) AsyncNetworkDataSource(org.minidns.source.async.AsyncNetworkDataSource) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 54 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class CoreTest method testExampleCom.

@IntegrationTest
public static void testExampleCom() throws IOException {
    DNSClient client = new DNSClient(new LRUCache(1024));
    // stable?
    String exampleIp4 = "93.184.216.34";
    // stable?
    String exampleIp6 = "2606:2800:220:1:248:1893:25c8:1946";
    assertEquals(client.query("example.com", Record.TYPE.A).answerSection.get(0).payloadData.toString(), exampleIp4);
    assertEquals(client.query("www.example.com", Record.TYPE.A).answerSection.get(0).payloadData.toString(), exampleIp4);
    assertEquals(client.query("example.com", Record.TYPE.AAAA).answerSection.get(0).payloadData.toString(), exampleIp6);
    assertEquals(client.query("www.example.com", Record.TYPE.AAAA).answerSection.get(0).payloadData.toString(), exampleIp6);
    DNSMessage nsRecords = client.query("example.com", Record.TYPE.NS);
    List<String> values = new ArrayList<>();
    for (Record<? extends Data> record : nsRecords.answerSection) {
        values.add(record.payloadData.toString());
    }
    Collections.sort(values);
    assertEquals(values.get(0), "a.iana-servers.net.");
    assertEquals(values.get(1), "b.iana-servers.net.");
}
Also used : DNSClient(org.minidns.DNSClient) LRUCache(org.minidns.cache.LRUCache) ArrayList(java.util.ArrayList) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 55 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage in project minidns by MiniDNS.

the class CoreTest method testTcpAnswer.

@IntegrationTest
public static void testTcpAnswer() throws IOException {
    DNSClient client = new DNSClient(new LRUCache(1024));
    client.setAskForDnssec(true);
    client.setDisableResultFilter(true);
    DNSMessage result = client.query("www-nsec.example.com", Record.TYPE.A);
    assertNotNull(result);
    assertTrue(result.toArray().length > 512);
}
Also used : DNSClient(org.minidns.DNSClient) LRUCache(org.minidns.cache.LRUCache) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Aggregations

DNSMessage (org.minidns.dnsmessage.DNSMessage)67 Test (org.junit.Test)35 Data (org.minidns.record.Data)16 Record (org.minidns.record.Record)16 IOException (java.io.IOException)15 Question (org.minidns.dnsmessage.Question)14 InetAddress (java.net.InetAddress)6 LRUCache (org.minidns.cache.LRUCache)6 ArrayList (java.util.ArrayList)5 DNSClient (org.minidns.DNSClient)5 LinkedList (java.util.LinkedList)4 EDNS (org.minidns.edns.EDNS)4 RRSIG (org.minidns.record.RRSIG)4 Date (java.util.Date)3 Level (java.util.logging.Level)3 InternalMiniDnsFuture (org.minidns.MiniDnsFuture.InternalMiniDnsFuture)3 DNSName (org.minidns.dnsname.DNSName)3 DNSKEY (org.minidns.record.DNSKEY)3 DatagramSocket (java.net.DatagramSocket)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2