udp_tester.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package udp
  4. import (
  5. "io"
  6. "net/netip"
  7. "os"
  8. "sync/atomic"
  9. "github.com/sirupsen/logrus"
  10. "github.com/slackhq/nebula/config"
  11. "github.com/slackhq/nebula/header"
  12. )
  13. type Packet struct {
  14. To netip.AddrPort
  15. From netip.AddrPort
  16. Data []byte
  17. }
  18. func (u *Packet) Copy() *Packet {
  19. n := &Packet{
  20. To: u.To,
  21. From: u.From,
  22. Data: make([]byte, len(u.Data)),
  23. }
  24. copy(n.Data, u.Data)
  25. return n
  26. }
  27. type TesterConn struct {
  28. Addr netip.AddrPort
  29. RxPackets chan *Packet // Packets to receive into nebula
  30. TxPackets chan *Packet // Packets transmitted outside by nebula
  31. closed atomic.Bool
  32. l *logrus.Logger
  33. }
  34. func NewListener(l *logrus.Logger, ip netip.Addr, port int, _ bool, _ int) (Conn, error) {
  35. return &TesterConn{
  36. Addr: netip.AddrPortFrom(ip, uint16(port)),
  37. RxPackets: make(chan *Packet, 10),
  38. TxPackets: make(chan *Packet, 10),
  39. l: l,
  40. }, nil
  41. }
  42. // Send will place a UdpPacket onto the receive queue for nebula to consume
  43. // this is an encrypted packet or a handshake message in most cases
  44. // packets were transmitted from another nebula node, you can send them with Tun.Send
  45. func (u *TesterConn) Send(packet *Packet) {
  46. if u.closed.Load() {
  47. return
  48. }
  49. h := &header.H{}
  50. if err := h.Parse(packet.Data); err != nil {
  51. panic(err)
  52. }
  53. if u.l.Level >= logrus.DebugLevel {
  54. u.l.WithField("header", h).
  55. WithField("udpAddr", packet.From).
  56. WithField("dataLen", len(packet.Data)).
  57. Debug("UDP receiving injected packet")
  58. }
  59. u.RxPackets <- packet
  60. }
  61. // Get will pull a UdpPacket from the transmit queue
  62. // nebula meant to send this message on the network, it will be encrypted
  63. // packets were ingested from the tun side (in most cases), you can send them with Tun.Send
  64. func (u *TesterConn) Get(block bool) *Packet {
  65. if block {
  66. return <-u.TxPackets
  67. }
  68. select {
  69. case p := <-u.TxPackets:
  70. return p
  71. default:
  72. return nil
  73. }
  74. }
  75. //********************************************************************************************************************//
  76. // Below this is boilerplate implementation to make nebula actually work
  77. //********************************************************************************************************************//
  78. func (u *TesterConn) WriteTo(b []byte, addr netip.AddrPort) error {
  79. if u.closed.Load() {
  80. return io.ErrClosedPipe
  81. }
  82. p := &Packet{
  83. Data: make([]byte, len(b), len(b)),
  84. From: u.Addr,
  85. To: addr,
  86. }
  87. copy(p.Data, b)
  88. u.TxPackets <- p
  89. return nil
  90. }
  91. func (u *TesterConn) ListenOut(r EncReader) error {
  92. for {
  93. p, ok := <-u.RxPackets
  94. if !ok {
  95. return os.ErrClosed
  96. }
  97. r(p.From, p.Data)
  98. }
  99. }
  100. func (u *TesterConn) ReloadConfig(*config.C) {}
  101. func NewUDPStatsEmitter(_ []Conn) func() {
  102. // No UDP stats for non-linux
  103. return func() {}
  104. }
  105. func (u *TesterConn) LocalAddr() (netip.AddrPort, error) {
  106. return u.Addr, nil
  107. }
  108. func (u *TesterConn) SupportsMultipleReaders() bool {
  109. return false
  110. }
  111. func (u *TesterConn) Rebind() error {
  112. return nil
  113. }
  114. func (u *TesterConn) Close() error {
  115. if u.closed.CompareAndSwap(false, true) {
  116. close(u.RxPackets)
  117. close(u.TxPackets)
  118. }
  119. return nil
  120. }