Writer.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2019 by Farsight Security, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package dnstap
  17. import (
  18. "io"
  19. "time"
  20. framestream "github.com/farsightsec/golang-framestream"
  21. )
  22. // A Writer writes dnstap frames to its destination.
  23. type Writer interface {
  24. WriteFrame([]byte) (int, error)
  25. Close() error
  26. }
  27. // WriterOptions specifies configuration for the Writer
  28. type WriterOptions struct {
  29. // If Bidirectional is true, the underlying io.Writer must also
  30. // satisfy io.Reader, and the dnstap Writer will use the bidirectional
  31. // Frame Streams protocol.
  32. Bidirectional bool
  33. // Timeout sets the write timeout for data and control messages and the
  34. // read timeout for handshake responses on the underlying Writer. Timeout
  35. // is only effective if the underlying Writer is a net.Conn.
  36. Timeout time.Duration
  37. }
  38. // NewWriter creates a Writer using the given io.Writer and options.
  39. func NewWriter(w io.Writer, opt *WriterOptions) (Writer, error) {
  40. if opt == nil {
  41. opt = &WriterOptions{}
  42. }
  43. return framestream.NewWriter(w,
  44. &framestream.WriterOptions{
  45. ContentTypes: [][]byte{FSContentType},
  46. Timeout: opt.Timeout,
  47. Bidirectional: opt.Bidirectional,
  48. })
  49. }