use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.
the class IterativeDNSClient method queryRecursive.
private DNSMessage queryRecursive(ResolutionState resolutionState, DNSMessage q, InetAddress address, DNSName authoritativeZone) throws IOException {
resolutionState.recurse(address, q);
DNSMessage resMessage = query(q, address);
if (resMessage == null) {
// TODO throw exception here?
return null;
}
if (resMessage.authoritativeAnswer) {
return resMessage;
}
if (cache != null) {
cache.offer(q, resMessage, authoritativeZone);
}
List<Record<? extends Data>> authorities = resMessage.copyAuthority();
List<IOException> ioExceptions = new LinkedList<>();
// Glued NS first
for (Iterator<Record<? extends Data>> iterator = authorities.iterator(); iterator.hasNext(); ) {
Record<? extends Data> record = iterator.next();
if (record.type != TYPE.NS) {
iterator.remove();
continue;
}
DNSName name = ((NS) record.payloadData).target;
IpResultSet gluedNs = searchAdditional(resMessage, name);
for (Iterator<InetAddress> addressIterator = gluedNs.addresses.iterator(); addressIterator.hasNext(); ) {
InetAddress target = addressIterator.next();
DNSMessage recursive = null;
try {
recursive = queryRecursive(resolutionState, q, target, record.name);
} catch (IOException e) {
abortIfFatal(e);
LOGGER.log(Level.FINER, "Exception while recursing", e);
resolutionState.decrementSteps();
ioExceptions.add(e);
if (!addressIterator.hasNext()) {
iterator.remove();
}
continue;
}
return recursive;
}
}
// Try non-glued NS
for (Record<? extends Data> record : authorities) {
final Question question = q.getQuestion();
DNSName name = ((NS) record.payloadData).target;
// AAAA RR, then we should not continue here as it would result in an endless loop.
if (question.name.equals(name) && (question.type == TYPE.A || question.type == TYPE.AAAA))
continue;
IpResultSet res = null;
try {
res = resolveIpRecursive(resolutionState, name);
} catch (IOException e) {
resolutionState.decrementSteps();
ioExceptions.add(e);
}
if (res == null) {
continue;
}
for (InetAddress target : res.addresses) {
DNSMessage recursive = null;
try {
recursive = queryRecursive(resolutionState, q, target, record.name);
} catch (IOException e) {
resolutionState.decrementSteps();
ioExceptions.add(e);
continue;
}
return recursive;
}
}
MultipleIoException.throwIfRequired(ioExceptions);
// where we able to find glue records or the IPs of the next nameservers.
return null;
}
use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.
the class IterativeDNSClient method queryRecursive.
private DNSMessage queryRecursive(ResolutionState resolutionState, DNSMessage q) throws IOException {
InetAddress primaryTarget = null, secondaryTarget = null;
Question question = q.getQuestion();
DNSName parent = question.name.getParent();
switch(ipVersionSetting) {
case v4only:
for (A a : getCachedIPv4NameserverAddressesFor(parent)) {
if (primaryTarget == null) {
primaryTarget = a.getInetAddress();
continue;
}
secondaryTarget = a.getInetAddress();
break;
}
break;
case v6only:
for (AAAA aaaa : getCachedIPv6NameserverAddressesFor(parent)) {
if (primaryTarget == null) {
primaryTarget = aaaa.getInetAddress();
continue;
}
secondaryTarget = aaaa.getInetAddress();
break;
}
break;
case v4v6:
InetAddress[] v4v6targets = getTargets(getCachedIPv4NameserverAddressesFor(parent), getCachedIPv6NameserverAddressesFor(parent));
primaryTarget = v4v6targets[0];
secondaryTarget = v4v6targets[1];
break;
case v6v4:
InetAddress[] v6v4targets = getTargets(getCachedIPv6NameserverAddressesFor(parent), getCachedIPv4NameserverAddressesFor(parent));
primaryTarget = v6v4targets[0];
secondaryTarget = v6v4targets[1];
break;
default:
throw new AssertionError();
}
DNSName authoritativeZone = parent;
if (primaryTarget == null) {
authoritativeZone = DNSName.ROOT;
switch(ipVersionSetting) {
case v4only:
primaryTarget = getRandomIpv4RootServer();
break;
case v6only:
primaryTarget = getRandomIpv6RootServer();
break;
case v4v6:
primaryTarget = getRandomIpv4RootServer();
secondaryTarget = getRandomIpv6RootServer();
break;
case v6v4:
primaryTarget = getRandomIpv6RootServer();
secondaryTarget = getRandomIpv4RootServer();
break;
}
}
List<IOException> ioExceptions = new LinkedList<>();
try {
return queryRecursive(resolutionState, q, primaryTarget, authoritativeZone);
} catch (IOException ioException) {
abortIfFatal(ioException);
ioExceptions.add(ioException);
}
if (secondaryTarget != null) {
try {
return queryRecursive(resolutionState, q, secondaryTarget, authoritativeZone);
} catch (IOException ioException) {
ioExceptions.add(ioException);
}
}
MultipleIoException.throwIfRequired(ioExceptions);
return null;
}
use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.
the class DNSNameTest method testFromVarargs.
@Test
public void testFromVarargs() {
String leftString = "leftmost.left";
String middleString = "leftMiddle.middle.rightMiddle";
String rightString = "right.rightMost";
DNSName left = DNSName.from(leftString);
DNSName middle = DNSName.from(middleString);
DNSName right = DNSName.from(rightString);
DNSName name = DNSName.from(left, middle, right);
String completeString = leftString + '.' + middleString + '.' + rightString;
assertEquals(name.getRawAce(), completeString);
DNSName expected = DNSName.from(completeString);
assertEquals(name, expected);
}
use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.
the class DNSNameTest method testConcact.
@Test
public void testConcact() {
String leftString = "foo.bar.de";
String rightString = "example.org";
DNSName left = DNSName.from(leftString);
DNSName right = DNSName.from(rightString);
DNSName concated = DNSName.from(left, right);
DNSName expected = DNSName.from(leftString + '.' + rightString);
assertEquals(expected, concated);
}
use of org.minidns.dnsname.DnsName in project minidns by MiniDNS.
the class DNSNameTest method caseInsenstiveCompare.
@Test
public void caseInsenstiveCompare() {
DNSName lowercase = DNSName.from("cs.fau.de");
DNSName uppercase = DNSName.from("CS.fau.de");
assertEquals(lowercase, uppercase);
}
Aggregations