Skip to content
Prev Previous commit
Next Next commit
make the format checker happy (again!).
  • Loading branch information
ka2ddo committed Mar 13, 2021
commit 31f26216f5627117cc3b4e398381095dc626be1e
87 changes: 45 additions & 42 deletions src/main/java/org/xbill/DNS/SimpleResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,48 +355,48 @@ CompletableFuture<Message> sendAsync(Message query, boolean forceTcp) {
CompletableFuture<Message> f = new CompletableFuture<>();

VerifyOneResponse verifyOneResponse = new VerifyOneResponse(query, qid);
@SuppressWarnings("unchecked")
Message response = null;
Throwable responseFailureException = null;
for (byte[] in : v) {
Message r = verifyOneResponse.parse(in);
if (r == null) {
responseFailureException = verifyOneResponse.getFailureReason();
continue;
}

verifyTSIG(query, r, in, tsig);
if (!tcp && !ignoreTruncation && r.getHeader().getFlag(Flags.TC)) {
log.debug("Got truncated response for id {}, discarding`", qid);
log.trace("Truncated response: {}", r);
continue;
}
@SuppressWarnings("unchecked")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer necessary

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Message response = null;
Throwable responseFailureException = null;
for (byte[] in : v) {
Message r = verifyOneResponse.parse(in);
if (r == null) {
responseFailureException = verifyOneResponse.getFailureReason();
continue;
}

if (response == null) {
response = r;
} else {
// not the first answer, append the results to first response
for (Record rec : r.getSection(Section.ANSWER)) {
response.addRecord(rec, Section.ANSWER);
}
for (Record rec : r.getSection(Section.AUTHORITY)) {
response.addRecord(rec, Section.AUTHORITY);
}
for (Record rec : r.getSection(Section.ADDITIONAL)) {
response.addRecord(rec, Section.ADDITIONAL);
}
}
verifyTSIG(query, r, in, tsig);
if (!tcp && !ignoreTruncation && r.getHeader().getFlag(Flags.TC)) {
log.debug("Got truncated response for id {}, discarding`", qid);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the fallback to TCP in normal DNS lookups?

log.trace("Truncated response: {}", r);
continue;
}

if (response != null) {
response.setResolver(this);
f.complete(response);
if (response == null) {
response = r;
} else {
if (responseFailureException == null) {
responseFailureException = new SocketTimeoutException("Query timed out");
// not the first answer, append the results to first response
for (Record rec : r.getSection(Section.ANSWER)) {
response.addRecord(rec, Section.ANSWER);
}
for (Record rec : r.getSection(Section.AUTHORITY)) {
response.addRecord(rec, Section.AUTHORITY);
}
for (Record rec : r.getSection(Section.ADDITIONAL)) {
response.addRecord(rec, Section.ADDITIONAL);
}
f.completeExceptionally(responseFailureException);
}
}

if (response != null) {
response.setResolver(this);
f.complete(response);
} else {
if (responseFailureException == null) {
responseFailureException = new SocketTimeoutException("Query timed out");
}
f.completeExceptionally(responseFailureException);
}
return f;
});
}
Expand Down Expand Up @@ -438,10 +438,11 @@ public VerifyOneResponse(Message query, int qid) {
}

/**
* Keep the exception that caused this to fail. We don't throw the exception
* immediately because, when using mDNS, we might get both failing and successful
* answers, in which case we just want to discard the bad answers and keep the
* good ones; the exception only gets thrown if only bad answers are received.
* Keep the exception that caused this to fail. We don't throw the exception immediately
* because, when using mDNS, we might get both failing and successful answers, in which case we
* just want to discard the bad answers and keep the good ones; the exception only gets thrown
* if only bad answers are received.
*
* @return the Throwable reporting why this parse failed.
*/
public Throwable getFailureReason() {
Expand All @@ -463,7 +464,8 @@ public Message parse(byte[] in) {
// doesn't confuse us.
int id = ((in[0] & 0xFF) << 8) + (in[1] & 0xFF);
if (id != qid) {
failureReasonException = new WireParseException("invalid message id: expected " + qid + "; got id " + id);
failureReasonException =
new WireParseException("invalid message id: expected " + qid + "; got id " + id);
return null;
}

Expand All @@ -486,7 +488,8 @@ public Message parse(byte[] in) {
return null;
}

if ((query.getQuestion().getDClass() & ~DClass.UNICAST_RESPONSE) != (r.getQuestion().getDClass() & ~DClass.UNICAST_RESPONSE)) {
if ((query.getQuestion().getDClass() & ~DClass.UNICAST_RESPONSE)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This masking must only be applied for mDNS, not regular DNS
  • In the mDNS case, the mask should be 0x7FFF so that 32768 to 65535 is ignored (see the note at IANA or in the RFC)

!= (r.getQuestion().getDClass() & ~DClass.UNICAST_RESPONSE)) {
failureReasonException =
new WireParseException(
"invalid class in message: expected "
Expand Down