tun_tester.go 2.6 KB

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