Reader.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 Reader is a source of dnstap frames.
  23. type Reader interface {
  24. ReadFrame([]byte) (int, error)
  25. }
  26. // ReaderOptions specifies configuration for the Reader.
  27. type ReaderOptions struct {
  28. // If Bidirectional is true, the underlying io.Reader must also
  29. // satisfy io.Writer, and the dnstap Reader will use the bidirectional
  30. // Frame Streams protocol.
  31. Bidirectional bool
  32. // Timeout sets the timeout for reading the initial handshake and
  33. // writing response control messages to the underlying Reader. Timeout
  34. // is only effective if the underlying Reader is a net.Conn.
  35. Timeout time.Duration
  36. }
  37. // NewReader creates a Reader using the given io.Reader and options.
  38. func NewReader(r io.Reader, opt *ReaderOptions) (Reader, error) {
  39. if opt == nil {
  40. opt = &ReaderOptions{}
  41. }
  42. return framestream.NewReader(r,
  43. &framestream.ReaderOptions{
  44. ContentTypes: [][]byte{FSContentType},
  45. Timeout: opt.Timeout,
  46. Bidirectional: opt.Bidirectional,
  47. })
  48. }