Search in sources :

Example 6 with Question

use of org.minidns.dnsmessage.Question 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 7 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) 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 8 with Question

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

the class DNSMessageTest method testFilledMessageToString.

@Test
public void testFilledMessageToString() throws Exception {
    // toString() should never throw an exception or be null
    DNSMessage.Builder message = DNSMessage.builder();
    message.setOpcode(DNSMessage.OPCODE.QUERY);
    message.setResponseCode(DNSMessage.RESPONSE_CODE.NO_ERROR);
    message.setId(1337);
    message.setAuthoritativeAnswer(true);
    message.addQuestion(new Question("www.example.com", TYPE.A));
    message.addAnswer(record("www.example.com", a("127.0.0.1")));
    message.addNameserverRecords(record("example.com", ns("ns.example.com")));
    message.addAdditionalResourceRecord(record("ns.example.com", a("127.0.0.1")));
    message.getEdnsBuilder().setUdpPayloadSize(512);
    assertNotNull(message.build().toString());
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 9 with Question

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

the class DNSMessageTest method testFilledMessageTerminalOutput.

@Test
public void testFilledMessageTerminalOutput() throws Exception {
    // asTerminalOutput() follows a certain design, however it might change in the future.
    // Once asTerminalOutput() is changed, it might be required to update this test routine.
    DNSMessage.Builder message = DNSMessage.builder();
    message.setOpcode(DNSMessage.OPCODE.QUERY);
    message.setResponseCode(DNSMessage.RESPONSE_CODE.NO_ERROR);
    message.setId(1337);
    message.setAuthoritativeAnswer(true);
    message.addQuestion(new Question("www.example.com", TYPE.A));
    message.addAnswer(record("www.example.com", a("127.0.0.1")));
    message.addNameserverRecords(record("example.com", ns("ns.example.com")));
    message.addAdditionalResourceRecord(record("ns.example.com", a("127.0.0.1")));
    message.getEdnsBuilder().setUdpPayloadSize(512);
    assertNotNull(message.build().asTerminalOutput());
}
Also used : Question(org.minidns.dnsmessage.Question) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 10 with Question

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

the class DNSSECClient method verifyAnswer.

private Set<UnverifiedReason> verifyAnswer(DNSMessage dnsMessage) throws IOException {
    Question q = dnsMessage.questions.get(0);
    List<Record<? extends Data>> answers = dnsMessage.answerSection;
    List<Record<? extends Data>> toBeVerified = dnsMessage.copyAnswers();
    VerifySignaturesResult verifiedSignatures = verifySignatures(q, answers, toBeVerified);
    Set<UnverifiedReason> result = verifiedSignatures.reasons;
    if (!result.isEmpty()) {
        return result;
    }
    // Keep SEPs separated, we only need one valid SEP.
    boolean sepSignatureValid = false;
    Set<UnverifiedReason> sepReasons = new HashSet<>();
    for (Iterator<Record<? extends Data>> iterator = toBeVerified.iterator(); iterator.hasNext(); ) {
        Record<DNSKEY> record = iterator.next().ifPossibleAs(DNSKEY.class);
        if (record == null) {
            continue;
        }
        // Verify all DNSKEYs as if it was a SEP. If we find a single SEP we are safe.
        Set<UnverifiedReason> reasons = verifySecureEntryPoint(q, record);
        if (reasons.isEmpty()) {
            sepSignatureValid = true;
        } else {
            sepReasons.addAll(reasons);
        }
        if (!verifiedSignatures.sepSignaturePresent) {
            LOGGER.finer("SEP key is not self-signed.");
        }
        iterator.remove();
    }
    if (verifiedSignatures.sepSignaturePresent && !sepSignatureValid) {
        result.addAll(sepReasons);
    }
    if (verifiedSignatures.sepSignatureRequired && !verifiedSignatures.sepSignaturePresent) {
        result.add(new NoSecureEntryPointReason(q.name.ace));
    }
    if (!toBeVerified.isEmpty()) {
        if (toBeVerified.size() != answers.size()) {
            throw new DNSSECValidationFailedException(q, "Only some records are signed!");
        } else {
            result.add(new NoSignaturesReason(q));
        }
    }
    return result;
}
Also used : Data(org.minidns.record.Data) DNSKEY(org.minidns.record.DNSKEY) NoSignaturesReason(org.minidns.dnssec.UnverifiedReason.NoSignaturesReason) Question(org.minidns.dnsmessage.Question) Record(org.minidns.record.Record) NoSecureEntryPointReason(org.minidns.dnssec.UnverifiedReason.NoSecureEntryPointReason) HashSet(java.util.HashSet)

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