udp_tester.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // +build e2e_testing
  2. package nebula
  3. import (
  4. "net"
  5. "github.com/sirupsen/logrus"
  6. )
  7. type UdpPacket struct {
  8. ToIp net.IP
  9. ToPort uint16
  10. FromIp net.IP
  11. FromPort uint16
  12. Data []byte
  13. }
  14. func (u *UdpPacket) Copy() *UdpPacket {
  15. n := &UdpPacket{
  16. ToIp: make(net.IP, len(u.ToIp)),
  17. ToPort: u.ToPort,
  18. FromIp: make(net.IP, len(u.FromIp)),
  19. FromPort: u.FromPort,
  20. Data: make([]byte, len(u.Data)),
  21. }
  22. copy(n.ToIp, u.ToIp)
  23. copy(n.FromIp, u.FromIp)
  24. copy(n.Data, u.Data)
  25. return n
  26. }
  27. type udpConn struct {
  28. addr *udpAddr
  29. rxPackets chan *UdpPacket // Packets to receive into nebula
  30. txPackets chan *UdpPacket // Packets transmitted outside by nebula
  31. l *logrus.Logger
  32. }
  33. func NewListener(l *logrus.Logger, ip string, port int, _ bool) (*udpConn, error) {
  34. return &udpConn{
  35. addr: &udpAddr{net.ParseIP(ip), uint16(port)},
  36. rxPackets: make(chan *UdpPacket, 1),
  37. txPackets: make(chan *UdpPacket, 1),
  38. l: l,
  39. }, nil
  40. }
  41. // Send will place a UdpPacket onto the receive queue for nebula to consume
  42. // this is an encrypted packet or a handshake message in most cases
  43. // packets were transmitted from another nebula node, you can send them with Tun.Send
  44. func (u *udpConn) Send(packet *UdpPacket) {
  45. u.l.Infof("UDP injecting packet %+v", packet)
  46. u.rxPackets <- packet
  47. }
  48. // Get will pull a UdpPacket from the transmit queue
  49. // nebula meant to send this message on the network, it will be encrypted
  50. // packets were ingested from the tun side (in most cases), you can send them with Tun.Send
  51. func (u *udpConn) Get(block bool) *UdpPacket {
  52. if block {
  53. return <-u.txPackets
  54. }
  55. select {
  56. case p := <-u.txPackets:
  57. return p
  58. default:
  59. return nil
  60. }
  61. }
  62. //********************************************************************************************************************//
  63. // Below this is boilerplate implementation to make nebula actually work
  64. //********************************************************************************************************************//
  65. func (u *udpConn) WriteTo(b []byte, addr *udpAddr) error {
  66. p := &UdpPacket{
  67. Data: make([]byte, len(b), len(b)),
  68. FromIp: make([]byte, 16),
  69. FromPort: u.addr.Port,
  70. ToIp: make([]byte, 16),
  71. ToPort: addr.Port,
  72. }
  73. copy(p.Data, b)
  74. copy(p.ToIp, addr.IP.To16())
  75. copy(p.FromIp, u.addr.IP.To16())
  76. u.txPackets <- p
  77. return nil
  78. }
  79. func (u *udpConn) ListenOut(f *Interface, q int) {
  80. plaintext := make([]byte, mtu)
  81. header := &Header{}
  82. fwPacket := &FirewallPacket{}
  83. ua := &udpAddr{IP: make([]byte, 16)}
  84. nb := make([]byte, 12, 12)
  85. lhh := f.lightHouse.NewRequestHandler()
  86. conntrackCache := NewConntrackCacheTicker(f.conntrackCacheTimeout)
  87. for {
  88. p := <-u.rxPackets
  89. ua.Port = p.FromPort
  90. copy(ua.IP, p.FromIp.To16())
  91. f.readOutsidePackets(ua, plaintext[:0], p.Data, header, fwPacket, lhh, nb, q, conntrackCache.Get(u.l))
  92. }
  93. }
  94. func (u *udpConn) reloadConfig(*Config) {}
  95. func NewUDPStatsEmitter(_ []*udpConn) func() {
  96. // No UDP stats for non-linux
  97. return func() {}
  98. }
  99. func (u *udpConn) LocalAddr() (*udpAddr, error) {
  100. return u.addr, nil
  101. }
  102. func (u *udpConn) Rebind() error {
  103. return nil
  104. }
  105. func hostDidRoam(addr *udpAddr, newaddr *udpAddr) bool {
  106. return !addr.Equals(newaddr)
  107. }