Java Code Examples for org.xbill.DNS.Message#addRecord()

The following examples show how to use org.xbill.DNS.Message#addRecord() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SimpleDoTResolver.java    From androdns with Apache License 2.0 6 votes vote down vote up
private Message
sendAXFR(Message query) throws IOException {
    Name qname = query.getQuestion().getName();
    ZoneTransferIn xfrin = ZoneTransferIn.newAXFR(qname, address, tsig);
    xfrin.setTimeout((int)(getTimeout() / 1000));
    xfrin.setLocalAddress(localAddress);
    try {
        xfrin.run();
    }
    catch (ZoneTransferException e) {
        throw new WireParseException(e.getMessage());
    }
    List records = xfrin.getAXFR();
    Message response = new Message(query.getHeader().getID());
    response.getHeader().setFlag(Flags.AA);
    response.getHeader().setFlag(Flags.QR);
    response.addRecord(query.getQuestion(), Section.QUESTION);
    Iterator it = records.iterator();
    while (it.hasNext())
        response.addRecord((Record)it.next(), Section.ANSWER);
    return response;
}
 
Example 2
Source File: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 6 votes vote down vote up
private Message messageWithNodes(String query, String[] names) throws TextParseException {
  Name queryName = Name.fromString(query);
  Record question = Record.newRecord(queryName, Type.SRV, DClass.IN);
  Message queryMessage = Message.newQuery(question);
  Message result = new Message();
  result.setHeader(queryMessage.getHeader());
  result.addRecord(question, Section.QUESTION);

  for (String name1 : names) {
    result.addRecord(
        new SRVRecord(queryName, DClass.IN, 1, 1, 1, 8080, Name.fromString(name1)),
        Section.ANSWER);
  }

  return result;
}
 
Example 3
Source File: XBillDnsSrvResolverTest.java    From dns-java with Apache License 2.0 5 votes vote down vote up
private Message messageWithRCode(String query, int rcode) throws TextParseException {
  Name queryName = Name.fromString(query);
  Record question = Record.newRecord(queryName, Type.SRV, DClass.IN);
  Message queryMessage = Message.newQuery(question);
  Message result = new Message();
  result.setHeader(queryMessage.getHeader());
  result.addRecord(question, Section.QUESTION);

  result.getHeader().setRcode(rcode);

  return result;
}
 
Example 4
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void addCacheNS(Message response, Cache cache, Name name) {
  SetResponse sr = cache.lookupRecords(name, Type.NS, Credibility.HINT);
  if (!sr.isDelegation()) {
    return;
  }
  RRset nsRecords = sr.getNS();
  for (Record r : nsRecords.rrs()) {
    response.addRecord(r, Section.AUTHORITY);
  }
}
 
Example 5
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 5 votes vote down vote up
byte[] buildErrorMessage(Header header, int rcode, Record question) {
  Message response = new Message();
  response.setHeader(header);
  for (int i = 0; i < 4; i++) {
    response.removeAllRecords(i);
  }
  if (rcode == Rcode.SERVFAIL) {
    response.addRecord(question, Section.QUESTION);
  }
  header.setRcode(rcode);
  return response.toWire();
}
 
Example 6
Source File: SimpleDoTResolver.java    From androdns with Apache License 2.0 4 votes vote down vote up
private void
applyEDNS(Message query) {
    if (queryOPT == null || query.getOPT() != null)
        return;
    query.addRecord(queryOPT, Section.ADDITIONAL);
}
 
Example 7
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void addSOA(Message response, Zone zone) {
  response.addRecord(zone.getSOA(), Section.AUTHORITY);
}
 
Example 8
Source File: jnamed.java    From dnsjava with BSD 2-Clause "Simplified" License 4 votes vote down vote up
byte[] generateReply(Message query, byte[] in, Socket s) {
  Header header;
  boolean badversion;
  int maxLength;
  int flags = 0;

  header = query.getHeader();
  if (header.getFlag(Flags.QR)) {
    return null;
  }
  if (header.getRcode() != Rcode.NOERROR) {
    return errorMessage(query, Rcode.FORMERR);
  }
  if (header.getOpcode() != Opcode.QUERY) {
    return errorMessage(query, Rcode.NOTIMP);
  }

  Record queryRecord = query.getQuestion();

  TSIGRecord queryTSIG = query.getTSIG();
  TSIG tsig = null;
  if (queryTSIG != null) {
    tsig = TSIGs.get(queryTSIG.getName());
    if (tsig == null || tsig.verify(query, in, null) != Rcode.NOERROR) {
      return formerrMessage(in);
    }
  }

  OPTRecord queryOPT = query.getOPT();
  if (s != null) {
    maxLength = 65535;
  } else if (queryOPT != null) {
    maxLength = Math.max(queryOPT.getPayloadSize(), 512);
  } else {
    maxLength = 512;
  }

  if (queryOPT != null && (queryOPT.getFlags() & ExtendedFlags.DO) != 0) {
    flags = FLAG_DNSSECOK;
  }

  Message response = new Message(query.getHeader().getID());
  response.getHeader().setFlag(Flags.QR);
  if (query.getHeader().getFlag(Flags.RD)) {
    response.getHeader().setFlag(Flags.RD);
  }
  response.addRecord(queryRecord, Section.QUESTION);

  Name name = queryRecord.getName();
  int type = queryRecord.getType();
  int dclass = queryRecord.getDClass();
  if (type == Type.AXFR && s != null) {
    return doAXFR(name, query, tsig, queryTSIG, s);
  }
  if (!Type.isRR(type) && type != Type.ANY) {
    return errorMessage(query, Rcode.NOTIMP);
  }

  byte rcode = addAnswer(response, name, type, dclass, 0, flags);
  if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) {
    return errorMessage(query, rcode);
  }

  addAdditional(response, flags);

  if (queryOPT != null) {
    int optflags = (flags == FLAG_DNSSECOK) ? ExtendedFlags.DO : 0;
    OPTRecord opt = new OPTRecord((short) 4096, rcode, (byte) 0, optflags);
    response.addRecord(opt, Section.ADDITIONAL);
  }

  response.setTSIG(tsig, Rcode.NOERROR, queryTSIG);
  return response.toWire(maxLength);
}