aboutsummaryrefslogtreecommitdiff
path: root/plugin/dnstap/encoder.go
blob: 09b1e2e6f87125f81ae38b8ba4371652eafc6731 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package dnstap

import (
	"io"
	"time"

	tap "github.com/dnstap/golang-dnstap"
	fs "github.com/farsightsec/golang-framestream"
	"github.com/golang/protobuf/proto"
)

// encoder wraps a golang-framestream.Encoder.
type encoder struct {
	fs *fs.Encoder
}

func newEncoder(w io.Writer, timeout time.Duration) (*encoder, error) {
	fs, err := fs.NewEncoder(w, &fs.EncoderOptions{
		ContentType:   []byte("protobuf:dnstap.Dnstap"),
		Bidirectional: true,
		Timeout:       timeout,
	})
	if err != nil {
		return nil, err
	}
	return &encoder{fs}, nil
}

func (e *encoder) writeMsg(msg *tap.Dnstap) error {
	buf, err := proto.Marshal(msg)
	if err != nil {
		return err
	}

	_, err = e.fs.Write(buf) // n < len(buf) should return an error?
	return err
}

func (e *encoder) flush() error { return e.fs.Flush() }
func (e *encoder) close() error { return e.fs.Close() }