Skip to main content
Port multiplexing allows multiple ICE sessions to share a single UDP or TCP port, reducing port usage and simplifying firewall configuration. This guide covers UDPMux, TCPMux, and UniversalUDPMux.

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
Multiplexing solves these issues by sharing ports using ICE username fragments (ufrag) to demultiplex traffic.

UDPMux

UDPMux allows multiple ICE agents to share a single UDP port for host candidates.

Basic Usage

How UDPMux Works

From udp_mux.go:21:
Demultiplexing Process:
  1. UDPMux reads packets from the shared UDP socket
  2. STUN messages are decoded to extract the username attribute
  3. Username is split by : to get the remote ufrag
  4. Packet is routed to the correct agent based on ufrag
  5. Unknown addresses with valid STUN messages create new connections
From udp_mux.go:285:

UDPMuxParams

UDPMuxDefault should not listen on unspecified addresses (0.0.0.0). Use NewMultiUDPMuxFromPort instead for wildcard binding.

UniversalUDPMux

UniversalUDPMux extends UDPMux to support server reflexive candidates on the same port:

How UniversalUDPMux Works

From udp_mux_universal.go:17:
Server Reflexive Handling:
  1. Agent sends STUN binding request through the mux
  2. UniversalUDPMux intercepts STUN responses with XOR-MAPPED-ADDRESS
  3. Caches the mapped address for the STUN server
  4. Returns cached value for subsequent requests (TTL: 25s default)
From udp_mux_universal.go:176:

TCPMux

TCPMux enables multiple ICE agents to share a single TCP listener for ICE-TCP candidates:

How TCPMux Works

From tcp_mux.go:22:
Connection Handling:
  1. TCPMux accepts incoming TCP connections
  2. Waits for first STUN binding request (with timeout)
  3. Extracts ufrag from STUN username attribute
  4. Routes connection to appropriate agent
  5. Creates tcpPacketConn wrapper for the TCP connection
From 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:
Larger buffers reduce packet drops under load but consume more memory. The default write buffer of 4MB is recommended for most applications.

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