Search in sources :

Example 16 with Message

use of org.xbill.DNS.Message in project opennms by OpenNMS.

the class DNSServer method buildErrorMessage.

byte[] buildErrorMessage(final Header header, final int rcode, final Record question) {
    final Message response = new Message();
    response.setHeader(header);
    for (int i = 0; i < 4; i++) response.removeAllRecords(i);
    if (rcode == Rcode.SERVFAIL)
        response.addRecord(question, Section.QUESTION);
    header.setRcode(rcode);
    return response.toWire();
}
Also used : Message(org.xbill.DNS.Message)

Example 17 with Message

use of org.xbill.DNS.Message in project nhin-d by DirectProject.

the class DNSResponder method processError.

/**
	 * Processes a DNS error condition and creates an appropriate DNS response.
	 * @param request The original DNS request.
	 * @param error The error condition that occured.
	 * @return A response to the DNS request.
	 */
protected Message processError(Message request, DNSError<?> error) {
    Message errorResponse = null;
    try {
        Header respHeader = new Header(request.toWire());
        Message response = new Message();
        response.setHeader(respHeader);
        for (int i = 0; i < 4; i++) response.removeAllRecords(i);
        response.addRecord(request.getQuestion(), Section.QUESTION);
        response.getHeader().setFlag(Flags.QR);
        if (request.getHeader().getFlag(Flags.RD))
            response.getHeader().setFlag(Flags.RD);
        respHeader.setRcode(Integer.parseInt(error.getError().toString()));
        return response;
    } catch (IOException e) {
    }
    return errorResponse;
}
Also used : Message(org.xbill.DNS.Message) Header(org.xbill.DNS.Header) IOException(java.io.IOException)

Example 18 with Message

use of org.xbill.DNS.Message in project nhin-d by DirectProject.

the class DNSCertificateStore_lookupDNSTest method getPKIXAnswer.

protected Answer<Message> getPKIXAnswer(final byte[] certData) {
    final Answer<Message> retVal = new Answer<Message>() {

        public Message answer(InvocationOnMock invocation) throws Throwable {
            final Message request = (Message) invocation.getArguments()[0];
            final Message response = new Message(request.getHeader().getID());
            response.getHeader().setFlag(Flags.QR);
            if (request.getHeader().getFlag(Flags.RD))
                response.getHeader().setFlag(Flags.RD);
            response.addRecord(request.getQuestion(), Section.QUESTION);
            if (certData != null) {
                final CERTRecord rec = new CERTRecord(request.getQuestion().getName(), DClass.IN, 86400L, CERTRecord.PKIX, 0, 5, /*public key alg, RFC 4034*/
                certData);
                response.addRecord(rec, Section.ANSWER);
            } else {
                response.getHeader().setRcode(Rcode.NXDOMAIN);
            }
            // we are authoritative only
            response.getHeader().setFlag(Flags.AA);
            return response;
        }
    };
    return retVal;
}
Also used : Answer(org.mockito.stubbing.Answer) Message(org.xbill.DNS.Message) CERTRecord(org.xbill.DNS.CERTRecord) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 19 with Message

use of org.xbill.DNS.Message in project nhin-d by DirectProject.

the class DNSCertificateStore_lookupDNSTest method getIPKIXAnswer.

protected Answer<Message> getIPKIXAnswer(final String url) {
    final Answer<Message> retVal = new Answer<Message>() {

        public Message answer(InvocationOnMock invocation) throws Throwable {
            final Message request = (Message) invocation.getArguments()[0];
            final Message response = new Message(request.getHeader().getID());
            response.getHeader().setFlag(Flags.QR);
            if (request.getHeader().getFlag(Flags.RD))
                response.getHeader().setFlag(Flags.RD);
            response.addRecord(request.getQuestion(), Section.QUESTION);
            final CERTRecord rec = new CERTRecord(request.getQuestion().getName(), DClass.IN, 86400L, CERTRecord.URI, 0, 5, /*public key alg, RFC 4034*/
            url.getBytes());
            response.addRecord(rec, Section.ANSWER);
            // we are authoritative only
            response.getHeader().setFlag(Flags.AA);
            return response;
        }
    };
    return retVal;
}
Also used : Answer(org.mockito.stubbing.Answer) Message(org.xbill.DNS.Message) CERTRecord(org.xbill.DNS.CERTRecord) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Example 20 with Message

use of org.xbill.DNS.Message in project hadoop-pcap by RIPE-NCC.

the class DnsPcapReader method processPacketPayload.

@Override
protected void processPacketPayload(Packet packet, byte[] payload) {
    String protocol = (String) packet.get(Packet.PROTOCOL);
    if (!PcapReader.PROTOCOL_UDP.equals(protocol) && !PcapReader.PROTOCOL_TCP.equals(protocol))
        return;
    DnsPacket dnsPacket = (DnsPacket) packet;
    if (DNS_PORT == (Integer) packet.get(Packet.SRC_PORT) || DNS_PORT == (Integer) packet.get(Packet.DST_PORT)) {
        if (PROTOCOL_TCP.equals(protocol) && // TODO Support DNS responses with multiple messages (as used for XFRs)
        payload.length > 2)
            // First two bytes denote the size of the DNS message, ignore them
            payload = Arrays.copyOfRange(payload, 2, payload.length);
        try {
            Message msg = new Message(payload);
            Header header = msg.getHeader();
            dnsPacket.put(DnsPacket.QUERYID, header.getID());
            dnsPacket.put(DnsPacket.FLAGS, header.printFlags());
            dnsPacket.put(DnsPacket.QR, header.getFlag(Flags.QR));
            dnsPacket.put(DnsPacket.OPCODE, Opcode.string(header.getOpcode()));
            dnsPacket.put(DnsPacket.RCODE, Rcode.string(header.getRcode()));
            dnsPacket.put(DnsPacket.QUESTION, convertRecordToString(msg.getQuestion()));
            dnsPacket.put(DnsPacket.QNAME, convertRecordOwnerToString(msg.getQuestion()));
            dnsPacket.put(DnsPacket.QTYPE, convertRecordTypeToInt(msg.getQuestion()));
            dnsPacket.put(DnsPacket.ANSWER, convertRecordsToStrings(msg.getSectionArray(Section.ANSWER)));
            dnsPacket.put(DnsPacket.AUTHORITY, convertRecordsToStrings(msg.getSectionArray(Section.AUTHORITY)));
            dnsPacket.put(DnsPacket.ADDITIONAL, convertRecordsToStrings(msg.getSectionArray(Section.ADDITIONAL)));
        } catch (Exception e) {
        // If we cannot decode a DNS packet we ignore it
        }
    }
}
Also used : Message(org.xbill.DNS.Message) Header(org.xbill.DNS.Header) DnsPacket(net.ripe.hadoop.pcap.packet.DnsPacket) IOException(java.io.IOException)

Aggregations

Message (org.xbill.DNS.Message)20 IOException (java.io.IOException)8 Name (org.xbill.DNS.Name)8 Record (org.xbill.DNS.Record)8 Header (org.xbill.DNS.Header)5 ArrayList (java.util.ArrayList)4 RRset (org.xbill.DNS.RRset)4 JSONObject (org.json.JSONObject)3 CERTRecord (org.xbill.DNS.CERTRecord)3 CNAMERecord (org.xbill.DNS.CNAMERecord)3 SimpleResolver (org.xbill.DNS.SimpleResolver)3 InterruptedIOException (java.io.InterruptedIOException)2 Endpoint (org.apache.camel.Endpoint)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Answer (org.mockito.stubbing.Answer)2 ARecord (org.xbill.DNS.ARecord)2 ExtendedResolver (org.xbill.DNS.ExtendedResolver)2 MXRecord (org.xbill.DNS.MXRecord)2 NSRecord (org.xbill.DNS.NSRecord)2 SetResponse (org.xbill.DNS.SetResponse)2