Search in sources :

Example 1 with Header

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

the class ConfigServiceDNSStore method get.

/**
	 * {@inheritDoc}
	 */
@SuppressWarnings("unchecked")
@Override
public Message get(Message request) throws DNSException {
    LOGGER.trace("get(Message) Entered");
    /* for testing time out cases
		try
		{
			Thread.sleep(1000000);
		}
		catch (Exception e)
		{

		}
	    */
    if (request == null)
        throw new DNSException(DNSError.newError(Rcode.FORMERR));
    Header header = request.getHeader();
    if (header.getFlag(Flags.QR) || header.getRcode() != Rcode.NOERROR)
        throw new DNSException(DNSError.newError(Rcode.FORMERR));
    if (header.getOpcode() != Opcode.QUERY)
        throw new DNSException(DNSError.newError(Rcode.NOTIMP));
    Record question = request.getQuestion();
    if (question == null || question.getDClass() != DClass.IN) {
        throw new DNSException(DNSError.newError(Rcode.NOTIMP));
    }
    Record queryRecord = request.getQuestion();
    Name name = queryRecord.getName();
    int type = queryRecord.getType();
    if (LOGGER.isDebugEnabled()) {
        StringBuilder builder = new StringBuilder("Recieved Query Request:");
        builder.append("\r\n\tName: " + name.toString());
        builder.append("\r\n\tType: " + type);
        builder.append("\r\n\tDClass: " + queryRecord.getDClass());
        LOGGER.debug(builder.toString());
    }
    Collection<Record> lookupRecords = null;
    switch(question.getType()) {
        case Type.A:
        case Type.MX:
        case Type.SOA:
        case Type.SRV:
        case Type.NS:
        case Type.CNAME:
            {
                try {
                    final RRset set = processGenericRecordRequest(name.toString(), type);
                    if (set != null) {
                        lookupRecords = new ArrayList<Record>();
                        Iterator<Record> iter = set.rrs();
                        while (iter.hasNext()) lookupRecords.add(iter.next());
                    }
                } catch (Exception e) {
                    throw new DNSException(DNSError.newError(Rcode.SERVFAIL), "DNS service proxy call failed: " + e.getMessage(), e);
                }
                break;
            }
        case Type.CERT:
            {
                final RRset set = processCERTRecordRequest(name.toString());
                if (set != null) {
                    lookupRecords = new ArrayList<Record>();
                    Iterator<Record> iter = set.rrs();
                    while (iter.hasNext()) lookupRecords.add(iter.next());
                }
                break;
            }
        case Type.ANY:
            {
                Collection<Record> genRecs = processGenericANYRecordRequest(name.toString());
                RRset certRecs = processCERTRecordRequest(name.toString());
                if (genRecs != null || certRecs != null) {
                    lookupRecords = new ArrayList<Record>();
                    if (genRecs != null)
                        lookupRecords.addAll(genRecs);
                    if (certRecs != null) {
                        Iterator<Record> iter = certRecs.rrs();
                        while (iter.hasNext()) lookupRecords.add(iter.next());
                    }
                }
                break;
            }
        default:
            {
                LOGGER.debug("Query Type " + type + " not implemented");
                throw new DNSException(DNSError.newError(Rcode.NOTIMP), "Query Type " + type + " not implemented");
            }
    }
    if (lookupRecords == null || lookupRecords.size() == 0) {
        LOGGER.debug("No records found.");
        return null;
    }
    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(queryRecord, Section.QUESTION);
    final Iterator<Record> iter = lookupRecords.iterator();
    while (iter.hasNext()) response.addRecord(iter.next(), Section.ANSWER);
    // we are authoritative only
    response.getHeader().setFlag(Flags.AA);
    // look for an SOA record
    final Record soaRecord = checkForSoaRecord(name.toString());
    if (soaRecord != null)
        response.addRecord(soaRecord, Section.AUTHORITY);
    LOGGER.trace("get(Message) Exit");
    return response;
}
Also used : Message(org.xbill.DNS.Message) RRset(org.xbill.DNS.RRset) ArrayList(java.util.ArrayList) CertificateConversionException(org.nhindirect.config.model.exceptions.CertificateConversionException) Name(org.xbill.DNS.Name) Header(org.xbill.DNS.Header) Iterator(java.util.Iterator) Collection(java.util.Collection) CERTRecord(org.xbill.DNS.CERTRecord) Record(org.xbill.DNS.Record) DnsRecord(org.nhind.config.DnsRecord)

Example 2 with Header

use of org.xbill.DNS.Header 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 3 with Header

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

the class DNSServer method doAXFR.

byte[] doAXFR(final Name name, final Message query, final TSIG tsig, TSIGRecord qtsig, final Socket s) {
    final Zone zone = m_znames.get(name);
    boolean first = true;
    if (zone == null)
        return errorMessage(query, Rcode.REFUSED);
    @SuppressWarnings("unchecked") final Iterator<RRset> it = zone.AXFR();
    try {
        final DataOutputStream dataOut = new DataOutputStream(s.getOutputStream());
        int id = query.getHeader().getID();
        while (it.hasNext()) {
            final RRset rrset = it.next();
            final Message response = new Message(id);
            final Header header = response.getHeader();
            header.setFlag(Flags.QR);
            header.setFlag(Flags.AA);
            addRRset(rrset.getName(), response, rrset, Section.ANSWER, FLAG_DNSSECOK);
            if (tsig != null) {
                tsig.applyStream(response, qtsig, first);
                qtsig = response.getTSIG();
            }
            first = false;
            final byte[] out = response.toWire();
            dataOut.writeShort(out.length);
            dataOut.write(out);
        }
    } catch (final IOException ex) {
        LOG.warn("AXFR failed", ex);
    }
    try {
        s.close();
    } catch (final IOException ex) {
        LOG.warn("error closing socket", ex);
    }
    return null;
}
Also used : Message(org.xbill.DNS.Message) Header(org.xbill.DNS.Header) Zone(org.xbill.DNS.Zone) DataOutputStream(java.io.DataOutputStream) RRset(org.xbill.DNS.RRset) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 4 with Header

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

the class DNSServer method generateReply.

/*
     * Note: a null return value means that the caller doesn't need to do
     * anything. Currently this only happens if this is an AXFR request over
     * TCP.
     */
byte[] generateReply(final Message query, final byte[] in, final int length, final Socket s) {
    final Header header = query.getHeader();
    int maxLength;
    int flags = 0;
    if (header.getFlag(Flags.QR))
        return null;
    if (header.getRcode() != Rcode.NOERROR)
        return errorMessage(query, Rcode.FORMERR);
    if (header.getOpcode() != Opcode.QUERY)
        return errorMessage(query, Rcode.NOTIMP);
    final Record queryRecord = query.getQuestion();
    final TSIGRecord queryTSIG = query.getTSIG();
    TSIG tsig = null;
    if (queryTSIG != null) {
        tsig = m_TSIGs.get(queryTSIG.getName());
        if (tsig == null || tsig.verify(query, in, length, null) != Rcode.NOERROR)
            return formerrMessage(in);
    }
    final OPTRecord queryOPT = query.getOPT();
    if (s != null)
        maxLength = 65535;
    else if (queryOPT != null)
        maxLength = Math.max(queryOPT.getPayloadSize(), 512);
    else
        maxLength = 512;
    if (queryOPT != null && (queryOPT.getFlags() & ExtendedFlags.DO) != 0)
        flags = FLAG_DNSSECOK;
    final Message response = new Message(query.getHeader().getID());
    response.getHeader().setFlag(Flags.QR);
    if (query.getHeader().getFlag(Flags.RD)) {
        response.getHeader().setFlag(Flags.RD);
    }
    response.addRecord(queryRecord, Section.QUESTION);
    final Name name = queryRecord.getName();
    final int type = queryRecord.getType();
    final int dclass = queryRecord.getDClass();
    if ((type == Type.AXFR || type == Type.IXFR) && s != null)
        return doAXFR(name, query, tsig, queryTSIG, s);
    if (!Type.isRR(type) && type != Type.ANY)
        return errorMessage(query, Rcode.NOTIMP);
    final byte rcode = addAnswer(response, name, type, dclass, 0, flags);
    if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN)
        return errorMessage(query, rcode);
    addAdditional(response, flags);
    if (queryOPT != null) {
        final int optflags = (flags == FLAG_DNSSECOK) ? ExtendedFlags.DO : 0;
        final OPTRecord opt = new OPTRecord((short) 4096, rcode, (byte) 0, optflags);
        response.addRecord(opt, Section.ADDITIONAL);
    }
    response.setTSIG(tsig, Rcode.NOERROR, queryTSIG);
    return response.toWire(maxLength);
}
Also used : Header(org.xbill.DNS.Header) Message(org.xbill.DNS.Message) OPTRecord(org.xbill.DNS.OPTRecord) CNAMERecord(org.xbill.DNS.CNAMERecord) TSIGRecord(org.xbill.DNS.TSIGRecord) OPTRecord(org.xbill.DNS.OPTRecord) Record(org.xbill.DNS.Record) DNAMERecord(org.xbill.DNS.DNAMERecord) TSIGRecord(org.xbill.DNS.TSIGRecord) TSIG(org.xbill.DNS.TSIG) Name(org.xbill.DNS.Name)

Example 5 with Header

use of org.xbill.DNS.Header 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

Header (org.xbill.DNS.Header)5 Message (org.xbill.DNS.Message)5 IOException (java.io.IOException)3 Name (org.xbill.DNS.Name)2 RRset (org.xbill.DNS.RRset)2 Record (org.xbill.DNS.Record)2 DataOutputStream (java.io.DataOutputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 DnsPacket (net.ripe.hadoop.pcap.packet.DnsPacket)1 DnsRecord (org.nhind.config.DnsRecord)1 CertificateConversionException (org.nhindirect.config.model.exceptions.CertificateConversionException)1 CERTRecord (org.xbill.DNS.CERTRecord)1 CNAMERecord (org.xbill.DNS.CNAMERecord)1 DNAMERecord (org.xbill.DNS.DNAMERecord)1 OPTRecord (org.xbill.DNS.OPTRecord)1 TSIG (org.xbill.DNS.TSIG)1 TSIGRecord (org.xbill.DNS.TSIGRecord)1