Search in sources :

Example 1 with Question

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

the class AbstractDNSClient method query.

public final DNSMessage query(DNSMessage requestMessage, InetAddress address, int port) throws IOException {
    // See if we have the answer to this question already cached
    DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
    if (responseMessage != null) {
        return responseMessage;
    }
    final Question q = requestMessage.getQuestion();
    final Level TRACE_LOG_LEVEL = Level.FINE;
    LOGGER.log(TRACE_LOG_LEVEL, "Asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
    try {
        responseMessage = dataSource.query(requestMessage, address, port);
    } catch (IOException e) {
        LOGGER.log(TRACE_LOG_LEVEL, "IOException {0} on {1} while resolving {2}: {3}", new Object[] { address, port, q, e });
        throw e;
    }
    if (responseMessage != null) {
        LOGGER.log(TRACE_LOG_LEVEL, "Response from {0} on {1} for {2}:\n{3}", new Object[] { address, port, q, responseMessage });
    } else {
        // TODO When should this ever happen?
        LOGGER.log(Level.SEVERE, "NULL response from " + address + " on " + port + " for " + q);
    }
    if (responseMessage == null)
        return null;
    onResponseCallback.onResponse(requestMessage, responseMessage);
    return responseMessage;
}
Also used : Question(org.minidns.dnsmessage.Question) Level(java.util.logging.Level) IOException(java.io.IOException) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 2 with Question

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

the class AbstractDNSClient method queryAsync.

public final MiniDnsFuture<DNSMessage, IOException> queryAsync(DNSMessage requestMessage, InetAddress address, int port) {
    // See if we have the answer to this question already cached
    DNSMessage responseMessage = (cache == null) ? null : cache.get(requestMessage);
    if (responseMessage != null) {
        return MiniDnsFuture.from(responseMessage);
    }
    final Question q = requestMessage.getQuestion();
    final Level TRACE_LOG_LEVEL = Level.FINE;
    LOGGER.log(TRACE_LOG_LEVEL, "Asynchronusly asking {0} on {1} for {2} with:\n{3}", new Object[] { address, port, q, requestMessage });
    return dataSource.queryAsync(requestMessage, address, port, onResponseCallback);
}
Also used : Question(org.minidns.dnsmessage.Question) Level(java.util.logging.Level) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 3 with Question

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

the class AbstractDNSClient method getCachedRecordsFor.

private <D extends Data> Set<D> getCachedRecordsFor(DNSName dnsName, TYPE type) {
    Question dnsNameNs = new Question(dnsName, type);
    DNSMessage queryDnsNameNs = getQueryFor(dnsNameNs);
    DNSMessage cachedResult = cache.get(queryDnsNameNs);
    if (cachedResult == null)
        return Collections.emptySet();
    return cachedResult.getAnswersFor(dnsNameNs);
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 4 with Question

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

the class LRUCacheTest method testOverfilledCache.

@Test
public void testOverfilledCache() {
    Question q = new Question("", Record.TYPE.A);
    DNSMessage question = q.asQueryMessage();
    lruCache.put(question, createSampleMessage());
    assertNotNull(lruCache.get(question));
    lruCache.put(new Question("1", Record.TYPE.A).asQueryMessage(), createSampleMessage());
    lruCache.put(new Question("2", Record.TYPE.A).asQueryMessage(), createSampleMessage());
    lruCache.put(new Question("3", Record.TYPE.A).asQueryMessage(), createSampleMessage());
    lruCache.put(new Question("4", Record.TYPE.A).asQueryMessage(), createSampleMessage());
    lruCache.put(new Question("5", Record.TYPE.A).asQueryMessage(), createSampleMessage());
    assertNull(lruCache.get(question));
    assertEquals(0, lruCache.getExpireCount());
    assertEquals(1, lruCache.getMissCount());
    assertEquals(1, lruCache.getHitCount());
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 5 with Question

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

the class IterativeDNSClient method queryRecursive.

private DNSMessage queryRecursive(ResolutionState resolutionState, DNSMessage q, InetAddress address, DNSName authoritativeZone) throws IOException {
    resolutionState.recurse(address, q);
    DNSMessage resMessage = query(q, address);
    if (resMessage == null) {
        // TODO throw exception here?
        return null;
    }
    if (resMessage.authoritativeAnswer) {
        return resMessage;
    }
    if (cache != null) {
        cache.offer(q, resMessage, authoritativeZone);
    }
    List<Record<? extends Data>> authorities = resMessage.copyAuthority();
    List<IOException> ioExceptions = new LinkedList<>();
    // Glued NS first
    for (Iterator<Record<? extends Data>> iterator = authorities.iterator(); iterator.hasNext(); ) {
        Record<? extends Data> record = iterator.next();
        if (record.type != TYPE.NS) {
            iterator.remove();
            continue;
        }
        DNSName name = ((NS) record.payloadData).target;
        IpResultSet gluedNs = searchAdditional(resMessage, name);
        for (Iterator<InetAddress> addressIterator = gluedNs.addresses.iterator(); addressIterator.hasNext(); ) {
            InetAddress target = addressIterator.next();
            DNSMessage recursive = null;
            try {
                recursive = queryRecursive(resolutionState, q, target, record.name);
            } catch (IOException e) {
                abortIfFatal(e);
                LOGGER.log(Level.FINER, "Exception while recursing", e);
                resolutionState.decrementSteps();
                ioExceptions.add(e);
                if (!addressIterator.hasNext()) {
                    iterator.remove();
                }
                continue;
            }
            return recursive;
        }
    }
    // Try non-glued NS
    for (Record<? extends Data> record : authorities) {
        final Question question = q.getQuestion();
        DNSName name = ((NS) record.payloadData).target;
        // AAAA RR, then we should not continue here as it would result in an endless loop.
        if (question.name.equals(name) && (question.type == TYPE.A || question.type == TYPE.AAAA))
            continue;
        IpResultSet res = null;
        try {
            res = resolveIpRecursive(resolutionState, name);
        } catch (IOException e) {
            resolutionState.decrementSteps();
            ioExceptions.add(e);
        }
        if (res == null) {
            continue;
        }
        for (InetAddress target : res.addresses) {
            DNSMessage recursive = null;
            try {
                recursive = queryRecursive(resolutionState, q, target, record.name);
            } catch (IOException e) {
                resolutionState.decrementSteps();
                ioExceptions.add(e);
                continue;
            }
            return recursive;
        }
    }
    MultipleIoException.throwIfRequired(ioExceptions);
    // where we able to find glue records or the IPs of the next nameservers.
    return null;
}
Also used : NS(org.minidns.record.NS) Data(org.minidns.record.Data) IOException(java.io.IOException) DNSName(org.minidns.dnsname.DNSName) LinkedList(java.util.LinkedList) Record(org.minidns.record.Record) Question(org.minidns.dnsmessage.Question) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Aggregations

Question (org.minidns.dnsmessage.Question)25 DNSMessage (org.minidns.dnsmessage.DNSMessage)15 Test (org.junit.Test)6 IOException (java.io.IOException)4 InetAddress (java.net.InetAddress)4 DNSName (org.minidns.dnsname.DNSName)3 Data (org.minidns.record.Data)3 Record (org.minidns.record.Record)3 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 Level (java.util.logging.Level)2 DnsMessage (org.minidns.dnsmessage.DnsMessage)2 TYPE (org.minidns.record.Record.TYPE)2 XMPPTCPConnection (org.jivesoftware.smack.tcp.XMPPTCPConnection)1 DnsOverXmppManager (org.jivesoftware.smackx.dox.DnsOverXmppManager)1 Test (org.junit.jupiter.api.Test)1 Jid (org.jxmpp.jid.Jid)1 DNSClient (org.minidns.DNSClient)1 DnsClient (org.minidns.DnsClient)1 Builder (org.minidns.dnsmessage.DNSMessage.Builder)1