Skip to content

Commit

Permalink
Support of DNSTap extra field on outgoing messages (#547)
Browse files Browse the repository at this point in the history
* encode dnstap extra field
  • Loading branch information
dmachard authored Jan 16, 2024
1 parent 2d47aa5 commit c08f7c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 41 deletions.
5 changes: 5 additions & 0 deletions dnsutils/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,11 @@ func (dm *DNSMessage) ToDNSTap() ([]byte, error) {

dt.Message = msg

// add dnstap extra
if len(dm.DNSTap.Extra) > 0 {
dt.Extra = []byte(dm.DNSTap.Extra)
}

data, err := proto.Marshal(dt)
if err != nil {
return nil, err
Expand Down
28 changes: 28 additions & 0 deletions dnsutils/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,36 @@ import (
"testing"

"github.com/dmachard/go-dnscollector/pkgconfig"
"github.com/dmachard/go-dnstap-protobuf"
"google.golang.org/protobuf/proto"
)

func TestDnsMessage_Encode_ToDNSTap(t *testing.T) {
dm := GetFakeDNSMessageWithPayload()
dm.DNSTap.Extra = "extra:value"

// encode to dnstap
tapMsg, err := dm.ToDNSTap()
if err != nil {
t.Fatalf("could not encode to dnstap: %v\n", err)
}

// decode dnstap message
dt := &dnstap.Dnstap{}
err = proto.Unmarshal(tapMsg, dt)
if err != nil {
t.Fatalf("error to decode dnstap: %v", err)
}

if string(dt.GetIdentity()) != dm.DNSTap.Identity {
t.Errorf("identify field should be equal got=%s", string(dt.GetIdentity()))
}

if string(dt.GetExtra()) != dm.DNSTap.Extra {
t.Errorf("extra field should be equal got=%s", string(dt.GetExtra()))
}
}

func TestDnsMessage_Json_Reference(t *testing.T) {
dm := DNSMessage{}
dm.Init()
Expand Down
41 changes: 0 additions & 41 deletions processors/dnstap.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,46 +120,8 @@ func (d *DNSTapProcessor) Stop() {
d.LogInfo("stopping to process...")
d.stopRun <- true
<-d.doneRun

// d.LogInfo("stopping to monitor loggers...")
// d.stopMonitor <- true
// <-d.doneMonitor
}

// func (d *DNSTapProcessor) MonitorLoggers() {
// // watchInterval := 10 * time.Second
// // bufferFull := time.NewTimer(watchInterval)
// MONITOR_LOOP:
// for {
// select {
// case <-d.stopMonitor:
// // close(d.dropped)
// // bufferFull.Stop()
// d.doneMonitor <- true
// break MONITOR_LOOP

// case loggerName := <-d.dropped:
// if _, ok := d.droppedCount[loggerName]; !ok {
// d.droppedCount[loggerName] = 1
// } else {
// d.droppedCount[loggerName]++
// }

// case <-bufferFull.C:
// for v, k := range d.droppedCount {
// if k > 0 {
// d.LogError("logger[%s] buffer is full, %d packet(s) dropped", v, k)
// d.droppedCount[v] = 0
// }
// }
// bufferFull.Reset(watchInterval)

// }
// }
// d.LogInfo("monitor terminated")
// }

// func (d *DNSTapProcessor) Run(loggersChannel []chan dnsutils.DNSMessage, loggersName []string) {
func (d *DNSTapProcessor) Run(defaultWorkers []pkgutils.Worker, droppedworkers []pkgutils.Worker) {
dt := &dnstap.Dnstap{}

Expand All @@ -170,9 +132,6 @@ func (d *DNSTapProcessor) Run(defaultWorkers []pkgutils.Worker, droppedworkers [
// prepare enabled transformers
transforms := transformers.NewTransforms(&d.config.IngoingTransformers, d.logger, d.name, defaultRoutes, d.ConnID)

// start goroutine to count dropped messsages
// go d.MonitorLoggers()

// read incoming dns message
d.LogInfo("waiting dns message to process...")
RUN_LOOP:
Expand Down

0 comments on commit c08f7c1

Please sign in to comment.