Skip to content

Commit e521835

Browse files
authored
DPS Solvers: Fixing Getting Non-authoritative answer when unexpected (#669)
1 parent b28b98e commit e521835

File tree

14 files changed

+175
-35
lines changed

14 files changed

+175
-35
lines changed

RELEASE-NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
## 5.6.1
1+
## 5.6.2
2+
* Fixing Getting Non-authoritative answer when unexpected, see #608.
23
* Fixing Getting NXDOMAIN when not expected, see #668.
34

45
## 5.6.0

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=5.6.1-snapshot
1+
version=5.6.2-snapshot

src/main/java/com/mageddo/dns/utils/Messages.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ private Messages() {
3636
public static final long DEFAULT_TTL = 30L;
3737
public static final Duration DEFAULT_TTL_DURATION = Duration.ofSeconds(DEFAULT_TTL);
3838

39+
public static Message authoritative(Message m) {
40+
setFlag(m, Flags.AA);
41+
return m;
42+
}
43+
3944
public static String simplePrint(Response res) {
4045
return simplePrint(res.getMessage());
4146
}
@@ -318,6 +323,7 @@ public static HostnameQuery toHostnameQuery(Message query) {
318323
return HostnameQuery.of(host, version);
319324
}
320325

326+
321327
public static boolean isSuccess(Message res) {
322328
return res.getRcode() == Rcode.NOERROR;
323329
}
@@ -333,4 +339,20 @@ public static boolean isNxDomain(Message m) {
333339
public static Message noData(Message query) {
334340
return withResponseCode(query.clone(), Rcode.NOERROR);
335341
}
342+
343+
public static Message authoritativeAnswer(Message query, String ip, IP.Version version) {
344+
return authoritative(answer(query, ip, version));
345+
}
346+
347+
public static Message authoritativeAnswer(Message query, String ip, IP.Version version, long ttl) {
348+
return authoritative(answer(query, ip, version, ttl));
349+
}
350+
351+
public static boolean isAuthoritative(Message m) {
352+
return Messages.hasFlag(m, Flags.AA);
353+
}
354+
355+
public static boolean isRecursionAvailable(Message m) {
356+
return hasFlag(m, Flags.RA);
357+
}
336358
}

src/main/java/com/mageddo/dnsproxyserver/solver/ResponseMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
public class ResponseMapper {
1010
public static Response toDefaultSuccessAnswer(Message query, IP ip, IP.Version version) {
1111
return Response.of(
12-
Messages.answer(query, Objects.mapOrNull(ip, IP::toText), version),
12+
Messages.authoritativeAnswer(query, Objects.mapOrNull(ip, IP::toText), version),
1313
Messages.DEFAULT_TTL_DURATION
1414
);
1515
}
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
package com.mageddo.dnsproxyserver.solver;
22

3+
import com.mageddo.dns.utils.Messages;
4+
35
public class Responses {
46
public static boolean hasFlag(Response r, int flag) {
57
if (r == null) {
68
return false;
79
}
8-
return r.getMessage()
9-
.getHeader()
10-
.getFlag(flag);
10+
return Messages.hasFlag(r.getMessage(), flag);
11+
}
12+
13+
public static boolean isAuthoritative(Response res) {
14+
return Messages.isAuthoritative(res.getMessage());
15+
}
16+
17+
public static boolean isRecursionAvailable(Response res) {
18+
return Messages.isRecursionAvailable(res.getMessage());
19+
}
20+
21+
public static boolean isSuccess(Response res) {
22+
return Messages.isSuccess(res.getMessage());
1123
}
1224
}

src/main/java/com/mageddo/dnsproxyserver/solver/SolverDocker.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Response handle(Message query) {
3131
}
3232

3333
final var type = Messages.findQuestionType(query);
34-
if (ConfigEntryTypes.isNot(type, Type.AAAA, Type.A)) {
34+
if (isNotSupported(type)) {
3535
log.trace("status=unsupportedType, type={}", type);
3636
return null;
3737
}
@@ -43,7 +43,7 @@ public Response handle(Message query) {
4343
if (!entry.isHostnameMatched()) {
4444
return null;
4545
}
46-
return Response.internalSuccess(Messages.answer(
46+
return Response.internalSuccess(Messages.authoritativeAnswer(
4747
query,
4848
entry.getIpText(),
4949
hostname.getVersion()
@@ -53,4 +53,8 @@ public Response handle(Message query) {
5353

5454
}
5555

56+
private static boolean isNotSupported(Type type) {
57+
return ConfigEntryTypes.isNot(type, Type.AAAA, Type.A);
58+
}
59+
5660
}

src/main/java/com/mageddo/dnsproxyserver/solver/SolverLocalDB.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public Response handle(Message query) {
3737
final var stopWatch = StopWatch.createStarted();
3838

3939
final var type = Messages.findQuestionTypeCode(query);
40-
if (ConfigEntryTypes.isNot(type, Type.A, Type.CNAME, Type.AAAA)) {
41-
log.debug("status=typeNotSupported, action=continue, type={}, time={}", type,
42-
stopWatch.getTime()
40+
if (isNotSupported(type)) {
41+
log.debug(
42+
"status=typeNotSupported, action=continue, type={}, time={}", type, stopWatch.getTime()
4343
);
4444
return null;
4545
}
@@ -66,7 +66,7 @@ public Response handle(Message query) {
6666
if (foundType.isAddressSolving()) {
6767
final var ip = foundType.equals(questionType) ? found.requireTextIp() : null;
6868
return Response.of(
69-
Messages.answer(query, ip, questionType.toVersion(), found.getTtl()),
69+
Messages.authoritativeAnswer(query, ip, questionType.toVersion(), found.getTtl()),
7070
Duration.ofSeconds(found.getTtl())
7171
);
7272
}
@@ -81,6 +81,10 @@ public Response handle(Message query) {
8181
return null;
8282
}
8383

84+
private static boolean isNotSupported(Integer type) {
85+
return ConfigEntryTypes.isNot(type, Type.A, Type.CNAME, Type.AAAA);
86+
}
87+
8488
@Override
8589
public String name() {
8690
return NAME;

src/main/java/com/mageddo/dnsproxyserver/solver/SolverSystem.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.mageddo.dnsproxyserver.config.ConfigEntryTypes;
99
import com.mageddo.dnsproxyserver.config.application.Configs;
1010
import com.mageddo.dnsproxyserver.usecase.HostMachineService;
11+
import com.mageddo.net.IP;
1112

1213
import org.xbill.DNS.Message;
1314

@@ -38,11 +39,15 @@ public Response handle(Message query) {
3839
final var config = Configs.getInstance();
3940
// fixme fazer case com hostname + search domain
4041
if (hostname.isEqualTo(config.getHostMachineHostname())) {
41-
final var ip = this.machineService.findHostMachineIP(questionType.toVersion());
42+
final var ip = this.findHostMachineIP(questionType.toVersion());
4243
log.debug("status=solvingHostMachineName, host={}, ip={}", hostname, ip);
4344
return ResponseMapper.toDefaultSuccessAnswer(query, ip, questionType.toVersion());
4445
}
4546
return null;
4647
}
4748

49+
IP findHostMachineIP(IP.Version version) {
50+
return this.machineService.findHostMachineIP(version);
51+
}
52+
4853
}

src/main/java/com/mageddo/dnsproxyserver/solver/stub/SolverStub.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
@AllArgsConstructor(onConstructor_ = @Inject)
2929
public class SolverStub implements Solver {
3030

31-
public static final String DOMAIN_NAME = "stub";
32-
3331
@Override
3432
public Response handle(Message query) {
3533
final var questionType = Messages.findQuestionType(query);
@@ -51,14 +49,15 @@ public Response handle(Message query) {
5149
log.debug("status=notSolved, hostname={}", hostname);
5250
return null;
5351
}
54-
if (!foundIp.versionIs(questionType.toVersion())) {
55-
log.debug("status=incompatibleIpAndQueryType, hostname={}, questionType={}", hostname,
56-
questionType
57-
);
58-
return Response.nxDomain(query);
59-
}
60-
log.debug("status=solved, host={}, ip={}", hostname, foundIp);
61-
return ResponseMapper.toDefaultSuccessAnswer(query, foundIp, questionType.toVersion());
52+
final var qTypeVersion = questionType.toVersion();
53+
final var sameVersion = foundIp.versionIs(qTypeVersion);
54+
log.debug(
55+
"status=solved, host={}, ip={}, qTypeVersion={}",
56+
hostname, foundIp, qTypeVersion
57+
);
58+
return ResponseMapper.toDefaultSuccessAnswer(
59+
query, sameVersion ? foundIp : null, qTypeVersion
60+
);
6261
}
6362

6463
String findDomainName() {

src/test/java/com/mageddo/dnsproxyserver/solver/SolverDockerTest.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.mockito.Mock;
1515
import org.mockito.junit.jupiter.MockitoExtension;
1616
import org.xbill.DNS.Flags;
17-
import org.xbill.DNS.Rcode;
1817

1918
import testing.templates.HostnameTemplates;
2019
import testing.templates.MessageTemplates;
@@ -70,9 +69,15 @@ void mustSolveExactHostname() {
7069
assertNotNull(res);
7170

7271
final var resText = res.toString();
73-
assertTrue(resText.contains(entry.getIp()
74-
.toText()), resText
72+
assertTrue(
73+
resText.contains(
74+
entry.getIp()
75+
.toText()
76+
),
77+
resText
7578
);
79+
assertTrue(Responses.isSuccess(res));
80+
assertTrue(Responses.isAuthoritative(res));
7681
verify(this.containerSolvingService).findBestMatch(hostname);
7782
}
7883

@@ -96,7 +101,8 @@ void mustSolveQuadARecordQuery() {
96101

97102
// assert
98103
assertNotNull(res);
99-
assertTrue(Responses.hasFlag(res, Flags.RA));
104+
assertTrue(Responses.isRecursionAvailable(res));
105+
assertTrue(Responses.isAuthoritative(res));
100106
final var resText = res.toString();
101107
assertTrue(resText.contains(entry.getIp()
102108
.toText()), resText
@@ -128,13 +134,13 @@ void mustSolveEmptyIpWhenHostnameMatchesButNoIpIsFound() {
128134
// assert
129135
assertNotNull(res);
130136
assertTrue(Responses.hasFlag(res, Flags.RA));
131-
assertEquals(Rcode.NOERROR, res.getRCode());
137+
assertTrue(Responses.isSuccess(res));
132138
assertEquals(Type.AAAA, Messages.findQuestionType(res.getMessage()));
133139
assertEquals("", Messages.detailedPrint(res.getMessage()));
134140
}
135141

136142
@Test
137-
void mustReturnNxDomainWhenHostnameDONTMatches() {
143+
void mustReturnNullWhenHostnameDONTMatches() {
138144
// arrange
139145
final var query = MessageTemplates.acmeQuadAQuery();
140146
final var entry = EntryTemplates.hostnameNotMatched();
@@ -155,4 +161,6 @@ void mustReturnNxDomainWhenHostnameDONTMatches() {
155161
assertNull(res);
156162
}
157163

164+
165+
158166
}

0 commit comments

Comments
 (0)