Skip to content

Commit

Permalink
dnsforward: imp docs
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed May 27, 2021
1 parent 1af7131 commit 7975957
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 9 additions & 5 deletions internal/dnsforward/recursiondetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghnet"
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/AdguardTeam/golibs/cache"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)

// uint* sizes in bytes to improve readability.
//
// TODO(e.burkov): Remove when there will be a more regardful way to define
// those. See https://github.com/golang/go/issues/29982.
const (
uint16sz = 2
uint64sz = 8
Expand All @@ -36,7 +38,7 @@ func (rd *recursionDetector) check(msg dns.Msg) (ok bool) {
return false
}

expire := time.Unix(0, int64(aghos.NativeEndian.Uint64(expireData)))
expire := time.Unix(0, int64(binary.BigEndian.Uint64(expireData)))

return time.Now().Before(expire)
}
Expand All @@ -52,7 +54,7 @@ func (rd *recursionDetector) add(msg dns.Msg) {
key := msgToSignature(msg)
expire64 := uint64(now.Add(rd.ttl).UnixNano())
expire := make([]byte, uint64sz)
aghos.NativeEndian.PutUint64(expire, expire64)
binary.BigEndian.PutUint64(expire, expire64)

rd.recentRequests.Set(key, expire)
}
Expand All @@ -76,7 +78,9 @@ func newRecursionDetector(ttl time.Duration, suspectsNum uint) (rd *recursionDet
// msgToSignature converts msg into it's signature represented in bytes.
func msgToSignature(msg dns.Msg) (sig []byte) {
sig = make([]byte, uint16sz*2+aghnet.MaxDomainNameLen)
byteOrder := aghos.NativeEndian
// The binary.BigEndian byte order is used everywhere except when the
// real machine's endianess is needed.
byteOrder := binary.BigEndian
byteOrder.PutUint16(sig[0:], msg.Id)
q := msg.Question[0]
byteOrder.PutUint16(sig[uint16sz:], q.Qtype)
Expand All @@ -103,7 +107,7 @@ func msgToSignatureSlow(msg dns.Msg) (sig []byte) {
qtype: q.Qtype,
}
copy(signature.name[:], q.Name)
if err := binary.Write(b, aghos.NativeEndian, signature); err != nil {
if err := binary.Write(b, binary.BigEndian, signature); err != nil {
log.Debug("writing message signature: %s", err)
}

Expand Down
6 changes: 3 additions & 3 deletions internal/dnsforward/recursiondetector_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dnsforward

import (
"encoding/binary"
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/aghos"
"github.com/miekg/dns"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -33,10 +33,10 @@ func TestRecursionDetector_Check(t *testing.T) {
// Manually add the message with big ttl.
key := msgToSignature(sampleMsg)
expire := make([]byte, uint64sz)
aghos.NativeEndian.PutUint64(expire, uint64(time.Now().Add(recTTL).UnixNano()))
binary.BigEndian.PutUint64(expire, uint64(time.Now().Add(recTTL).UnixNano()))
rd.recentRequests.Set(key, expire)

// Add an expired message cause
// Add an expired message.
sampleMsg.Id = nonRecID
rd.add(sampleMsg)

Expand Down

0 comments on commit 7975957

Please sign in to comment.