Skip to content

Commit

Permalink
dns parser: set RCODE only in DNS responses (#885)
Browse files Browse the repository at this point in the history
* set RCODE only in DNS responses
* prometheus logger: ignore default rcode in counter
  • Loading branch information
dmachard authored Nov 30, 2024
1 parent 466760d commit d95e25a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<p align="center">
<img src="https://goreportcard.com/badge/github.com/dmachard/go-dns-collector" alt="Go Report"/>
<img src="https://img.shields.io/badge/go%20version-min%201.21-green" alt="Go version"/>
<img src="https://img.shields.io/badge/go%20tests-508-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20tests-509-green" alt="Go tests"/>
<img src="https://img.shields.io/badge/go%20bench-21-green" alt="Go bench"/>
<img src="https://img.shields.io/badge/go%20lines-31977-green" alt="Go lines"/>
</p>
Expand Down
5 changes: 4 additions & 1 deletion dnsutils/dns_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ func DecodePayload(dm *DNSMessage, header *DNSHeader, config *pkgconfig.Config)
}

dm.DNS.ID = header.ID
dm.DNS.Rcode = RcodeToString(header.Rcode)
// set rcode only with response
if header.Qr == 1 {
dm.DNS.Rcode = RcodeToString(header.Rcode)
}
dm.DNS.Opcode = header.Opcode

// update dnstap operation if the opcode is equal to 5 (dns update)
Expand Down
37 changes: 34 additions & 3 deletions dnsutils/dns_parser_payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestDecodePayload_QueryHappy(t *testing.T) {

if dm.DNS.ID != 0x9e84 ||
dm.DNS.Opcode != 0 ||
dm.DNS.Rcode != RcodeToString(0) ||
dm.DNS.Flags.QR ||
dm.DNS.Flags.TC ||
dm.DNS.Flags.AA ||
Expand Down Expand Up @@ -72,7 +71,6 @@ func TestDecodePayload_QueryHappy(t *testing.T) {
len(dm.DNS.DNSRRs.Records) != 0 {
t.Errorf("Unexpected sections parsed")
}

}
func TestDecodePayload_QueryInvalid(t *testing.T) {
payload := []byte{
Expand Down Expand Up @@ -996,7 +994,6 @@ func TestDecodePayload_Truncated(t *testing.T) {
if dm.DNS.MalformedPacket != true {
t.Errorf("expected packet to be malformed")
}

}

// Dynamic query (UPDATE)
Expand Down Expand Up @@ -1086,3 +1083,37 @@ func TestDecodePayload_MDNSResponseWithNoQuestion(t *testing.T) {
t.Error("expected no error on decode", err)
}
}

// Rcode should not be set on query
func TestDecodePayload_Query_NoRcode(t *testing.T) {
payload := []byte{
// transaction ID
0xb1, 0x17,
// flags
0x01, 0x00,
// questions
0x00, 0x01,
// answer
0x00, 0x00,
// authority
0x00, 0x00,
// additional
0x00, 0x00,
// queries
0x00, 0x00, 0x06, 0x00, 0x01,
}

dm := DNSMessage{}
dm.Init()
dm.DNS.Payload = payload
dm.DNS.Length = len(payload)

// decode header and paylo
header, _ := DecodeDNS(payload)
DecodePayload(&dm, &header, pkgconfig.GetDefaultConfig())

// check the rcode
if dm.DNS.Rcode != "-" {
t.Errorf("invalid rcode: %s", dm.DNS.Rcode)
}
}
10 changes: 6 additions & 4 deletions workers/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,12 @@ func (w *PrometheusCountersSet) Record(dm dnsutils.DNSMessage) {
w.epsCounters.TotalQtypes[dm.DNS.Qtype]++
}

if _, exists := w.epsCounters.TotalRcodes[dm.DNS.Rcode]; !exists {
w.epsCounters.TotalRcodes[dm.DNS.Rcode] = 1
} else {
w.epsCounters.TotalRcodes[dm.DNS.Rcode]++
if dm.DNS.Rcode != "-" {
if _, exists := w.epsCounters.TotalRcodes[dm.DNS.Rcode]; !exists {
w.epsCounters.TotalRcodes[dm.DNS.Rcode] = 1
} else {
w.epsCounters.TotalRcodes[dm.DNS.Rcode]++
}
}

if _, exists := w.epsCounters.TotalOperations[dm.DNSTap.Operation]; !exists {
Expand Down

0 comments on commit d95e25a

Please sign in to comment.