mirror of
https://github.com/soypat/lneto.git
synced 2026-08-20 14:39:02 +00:00
CopyFrom pattern for DNS and use it for Cookie
This commit is contained in:
@@ -91,6 +91,18 @@ func (c *Client) isClosed() bool {
|
||||
return c.state == dnsClosed || c.state == dnsAborted
|
||||
}
|
||||
|
||||
func (c *Client) MessageCopyTo(dst *Message) (done bool, err error) {
|
||||
if !c.respFlags.IsResponse() {
|
||||
return false, nil
|
||||
}
|
||||
dst.CopyFrom(c.msg)
|
||||
rcode := c.respFlags.ResponseCode()
|
||||
if rcode != 0 {
|
||||
return true, rcode
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (c *Client) Answers() []Resource {
|
||||
if c.state != dnsDone {
|
||||
return nil
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
// common errors. Taken from golang.org/x/net/dns/dnsmessage module.
|
||||
var (
|
||||
errNoResponse = errors.New("no DNS response")
|
||||
errNameTooLong = errors.New("DNS name exceeds maximum length")
|
||||
errNoNullTerm = errors.New("DNS name missing null terminator")
|
||||
errCalcLen = errors.New("DNS calculated name label length exceeds remaining buffer length")
|
||||
@@ -141,6 +142,10 @@ func (flags HeaderFlags) String() string {
|
||||
return string(flags.appendF(buf))
|
||||
}
|
||||
|
||||
func (flags RCode) Error() string {
|
||||
return flags.String()
|
||||
}
|
||||
|
||||
func (flags HeaderFlags) appendF(buf []byte) []byte {
|
||||
writeBit := func(b bool, s string) {
|
||||
if b {
|
||||
|
||||
+47
-2
@@ -245,7 +245,7 @@ func (m *Message) AddQuestions(questions []Question) {
|
||||
m.Questions = slices.Grow(m.Questions, len(questions))
|
||||
m.Questions = m.Questions[:qoff+len(questions)]
|
||||
for i := range questions {
|
||||
m.Questions[qoff+i].Name.CloneFrom(questions[i].Name)
|
||||
m.Questions[qoff+i].Name.CopyFrom(questions[i].Name)
|
||||
m.Questions[qoff+i].Type = questions[i].Type
|
||||
m.Questions[qoff+i].Class = questions[i].Class
|
||||
}
|
||||
@@ -418,7 +418,7 @@ func (n *Name) Len() uint16 {
|
||||
return uint16(len(n.data))
|
||||
}
|
||||
|
||||
func (n *Name) CloneFrom(ex Name) {
|
||||
func (n *Name) CopyFrom(ex Name) {
|
||||
n.data = append(n.data[:0], ex.data...)
|
||||
}
|
||||
|
||||
@@ -573,3 +573,48 @@ LOOP:
|
||||
}
|
||||
return newOff, nil
|
||||
}
|
||||
|
||||
func (dst *Message) CopyFrom(m Message) {
|
||||
reuseGrowSlice(&dst.Questions, len(m.Questions))
|
||||
reuseGrowSlice(&dst.Answers, len(m.Answers))
|
||||
reuseGrowSlice(&dst.Authorities, len(m.Authorities))
|
||||
reuseGrowSlice(&dst.Additionals, len(m.Additionals))
|
||||
for i := range dst.Questions {
|
||||
dst.Questions[i].CopyFrom(m.Questions[i])
|
||||
}
|
||||
for i := range dst.Answers {
|
||||
dst.Answers[i].CopyFrom(m.Answers[i])
|
||||
}
|
||||
for i := range dst.Answers {
|
||||
dst.Authorities[i].CopyFrom(m.Authorities[i])
|
||||
}
|
||||
for i := range dst.Answers {
|
||||
dst.Additionals[i].CopyFrom(m.Additionals[i])
|
||||
}
|
||||
}
|
||||
|
||||
func (dst *Question) CopyFrom(q Question) {
|
||||
dst.Name.CopyFrom(q.Name)
|
||||
dst.Class = q.Class
|
||||
dst.Type = q.Type
|
||||
}
|
||||
|
||||
func (dst *Resource) CopyFrom(r Resource) {
|
||||
dst.Header.CopyFrom(r.Header)
|
||||
dst.data = append(dst.data[:0], r.data...)
|
||||
}
|
||||
|
||||
func (dst *ResourceHeader) CopyFrom(rh ResourceHeader) {
|
||||
dst.Name.CopyFrom(rh.Name)
|
||||
dst.Type = rh.Type
|
||||
dst.Class = rh.Class
|
||||
dst.TTL = rh.TTL
|
||||
dst.Length = rh.Length
|
||||
}
|
||||
|
||||
func reuseGrowSlice[T any](dst *[]T, n int) {
|
||||
if n == 0 {
|
||||
return
|
||||
}
|
||||
*dst = slices.Grow(*dst, n)[:n]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user