ICE, STUN, TURN and mDNS
You bind the sockets and add the host candidates; the engine runs ICE (RFC 8445), gathers server-reflexive and relayed candidates from them, runs ICE-TCP and TURN over TCP and TLS through its stream interface, and resolves mDNS names.
Host candidates are yours
Because the engine does no I/O, it does no host candidate gathering: you bind the sockets, so you tell the connection which local candidates exist. Build them with the rtci-ice candidate constructors and hand each one to add_local_candidate. A host candidate's address is a concrete local address, and it is the local_addr of every datagram you then feed for it (The local address of a datagram).
use rtci::peer_connection::RTCPeerConnectionBuilder;
use rtci::peer_connection::transport::{
CandidateConfig, CandidateHostConfig, RTCIceCandidate, RTCIceCandidateInit,
};
use std::time::Instant;
fn gather(pc: &mut rtci::peer_connection::RTCPeerConnection) -> Result<(), Box<dyn std::error::Error>> {
let candidate = CandidateHostConfig {
base_config: CandidateConfig {
network: "udp".to_owned(),
address: "192.168.1.100".to_string(),
port: 8080,
component: 1,
..Default::default()
},
..Default::default()
}
.new_candidate_host()?;
pc.add_local_candidate(RTCIceCandidate::from(&candidate).to_json()?)?;
// An EMPTY candidate string is the end-of-gathering sentinel: it moves the ICE gathering
// state to `Complete` and emits `OnIceGatheringStateChangeEvent(Complete)`. Nothing else
// reaches that state, so send it once you have added every local candidate.
pc.add_local_candidate(RTCIceCandidateInit::default())?;
Ok(())
}
From README.md, “Feeding ICE Candidates” at v0.5.0.
Remote candidates arrive over your signalling channel and go in through add_remote_candidate, which requires a remote description first. A non-empty candidate needs sdp_mid or sdp_mline_index (W3C addIceCandidate). For srflx and relay candidates, set RTCIceCandidateInit::url to the STUN/TURN server the candidate was gathered from so it is attributed correctly in getStats.
Server-reflexive and relayed candidates: the engine's by default
ReflexiveGathering::Engine and RelayGathering::Engine are the defaults: with stun: / turn: servers configured, the engine sends the Binding and Allocate requests through poll_write from the UDP host candidates you add and takes the answers out of handle_read. Code that runs its own STUN / TURN client sets SettingEngineBuilder::with_server_reflexive_gathering(ReflexiveGathering::Application) and with_relay_gathering(RelayGathering::Application), or the engine gathers too.
ICE servers go in the configuration:
RTCConfigurationBuilder::new()
.with_ice_servers(vec![RTCIceServer {
urls: vec!["stun:stun.l.google.com:19302".to_string()],
..Default::default()
}])
.build()
From README.md, "Event Loop Example" at v0.5.0.
TURN
The engine's TURN client (from rtci-turn: RFC 8656, RFC 6062 TCP allocations; there is no TURN server) gathers relayed candidates over UDP, TCP, TLS and DTLS:
- a plain
turn:server over UDP is allocated on from each transport's UDP host candidate's base, throughpoll_write/handle_read; turns:...?transport=udpis reached over DTLS (RFC 7350) the same way, without the stream interface;turn:...?transport=tcpandturns:servers go through the stream interface (below): names resolved by the driver, Happy Eyeballs (RFC 8305) across the addresses, TLS with rustls sans-I/O (ALPNstun.turn). Roots or a verifier come from the application (with_turn_tls), or the Mozilla roots with featureturn-tls-webpki-roots. For each TCP / TLS server the engine also makes a TCP allocation (RFC 6062);with_turn_tcp_relay(false)turns this off.
Expiring credentials. TURN credentials that expire (a TURN REST API username) are rotated on a live connection with rotate_ice_servers(now, servers) and one offer / answer round: the peer sees an ICE restart (same connection, new ice-ufrag, same DTLS fingerprint). For no gap, bind a second UDP socket and add its host candidate after create_offer and before set_local_description of the restart offer.
Re-allocating on the same socket. A TURN allocation is its 5-tuple's, and an Allocate on a 5-tuple that still has one is refused with 437. Within one peer connection the engine allocates again on a released 5-tuple only once the server has answered the release; SettingEngineBuilder::with_turn_reallocation_delay makes it wait longer after that answer (2 s covers coturn, which keeps a released allocation one more second; the default, zero, is RFC 8656's). The README's "Sans-I/O Event Loop Pattern" section has the full rules, for engine-run and application-run TURN.
ICE over TCP
Once the application serves the engine's stream interface (The stream half), the engine gathers a passive, a simultaneous-open and an active TCP candidate for each UDP host address it is given (RFC 6544), frames with RFC 4571 and closes the connections ICE no longer needs. Inside one peer connection it routes between its transports by the ufrag the first Binding request names. SettingEngineBuilder::with_tcp_candidate_gathering(false) keeps the engine's TCP off.
mDNS
Browsers often send their host candidates under <uuid>.local names (draft-ietf-mmusic-mdns-ice-candidates). By default (MulticastDnsMode::QueryOnly) the connection resolves such a name with an mDNS query, which leaves through poll_write to 224.0.0.251:5353. A name that does not resolve, the usual case when the peer is not on your link, is ignored when its query ends (10 s by default, SettingEngineBuilder::with_multicast_dns_timeout) and logged at debug level only; the peer's checks can still reach you as a peer-reflexive candidate. To send no mDNS at all and ignore remote .local candidates, set SettingEngineBuilder::with_multicast_dns_mode(MulticastDnsMode::Disabled).
Server sockets: the reference driver
The reference driver (rtci-stream-driver) has the socket choices a server makes:
| Need | Where |
|---|---|
| Ports from a range (firewall rules) | DriverConfig::tcp_port_range, sockets::bind_udp_in_range / bind_tcp_listener_in_range with PortRange::new(min, max) |
| One UDP port for many peer connections | sockets::UfragMux: register every pc.ice_username_fragments() entry after each local description; route(from, datagram) picks the connection |
| One passive ICE-TCP port for many peer connections | DriverConfig::shared_passive, sockets::accept_by_ufrag, then StreamDriver::adopt_shared on the connection the first check names |
| Which local addresses get candidates | sockets::AddressFilter (bind_addresses, include_loopback, include_link_local, interface_filter, ip_filter) and local_addresses() |
| mDNS on loopback on Windows (off by default) | PeerDriverConfig::mdns_on_loopback_on_windows |
| SRV / NAPTR lookups and per-family answers | feature dns-records (hickory-resolver) |
| A server behind a 1:1 NAT | SettingEngineBuilder::with_nat_1to1_ips(vec![external_ip], RTCIceCandidateType::Host) (or Srflx); it applies to the engine's TCP candidates too |
From README.md, "Driving the Engine" at v0.5.0.
It also has nat_mapping (opt-in, no dependency): a PCP (RFC 6887) MAP request with a NAT-PMP (RFC 6886) fallback to the gateway the application names, added as a server-reflexive candidate.
ICE restarts
See Peer connection lifecycle. The restarted generation keeps its local candidates by default; SettingEngineBuilder::with_discard_local_candidates_during_ice_restart(true) drops them instead.
Where the ICE defaults differ from Chrome
Nomination, failure on an exhausted check list, consent checks and the other ICE behaviours where the RFC and libwebrtc differ are rows of the behaviour profiles table (the ice.* rows), each with its setting and its value per profile.