示例#1
0
文件: Master.java 项目: lemmy/dnsjava
  private Record parseRR(MyStringTokenizer st, boolean useLast, Record last, Name origin)
      throws IOException {
    Name name;
    int ttl;
    short type, dclass;

    if (!useLast) name = new Name(st.nextToken(), origin);
    else name = last.getName();

    String s = st.nextToken();

    try {
      ttl = TTL.parseTTL(s);
      s = st.nextToken();
    } catch (NumberFormatException e) {
      if (!useLast || last == null) ttl = defaultTTL;
      else ttl = last.getTTL();
    }

    if ((dclass = DClass.value(s)) > 0) s = st.nextToken();
    else dclass = DClass.IN;

    if ((type = Type.value(s)) < 0) throw new IOException("Parse error");

    return Record.fromString(name, type, dclass, ttl, st, origin);
  }
示例#2
0
  /**
   * Creates a specification for generating records, as a $GENERATE statement in a master file.
   *
   * @param start The start of the range.
   * @param end The end of the range.
   * @param step The step value of the range.
   * @param namePattern The pattern to use for generating record names.
   * @param type The type of the generated records. The supported types are PTR, CNAME, DNAME, A,
   *     AAAA, and NS.
   * @param dclass The class of the generated records.
   * @param ttl The ttl of the generated records.
   * @param rdataPattern The pattern to use for generating record data.
   * @param origin The origin to append to relative names.
   * @throws IllegalArgumentException The range is invalid.
   * @throws IllegalArgumentException The type does not support generation.
   * @throws IllegalArgumentException The dclass is not a valid class.
   */
  public Generator(
      long start,
      long end,
      long step,
      String namePattern,
      int type,
      int dclass,
      long ttl,
      String rdataPattern,
      Name origin) {
    if (start < 0 || end < 0 || start > end || step <= 0)
      throw new IllegalArgumentException("invalid range specification");
    if (!supportedType(type)) throw new IllegalArgumentException("unsupported type");
    DClass.check(dclass);

    this.start = start;
    this.end = end;
    this.step = step;
    this.namePattern = namePattern;
    this.type = type;
    this.dclass = dclass;
    this.ttl = ttl;
    this.rdataPattern = rdataPattern;
    this.origin = origin;
    this.current = start;
  }
  /**
   * Builds a new Record from its textual representation
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @param st A tokenizer containing the textual representation of the rdata.
   * @param origin The default origin to be appended to relative domain names.
   * @return The new record
   * @throws IOException The text format was invalid.
   */
  public static Record fromString(
      Name name, int type, int dclass, long ttl, Tokenizer st, Name origin) throws IOException {
    Record rec;

    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    Tokenizer.Token t = st.get();
    if (t.type == Tokenizer.IDENTIFIER && t.value.equals("\\#")) {
      int length = st.getUInt16();
      byte[] data = st.getHex();
      if (data == null) {
        data = new byte[0];
      }
      if (length != data.length)
        throw st.exception("invalid unknown RR encoding: " + "length mismatch");
      DNSInput in = new DNSInput(data);
      return newRecord(name, type, dclass, ttl, length, in);
    }
    st.unget();
    rec = getEmptyRecord(name, type, dclass, ttl, true);
    rec.rdataFromString(st, origin);
    t = st.get();
    if (t.type != Tokenizer.EOL && t.type != Tokenizer.EOF) {
      throw st.exception("unexpected tokens at end of record");
    }
    return rec;
  }
  /**
   * Creates a new empty record, with the given parameters.
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @return An object of a subclass of Record
   */
  public static Record newRecord(Name name, int type, int dclass, long ttl) {
    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    return getEmptyRecord(name, type, dclass, ttl, false);
  }
 Record(Name name, int type, int dclass, long ttl) {
   if (!name.isAbsolute()) throw new RelativeNameException(name);
   Type.check(type);
   DClass.check(dclass);
   TTL.check(ttl);
   this.name = name;
   this.type = type;
   this.dclass = dclass;
   this.ttl = ttl;
 }
示例#6
0
 /**
  * Converts the generate specification to a string containing the corresponding $GENERATE
  * statement.
  */
 public String toString() {
   StringBuffer sb = new StringBuffer();
   sb.append("$GENERATE ");
   sb.append(start + "-" + end);
   if (step > 1) sb.append("/" + step);
   sb.append(" ");
   sb.append(namePattern + " ");
   sb.append(ttl + " ");
   if (dclass != DClass.IN || !Options.check("noPrintIN")) sb.append(DClass.string(dclass) + " ");
   sb.append(Type.string(type) + " ");
   sb.append(rdataPattern + " ");
   return sb.toString();
 }
  /**
   * Creates a new record, with the given parameters.
   *
   * @param name The owner name of the record.
   * @param type The record's type.
   * @param dclass The record's class.
   * @param ttl The record's time to live.
   * @param length The length of the record's data.
   * @param data The rdata of the record, in uncompressed DNS wire format. Only the first length
   *     bytes are used.
   */
  public static Record newRecord(
      Name name, int type, int dclass, long ttl, int length, byte[] data) {
    if (!name.isAbsolute()) throw new RelativeNameException(name);
    Type.check(type);
    DClass.check(dclass);
    TTL.check(ttl);

    DNSInput in;
    if (data != null) in = new DNSInput(data);
    else in = null;
    try {
      return newRecord(name, type, dclass, ttl, length, in);
    } catch (IOException e) {
      return null;
    }
  }
示例#8
0
  /**
   * Converts the given section of the Message to a String.
   *
   * @see Section
   */
  public String sectionToString(int i) {
    if (i > 3) return null;

    StringBuffer sb = new StringBuffer();

    Record[] records = getSectionArray(i);
    for (int j = 0; j < records.length; j++) {
      Record rec = records[j];
      if (i == Section.QUESTION) {
        sb.append(";;\t" + rec.name);
        sb.append(", type = " + Type.string(rec.type));
        sb.append(", class = " + DClass.string(rec.dclass));
      } else sb.append(rec);
      sb.append("\n");
    }
    return sb.toString();
  }
 /** Converts a Record into a String representation */
 public String toString() {
   StringBuffer sb = new StringBuffer();
   sb.append(name);
   if (sb.length() < 8) sb.append("\t");
   if (sb.length() < 16) sb.append("\t");
   sb.append("\t");
   if (Options.check("BINDTTL")) sb.append(TTL.format(ttl));
   else sb.append(ttl);
   sb.append("\t");
   if (dclass != DClass.IN || !Options.check("noPrintIN")) {
     sb.append(DClass.string(dclass));
     sb.append("\t");
   }
   sb.append(Type.string(type));
   String rdata = rrToString();
   if (!rdata.equals("")) {
     sb.append("\t");
     sb.append(rdata);
   }
   return sb.toString();
 }