Skip to content

Commit

Permalink
Use correct string format IPv4 encoded as IPv6 for for AAAA records (#…
Browse files Browse the repository at this point in the history
…1457)

In order to parse an IPv4 address encoded as IPv6 it needs to have the
prefix of "::ffff:" i.e. ::ffff:192.0.2.1

Co-authored-by: Christian Elmerot <elmerot@cloudflare.com>
  • Loading branch information
Chreo and Christian Elmerot committed Sep 12, 2023
1 parent a36acb4 commit d00c672
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
27 changes: 27 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,33 @@ func TestNULLRecord(t *testing.T) {
t.Fatalf("Expected packet to contain NULL record")
}
}
func TestAAAAParsing(t *testing.T) {
tests := []string{
"2001:db8::1",
"1::1",
"2001:db81:d2b4:b6ba:50db:49cc:a8d1:5bb1",
"::ffff:192.0.2.0",
}

rrPrefix := ".\t1\tIN\tAAAA\t"

for num, tc := range tests {
t.Run(fmt.Sprintf("Test %d", num), func(t *testing.T) {
rr, err := NewRR(rrPrefix + tc)
if err != nil {
t.Fatalf("failed to parse RR: %s", err)
}
// Output presentation format and try to parse again
reparseRR, err := NewRR(rr.String())
if err != nil {
t.Fatalf("failed to reparse RR: %s", err)
}
if reparseRR.String() != rrPrefix+tc {
t.Errorf("expected %s,got %s", rrPrefix+tc, reparseRR.String())
}
})
}
}

func TestParseAPL(t *testing.T) {
tests := []struct {
Expand Down
10 changes: 9 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ var CertTypeToString = map[uint16]string{
CertOID: "OID",
}

// Prefix for IPv4 encoded as IPv6 address
const ipv4InIPv6Prefix = "::ffff:"

//go:generate go run types_generate.go

// Question holds a DNS question. Usually there is just one. While the
Expand Down Expand Up @@ -751,6 +754,11 @@ func (rr *AAAA) String() string {
if rr.AAAA == nil {
return rr.Hdr.String()
}

if rr.AAAA.To4() != nil {
return rr.Hdr.String() + ipv4InIPv6Prefix + rr.AAAA.String()
}

return rr.Hdr.String() + rr.AAAA.String()
}

Expand Down Expand Up @@ -1517,7 +1525,7 @@ func (a *APLPrefix) str() string {
case net.IPv6len:
// add prefix for IPv4-mapped IPv6
if v4 := a.Network.IP.To4(); v4 != nil {
sb.WriteString("::ffff:")
sb.WriteString(ipv4InIPv6Prefix)
}
sb.WriteString(a.Network.IP.String())
}
Expand Down

0 comments on commit d00c672

Please sign in to comment.