2024-05-15 23:15:00 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
package ice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pion/logging"
|
2024-05-15 23:15:00 +00:00
|
|
|
"github.com/pion/transport/v2/packetio"
|
2022-03-10 09:44:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type udpMuxedConnParams struct {
|
|
|
|
Mux *UDPMuxDefault
|
|
|
|
AddrPool *sync.Pool
|
|
|
|
Key string
|
|
|
|
LocalAddr net.Addr
|
|
|
|
Logger logging.LeveledLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
// udpMuxedConn represents a logical packet conn for a single remote as identified by ufrag
|
|
|
|
type udpMuxedConn struct {
|
|
|
|
params *udpMuxedConnParams
|
2024-05-15 23:15:00 +00:00
|
|
|
// Remote addresses that we have sent to on this conn
|
2022-03-10 09:44:48 +00:00
|
|
|
addresses []string
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Channel holding incoming packets
|
|
|
|
buf *packetio.Buffer
|
2022-03-10 09:44:48 +00:00
|
|
|
closedChan chan struct{}
|
|
|
|
closeOnce sync.Once
|
|
|
|
mu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUDPMuxedConn(params *udpMuxedConnParams) *udpMuxedConn {
|
|
|
|
p := &udpMuxedConn{
|
|
|
|
params: params,
|
2024-05-15 23:15:00 +00:00
|
|
|
buf: packetio.NewBuffer(),
|
2022-03-10 09:44:48 +00:00
|
|
|
closedChan: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (c *udpMuxedConn) ReadFrom(b []byte) (n int, rAddr net.Addr, err error) {
|
|
|
|
buf := c.params.AddrPool.Get().(*bufferHolder) //nolint:forcetypeassert
|
2022-03-10 09:44:48 +00:00
|
|
|
defer c.params.AddrPool.Put(buf)
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Read address
|
|
|
|
total, err := c.buf.Read(buf.buf)
|
2022-03-10 09:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
dataLen := int(binary.LittleEndian.Uint16(buf.buf[:2]))
|
2022-03-10 09:44:48 +00:00
|
|
|
if dataLen > total || dataLen > len(b) {
|
|
|
|
return 0, nil, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Read data and then address
|
2022-03-10 09:44:48 +00:00
|
|
|
offset := 2
|
2024-05-15 23:15:00 +00:00
|
|
|
copy(b, buf.buf[offset:offset+dataLen])
|
2022-03-10 09:44:48 +00:00
|
|
|
offset += dataLen
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Read address len & decode address
|
|
|
|
addrLen := int(binary.LittleEndian.Uint16(buf.buf[offset : offset+2]))
|
2022-03-10 09:44:48 +00:00
|
|
|
offset += 2
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
if rAddr, err = decodeUDPAddr(buf.buf[offset : offset+addrLen]); err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
return dataLen, rAddr, nil
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (c *udpMuxedConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
|
2022-03-10 09:44:48 +00:00
|
|
|
if c.isClosed() {
|
|
|
|
return 0, io.ErrClosedPipe
|
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
// Each time we write to a new address, we'll register it with the mux
|
|
|
|
addr := rAddr.String()
|
2022-03-10 09:44:48 +00:00
|
|
|
if !c.containsAddress(addr) {
|
|
|
|
c.addAddress(addr)
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
return c.params.Mux.writeTo(buf, rAddr)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) LocalAddr() net.Addr {
|
|
|
|
return c.params.LocalAddr
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (c *udpMuxedConn) SetDeadline(time.Time) error {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (c *udpMuxedConn) SetReadDeadline(time.Time) error {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (c *udpMuxedConn) SetWriteDeadline(time.Time) error {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) CloseChannel() <-chan struct{} {
|
|
|
|
return c.closedChan
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) Close() error {
|
|
|
|
var err error
|
|
|
|
c.closeOnce.Do(func() {
|
2024-05-15 23:15:00 +00:00
|
|
|
err = c.buf.Close()
|
2022-03-10 09:44:48 +00:00
|
|
|
close(c.closedChan)
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) isClosed() bool {
|
|
|
|
select {
|
|
|
|
case <-c.closedChan:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) getAddresses() []string {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
addresses := make([]string, len(c.addresses))
|
|
|
|
copy(addresses, c.addresses)
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) addAddress(addr string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
c.addresses = append(c.addresses, addr)
|
|
|
|
c.mu.Unlock()
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Map it on mux
|
2022-03-10 09:44:48 +00:00
|
|
|
c.params.Mux.registerConnForAddress(c, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) removeAddress(addr string) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
|
|
|
newAddresses := make([]string, 0, len(c.addresses))
|
|
|
|
for _, a := range c.addresses {
|
|
|
|
if a != addr {
|
|
|
|
newAddresses = append(newAddresses, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.addresses = newAddresses
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) containsAddress(addr string) bool {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
for _, a := range c.addresses {
|
|
|
|
if addr == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *udpMuxedConn) writePacket(data []byte, addr *net.UDPAddr) error {
|
2024-05-15 23:15:00 +00:00
|
|
|
// Write two packets, address and data
|
|
|
|
buf := c.params.AddrPool.Get().(*bufferHolder) //nolint:forcetypeassert
|
2022-03-10 09:44:48 +00:00
|
|
|
defer c.params.AddrPool.Put(buf)
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Format of buffer | data len | data bytes | addr len | addr bytes |
|
|
|
|
if len(buf.buf) < len(data)+maxAddrSize {
|
2022-03-10 09:44:48 +00:00
|
|
|
return io.ErrShortBuffer
|
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
// Data length
|
|
|
|
binary.LittleEndian.PutUint16(buf.buf, uint16(len(data)))
|
2022-03-10 09:44:48 +00:00
|
|
|
offset := 2
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Data
|
|
|
|
copy(buf.buf[offset:], data)
|
2022-03-10 09:44:48 +00:00
|
|
|
offset += len(data)
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Write address first, leaving room for its length
|
|
|
|
n, err := encodeUDPAddr(addr, buf.buf[offset+2:])
|
2022-03-10 09:44:48 +00:00
|
|
|
if err != nil {
|
2024-05-15 23:15:00 +00:00
|
|
|
return err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
total := offset + n + 2
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// Address len
|
|
|
|
binary.LittleEndian.PutUint16(buf.buf[offset:], uint16(n))
|
2022-03-10 09:44:48 +00:00
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
if _, err := c.buf.Write(buf.buf[:total]); err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeUDPAddr(addr *net.UDPAddr, buf []byte) (int, error) {
|
2024-05-15 23:15:00 +00:00
|
|
|
ipData, err := addr.IP.MarshalText()
|
2022-03-10 09:44:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
total := 2 + len(ipData) + 2 + len(addr.Zone)
|
2022-03-10 09:44:48 +00:00
|
|
|
if total > len(buf) {
|
|
|
|
return 0, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(len(ipData)))
|
2022-03-10 09:44:48 +00:00
|
|
|
offset := 2
|
2024-05-15 23:15:00 +00:00
|
|
|
n := copy(buf[offset:], ipData)
|
2022-03-10 09:44:48 +00:00
|
|
|
offset += n
|
|
|
|
binary.LittleEndian.PutUint16(buf[offset:], uint16(addr.Port))
|
|
|
|
offset += 2
|
|
|
|
copy(buf[offset:], addr.Zone)
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeUDPAddr(buf []byte) (*net.UDPAddr, error) {
|
|
|
|
addr := net.UDPAddr{}
|
|
|
|
|
|
|
|
offset := 0
|
|
|
|
ipLen := int(binary.LittleEndian.Uint16(buf[:2]))
|
|
|
|
offset += 2
|
2024-05-15 23:15:00 +00:00
|
|
|
// Basic bounds checking
|
2022-03-10 09:44:48 +00:00
|
|
|
if ipLen+offset > len(buf) {
|
|
|
|
return nil, io.ErrShortBuffer
|
|
|
|
}
|
|
|
|
if err := addr.IP.UnmarshalText(buf[offset : offset+ipLen]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
offset += ipLen
|
|
|
|
addr.Port = int(binary.LittleEndian.Uint16(buf[offset : offset+2]))
|
|
|
|
offset += 2
|
|
|
|
zone := make([]byte, len(buf[offset:]))
|
|
|
|
copy(zone, buf[offset:])
|
|
|
|
addr.Zone = string(zone)
|
|
|
|
|
|
|
|
return &addr, nil
|
|
|
|
}
|