tun_tester.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package nebula
  4. import (
  5. "fmt"
  6. "io"
  7. "net"
  8. "github.com/sirupsen/logrus"
  9. )
  10. type Tun struct {
  11. Device string
  12. Cidr *net.IPNet
  13. MTU int
  14. UnsafeRoutes []route
  15. l *logrus.Logger
  16. rxPackets chan []byte // Packets to receive into nebula
  17. txPackets chan []byte // Packets transmitted outside by nebula
  18. }
  19. func newTun(l *logrus.Logger, deviceName string, cidr *net.IPNet, defaultMTU int, _ []route, unsafeRoutes []route, _ int, _ bool) (ifce *Tun, err error) {
  20. return &Tun{
  21. Device: deviceName,
  22. Cidr: cidr,
  23. MTU: defaultMTU,
  24. UnsafeRoutes: unsafeRoutes,
  25. l: l,
  26. rxPackets: make(chan []byte, 1),
  27. txPackets: make(chan []byte, 1),
  28. }, nil
  29. }
  30. func newTunFromFd(_ *logrus.Logger, _ int, _ *net.IPNet, _ int, _ []route, _ []route, _ int) (ifce *Tun, err error) {
  31. return nil, fmt.Errorf("newTunFromFd not supported")
  32. }
  33. // Send will place a byte array onto the receive queue for nebula to consume
  34. // These are unencrypted ip layer frames destined for another nebula node.
  35. // packets should exit the udp side, capture them with udpConn.Get
  36. func (c *Tun) Send(packet []byte) {
  37. c.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet")
  38. c.rxPackets <- packet
  39. }
  40. // Get will pull an unencrypted ip layer frame from the transmit queue
  41. // nebula meant to send this message to some application on the local system
  42. // packets were ingested from the udp side, you can send them with udpConn.Send
  43. func (c *Tun) Get(block bool) []byte {
  44. if block {
  45. return <-c.txPackets
  46. }
  47. select {
  48. case p := <-c.txPackets:
  49. return p
  50. default:
  51. return nil
  52. }
  53. }
  54. //********************************************************************************************************************//
  55. // Below this is boilerplate implementation to make nebula actually work
  56. //********************************************************************************************************************//
  57. func (c *Tun) Activate() error {
  58. return nil
  59. }
  60. func (c *Tun) CidrNet() *net.IPNet {
  61. return c.Cidr
  62. }
  63. func (c *Tun) DeviceName() string {
  64. return c.Device
  65. }
  66. func (c *Tun) Write(b []byte) (n int, err error) {
  67. return len(b), c.WriteRaw(b)
  68. }
  69. func (c *Tun) Close() error {
  70. close(c.rxPackets)
  71. return nil
  72. }
  73. func (c *Tun) WriteRaw(b []byte) error {
  74. packet := make([]byte, len(b), len(b))
  75. copy(packet, b)
  76. c.txPackets <- packet
  77. return nil
  78. }
  79. func (c *Tun) Read(b []byte) (int, error) {
  80. p := <-c.rxPackets
  81. copy(b, p)
  82. return len(p), nil
  83. }
  84. func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  85. return nil, fmt.Errorf("TODO: multiqueue not implemented")
  86. }