rtciv0.5.0

rtci-stream-driver

A socket driver for a sans-I/O WebRTC engine (ICE-TCP, TURN over TCP and TLS, whole peer connections) on an async runtime of your choice. 6 public modules, 82 items at 0.5.0.

README

The reference driver for the stream half of the rtci engine's sans-I/O interface: it owns the TCP sockets, on the async runtime it is given, and carries out what the engine asks (listen, connect, close, reset, resolve a name or SRV / NAPTR records), and reports back what happened. The engine decides everything else: which ICE-TCP candidates exist (RFC 6544), RFC 4571 framing, TURN over TCP and TLS, the Happy Eyeballs race (RFC 8305).

It also has what a server decides and the engine does not (sockets): port ranges, which local addresses get candidates, one UDP port and one passive ICE-TCP port shared by many peer connections; and an opt-in PCP / NAT-PMP client for NAT-assisted candidates (nat_mapping, RFC 6887, RFC 6886).

Use

while let Some(command) = pc.poll_stream_command() { driver.execute(command) }
while let Some(transmit) = pc.poll_stream_transmit() { driver.write(transmit) }
match driver.next().await {
    DriverInput::Event(event) => pc.handle_stream_event(now, event)?,
    DriverInput::Read { id, data } => pc.handle_stream_read(now, id, &data)?,
    DriverInput::Accepted(accepted) => { /* pc.handle_stream_accepted, then driver.adopt */ }
}

The crate documentation has the command table and the complete loop; the root crate's stream-driver-loopback example runs two peer connections over loopback TCP through it. Feature dns-records adds SRV / NAPTR lookups and per-family answers to TokioRuntime through hickory-resolver (0.26.2 or later, which has no open RustSec advisory; the lookups are those of a stub resolver asking the system's name servers, without DNSSEC validation); without it, records are answered as none and names through the system resolver.

A whole peer connection

peer::PeerDriver (feature peer, on by default, which depends on rtci) drives an rtci peer connection over sockets it owns, its UDP host sockets included: it gathers when a transport begins gathering, and again by itself when the engine says a transport needs new local candidates (OnLocalCandidatesNeeded: a new DTLS association runs on a new 3-tuple, RFC 8842 section 5.1), binding new ports in its range and closing the previous sockets once a pair on the new ones is selected. The application only signals.

A connection with mDNS on gets its mDNS carried too: at its first mDNS datagram the driver binds the host's mDNS sockets (port 5353, the IPv4 and, where available, IPv6 groups), and every driver of the process sends its mDNS within one message budget (peer::process_mdns_rate_limit), the implementation-wide rate limit of draft-ietf-mmusic-mdns-ice-candidates section 7.1.

I/O, and bringing your own runtime

Unlike the other crates of the workspace this one does I/O. The engine itself still never touches a socket or a clock; the driver does, through the async runtime it is given.

Everything that touches a reactor or a clock goes through one object-safe trait, runtime::Runtime, given to each driver as an Arc<dyn Runtime>:

Injected (Runtime)Not injected
spawn (a JoinHandle trait object; dropping it detaches)channels and wake-ups between the driver's tasks (async-channel, executor-agnostic)
now, sleep_until: the clock every time given to the engine comes fromruntime::sleep, timeout, timeout_at, yield_now: free functions on top
bind_udp, bind_udp_multicast (AsyncUdpSocket: poll form, batched receive, per-datagram traffic class)
listen_tcp, connect_tcp (AsyncTcpListener, AsyncTcpStream)
resolve_host, and the optional resolve_family / resolve_records
block_on (a future that need not be Send)

There is no global or default runtime, and none can be set: StreamDriver::with_runtime and PeerDriver::with_runtime take one. With the default feature runtime-tokio, StreamDriver::new, StreamDriver::default and PeerDriver::new use TokioRuntime::new() (the tokio runtime of the calling code, as tokio::spawn does). The one process-wide state the crate keeps is the mDNS budget above: the mDNS draft requires an implementation-wide rate limit, and it is a budget, not a runtime.

FeatureGives
runtime-tokio (default)TokioRuntime, and StreamDriver::new / default, PeerDriver::new
runtime-smolSmolRuntime
runtime-mockMockRuntime: virtual time and an in-memory network
peer (default)peer::PeerDriver (depends on rtci)
dns-recordshickory lookups in TokioRuntime (turns runtime-tokio on)

Features only decide which runtime types exist; several can be on at once, and with default-features = false and none of them the crate builds without any async runtime in its dependency tree (CI checks it). Your own runtime implements Runtime; the workspace's examples/custom-runtime does it over async-executor and async-io in about 300 lines, with the helpers runtime::{std_udp, std_multicast, std_tcp_listener, std_tcp_connect_start, TrafficClassMarker, ...} for runtimes whose sockets are real ones.

MockRuntime runs drivers on virtual time: its clock moves when told (MockClock::advance) or, inside MockRuntime::run, to the next timer whenever nothing can run; its network (MockNet) carries UDP, multicast and TCP between mock sockets with per-link delay, seeded loss and blocking, a datagram filter and a log. Two PeerDrivers connect on it with no socket, and a 30-second ICE consent expiry takes no wall time (tests/virtual_time.rs).

The design follows the upstream project's "Bring Your Own Async Runtime" (webrtc.rs, 2026-07-30, <https://webrtc.rs/blog/2026/07/30/pluggable-async-runtime.html>); where it differs (the clock is injected, sockets are bound by address, the socket types are the crate's own), the crate documentation of runtime says why.

Licence

Licensed under either of the MIT licence or the Apache License, Version 2.0, at your option.

This crate is part of the rtci workspace, which descends from webrtc-rs/rtc and is developed independently; FORK.md lists what it carries over upstream. Upstream's licence and copyright notices are retained.

rtci_stream_driver

The reference driver for the stream half of WebRTC.rs's sans-I/O engine (rtci_shared::stream): it owns the TCP sockets, on the async runtime it is given (runtime), and does what the engine asks.

  • modnat_mappingNAT-assisted candidates (RFC 6544 section 5.3, RFC 8445 section 5.1.1.2): an opt-in client of the Port Control Protocol (PCP, RFC 6887) with a fallback to NAT-PMP (RFC 6886), asking the gateway for a port mapping. Nothing here runs unless the application calls it, and it adds no dependency: one UDP socket of the driver's Runtime.
  • modpeercfg(feature = "peer")A whole peer connection driven over sockets the driver owns (feature peer, on by default): its UDP sockets for the ICE host candidates, and the TCP sockets of the engine's stream interface through a StreamDriver.
  • modruntimeThe async runtime a driver runs on, injected per driver.
  • modsocketsServer-side socket choices, which belong to the driver and not to the engine: port ranges, which local addresses get candidates, and one UDP port or one ICE-TCP listening port shared by many peer connections.
  • structAcceptedA connection accepted on a listener, waiting for its id.
  • structDriverConfigThe driver's settings.
  • structStreamDriverOwns the sockets of one engine (see the crate documentation).
  • enumDriverInputWhat the driver has for the engine.

rtci_stream_driver::nat_mapping

NAT-assisted candidates (RFC 6544 section 5.3, RFC 8445 section 5.1.1.2): an opt-in client of the Port Control Protocol (PCP, RFC 6887) with a fallback to NAT-PMP (RFC 6886), asking the gateway for a port mapping. Nothing here runs unless the application calls it, and it adds no dependency: one UDP socket of the driver's Runtime.

  • structMappingA mapping the gateway granted.
  • structMappingConfigHow map_port asks.
  • enumMappingProtocolWhich protocol answered.
  • enumNatPmpAnswerA NAT-PMP answer.
  • enumPcpAnswerWhat a PCP response says.
  • enumProtocolThe transport a mapping is for.
  • fncandidate_lineThe NAT-assisted candidate of mapping for the local base (RFC 6544 sections 4.2 and 5.3): a server-reflexive candidate with the type preference 105, for the application to add with add_local_candidate. Over TCP tcp_type names its direction (RFC 6544 section 4.2: for NAT-assisted candidates simultaneous-open 6, active 4, passive 2; an active one is signalled on port 9).
  • fnmap_portAsks the gateway at gateway for a mapping of internal_port on local_ip (the interface the gateway is reached from): PCP first, NAT-PMP when the gateway does not speak PCP. Ok(None) when no gateway answered; an error when it refused.
  • fnnat_pmp_address_requestA NAT-PMP external address request (RFC 6886 section 3.2).
  • fnnat_pmp_map_requestA NAT-PMP mapping request (RFC 6886 section 3.3).
  • fnparse_nat_pmp_responseParses a NAT-PMP response to opcode (RFC 6886 sections 3.2, 3.3, 3.5).
  • fnparse_pcp_map_responseParses a PCP MAP response to the request with nonce (RFC 6887 sections 7.2 and 11.1). None for anything else (not PCP, not a MAP response, another nonce).
  • fnpcp_map_requestA PCP MAP request (RFC 6887 sections 7.1 and 11.1): 24-byte header, 36-byte MAP opcode.
  • constSERVER_PORTThe port PCP and NAT-PMP servers listen on (RFC 6887 section 19.1, RFC 6886 section 3).

rtci_stream_driver::peercfg(feature = "peer")

A whole peer connection driven over sockets the driver owns (feature peer, on by default): its UDP sockets for the ICE host candidates, and the TCP sockets of the engine's stream interface through a StreamDriver.

  • structMdnsFailureWhy the driver's mDNS of one address family stopped: what PeerDriverEvent::MdnsUnavailable tells the application.
  • structPeerDriverDrives an RTCPeerConnection over sockets of its own (see the module documentation).
  • structPeerDriverConfigWhat a PeerDriver binds.
  • enumMdnsStageWhat failed in a MdnsFailure.
  • enumPeerDriverEventSomething the driver itself reports to the application (PeerDriver::poll_driver_event), beside the connection's own events.
  • fnprocess_mdns_rate_limitfrom crate::mdns_io::process_mdns_rate_limitThe message budget every PeerDriver of this process takes its mDNS from, unless its configuration names another (PeerDriverConfig::mdns_rate_limit): 40 messages at once, then 10 a second (MdnsRateLimit::default). One budget for the process is draft-ietf-mmusic-mdns-ice-candidates section 7.1's "implementation-wide rate limit". The returned value is a handle: every clone takes from the …
  • fnset_process_mdns_rate_limitfrom crate::mdns_io::set_process_mdns_rate_limitSets the process's mDNS budget (process_mdns_rate_limit) instead of the default, once, before its first use (the first driver that sends mDNS uses it).
  • constMDNS_MULTICAST_IPV6from crate::mdns_io::MDNS_MULTICAST_IPV6The mDNS IPv6 link-local group (RFC 6762 section 3).

rtci_stream_driver::runtime

The async runtime a driver runs on, injected per driver.

  • modmockcfg(feature = "runtime-mock")MockRuntime (feature runtime-mock): virtual time and an in-memory network, so that driven connections run deterministically and a 30-second timeout takes no wall time.
  • structElapsedtimeout ran out.
  • structMulticastConfigWhat a multicast socket is bound and joined to.
  • structMulticastInterfaceSelectorApplies a Transmit::source to a real multicast socket bound to the unspecified address: before a datagram to a multicast group, it sets the interface the datagram leaves by (IP_MULTICAST_IF to the source address, IPV6_MULTICAST_IF to the index of the interface that holds it) when it differs from the last one set. The system then sends from that interface's address: on Linux exactly the …
  • structRecvMetaWhat AsyncUdpSocket::poll_recv says about one buffer it filled.
  • structSmolRuntimecfg(feature = "runtime-smol")from smol_runtime::SmolRuntimeThe smol runtime: tasks on smol's global executor, timers and sockets on async-io's reactor, name lookups on smol's thread pool. It needs no setup and can be used from any thread, inside or outside smol::block_on.
  • structTokioRuntimecfg(feature = "runtime-tokio")from tokio_runtime::TokioRuntimeThe tokio runtime.
  • structTrafficClassMarkerApplies a Transmit::traffic_class to a real socket: sets IP_TOS / IPV6_TCLASS when the value differs from the last one set, and stops asking once the platform refused (its datagrams then leave unmarked, AsyncUdpSocket::unmarked). For built-in and third-party runtimes whose sockets have a file descriptor.
  • structTransmitOne datagram (or, with segment_size, several of one size back to back) to send.
  • traitAsyncTcpListenerA TCP listener of a runtime.
  • traitAsyncTcpStreamA TCP connection of a runtime. &self methods: the driver's reader and writer tasks share one Arc.
  • traitAsyncUdpSocketA UDP socket of a runtime, in the poll form of quinn-udp's so that batched send and receive (sendmmsg, recvmmsg, GSO, GRO) fit.
  • traitJoinHandleA task spawned with Runtime::spawn. Dropping it detaches the task.
  • traitRuntimeWhat a driver needs from an async runtime: the primitives that touch a reactor or a clock.
  • fninterface_indexThe index of the interface that holds the IPv6 address ip (what IPV6_MULTICAST_IF and an IPv6 group membership name an interface by), None when no interface of the host holds it.
  • fninterface_nameThe name of the interface that holds ip, None when no interface of the host holds it.
  • fnreset_on_closeMakes socket's close abortive (SO_LINGER 0: RST instead of FIN), for AsyncTcpStream::reset_on_close.
  • fnset_and_read_traffic_classSets socket's TOS / Traffic Class and reads it back (for AsyncTcpStream::set_traffic_class).
  • fnsleepWaits duration on runtime's clock.
  • fnstd_multicastA non-blocking std UDP socket joined as config says: for IPv4 what rtci-mdns' MulticastSocket builds (bound to the group on Linux, to 0.0.0.0 elsewhere), for IPv6 [::]:port, IPv6 only; a membership of the group on every interface of MulticastConfig::interfaces (the default interface when there is none).
  • fnstd_tcp_boundA non-blocking TCP socket bound to local, for a runtime to connect to a peer.
  • fnstd_tcp_connect_startStarts a non-blocking TCP connection from local to remote: the socket is bound, and the handshake runs. A runtime waits for it to become writable, then reads take_error and peer_addr (what async-io's own connect does), for runtimes whose connect cannot bind a local address first.
  • fnstd_tcp_listenerA non-blocking std TCP listener on local, backlog 128.
  • fnstd_udpA non-blocking std UDP socket on local: what the built-in runtimes wrap.
  • fntimeoutfuture's output, or Elapsed if duration of runtime's clock passes first.
  • fntimeout_atfuture's output, or Elapsed if runtime's clock reaches deadline first.
  • fnyield_nowGives other tasks a turn: pending once, with the task woken at once.
  • typeBoxFutureA boxed future that can move between threads.
  • useMockClockcfg(feature = "runtime-mock")from mock::MockClockThe virtual clock of a MockRuntime.
  • useMockNetcfg(feature = "runtime-mock")from mock::MockNetThe in-memory network of a MockRuntime.
  • useMockRuntimecfg(feature = "runtime-mock")from mock::MockRuntimeA runtime on virtual time with an in-memory network (see the module documentation).

rtci_stream_driver::runtime::mockcfg(feature = "runtime-mock")

MockRuntime (feature runtime-mock): virtual time and an in-memory network, so that driven connections run deterministically and a 30-second timeout takes no wall time.

  • structDatagramRecordOne datagram of the MockNet::log.
  • structLinkConfigOne direction between two IP addresses of the MockNet.
  • structMockClockThe virtual clock of a MockRuntime.
  • structMockDatagramA datagram as the MockNet sees it, for a filter.
  • structMockNetThe in-memory network of a MockRuntime.
  • structMockRuntimeA runtime on virtual time with an in-memory network (see the module documentation).
  • enumFateWhat became of a datagram.
  • typeDatagramFilterA filter of the MockNet: false drops the datagram.

rtci_stream_driver::sockets

Server-side socket choices, which belong to the driver and not to the engine: port ranges, which local addresses get candidates, and one UDP port or one ICE-TCP listening port shared by many peer connections.

  • structAddressFilterWhich local addresses get host candidates.
  • structPortRangeAn inclusive range of ports for the sockets a peer connection gets.
  • structSharedAcceptA connection accepted on an ICE-TCP listening port shared by many peer connections, with the ufrag its first check names.
  • structUfragMuxRoutes what arrives on a shared port to the peer connection it is for: by the receiver's ufrag of the first Binding request from an address, then by that address.
  • fnaccept_by_ufragAccepts the next connection on a shared ICE-TCP port and reads its first RFC 4571 frame (for timeout at most) to learn which peer connection it is for. Connections whose first frame is not a Binding request with a USERNAME are dropped and the next one is awaited.
  • fnbind_runtime_udp_in_rangeA UDP socket of runtime on ip, on a free port of range, skipping refused ports as bind_udp_in_range does.
  • fnbind_tcp_listener_in_rangeA TCP listener of runtime on ip, on a free port of range.
  • fnbind_udp_in_rangeA UDP socket on ip, on a free port of range. Ports the host refuses (an excluded port range on Windows, a privileged port) are skipped like ports in use.
  • fnbinding_request_ufragsThe ufrags of a STUN Binding request: (receiver, sender) from its USERNAME (RFC 8445 section 7.2.2). None for anything else.
  • fnset_traffic_classSets the byte the socket's packets carry in the IPv4 TOS / IPv6 Traffic Class field: the DSCP in its upper six bits (RFC 2474 section 3), the ECN codepoint in the lower two (RFC 3168 section 5). ipv6 says which option (IPV6_TCLASS or IP_TOS) the socket's family takes.
  • fntraffic_classThe byte the socket's packets carry in the IPv4 TOS / IPv6 Traffic Class field, as the operating system reports it (getsockopt of IP_TOS / IPV6_TCLASS).
  • typeInterfaceFilterKeeps an interface by name.
  • typeIpFilterKeeps an address.

Generated from the sources of rtci-stream-driver 0.5.0 at its release tag. Items made by macros and methods are on docs.rs.