Ejemplo n.º 1
0
  /**
   * Looks up Records in the Zone. This follows CNAMEs and wildcards.
   *
   * @param name The name to look up
   * @param type The type to look up
   * @return A SetResponse object
   * @see SetResponse
   */
  public SetResponse findRecords(Name name, short type) {
    SetResponse zr = null;

    Object o = findSets(name, type);
    if (o == null) {
      /* The name does not exist */
      if (name.isWild()) return SetResponse.ofType(SetResponse.NXDOMAIN);

      int labels = name.labels() - origin.labels();
      if (labels == 0) return SetResponse.ofType(SetResponse.NXDOMAIN);
      if (hasWild) {
        SetResponse sr;
        Name tname = name;
        do {
          sr = findRecords(tname.wild(1), type);
          if (!sr.isNXDOMAIN()) return sr;
          tname = new Name(tname, 1);
        } while (labels-- >= 1);
        return sr;
      } else return SetResponse.ofType(SetResponse.NXDOMAIN);
    }

    if (o instanceof TypeMap) {
      /* The name exists but the type does not. */
      return SetResponse.ofType(SetResponse.NXRRSET);
    }

    Object[] objects;
    RRset rrset;

    if (o instanceof RRset) {
      objects = null;
      rrset = (RRset) o;
    } else {
      objects = (Object[]) o;
      rrset = (RRset) objects[0];
    }

    if (name.equals(rrset.getName())) {
      if (type != Type.CNAME && type != Type.ANY && rrset.getType() == Type.CNAME)
        zr = new SetResponse(SetResponse.CNAME, rrset);
      else if (rrset.getType() == Type.NS && !name.equals(origin))
        zr = new SetResponse(SetResponse.DELEGATION, rrset);
      else {
        zr = new SetResponse(SetResponse.SUCCESSFUL);
        zr.addRRset(rrset);
        if (objects != null) {
          for (int i = 1; i < objects.length; i++) zr.addRRset((RRset) objects[i]);
        }
      }
    } else {
      if (rrset.getType() == Type.CNAME) return SetResponse.ofType(SetResponse.NXDOMAIN);
      else if (rrset.getType() == Type.DNAME) {
        zr = new SetResponse(SetResponse.DNAME, rrset);
      } else if (rrset.getType() == Type.NS) {
        zr = new SetResponse(SetResponse.DELEGATION, rrset);
      }
    }
    return zr;
  }
Ejemplo n.º 2
0
  private synchronized SetResponse lookup(Name name, int type) {
    int labels;
    int olabels;
    int tlabels;
    RRset rrset;
    Name tname;
    Object types;
    SetResponse sr;

    if (!name.subdomain(origin)) return SetResponse.ofType(SetResponse.NXDOMAIN);

    labels = name.labels();
    olabels = origin.labels();

    for (tlabels = olabels; tlabels <= labels; tlabels++) {
      boolean isOrigin = (tlabels == olabels);
      boolean isExact = (tlabels == labels);

      if (isOrigin) tname = origin;
      else if (isExact) tname = name;
      else tname = new Name(name, labels - tlabels);

      types = exactName(tname);
      if (types == null) continue;

      /* If this is a delegation, return that. */
      if (!isOrigin) {
        RRset ns = oneRRset(types, Type.NS);
        if (ns != null) return new SetResponse(SetResponse.DELEGATION, ns);
      }

      /* If this is an ANY lookup, return everything. */
      if (isExact && type == Type.ANY) {
        sr = new SetResponse(SetResponse.SUCCESSFUL);
        RRset[] sets = allRRsets(types);
        for (int i = 0; i < sets.length; i++) sr.addRRset(sets[i]);
        return sr;
      }

      /*
       * If this is the name, look for the actual type or a CNAME.
       * Otherwise, look for a DNAME.
       */
      if (isExact) {
        rrset = oneRRset(types, type);
        if (rrset != null) {
          sr = new SetResponse(SetResponse.SUCCESSFUL);
          sr.addRRset(rrset);
          return sr;
        }
        rrset = oneRRset(types, Type.CNAME);
        if (rrset != null) return new SetResponse(SetResponse.CNAME, rrset);
      } else {
        rrset = oneRRset(types, Type.DNAME);
        if (rrset != null) return new SetResponse(SetResponse.DNAME, rrset);
      }

      /* We found the name, but not the type. */
      if (isExact) return SetResponse.ofType(SetResponse.NXRRSET);
    }

    if (hasWild) {
      for (int i = 0; i < labels - olabels; i++) {
        tname = name.wild(i + 1);

        types = exactName(tname);
        if (types == null) continue;

        rrset = oneRRset(types, type);
        if (rrset != null) {
          sr = new SetResponse(SetResponse.SUCCESSFUL);
          sr.addRRset(rrset);
          return sr;
        }
      }
    }

    return SetResponse.ofType(SetResponse.NXDOMAIN);
  }