Skip to content

Commit

Permalink
feat: add invalid threshold to DNS analyzer for UDP offloading
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyxdd committed Jan 20, 2024
1 parent 25241c9 commit f986462
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions analyzer/udp/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/google/gopacket/layers"
)

const (
dnsUDPInvalidCountThreshold = 4
)

// DNSAnalyzer is for both DNS over UDP and TCP.
var (
_ analyzer.UDPAnalyzer = (*DNSAnalyzer)(nil)
Expand Down Expand Up @@ -44,14 +48,21 @@ func (a *DNSAnalyzer) NewTCP(info analyzer.TCPInfo, logger analyzer.Logger) anal
}

type dnsUDPStream struct {
logger analyzer.Logger
logger analyzer.Logger
invalidCount int
}

func (s *dnsUDPStream) Feed(rev bool, data []byte) (u *analyzer.PropUpdate, done bool) {
m := parseDNSMessage(data)
// To allow non-DNS UDP traffic to get offloaded,
// we consider a UDP stream invalid and "done" if
// it has more than a certain number of consecutive
// packets that are not valid DNS messages.
if m == nil {
return nil, false
s.invalidCount++
return nil, s.invalidCount >= dnsUDPInvalidCountThreshold
}
s.invalidCount = 0 // Reset invalid count on valid DNS message
return &analyzer.PropUpdate{
Type: analyzer.PropUpdateReplace,
M: m,
Expand Down

0 comments on commit f986462

Please sign in to comment.