udp_tester.go 3.3 KB

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