tun_tester.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. c.l.WithField("dataLen", len(packet)).Info("Tun receiving injected packet")
  37. c.rxPackets <- packet
  38. }
  39. // Get will pull an unencrypted ip layer frame from the transmit queue
  40. // nebula meant to send this message to some application on the local system
  41. // packets were ingested from the udp side, you can send them with udpConn.Send
  42. func (c *Tun) Get(block bool) []byte {
  43. if block {
  44. return <-c.txPackets
  45. }
  46. select {
  47. case p := <-c.txPackets:
  48. return p
  49. default:
  50. return nil
  51. }
  52. }
  53. //********************************************************************************************************************//
  54. // Below this is boilerplate implementation to make nebula actually work
  55. //********************************************************************************************************************//
  56. func (c *Tun) Activate() error {
  57. return nil
  58. }
  59. func (c *Tun) CidrNet() *net.IPNet {
  60. return c.Cidr
  61. }
  62. func (c *Tun) DeviceName() string {
  63. return c.Device
  64. }
  65. func (c *Tun) Write(b []byte) (n int, err error) {
  66. return len(b), c.WriteRaw(b)
  67. }
  68. func (c *Tun) Close() error {
  69. close(c.rxPackets)
  70. return nil
  71. }
  72. func (c *Tun) WriteRaw(b []byte) error {
  73. packet := make([]byte, len(b), len(b))
  74. copy(packet, b)
  75. c.txPackets <- packet
  76. return nil
  77. }
  78. func (c *Tun) Read(b []byte) (int, error) {
  79. p := <-c.rxPackets
  80. copy(b, p)
  81. return len(p), nil
  82. }
  83. func (c *Tun) NewMultiQueueReader() (io.ReadWriteCloser, error) {
  84. return nil, fmt.Errorf("TODO: multiqueue not implemented")
  85. }