udp_tester.go 2.8 KB

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