Search in sources :

Example 1 with DnsName

use of org.minidns.dnsname.DnsName 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 DnsName

use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.

the class IterativeDNSClient method queryRecursive.

private DNSMessage queryRecursive(ResolutionState resolutionState, DNSMessage q) throws IOException {
    InetAddress primaryTarget = null, secondaryTarget = null;
    Question question = q.getQuestion();
    DNSName parent = question.name.getParent();
    switch(ipVersionSetting) {
        case v4only:
            for (A a : getCachedIPv4NameserverAddressesFor(parent)) {
                if (primaryTarget == null) {
                    primaryTarget = a.getInetAddress();
                    continue;
                }
                secondaryTarget = a.getInetAddress();
                break;
            }
            break;
        case v6only:
            for (AAAA aaaa : getCachedIPv6NameserverAddressesFor(parent)) {
                if (primaryTarget == null) {
                    primaryTarget = aaaa.getInetAddress();
                    continue;
                }
                secondaryTarget = aaaa.getInetAddress();
                break;
            }
            break;
        case v4v6:
            InetAddress[] v4v6targets = getTargets(getCachedIPv4NameserverAddressesFor(parent), getCachedIPv6NameserverAddressesFor(parent));
            primaryTarget = v4v6targets[0];
            secondaryTarget = v4v6targets[1];
            break;
        case v6v4:
            InetAddress[] v6v4targets = getTargets(getCachedIPv6NameserverAddressesFor(parent), getCachedIPv4NameserverAddressesFor(parent));
            primaryTarget = v6v4targets[0];
            secondaryTarget = v6v4targets[1];
            break;
        default:
            throw new AssertionError();
    }
    DNSName authoritativeZone = parent;
    if (primaryTarget == null) {
        authoritativeZone = DNSName.ROOT;
        switch(ipVersionSetting) {
            case v4only:
                primaryTarget = getRandomIpv4RootServer();
                break;
            case v6only:
                primaryTarget = getRandomIpv6RootServer();
                break;
            case v4v6:
                primaryTarget = getRandomIpv4RootServer();
                secondaryTarget = getRandomIpv6RootServer();
                break;
            case v6v4:
                primaryTarget = getRandomIpv6RootServer();
                secondaryTarget = getRandomIpv4RootServer();
                break;
        }
    }
    List<IOException> ioExceptions = new LinkedList<>();
    try {
        return queryRecursive(resolutionState, q, primaryTarget, authoritativeZone);
    } catch (IOException ioException) {
        abortIfFatal(ioException);
        ioExceptions.add(ioException);
    }
    if (secondaryTarget != null) {
        try {
            return queryRecursive(resolutionState, q, secondaryTarget, authoritativeZone);
        } catch (IOException ioException) {
            ioExceptions.add(ioException);
        }
    }
    MultipleIoException.throwIfRequired(ioExceptions);
    return null;
}
Also used : AAAA(org.minidns.record.AAAA) A(org.minidns.record.A) Question(org.minidns.dnsmessage.Question) IOException(java.io.IOException) DNSName(org.minidns.dnsname.DNSName) InetAddress(java.net.InetAddress) AAAA(org.minidns.record.AAAA) LinkedList(java.util.LinkedList)

Example 3 with DnsName

use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.

the class DNSNameTest method testFromVarargs.

@Test
public void testFromVarargs() {
    String leftString = "leftmost.left";
    String middleString = "leftMiddle.middle.rightMiddle";
    String rightString = "right.rightMost";
    DNSName left = DNSName.from(leftString);
    DNSName middle = DNSName.from(middleString);
    DNSName right = DNSName.from(rightString);
    DNSName name = DNSName.from(left, middle, right);
    String completeString = leftString + '.' + middleString + '.' + rightString;
    assertEquals(name.getRawAce(), completeString);
    DNSName expected = DNSName.from(completeString);
    assertEquals(name, expected);
}
Also used : DNSName(org.minidns.dnsname.DNSName) Test(org.junit.Test)

Example 4 with DnsName

use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.

the class DNSNameTest method testConcact.

@Test
public void testConcact() {
    String leftString = "foo.bar.de";
    String rightString = "example.org";
    DNSName left = DNSName.from(leftString);
    DNSName right = DNSName.from(rightString);
    DNSName concated = DNSName.from(left, right);
    DNSName expected = DNSName.from(leftString + '.' + rightString);
    assertEquals(expected, concated);
}
Also used : DNSName(org.minidns.dnsname.DNSName) Test(org.junit.Test)

Example 5 with DnsName

use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.

the class DNSNameTest method caseInsenstiveCompare.

@Test
public void caseInsenstiveCompare() {
    DNSName lowercase = DNSName.from("cs.fau.de");
    DNSName uppercase = DNSName.from("CS.fau.de");
    assertEquals(lowercase, uppercase);
}
Also used : DNSName(org.minidns.dnsname.DNSName) Test(org.junit.Test)

Aggregations

DNSName (org.minidns.dnsname.DNSName)22 Test (org.junit.Test)8 IOException (java.io.IOException)6 DnsName (org.minidns.dnsname.DnsName)5 InetAddress (java.net.InetAddress)4 ArrayList (java.util.ArrayList)4 Data (org.minidns.record.Data)4 Record (org.minidns.record.Record)4 LinkedList (java.util.LinkedList)3 DNSMessage (org.minidns.dnsmessage.DNSMessage)3 Question (org.minidns.dnsmessage.Question)3 TYPE (org.minidns.record.Record.TYPE)3 UInt16 (org.jivesoftware.smack.datatypes.UInt16)2 RemoteConnectionEndpointLookupFailure (org.jivesoftware.smack.util.rce.RemoteConnectionEndpointLookupFailure)2 InternetAddressRR (org.minidns.record.InternetAddressRR)2 SRV (org.minidns.record.SRV)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 Inet4Address (java.net.Inet4Address)1 Inet6Address (java.net.Inet6Address)1