Search in sources :

Example 1 with NameTooLongException

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

the class DNSServer method addAnswer.

byte addAnswer(final Message response, final Name name, int type, int dclass, int iterations, int flags) {
    SetResponse sr;
    byte rcode = Rcode.NOERROR;
    if (iterations > 6)
        return Rcode.NOERROR;
    if (type == Type.SIG || type == Type.RRSIG) {
        type = Type.ANY;
        flags |= FLAG_SIGONLY;
    }
    final Zone zone = findBestZone(name);
    if (zone != null)
        sr = zone.findRecords(name, type);
    else {
        sr = getCache(dclass).lookupRecords(name, type, Credibility.NORMAL);
    }
    if (sr.isUnknown()) {
        addCacheNS(response, getCache(dclass), name);
    }
    if (sr.isNXDOMAIN()) {
        response.getHeader().setRcode(Rcode.NXDOMAIN);
        if (zone != null) {
            addSOA(response, zone);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        }
        rcode = Rcode.NXDOMAIN;
    } else if (sr.isNXRRSET()) {
        if (zone != null) {
            addSOA(response, zone);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        }
    } else if (sr.isDelegation()) {
        final RRset nsRecords = sr.getNS();
        addRRset(nsRecords.getName(), response, nsRecords, Section.AUTHORITY, flags);
    } else if (sr.isCNAME()) {
        final CNAMERecord cname = sr.getCNAME();
        addRRset(name, response, new RRset(cname), Section.ANSWER, flags);
        if (zone != null && iterations == 0)
            response.getHeader().setFlag(Flags.AA);
        rcode = addAnswer(response, cname.getTarget(), type, dclass, iterations + 1, flags);
    } else if (sr.isDNAME()) {
        final DNAMERecord dname = sr.getDNAME();
        RRset rrset = new RRset(dname);
        addRRset(name, response, rrset, Section.ANSWER, flags);
        final Name newname;
        try {
            newname = name.fromDNAME(dname);
        } catch (final NameTooLongException e) {
            return Rcode.YXDOMAIN;
        }
        rrset = new RRset(new CNAMERecord(name, dclass, 0, newname));
        addRRset(name, response, rrset, Section.ANSWER, flags);
        if (zone != null && iterations == 0)
            response.getHeader().setFlag(Flags.AA);
        rcode = addAnswer(response, newname, type, dclass, iterations + 1, flags);
    } else if (sr.isSuccessful()) {
        final RRset[] rrsets = sr.answers();
        for (int i = 0; i < rrsets.length; i++) addRRset(name, response, rrsets[i], Section.ANSWER, flags);
        if (zone != null) {
            addNS(response, zone, flags);
            if (iterations == 0)
                response.getHeader().setFlag(Flags.AA);
        } else
            addCacheNS(response, getCache(dclass), name);
    }
    return rcode;
}
Also used : SetResponse(org.xbill.DNS.SetResponse) CNAMERecord(org.xbill.DNS.CNAMERecord) NameTooLongException(org.xbill.DNS.NameTooLongException) DNAMERecord(org.xbill.DNS.DNAMERecord) Zone(org.xbill.DNS.Zone) RRset(org.xbill.DNS.RRset) Name(org.xbill.DNS.Name)

Aggregations

CNAMERecord (org.xbill.DNS.CNAMERecord)1 DNAMERecord (org.xbill.DNS.DNAMERecord)1 Name (org.xbill.DNS.Name)1 NameTooLongException (org.xbill.DNS.NameTooLongException)1 RRset (org.xbill.DNS.RRset)1 SetResponse (org.xbill.DNS.SetResponse)1 Zone (org.xbill.DNS.Zone)1