2022-03-10 09:44:48 +00:00
|
|
|
package ice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pion/logging"
|
|
|
|
"github.com/pion/stun"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UDPMux allows multiple connections to go over a single UDP port
|
|
|
|
type UDPMux interface {
|
|
|
|
io.Closer
|
2024-01-18 18:54:54 +00:00
|
|
|
GetConn(ufrag string) (net.PacketConn, error)
|
2022-03-10 09:44:48 +00:00
|
|
|
RemoveConnByUfrag(ufrag string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UDPMuxDefault is an implementation of the interface
|
|
|
|
type UDPMuxDefault struct {
|
|
|
|
params UDPMuxParams
|
|
|
|
|
|
|
|
closedChan chan struct{}
|
|
|
|
closeOnce sync.Once
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
// conns is a map of all udpMuxedConn indexed by ufrag|network|candidateType
|
|
|
|
conns map[string]*udpMuxedConn
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
addressMapMu sync.RWMutex
|
|
|
|
addressMap map[string]*udpMuxedConn
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
// buffer pool to recycle buffers for net.UDPAddr encodes/decodes
|
2022-03-10 09:44:48 +00:00
|
|
|
pool *sync.Pool
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
const maxAddrSize = 512
|
|
|
|
|
|
|
|
// UDPMuxParams are parameters for UDPMux.
|
|
|
|
type UDPMuxParams struct {
|
|
|
|
Logger logging.LeveledLogger
|
|
|
|
UDPConn net.PacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUDPMuxDefault creates an implementation of UDPMux
|
|
|
|
func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault {
|
|
|
|
if params.Logger == nil {
|
|
|
|
params.Logger = logging.NewDefaultLoggerFactory().NewLogger("ice")
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &UDPMuxDefault{
|
|
|
|
addressMap: map[string]*udpMuxedConn{},
|
|
|
|
params: params,
|
2024-01-18 18:54:54 +00:00
|
|
|
conns: make(map[string]*udpMuxedConn),
|
2022-03-10 09:44:48 +00:00
|
|
|
closedChan: make(chan struct{}, 1),
|
|
|
|
pool: &sync.Pool{
|
|
|
|
New: func() interface{} {
|
2024-01-18 18:54:54 +00:00
|
|
|
// big enough buffer to fit both packet and address
|
2022-03-10 09:44:48 +00:00
|
|
|
return newBufferHolder(receiveMTU + maxAddrSize)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go m.connWorker()
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the listening address of this UDPMuxDefault
|
|
|
|
func (m *UDPMuxDefault) LocalAddr() net.Addr {
|
|
|
|
return m.params.UDPConn.LocalAddr()
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
// GetConn returns a PacketConn given the connection's ufrag and network
|
2022-03-10 09:44:48 +00:00
|
|
|
// creates the connection if an existing one can't be found
|
2024-01-18 18:54:54 +00:00
|
|
|
func (m *UDPMuxDefault) GetConn(ufrag string) (net.PacketConn, error) {
|
2022-03-10 09:44:48 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
|
|
|
if m.IsClosed() {
|
|
|
|
return nil, io.ErrClosedPipe
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
if c, ok := m.conns[ufrag]; ok {
|
|
|
|
return c, nil
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := m.createMuxedConn(ufrag)
|
|
|
|
go func() {
|
|
|
|
<-c.CloseChannel()
|
2024-01-18 18:54:54 +00:00
|
|
|
m.removeConn(ufrag)
|
2022-03-10 09:44:48 +00:00
|
|
|
}()
|
2024-01-18 18:54:54 +00:00
|
|
|
m.conns[ufrag] = c
|
2022-03-10 09:44:48 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveConnByUfrag stops and removes the muxed packet connection
|
|
|
|
func (m *UDPMuxDefault) RemoveConnByUfrag(ufrag string) {
|
2024-01-03 21:57:33 +00:00
|
|
|
m.mu.Lock()
|
2024-01-18 18:54:54 +00:00
|
|
|
removedConns := make([]*udpMuxedConn, 0)
|
|
|
|
for key := range m.conns {
|
|
|
|
if key != ufrag {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-10 09:44:48 +00:00
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
c := m.conns[key]
|
|
|
|
delete(m.conns, key)
|
|
|
|
if c != nil {
|
|
|
|
removedConns = append(removedConns, c)
|
|
|
|
}
|
2024-01-03 21:57:33 +00:00
|
|
|
}
|
2024-01-18 18:54:54 +00:00
|
|
|
// keep lock section small to avoid deadlock with conn lock
|
|
|
|
m.mu.Unlock()
|
2024-01-03 21:57:33 +00:00
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
m.addressMapMu.Lock()
|
|
|
|
defer m.addressMapMu.Unlock()
|
|
|
|
|
|
|
|
for _, c := range removedConns {
|
|
|
|
addresses := c.getAddresses()
|
|
|
|
for _, addr := range addresses {
|
|
|
|
delete(m.addressMap, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsClosed returns true if the mux had been closed
|
|
|
|
func (m *UDPMuxDefault) IsClosed() bool {
|
|
|
|
select {
|
|
|
|
case <-m.closedChan:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the mux, no further connections could be created
|
|
|
|
func (m *UDPMuxDefault) Close() error {
|
|
|
|
var err error
|
|
|
|
m.closeOnce.Do(func() {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
for _, c := range m.conns {
|
2022-03-10 09:44:48 +00:00
|
|
|
_ = c.Close()
|
|
|
|
}
|
2024-01-18 18:54:54 +00:00
|
|
|
m.conns = make(map[string]*udpMuxedConn)
|
2024-01-03 21:57:33 +00:00
|
|
|
close(m.closedChan)
|
|
|
|
})
|
|
|
|
return err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-01-18 18:54:54 +00:00
|
|
|
func (m *UDPMuxDefault) removeConn(key string) {
|
|
|
|
m.mu.Lock()
|
|
|
|
c := m.conns[key]
|
|
|
|
delete(m.conns, key)
|
|
|
|
// keep lock section small to avoid deadlock with conn lock
|
|
|
|
m.mu.Unlock()
|
|
|
|
|
|
|
|
if c == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.addressMapMu.Lock()
|
|
|
|
defer m.addressMapMu.Unlock()
|
|
|
|
|
|
|
|
addresses := c.getAddresses()
|
|
|
|
for _, addr := range addresses {
|
|
|
|
delete(m.addressMap, addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UDPMuxDefault) writeTo(buf []byte, raddr net.Addr) (n int, err error) {
|
|
|
|
return m.params.UDPConn.WriteTo(buf, raddr)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UDPMuxDefault) registerConnForAddress(conn *udpMuxedConn, addr string) {
|
|
|
|
if m.IsClosed() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.addressMapMu.Lock()
|
|
|
|
defer m.addressMapMu.Unlock()
|
|
|
|
|
|
|
|
existing, ok := m.addressMap[addr]
|
|
|
|
if ok {
|
|
|
|
existing.removeAddress(addr)
|
|
|
|
}
|
|
|
|
m.addressMap[addr] = conn
|
|
|
|
|
|
|
|
m.params.Logger.Debugf("Registered %s for %s", addr, conn.params.Key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UDPMuxDefault) createMuxedConn(key string) *udpMuxedConn {
|
|
|
|
c := newUDPMuxedConn(&udpMuxedConnParams{
|
|
|
|
Mux: m,
|
|
|
|
Key: key,
|
|
|
|
AddrPool: m.pool,
|
|
|
|
LocalAddr: m.LocalAddr(),
|
|
|
|
Logger: m.params.Logger,
|
|
|
|
})
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UDPMuxDefault) connWorker() {
|
|
|
|
logger := m.params.Logger
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = m.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf := make([]byte, receiveMTU)
|
|
|
|
for {
|
|
|
|
n, addr, err := m.params.UDPConn.ReadFrom(buf)
|
|
|
|
if m.IsClosed() {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
if os.IsTimeout(err) {
|
|
|
|
continue
|
2024-01-18 18:54:54 +00:00
|
|
|
} else if err != io.EOF {
|
2022-03-10 09:44:48 +00:00
|
|
|
logger.Errorf("could not read udp packet: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
udpAddr, ok := addr.(*net.UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
logger.Errorf("underlying PacketConn did not return a UDPAddr")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have already seen this address dispatch to the appropriate destination
|
|
|
|
m.addressMapMu.Lock()
|
|
|
|
destinationConn := m.addressMap[addr.String()]
|
|
|
|
m.addressMapMu.Unlock()
|
|
|
|
|
|
|
|
// If we haven't seen this address before but is a STUN packet lookup by ufrag
|
|
|
|
if destinationConn == nil && stun.IsMessage(buf[:n]) {
|
|
|
|
msg := &stun.Message{
|
|
|
|
Raw: append([]byte{}, buf[:n]...),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = msg.Decode(); err != nil {
|
2024-01-18 18:54:54 +00:00
|
|
|
m.params.Logger.Warnf("Failed to handle decode ICE from %s: %v\n", addr.String(), err)
|
2022-03-10 09:44:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
attr, stunAttrErr := msg.Get(stun.AttrUsername)
|
|
|
|
if stunAttrErr != nil {
|
2024-01-18 18:54:54 +00:00
|
|
|
m.params.Logger.Warnf("No Username attribute in STUN message from %s\n", addr.String())
|
2022-03-10 09:44:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ufrag := strings.Split(string(attr), ":")[0]
|
|
|
|
|
|
|
|
m.mu.Lock()
|
2024-01-18 18:54:54 +00:00
|
|
|
destinationConn = m.conns[ufrag]
|
2022-03-10 09:44:48 +00:00
|
|
|
m.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if destinationConn == nil {
|
|
|
|
m.params.Logger.Tracef("dropping packet from %s, addr: %s", udpAddr.String(), addr.String())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = destinationConn.writePacket(buf[:n], udpAddr); err != nil {
|
|
|
|
m.params.Logger.Errorf("could not write packet: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bufferHolder struct {
|
2024-01-18 18:54:54 +00:00
|
|
|
buffer []byte
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBufferHolder(size int) *bufferHolder {
|
|
|
|
return &bufferHolder{
|
2024-01-18 18:54:54 +00:00
|
|
|
buffer: make([]byte, size),
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
}
|