Skip to content
Permalink
Browse files

Lists for RRset (#56)

* Fix or disable unchecked warnings

* Use List as return types for RRset rrs/sigs
  • Loading branch information...
ibauersachs committed Jul 14, 2019
1 parent 2cf542f commit f237c5b6fd13e6f948a445bab4316b9e83fc2bac
@@ -65,6 +65,7 @@
<configuration>
<source>${target.jdk}</source>
<target>${target.jdk}</target>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>

@@ -163,13 +163,13 @@
return null;
}

public RRset
public <T extends Record> RRset<T>
findExactMatch(Name name, int type, int dclass, boolean glue) {
Zone zone = findBestZone(name);
if (zone != null)
return zone.findExactMatch(name, type);
else {
RRset [] rrsets;
List<RRset<T>> rrsets;
Cache cache = getCache(dclass);
if (glue)
rrsets = cache.findAnyRecords(name, type);
@@ -178,28 +178,24 @@
if (rrsets == null)
return null;
else
return rrsets[0]; /* not quite right */
return rrsets.get(0); /* not quite right */
}
}

void
addRRset(Name name, Message response, RRset rrset, int section, int flags) {
<T extends Record> void
addRRset(Name name, Message response, RRset<T> rrset, int section, int flags) {
for (int s = 1; s <= section; s++)
if (response.findRRset(name, rrset.getType(), s))
return;
if ((flags & FLAG_SIGONLY) == 0) {
Iterator<Record> it = rrset.rrs();
while (it.hasNext()) {
Record r = (Record) it.next();
for (Record r : rrset.rrs()) {
if (r.getName().isWild() && !name.isWild())
r = r.withName(name);
response.addRecord(r, section);
}
}
if ((flags & (FLAG_SIGONLY | FLAG_DNSSECOK)) != 0) {
Iterator<Record> it = rrset.sigs();
while (it.hasNext()) {
Record r = (Record) it.next();
for (Record r : rrset.sigs()) {
if (r.getName().isWild() && !name.isWild())
r = r.withName(name);
response.addRecord(r, section);
@@ -214,7 +210,7 @@

private void
addNS(Message response, Zone zone, int flags) {
RRset nsRecords = zone.getNS();
RRset<NSRecord> nsRecords = zone.getNS();
addRRset(nsRecords.getName(), response, nsRecords,
Section.AUTHORITY, flags);
}
@@ -224,17 +220,15 @@
SetResponse sr = cache.lookupRecords(name, Type.NS, Credibility.HINT);
if (!sr.isDelegation())
return;
RRset nsRecords = sr.getNS();
Iterator<Record> it = nsRecords.rrs();
while (it.hasNext()) {
Record r = (Record) it.next();
RRset<?> nsRecords = sr.getNS();
for (Record r : nsRecords.rrs()) {
response.addRecord(r, Section.AUTHORITY);
}
}

private void
addGlue(Message response, Name name, int flags) {
RRset a = findExactMatch(name, Type.A, DClass.IN, true);
RRset<ARecord> a = findExactMatch(name, Type.A, DClass.IN, true);
if (a == null)
return;
addRRset(name, response, a, Section.ADDITIONAL, flags);
@@ -299,13 +293,13 @@ else if (sr.isNXRRSET()) {
}
}
else if (sr.isDelegation()) {
RRset nsRecords = sr.getNS();
RRset<NSRecord> nsRecords = sr.getNS();
addRRset(nsRecords.getName(), response, nsRecords,
Section.AUTHORITY, flags);
}
else if (sr.isCNAME()) {
CNAMERecord cname = sr.getCNAME();
RRset rrset = new RRset(cname);
RRset<CNAMERecord> rrset = new RRset<>(cname);
addRRset(name, response, rrset, Section.ANSWER, flags);
if (zone != null && iterations == 0)
response.getHeader().setFlag(Flags.AA);
@@ -314,7 +308,7 @@ else if (sr.isCNAME()) {
}
else if (sr.isDNAME()) {
DNAMERecord dname = sr.getDNAME();
RRset rrset = new RRset(dname);
RRset<DNAMERecord> rrset = new RRset<>(dname);
addRRset(name, response, rrset, Section.ANSWER, flags);
Name newname;
try {
@@ -323,17 +317,19 @@ else if (sr.isDNAME()) {
catch (NameTooLongException e) {
return Rcode.YXDOMAIN;
}
rrset = new RRset(new CNAMERecord(name, dclass, 0, newname));
addRRset(name, response, rrset, Section.ANSWER, flags);

CNAMERecord cname = new CNAMERecord(name, dclass, 0, newname);
RRset<CNAMERecord> cnamerrset = new RRset<>(cname);
addRRset(name, response, cnamerrset, Section.ANSWER, flags);
if (zone != null && iterations == 0)
response.getHeader().setFlag(Flags.AA);
rcode = addAnswer(response, newname, type, dclass,
iterations + 1, flags);
}
else if (sr.isSuccessful()) {
RRset [] rrsets = sr.answers();
for (RRset rrset : rrsets)
addRRset(name, response, rrset, Section.ANSWER, flags);
List<RRset<?>> rrsets = sr.answers();
for (RRset<?> rrset : rrsets)
addRRset(name, response, rrset, Section.ANSWER, flags);
if (zone != null) {
addNS(response, zone, flags);
if (iterations == 0)
@@ -351,13 +347,13 @@ else if (sr.isSuccessful()) {
boolean first = true;
if (zone == null)
return errorMessage(query, Rcode.REFUSED);
Iterator<RRset> it = zone.AXFR();
try {
DataOutputStream dataOut;
dataOut = new DataOutputStream(s.getOutputStream());
int id = query.getHeader().getID();
Iterator<RRset<?>> it = zone.AXFR();
while (it.hasNext()) {
RRset rrset = (RRset) it.next();
RRset<?> rrset = it.next();
Message response = new Message(id);
Header header = response.getHeader();
header.setFlag(Flags.QR);
@@ -35,22 +35,22 @@
return (int)expire;
}

private static class CacheRRset extends RRset implements Element {
private static class CacheRRset<T extends Record> extends RRset<T> implements Element {
private static final long serialVersionUID = 5971755205903597024L;

int credibility;
int expire;

public
CacheRRset(Record rec, int cred, long maxttl) {
CacheRRset(T rec, int cred, long maxttl) {
super();
this.credibility = cred;
this.expire = limitExpire(rec.getTTL(), maxttl);
addRR(rec);
}

public
CacheRRset(RRset rrset, int cred, long maxttl) {
CacheRRset(RRset<T> rrset, int cred, long maxttl) {
super(rrset);
this.credibility = cred;
this.expire = limitExpire(rrset.getTTL(), maxttl);
@@ -132,7 +132,7 @@
}
}

private static class CacheMap extends LinkedHashMap {
private static class CacheMap extends LinkedHashMap<Name, Object> {
private int maxsize;

CacheMap(int maxsize) {
@@ -198,7 +198,7 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
Master m = new Master(file);
Record record;
while ((record = m.nextRecord()) != null)
addRecord(record, Credibility.HINT, m);
addRecord(record, Credibility.HINT);
}

private synchronized Object
@@ -214,9 +214,10 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
private synchronized Element []
allElements(Object types) {
if (types instanceof List) {
List typelist = (List) types;
@SuppressWarnings("unchecked")
List<Element> typelist = (List<Element>) types;
int size = typelist.size();
return (Element []) typelist.toArray(new Element[size]);
return typelist.toArray(new Element[size]);
} else {
Element set = (Element) types;
return new Element[] {set};
@@ -271,9 +272,10 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
}
int type = element.getType();
if (types instanceof List) {
@SuppressWarnings("unchecked")
List<Element> list = (List<Element>) types;
for (int i = 0; i < list.size(); i++) {
Element elt = (Element) list.get(i);
Element elt = list.get(i);
if (elt.getType() == type) {
list.set(i, element);
return;
@@ -328,22 +330,22 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
* Adds a record to the Cache.
* @param r The record to be added
* @param cred The credibility of the record
* @param o The source of the record (this could be a Message, for example)
* @see Record
*/
public synchronized void
addRecord(Record r, int cred, Object o) {
addRecord(Record r, int cred) {
Name name = r.getName();
int type = r.getRRsetType();
if (!Type.isRR(type))
return;
Element element = findElement(name, type, cred);
if (element == null) {
CacheRRset crrset = new CacheRRset(r, cred, maxcache);
CacheRRset<Record> crrset = new CacheRRset<>(r, cred, maxcache);
addRRset(crrset, cred);
} else if (element.compareCredibility(cred) == 0) {
if (element instanceof CacheRRset) {
CacheRRset crrset = (CacheRRset) element;
@SuppressWarnings("unchecked")
CacheRRset<Record> crrset = (CacheRRset<Record>) element;
crrset.addRR(r);
}
}
@@ -355,8 +357,8 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
* @param cred The credibility of these records
* @see RRset
*/
public synchronized void
addRRset(RRset rrset, int cred) {
public synchronized <T extends Record> void
addRRset(RRset<T> rrset, int cred) {
long ttl = rrset.getTTL();
Name name = rrset.getName();
int type = rrset.getType();
@@ -368,11 +370,11 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
if (element != null && element.compareCredibility(cred) <= 0)
element = null;
if (element == null) {
CacheRRset crrset;
CacheRRset<T> crrset;
if (rrset instanceof CacheRRset)
crrset = (CacheRRset) rrset;
crrset = (CacheRRset<T>) rrset;
else
crrset = new CacheRRset(rrset, cred, maxcache);
crrset = new CacheRRset<>(rrset, cred, maxcache);
addElement(name, crrset);
}
}
@@ -453,7 +455,7 @@ else if (isExact)
continue;
if (element.compareCredibility(minCred) < 0)
continue;
sr.addRRset((CacheRRset) element);
sr.addRRset((CacheRRset<?>) element);
added++;
}
/* There were positive entries */
@@ -518,11 +520,12 @@ else if (isExact)
return lookup(name, type, minCred);
}

private RRset []
@SuppressWarnings("unchecked")
private <T extends Record> List<RRset<T>>
findRecords(Name name, int type, int minCred) {
SetResponse cr = lookupRecords(name, type, minCred);
if (cr.isSuccessful())
return cr.answers();
return (List<RRset<T>>)(List) cr.answers();
else
return null;
}
@@ -535,7 +538,7 @@ else if (isExact)
* @return An array of RRsets, or null
* @see Credibility
*/
public RRset []
public <T extends Record> List<RRset<T>>
findRecords(Name name, int type) {
return findRecords(name, type, Credibility.NORMAL);
}
@@ -548,7 +551,7 @@ else if (isExact)
* @return An array of RRsets, or null
* @see Credibility
*/
public RRset []
public <T extends Record>List<RRset<T>>
findAnyRecords(Name name, int type) {
return findRecords(name, type, Credibility.GLUE);
}
@@ -572,14 +575,12 @@ else if (isExact)
}

private static void
markAdditional(RRset rrset, Set<Name> names) {
markAdditional(RRset<?> rrset, Set<Name> names) {
Record first = rrset.first();
if (first.getAdditionalName() == null)
return;

Iterator<Record> it = rrset.rrs();
while (it.hasNext()) {
Record r = it.next();
for (Record r : rrset.rrs()) {
Name name = r.getAdditionalName();
if (name != null)
names.add(name);
@@ -594,6 +595,7 @@ else if (isExact)
* lookup, or null if nothing useful could be cached from the message.
* @see Message
*/
@SuppressWarnings("unchecked")
public SetResponse
addMessage(Message in) {
boolean isAuth = in.getHeader().getFlag(Flags.AA);
@@ -623,7 +625,7 @@ else if (isExact)
additionalNames = new HashSet<>();

answers = in.getSectionRRsets(Section.ANSWER);
for (RRset answer : answers) {
for (RRset<?> answer : answers) {
if (answer.getDClass() != qclass)
continue;
int type = answer.getType();
@@ -664,14 +666,15 @@ else if (isExact)
}

auth = in.getSectionRRsets(Section.AUTHORITY);
RRset soa = null, ns = null;
for (RRset rset : auth) {
RRset<SOARecord> soa = null;
RRset<NSRecord> ns = null;
for (RRset<?> rset : auth) {
if (rset.getType() == Type.SOA &&
curname.subdomain(rset.getName()))
soa = rset;
soa = (RRset<SOARecord>) rset;
else if (rset.getType() == Type.NS &&
curname.subdomain(rset.getName()))
ns = rset;
ns = (RRset<NSRecord>) rset;
}
if (!completed) {
/* This is a negative response or a referral. */
@@ -710,7 +713,7 @@ else if (rset.getType() == Type.NS &&
}

addl = in.getSectionRRsets(Section.ADDITIONAL);
for (RRset rRset : addl) {
for (RRset<?> rRset : addl) {
int type = rRset.getType();
if (type != Type.A && type != Type.AAAA && type != Type.A6)
continue;

0 comments on commit f237c5b

Please sign in to comment.
You can’t perform that action at this time.