Search in sources :

Example 1 with EDNS

use of org.minidns.edns.EDNS in project minidns by MiniDNS.

the class DNSMessage method asTerminalOutput.

/**
 * Format the DNSMessage object in a way suitable for terminal output.
 * The format is loosely based on the output provided by {@code dig}.
 *
 * @return This message as a String suitable for terminal output.
 */
public String asTerminalOutput() {
    if (terminalOutputCache != null)
        return terminalOutputCache;
    StringBuilder sb = new StringBuilder(";; ->>HEADER<<-").append(" opcode: ").append(opcode).append(", status: ").append(responseCode).append(", id: ").append(id).append("\n").append(";; flags:");
    if (!qr)
        sb.append(" qr");
    if (authoritativeAnswer)
        sb.append(" aa");
    if (truncated)
        sb.append(" tr");
    if (recursionDesired)
        sb.append(" rd");
    if (recursionAvailable)
        sb.append(" ra");
    if (authenticData)
        sb.append(" ad");
    if (checkingDisabled)
        sb.append(" cd");
    sb.append("; QUERY: ").append(questions.size()).append(", ANSWER: ").append(answerSection.size()).append(", AUTHORITY: ").append(authoritySection.size()).append(", ADDITIONAL: ").append(additionalSection.size()).append("\n\n");
    for (Record<? extends Data> record : additionalSection) {
        EDNS edns = EDNS.fromRecord(record);
        if (edns != null) {
            sb.append(";; OPT PSEUDOSECTION:\n; ").append(edns.asTerminalOutput());
            break;
        }
    }
    if (questions.size() != 0) {
        sb.append(";; QUESTION SECTION:\n");
        for (Question question : questions) {
            sb.append(';').append(question.toString()).append('\n');
        }
    }
    if (authoritySection.size() != 0) {
        sb.append("\n;; AUTHORITY SECTION:\n");
        for (Record<? extends Data> record : authoritySection) {
            sb.append(record.toString()).append('\n');
        }
    }
    if (answerSection.size() != 0) {
        sb.append("\n;; ANSWER SECTION:\n");
        for (Record<? extends Data> record : answerSection) {
            sb.append(record.toString()).append('\n');
        }
    }
    if (additionalSection.size() != 0) {
        boolean hasNonOptArr = false;
        for (Record<? extends Data> record : additionalSection) {
            if (record.type != Record.TYPE.OPT) {
                if (!hasNonOptArr) {
                    hasNonOptArr = true;
                    sb.append("\n;; ADDITIONAL SECTION:\n");
                }
                sb.append(record.toString()).append('\n');
            }
        }
    }
    if (receiveTimestamp > 0) {
        sb.append("\n;; WHEN: ").append(new Date(receiveTimestamp).toString());
    }
    terminalOutputCache = sb.toString();
    return terminalOutputCache;
}
Also used : EDNS(org.minidns.edns.EDNS) Date(java.util.Date)

Example 2 with EDNS

use of org.minidns.edns.EDNS in project minidns by MiniDNS.

the class DNSMessageTest method testComDsAndRrsigLookup.

@Test
public void testComDsAndRrsigLookup() throws Exception {
    DNSMessage m = getMessageFromResource("com-ds-rrsig");
    assertFalse(m.authoritativeAnswer);
    assertTrue(m.recursionDesired);
    assertTrue(m.recursionAvailable);
    List<Record<? extends Data>> answers = m.answerSection;
    assertEquals(2, answers.size());
    assertEquals(TYPE.DS, answers.get(0).type);
    assertEquals(TYPE.DS, answers.get(0).payloadData.getType());
    DS ds = (DS) answers.get(0).payloadData;
    assertEquals(30909, ds.keyTag);
    assertEquals(SignatureAlgorithm.RSASHA256, ds.algorithm);
    assertEquals(DigestAlgorithm.SHA256, ds.digestType);
    assertCsEquals("E2D3C916F6DEEAC73294E8268FB5885044A833FC5459588F4A9184CFC41A5766", ds.getDigestHex());
    assertEquals(TYPE.RRSIG, answers.get(1).type);
    assertEquals(TYPE.RRSIG, answers.get(1).payloadData.getType());
    RRSIG rrsig = (RRSIG) answers.get(1).payloadData;
    assertEquals(TYPE.DS, rrsig.typeCovered);
    assertEquals(SignatureAlgorithm.RSASHA256, rrsig.algorithm);
    assertEquals(1, rrsig.labels);
    assertEquals(86400, rrsig.originalTtl);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    assertCsEquals("20150629170000", dateFormat.format(rrsig.signatureExpiration));
    assertCsEquals("20150619160000", dateFormat.format(rrsig.signatureInception));
    assertEquals(48613, rrsig.keyTag);
    assertCsEquals(".", rrsig.signerName);
    assertEquals(128, rrsig.signature.length);
    List<Record<? extends Data>> arr = m.additionalSection;
    assertEquals(1, arr.size());
    assertEquals(TYPE.OPT, arr.get(0).getPayload().getType());
    Record<? extends Data> opt = arr.get(0);
    EDNS edns = EDNS.fromRecord(opt);
    assertEquals(512, edns.udpPayloadSize);
    assertEquals(0, edns.version);
    assertTrue(edns.dnssecOk);
}
Also used : EDNS(org.minidns.edns.EDNS) Record(org.minidns.record.Record) Data(org.minidns.record.Data) RRSIG(org.minidns.record.RRSIG) SimpleDateFormat(java.text.SimpleDateFormat) DNSMessage(org.minidns.dnsmessage.DNSMessage) DS(org.minidns.record.DS) Test(org.junit.Test)

Example 3 with EDNS

use of org.minidns.edns.EDNS in project minidns by MiniDNS.

the class DNSMessageTest method testComNsLookup.

@Test
public void testComNsLookup() throws Exception {
    DNSMessage m = getMessageFromResource("com-ns");
    assertFalse(m.authoritativeAnswer);
    assertFalse(m.authenticData);
    assertTrue(m.recursionDesired);
    assertTrue(m.recursionAvailable);
    assertTrue(m.qr);
    List<Record<? extends Data>> answers = m.answerSection;
    assertEquals(13, answers.size());
    for (Record<? extends Data> answer : answers) {
        assertCsEquals("com", answer.name);
        assertEquals(Record.CLASS.IN, answer.clazz);
        assertEquals(TYPE.NS, answer.type);
        assertEquals(112028, answer.ttl);
        assertTrue(((NS) answer.payloadData).target.ace.endsWith(".gtld-servers.net"));
    }
    List<Record<? extends Data>> arr = m.additionalSection;
    assertEquals(1, arr.size());
    EDNS edns = EDNS.fromRecord(arr.get(0));
    assertEquals(4096, edns.udpPayloadSize);
    assertEquals(0, edns.version);
}
Also used : EDNS(org.minidns.edns.EDNS) Record(org.minidns.record.Record) Data(org.minidns.record.Data) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 4 with EDNS

use of org.minidns.edns.EDNS in project minidns by MiniDNS.

the class DNSMessageTest method testMessageSelfOptRecordReconstructione.

@SuppressWarnings("unchecked")
@Test
public void testMessageSelfOptRecordReconstructione() throws Exception {
    DNSMessage.Builder m = DNSMessage.builder();
    m.addAdditionalResourceRecord(record("www.example.com", a("127.0.0.1")));
    m.getEdnsBuilder().setUdpPayloadSize(512).setDnssecOk();
    DNSMessage message = new DNSMessage(m.build().toArray());
    assertEquals(2, message.additionalSection.size());
    assertCsEquals("www.example.com", message.additionalSection.get(0).name);
    assertEquals(TYPE.A, message.additionalSection.get(0).type);
    assertCsEquals("127.0.0.1", message.additionalSection.get(0).payloadData.toString());
    assertCsEquals("EDNS: version: 0, flags: do; udp: 512", new EDNS((Record<OPT>) message.additionalSection.get(1)).toString());
}
Also used : OPT(org.minidns.record.OPT) EDNS(org.minidns.edns.EDNS) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Example 5 with EDNS

use of org.minidns.edns.EDNS in project minidns by MiniDNS.

the class DNSMessageTest method testRootDnskeyLookup.

@Test
public void testRootDnskeyLookup() throws Exception {
    DNSMessage m = getMessageFromResource("root-dnskey");
    assertFalse(m.authoritativeAnswer);
    assertTrue(m.recursionDesired);
    assertTrue(m.recursionAvailable);
    List<Record<? extends Data>> answers = m.answerSection;
    assertEquals(3, answers.size());
    for (int i = 0; i < answers.size(); i++) {
        Record<? extends Data> answer = answers.get(i);
        assertCsEquals(".", answer.name);
        assertEquals(19593, answer.getTtl());
        assertEquals(TYPE.DNSKEY, answer.type);
        assertEquals(TYPE.DNSKEY, answer.getPayload().getType());
        DNSKEY dnskey = (DNSKEY) answer.getPayload();
        assertEquals(3, dnskey.protocol);
        assertEquals(SignatureAlgorithm.RSASHA256, dnskey.algorithm);
        assertTrue((dnskey.flags & DNSKEY.FLAG_ZONE) > 0);
        assertEquals(dnskey.getKeyTag(), dnskey.getKeyTag());
        switch(i) {
            case 0:
                assertTrue((dnskey.flags & DNSKEY.FLAG_SECURE_ENTRY_POINT) > 0);
                assertEquals(260, dnskey.getKeyLength());
                assertEquals(19036, dnskey.getKeyTag());
                break;
            case 1:
                assertEquals(DNSKEY.FLAG_ZONE, dnskey.flags);
                assertEquals(132, dnskey.getKeyLength());
                assertEquals(48613, dnskey.getKeyTag());
                break;
            case 2:
                assertEquals(DNSKEY.FLAG_ZONE, dnskey.flags);
                assertEquals(132, dnskey.getKeyLength());
                assertEquals(1518, dnskey.getKeyTag());
                break;
        }
    }
    List<Record<? extends Data>> arr = m.additionalSection;
    assertEquals(1, arr.size());
    Record<? extends Data> opt = arr.get(0);
    EDNS edns = EDNS.fromRecord(opt);
    assertEquals(512, edns.udpPayloadSize);
    assertEquals(0, edns.version);
}
Also used : EDNS(org.minidns.edns.EDNS) Record(org.minidns.record.Record) Data(org.minidns.record.Data) DNSKEY(org.minidns.record.DNSKEY) DNSMessage(org.minidns.dnsmessage.DNSMessage) Test(org.junit.Test)

Aggregations

EDNS (org.minidns.edns.EDNS)7 Test (org.junit.Test)4 DNSMessage (org.minidns.dnsmessage.DNSMessage)4 Data (org.minidns.record.Data)3 Record (org.minidns.record.Record)3 OPT (org.minidns.record.OPT)2 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 DNSKEY (org.minidns.record.DNSKEY)1 DS (org.minidns.record.DS)1 RRSIG (org.minidns.record.RRSIG)1