protected ARecord getFirstARecord(Record[] rrecordSet) { ARecord arecord = null; if (rrecordSet == null || rrecordSet.length == 0) { if (logger.isLoggable(Level.FINEST)) { logger.finest("rrecordSet is null or zero length: " + rrecordSet); } return arecord; } for (int i = 0; i < rrecordSet.length; i++) { if (rrecordSet[i].getType() != Type.A) { if (logger.isLoggable(Level.FINEST)) { logger.finest("Record " + Integer.toString(i) + " is not A type but " + rrecordSet[i].getType()); } continue; } arecord = (ARecord) rrecordSet[i]; break; } return arecord; } }
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);
/** * 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; }
@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()); }
/** * Converts a raw wire transfer format of a record to a DNS record. * @param data The raw byte stream of a record in wire transfer format. * @return A DNSRecord converted from the wire format. * @throws IOException */ public static DNSRecord fromWire(byte[] data) throws IOException { Record rec = Record.fromWire(data, Section.ANSWER); DNSRecord retVal = new DNSRecord(); retVal.setDclass(rec.getDClass()); retVal.setName(rec.getName().toString()); retVal.setData(rec.rdataToWireCanonical()); retVal.setTtl(rec.getTTL()); retVal.setType(rec.getType()); return retVal; } }
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; }
boolean answersQuery(final Record record) { if (record != null) { for (Message query : browser.queries) { for (Record question : MulticastDNSUtils.extractRecords(query, Section.QUESTION)) { Name questionName = question.getName(); Name recordName = record.getName(); int questionType = question.getType(); int recordType = record.getType(); int questionDClass = question.getDClass(); int recordDClass = record.getDClass(); if (((questionType == Type.ANY) || (questionType == recordType)) && (questionName.equals(recordName) || questionName.subdomain(recordName) || recordName.toString().endsWith("." + questionName.toString())) && ((questionDClass == DClass.ANY) || ((questionDClass & 0x7FFF) == (recordDClass & 0x7FFF)))) { return true; } } } } return false; }
@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);
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(); } }
void sendUpdate() throws IOException { if (query.getHeader().getCount(Section.UPDATE) == 0) { print("Empty update message. Ignoring."); return; if (query.getHeader().getCount(Section.ZONE) == 0) { Name updzone; updzone = zone; int dclass = defaultClass; if (updzone == null) { Record [] recs = query.getSectionArray(Section.UPDATE); for (int i = 0; i < recs.length; i++) { if (updzone == null) updzone = new Name(recs[i].getName(), 1); if (recs[i].getDClass() != DClass.NONE && recs[i].getDClass() != DClass.ANY) dclass = recs[i].getDClass(); break; Record soa = Record.newRecord(updzone, Type.SOA, dclass); query.addRecord(soa, Section.ZONE);
public static String reverseDns(String hostIp) throws IOException { Resolver res = new ExtendedResolver(); // set the timeout, defaults to 200 milliseconds int timeout = ConfigurationManager.getIntProperty("solr-statistics", "resolver.timeout", 200); res.setTimeout(0, timeout); Name name = ReverseMap.fromAddress(hostIp); int type = Type.PTR; int dclass = DClass.IN; Record rec = Record.newRecord(name, type, dclass); Message query = Message.newQuery(rec); Message response = res.send(query); Record[] answers = response.getSectionArray(Section.ANSWER); if (answers.length == 0) { return hostIp; } else { return answers[0].rdataToString(); } } }
res.setTimeout(0, timeout); Name name = Name.fromString(hostname, Name.root); Record rec = Record.newRecord(name, Type.A, DClass.IN); Message query = Message.newQuery(rec); Message response = res.send(query); Record[] answers = response.getSectionArray(Section.ANSWER); if (answers.length == 0) { throw new IOException("Unresolvable host name (empty response)"); if (answer.getType() == Type.A) { resolution = answer.rdataToString(); break;
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; } }
if ("A".equals(Type.string(rec.getType())) || "AAAA".equals(Type.string(rec.getType()))) { LOG.debug("matchingRecord: record is an {} record, continuing...", Type.string(rec.getType())); Matcher m = p.matcher(rec.getName().toString()); LOG.debug("matchingRecord: attempting to match hostname: [{}] with expression: [ {} ]", rec.getName(), expression); if (m.matches()) { rec.getName(), rec.rdataToString(), expression); m = p.matcher(rec.getName().toString() + " " + rec.rdataToString()); if (m.matches()) { matches = true; } else { LOG.debug("matchingRecord: no expression for this zone, returning valid match for this {} record...", Type.string(rec.getType())); matches = true;
public void receiveRecord(final Object id, final Record record) { if (record.getTTL() > 0) { if (record.getType() == Type.PTR) { String value = ((PTRRecord) record).getTarget().toString(); if (!value.endsWith(".")) { value += "."; } // Check if domain is already in the list, add if not, otherwise manipulate booleans. try { domains.add(new Domain(new Name(value))); } catch (TextParseException e) { e.printStackTrace(System.err); } } } } });
private void sendQuery() throws IOException { Record question = Record.newRecord(zname, qtype, dclass); Message query = new Message(); query.getHeader().setOpcode(Opcode.QUERY); query.addRecord(question, Section.QUESTION); if (qtype == Type.IXFR) { Record soa = new SOARecord(zname, dclass, 0, Name.root, Name.root, ixfr_serial, 0, 0, 0, 0); query.addRecord(soa, Section.AUTHORITY); } if (tsig != null) { tsig.apply(query, null); verifier = new TSIG.StreamVerifier(tsig, query.getTSIG()); } byte [] out = query.toWire(Message.MAXLENGTH); client.send(out); }
Name additionalName = record.getAdditionalName(); if (additionalName != null) switch (record.getType()) if (additionalNames.contains(record.getName())) switch (record.getType()) ServiceInstance[] instances = extractServiceInstances(querier.send(Message.newQuery(Record.newRecord(ptr.getTarget(), Type.ANY, dclass)))); if (instances.length > 0)
public static String toString(Record record) { if (record == null) { return null; } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(record.getName()); stringBuilder.append(" "); stringBuilder.append(record.getTTL()); stringBuilder.append(" "); stringBuilder.append(DClass.string(record.getDClass())); stringBuilder.append(" "); stringBuilder.append(Type.string(record.getType())); String rdata = record.rdataToString(); if (!rdata.equals("")) { stringBuilder.append(" "); stringBuilder.append(rdata); } return stringBuilder.toString(); }