mirror of
https://github.com/soypat/lneto.git
synced 2026-09-11 17:19:30 +00:00
dns: simplify @hnw function and additional fixes
This commit is contained in:
+8
-6
@@ -24,8 +24,11 @@ type ResolveConfig struct {
|
|||||||
Questions []Question
|
Questions []Question
|
||||||
Additional []Resource
|
Additional []Resource
|
||||||
EnableRecursion bool
|
EnableRecursion bool
|
||||||
MaxIPs uint16
|
// MaxResponseAnswers limits how many answer records are decoded from the
|
||||||
MaxCNAMEs uint16
|
// DNS response. If zero it defaults to the number of Questions. Answers
|
||||||
|
// are decoded in wire order regardless of type, so a response resolved
|
||||||
|
// through CNAMEs needs room for the CNAME records as well as the addresses.
|
||||||
|
MaxResponseAnswers uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sudp *Client) Protocol() uint64 { return uint64(lneto.IPProtoUDP) }
|
func (sudp *Client) Protocol() uint64 { return uint64(lneto.IPProtoUDP) }
|
||||||
@@ -39,11 +42,10 @@ func (c *Client) StartResolve(localPort, txid uint16, cfg ResolveConfig) error {
|
|||||||
if nd > math.MaxUint16 || nd == 0 {
|
if nd > math.MaxUint16 || nd == 0 {
|
||||||
return lneto.ErrInvalidConfig
|
return lneto.ErrInvalidConfig
|
||||||
}
|
}
|
||||||
maxIPs := cfg.MaxIPs
|
maxAns := cfg.MaxResponseAnswers
|
||||||
if maxIPs == 0 {
|
if maxAns == 0 {
|
||||||
maxIPs = uint16(nd)
|
maxAns = uint16(nd)
|
||||||
}
|
}
|
||||||
maxAns := maxIPs + cfg.MaxCNAMEs
|
|
||||||
c.reset(localPort, txid, CQueryPending, cfg.EnableRecursion)
|
c.reset(localPort, txid, CQueryPending, cfg.EnableRecursion)
|
||||||
c.msg.LimitResourceDecoding(uint16(nd), maxAns, 0, 0)
|
c.msg.LimitResourceDecoding(uint16(nd), maxAns, 0, 0)
|
||||||
c.msg.AddQuestions(cfg.Questions)
|
c.msg.AddQuestions(cfg.Questions)
|
||||||
|
|||||||
@@ -197,6 +197,8 @@ const (
|
|||||||
TypeALL Type = 255 // ALL
|
TypeALL Type = 255 // ALL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (tp Type) IsIPAddr() bool { return tp == TypeA || tp == TypeAAAA }
|
||||||
|
|
||||||
// A Class is a type of network.
|
// A Class is a type of network.
|
||||||
type Class uint16
|
type Class uint16
|
||||||
|
|
||||||
|
|||||||
+48
-62
@@ -99,6 +99,12 @@ func NamesEqual(a, b Name) bool {
|
|||||||
return internal.BytesEqual(a.data, b.data)
|
return internal.BytesEqual(a.data, b.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NamesEqualFold reports whether two DNS names are equal under ASCII case
|
||||||
|
// folding, which is how DNS labels compare per RFC 1035 section 2.3.3.
|
||||||
|
func NamesEqualFold(a, b Name) bool {
|
||||||
|
return internal.BytesEqualFoldASCII(a.data, b.data)
|
||||||
|
}
|
||||||
|
|
||||||
type ZFlags uint16
|
type ZFlags uint16
|
||||||
|
|
||||||
func NewResource(name Name, typ Type, class Class, ttl uint32, data []byte) Resource {
|
func NewResource(name Name, typ Type, class Class, ttl uint32, data []byte) Resource {
|
||||||
@@ -299,64 +305,55 @@ func (m *Message) AppendTo(buf []byte, txid uint16, flags HeaderFlags) (_ []byte
|
|||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteAnswers follows CNAMEs from host among the answers and writes the
|
// WriteAnswers writes the addresses answering host into dst, following the
|
||||||
// resulting addresses into dst.
|
// CNAME chain rooted at host. It returns the number of addresses written.
|
||||||
func (m *Message) WriteAnswers(dst []netip.Addr, host string) (n uint16, err error) {
|
func (m *Message) WriteAnswers(dst []netip.Addr, host string) (n uint16, err error) {
|
||||||
var alias Name
|
// Each round resolves one CNAME, which consumes an answer. Bounding the
|
||||||
|
// walk by the answer count is thus enough to reach the addresses, and
|
||||||
|
// terminates on cyclic chains.
|
||||||
|
var alias Name // Canonical name reached so far; zero means host itself.
|
||||||
for range m.Answers {
|
for range m.Answers {
|
||||||
var (
|
var next Name
|
||||||
next Name
|
|
||||||
hasAddrs bool
|
|
||||||
)
|
|
||||||
for i := range m.Answers {
|
for i := range m.Answers {
|
||||||
ans := &m.Answers[i]
|
ans := &m.Answers[i]
|
||||||
hdr := ans.Header()
|
if !ans.header.ownedBy(alias, host) {
|
||||||
if !hdr.pertainsTo(alias, host) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
switch hdr.Type {
|
switch {
|
||||||
case TypeA, TypeAAAA:
|
case ans.header.Type.IsIPAddr():
|
||||||
hasAddrs = true
|
if int(n) >= len(dst) {
|
||||||
case TypeCNAME:
|
return n, lneto.ErrExhausted
|
||||||
if cname, ok := ans.CNAMEView(); ok && cname.Len() != 0 {
|
}
|
||||||
|
addr, ok := netip.AddrFromSlice(ans.RawData())
|
||||||
|
if !ok {
|
||||||
|
err = lneto.ErrInvalidAddr
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dst[n] = addr
|
||||||
|
n++
|
||||||
|
case ans.header.Type == TypeCNAME:
|
||||||
|
if cname := ans.CNAMEView(); cname.Len() != 0 {
|
||||||
next = cname
|
next = cname
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if hasAddrs || next.Len() == 0 {
|
if n > 0 || next.Len() == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
alias = next
|
alias = next
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range m.Answers {
|
|
||||||
if int(n) >= len(dst) {
|
|
||||||
return n, lneto.ErrExhausted
|
|
||||||
}
|
|
||||||
ans := &m.Answers[i]
|
|
||||||
hdr := ans.Header()
|
|
||||||
isAddr := hdr.Type == TypeA || hdr.Type == TypeAAAA
|
|
||||||
if !isAddr || !hdr.pertainsTo(alias, host) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var ok bool
|
|
||||||
dst[n], ok = netip.AddrFromSlice(ans.RawData())
|
|
||||||
if !ok {
|
|
||||||
err = lneto.ErrInvalidAddr
|
|
||||||
} else {
|
|
||||||
n++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// pertainsTo reports whether the record's owner name is the given name: host
|
// ownedBy reports whether the record's owner name is the name being resolved:
|
||||||
// or one of the aliases resolved so far.
|
// the alias reached by following CNAMEs, or host at the root of the chain.
|
||||||
func (h *ResourceHeader) pertainsTo(alias Name, host string) bool {
|
func (h *ResourceHeader) ownedBy(alias Name, host string) bool {
|
||||||
if alias.Len() == 0 {
|
if alias.Len() == 0 {
|
||||||
return h.Name.EqualString(host)
|
return h.Name.EqualString(host)
|
||||||
}
|
}
|
||||||
return NamesEqual(h.Name, alias)
|
// Fold: the server chooses the case of both the CNAME target and the owner
|
||||||
|
// name of the records it aliases, and may randomize it (DNS 0x20).
|
||||||
|
return NamesEqualFold(h.Name, alias)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Message) Len() uint16 {
|
func (m *Message) Len() uint16 {
|
||||||
@@ -450,11 +447,13 @@ func (r *Resource) RawData() []byte {
|
|||||||
return r.data[:length]
|
return r.data[:length]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Resource) CNAMEView() (Name, bool) {
|
// CNAMEView returns the canonical name held by a CNAME record, aliasing the
|
||||||
if r.header.Type == TypeCNAME {
|
// Resource's buffer. It returns a zero Name for any other record type.
|
||||||
return Name{data: r.RawData()}, true
|
func (r *Resource) CNAMEView() Name {
|
||||||
|
if r.header.Type != TypeCNAME {
|
||||||
|
return Name{}
|
||||||
}
|
}
|
||||||
return Name{}, false
|
return Name{data: r.RawData()}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Question) Reset() {
|
func (q *Question) Reset() {
|
||||||
@@ -507,33 +506,20 @@ func (r *Resource) Decode(b []byte, off uint16) (uint16, error) {
|
|||||||
return off, errResourceLen
|
return off, errResourceLen
|
||||||
}
|
}
|
||||||
end := off + r.header.Length
|
end := off + r.header.Length
|
||||||
r.data = append(r.data[:0], b[off:end]...)
|
|
||||||
if r.header.Type == TypeCNAME {
|
if r.header.Type == TypeCNAME {
|
||||||
raw := b[off:end]
|
// CNAME data is a name which may use message compression. Expand it now
|
||||||
data, derr := expandName(r.data[:0], b, off)
|
// since r.data is detached from b, leaving pointers unresolvable later.
|
||||||
if derr == nil {
|
cname := Name{data: r.data[:0]}
|
||||||
r.data = data
|
if _, derr := cname.Decode(b, off); derr == nil {
|
||||||
|
r.data = cname.data
|
||||||
r.header.Length = uint16(len(r.data))
|
r.header.Length = uint16(len(r.data))
|
||||||
} else {
|
return end, nil
|
||||||
r.data = append(r.data[:0], raw...)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
r.data = append(r.data[:0], b[off:end]...)
|
||||||
return end, nil
|
return end, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func expandName(buf []byte, msg []byte, off uint16) ([]byte, error) {
|
|
||||||
appended := 0
|
|
||||||
_, err := visitAllLabels(msg, off, func(label []byte) {
|
|
||||||
buf = append(buf, byte(len(label)))
|
|
||||||
buf = append(buf, label...)
|
|
||||||
appended += 1 + len(label)
|
|
||||||
}, allowCompression)
|
|
||||||
if err != nil {
|
|
||||||
return buf[:len(buf)-appended], err
|
|
||||||
}
|
|
||||||
return append(buf, 0), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Resource) appendTo(buf []byte) (_ []byte, err error) {
|
func (r *Resource) appendTo(buf []byte) (_ []byte, err error) {
|
||||||
buf, err = r.header.appendTo(buf)
|
buf, err = r.header.appendTo(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+25
-9
@@ -268,9 +268,8 @@ func TestClient_CNAMEResponse(t *testing.T) {
|
|||||||
Type: TypeA,
|
Type: TypeA,
|
||||||
Class: ClassINET,
|
Class: ClassINET,
|
||||||
}},
|
}},
|
||||||
EnableRecursion: true,
|
EnableRecursion: true,
|
||||||
MaxIPs: 4,
|
MaxResponseAnswers: 6,
|
||||||
MaxCNAMEs: 2,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to start DNS resolve:", err)
|
t.Fatal("failed to start DNS resolve:", err)
|
||||||
@@ -342,6 +341,24 @@ func TestMessage_WriteAnswers(t *testing.T) {
|
|||||||
},
|
},
|
||||||
want: nil,
|
want: nil,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "CNAME target case differs from owner name",
|
||||||
|
host: "a.com",
|
||||||
|
// A server picks the case of both the CNAME target and the owner
|
||||||
|
// name of the record it aliases, and may randomize it (DNS 0x20),
|
||||||
|
// so the two must compare under ASCII case folding.
|
||||||
|
response: []byte{
|
||||||
|
// Header: txid 0xabcd, QR|RD|RA, QD=1 AN=2 NS=0 AR=0.
|
||||||
|
0xab, 0xcd, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
// Question: a.com A IN.
|
||||||
|
0x01, 'a', 0x03, 'c', 'o', 'm', 0x00, 0x00, 0x01, 0x00, 0x01,
|
||||||
|
// Answer 1: a.com CNAME B.CoM.
|
||||||
|
0x01, 'a', 0x03, 'c', 'o', 'm', 0x00, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x07, 0x01, 'B', 0x03, 'C', 'o', 'M', 0x00,
|
||||||
|
// Answer 2: b.com A IN ttl=10 rdlen=4 1.2.3.4.
|
||||||
|
0x01, 'b', 0x03, 'c', 'o', 'm', 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x04, 0x01, 0x02, 0x03, 0x04,
|
||||||
|
},
|
||||||
|
want: []netip.Addr{netip.AddrFrom4([4]byte{1, 2, 3, 4})},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@@ -372,7 +389,7 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
|
|||||||
const hostname = "example.com"
|
const hostname = "example.com"
|
||||||
const txid = uint16(12345)
|
const txid = uint16(12345)
|
||||||
const clientPort = uint16(54321)
|
const clientPort = uint16(54321)
|
||||||
const maxIPs = 4
|
const maxAnswers = 4
|
||||||
allIPs := [5][4]byte{
|
allIPs := [5][4]byte{
|
||||||
{192, 0, 2, 1},
|
{192, 0, 2, 1},
|
||||||
{192, 0, 2, 2},
|
{192, 0, 2, 2},
|
||||||
@@ -387,7 +404,7 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{name: "single_answer", responseIPs: allIPs[:1], wantAnswers: 1},
|
{name: "single_answer", responseIPs: allIPs[:1], wantAnswers: 1},
|
||||||
{name: "multiple_answers", responseIPs: allIPs[:4], wantAnswers: 4},
|
{name: "multiple_answers", responseIPs: allIPs[:4], wantAnswers: 4},
|
||||||
{name: "answer_limit", responseIPs: allIPs[:5], wantAnswers: maxIPs},
|
{name: "answer_limit", responseIPs: allIPs[:5], wantAnswers: maxAnswers},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
@@ -419,9 +436,8 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
|
|||||||
Type: TypeA,
|
Type: TypeA,
|
||||||
Class: ClassINET,
|
Class: ClassINET,
|
||||||
}},
|
}},
|
||||||
EnableRecursion: true,
|
EnableRecursion: true,
|
||||||
MaxIPs: maxIPs,
|
MaxResponseAnswers: maxAnswers,
|
||||||
MaxCNAMEs: 0, // No CNAME records in this response.
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to start DNS resolve:", err)
|
t.Fatal("failed to start DNS resolve:", err)
|
||||||
@@ -437,7 +453,7 @@ func TestClient_ReceivesDNSResponse(t *testing.T) {
|
|||||||
t.Fatal("failed to demux DNS response:", err)
|
t.Fatal("failed to demux DNS response:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var addrs [maxIPs]netip.Addr
|
var addrs [maxAnswers]netip.Addr
|
||||||
answers, err := client.ResponseAnswerLookup(addrs[:], hostname)
|
answers, err := client.ResponseAnswerLookup(addrs[:], hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("failed to look up DNS response answers:", err)
|
t.Fatal("failed to look up DNS response answers:", err)
|
||||||
|
|||||||
@@ -300,25 +300,7 @@ func (kvb *kvBuffer) getFoldIdx(key string) int {
|
|||||||
// EqualFoldASCII reports whether a and b are equal under ASCII case folding.
|
// EqualFoldASCII reports whether a and b are equal under ASCII case folding.
|
||||||
// Unlike strings.EqualFold it does not fold non-ASCII runes, so no multi-byte
|
// Unlike strings.EqualFold it does not fold non-ASCII runes, so no multi-byte
|
||||||
// rune such as U+212A KELVIN SIGN can alias a header key.
|
// rune such as U+212A KELVIN SIGN can alias a header key.
|
||||||
func EqualFoldASCII(a, b string) bool {
|
func EqualFoldASCII(a, b string) bool { return internal.EqualFoldASCII(a, b) }
|
||||||
if len(a) != len(b) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
const asciiCapDiff = 'a' - 'A'
|
|
||||||
for i := 0; i < len(a); i++ {
|
|
||||||
ca, cb := a[i], b[i]
|
|
||||||
if ca >= 'A' && ca <= 'Z' {
|
|
||||||
ca += asciiCapDiff
|
|
||||||
}
|
|
||||||
if cb >= 'A' && cb <= 'Z' {
|
|
||||||
cb += asciiCapDiff
|
|
||||||
}
|
|
||||||
if ca != cb {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// reserve ensures need free bytes are available in the buffer, growing it when
|
// reserve ensures need free bytes are available in the buffer, growing it when
|
||||||
// permitted. It accounts for the byte-0 reservation on an empty buffer (see
|
// permitted. It accounts for the byte-0 reservation on an empty buffer (see
|
||||||
|
|||||||
@@ -253,20 +253,7 @@ func trimOWS(b []byte) []byte {
|
|||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
// equalFold compares b to the ASCII lowercase key, case insensitively.
|
// equalFold compares b to key, case insensitively.
|
||||||
func equalFold(b []byte, key string) bool {
|
func equalFold(b []byte, key string) bool {
|
||||||
if len(b) != len(key) {
|
return EqualFoldASCII(b2s(b), key)
|
||||||
return false
|
|
||||||
}
|
|
||||||
const asciiCapDiff = 'a' - 'A'
|
|
||||||
for i := range b {
|
|
||||||
c := b[i]
|
|
||||||
if c >= 'A' && c <= 'Z' {
|
|
||||||
c += asciiCapDiff
|
|
||||||
}
|
|
||||||
if c != key[i] {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,40 @@ func BytesEqual(a, b []byte) bool {
|
|||||||
return unsafe.String(&a[0], len(a)) == unsafe.String(&b[0], len(b))
|
return unsafe.String(&a[0], len(a)) == unsafe.String(&b[0], len(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EqualFoldASCII reports whether a and b are equal under ASCII case folding.
|
||||||
|
// Unlike [strings.EqualFold] it does not fold non-ASCII runes, so no multi-byte
|
||||||
|
// rune such as U+212A KELVIN SIGN can alias an ASCII key.
|
||||||
|
func EqualFoldASCII(a, b string) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
const asciiCapDiff = 'a' - 'A'
|
||||||
|
for i := 0; i < len(a); i++ {
|
||||||
|
ca, cb := a[i], b[i]
|
||||||
|
if ca >= 'A' && ca <= 'Z' {
|
||||||
|
ca += asciiCapDiff
|
||||||
|
}
|
||||||
|
if cb >= 'A' && cb <= 'Z' {
|
||||||
|
cb += asciiCapDiff
|
||||||
|
}
|
||||||
|
if ca != cb {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// BytesEqualFoldASCII is the []byte form of [EqualFoldASCII]. Like [BytesEqual]
|
||||||
|
// it is heapless in tinygo, unlike [bytes.EqualFold] which also folds non-ASCII.
|
||||||
|
func BytesEqualFoldASCII(a, b []byte) bool {
|
||||||
|
if len(a) != len(b) {
|
||||||
|
return false
|
||||||
|
} else if len(a) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return EqualFoldASCII(unsafe.String(&a[0], len(a)), unsafe.String(&b[0], len(b)))
|
||||||
|
}
|
||||||
|
|
||||||
// IsZeroed returns true if all arguments are set to their zero value.
|
// IsZeroed returns true if all arguments are set to their zero value.
|
||||||
func IsZeroed[T comparable](a ...T) bool {
|
func IsZeroed[T comparable](a ...T) bool {
|
||||||
var z T
|
var z T
|
||||||
|
|||||||
@@ -630,8 +630,9 @@ func (s *StackAsync) StartLookupIPType(host string, qtype dns.Type) error {
|
|||||||
s.ednsopt,
|
s.ednsopt,
|
||||||
},
|
},
|
||||||
EnableRecursion: true,
|
EnableRecursion: true,
|
||||||
MaxIPs: uint16(len(s.addrbufnip)),
|
// Leave headroom above the address buffer for CNAME records, which
|
||||||
MaxCNAMEs: 8,
|
// occupy answer slots before the addresses they alias.
|
||||||
|
MaxResponseAnswers: uint16(len(s.addrbufnip)) + 8,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
Reference in New Issue
Block a user