Overview
UDP Mux allows multiple ICE connections to share a single UDP port by demultiplexing packets based on the username fragment (ufrag) from STUN messages. This enables efficient port usage and is particularly useful when you need to handle multiple concurrent WebRTC connections.UDPMux Interface
TheUDPMux interface defines the contract for UDP multiplexing implementations.
Methods
Returns a
PacketConn for the given username fragment and address. Creates a new connection if one doesn’t exist for the specified ufrag.Parameters:ufrag- The username fragment from the ICE credentialsaddr- The network address to bind to
net.PacketConn or an error if the connection cannot be createdStops and removes the muxed packet connection associated with the given username fragment.Parameters:
ufrag- The username fragment identifying the connection to remove
Returns the list of addresses that this mux is listening on.Returns: A slice of
net.Addr representing all listening addressesCloses the mux and all associated connections. No further connections can be created after closing.Returns: An error if the close operation fails
UDPMuxDefault
UDPMuxDefault is the default implementation of the UDPMux interface.
Creating a UDPMuxDefault
UDPMuxParams
Logger instance for the mux. If nil, a default logger will be created.
The UDP connection to multiplex. This should be bound to a specific address.
Network transport interface. Required when the UDPConn binds to an unspecified address.
Example: Basic UDP Mux
When using an unspecified address (0.0.0.0 or ::), the mux will automatically detect all local interfaces. However, it’s recommended to use
NewMultiUDPMuxFromPort instead for better control.MultiUDPMuxDefault
MultiUDPMuxDefault allows multiple UDPMux instances to be used together, enabling listening on multiple ports or addresses simultaneously.
Creating a MultiUDPMuxDefault
UDPMux instances.
Configuration Options
Filter to determine which network interfaces should be used.
Filter to determine which IP addresses should be used.
Specify which network types to use (IPv4, IPv6, or both).
Set the UDP connection read buffer size in bytes.
Set the UDP connection write buffer size in bytes.
Set a custom logger for the mux.
Include loopback interfaces in the mux.
Set a custom network transport implementation.
Example: Multi-Interface UDP Mux
Port Sharing Concepts
How It Works
- Packet Demultiplexing: When a packet arrives on the shared UDP port, the mux examines it to determine which connection it belongs to.
- STUN Username: For STUN packets, the mux extracts the username attribute and uses the first part (before the colon) as the ufrag to route the packet.
- Address Mapping: After the first STUN packet, the mux remembers the remote address and can route subsequent packets (including media) based on the source address.
- Connection Lifecycle: Each muxed connection is independent and can be closed without affecting others sharing the same port.
Benefits
- Reduced Port Usage: Handle multiple WebRTC connections on a single UDP port
- NAT Traversal: Simplified firewall configuration with fewer ports to open
- Scalability: Better resource utilization for applications with many concurrent connections
- IPv4 and IPv6: Separate connection tracking for IPv4 and IPv6 traffic
Limitations
- The first packet from each remote peer must be a STUN message with a USERNAME attribute
- Each ufrag should be unique across all connections using the mux
- Performance may be impacted with very high numbers of concurrent connections (thousands)
Best Practices
- Use Specific Addresses: When possible, bind to specific IP addresses rather than 0.0.0.0 for better security and predictability.
-
Buffer Sizes: For high-throughput applications, configure appropriate read and write buffer sizes:
-
Cleanup: Always defer
Close()to ensure proper cleanup of resources: -
Interface Filtering: In containerized environments, filter out virtual interfaces:
Related
- TCP Mux - TCP connection multiplexing
- Universal Mux - Combined UDP mux with STUN/TURN support
- Agent Options - Using muxes with ICE agents