use of org.minidns.record.Data 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.record.Data in project minidns by MiniDNS.
the class IterativeDNSClientTest method notGluedNsTest.
@SuppressWarnings("unchecked")
@Test
public void notGluedNsTest() throws IOException {
IterativeDNSClient client = new IterativeDNSClient(new LRUCache(0));
applyZones(client, rootZone(record("com", ns("ns.com")), record("net", ns("ns.net")), record("ns.com", a("1.1.1.1")), record("ns.net", a("1.1.2.1"))), zone("com", "ns.com", "1.1.1.1", record("example.com", ns("example.ns.net"))), zone("net", "ns.net", "1.1.2.1", record("example.ns.net", a("1.1.2.2"))), zone("example.com", "example.ns.net", "1.1.2.2", record("www.example.com", a("1.1.1.3"))));
DNSMessage message = client.query("www.example.com", TYPE.A);
assertNotNull(message);
List<Record<? extends Data>> answers = message.answerSection;
assertEquals(1, answers.size());
assertEquals(TYPE.A, answers.get(0).type);
assertArrayEquals(new byte[] { 1, 1, 1, 3 }, ((A) answers.get(0).payloadData).getIp());
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class IterativeDNSClientTest method basicIterativeTest.
@SuppressWarnings("unchecked")
@Test
public void basicIterativeTest() throws IOException {
IterativeDNSClient client = new IterativeDNSClient(new LRUCache(0));
applyZones(client, rootZone(record("com", ns("ns.com")), record("ns.com", a("1.1.1.1"))), zone("com", "ns.com", "1.1.1.1", record("example.com", ns("ns.example.com")), record("ns.example.com", a("1.1.1.2"))), zone("example.com", "ns.example.com", "1.1.1.2", record("www.example.com", a("1.1.1.3"))));
DNSMessage message = client.query("www.example.com", TYPE.A);
assertNotNull(message);
List<Record<? extends Data>> answers = message.answerSection;
assertEquals(1, answers.size());
assertEquals(TYPE.A, answers.get(0).type);
assertArrayEquals(new byte[] { 1, 1, 1, 3 }, ((A) answers.get(0).payloadData).getIp());
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSSECClientTest method testValidNSEC.
@SuppressWarnings("unchecked")
@Test
public void testValidNSEC() throws Exception {
DNSWorld world = applyZones(client, signedRootZone(sign(rootKSK, "", rootPrivateKSK, algorithm, record("", rootKSK), record("", rootZSK)), sign(rootZSK, "", rootPrivateZSK, algorithm, record("com", ds("com", digestType, comKSK))), sign(rootZSK, "", rootPrivateZSK, algorithm, record("com", ns("ns.com"))), sign(rootZSK, "", rootPrivateZSK, algorithm, record("ns.com", a("1.1.1.1")))), signedZone("com", "ns.com", "1.1.1.1", sign(comKSK, "com", comPrivateKSK, algorithm, record("com", comKSK), record("com", comZSK)), sign(comZSK, "com", comPrivateZSK, algorithm, record("example.com", a("1.1.1.2")))));
DNSMessage.Builder nsecMessage = DNSMessage.builder();
List<Record<? extends Data>> records = DNSSECWorld.merge(sign(comZSK, "com", comPrivateZSK, algorithm, record("example.com", nsec("www.example.com", Record.TYPE.A))), sign(comZSK, "com", comPrivateZSK, algorithm, record("example.com", soa("sns.dns.icann.org", "noc.dns.icann.org", 2015081265, 7200, 3600, 1209600, 3600))));
nsecMessage.setNameserverRecords(records);
nsecMessage.setAuthoritativeAnswer(true);
world.addPreparedResponse(new DNSSECWorld.AddressedNsecResponse(InetAddress.getByAddress("ns.com", new byte[] { 1, 1, 1, 1 }), nsecMessage.build()));
DNSMessage message = client.query("nsec.example.com", Record.TYPE.A);
client.setStripSignatureRecords(false);
assertNotNull(message);
assertEquals(0, message.answerSection.size());
assertTrue(message.authenticData);
}
use of org.minidns.record.Data in project minidns by MiniDNS.
the class DNSMessageTest method testTXTLookup.
@Test
public void testTXTLookup() throws Exception {
DNSMessage m = getMessageFromResource("codinghorror-txt");
HashSet<String> txtToBeFound = new HashSet<>();
txtToBeFound.add("google-site-verification=2oV3cW79A6icpGf-JbLGY4rP4_omL4FOKTqRxb-Dyl4");
txtToBeFound.add("keybase-site-verification=dKxf6T30x5EbNIUpeJcbWxUABJEnVWzQ3Z3hCumnk10");
txtToBeFound.add("v=spf1 include:spf.mandrillapp.com ~all");
List<Record<? extends Data>> answers = m.answerSection;
for (Record<? extends Data> r : answers) {
assertCsEquals("codinghorror.com", r.name);
Data d = r.getPayload();
assertTrue(d instanceof TXT);
assertEquals(TYPE.TXT, d.getType());
TXT txt = (TXT) d;
assertTrue(txtToBeFound.contains(txt.getText()));
txtToBeFound.remove(txt.getText());
}
assertEquals(txtToBeFound.size(), 0);
}
Aggregations