Overview
TCP Mux enables multiplexing of TCP connections for ICE, allowing multiple ICE connections to share TCP ports. This is particularly useful for ICE-TCP scenarios where UDP is blocked or unavailable. Unlike UDP where packets can be easily demultiplexed, TCP is connection-oriented. The TCP Mux accepts incoming TCP connections and groups them by username fragment (ufrag) after inspecting the first STUN message, then presents them asPacketConn interfaces for compatibility with the ICE agent.
TCPMux Interface
TheTCPMux interface defines the contract for TCP multiplexing implementations.
Methods
Returns a
PacketConn for the given username fragment. Creates a new connection if one doesn’t exist.Parameters:ufrag- The username fragment from the ICE credentialsisIPv6- Whether this is an IPv6 connectionlocal- The local IP address to bind to
net.PacketConn that aggregates TCP connections for this ufragCloses and removes all TCP connections associated with the given username fragment.Parameters:
ufrag- The username fragment identifying the connections to remove
Closes the listener and all associated connections. No further connections can be created after closing.Returns: An error if the close operation fails
TCPMuxDefault
TCPMuxDefault is the default implementation of the TCPMux interface. It accepts TCP connections, reads the first STUN packet to extract the ufrag, and multiplexes connections accordingly.
Creating a TCPMuxDefault
TCPMuxParams
The TCP listener that accepts incoming connections.
Logger instance for the mux. If nil, a default logger will be created.
Maximum buffer size for read operations. Determines how many packets can be buffered per connection.
Maximum buffer size for write operations. If set to 0, writes block until the packet is sent. If the buffer is full, subsequent packets are dropped.Recommended: 4MB (4 * 1024 * 1024) for production use.
Timeout for receiving the first STUN binding request after a connection is established. Connections that don’t send a STUN message within this timeout are closed.Default: 30 seconds
Timeout for connections created from unknown STUN usernames. If the connection is not used within this duration, it will be removed.Default: 30 seconds
Example: Basic TCP Mux
The TCP mux automatically starts accepting connections when created. Make sure your listener is properly configured before passing it to
NewTCPMuxDefault.MultiTCPMuxDefault
MultiTCPMuxDefault allows multiple TCPMux instances to be used together, enabling listening on multiple ports or addresses simultaneously. It implements both TCPMux and AllConnsGetter interfaces.
Creating a MultiTCPMuxDefault
TCPMux instances.
AllConnsGetter Interface
TheAllConnsGetter interface provides a method to get connections from all underlying muxes:
Returns a
PacketConn for each underlying TCP mux, allowing the ICE agent to use multiple TCP ports simultaneously.Parameters:ufrag- The username fragment from the ICE credentialsisIPv6- Whether this is an IPv6 connectionlocalIP- The local IP address to bind to
net.PacketConn, one for each muxExample: Multiple TCP Ports
TCP Packet Framing
TCP is a stream protocol, so packets need to be framed. The TCP mux uses RFC 4571 framing:- Each packet is prefixed with a 2-byte length header (big-endian)
- The length indicates the size of the packet that follows
- This allows the receiver to know where one packet ends and the next begins
The framing is handled automatically by the TCP mux. You don’t need to implement it yourself when using the
PacketConn interface.How TCP Mux Works
- Accept Connection: The mux accepts a new TCP connection from a remote peer.
-
First Packet Timeout: The connection must send a STUN binding request within
FirstStunBindTimeout(default 30s) or it will be closed. - Extract Ufrag: The mux reads the first STUN packet, extracts the USERNAME attribute, and uses the first part (before the colon) as the ufrag.
-
Connection Assignment: The TCP connection is assigned to the
PacketConnassociated with that ufrag. If no connection exists, one is created. -
Packet Routing: All subsequent packets on that TCP connection are routed to the same
PacketConn. -
Multiple Connections: A single ufrag can have multiple TCP connections from different remote addresses, all multiplexed into the same
PacketConn.
Use Cases
Enterprise Networks
Many corporate networks block UDP traffic entirely. TCP Mux enables WebRTC to work in these environments:Firewall-Friendly Applications
Reduce the number of ports that need to be opened in firewalls:High-Security Environments
Some environments require all traffic to go over TCP for deep packet inspection:Best Practices
-
Write Buffer Size: Always set a write buffer size for production. Without it, writes block, which can cause performance issues:
-
Timeout Configuration: Adjust timeouts based on your network conditions:
-
Resource Management: Always close the mux to clean up resources:
-
Dual Stack: Support both IPv4 and IPv6 by creating separate muxes:
- Error Handling: Monitor logs for connection failures and adjust timeouts accordingly.
Performance Considerations
TCP vs UDP PerformanceTCP multiplexing has higher overhead than UDP multiplexing due to:
- Connection establishment (3-way handshake)
- Connection state management
- Packet framing overhead (2 bytes per packet)
- Head-of-line blocking in the TCP stack
Memory Usage
Each TCP connection maintains:- Read buffer (configurable via
ReadBufferSize) - Write buffer (configurable via
WriteBufferSize) - TCP stack buffers (OS-managed)
Buffer Sizing
- Small buffers (< 1MB): Lower memory usage, but increased risk of packet drops under load
- Large buffers (4-8MB): Better handling of traffic bursts, higher memory usage
- No buffer (0): Lowest memory, but writes block and can cause congestion
Related
- UDP Mux - UDP connection multiplexing
- Universal Mux - Combined UDP mux with STUN/TURN support
- Agent Options - Using muxes with ICE agents
- TCP Support - Understanding ICE over TCP