Why Multiplexing?
Without multiplexing, each ICE agent allocates separate ports for each network interface and candidate type. This can:- Exhaust available ports on busy servers
- Require complex firewall rules
- Increase NAT mapping overhead
- Complicate containerized deployments
UDPMux
UDPMux allows multiple ICE agents to share a single UDP port for host candidates.Basic Usage
How UDPMux Works
Fromudp_mux.go:21:
- UDPMux reads packets from the shared UDP socket
- STUN messages are decoded to extract the username attribute
- Username is split by
:to get the remote ufrag - Packet is routed to the correct agent based on ufrag
- Unknown addresses with valid STUN messages create new connections
udp_mux.go:285:
UDPMuxParams
UniversalUDPMux
UniversalUDPMux extends UDPMux to support server reflexive candidates on the same port:How UniversalUDPMux Works
Fromudp_mux_universal.go:17:
- Agent sends STUN binding request through the mux
- UniversalUDPMux intercepts STUN responses with XOR-MAPPED-ADDRESS
- Caches the mapped address for the STUN server
- Returns cached value for subsequent requests (TTL: 25s default)
udp_mux_universal.go:176:
TCPMux
TCPMux enables multiple ICE agents to share a single TCP listener for ICE-TCP candidates:How TCPMux Works
Fromtcp_mux.go:22:
- TCPMux accepts incoming TCP connections
- Waits for first STUN binding request (with timeout)
- Extracts ufrag from STUN username attribute
- Routes connection to appropriate agent
- Creates tcpPacketConn wrapper for the TCP connection
tcp_mux.go:196:
TCPMuxParams
TCPMux creates passive (server) TCP candidates. Active (client) TCP candidates are created automatically when remote passive candidates are added (unless disabled with
WithDisableActiveTCP()).TCP Packet Framing
ICE-TCP uses 2-byte length framing as defined in RFC 4571:Port Sharing Patterns
Single Port for Everything
Share one port across all agents and candidate types:Separate Muxes for Different Roles
Use different muxes for different purposes:Performance Considerations
Buffer Sizing
For TCPMux, configure appropriate buffer sizes:Connection Timeouts
Configure timeouts to prevent resource exhaustion:Limitations
UDPMux Limitations
- Port range configuration is ignored when UDPMux is used
- Each agent must have a unique ufrag
- Unspecified address (0.0.0.0) requires special handling
TCPMux Limitations
- Only passive TCP candidates are directly supported
- Active TCP candidates require additional setup
- Disable active TCP with
WithDisableActiveTCP()if not needed
General Limitations
- Multiplexing increases complexity of debugging
- Single point of failure (shared socket)
- Potential performance bottleneck under high load
Example: Complete Multiplexing Setup
Troubleshooting
No Candidates with UDPMux
- Verify
WithUDPMux()is set on the agent - Check that the UDP socket is bound correctly
- Ensure network types include UDP variants
TCPMux Connection Failures
- Verify first STUN binding arrives within timeout
- Check firewall allows incoming TCP connections
- Enable debug logging to see connection handling
Ufrag Conflicts
- Ensure each agent has a unique ufrag
- Use
WithLocalCredentials()for explicit control - Check for ufrag reuse across agents
Next Steps
Configuration
Configure ICE agent options
NAT Traversal
Set up NAT traversal with address rewriting
Gathering
Learn about candidate gathering
Examples
See multiplexing examples