mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-21 09:46:40 +00:00
Working single channel bind
This commit is contained in:
152
iface/bind.go
152
iface/bind.go
@@ -1,152 +1,110 @@
|
||||
package iface
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.zx2c4.com/wireguard/conn"
|
||||
"net"
|
||||
"net/netip"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type packets struct {
|
||||
type UserEndpoint struct {
|
||||
conn.StdNetEndpoint
|
||||
}
|
||||
|
||||
type packet struct {
|
||||
buff []byte
|
||||
addr net.UDPAddr
|
||||
addr *net.UDPAddr
|
||||
}
|
||||
|
||||
type ICEBind struct {
|
||||
mu sync.Mutex
|
||||
packets chan packets
|
||||
type UserBind struct {
|
||||
endpointsLock sync.RWMutex
|
||||
endpoints map[netip.AddrPort]*UserEndpoint
|
||||
sharedConn net.PacketConn
|
||||
|
||||
Packets chan packet
|
||||
closeSignal chan struct{}
|
||||
conn *net.UDPConn
|
||||
}
|
||||
|
||||
func NewICEBind(udpConn *net.UDPConn) *ICEBind {
|
||||
return &ICEBind{
|
||||
conn: udpConn,
|
||||
mu: sync.Mutex{},
|
||||
}
|
||||
func NewUserBind(sharedConn net.PacketConn) *UserBind {
|
||||
return &UserBind{sharedConn: sharedConn}
|
||||
}
|
||||
|
||||
func (bind *ICEBind) Open(port uint16) ([]conn.ReceiveFunc, uint16, error) {
|
||||
func (b *UserBind) Open(port uint16) ([]conn.ReceiveFunc, uint16, error) {
|
||||
|
||||
bind.mu.Lock()
|
||||
defer bind.mu.Unlock()
|
||||
log.Infof("opening Bind on port %d", port)
|
||||
b.Packets = make(chan packet, 1000)
|
||||
b.closeSignal = make(chan struct{})
|
||||
|
||||
udpConn, p, err := listenNet("udp4", int(port))
|
||||
return []conn.ReceiveFunc{b.receive}, port, nil
|
||||
}
|
||||
|
||||
func (b *UserBind) receive(buff []byte) (int, conn.Endpoint, error) {
|
||||
|
||||
/*n, endpoint, err := b.sharedConn.ReadFrom(buff)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
bind.conn = udpConn
|
||||
return []conn.ReceiveFunc{bind.makeReceiveIPv4(udpConn)}, uint16(p), nil
|
||||
|
||||
/*bind.packets = make(chan packets)
|
||||
bind.closeSignal = make(chan struct{})
|
||||
|
||||
addrPort, err := netip.ParseAddrPort(bind.conn.LocalAddr().String())
|
||||
e, err := netip.ParseAddrPort(endpoint.String())
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
return []conn.ReceiveFunc{bind.makeReceiveIPv4(bind.conn)}, addrPort.Port(), nil*/
|
||||
}
|
||||
|
||||
func (bind *ICEBind) fakeReceiveIPv4(c *net.UDPConn) conn.ReceiveFunc {
|
||||
return func(buff []byte) (int, conn.Endpoint, error) {
|
||||
return 0, nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (bind *ICEBind) makeReceiveIPv4(c *net.UDPConn) conn.ReceiveFunc {
|
||||
return func(buff []byte) (int, conn.Endpoint, error) {
|
||||
n, endpoint, err := c.ReadFromUDP(buff)
|
||||
if endpoint != nil {
|
||||
endpoint.IP = endpoint.IP.To4()
|
||||
}
|
||||
return n, (*conn.StdNetEndpoint)(endpoint), err
|
||||
}
|
||||
}
|
||||
|
||||
/*func (bind *ICEBind) receive(buff []byte) (int, conn.Endpoint, error) {
|
||||
n, endpoint, err := bind.conn.ReadFromUDP(buff)
|
||||
if endpoint != nil {
|
||||
endpoint.IP = endpoint.IP.To4()
|
||||
}
|
||||
return n, (*conn.StdNetEndpoint)(endpoint), err
|
||||
return n, (*conn.StdNetEndpoint)(&net.UDPAddr{
|
||||
IP: e.addr().AsSlice(),
|
||||
Port: int(e.Port()),
|
||||
Zone: e.addr().Zone(),
|
||||
}), err*/
|
||||
|
||||
select {
|
||||
case <-bind.closeSignal:
|
||||
case <-b.closeSignal:
|
||||
return 0, nil, net.ErrClosed
|
||||
case pkt := <-bind.packets:
|
||||
return copy(buf, pkt.buff), (*conn.StdNetEndpoint)(&pkt.addr), nil
|
||||
case pkt := <-b.Packets:
|
||||
/*log.Infof("received packet %d from %s to copy to buffer %d", binary.Size(pkt.buff), pkt.addr.String(),
|
||||
len(buff))*/
|
||||
return copy(buff, pkt.buff), (*conn.StdNetEndpoint)(pkt.addr), nil
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
func (bind *ICEBind) Close() error {
|
||||
bind.mu.Lock()
|
||||
defer bind.mu.Unlock()
|
||||
|
||||
err := bind.conn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if bind.closeSignal != nil {
|
||||
func (b *UserBind) Close() error {
|
||||
if b.closeSignal != nil {
|
||||
select {
|
||||
case <-bind.closeSignal:
|
||||
case <-b.closeSignal:
|
||||
default:
|
||||
close(bind.closeSignal)
|
||||
close(b.closeSignal)
|
||||
}
|
||||
bind.packets = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetMark sets the mark for each packet sent through this Bind.
|
||||
// This mark is passed to the kernel as the socket option SO_MARK.
|
||||
func (bind *ICEBind) SetMark(mark uint32) error {
|
||||
func (b *UserBind) SetMark(mark uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bind *ICEBind) Send(buf []byte, endpoint conn.Endpoint) error {
|
||||
|
||||
func (b *UserBind) Send(buff []byte, endpoint conn.Endpoint) error {
|
||||
nend, ok := endpoint.(*conn.StdNetEndpoint)
|
||||
if !ok {
|
||||
return conn.ErrWrongEndpointType
|
||||
}
|
||||
|
||||
_, err := bind.conn.WriteToUDP(buf, (*net.UDPAddr)(nend))
|
||||
//log.Infof("sending packet %d from %s to %s", binary.Size(buff), b.sharedConn.LocalAddr().String(), (*net.UDPAddr)(nend).String())
|
||||
|
||||
_, err := b.sharedConn.WriteTo(buff, (*net.UDPAddr)(nend))
|
||||
return err
|
||||
}
|
||||
|
||||
// ParseEndpoint creates a new endpoint from a string.
|
||||
func (bind *ICEBind) ParseEndpoint(s string) (ep conn.Endpoint, err error) {
|
||||
ap, err := netip.ParseAddrPort(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (b *UserBind) ParseEndpoint(s string) (ep conn.Endpoint, err error) {
|
||||
e, err := netip.ParseAddrPort(s)
|
||||
return (*conn.StdNetEndpoint)(&net.UDPAddr{
|
||||
IP: ap.Addr().AsSlice(),
|
||||
Port: int(ap.Port()),
|
||||
Zone: ap.Addr().Zone(),
|
||||
IP: e.Addr().AsSlice(),
|
||||
Port: int(e.Port()),
|
||||
Zone: e.Addr().Zone(),
|
||||
}), err
|
||||
}
|
||||
|
||||
func listenNet(network string, port int) (*net.UDPConn, int, error) {
|
||||
conn, err := net.ListenUDP(network, &net.UDPAddr{Port: port})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
func (b *UserBind) OnData(buff []byte, addr *net.UDPAddr) {
|
||||
b.Packets <- packet{
|
||||
buff: buff,
|
||||
addr: addr,
|
||||
}
|
||||
|
||||
// Retrieve port.
|
||||
laddr := conn.LocalAddr()
|
||||
uaddr, err := net.ResolveUDPAddr(
|
||||
laddr.Network(),
|
||||
laddr.String(),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return conn, uaddr.Port, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user