Search in sources :

Example 1 with NS

use of org.minidns.record.NS 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 2 with NS

use of org.minidns.record.NS in project minidns by MiniDNS.

the class AbstractDNSClient method getCachedIPNameserverAddressesFor.

@SuppressWarnings("unchecked")
private <D extends Data> Set<D> getCachedIPNameserverAddressesFor(DNSName dnsName, TYPE type) {
    Set<NS> nsSet = getCachedNameserverRecordsFor(dnsName);
    if (nsSet.isEmpty())
        return Collections.emptySet();
    Set<D> res = new HashSet<>(3 * nsSet.size());
    for (NS ns : nsSet) {
        Set<D> addresses;
        switch(type) {
            case A:
                addresses = (Set<D>) getCachedIPv4AddressesFor(ns.target);
                break;
            case AAAA:
                addresses = (Set<D>) getCachedIPv6AddressesFor(ns.target);
                break;
            default:
                throw new AssertionError();
        }
        res.addAll(addresses);
    }
    return res;
}
Also used : NS(org.minidns.record.NS) HashSet(java.util.HashSet)

Aggregations

NS (org.minidns.record.NS)2 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 DNSMessage (org.minidns.dnsmessage.DNSMessage)1 Question (org.minidns.dnsmessage.Question)1 DNSName (org.minidns.dnsname.DNSName)1 Data (org.minidns.record.Data)1 Record (org.minidns.record.Record)1