refactor(dns): eagerly decode CNAME target into r.data, drop target field

Address review feedback on #189:

- Remove Resource.target: Resource.Decode expands CNAME RDATA in place
  into r.data (uncompressed wire format) while the full message is still
  available and updates header.Length, storing the name bytes exactly once.
- Add Resource.CNAMEView returning a length-bounded view of the expanded
  target; Message.WriteAnswers uses it.
- Rename matchesHost to ResourceHeader.pertainsTo.
- Make ResolveConfig.MaxCNAMEs explicit: drop the silent default in
  Client.StartResolve and let callers opt in (x/xnet).
- Reduce test suite to regression coverage only.

refs #189
This commit is contained in:
Yoshio HANAWA
2026-08-27 09:46:00 +09:00
parent a33e9c5fab
commit 8c49548440
4 changed files with 81 additions and 71 deletions
+10 -33
View File
@@ -296,8 +296,8 @@ func TestClient_CNAMEResponse(t *testing.T) {
}
}
// Table-driven tests for Message.WriteAnswers covering answer reordering,
// non-address record types and cyclic CNAME aliases.
// Table-driven tests for Message.WriteAnswers covering answer reordering
// and cyclic CNAME aliases.
func TestMessage_WriteAnswers(t *testing.T) {
tests := []struct {
name string
@@ -327,22 +327,6 @@ func TestMessage_WriteAnswers(t *testing.T) {
},
want: []netip.Addr{netip.AddrFrom4([4]byte{182, 22, 23, 124})},
},
{
name: "ignores non-address records",
host: "example.com",
response: []byte{
// Header: txid 0x5678, QR|RD|RA, QD=1 AN=2 NS=0 AR=0.
0x56, 0x78, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
// Question: example.com A IN.
0x07, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0x03, 'c', 'o', 'm', 0x00,
0x00, 0x01, 0x00, 0x01,
// Answer 1: (ptr to question) TXT IN ttl=60 rdlen=6 "hello".
0xc0, 0x0c, 0x00, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x06, 0x05, 'h', 'e', 'l', 'l', 'o',
// Answer 2: (ptr to question) A IN ttl=300 rdlen=4 192.0.2.1.
0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0xc0, 0x00, 0x02, 0x01,
},
want: []netip.Addr{netip.AddrFrom4([4]byte{192, 0, 2, 1})},
},
{
name: "CNAME cycle terminates",
host: "a.com",
@@ -389,28 +373,21 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
const txid = uint16(12345)
const clientPort = uint16(54321)
const maxIPs = 4
const maxCNAMEs = 2
const maxDecoded = maxIPs + maxCNAMEs
allIPs := [8][4]byte{
allIPs := [5][4]byte{
{192, 0, 2, 1},
{192, 0, 2, 2},
{192, 0, 2, 3},
{192, 0, 2, 4},
{192, 0, 2, 5},
{192, 0, 2, 6},
{192, 0, 2, 7},
{192, 0, 2, 8},
}
tests := []struct {
name string
responseIPs [][4]byte
wantDecoded int // Answers decoded (and copied by ResponseCopyTo).
wantAnswers int // Addresses returned by ResponseAnswerLookup.
wantAnswers int // Addresses returned by ResponseAnswerLookup and copied by ResponseCopyTo.
}{
{name: "single_answer", responseIPs: allIPs[:1], wantDecoded: 1, wantAnswers: 1},
{name: "multiple_answers", responseIPs: allIPs[:4], wantDecoded: 4, wantAnswers: 4},
{name: "answer_limit", responseIPs: allIPs[:5], wantDecoded: 5, wantAnswers: maxIPs},
{name: "decode_limit", responseIPs: allIPs[:8], wantDecoded: maxDecoded, wantAnswers: maxIPs},
{name: "single_answer", responseIPs: allIPs[:1], wantAnswers: 1},
{name: "multiple_answers", responseIPs: allIPs[:4], wantAnswers: 4},
{name: "answer_limit", responseIPs: allIPs[:5], wantAnswers: maxIPs},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -444,7 +421,7 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
}},
EnableRecursion: true,
MaxIPs: maxIPs,
MaxCNAMEs: maxCNAMEs,
MaxCNAMEs: 0, // No CNAME records in this response.
})
if err != nil {
t.Fatal("failed to start DNS resolve:", err)
@@ -487,8 +464,8 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
if !done {
t.Fatal("expected done=true")
}
if len(lookup.Answers) != tt.wantDecoded {
t.Fatalf("expected %d copied answers, got %d", tt.wantDecoded, len(lookup.Answers))
if len(lookup.Answers) != tt.wantAnswers {
t.Fatalf("expected %d copied answers, got %d", tt.wantAnswers, len(lookup.Answers))
}
})
}