nebula/udp/udp_generic.go

94 lines
2.2 KiB
Go
Raw Normal View History

2021-10-22 05:24:11 +08:00
//go:build (!linux || android) && !e2e_testing
// +build !linux android
2021-03-26 22:46:30 +08:00
// +build !e2e_testing
2019-11-20 01:00:20 +08:00
// 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
2019-11-20 01:00:20 +08:00
import (
"context"
"fmt"
"net"
2021-03-26 22:46:30 +08:00
"github.com/sirupsen/logrus"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/firewall"
"github.com/slackhq/nebula/header"
2019-11-20 01:00:20 +08:00
)
2023-06-14 23:48:52 +08:00
type GenericConn struct {
2019-11-20 01:00:20 +08:00
*net.UDPConn
2021-03-26 22:46:30 +08:00
l *logrus.Logger
2019-11-20 01:00:20 +08:00
}
var _ Conn = &GenericConn{}
func NewGenericListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (Conn, error) {
2019-11-20 01:00:20 +08:00
lc := NewListenConfig(multi)
pc, err := lc.ListenPacket(context.TODO(), "udp", net.JoinHostPort(ip.String(), fmt.Sprintf("%v", port)))
2019-11-20 01:00:20 +08:00
if err != nil {
return nil, err
}
if uc, ok := pc.(*net.UDPConn); ok {
2023-06-14 23:48:52 +08:00
return &GenericConn{UDPConn: uc, l: l}, nil
2019-11-20 01:00:20 +08:00
}
return nil, fmt.Errorf("Unexpected PacketConn: %T %#v", pc, pc)
}
2023-06-14 23:48:52 +08:00
func (u *GenericConn) WriteTo(b []byte, addr *Addr) error {
_, err := u.UDPConn.WriteToUDP(b, &net.UDPAddr{IP: addr.IP, Port: int(addr.Port)})
2019-11-20 01:00:20 +08:00
return err
}
2023-06-14 23:48:52 +08:00
func (u *GenericConn) LocalAddr() (*Addr, error) {
a := u.UDPConn.LocalAddr()
2019-11-20 01:00:20 +08:00
switch v := a.(type) {
case *net.UDPAddr:
addr := &Addr{IP: make([]byte, len(v.IP))}
2021-03-19 09:37:24 +08:00
copy(addr.IP, v.IP)
addr.Port = uint16(v.Port)
return addr, nil
2019-11-20 01:00:20 +08:00
default:
return nil, fmt.Errorf("LocalAddr returned: %#v", a)
}
}
2023-06-14 23:48:52 +08:00
func (u *GenericConn) ReloadConfig(c *config.C) {
2019-11-20 01:00:20 +08:00
// TODO
}
2023-06-14 23:48:52 +08:00
func NewUDPStatsEmitter(udpConns []Conn) func() {
// No UDP stats for non-linux
return func() {}
}
2019-11-20 01:00:20 +08:00
type rawMessage struct {
Len uint32
}
2023-06-14 23:48:52 +08:00
func (u *GenericConn) ListenOut(r EncReader, lhf LightHouseHandlerFunc, cache *firewall.ConntrackCacheTicker, q int) {
plaintext := make([]byte, MTU)
buffer := make([]byte, MTU)
h := &header.H{}
fwPacket := &firewall.Packet{}
udpAddr := &Addr{IP: make([]byte, 16)}
2019-11-20 01:00:20 +08:00
nb := make([]byte, 12, 12)
for {
// Just read one packet at a time
n, rua, err := u.ReadFromUDP(buffer)
if err != nil {
u.l.WithError(err).Debug("udp socket is closed, exiting read loop")
return
2019-11-20 01:00:20 +08:00
}
2021-03-19 09:37:24 +08:00
udpAddr.IP = rua.IP
udpAddr.Port = uint16(rua.Port)
r(udpAddr, plaintext[:0], buffer[:n], h, fwPacket, lhf, nb, q, cache.Get(u.l))
2019-11-20 01:00:20 +08:00
}
}