@SuppressWarnings("unchecked") private static void addRRset( Name name, final Message response, Record[] records, final int section) { Multimap<RequestType, Record> rrsets = ArrayListMultimap.create(); for (Record r : records) { RequestType type = RequestType.typeOf(r.getType()); rrsets.get(type).addAll(Collections2.filter(Arrays.asList(records), type)); } Predicate<Record> checkNewRecord = new Predicate<Record>() { @Override public boolean apply(Record input) { for (int s = 1; s <= section; s++) { if (response.findRecord(input, s)) { return false; } } return true; } }; if (rrsets.containsKey(RequestType.CNAME)) { for (Record cnames : Iterables.filter(rrsets.removeAll(RequestType.CNAME), checkNewRecord)) { response.addRecord(cnames, section); } } for (Record sectionRecord : Iterables.filter(rrsets.values(), checkNewRecord)) { response.addRecord(sectionRecord, section); } }
private static SetResponse lookupRecords( final Message response, final Record query, final InetAddress source) { final Name name = query.getName(); final int type = query.getType(); response.getHeader().setFlag(Flags.RA); // always mark the response w/ the recursion available // bit LOG.debug("DnsResolver: " + RequestType.typeOf(type) + " " + name); for (final DnsResolver r : DnsResolvers.resolversFor(query, source)) { try { final DnsResponse reply = r.lookupRecords(query); if (reply == null) { LOG.debug("DnsResolver: returned null " + name + " using " + r); continue; } if (reply.isAuthoritative()) { // mark response.getHeader().setFlag(Flags.AA); } if (reply.isNxdomain()) { addRRset(name, response, new Record[] {DomainNameRecords.sourceOfAuthority(name)}, type); response.getHeader().setRcode(Rcode.NXDOMAIN); return SetResponse.ofType(SetResponse.NXDOMAIN); } else if (reply.hasAnswer()) { for (ResponseSection s : ResponseSection.values()) { Record[] records = reply.section(s); if (records != null) { addRRset(name, response, records, s.section()); } } return SetResponse.ofType(SetResponse.SUCCESSFUL); } } catch (final Exception ex) { LOG.debug( "DnsResolver: failed for " + name + " using " + r + " because of: " + ex.getMessage(), ex); } } return SetResponse.ofType(SetResponse.UNKNOWN); // no dice, return unknown }