Search in sources :

Example 66 with DNSMessage

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

the class ExtendedLRUCache method gather.

private final void gather(Map<DNSMessage, List<Record<? extends Data>>> extraCaches, DNSMessage q, List<Record<? extends Data>> records, DNSName authoritativeZone) {
    for (Record<? extends Data> extraRecord : records) {
        if (!shouldGather(extraRecord, q.getQuestion(), authoritativeZone))
            continue;
        DNSMessage.Builder additionalRecordQuestionBuilder = extraRecord.getQuestionMessage();
        if (additionalRecordQuestionBuilder == null)
            continue;
        additionalRecordQuestionBuilder.copyFlagsFrom(q);
        additionalRecordQuestionBuilder.setAdditionalResourceRecords(q.additionalSection);
        DNSMessage additionalRecordQuestion = additionalRecordQuestionBuilder.build();
        if (additionalRecordQuestion.equals(q)) {
            // No need to cache the additional question if it is the same as the original question.
            continue;
        }
        List<Record<? extends Data>> additionalRecords = extraCaches.get(additionalRecordQuestion);
        if (additionalRecords == null) {
            additionalRecords = new LinkedList<>();
            extraCaches.put(additionalRecordQuestion, additionalRecords);
        }
        additionalRecords.add(extraRecord);
    }
}
Also used : Record(org.minidns.record.Record) Data(org.minidns.record.Data) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 67 with DNSMessage

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

the class LRUCache method getNormalized.

@Override
protected synchronized DNSMessage getNormalized(DNSMessage q) {
    DNSMessage message = backend.get(q);
    if (message == null) {
        missCount++;
        return null;
    }
    long ttl = maxTTL;
    // shortest TTL to be the effective one.
    for (Record<? extends Data> r : message.answerSection) {
        ttl = Math.min(ttl, r.ttl);
    }
    if (message.receiveTimestamp + ttl < System.currentTimeMillis()) {
        missCount++;
        expireCount++;
        backend.remove(q);
        return null;
    } else {
        hitCount++;
        return message;
    }
}
Also used : DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 68 with DNSMessage

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

the class AbstractDNSClient method queryAsync.

/**
 * Default implementation of an asynchronous DNS query which just wraps the synchronous case.
 * <p>
 * Subclasses override this method to support true asynchronous queries.
 * </p>
 *
 * @param query the query.
 * @return a future for this query.
 */
protected MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage.Builder query) {
    InternalMiniDnsFuture<DNSMessage, IOException> future = new InternalMiniDnsFuture<>();
    DNSMessage result;
    try {
        result = query(query);
    } catch (IOException e) {
        future.setException(e);
        return future;
    }
    future.setResult(result);
    return future;
}
Also used : InternalMiniDnsFuture(org.minidns.MiniDnsFuture.InternalMiniDnsFuture) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 69 with DNSMessage

use of org.minidns.dnsmessage.DNSMessage in project Smack by igniterealtime.

the class DnsOverXmppMiniDnsResolverTest method dnsOverXmppMiniDnsResolverTest.

@Test
public void dnsOverXmppMiniDnsResolverTest() throws IOException {
    TestDnsDataSource dnsSource = new TestDnsDataSource();
    TestDnsDataSource dnssecSource = new TestDnsDataSource();
    DnsClient dnsClient = new DnsClient(NoopDnsCache.INSTANCE);
    dnsClient.setDataSource(dnsSource);
    DnssecClient dnssecClient = new DnssecClient(NoopDnsCache.INSTANCE);
    dnssecClient.setDataSource(dnssecSource);
    DnsOverXmppMiniDnsResolver doxResolver = new DnsOverXmppMiniDnsResolver(dnsClient, dnssecClient);
    Question question = new Question("example.org", TYPE.A);
    {
        DnsMessage nondnssecQuery = question.asQueryMessage();
        doxResolver.resolve(nondnssecQuery);
        assertTrue(dnsSource.getAndResetWasQueried());
        assertFalse(dnssecSource.getAndResetWasQueried());
    }
    {
        DnsMessage.Builder dnssecQueryBuilder = question.asMessageBuilder();
        dnssecQueryBuilder.getEdnsBuilder().setDnssecOk();
        DnsMessage dnssecQuery = dnssecQueryBuilder.build();
        DnssecValidationFailedException dnssecValidationFailedException = null;
        try {
            doxResolver.resolve(dnssecQuery);
        } catch (DnssecValidationFailedException e) {
            dnssecValidationFailedException = e;
        }
        // This exception is expected since we don't have a realy DNS source.
        assertNotNull(dnssecValidationFailedException);
        assertFalse(dnsSource.getAndResetWasQueried());
        assertTrue(dnssecSource.getAndResetWasQueried());
    }
}
Also used : DnssecClient(org.minidns.dnssec.DnssecClient) DnssecValidationFailedException(org.minidns.dnssec.DnssecValidationFailedException) DnsClient(org.minidns.DnsClient) Question(org.minidns.dnsmessage.Question) DnsMessage(org.minidns.dnsmessage.DnsMessage) Test(org.junit.jupiter.api.Test)

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