Search in sources :

Example 11 with DnsMessage

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

the class DNSClientTest method testReturnNullSource.

@Test
public void testReturnNullSource() throws IOException {
    class NullSource extends DNSDataSource {

        boolean queried = false;

        @Override
        public DNSMessage query(DNSMessage message, InetAddress address, int port) {
            queried = true;
            return null;
        }
    }
    DNSClient client = new DNSClient(new LRUCache(0));
    NullSource source = new NullSource();
    client.setDataSource(source);
    DNSMessage message = client.query("www.example.com", TYPE.A);
    assertNull(message);
    assertTrue(source.queried);
}
Also used : DNSDataSource(org.minidns.source.DNSDataSource) LRUCache(org.minidns.cache.LRUCache) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 12 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage 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 13 with DnsMessage

use of org.minidns.dnsmessage.DnsMessage 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)

Example 14 with DnsMessage

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

the class IterativeDNSClient method resolveIpRecursive.

private IpResultSet resolveIpRecursive(ResolutionState resolutionState, DNSName name) throws IOException {
    IpResultSet.Builder res = newIpResultSetBuilder();
    if (ipVersionSetting.v4) {
        // TODO Try to retrieve A records for name out from cache.
        Question question = new Question(name, TYPE.A);
        final DNSMessage query = getQueryFor(question);
        DNSMessage aMessage = queryRecursive(resolutionState, query);
        if (aMessage != null) {
            for (Record<? extends Data> answer : aMessage.answerSection) {
                if (answer.isAnswer(question)) {
                    InetAddress inetAddress = inetAddressFromRecord(name.ace, (A) answer.payloadData);
                    res.ipv4Addresses.add(inetAddress);
                } else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
                    return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
                }
            }
        }
    }
    if (ipVersionSetting.v6) {
        // TODO Try to retrieve AAAA records for name out from cache.
        Question question = new Question(name, TYPE.AAAA);
        final DNSMessage query = getQueryFor(question);
        DNSMessage aMessage = queryRecursive(resolutionState, query);
        if (aMessage != null) {
            for (Record<? extends Data> answer : aMessage.answerSection) {
                if (answer.isAnswer(question)) {
                    InetAddress inetAddress = inetAddressFromRecord(name.ace, (AAAA) answer.payloadData);
                    res.ipv6Addresses.add(inetAddress);
                } else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
                    return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
                }
            }
        }
    }
    return res.build();
}
Also used : RRWithTarget(org.minidns.record.RRWithTarget) Question(org.minidns.dnsmessage.Question) InetAddress(java.net.InetAddress) DNSMessage(org.minidns.dnsmessage.DNSMessage)

Example 15 with DnsMessage

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

the class IterativeDNSClient method query.

/**
 * Recursively query the DNS system for one entry.
 *
 * @param queryBuilder The query DNS message builder.
 * @return The response (or null on timeout/error).
 * @throws IOException if an IO error occurs.
 */
@Override
protected DNSMessage query(DNSMessage.Builder queryBuilder) throws IOException {
    DNSMessage q = queryBuilder.build();
    ResolutionState resolutionState = new ResolutionState(this);
    DNSMessage message = queryRecursive(resolutionState, q);
    return message;
}
Also used : 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