📄 lookup.java
字号:
// Copyright (c) 2002-2004 Brian Wellington (bwelling@xbill.org)package org.xbill.DNS;import java.util.*;import java.io.*;import java.net.*;/** * The Lookup object issues queries to caching DNS servers. The input consists * of a name, an optional type, and an optional class. Caching is enabled * by default and used when possible to reduce the number of DNS requests. * A Resolver, which defaults to an ExtendedResolver initialized with the * resolvers located by the ResolverConfig class, performs the queries. A * search path of domain suffixes is used to resolve relative names, and is * also determined by the ResolverConfig class. * * A Lookup object may be reused, but should not be used by multiple threads. * * @see Cache * @see Resolver * @see ResolverConfig * * @author Brian Wellington */public final class Lookup {private static Resolver defaultResolver;private static Name [] defaultSearchPath;private static Map defaultCaches;private Resolver resolver;private Name [] searchPath;private Cache cache;private boolean temporary_cache;private int credibility;private Name name;private int type;private int dclass;private boolean verbose;private int iterations;private boolean foundAlias;private boolean done;private boolean doneCurrent;private List aliases;private Record [] answers;private int result;private String error;private boolean nxdomain;private boolean badresponse;private String badresponse_error;private boolean networkerror;private boolean timedout;private boolean nametoolong;private boolean referral;private static final Name [] noAliases = new Name[0];/** The lookup was successful. */public static final int SUCCESSFUL = 0;/** * The lookup failed due to a data or server error. Repeating the lookup * would not be helpful. */public static final int UNRECOVERABLE = 1;/** * The lookup failed due to a network error. Repeating the lookup may be * helpful. */public static final int TRY_AGAIN = 2;/** The host does not exist. */public static final int HOST_NOT_FOUND = 3;/** The host exists, but has no records associated with the queried type. */public static final int TYPE_NOT_FOUND = 4;public static synchronized voidrefreshDefault() { try { defaultResolver = new ExtendedResolver(); } catch (UnknownHostException e) { throw new RuntimeException("Failed to initialize resolver"); } defaultSearchPath = ResolverConfig.getCurrentConfig().searchPath(); defaultCaches = new HashMap();}static { refreshDefault();}/** * Gets the Resolver that will be used as the default by future Lookups. * @return The default resolver. */public static synchronized ResolvergetDefaultResolver() { return defaultResolver;}/** * Sets the default Resolver to be used as the default by future Lookups. * @param resolver The default resolver. */public static synchronized voidsetDefaultResolver(Resolver resolver) { defaultResolver = resolver;}/** * Gets the Cache that will be used as the default for the specified * class by future Lookups. * @param dclass The class whose cache is being retrieved. * @return The default cache for the specified class. */public static synchronized CachegetDefaultCache(int dclass) { DClass.check(dclass); Cache c = (Cache) defaultCaches.get(Mnemonic.toInteger(dclass)); if (c == null) { c = new Cache(dclass); defaultCaches.put(Mnemonic.toInteger(dclass), c); } return c;}/** * Sets the Cache to be used as the default for the specified class by future * Lookups. * @param cache The default cache for the specified class. * @param dclass The class whose cache is being set. */public static synchronized voidsetDefaultCache(Cache cache, int dclass) { DClass.check(dclass); defaultCaches.put(Mnemonic.toInteger(dclass), cache);}/** * Gets the search path that will be used as the default by future Lookups. * @return The default search path. */public static synchronized Name []getDefaultSearchPath() { return defaultSearchPath;}/** * Sets the search path to be used as the default by future Lookups. * @param domains The default search path. */public static synchronized voidsetDefaultSearchPath(Name [] domains) { defaultSearchPath = domains;}/** * Sets the search path that will be used as the default by future Lookups. * @param domains The default search path. * @throws TextParseException A name in the array is not a valid DNS name. */public static synchronized voidsetDefaultSearchPath(String [] domains) throws TextParseException { if (domains == null) { defaultSearchPath = null; return; } Name [] newdomains = new Name[domains.length]; for (int i = 0; i < domains.length; i++) newdomains[i] = Name.fromString(domains[i], Name.root); defaultSearchPath = newdomains;}private final voidreset() { iterations = 0; foundAlias = false; done = false; doneCurrent = false; aliases = null; answers = null; result = -1; error = null; nxdomain = false; badresponse = false; badresponse_error = null; networkerror = false; timedout = false; nametoolong = false; referral = false; if (temporary_cache) cache.clearCache();}/** * Create a Lookup object that will find records of the given name, type, * and class. The lookup will use the default cache, resolver, and search * path, and look for records that are reasonably credible. * @param name The name of the desired records * @param type The type of the desired records * @param dclass The class of the desired records * @throws IllegalArgumentException The type is a meta type other than ANY. * @see Cache * @see Resolver * @see Credibility * @see Name * @see Type * @see DClass */publicLookup(Name name, int type, int dclass) { Type.check(type); DClass.check(dclass); if (!Type.isRR(type) && type != Type.ANY) throw new IllegalArgumentException("Cannot query for " + "meta-types other than ANY"); this.name = name; this.type = type; this.dclass = dclass; synchronized (Lookup.class) { this.resolver = getDefaultResolver(); this.searchPath = getDefaultSearchPath(); this.cache = getDefaultCache(dclass); } this.credibility = Credibility.NORMAL; this.verbose = Options.check("verbose"); this.result = -1;}/** * Create a Lookup object that will find records of the given name and type * in the IN class. * @param name The name of the desired records * @param type The type of the desired records * @throws IllegalArgumentException The type is a meta type other than ANY. * @see #Lookup(Name,int,int) */publicLookup(Name name, int type) { this(name, type, DClass.IN);}/** * Create a Lookup object that will find records of type A at the given name * in the IN class. * @param name The name of the desired records * @see #Lookup(Name,int,int) */publicLookup(Name name) { this(name, Type.A, DClass.IN);}/** * Create a Lookup object that will find records of the given name, type, * and class. * @param name The name of the desired records * @param type The type of the desired records * @param dclass The class of the desired records * @throws TextParseException The name is not a valid DNS name * @throws IllegalArgumentException The type is a meta type other than ANY. * @see #Lookup(Name,int,int) */publicLookup(String name, int type, int dclass) throws TextParseException { this(Name.fromString(name), type, dclass);}/** * Create a Lookup object that will find records of the given name and type * in the IN class. * @param name The name of the desired records * @param type The type of the desired records * @throws TextParseException The name is not a valid DNS name * @throws IllegalArgumentException The type is a meta type other than ANY. * @see #Lookup(Name,int,int) */publicLookup(String name, int type) throws TextParseException { this(Name.fromString(name), type, DClass.IN);}/** * Create a Lookup object that will find records of type A at the given name * in the IN class. * @param name The name of the desired records * @throws TextParseException The name is not a valid DNS name * @see #Lookup(Name,int,int) */publicLookup(String name) throws TextParseException { this(Name.fromString(name), Type.A, DClass.IN);}/** * Sets the resolver to use when performing this lookup. This overrides the * default value. * @param resolver The resolver to use. */public voidsetResolver(Resolver resolver) { this.resolver = resolver;}/** * Sets the search path to use when performing this lookup. This overrides the * default value. * @param domains An array of names containing the search path. */public void
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -