3
0

udp_tester.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package udp
  4. import (
  5. "io"
  6. "net/netip"
  7. "sync/atomic"
  8. "github.com/sirupsen/logrus"
  9. "github.com/slackhq/nebula/config"
  10. "github.com/slackhq/nebula/firewall"
  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, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
  92. plaintext := make([]byte, MTU)
  93. h := &header.H{}
  94. fwPacket := &firewall.Packet{}
  95. nb := make([]byte, 12, 12)
  96. for {
  97. p, ok := <-u.RxPackets
  98. if !ok {
  99. return
  100. }
  101. r(p.From, plaintext[:0], p.Data, h, fwPacket, lhf, nb, q, cache.Get(u.l))
  102. }
  103. }
  104. func (u *TesterConn) ReloadConfig(*config.C) {}
  105. func NewUDPStatsEmitter(_ []Conn) func() {
  106. // No UDP stats for non-linux
  107. return func() {}
  108. }
  109. func (u *TesterConn) LocalAddr() (netip.AddrPort, error) {
  110. return u.Addr, nil
  111. }
  112. func (u *TesterConn) Rebind() error {
  113. return nil
  114. }
  115. func (u *TesterConn) Close() error {
  116. if u.closed.CompareAndSwap(false, true) {
  117. close(u.RxPackets)
  118. close(u.TxPackets)
  119. }
  120. return nil
  121. }