⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lookup.java

📁 DNS Java 是java实现的DNS
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
setSearchPath(Name [] domains) {	this.searchPath = domains;}/** * 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. * @throws TextParseException A name in the array is not a valid DNS name. */public voidsetSearchPath(String [] domains) throws TextParseException {	if (domains == null) {		this.searchPath = null;		return;	}	Name [] newdomains = new Name[domains.length];	for (int i = 0; i < domains.length; i++)		newdomains[i] = Name.fromString(domains[i], Name.root);	this.searchPath = newdomains;}/** * Sets the cache to use when performing this lookup.  This overrides the * default value.  If the results of this lookup should not be permanently * cached, null can be provided here. * @param cache The cache to use. */public voidsetCache(Cache cache) {	if (cache == null) {		this.cache = new Cache(dclass);		this.temporary_cache = true;	} else {		this.cache = cache;		this.temporary_cache = false;	}}/** * Sets the minimum credibility level that will be accepted when performing * the lookup.  This defaults to Credibility.NORMAL. * @param credibility The minimum credibility level. */public voidsetCredibility(int credibility) {	this.credibility = credibility;}private voidfollow(Name name, Name oldname) {	foundAlias = true;	badresponse = false;	networkerror = false;	timedout = false;	nxdomain = false;	referral = false;	iterations++;	if (iterations >= 6 || name.equals(oldname)) {		result = UNRECOVERABLE;		error = "CNAME loop";		done = true;		return;	}	if (aliases == null)		aliases = new ArrayList();	aliases.add(oldname);	lookup(name);}private voidprocessResponse(Name name, SetResponse response) {	if (response.isSuccessful()) {		RRset [] rrsets = response.answers();		List l = new ArrayList();		Iterator it;		int i;		for (i = 0; i < rrsets.length; i++) {			it = rrsets[i].rrs();			while (it.hasNext())				l.add(it.next());		}		result = SUCCESSFUL;		answers = (Record []) l.toArray(new Record[l.size()]);		done = true;	} else if (response.isNXDOMAIN()) {		nxdomain = true;		doneCurrent = true;		if (iterations > 0) {			result = HOST_NOT_FOUND;			done = true;		}	} else if (response.isNXRRSET()) {		result = TYPE_NOT_FOUND;		answers = null;		done = true;	} else if (response.isCNAME()) {		CNAMERecord cname = response.getCNAME();		follow(cname.getTarget(), name);	} else if (response.isDNAME()) {		DNAMERecord dname = response.getDNAME();		Name newname = null;		try {			follow(name.fromDNAME(dname), name);		} catch (NameTooLongException e) {			result = UNRECOVERABLE;			error = "Invalid DNAME target";			done = true;		}	} else if (response.isDelegation()) {		// We shouldn't get a referral.  Ignore it.		referral = true;	}}private voidlookup(Name current) {	SetResponse sr = cache.lookupRecords(current, type, credibility);	if (verbose) {		System.err.println("lookup " + current + " " +				   Type.string(type));		System.err.println(sr);	}	processResponse(current, sr);	if (done || doneCurrent)		return;	Record question = Record.newRecord(current, type, dclass);	Message query = Message.newQuery(question);	Message response = null;	try {		response = resolver.send(query);	}	catch (IOException e) {		// A network error occurred.  Press on.		if (e instanceof InterruptedIOException)			timedout = true;		else			networkerror = true;		return;	}	int rcode = response.getHeader().getRcode();	if (rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) {		// The server we contacted is broken or otherwise unhelpful.		// Press on.		badresponse = true;		badresponse_error = Rcode.string(rcode);		return;	}	if (!query.getQuestion().equals(response.getQuestion())) {		// The answer doesn't match the question.  That's not good.		badresponse = true;		badresponse_error = "response does not match query";		return;	}	sr = cache.addMessage(response);	if (sr == null)		sr = cache.lookupRecords(current, type, credibility);	if (verbose) {		System.err.println("queried " + current + " " +				   Type.string(type));		System.err.println(sr);	}	processResponse(current, sr);}private voidresolve(Name current, Name suffix) {	doneCurrent = false;	Name tname = null;	if (suffix == null)		tname = current;	else {		try {			tname = Name.concatenate(current, suffix);		}		catch (NameTooLongException e) {			nametoolong = true;			return;		}	}	lookup(tname);}/** * Performs the lookup, using the specified Cache, Resolver, and search path. * @return The answers, or null if none are found. */public Record []run() {	if (done)		reset();	if (name.isAbsolute())		resolve(name, null);	else if (searchPath == null)		resolve(name, Name.root);	else {		if (name.labels() > 1)			resolve(name, Name.root);		if (done)			return answers;		for (int i = 0; i < searchPath.length; i++) {			resolve(name, searchPath[i]);			if (done)				return answers;			else if (foundAlias)				break;		}	}	if (!done) {		if (badresponse) {			result = TRY_AGAIN;			error = badresponse_error;			done = true;		} else if (timedout) {			result = TRY_AGAIN;			error = "timed out";			done = true;		} else if (networkerror) {			result = TRY_AGAIN;			error = "network error";			done = true;		} else if (nxdomain) {			result = HOST_NOT_FOUND;			done = true;		} else if (referral) {			result = UNRECOVERABLE;			error = "referral";			done = true;		} else if (nametoolong) {			result = UNRECOVERABLE;			error = "name too long";			done = true;		}	}	return answers;}private voidcheckDone() {	if (done && result != -1)		return;	StringBuffer sb = new StringBuffer("Lookup of " + name + " ");	if (dclass != DClass.IN)		sb.append(DClass.string(dclass) + " ");	sb.append(Type.string(type) + " isn't done");	throw new IllegalStateException(sb.toString());}/** * Returns the answers from the lookup. * @return The answers, or null if none are found. * @throws IllegalStateException The lookup has not completed. */public Record []getAnswers() {	checkDone();	return answers;}/** * Returns all known aliases for this name.  Whenever a CNAME/DNAME is * followed, an alias is added to this array.  The last element in this * array will be the owner name for records in the answer, if there are any. * @return The aliases. * @throws IllegalStateException The lookup has not completed. */public Name []getAliases() {	checkDone();	if (aliases == null)		return noAliases;	return (Name []) aliases.toArray(new Name[aliases.size()]);}/** * Returns the result code of the lookup. * @return The result code, which can be SUCCESSFUL, UNRECOVERABLE, TRY_AGAIN, * HOST_NOT_FOUND, or TYPE_NOT_FOUND. * @throws IllegalStateException The lookup has not completed. */public intgetResult() {	checkDone();	return result;}/** * Returns an error string describing the result code of this lookup. * @return A string, which may either directly correspond the result code * or be more specific. * @throws IllegalStateException The lookup has not completed. */public StringgetErrorString() {	checkDone();	if (error != null)		return error;	switch (result) {		case SUCCESSFUL:	return "successful";		case UNRECOVERABLE:	return "unrecoverable error";		case TRY_AGAIN:		return "try again";		case HOST_NOT_FOUND:	return "host not found";		case TYPE_NOT_FOUND:	return "type not found";	}	throw new IllegalStateException("unknown result");}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -