udp_generic.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //go:build (!linux || android) && !e2e_testing && !darwin
  2. // +build !linux android
  3. // +build !e2e_testing
  4. // +build !darwin
  5. // udp_generic implements the nebula UDP interface in pure Go stdlib. This
  6. // means it can be used on platforms like Darwin and Windows.
  7. package udp
  8. import (
  9. "context"
  10. "errors"
  11. "fmt"
  12. "net"
  13. "net/netip"
  14. "time"
  15. "github.com/sirupsen/logrus"
  16. "github.com/slackhq/nebula/config"
  17. )
  18. type GenericConn struct {
  19. *net.UDPConn
  20. l *logrus.Logger
  21. }
  22. var _ Conn = &GenericConn{}
  23. func NewGenericListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
  24. lc := NewListenConfig(multi)
  25. pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
  26. if err != nil {
  27. return nil, err
  28. }
  29. if uc, ok := pc.(*net.UDPConn); ok {
  30. return &GenericConn{UDPConn: uc, l: l}, nil
  31. }
  32. return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
  33. }
  34. func (u *GenericConn) WriteTo(b []byte, addr netip.AddrPort) error {
  35. _, err := u.UDPConn.WriteToUDPAddrPort(b, addr)
  36. return err
  37. }
  38. func (u *GenericConn) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) error {
  39. for i, b := range bufs {
  40. if _, err := u.UDPConn.WriteToUDPAddrPort(b, addrs[i]); err != nil {
  41. return err
  42. }
  43. }
  44. return nil
  45. }
  46. func (u *GenericConn) WriteSegmented(bufs [][]byte, addr netip.AddrPort, _ int) error {
  47. for _, b := range bufs {
  48. if _, err := u.UDPConn.WriteToUDPAddrPort(b, addr); err != nil {
  49. return err
  50. }
  51. }
  52. return nil
  53. }
  54. func (u *GenericConn) SupportsGSO() bool { return false }
  55. func (u *GenericConn) LocalAddr() (netip.AddrPort, error) {
  56. a := u.UDPConn.LocalAddr()
  57. switch v := a.(type) {
  58. case *net.UDPAddr:
  59. addr, ok := netip.AddrFromSlice(v.IP)
  60. if !ok {
  61. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned invalid IP address: %s", v.IP)
  62. }
  63. return netip.AddrPortFrom(addr, uint16(v.Port)), nil
  64. default:
  65. return netip.AddrPort{}, fmt.Errorf("LocalAddr returned: %#v", a)
  66. }
  67. }
  68. func (u *GenericConn) ReloadConfig(c *config.C) {
  69. }
  70. func NewUDPStatsEmitter(udpConns []Conn) func() {
  71. // No UDP stats for non-linux
  72. return func() {}
  73. }
  74. type rawMessage struct {
  75. Len uint32
  76. }
  77. func (u *GenericConn) ListenOut(r EncReader, flush func()) error {
  78. buffer := make([]byte, MTU)
  79. var lastRecvErr time.Time
  80. for {
  81. // Just read one packet at a time
  82. n, rua, err := u.ReadFromUDPAddrPort(buffer)
  83. if err != nil {
  84. if errors.Is(err, net.ErrClosed) {
  85. return err
  86. }
  87. // Dampen unexpected message warns to once per minute
  88. if lastRecvErr.IsZero() || time.Since(lastRecvErr) > time.Minute {
  89. lastRecvErr = time.Now()
  90. u.l.WithError(err).Warn("unexpected udp socket receive error")
  91. }
  92. continue
  93. }
  94. r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
  95. flush()
  96. }
  97. }
  98. func (u *GenericConn) SupportsMultipleReaders() bool {
  99. return false
  100. }