/**
  * Creates a new Extended Resolver
  *
  * @param servers An array of server names for which SimpleResolver contexts should be
  *     initialized.
  * @see SimpleResolver
  * @exception UnknownHostException Failure occured initializing SimpleResolvers
  */
 public ExtendedResolver(String[] servers) throws UnknownHostException {
   init();
   for (int i = 0; i < servers.length; i++) {
     Resolver r = new SimpleResolver(servers[i]);
     r.setTimeout(quantum);
     resolvers.add(r);
   }
 }
 /**
  * Creates a new Extended Resolver. The default ResolverConfig is used to determine the servers
  * for which SimpleResolver contexts should be initialized.
  *
  * @see SimpleResolver
  * @see ResolverConfig
  * @exception UnknownHostException Failure occured initializing SimpleResolvers
  */
 public ExtendedResolver() throws UnknownHostException {
   init();
   String[] servers = ResolverConfig.getCurrentConfig().servers();
   if (servers != null) {
     for (int i = 0; i < servers.length; i++) {
       Resolver r = new SimpleResolver(servers[i]);
       r.setTimeout(quantum);
       resolvers.add(r);
     }
   } else resolvers.add(new SimpleResolver());
 }
예제 #3
0
파일: dns.java 프로젝트: lemmy/dnsjava
  public static Record[] getRecords(String namestr, short type, short dclass, byte cred) {
    Message query;
    Message response;
    Record question;
    Record[] answers;
    int answerCount = 0, i = 0;
    Enumeration e;
    Name name = new Name(namestr);

    /*System.out.println("lookup of " + name + " " + Type.string(type));*/
    if (!Type.isRR(type) && type != Type.ANY) return null;

    if (res == null) {
      try {
        eres = new ExtendedResolver();
      } catch (UnknownHostException uhe) {
        System.out.println("Failed to initialize resolver");
        System.exit(-1);
      }
    }
    if (cache == null) cache = new Cache();

    CacheResponse cached = cache.lookupRecords(name, type, dclass, cred);
    /*System.out.println(cached);*/
    if (cached.isSuccessful()) {
      RRset rrset = cached.answer();
      answerCount = rrset.size();
      e = rrset.rrs();
    } else if (cached.isNegative()) {
      answerCount = 0;
      e = null;
    } else {
      question = Record.newRecord(name, type, dclass);
      query = Message.newQuery(question);

      if (res != null) response = res.send(query);
      else response = eres.send(query);

      short rcode = response.getHeader().getRcode();
      if (rcode == Rcode.NOERROR || rcode == Rcode.NXDOMAIN) cache.addMessage(response);

      if (rcode != Rcode.NOERROR) return null;

      e = response.getSection(Section.ANSWER);
      while (e.hasMoreElements()) {
        Record r = (Record) e.nextElement();
        if (matchType(r.getType(), type)) answerCount++;
      }

      e = response.getSection(Section.ANSWER);
    }

    if (answerCount == 0) return null;

    answers = new Record[answerCount];

    while (e.hasMoreElements()) {
      Record r = (Record) e.nextElement();
      if (matchType(r.getType(), type)) answers[i++] = r;
    }

    return answers;
  }