Skip to main content

Module peer

Module peer 

Source
Expand description

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.

PeerDriver gathers for the application and keeps the gathering right across what makes a transport lose its candidates. The engine has no sockets, so it tells the application with RTCPeerConnectionEvent::OnLocalCandidatesNeeded when a transport needs a new set of local candidates: a new DTLS association over UDP runs on a new 3-tuple (RFC 8842 section 5.1), so after an ICE restart for a new association (automatic SRTP re-keying included) or on the transport that replaced one an answer closed, the transport’s previous UDP addresses are refused. The driver reacts by itself: it binds new UDP ports (inside PeerDriverConfig::udp_port_range, skipping ports the host refuses), adds their host candidates for the event’s section, ends the generation, and closes the previous sockets only once a candidate pair on a new one is selected for that transport (and then after PeerDriverConfig::retired_socket_linger, for what the peer still sends there before its own selection switches), or the transport is closed: until then the previous path keeps carrying the transport’s packets. The engine’s ICE-TCP listener is kept (RFC 8842 asks a new 3-tuple of unordered transports only), and its server-reflexive and TURN gathering follow the new host candidates by themselves.

With SettingEngineBuilder::with_dtls_new_association_on_previous_addresses(true) the engine keeps the candidates and fires no event: the driver keeps its sockets and gives their candidates to each new ICE generation again.

§mDNS

A connection with mDNS on (SettingEngineBuilder::with_multicast_dns_mode) publishes its host addresses behind .local names and resolves the peer’s: its ICE agents write their mDNS datagrams from local port 5353. At the first one the driver binds the host’s mDNS sockets, port 5353 joined to 224.0.0.251 and, where the host has IPv6, to ff02::fb (shared with other mDNS responders of the host through address reuse), on the interface of every address of PeerDriverConfig::addresses; it sends the connection’s queries, probes and answers there and hands what arrives back to the connection. A connection without mDNS writes none, and the driver binds nothing.

RFC 6762 section 14: a host on several links takes part in mDNS on each, and an answer carries only addresses valid on the interface it leaves by. The engine names, for each message, the address it is about (its record’s); the driver sends it by that address’s interface (Transmit::source: IP_MULTICAST_IF / IPV6_MULTICAST_IF), an IPv6 record’s on the IPv6 group (and on the IPv4 group of the same interface where the driver has an IPv4 address there), and a query once on each interface.

Where mDNS cannot be sent (a socket that cannot be bound, or a system that refuses every send to the group: no multicast route, or a Mac application without the Local Network permission) the driver tells the application with PeerDriverEvent::MdnsUnavailable (PeerDriver::poll_driver_event), with the system’s error, counts it (PeerDriver::mdns_send_failures, PeerDriver::mdns_bind_failures), and tells the connection that the family’s names cannot be published. It tries again at the next gathering and, while mDNS datagrams wait, after a back-off; the first send that works is reported as PeerDriverEvent::MdnsAvailable. The connection itself goes on over its other candidates, or fails as ICE does.

With QueryAndGather the driver binds the mDNS sockets before it adds the first host candidates and tells the connection for which address families it has one (RTCPeerConnection::set_multicast_dns_publishing): a host address of a family without one cannot have its name published (draft-ietf-mmusic-mdns-ice-candidates section 3.1.1 step 4), and its candidates go out under a .local name that is not published, or not at all when the connection fails closed (SettingEngineBuilder::with_multicast_dns_fail_closed). PeerDriverConfig::mdns_sockets false binds none.

Every mDNS datagram takes one message from a budget shared by every driver of the process (process_mdns_rate_limit, unless PeerDriverConfig::mdns_rate_limit names another): draft-ietf-mmusic-mdns-ice-candidates section 7.1 requires “an implementation-wide rate limit” over the mDNS queries and responses of all connections. A datagram the budget refuses waits in the driver and leaves, in order, when the budget has room.

§The loop

let mut driver = PeerDriver::new(pc, PeerDriverConfig::on(vec![ip]));
// offer / answer through the application's signalling, on driver.pc_mut();
// after the first local description, the driver gathers by itself;
loop {
    driver.step(Duration::from_millis(20)).await?;
    while let Some(event) = driver.poll_event() {
        // signal OnIceCandidateEvent candidates to the peer, answer negotiation-needed, ...
    }
    while let Some(read) = driver.pc_mut().poll_read() { /* media, data channels */ }
}

Structs§

MdnsFailure
Why the driver’s mDNS of one address family stopped: what PeerDriverEvent::MdnsUnavailable tells the application.
PeerDriver
Drives an RTCPeerConnection over sockets of its own (see the module documentation).
PeerDriverConfig
What a PeerDriver binds.

Enums§

MdnsStage
What failed in a MdnsFailure.
PeerDriverEvent
Something the driver itself reports to the application (PeerDriver::poll_driver_event), beside the connection’s own events.

Constants§

MDNS_MULTICAST_IPV6
The mDNS IPv6 link-local group (RFC 6762 section 3).

Functions§

process_mdns_rate_limit
The 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 same budget.
set_process_mdns_rate_limit
Sets 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).