mirror of
https://github.com/slackhq/nebula.git
synced 2025-11-22 16:34:25 +01:00
127 lines
2.9 KiB
Go
127 lines
2.9 KiB
Go
//go:build (!linux || android) && !e2e_testing && !darwin
|
|
// +build !linux android
|
|
// +build !e2e_testing
|
|
// +build !darwin
|
|
|
|
// udp_generic implements the nebula UDP interface in pure Go stdlib. This
|
|
// means it can be used on platforms like Darwin and Windows.
|
|
|
|
package udp
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/slackhq/nebula/config"
|
|
)
|
|
|
|
type GenericConn struct {
|
|
*net.UDPConn
|
|
l *logrus.Logger
|
|
}
|
|
|
|
var _ Conn = &GenericConn{}
|
|
|
|
func NewGenericListener(l *logrus.Logger, ip netip.Addr, port int, multi bool, batch int) (Conn, error) {
|
|
lc := NewListenConfig(multi)
|
|
pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if uc, ok := pc.(*net.UDPConn); ok {
|
|
return &GenericConn{UDPConn: uc, l: l}, nil
|
|
}
|
|
return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
|
|
}
|
|
|
|
func (u *GenericConn) WriteTo(b []byte, addr netip.AddrPort) error {
|
|
_, err := u.UDPConn.WriteToUDPAddrPort(b, addr)
|
|
return err
|
|
}
|
|
|
|
func (u *GenericConn) LocalAddr() (netip.AddrPort, error) {
|
|
a := u.UDPConn.LocalAddr()
|
|
|
|
switch v := a.(type) {
|
|
case *net.UDPAddr:
|
|
addr, ok := netip.AddrFromSlice(v.IP)
|
|
if !ok {
|
|
return netip.AddrPort{}, fmt.Errorf("LocalAddr returned invalid IP address: %s", v.IP)
|
|
}
|
|
return netip.AddrPortFrom(addr, uint16(v.Port)), nil
|
|
|
|
default:
|
|
return netip.AddrPort{}, fmt.Errorf("LocalAddr returned: %#v", a)
|
|
}
|
|
}
|
|
|
|
func (u *GenericConn) ReloadConfig(c *config.C) {
|
|
|
|
}
|
|
|
|
func NewUDPStatsEmitter(udpConns []Conn) func() {
|
|
// No UDP stats for non-linux
|
|
return func() {}
|
|
}
|
|
|
|
type rawMessage struct {
|
|
Len uint32
|
|
}
|
|
|
|
func (u *GenericConn) ListenOut(r EncReader) {
|
|
buffer := make([]byte, MTU)
|
|
|
|
for {
|
|
// Just read one packet at a time
|
|
n, rua, err := u.ReadFromUDPAddrPort(buffer)
|
|
if err != nil {
|
|
u.l.WithError(err).Debug("udp socket is closed, exiting read loop")
|
|
return
|
|
}
|
|
|
|
r(netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port()), buffer[:n])
|
|
}
|
|
}
|
|
|
|
// ListenOutBatch - fallback to single-packet reads for generic platforms
|
|
func (u *GenericConn) ListenOutBatch(r EncBatchReader) {
|
|
buffer := make([]byte, MTU)
|
|
addrs := make([]netip.AddrPort, 1)
|
|
payloads := make([][]byte, 1)
|
|
|
|
for {
|
|
// Just read one packet at a time and call batch callback with count=1
|
|
n, rua, err := u.ReadFromUDPAddrPort(buffer)
|
|
if err != nil {
|
|
u.l.WithError(err).Debug("udp socket is closed, exiting read loop")
|
|
return
|
|
}
|
|
|
|
addrs[0] = netip.AddrPortFrom(rua.Addr().Unmap(), rua.Port())
|
|
payloads[0] = buffer[:n]
|
|
r(addrs, payloads, 1)
|
|
}
|
|
}
|
|
|
|
// WriteMulti sends multiple packets - fallback implementation
|
|
func (u *GenericConn) WriteMulti(packets [][]byte, addrs []netip.AddrPort) (int, error) {
|
|
for i := range packets {
|
|
err := u.WriteTo(packets[i], addrs[i])
|
|
if err != nil {
|
|
return i, err
|
|
}
|
|
}
|
|
return len(packets), nil
|
|
}
|
|
|
|
func (u *GenericConn) BatchSize() int {
|
|
return 1
|
|
}
|
|
|
|
func (u *GenericConn) Rebind() error {
|
|
return nil
|
|
}
|