public static SetResponse findRecords( final Message response, final Record queryRecord, final InetAddress source) { final Name name = queryRecord.getName(); final int type = queryRecord.getType(); try { if (!enabled || !Bootstrap.isOperational()) { return SetResponse.ofType(SetResponse.UNKNOWN); } else { final Iterable<DnsResolver> resolverList = DnsResolvers.resolversFor(queryRecord, source); LOG.debug("DnsResolvers.findRecords(): resolvers for " + name + " are: " + resolverList); if (Iterables.isEmpty(resolverList)) { return SetResponse.ofType(SetResponse.UNKNOWN); } else { return DnsResolvers.lookupRecords(response, queryRecord, source); } } } catch (final Exception ex) { LOG.error(ex); LOG.trace(ex, ex); } return SetResponse.ofType(SetResponse.UNKNOWN); }
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 }
public static ResponseType lookup(SetResponse sr) { if (sr.isCNAME()) { return CNAME; } else if (sr.isDelegation()) { return DELEGATION; } else if (sr.isDNAME()) { return DNAME; } else if (sr.isNXDOMAIN()) { return NXDOMAIN; } else if (sr.isNXRRSET()) { return NXRRSET; } else if (sr.isSuccessful()) { return SUCCESSFUL; } else { return UNKNOWN; } }
/* (non-Javadoc) * @see com.eucalyptus.dns.Zone#findRecords(org.xbill.DNS.Name, int) */ @Override public SetResponse findRecords(Name name, int type) { if (type == Type.AAAA) return (SetResponse.ofType(SetResponse.SUCCESSFUL)); if (StackConfiguration.USE_INSTANCE_DNS && name.toString().matches("euca-.+{3}-.+{3}-.+{3}-.+{3}\\..*")) { try { String[] tryIp = name.toString() .replaceAll("euca-", "") .replaceAll(VmInstances.INSTANCE_SUBDOMAIN + ".*", "") .split("-"); if (tryIp.length < 4) return super.findRecords(name, type); String ipCandidate = new StringBuffer() .append(tryIp[0]) .append(".") .append(tryIp[1]) .append(".") .append(tryIp[2]) .append(".") .append(tryIp[3]) .toString(); try { VmInstances.lookupByPublicIp(ipCandidate); } catch (Exception e) { try { VmInstances.lookupByPrivateIp(ipCandidate); } catch (Exception e1) { return super.findRecords(name, type); } } InetAddress ip = InetAddress.getByName(ipCandidate); SetResponse resp = new SetResponse(SetResponse.SUCCESSFUL); resp.addRRset(new RRset(new ARecord(name, 1, ttl, ip))); return resp; } catch (Exception e) { return super.findRecords(name, type); } } else if (StackConfiguration.USE_INSTANCE_DNS && name.toString().endsWith(".in-addr.arpa.")) { int index = name.toString().indexOf(".in-addr.arpa."); Name target; if (index > 0) { String ipString = name.toString().substring(0, index); String[] parts = ipString.split("\\."); String ipCandidate; if (parts.length == 4) { ipCandidate = new StringBuffer() .append(parts[3]) .append(".") .append(parts[2]) .append(".") .append(parts[1]) .append(".") .append(parts[0]) .toString(); } else { return super.findRecords(name, type); } try { VmInstance instance = VmInstances.lookupByPublicIp(ipCandidate); target = new Name(instance.getPublicDnsName() + "."); } catch (Exception e) { try { VmInstance instance = VmInstances.lookupByPrivateIp(ipCandidate); target = new Name(instance.getPrivateDnsName() + "."); } catch (Exception e1) { return super.findRecords(name, type); } } SetResponse resp = new SetResponse(SetResponse.SUCCESSFUL); resp.addRRset(new RRset(new PTRRecord(name, DClass.IN, ttl, target))); return resp; } else { return super.findRecords(name, type); } } else { return super.findRecords(name, type); } }
@Override public DnsResponse lookupRecords(final Record query) { final Name name = query.getName(); final int type = query.getType(); final Cache cache = new Cache(); Lookup aLookup = new Lookup(name, type); aLookup.setCache(cache); Record[] found = aLookup.run(); List<Record> queriedrrs = Arrays.asList(found != null ? found : new Record[] {}); List<Name> cnames = (List<Name>) (aLookup.getAliases().length > 0 ? Arrays.asList(aLookup.getAliases()) : Lists.newArrayList()); List<Record> answer = Lists.newArrayList(); List<Record> authority = Lists.newArrayList(); List<Record> additional = Lists.newArrayList(); for (Name cnameRec : cnames) { SetResponse sr = cache.lookupRecords(cnameRec, Type.CNAME, Credibility.ANY); if (sr != null && sr.isSuccessful() && sr.answers() != null) { for (RRset result : sr.answers()) { Iterator rrs = result.rrs(false); if (rrs != null) { for (Object record : ImmutableSet.copyOf(rrs)) { answer.add((Record) record); } } } } } for (Record queriedRec : queriedrrs) { SetResponse sr = cache.lookupRecords(queriedRec.getName(), queriedRec.getType(), Credibility.ANY); if (sr != null && sr.isSuccessful() && sr.answers() != null) { for (RRset result : sr.answers()) { Iterator rrs = result.rrs(false); if (rrs != null) { for (Object record : ImmutableSet.copyOf(rrs)) { answer.add((Record) record); } } } } } for (Record aRec : queriedrrs) { List<Record> nsRecs = lookupNSRecords(aRec.getName(), cache); for (Record nsRec : nsRecs) { authority.add(nsRec); Lookup nsLookup = new Lookup(((NSRecord) nsRec).getTarget(), Type.A); nsLookup.setCache(cache); Record[] nsAnswers = nsLookup.run(); if (nsAnswers != null) { additional.addAll(Arrays.asList(nsAnswers)); } } } return DnsResponse.forName(query.getName()) .recursive() .withAuthority(authority) .withAdditional(additional) .answer(answer); }