feat(webrtc): add UdpMux for shared-port WebRTC-Direct inbound dispatch#1381
Open
yashksaini-coder wants to merge 2 commits into
Open
feat(webrtc): add UdpMux for shared-port WebRTC-Direct inbound dispatch#1381yashksaini-coder wants to merge 2 commits into
yashksaini-coder wants to merge 2 commits into
Conversation
…ch (libp2p#1352) Implements `_udp_mux.UdpMux` — a shared UDP DatagramProtocol that owns a single OS socket and demultiplexes concurrent inbound WebRTC-Direct dials: - Pre-ICE STUN BINDING REQUESTs routed by `USERNAME` ufrag prefix - Post-ICE DTLS/SCTP frames routed by remote (host, port) - `_MuxedTransport` adapter gives each aioice.Connection a fake transport backed by the shared socket, bypassing `_gather_candidates` entirely - `add_ice_connection()` creates an `aioice.Connection` with preset `local_username`/`local_password` and injects a mux-backed StunProtocol into its `_protocols` list without binding a new UDP socket This is the foundational primitive for a spec-aligned WebRTC-Direct listener (libp2p/specs#715) that advertises one fixed UDP port in its multiaddr. Follow-up: wire UdpMux into listener.py for the full v2 listener flow.
- Remove Optional import, use X | None syntax (ruff pyupgrade) - Fix import order in try block: from before import, alphabetical - Add _udp_mux.py to pyrefly project_excludes (aioice not installed in lint tox env, same treatment as other webrtc-optional files) - Drop unused _Connection import and unused `conn` variable in tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes : #1352
Summary
libp2p/transport/webrtc/_udp_mux.py— aUdpMuxclass (a sharedasyncio.DatagramProtocol) that owns one OS UDP socket and demultiplexes concurrent inbound WebRTC-Direct dialsUSERNAMEufrag prefix (parsed from the STUN message)(host, port)(registered after ICE nomination)_MuxedTransportadapter gives eachaioice.Connectiona fake transport backed by the shared socket, bypassing_gather_candidatesentirely so aioice never binds its own portadd_ice_connection(ufrag, pwd, host=)creates anaioice.Connectionwith presetlocal_username/local_passwordand injects a mux-backedStunProtocolintoconnection._protocolsWhy
Closes the ICE mux gap identified in the spike (#1352). A spec-aligned WebRTC-Direct listener (libp2p/specs#715) must advertise a single fixed UDP port and demux concurrent dials on that port — impossible with the current aioice architecture where each
Connectionbinds its own ephemeral socket.UdpMuxsolves this without modifying aioice.Architecture note
StunProtocolis an internal aioice class. We access it directly because we're injecting into aioice's_protocolslist — this is intentional monkey-patching. The two# type: ignoreannotations mark aioice'sconnection_lost(exc: Exception)annotation gap (should beOptional[Exception]per asyncio docs).What's next
Follow-up PR: wire
UdpMuxintolistener.pyto replace the current HTTP-signaling harness with a spec-aligned STUN-dispatch listener (WebRTC-Direct v2, libp2p/specs#715).Test plan
uv run pytest tests/core/transport/webrtc/test_udp_mux.py -v→ 11/11 pass (skipped when aioice not installed)uv run make lint→ all pre-commit hooks pass (mypy + pyrefly clean)