📄 cache.java
字号:
continue; /* If this is an ANY lookup, return everything. */ if (isExact && type == Type.ANY) { sr = new SetResponse(SetResponse.SUCCESSFUL); Element [] elements = allElements(types); int added = 0; for (int i = 0; i < elements.length; i++) { element = elements[i]; if (element.expired()) { removeElement(tname, element.getType()); continue; } if (!(element instanceof CacheRRset)) continue; if (element.compareCredibility(minCred) < 0) continue; sr.addRRset((CacheRRset)element); added++; } /* There were positive entries */ if (added > 0) return sr; } /* * If this is the name, look for the actual type or a CNAME. * Otherwise, look for a DNAME. */ if (isExact) { element = oneElement(tname, types, type, minCred); if (element != null && element instanceof CacheRRset) { sr = new SetResponse(SetResponse.SUCCESSFUL); sr.addRRset((CacheRRset) element); return sr; } else if (element != null) { sr = new SetResponse(SetResponse.NXRRSET); return sr; } element = oneElement(tname, types, Type.CNAME, minCred); if (element != null && element instanceof CacheRRset) { return new SetResponse(SetResponse.CNAME, (CacheRRset) element); } } else { element = oneElement(tname, types, Type.DNAME, minCred); if (element != null && element instanceof CacheRRset) { return new SetResponse(SetResponse.DNAME, (CacheRRset) element); } } /* Look for an NS */ element = oneElement(tname, types, Type.NS, minCred); if (element != null && element instanceof CacheRRset) return new SetResponse(SetResponse.DELEGATION, (CacheRRset) element); /* Check for the special NXDOMAIN element. */ if (isExact) { element = oneElement(tname, types, 0, minCred); if (element != null) return SetResponse.ofType(SetResponse.NXDOMAIN); } } return SetResponse.ofType(SetResponse.UNKNOWN);}/** * Looks up Records in the Cache. This follows CNAMEs and handles negatively * cached data. * @param name The name to look up * @param type The type to look up * @param minCred The minimum acceptable credibility * @return A SetResponse object * @see SetResponse * @see Credibility */public SetResponselookupRecords(Name name, int type, int minCred) { return lookup(name, type, minCred);}private RRset []findRecords(Name name, int type, int minCred) { SetResponse cr = lookupRecords(name, type, minCred); if (cr.isSuccessful()) return cr.answers(); else return null;}/** * Looks up credible Records in the Cache (a wrapper around lookupRecords). * Unlike lookupRecords, this given no indication of why failure occurred. * @param name The name to look up * @param type The type to look up * @return An array of RRsets, or null * @see Credibility */public RRset []findRecords(Name name, int type) { return findRecords(name, type, Credibility.NORMAL);}/** * Looks up Records in the Cache (a wrapper around lookupRecords). Unlike * lookupRecords, this given no indication of why failure occurred. * @param name The name to look up * @param type The type to look up * @return An array of RRsets, or null * @see Credibility */public RRset []findAnyRecords(Name name, int type) { return findRecords(name, type, Credibility.GLUE);}private final intgetCred(int section, boolean isAuth) { if (section == Section.ANSWER) { if (isAuth) return Credibility.AUTH_ANSWER; else return Credibility.NONAUTH_ANSWER; } else if (section == Section.AUTHORITY) { if (isAuth) return Credibility.AUTH_AUTHORITY; else return Credibility.NONAUTH_AUTHORITY; } else if (section == Section.ADDITIONAL) { return Credibility.ADDITIONAL; } else throw new IllegalArgumentException("getCred: invalid section");}private static voidmarkAdditional(RRset rrset, Set names) { Record first = rrset.first(); if (first.getAdditionalName() == null) return; Iterator it = rrset.rrs(); while (it.hasNext()) { Record r = (Record) it.next(); Name name = r.getAdditionalName(); if (name != null) names.add(name); }}/** * Adds all data from a Message into the Cache. Each record is added with * the appropriate credibility, and negative answers are cached as such. * @param in The Message to be added * @return A SetResponse that reflects what would be returned from a cache * lookup, or null if nothing useful could be cached from the message. * @see Message */public SetResponseaddMessage(Message in) { boolean isAuth = in.getHeader().getFlag(Flags.AA); Record question = in.getQuestion(); Name qname; Name curname; int qtype; int qclass; int cred; int rcode = in.getHeader().getRcode(); boolean haveAnswer = false; boolean completed = false; RRset [] answers, auth, addl; SetResponse response = null; boolean verbose = Options.check("verbosecache"); HashSet additionalNames; if ((rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) || question == null) return null; qname = question.getName(); qtype = question.getType(); qclass = question.getDClass(); curname = qname; additionalNames = new HashSet(); answers = in.getSectionRRsets(Section.ANSWER); for (int i = 0; i < answers.length; i++) { if (answers[i].getDClass() != qclass) continue; int type = answers[i].getType(); Name name = answers[i].getName(); cred = getCred(Section.ANSWER, isAuth); if ((type == qtype || qtype == Type.ANY) && name.equals(curname)) { addRRset(answers[i], cred); completed = true; haveAnswer = true; if (curname == qname) { if (response == null) response = new SetResponse( SetResponse.SUCCESSFUL); response.addRRset(answers[i]); } markAdditional(answers[i], additionalNames); } else if (type == Type.CNAME && name.equals(curname)) { CNAMERecord cname; addRRset(answers[i], cred); if (curname == qname) response = new SetResponse(SetResponse.CNAME, answers[i]); cname = (CNAMERecord) answers[i].first(); curname = cname.getTarget(); haveAnswer = true; } else if (type == Type.DNAME && curname.subdomain(name)) { DNAMERecord dname; addRRset(answers[i], cred); if (curname == qname) response = new SetResponse(SetResponse.DNAME, answers[i]); dname = (DNAMERecord) answers[i].first(); try { curname = curname.fromDNAME(dname); } catch (NameTooLongException e) { break; } haveAnswer = true; } } auth = in.getSectionRRsets(Section.AUTHORITY); RRset soa = null, ns = null; for (int i = 0; i < auth.length; i++) { if (auth[i].getType() == Type.SOA && curname.subdomain(auth[i].getName())) soa = auth[i]; else if (auth[i].getType() == Type.NS && curname.subdomain(auth[i].getName())) ns = auth[i]; } if (!completed) { /* This is a negative response or a referral. */ int cachetype = (rcode == Rcode.NXDOMAIN) ? 0 : qtype; if (soa != null || ns == null) { /* Negative response */ cred = getCred(Section.AUTHORITY, isAuth); SOARecord soarec = null; if (soa != null) soarec = (SOARecord) soa.first(); addNegative(curname, cachetype, soarec, cred); if (response == null) { int responseType; if (rcode == Rcode.NXDOMAIN) responseType = SetResponse.NXDOMAIN; else responseType = SetResponse.NXRRSET; response = SetResponse.ofType(responseType); } /* NXT records are not cached yet. */ } else { /* Referral response */ cred = getCred(Section.AUTHORITY, isAuth); addRRset(ns, cred); markAdditional(ns, additionalNames); if (response == null) response = new SetResponse( SetResponse.DELEGATION, ns); } } else if (rcode == Rcode.NOERROR && ns != null) { /* Cache the NS set from a positive response. */ cred = getCred(Section.AUTHORITY, isAuth); addRRset(ns, cred); markAdditional(ns, additionalNames); } addl = in.getSectionRRsets(Section.ADDITIONAL); for (int i = 0; i < addl.length; i++) { int type = addl[i].getType(); if (type != Type.A && type != Type.AAAA && type != Type.A6) continue; Name name = addl[i].getName(); if (!additionalNames.contains(name)) continue; cred = getCred(Section.ADDITIONAL, isAuth); addRRset(addl[i], cred); } if (verbose) System.out.println("addMessage: " + response); return (response);}/** * Flushes an RRset from the cache * @param name The name of the records to be flushed * @param type The type of the records to be flushed * @see RRset */public voidflushSet(Name name, int type) { removeElement(name, type);}/** * Flushes all RRsets with a given name from the cache * @param name The name of the records to be flushed * @see RRset */public voidflushName(Name name) { removeName(name);}/** * Sets the maximum length of time that a negative response will be stored * in this Cache. A negative value disables this feature (that is, sets * no limit). */public voidsetMaxNCache(int seconds) { maxncache = seconds;}/** * Gets the maximum length of time that a negative response will be stored * in this Cache. A negative value indicates no limit. */public intgetMaxNCache() { return maxncache;}/** * Sets the maximum length of time that records will be stored in this * Cache. A negative value disables this feature (that is, sets no limit). */public voidsetMaxCache(int seconds) { maxcache = seconds;}/** * Gets the maximum length of time that records will be stored * in this Cache. A negative value indicates no limit. */public intgetMaxCache() { return maxcache;}/** * Gets the current number of entries in the Cache, where an entry consists * of all records with a specific Name. */public intgetSize() { return data.size();}/** * Gets the maximum number of entries in the Cache, where an entry consists * of all records with a specific Name. A negative value is treated as an * infinite limit. */public intgetMaxEntries() { return data.getMaxSize();}/** * Sets the maximum number of entries in the Cache, where an entry consists * of all records with a specific Name. A negative value is treated as an * infinite limit. * * Note that setting this to a value lower than the current number * of entries will not cause the Cache to shrink immediately. * * The default maximum number of entries is 50000. * * @param entries The maximum number of entries in the Cache. */public voidsetMaxEntries(int entries) { data.setMaxSize(entries);}/** * Returns the DNS class of this cache. */public intgetDClass() { return dclass;}/** * Returns the contents of the Cache as a string. */ public StringtoString() { StringBuffer sb = new StringBuffer(); synchronized (this) { Iterator it = data.values().iterator(); while (it.hasNext()) { Element [] elements = allElements(it.next()); for (int i = 0; i < elements.length; i++) { sb.append(elements[i]); sb.append("\n"); } } } return sb.toString();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -