2020-11-05 14:37:16 +01:00
|
|
|
package dnstap
|
2020-11-03 15:31:34 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
tap "github.com/dnstap/golang-dnstap"
|
|
|
|
|
fs "github.com/farsightsec/golang-framestream"
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
)
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
// encoder wraps a golang-framestream.Encoder.
|
|
|
|
|
type encoder struct {
|
2020-11-03 15:31:34 +01:00
|
|
|
fs *fs.Encoder
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
func newEncoder(w io.Writer, timeout time.Duration) (*encoder, error) {
|
2020-11-03 15:31:34 +01:00
|
|
|
fs, err := fs.NewEncoder(w, &fs.EncoderOptions{
|
|
|
|
|
ContentType: []byte("protobuf:dnstap.Dnstap"),
|
|
|
|
|
Bidirectional: true,
|
|
|
|
|
Timeout: timeout,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-05 14:37:16 +01:00
|
|
|
return &encoder{fs}, nil
|
2020-11-03 15:31:34 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
func (e *encoder) writeMsg(msg *tap.Dnstap) error {
|
2020-11-03 15:31:34 +01:00
|
|
|
buf, err := proto.Marshal(msg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
_, err = e.fs.Write(buf) // n < len(buf) should return an error?
|
2020-11-03 15:31:34 +01:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 14:37:16 +01:00
|
|
|
func (e *encoder) flush() error { return e.fs.Flush() }
|
|
|
|
|
func (e *encoder) close() error { return e.fs.Close() }
|