tun.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-License-Identifier: MIT
  2. //
  3. // Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
  4. package tun
  5. import (
  6. "os"
  7. )
  8. type Event int
  9. const (
  10. EventUp = 1 << iota
  11. EventDown
  12. EventMTUUpdate
  13. )
  14. type Device interface {
  15. // File returns the file descriptor of the device.
  16. File() *os.File
  17. // Read one or more packets from the Device (without any additional headers).
  18. // On a successful read it returns the number of packets read, and sets
  19. // packet lengths within the sizes slice. len(sizes) must be >= len(bufs).
  20. // A nonzero offset can be used to instruct the Device on where to begin
  21. // reading into each element of the bufs slice.
  22. Read(bufs [][]byte, sizes []int, offset int) (n int, err error)
  23. // Write one or more packets to the device (without any additional headers).
  24. // On a successful write it returns the number of packets written. A nonzero
  25. // offset can be used to instruct the Device on where to begin writing from
  26. // each packet contained within the bufs slice.
  27. Write(bufs [][]byte, offset int) (int, error)
  28. // MTU returns the MTU of the Device.
  29. MTU() (int, error)
  30. // Name returns the current name of the Device.
  31. Name() (string, error)
  32. // Events returns a channel of type Event, which is fed Device events.
  33. Events() <-chan Event
  34. // Close stops the Device and closes the Event channel.
  35. Close() error
  36. // BatchSize returns the preferred/max number of packets that can be read or
  37. // written in a single read/write call. BatchSize must not change over the
  38. // lifetime of a Device.
  39. BatchSize() int
  40. }