use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class IterativeDNSClient method resolveIpRecursive.
private IpResultSet resolveIpRecursive(ResolutionState resolutionState, DNSName name) throws IOException {
IpResultSet.Builder res = newIpResultSetBuilder();
if (ipVersionSetting.v4) {
// TODO Try to retrieve A records for name out from cache.
Question question = new Question(name, TYPE.A);
final DNSMessage query = getQueryFor(question);
DNSMessage aMessage = queryRecursive(resolutionState, query);
if (aMessage != null) {
for (Record<? extends Data> answer : aMessage.answerSection) {
if (answer.isAnswer(question)) {
InetAddress inetAddress = inetAddressFromRecord(name.ace, (A) answer.payloadData);
res.ipv4Addresses.add(inetAddress);
} else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
}
}
}
}
if (ipVersionSetting.v6) {
// TODO Try to retrieve AAAA records for name out from cache.
Question question = new Question(name, TYPE.AAAA);
final DNSMessage query = getQueryFor(question);
DNSMessage aMessage = queryRecursive(resolutionState, query);
if (aMessage != null) {
for (Record<? extends Data> answer : aMessage.answerSection) {
if (answer.isAnswer(question)) {
InetAddress inetAddress = inetAddressFromRecord(name.ace, (AAAA) answer.payloadData);
res.ipv6Addresses.add(inetAddress);
} else if (answer.type == TYPE.CNAME && answer.name.equals(name)) {
return resolveIpRecursive(resolutionState, ((RRWithTarget) answer.payloadData).target);
}
}
}
}
return res.build();
}
use of org.minidns.dnsmessage.Question 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.dnsmessage.Question in project minidns by MiniDNS.
the class DNSMessageTest method testFilledMessageToString.
@Test
public void testFilledMessageToString() throws Exception {
// toString() should never throw an exception or be null
DNSMessage.Builder message = DNSMessage.builder();
message.setOpcode(DNSMessage.OPCODE.QUERY);
message.setResponseCode(DNSMessage.RESPONSE_CODE.NO_ERROR);
message.setId(1337);
message.setAuthoritativeAnswer(true);
message.addQuestion(new Question("www.example.com", TYPE.A));
message.addAnswer(record("www.example.com", a("127.0.0.1")));
message.addNameserverRecords(record("example.com", ns("ns.example.com")));
message.addAdditionalResourceRecord(record("ns.example.com", a("127.0.0.1")));
message.getEdnsBuilder().setUdpPayloadSize(512);
assertNotNull(message.build().toString());
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class DNSMessageTest method testFilledMessageTerminalOutput.
@Test
public void testFilledMessageTerminalOutput() throws Exception {
// asTerminalOutput() follows a certain design, however it might change in the future.
// Once asTerminalOutput() is changed, it might be required to update this test routine.
DNSMessage.Builder message = DNSMessage.builder();
message.setOpcode(DNSMessage.OPCODE.QUERY);
message.setResponseCode(DNSMessage.RESPONSE_CODE.NO_ERROR);
message.setId(1337);
message.setAuthoritativeAnswer(true);
message.addQuestion(new Question("www.example.com", TYPE.A));
message.addAnswer(record("www.example.com", a("127.0.0.1")));
message.addNameserverRecords(record("example.com", ns("ns.example.com")));
message.addAdditionalResourceRecord(record("ns.example.com", a("127.0.0.1")));
message.getEdnsBuilder().setUdpPayloadSize(512);
assertNotNull(message.build().asTerminalOutput());
}
use of org.minidns.dnsmessage.Question in project minidns by MiniDNS.
the class DNSSECClient method verifyAnswer.
private Set<UnverifiedReason> verifyAnswer(DNSMessage dnsMessage) throws IOException {
Question q = dnsMessage.questions.get(0);
List<Record<? extends Data>> answers = dnsMessage.answerSection;
List<Record<? extends Data>> toBeVerified = dnsMessage.copyAnswers();
VerifySignaturesResult verifiedSignatures = verifySignatures(q, answers, toBeVerified);
Set<UnverifiedReason> result = verifiedSignatures.reasons;
if (!result.isEmpty()) {
return result;
}
// Keep SEPs separated, we only need one valid SEP.
boolean sepSignatureValid = false;
Set<UnverifiedReason> sepReasons = new HashSet<>();
for (Iterator<Record<? extends Data>> iterator = toBeVerified.iterator(); iterator.hasNext(); ) {
Record<DNSKEY> record = iterator.next().ifPossibleAs(DNSKEY.class);
if (record == null) {
continue;
}
// Verify all DNSKEYs as if it was a SEP. If we find a single SEP we are safe.
Set<UnverifiedReason> reasons = verifySecureEntryPoint(q, record);
if (reasons.isEmpty()) {
sepSignatureValid = true;
} else {
sepReasons.addAll(reasons);
}
if (!verifiedSignatures.sepSignaturePresent) {
LOGGER.finer("SEP key is not self-signed.");
}
iterator.remove();
}
if (verifiedSignatures.sepSignaturePresent && !sepSignatureValid) {
result.addAll(sepReasons);
}
if (verifiedSignatures.sepSignatureRequired && !verifiedSignatures.sepSignaturePresent) {
result.add(new NoSecureEntryPointReason(q.name.ace));
}
if (!toBeVerified.isEmpty()) {
if (toBeVerified.size() != answers.size()) {
throw new DNSSECValidationFailedException(q, "Only some records are signed!");
} else {
result.add(new NoSignaturesReason(q));
}
}
return result;
}
Aggregations