Example #1
0
  /* Returns true if the message could be rendered. */
  private boolean toWire(DNSOutput out, int maxLength) {
    if (maxLength < Header.LENGTH) return false;

    Header newheader = null;

    int tempMaxLength = maxLength;
    if (tsigkey != null) tempMaxLength -= tsigkey.recordLength();

    OPTRecord opt = getOPT();
    byte[] optBytes = null;
    if (opt != null) {
      optBytes = opt.toWire(Section.ADDITIONAL);
      tempMaxLength -= optBytes.length;
    }

    int startpos = out.current();
    header.toWire(out);
    Compression c = new Compression();
    int flags = header.getFlagsByte();
    int additionalCount = 0;
    for (int i = 0; i < 4; i++) {
      int skipped;
      if (sections[i] == null) continue;
      skipped = sectionToWire(out, i, c, tempMaxLength);
      if (skipped != 0 && i != Section.ADDITIONAL) {
        flags = Header.setFlag(flags, Flags.TC, true);
        out.writeU16At(header.getCount(i) - skipped, startpos + 4 + 2 * i);
        for (int j = i + 1; j < Section.ADDITIONAL; j++) out.writeU16At(0, startpos + 4 + 2 * j);
        break;
      }
      if (i == Section.ADDITIONAL) additionalCount = header.getCount(i) - skipped;
    }

    if (optBytes != null) {
      out.writeByteArray(optBytes);
      additionalCount++;
    }

    if (flags != header.getFlagsByte()) out.writeU16At(flags, startpos + 2);

    if (additionalCount != header.getCount(Section.ADDITIONAL))
      out.writeU16At(additionalCount, startpos + 10);

    if (tsigkey != null) {
      TSIGRecord tsigrec = tsigkey.generate(this, out.toByteArray(), tsigerror, querytsig);

      tsigrec.toWire(out, Section.ADDITIONAL, c);
      out.writeU16At(additionalCount + 1, startpos + 10);
    }

    return true;
  }
Example #2
0
 Message(DNSInput in) throws IOException {
   this(new Header(in));
   boolean isUpdate = (header.getOpcode() == Opcode.UPDATE);
   boolean truncated = header.getFlag(Flags.TC);
   try {
     for (int i = 0; i < 4; i++) {
       int count = header.getCount(i);
       if (count > 0) sections[i] = new ArrayList(count);
       for (int j = 0; j < count; j++) {
         int pos = in.current();
         Record rec = Record.fromWire(in, i, isUpdate);
         sections[i].add(rec);
         if (i == Section.ADDITIONAL) {
           if (rec.getType() == Type.TSIG) tsigstart = pos;
           if (rec.getType() == Type.SIG) {
             SIGRecord sig = (SIGRecord) rec;
             if (sig.getTypeCovered() == 0) sig0start = pos;
           }
         }
       }
     }
   } catch (WireParseException e) {
     if (!truncated) throw e;
   }
   size = in.current();
 }
Example #3
0
 /**
  * Returns the TSIG record from the ADDITIONAL section, if one is present.
  *
  * @see TSIGRecord
  * @see TSIG
  * @see Section
  */
 public TSIGRecord getTSIG() {
   int count = header.getCount(Section.ADDITIONAL);
   if (count == 0) return null;
   List l = sections[Section.ADDITIONAL];
   Record rec = (Record) l.get(count - 1);
   if (rec.type != Type.TSIG) return null;
   return (TSIGRecord) rec;
 }
Example #4
0
  /* Returns true if the message could be rendered. */
  private boolean toWire(DNSOutput out, int maxLength) {
    if (maxLength < Header.LENGTH) return false;

    Header newheader = null;

    int tempMaxLength = maxLength;
    if (tsigkey != null) tempMaxLength -= tsigkey.recordLength();

    int startpos = out.current();
    header.toWire(out);
    Compression c = new Compression();
    for (int i = 0; i < 4; i++) {
      int skipped;
      if (sections[i] == null) continue;
      skipped = sectionToWire(out, i, c, tempMaxLength);
      if (skipped != 0) {
        if (i != Section.ADDITIONAL) {
          if (newheader == null) newheader = (Header) header.clone();
          newheader.setFlag(Flags.TC);
          int count = newheader.getCount(i);
          newheader.setCount(i, count - skipped);
          for (int j = i + 1; j < 4; j++) newheader.setCount(j, 0);

          out.save();
          out.jump(startpos);
          newheader.toWire(out);
          out.restore();
        }
        break;
      }
    }

    if (tsigkey != null) {
      TSIGRecord tsigrec = tsigkey.generate(this, out.toByteArray(), tsigerror, querytsig);

      if (newheader == null) newheader = (Header) header.clone();
      tsigrec.toWire(out, Section.ADDITIONAL, c);
      newheader.incCount(Section.ADDITIONAL);

      out.save();
      out.jump(startpos);
      newheader.toWire(out);
      out.restore();
    }

    return true;
  }