private List<ScoredHost> scoreMatches(final List<String> results) { final ImmutableList.Builder<ScoredHost> scored = ImmutableList.builder(); for (final String name : results) { int score = Integer.MAX_VALUE; for (int i = 0; i < searchPath.length; i++) { if (name.endsWith(searchPath[i].toString())) { if (i < score) { score = i; } } } scored.add(new ScoredHost(name, score)); } return scored.build(); } }
public static String shortHostname(final String host) { final Name root = Name.fromConstantString("."); final Name hostname; try { hostname = Name.fromString(host, root); } catch (TextParseException e) { throw new IllegalArgumentException("Invalid hostname '" + host + "'"); } final ResolverConfig currentConfig = ResolverConfig.getCurrentConfig(); if (currentConfig != null) { final Name[] searchPath = currentConfig.searchPath(); if (searchPath != null) { for (final Name domain : searchPath) { if (hostname.subdomain(domain)) { return hostname.relativize(domain).toString(); } } } } return hostname.toString(); }
@Test public void testContainerRegistrationPersistanceAbsent() throws Exception { ServiceRecord record = marshal.fromBytes("somepath", CONTAINER_RECORD_YARN_PERSISTANCE_ABSENT.getBytes()); registryDNS.register( "/registry/users/root/services/org-apache-slider/test1/components/" + "ctr-e50-1451931954322-0016-01-000003", record); Name name = Name.fromString("ctr-e50-1451931954322-0016-01-000002.dev.test."); Record question = Record.newRecord(name, Type.A, DClass.IN); Message query = Message.newQuery(question); byte[] responseBytes = registryDNS.generateReply(query, null); Message response = new Message(responseBytes); assertEquals("Excepting NXDOMAIN as Record must not have regsisterd wrong", Rcode.NXDOMAIN, response.getRcode()); }
/** * Create a query to forward to the primary DNS server (if configured). * NOTE: Experimental * * @param query the inbound query. * @return the query to forward to the primary server. * @throws NameTooLongException * @throws TextParseException if query creation fails. */ private Message createPrimaryQuery(Message query) throws NameTooLongException, TextParseException { Name name = query.getQuestion().getName(); if (name.labels() > 0 && name.labels() <= 2) { // short relative or absolute name. this code may not be necessary - // OS resolution utilities probably append the search paths defined // in resolv.conf prior to the lookup int id = query.getHeader().getID(); String queryName = name.getLabelString(0); Name qualifiedName = Name.concatenate(Name.fromString(queryName), Name.fromString(domainName)); LOG.info("Received query {}. Forwarding query {}", name, qualifiedName); Record question = Record.newRecord(qualifiedName, query.getQuestion().getType(), query.getQuestion().getDClass()); query = Message.newQuery(question); query.getHeader().setID(id); } return query; }
Record[] assertDNSQueryNotNull(String lookup, int type, int answerCount) throws IOException { Name name = Name.fromString(lookup); Record question = Record.newRecord(name, type, DClass.IN); Message query = Message.newQuery(question); OPTRecord optRecord = new OPTRecord(4096, 0, 0, Flags.DO, null); query.addRecord(optRecord, Section.ADDITIONAL); byte[] responseBytes = getRegistryDNS().generateReply(query, null); Message response = new Message(responseBytes); assertEquals("not successful", Rcode.NOERROR, response.getRcode()); assertNotNull("Null response", response); assertEquals("Questions do not match", query.getQuestion(), response.getQuestion()); Record[] recs = response.getSectionArray(Section.ANSWER); assertEquals(answerCount, recs.length); assertEquals(recs[0].getType(), type); return recs; }
@Test public void testBlockedDnsQuery() throws Exception { Message message = Message.newQuery(new ARecord(new Name("blocked.example.com."), 0x01, 3600, .payloadBuilder( new UnknownPacket.Builder() .rawData(message.toWire()) ); assertTrue(mockEventLoop.lastResponse.getPayload() instanceof UdpPacket); Message responseMsg = new Message(mockEventLoop.lastResponse.getPayload().getPayload().getRawData()); assertEquals(NOERROR, responseMsg.getHeader().getRcode()); assertArrayEquals(new Record[] {}, responseMsg.getSectionArray(Section.ANSWER)); assertNotEquals(0, responseMsg.getSectionArray(Section.AUTHORITY).length); assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0] instanceof SOARecord); assertTrue(responseMsg.getSectionArray(Section.AUTHORITY)[0].getTTL() > 0);
Message dnsMsg; try { dnsMsg = new Message(dnsRawData); } catch (IOException e) { Log.i(TAG, "handleDnsRequest: Discarding non-DNS or invalid packet", e); return; if (dnsMsg.getQuestion() == null) { Log.i(TAG, "handleDnsRequest: Discarding DNS packet with no query " + dnsMsg); return; String dnsQueryName = dnsMsg.getQuestion().getName().toString(true); if (!ruleDatabase.isBlocked(dnsQueryName.toLowerCase(Locale.ENGLISH))) { Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " Allowed, sending to " + destAddr);
public DBRecord(Record record, Name origin, long zoneTTL) { this.name = record.getName().relativize(origin).toString(); this.type = Type.string(record.getType()); this.dclass = DClass.string(record.getDClass()); this.content = record.rdataToString(); if(record.getTTL() == zoneTTL){ this.ttl = null; }else{ this.ttl = record.getTTL(); } }
private final void maybeAddRecord(Record record) throws IOException { int rtype = record.getType(); Name name = record.getName(); if (rtype == Type.SOA && !name.equals(origin)) { throw new IOException("SOA owner " + name + " does not match zone origin " + origin); } if (name.subdomain(origin)) addRecord(record); }
private Message makeQuery(DNSRequest request, int id) throws TextParseException { Name name = Name.fromString(request.getHostname(), Name.root); int type; switch (request.getRecordType()) { case DNSRequest.A: type = Type.A; break; case DNSRequest.AAAA: type = Type.AAAA; break; case DNSRequest.MX: type = Type.MX; break; case DNSRequest.PTR: type = Type.PTR; break; case DNSRequest.SPF: type = Type.SPF; break; case DNSRequest.TXT: type = Type.TXT; break; default: throw new UnsupportedOperationException("Unknown query type: "+request.getRecordType()); } Record question = Record.newRecord(name, type, DClass.ANY); Message query = Message.newQuery(question); query.getHeader().setID(id); return query; } }
/** * Asynchronously sends a message to a single server, registering a listener * to receive a callback on success or exception. Multiple asynchronous * lookups can be performed in parallel. Since the callback may be invoked * before the function returns, external synchronization is necessary. * @param query The query to send * @param listener The object containing the callbacks. * @return An identifier, which is also a parameter in the callback */ public Object sendAsync(final Message query, final ResolverListener listener) { final Object id; synchronized (this) { id = new Integer(uniqueID++); } Record question = query.getQuestion(); String qname; if (question != null) qname = question.getName().toString(); else qname = "(none)"; String name = this.getClass() + ": " + qname; Thread thread = new ResolveThread(this, query, id, listener); thread.setName(name); thread.setDaemon(true); thread.start(); return id; }
void addRRset(Name name, Message response, RRset rrset, int section, int flags) { for (int s = 1; s <= section; s++) if (response.findRRset(name, rrset.getType(), s)) return; if ((flags & FLAG_SIGONLY) == 0) { Iterator it = rrset.rrs(); while (it.hasNext()) { Record r = (Record) it.next(); if (r.getName().isWild() && !name.isWild()) r = r.withName(name); response.addRecord(r, section); } } if ((flags & (FLAG_SIGONLY | FLAG_DNSSECOK)) != 0) { Iterator it = rrset.sigs(); while (it.hasNext()) { Record r = (Record) it.next(); if (r.getName().isWild() && !name.isWild()) r = r.withName(name); response.addRecord(r, section); } } }
/** * Create a Lookup object that will find records of type A at the given name * in the IN class. * @param name The name of the desired records * @throws TextParseException The name is not a valid DNS name * @see #Lookup(Name,int,int) */ public Lookup(String name) throws TextParseException { this(Name.fromString(name), Type.A, DClass.IN); }
private static boolean sameSet(Record r1, Record r2) { return (r1.getRRsetType() == r2.getRRsetType() && r1.getDClass() == r2.getDClass() && r1.getName().equals(r2.getName())); }
public static void main(String[] args) throws TextParseException, IOException { File zoneFile = new File("zones/unlogic.se"); Master master = new Master(zoneFile.getPath(),Name.fromString(zoneFile.getName(), Name.root)); Record record = master._nextRecord(); Logger LOG = Logger.getLogger(ZoneDissasembler.class); while(record != null){ LOG.info("Class: " + record.getClass()); LOG.info("Name: " + record.getName()); LOG.info("toString: " + record.toString()); record = master._nextRecord(); } }
public Zone findBestZone(Name name) { Zone foundzone = null; foundzone = (Zone) znames.get(name); if (foundzone != null) return foundzone; int labels = name.labels(); for (int i = 1; i < labels; i++) { Name tname = new Name(name, i); foundzone = (Zone) znames.get(tname); if (foundzone != null) return foundzone; } return null; }