2024-06-05 20:10:03 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
package turn
|
|
|
|
|
|
|
|
import (
|
|
|
|
b64 "encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pion/logging"
|
|
|
|
"github.com/pion/stun"
|
2024-05-15 23:15:00 +00:00
|
|
|
"github.com/pion/transport/v2"
|
|
|
|
"github.com/pion/transport/v2/stdnet"
|
2022-03-10 09:44:48 +00:00
|
|
|
"github.com/pion/turn/v2/internal/client"
|
|
|
|
"github.com/pion/turn/v2/internal/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultRTO = 200 * time.Millisecond
|
2024-06-05 20:10:03 +00:00
|
|
|
maxRtxCount = 7 // Total 7 requests (Rc)
|
|
|
|
maxDataBufferSize = math.MaxUint16 // Message size limit for Chromium
|
2022-03-10 09:44:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// interval [msec]
|
|
|
|
// 0: 0 ms +500
|
|
|
|
// 1: 500 ms +1000
|
|
|
|
// 2: 1500 ms +2000
|
|
|
|
// 3: 3500 ms +4000
|
|
|
|
// 4: 7500 ms +8000
|
|
|
|
// 5: 15500 ms +16000
|
|
|
|
// 6: 31500 ms +32000
|
|
|
|
// -: 63500 ms failed
|
|
|
|
|
|
|
|
// ClientConfig is a bag of config parameters for Client.
|
|
|
|
type ClientConfig struct {
|
|
|
|
STUNServerAddr string // STUN server address (e.g. "stun.abc.com:3478")
|
2024-05-15 23:15:00 +00:00
|
|
|
TURNServerAddr string // TURN server address (e.g. "turn.abc.com:3478")
|
2022-03-10 09:44:48 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Realm string
|
|
|
|
Software string
|
|
|
|
RTO time.Duration
|
|
|
|
Conn net.PacketConn // Listening socket (net.PacketConn)
|
2024-05-15 23:15:00 +00:00
|
|
|
Net transport.Net
|
2024-06-05 20:10:03 +00:00
|
|
|
LoggerFactory logging.LoggerFactory
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Client is a STUN server client
|
|
|
|
type Client struct {
|
2024-06-05 20:10:03 +00:00
|
|
|
conn net.PacketConn // Read-only
|
|
|
|
net transport.Net // Read-only
|
|
|
|
stunServerAddr net.Addr // Read-only
|
|
|
|
turnServerAddr net.Addr // Read-only
|
|
|
|
|
|
|
|
username stun.Username // Read-only
|
|
|
|
password string // Read-only
|
|
|
|
realm stun.Realm // Read-only
|
|
|
|
integrity stun.MessageIntegrity // Read-only
|
|
|
|
software stun.Software // Read-only
|
|
|
|
trMap *client.TransactionMap // Thread-safe
|
|
|
|
rto time.Duration // Read-only
|
|
|
|
relayedConn *client.UDPConn // Protected by mutex ***
|
|
|
|
tcpAllocation *client.TCPAllocation // Protected by mutex ***
|
|
|
|
allocTryLock client.TryLock // Thread-safe
|
|
|
|
listenTryLock client.TryLock // Thread-safe
|
|
|
|
mutex sync.RWMutex // Thread-safe
|
|
|
|
mutexTrMap sync.Mutex // Thread-safe
|
|
|
|
log logging.LeveledLogger // Read-only
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns a new Client instance. listeningAddress is the address and port to listen on, default "0.0.0.0:0"
|
|
|
|
func NewClient(config *ClientConfig) (*Client, error) {
|
|
|
|
loggerFactory := config.LoggerFactory
|
|
|
|
if loggerFactory == nil {
|
|
|
|
loggerFactory = logging.NewDefaultLoggerFactory()
|
|
|
|
}
|
|
|
|
|
|
|
|
log := loggerFactory.NewLogger("turnc")
|
|
|
|
|
|
|
|
if config.Conn == nil {
|
|
|
|
return nil, errNilConn
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
rto := defaultRTO
|
|
|
|
if config.RTO > 0 {
|
|
|
|
rto = config.RTO
|
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
if config.Net == nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
n, err := stdnet.NewNet()
|
2024-05-15 23:15:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
config.Net = n
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var stunServ, turnServ net.Addr
|
2024-06-05 20:10:03 +00:00
|
|
|
var err error
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
if len(config.STUNServerAddr) > 0 {
|
|
|
|
stunServ, err = config.Net.ResolveUDPAddr("udp4", config.STUNServerAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
|
|
|
|
log.Debugf("Resolved STUN server %s to %s", config.STUNServerAddr, stunServ)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
if len(config.TURNServerAddr) > 0 {
|
|
|
|
turnServ, err = config.Net.ResolveUDPAddr("udp4", config.TURNServerAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
log.Debugf("Resolved TURN server %s to %s", config.TURNServerAddr, turnServ)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &Client{
|
2024-06-05 20:10:03 +00:00
|
|
|
conn: config.Conn,
|
|
|
|
stunServerAddr: stunServ,
|
|
|
|
turnServerAddr: turnServ,
|
|
|
|
username: stun.NewUsername(config.Username),
|
|
|
|
password: config.Password,
|
|
|
|
realm: stun.NewRealm(config.Realm),
|
|
|
|
software: stun.NewSoftware(config.Software),
|
|
|
|
trMap: client.NewTransactionMap(),
|
|
|
|
net: config.Net,
|
|
|
|
rto: rto,
|
|
|
|
log: log,
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TURNServerAddr return the TURN server address
|
|
|
|
func (c *Client) TURNServerAddr() net.Addr {
|
2024-06-05 20:10:03 +00:00
|
|
|
return c.turnServerAddr
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// STUNServerAddr return the STUN server address
|
|
|
|
func (c *Client) STUNServerAddr() net.Addr {
|
2024-06-05 20:10:03 +00:00
|
|
|
return c.stunServerAddr
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Username returns username
|
|
|
|
func (c *Client) Username() stun.Username {
|
|
|
|
return c.username
|
|
|
|
}
|
|
|
|
|
|
|
|
// Realm return realm
|
|
|
|
func (c *Client) Realm() stun.Realm {
|
|
|
|
return c.realm
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo sends data to the specified destination using the base socket.
|
|
|
|
func (c *Client) WriteTo(data []byte, to net.Addr) (int, error) {
|
|
|
|
return c.conn.WriteTo(data, to)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen will have this client start listening on the conn provided via the config.
|
|
|
|
// This is optional. If not used, you will need to call HandleInbound method
|
|
|
|
// to supply incoming data, instead.
|
|
|
|
func (c *Client) Listen() error {
|
|
|
|
if err := c.listenTryLock.Lock(); err != nil {
|
|
|
|
return fmt.Errorf("%w: %s", errAlreadyListening, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
buf := make([]byte, maxDataBufferSize)
|
|
|
|
for {
|
|
|
|
n, from, err := c.conn.ReadFrom(buf)
|
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debugf("Failed to read: %s. Exiting loop", err)
|
2022-03-10 09:44:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = c.HandleInbound(buf[:n], from)
|
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debugf("Failed to handle inbound message: %s. Exiting loop", err)
|
2022-03-10 09:44:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.listenTryLock.Unlock()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes this client
|
|
|
|
func (c *Client) Close() {
|
|
|
|
c.mutexTrMap.Lock()
|
|
|
|
defer c.mutexTrMap.Unlock()
|
|
|
|
|
|
|
|
c.trMap.CloseAndDeleteAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionID & Base64: https://play.golang.org/p/EEgmJDI971P
|
|
|
|
|
|
|
|
// SendBindingRequestTo sends a new STUN request to the given transport address
|
|
|
|
func (c *Client) SendBindingRequestTo(to net.Addr) (net.Addr, error) {
|
|
|
|
attrs := []stun.Setter{stun.TransactionID, stun.BindingRequest}
|
|
|
|
if len(c.software) > 0 {
|
|
|
|
attrs = append(attrs, c.software)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := stun.Build(attrs...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trRes, err := c.PerformTransaction(msg, to, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var reflAddr stun.XORMappedAddress
|
|
|
|
if err := reflAddr.GetFrom(trRes.Msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &net.UDPAddr{
|
|
|
|
IP: reflAddr.IP,
|
|
|
|
Port: reflAddr.Port,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendBindingRequest sends a new STUN request to the STUN server
|
|
|
|
func (c *Client) SendBindingRequest() (net.Addr, error) {
|
2024-06-05 20:10:03 +00:00
|
|
|
if c.stunServerAddr == nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil, errSTUNServerAddressNotSet
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
return c.SendBindingRequestTo(c.stunServerAddr)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
func (c *Client) sendAllocateRequest(protocol proto.Protocol) (proto.RelayedAddress, proto.Lifetime, stun.Nonce, error) {
|
|
|
|
var relayed proto.RelayedAddress
|
|
|
|
var lifetime proto.Lifetime
|
|
|
|
var nonce stun.Nonce
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
msg, err := stun.Build(
|
|
|
|
stun.TransactionID,
|
|
|
|
stun.NewType(stun.MethodAllocate, stun.ClassRequest),
|
2024-06-05 20:10:03 +00:00
|
|
|
proto.RequestedTransport{Protocol: protocol},
|
2022-03-10 09:44:48 +00:00
|
|
|
stun.Fingerprint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
trRes, err := c.PerformTransaction(msg, c.turnServerAddr, false)
|
2022-03-10 09:44:48 +00:00
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := trRes.Msg
|
|
|
|
|
|
|
|
// Anonymous allocate failed, trying to authenticate.
|
|
|
|
if err = nonce.GetFrom(res); err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
if err = c.realm.GetFrom(res); err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
c.realm = append([]byte(nil), c.realm...)
|
|
|
|
c.integrity = stun.NewLongTermIntegrity(
|
|
|
|
c.username.String(), c.realm.String(), c.password,
|
|
|
|
)
|
|
|
|
// Trying to authorize.
|
|
|
|
msg, err = stun.Build(
|
|
|
|
stun.TransactionID,
|
|
|
|
stun.NewType(stun.MethodAllocate, stun.ClassRequest),
|
2024-06-05 20:10:03 +00:00
|
|
|
proto.RequestedTransport{Protocol: protocol},
|
2022-03-10 09:44:48 +00:00
|
|
|
&c.username,
|
|
|
|
&c.realm,
|
|
|
|
&nonce,
|
|
|
|
&c.integrity,
|
|
|
|
stun.Fingerprint,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
trRes, err = c.PerformTransaction(msg, c.turnServerAddr, false)
|
2022-03-10 09:44:48 +00:00
|
|
|
if err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
res = trRes.Msg
|
|
|
|
|
|
|
|
if res.Type.Class == stun.ClassErrorResponse {
|
|
|
|
var code stun.ErrorCodeAttribute
|
|
|
|
if err = code.GetFrom(res); err == nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, fmt.Errorf("%s (error %s)", res.Type, code) //nolint:goerr113
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, fmt.Errorf("%s", res.Type) //nolint:goerr113
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getting relayed addresses from response.
|
|
|
|
if err := relayed.GetFrom(res); err != nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
return relayed, lifetime, nonce, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getting lifetime from response
|
|
|
|
if err := lifetime.GetFrom(res); err != nil {
|
|
|
|
return relayed, lifetime, nonce, err
|
|
|
|
}
|
|
|
|
return relayed, lifetime, nonce, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate sends a TURN allocation request to the given transport address
|
|
|
|
func (c *Client) Allocate() (net.PacketConn, error) {
|
|
|
|
if err := c.allocTryLock.Lock(); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errOneAllocateOnly, err.Error())
|
|
|
|
}
|
|
|
|
defer c.allocTryLock.Unlock()
|
|
|
|
|
|
|
|
relayedConn := c.relayedUDPConn()
|
|
|
|
if relayedConn != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errAlreadyAllocated, relayedConn.LocalAddr().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
relayed, lifetime, nonce, err := c.sendAllocateRequest(proto.ProtoUDP)
|
|
|
|
if err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
relayedAddr := &net.UDPAddr{
|
|
|
|
IP: relayed.IP,
|
|
|
|
Port: relayed.Port,
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
relayedConn = client.NewUDPConn(&client.AllocationConfig{
|
|
|
|
Client: c,
|
|
|
|
RelayedAddr: relayedAddr,
|
|
|
|
ServerAddr: c.turnServerAddr,
|
|
|
|
Realm: c.realm,
|
|
|
|
Username: c.username,
|
|
|
|
Integrity: c.integrity,
|
|
|
|
Nonce: nonce,
|
|
|
|
Lifetime: lifetime.Duration,
|
|
|
|
Net: c.net,
|
|
|
|
Log: c.log,
|
|
|
|
})
|
|
|
|
c.setRelayedUDPConn(relayedConn)
|
|
|
|
|
|
|
|
return relayedConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AllocateTCP creates a new TCP allocation at the TURN server.
|
|
|
|
func (c *Client) AllocateTCP() (*client.TCPAllocation, error) {
|
|
|
|
if err := c.allocTryLock.Lock(); err != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errOneAllocateOnly, err.Error())
|
|
|
|
}
|
|
|
|
defer c.allocTryLock.Unlock()
|
|
|
|
|
|
|
|
allocation := c.getTCPAllocation()
|
|
|
|
if allocation != nil {
|
|
|
|
return nil, fmt.Errorf("%w: %s", errAlreadyAllocated, allocation.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
relayed, lifetime, nonce, err := c.sendAllocateRequest(proto.ProtoTCP)
|
|
|
|
if err != nil {
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
relayedAddr := &net.TCPAddr{
|
|
|
|
IP: relayed.IP,
|
|
|
|
Port: relayed.Port,
|
|
|
|
}
|
|
|
|
|
|
|
|
allocation = client.NewTCPAllocation(&client.AllocationConfig{
|
|
|
|
Client: c,
|
2022-03-10 09:44:48 +00:00
|
|
|
RelayedAddr: relayedAddr,
|
2024-06-05 20:10:03 +00:00
|
|
|
ServerAddr: c.turnServerAddr,
|
|
|
|
Realm: c.realm,
|
|
|
|
Username: c.username,
|
2022-03-10 09:44:48 +00:00
|
|
|
Integrity: c.integrity,
|
|
|
|
Nonce: nonce,
|
|
|
|
Lifetime: lifetime.Duration,
|
2024-06-05 20:10:03 +00:00
|
|
|
Net: c.net,
|
2022-03-10 09:44:48 +00:00
|
|
|
Log: c.log,
|
|
|
|
})
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
c.setTCPAllocation(allocation)
|
2022-03-10 09:44:48 +00:00
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
return allocation, nil
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// CreatePermission Issues a CreatePermission request for the supplied addresses
|
|
|
|
// as described in https://datatracker.ietf.org/doc/html/rfc5766#section-9
|
|
|
|
func (c *Client) CreatePermission(addrs ...net.Addr) error {
|
2024-06-05 20:10:03 +00:00
|
|
|
if conn := c.relayedUDPConn(); conn != nil {
|
|
|
|
if err := conn.CreatePermissions(addrs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if allocation := c.getTCPAllocation(); allocation != nil {
|
|
|
|
if err := allocation.CreatePermissions(addrs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2024-05-15 23:15:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
// PerformTransaction performs STUN transaction
|
|
|
|
func (c *Client) PerformTransaction(msg *stun.Message, to net.Addr, ignoreResult bool) (client.TransactionResult,
|
2024-05-15 23:15:00 +00:00
|
|
|
error,
|
|
|
|
) {
|
2022-03-10 09:44:48 +00:00
|
|
|
trKey := b64.StdEncoding.EncodeToString(msg.TransactionID[:])
|
|
|
|
|
|
|
|
raw := make([]byte, len(msg.Raw))
|
|
|
|
copy(raw, msg.Raw)
|
|
|
|
|
|
|
|
tr := client.NewTransaction(&client.TransactionConfig{
|
|
|
|
Key: trKey,
|
|
|
|
Raw: raw,
|
|
|
|
To: to,
|
|
|
|
Interval: c.rto,
|
|
|
|
IgnoreResult: ignoreResult,
|
|
|
|
})
|
|
|
|
|
|
|
|
c.trMap.Insert(trKey, tr)
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Tracef("Start %s transaction %s to %s", msg.Type, trKey, tr.To)
|
2022-03-10 09:44:48 +00:00
|
|
|
_, err := c.conn.WriteTo(tr.Raw, to)
|
|
|
|
if err != nil {
|
|
|
|
return client.TransactionResult{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tr.StartRtxTimer(c.onRtxTimeout)
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
// If ignoreResult is true, get the transaction going and return immediately
|
2022-03-10 09:44:48 +00:00
|
|
|
if ignoreResult {
|
|
|
|
return client.TransactionResult{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := tr.WaitForResult()
|
|
|
|
if res.Err != nil {
|
|
|
|
return res, res.Err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
// OnDeallocated is called when de-allocation of relay address has been complete.
|
2022-03-10 09:44:48 +00:00
|
|
|
// (Called by UDPConn)
|
2024-06-05 20:10:03 +00:00
|
|
|
func (c *Client) OnDeallocated(net.Addr) {
|
2022-03-10 09:44:48 +00:00
|
|
|
c.setRelayedUDPConn(nil)
|
2024-06-05 20:10:03 +00:00
|
|
|
c.setTCPAllocation(nil)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleInbound handles data received.
|
2024-05-15 23:15:00 +00:00
|
|
|
// This method handles incoming packet de-multiplex it by the source address
|
2022-03-10 09:44:48 +00:00
|
|
|
// and the types of the message.
|
2024-05-15 23:15:00 +00:00
|
|
|
// This return a boolean (handled or not) and if there was an error.
|
2022-03-10 09:44:48 +00:00
|
|
|
// Caller should check if the packet was handled by this client or not.
|
|
|
|
// If not handled, it is assumed that the packet is application data.
|
|
|
|
// If an error is returned, the caller should discard the packet regardless.
|
|
|
|
func (c *Client) HandleInbound(data []byte, from net.Addr) (bool, error) {
|
|
|
|
// +-------------------+-------------------------------+
|
|
|
|
// | Return Values | |
|
|
|
|
// +-------------------+ Meaning / Action |
|
|
|
|
// | handled | error | |
|
|
|
|
// |=========+=========+===============================+
|
|
|
|
// | false | nil | Handle the packet as app data |
|
|
|
|
// |---------+---------+-------------------------------+
|
|
|
|
// | true | nil | Nothing to do |
|
|
|
|
// |---------+---------+-------------------------------+
|
|
|
|
// | false | error | (shouldn't happen) |
|
|
|
|
// |---------+---------+-------------------------------+
|
|
|
|
// | true | error | Error occurred while handling |
|
|
|
|
// +---------+---------+-------------------------------+
|
|
|
|
// Possible causes of the error:
|
|
|
|
// - Malformed packet (parse error)
|
|
|
|
// - STUN message was a request
|
|
|
|
// - Non-STUN message from the STUN server
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case stun.IsMessage(data):
|
|
|
|
return true, c.handleSTUNMessage(data, from)
|
|
|
|
case proto.IsChannelData(data):
|
|
|
|
return true, c.handleChannelData(data)
|
2024-06-05 20:10:03 +00:00
|
|
|
case c.stunServerAddr != nil && from.String() == c.stunServerAddr.String():
|
|
|
|
// Received from STUN server but it is not a STUN message
|
2022-03-10 09:44:48 +00:00
|
|
|
return true, errNonSTUNMessage
|
|
|
|
default:
|
2024-06-05 20:10:03 +00:00
|
|
|
// Assume, this is an application data
|
|
|
|
c.log.Tracef("Ignoring non-STUN/TURN packet")
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) handleSTUNMessage(data []byte, from net.Addr) error {
|
|
|
|
raw := make([]byte, len(data))
|
|
|
|
copy(raw, data)
|
|
|
|
|
|
|
|
msg := &stun.Message{Raw: raw}
|
|
|
|
if err := msg.Decode(); err != nil {
|
|
|
|
return fmt.Errorf("%w: %s", errFailedToDecodeSTUN, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type.Class == stun.ClassRequest {
|
|
|
|
return fmt.Errorf("%w : %s", errUnexpectedSTUNRequestMessage, msg.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Type.Class == stun.ClassIndication {
|
2024-06-05 20:10:03 +00:00
|
|
|
switch msg.Type.Method {
|
|
|
|
case stun.MethodData:
|
2022-03-10 09:44:48 +00:00
|
|
|
var peerAddr proto.PeerAddress
|
|
|
|
if err := peerAddr.GetFrom(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
from = &net.UDPAddr{
|
|
|
|
IP: peerAddr.IP,
|
|
|
|
Port: peerAddr.Port,
|
|
|
|
}
|
|
|
|
|
|
|
|
var data proto.Data
|
|
|
|
if err := data.GetFrom(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Tracef("Data indication received from %s", from)
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
relayedConn := c.relayedUDPConn()
|
|
|
|
if relayedConn == nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debug("No relayed conn allocated")
|
|
|
|
return nil // Silently discard
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
relayedConn.HandleInbound(data, from)
|
2024-06-05 20:10:03 +00:00
|
|
|
case stun.MethodConnectionAttempt:
|
|
|
|
var peerAddr proto.PeerAddress
|
|
|
|
if err := peerAddr.GetFrom(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := &net.TCPAddr{
|
|
|
|
IP: peerAddr.IP,
|
|
|
|
Port: peerAddr.Port,
|
|
|
|
}
|
|
|
|
|
|
|
|
var cid proto.ConnectionID
|
|
|
|
if err := cid.GetFrom(msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.log.Debugf("Connection attempt from %s", addr)
|
|
|
|
|
|
|
|
allocation := c.getTCPAllocation()
|
|
|
|
if allocation == nil {
|
|
|
|
c.log.Debug("No TCP allocation exists")
|
|
|
|
return nil // Silently discard
|
|
|
|
}
|
|
|
|
|
|
|
|
allocation.HandleConnectionAttempt(addr, cid)
|
|
|
|
default:
|
|
|
|
c.log.Debug("Received unsupported STUN method")
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a STUN response message (transactional)
|
|
|
|
// The type is either:
|
|
|
|
// - stun.ClassSuccessResponse
|
|
|
|
// - stun.ClassErrorResponse
|
|
|
|
|
|
|
|
trKey := b64.StdEncoding.EncodeToString(msg.TransactionID[:])
|
|
|
|
|
|
|
|
c.mutexTrMap.Lock()
|
|
|
|
tr, ok := c.trMap.Find(trKey)
|
|
|
|
if !ok {
|
|
|
|
c.mutexTrMap.Unlock()
|
2024-06-05 20:10:03 +00:00
|
|
|
// Silently discard
|
|
|
|
c.log.Debugf("No transaction for %s", msg)
|
2022-03-10 09:44:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// End the transaction
|
|
|
|
tr.StopRtxTimer()
|
|
|
|
c.trMap.Delete(trKey)
|
|
|
|
c.mutexTrMap.Unlock()
|
|
|
|
|
|
|
|
if !tr.WriteResult(client.TransactionResult{
|
|
|
|
Msg: msg,
|
|
|
|
From: from,
|
|
|
|
Retries: tr.Retries(),
|
|
|
|
}) {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debugf("No listener for %s", msg)
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) handleChannelData(data []byte) error {
|
|
|
|
chData := &proto.ChannelData{
|
|
|
|
Raw: make([]byte, len(data)),
|
|
|
|
}
|
|
|
|
copy(chData.Raw, data)
|
|
|
|
if err := chData.Decode(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relayedConn := c.relayedUDPConn()
|
|
|
|
if relayedConn == nil {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debug("No relayed conn allocated")
|
|
|
|
return nil // Silently discard
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
addr, ok := relayedConn.FindAddrByChannelNumber(uint16(chData.Number))
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("%w: %d", errChannelBindNotFound, int(chData.Number))
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Tracef("Channel data received from %s (ch=%d)", addr.String(), int(chData.Number))
|
2022-03-10 09:44:48 +00:00
|
|
|
|
|
|
|
relayedConn.HandleInbound(chData.Data, addr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) onRtxTimeout(trKey string, nRtx int) {
|
|
|
|
c.mutexTrMap.Lock()
|
|
|
|
defer c.mutexTrMap.Unlock()
|
|
|
|
|
|
|
|
tr, ok := c.trMap.Find(trKey)
|
|
|
|
if !ok {
|
2024-06-05 20:10:03 +00:00
|
|
|
return // Already gone
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nRtx == maxRtxCount {
|
2024-06-05 20:10:03 +00:00
|
|
|
// All retransmissions failed
|
2022-03-10 09:44:48 +00:00
|
|
|
c.trMap.Delete(trKey)
|
|
|
|
if !tr.WriteResult(client.TransactionResult{
|
|
|
|
Err: fmt.Errorf("%w %s", errAllRetransmissionsFailed, trKey),
|
|
|
|
}) {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debug("No listener for transaction")
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Tracef("Retransmitting transaction %s to %s (nRtx=%d)",
|
2022-03-10 09:44:48 +00:00
|
|
|
trKey, tr.To.String(), nRtx)
|
|
|
|
_, err := c.conn.WriteTo(tr.Raw, tr.To)
|
|
|
|
if err != nil {
|
|
|
|
c.trMap.Delete(trKey)
|
|
|
|
if !tr.WriteResult(client.TransactionResult{
|
|
|
|
Err: fmt.Errorf("%w %s", errFailedToRetransmitTransaction, trKey),
|
|
|
|
}) {
|
2024-06-05 20:10:03 +00:00
|
|
|
c.log.Debug("No listener for transaction")
|
2022-03-10 09:44:48 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tr.StartRtxTimer(c.onRtxTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) setRelayedUDPConn(conn *client.UDPConn) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
c.relayedConn = conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) relayedUDPConn() *client.UDPConn {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
|
|
|
|
return c.relayedConn
|
|
|
|
}
|
2024-06-05 20:10:03 +00:00
|
|
|
|
|
|
|
func (c *Client) setTCPAllocation(alloc *client.TCPAllocation) {
|
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
c.tcpAllocation = alloc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getTCPAllocation() *client.TCPAllocation {
|
|
|
|
c.mutex.RLock()
|
|
|
|
defer c.mutex.RUnlock()
|
|
|
|
|
|
|
|
return c.tcpAllocation
|
|
|
|
}
|