Skip to main content

Agent

Struct Agent 

Source
pub struct Agent { /* private fields */ }
Expand description

Represents the ICE agent.

Implementations§

Source§

impl Agent

Source

pub fn checklist_state(&self) -> ChecklistState

The state of the checklist, in RFC 8445’s terms: Running until a pair is nominated, Completed from then on, Failed when the agent is.

Agent::state is the W3C transport state, which is Connected once a pair is nominated and Completed only when, in addition, both sides have said they have no more candidates.

Source

pub fn handle_transport_error( &mut self, now: Instant, transport: TransportContext, error: TransportError, ) -> bool

What the transport learned about the path transport (its local address the base of a local candidate, its peer address a remote candidate):

  • TransportError::IcmpHardError, RFC 8445 Section 7.2.5.2.2: “if a Binding request generates a hard ICMP error, the agent SHOULD set the state of the candidate pair to Failed”. Only a pair whose check is in flight fails, and never a valid one: an ICMP error is not authenticated (“ICMP errors can be used as a method for Denial-of-Service attacks”), so it cannot take away a pair a check has validated. Consent decides for those.
  • TransportError::ConnectionFailed, RFC 6544 Section 7.1: “If the TCP connection cannot be established, the check is considered to have failed, and a full-mode agent MUST update the pair state to Failed”. The pair fails whether or not its check has started; a valid pair is left to consent, as above.
  • TransportError::NotStun, RFC 6544 Section 7.1: “If the response to the first connectivity check on the established TCP connection is something other than a STUN message, the remote candidate address apparently was not one of the peer’s addresses, and the agent SHOULD close the connection and consider all pairs with that remote candidate as failed.” Every pair with that remote candidate that is not valid fails; closing the connection is the caller’s.

Returns whether a pair failed.

Source

pub fn handle_non_stun_before_validation( &mut self, now: Instant, transport: TransportContext, ) -> bool

Something other than STUN arrived over TCP on transport, on no valid pair: what a driver that demultiplexes the connection itself (the peer connection does) calls instead of deciding on TransportError::NotStun on its own.

RFC 6544 Section 7.1 concludes from “the response to the first connectivity check on the established TCP connection”. Only on a connection this agent opened (its local candidate is active) are the peer’s first bytes that response: the agent sends its check as soon as the connection is up, and the peer has nothing to send before it. On a connection the peer opened (a passive local candidate) the peer may send DTLS before it answers this agent’s triggered check, so nothing is concluded there. Returns whether a pair failed; closing the connection is the caller’s.

Source§

impl Agent

Source

pub fn get_candidate_pairs_stats(&self, now: Instant) -> Vec<CandidatePairStats>

Returns a list of candidate pair stats.

Source

pub fn get_local_candidates_stats(&self, now: Instant) -> Vec<CandidateStats>

Returns a list of local candidates stats.

Source

pub fn get_remote_candidates_stats(&self, now: Instant) -> Vec<CandidateStats>

Returns a list of remote candidates stats.

Source§

impl Agent

Source

pub fn apply_restart_revertible( &mut self, now: Instant, keep_local_candidates: bool, ) -> Result<()>

Agent::apply_restart, remembering the session it replaces so that Agent::revert_restart can bring it back: the restart belongs to a local offer that may still be rolled back (RFC 9429 section 5.7: “any resources allocated by the abandoned local description are discarded”). A second restart before the first is committed keeps the session the first one replaced.

Source

pub fn has_revertible_restart(&self) -> bool

Whether a restart applied by Agent::apply_restart_revertible can still be taken back.

Source

pub fn commit_restart(&mut self)

Keeps the restart Agent::apply_restart_revertible applied: the answer to its offer arrived. A no-op without one.

Source

pub fn revert_restart(&mut self, now: Instant) -> bool

Takes back the restart Agent::apply_restart_revertible applied: the local offer was rolled back, so the agent goes on with the session it had (RFC 9429 section 5.7), its credentials, candidates, pairs and selected pair, and what the new generation gathered or learned is dropped. Consent on the selected pair carries over what the previous-generation checks of the meantime established (RFC 7675). Returns whether there was a restart to take back; a closed agent stays closed.

Source§

impl Agent

Source

pub fn new( now: Instant, config: Arc<AgentConfig>, crypto_provider: Arc<dyn RTCCryptoProvider>, ) -> Result<Self>

Creates a new Agent.

The crypto provider is supplied by the caller; this crate never resolves a default. Builds an agent whose clock starts at now.

now is a constructor argument rather than an AgentConfig field because it is a value consumed once at construction, not a setting that persists for the agent’s lifetime — and because Instant has no Default, so a config field would force AgentConfig to drop #[derive(Default)] and break every ..Default::default() in its 31 call sites.

Source

pub fn add_local_candidate(&mut self, c: Candidate) -> Result<bool>

Adds a new local candidate that the caller conveys to the peer with this call (trickles it, or writes it into the description it sends): the agent pairs it at once.

RFC 8838 Section 10: “A Trickle ICE agent MUST NOT pair a local candidate until it has been trickled to the remote party.” A caller that gathers a candidate before it can convey it adds it with Agent::add_unconveyed_local_candidate and tells the agent with Agent::local_candidate_conveyed when it has.

With AgentConfig::hold_local_candidates_until_conveyed this is Agent::add_unconveyed_local_candidate.

Source

pub fn add_unconveyed_local_candidate(&mut self, c: Candidate) -> Result<bool>

Adds a new local candidate that is not conveyed to the peer yet: the agent holds it, answers checks that reach it, and pairs it once Agent::local_candidate_conveyed (or Agent::all_local_candidates_conveyed) says it has been trickled (RFC 8838 Section 10). Otherwise as Agent::add_local_candidate; a server-reflexive candidate the agent adds next to it for a 1:1 NAT mapping is held too.

Source

pub fn local_candidate_conveyed(&mut self, id: &str) -> bool

The local candidate id has been conveyed to the peer: it is paired now with the remote candidates known (RFC 8838 Section 10, steps 2 and 3). Returns whether the agent held such a candidate unconveyed.

Source

pub fn all_local_candidates_conveyed(&mut self)

Every local candidate the agent holds has been conveyed to the peer (a description listing them was applied, say): each one held unconveyed is paired now.

Source

pub fn stop_for_ice_mismatch(&mut self, now: Instant) -> bool

RFC 8839 Sections 4.2.5, 4.3.3 and 5.3: the answer carries a=ice-mismatch for the data stream this agent serves (the offer arrived at the peer with a default destination that no candidate matched), so “ICE procedures MUST NOT be used for this data stream”. The agent stops: no more checks or consent requests, no candidates, state Failed. The RFC 3264 fallback the RFC then calls for (media to the m= / c= destination without checks) is not taken: a WebRTC endpoint sends nothing without consent (RFC 7675 Section 5.1), so the stream ends with the agent. Returns whether the agent was running.

Source

pub fn is_local_candidate_unconveyed(&self, id: &str) -> bool

Whether the local candidate id is held unconveyed (and so unpaired).

Source

pub fn add_local_candidates( &mut self, candidates: Vec<Candidate>, ) -> Result<Vec<bool>>

Adds local candidates gathered together: those without a priority or a rank of their own are ranked first as RFC 8421 Section 4 recommends for a dual-stack host (an IPv6 head start, then the families alternating; see rank_local_candidates), around the ranks of the candidates the agent holds, and then each is added as by Agent::add_local_candidate, whose results are returned in order.

Source

pub fn add_remote_candidate(&mut self, c: Candidate) -> Result<bool>

Adds a new remote candidate.

Source

pub fn pending_mdns_candidates(&self) -> impl Iterator<Item = &Candidate>

The remote mDNS candidates whose names have not resolved yet. They are held by no pair, but they were submitted: statistics list them (mDNS draft Section 3.3.1).

Source

pub fn set_remote_end_of_candidates(&mut self)

The peer has no more candidates for this generation (an end-of-candidates indication, RFC 8838 Section 14). Remote candidates signalled after it are ignored, and a checking agent whose pairs have all failed fails without waiting for its timeout, once local gathering is complete too. An ICE restart starts a new generation.

Source

pub fn set_local_gathering_complete(&mut self)

Local gathering is complete for this generation (RFC 8838 Section 13): no local candidate is accepted after it. An ICE restart starts a new generation.

Source

pub fn is_signalled_remote_ip(&self, ip: IpAddr) -> bool

Whether this IP address is one the peer’s signalling led to: the address of a candidate it signalled, or the address a .local name it signalled has been resolved to. A peer-reflexive remote candidate’s address may be shown in statistics only then (draft-ietf-mmusic-mdns-ice-candidates Section 3.3.1: “a peer-reflexive remote candidate could be constructed from a remote host IP address as a result of an ICE connectivity check”; before the peer’s name resolves, such a candidate would reveal what the name hides). Once the agent has resolved the name itself, the address is no news; the mDNS candidate’s own entry still shows the name, never the address.

Source

pub fn get_local_candidates_to_signal(&self) -> Vec<Candidate>

The local candidates to signal to the peer: the ones the application added (with their 1:1 NAT companions), not the peer-reflexive ones the agent learned from the mapped address of a response (RFC 8445 Section 7.2.5.3.1), which need no signalling.

Source

pub fn set_remote_credentials( &mut self, remote_ufrag: String, remote_pwd: String, ) -> Result<()>

Sets the credentials of the remote agent.

Source

pub fn get_remote_credentials(&self) -> Option<&Credentials>

Returns the remote credentials.

Source

pub fn get_local_credentials(&self) -> &Credentials

Returns the local credentials. The credentials to advertise, which is what SDP generation wants.

Returns credentials staged by Agent::generate_restart_credentials when an ICE restart is pending, so an offer carries the new ufrag/pwd. Inbound STUN validation deliberately does not go through here — it reads local_credentials directly, so the live session keeps working until Agent::apply_restart installs the new pair.

Source

pub fn get_candidate_pairs(&self) -> Vec<(Candidate, Candidate)>

The candidate pairs on the agent’s list (webrtc-extensions 7 [[CandidatePairs]]): (local, remote), in the order they were formed. A pair the application removed is not listed.

Source

pub fn proposal_pending(&self) -> bool

Whether a nomination proposal waits for Self::resolve_nomination_proposal (webrtc-extensions 7 [[ProposalPending]]).

Source

pub fn resolve_nomination_proposal(&mut self, now: Instant, accept: bool)

Answers the pending nomination proposal: accept nominates the pair (a check with USE-CANDIDATE); otherwise the pair is not nominated and the agent keeps checking (webrtc-extensions 7, a cancelled icecandidatepairnominate). Nothing when no proposal is pending.

Source

pub fn select_candidate_pair( &mut self, now: Instant, local: &Candidate, remote: &Candidate, ) -> Result<()>

webrtc-extensions 7 selectCandidatePair(pair): the agent sends data on the pair of local and remote from now on; the selected pair changes, with its SelectedCandidatePairChange event, and the state with its event when it was not connected. Connectivity checks on other pairs stop, as they do once a pair is selected; consent keeps being checked on this one.

§Errors

InvalidStateError when the agent is closed, or new or failed; NotFoundError when the pair is not on the list (Self::get_candidate_pairs).

Source

pub fn remove_candidate_pair( &mut self, now: Instant, local: &Candidate, remote: &Candidate, ) -> Result<()>

webrtc-extensions 7 removeCandidatePair(pair): the agent stops the checks on the pair of local and remote (an In-Progress transaction is cancelled, no check is sent or answered on it any more, data from it is not accepted through it), then queues CandidatePairRemoved. When it was the selected pair the best other valid pair takes its place, or none: the state then follows consent as for a lost path.

§Errors

As Self::select_candidate_pair.

Source

pub fn role(&self) -> bool

Whether this agent is the controlling one, which decides nomination.

Source

pub fn set_role(&mut self, is_controlling: bool)

Sets the role, when the roles are determined (RFC 8445 Section 6.1.1): the agent that started the ICE processing is controlling, unless exactly one of the two is lite (the full one is controlling then).

“Once the roles are determined for a session, they persist throughout the lifetime of the session”: an ICE restart keeps them (the agent does, by itself), and they are determined again at a restart only when an agent changed between full and lite. A role conflict is repaired by the agent (Section 7.3.1.1).

The pairs the agent holds get the priority of the new role (Section 6.1.2.3), and a nomination that was being made is dropped: only the controlling agent nominates.

Source

pub fn tie_breaker(&self) -> u64

The tiebreaker the agent’s checks carry (RFC 8445 Section 5.2).

Source

pub fn is_lite(&self) -> bool

Whether this agent is a lite implementation.

Source

pub fn set_remote_lite(&mut self, lite: bool)

Tells the agent whether the peer is a lite implementation (in SDP, a=ice-lite).

It matters to a lite agent: with a full peer it answers checks and waits for the nomination; with a lite peer “no connectivity checks are ever sent. Rather, once the candidates are exchanged, each agent performs the processing described in Section 8 without connectivity checks” (RFC 8445 Section 6.1.1). See Event::LiteCandidateUpdate and Agent::replace_remote_candidates.

Source

pub fn pacing(&self) -> Duration

Ta, the pacing of connectivity checks in use (RFC 8445 Section 14.2).

Source

pub fn proposed_pacing(&self) -> Option<Duration>

The Ta this agent proposes to its peer, when it is not the default of 50 ms: “If an agent wants to use a Ta value other than the default value, the agent MUST indicate the proposed value to its peer during the establishment of the ICE session” (RFC 8445 Section 14.2; in SDP, a=ice-pacing, RFC 8839 Section 5.5, in milliseconds).

Source

pub fn set_remote_pacing(&mut self, proposed: Option<Duration>)

The peer’s proposal for Ta (None: it proposed nothing, which is the default, 50 ms). “Both agents MUST use the higher out of the two proposed values” (RFC 8445 Section 14.2): from now on Ta is the larger of this agent’s own and the peer’s.

Until this is called the agent paces by its own configuration alone: it cannot know that the peer was told. An application that configures a Ta under 50 ms and never calls this is pacing faster than the peer agreed to.

Source

pub fn replace_remote_candidates( &mut self, now: Instant, candidates: Vec<Candidate>, ) -> bool

Two lite agents: replaces the remote candidates with those of an updated candidate exchange (RFC 8445 Section 8.2: “the controlling agent MUST send an updated candidate list to reconcile different agents selecting different candidate pairs. ICE processing is complete after and only after the updated candidate exchange is complete”), and selects again. Returns false, and changes nothing, unless both agents are lite.

Source

pub fn replace_remote_candidates_pinning( &mut self, now: Instant, candidates: Vec<Candidate>, local: Option<SocketAddr>, ) -> bool

Agent::replace_remote_candidates, with the local side of the pair given: RFC 8839 Section 4.4.2.2, a lite answerer offered a=remote-candidates forms the pair from the offer’s default destination and, as its local candidate, the transport address the attribute names (the controlling agent’s remote candidate is this agent’s local one). A pair with a local candidate at local is chosen over any other; None (or an address no local candidate has) leaves the choice to the RFC 6724 preference.

Source

pub fn set_candidate_types(&mut self, candidate_types: Vec<CandidateType>)

Replaces the candidate types the agent accepts as local candidates from now on (AgentConfig::candidate_types; empty means the default set).

Candidates already added stay: a new ICE transport policy takes effect at the next gathering (RFC 9429, Section 4.1.18). A lite agent keeps host candidates only.

Source

pub fn state(&self) -> ConnectionState

The agent’s current connection state.

Source

pub fn is_valid_non_stun_traffic( &mut self, now: Instant, transport: TransportContext, ) -> bool

Whether a non-STUN datagram on transport should be accepted as media.

Guards against accepting media from an address that has not passed a connectivity check.

Source

pub fn media_path(&self) -> Option<MediaPath>

The path data (DTLS, SRTP) is sent on and accepted from: the selected pair’s, and during an ICE restart, until the restart selects a pair, the one the session had when the restart was applied.

RFC 8445 Section 9: “the ICE agent continues to use the previously selected candidate pair until a new pair is selected by the restart”. Without it every packet between apply_restart and the first nomination of the new generation was dropped, on both sides, for as long as the offer/answer exchange took.

While the controlling agent’s nomination is in flight, it is the nominated pair’s. That pair is valid, so Section 12.1 allows it (“An ICE agent MAY send data on any valid pair before selected pairs have been produced for the data stream”), and it is the pair the controlled agent selected when the nomination’s first request reached it: the peer sends on it from then on (its DTLS handshake starts), and Section 12.2 asks for that data to be received (“ICE implementations SHOULD by default be prepared to receive data on any of the candidates”). Without it, while the responses to the nomination were lost, the controlling agent dropped everything the peer sent and could answer nothing: the peer’s DTLS retransmissions went unanswered and its timer backed off for nothing, so that after a few more losses at 10 % the data channel had not opened in 30 seconds.

With AgentConfig::send_data_before_nomination, and before the first pair is selected, it is the best valid pair’s (Section 12.1).

Source

pub fn retired_media_path(&self) -> Option<MediaPath>

The path an ICE restart replaced, while data on it is still taken after the restart’s pair was selected (RETIRED_MEDIA_PATH_GRACE); nothing is sent on it.

Source

pub fn accepts_data(&self, transport: TransportContext) -> bool

Whether data (anything that is not STUN: DTLS, SRTP) that arrived on transport is accepted.

RFC 8445 Section 12.2: “ICE implementations SHOULD by default be prepared to receive data on any of the candidates provided in the most recent candidate exchange with the peer”, and “an agent will be able to determine that a data stream is from the same peer as a consequence of the STUN exchange that proceeds media data transmission”. So, once there is a path to send on (Agent::media_path), data is accepted on any local candidate, from that path’s remote address and from the remote candidate of any valid pair: an address one of the agent’s own checks was answered from, by someone who holds the peer’s password. A source that only sent requests, or that no check reached, is not accepted.

With AgentConfig::data_from_selected_pair_only only the remote address of the path data is sent on is accepted.

Source

pub fn data_path(&self, transport: TransportContext) -> Option<DataPath<'_>>

As Agent::accepts_data, saying which pair the data arrived on: the one in use, or another valid pair (its local and remote candidates).

Source

pub fn get_selected_candidate_pair(&self) -> Option<(&Candidate, &Candidate)>

Returns the selected pair (local_candidate, remote_candidate) or none

Source

pub fn get_best_available_candidate_pair( &self, ) -> Option<(&Candidate, &Candidate)>

The highest-priority pair that is still usable, whether or not it has been nominated.

Source

pub fn start_connectivity_checks( &mut self, now: Instant, is_controlling: bool, remote_ufrag: String, remote_pwd: String, ) -> Result<()>

start connectivity checks

Source

pub fn generate_restart_credentials( &mut self, ufrag: String, pwd: String, ) -> Result<()>

Restarts the ICE Agent with the provided ufrag/pwd If no ufrag/pwd is provided the Agent will generate one itself. Stages new local credentials for an ICE restart, without disturbing the live session.

This is the half of an ICE restart that an offerer needs before generating SDP: the offer must carry the new ufrag/pwd. It deliberately does not touch local_credentials, candidate pairs, or connection state, because JSEP requires createOffer to be free of side effects — and because inbound STUN is still being validated against the current credentials until the local description is applied.

Empty ufrag / pwd are filled from the crypto provider’s RNG. Call Agent::apply_restart to install them.

Source

pub fn has_pending_restart(&self) -> bool

Whether Agent::generate_restart_credentials has staged a restart that is not yet applied.

Source

pub fn discard_restart_credentials(&mut self)

Forgets the credentials Agent::generate_restart_credentials staged, so that no restart is pending: the description they were staged for was abandoned (a rolled-back remote offer, RFC 9429 section 5.7). The live session is untouched.

Source

pub fn get_restart_generation_local_candidates(&self) -> Vec<Candidate>

The local candidates gathered for the staged ICE restart (see Agent::generate_restart_credentials): added, or added again, since it was staged. The restart keeps them whether or not it keeps the others.

Source

pub fn apply_restart( &mut self, now: Instant, keep_local_candidates: bool, ) -> Result<()>

Applies a staged ICE restart, tearing down the old session and starting a new one at now.

Installs the credentials staged by Agent::generate_restart_credentials, if any, then discards the remote credentials, pending binding requests, candidate pairs and selected pair, and restarts the agent’s timers. With nothing staged, the current credentials are kept and only the session is restarted.

Source

pub fn restart( &mut self, now: Instant, ufrag: String, pwd: String, keep_local_candidates: bool, ) -> Result<()>

Generates and immediately applies an ICE restart.

Equivalent to Agent::generate_restart_credentials followed by Agent::apply_restart. A JSEP-conformant offerer wants the two halves separately — credentials at createOffer, application at setLocalDescription — but this remains the right call for anything that restarts in one step.

Source

pub fn get_local_candidates(&self) -> &[Candidate]

Returns the local candidates.

Source

pub fn get_remote_candidates(&self) -> &[Candidate]

The remote candidates this agent has been given, in the order they were added.

Trait Implementations§

Source§

impl Protocol<TransportMessage<BytesMut>, (), ()> for Agent

Source§

type Rout = TransportMessage<BytesMut>

Output read message type Read more
Source§

type Wout = TransportMessage<BytesMut>

Output write message type Read more
Source§

type Eout = TaggedEvent

Output event type Read more
Source§

type Error = Error

Error type for protocol operations
Source§

type Time = Instant

Time/Instant type for timeout handling Read more
Source§

fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<()>

Handle an incoming read message. Read more
Source§

fn poll_read(&mut self) -> Option<Self::Rout>

Poll for a processed read message. Read more
Source§

fn handle_write(&mut self, _msg: ()) -> Result<(), Self::Error>

Handle an outgoing write message. Read more
Source§

fn poll_write(&mut self) -> Option<Self::Wout>

Poll for a processed write message. Read more
Source§

fn handle_event(&mut self, _evt: ()) -> Result<(), Self::Error>

Handle a custom event. Read more
Source§

fn poll_event(&mut self) -> Option<Self::Eout>

Poll for a generated event. Read more
Source§

fn handle_timeout(&mut self, now: Self::Time) -> Result<(), Self::Error>

Handle a timeout event. Read more
Source§

fn poll_timeout(&mut self) -> Option<Self::Time>

Poll for the next timeout deadline. Read more
Source§

fn close(&mut self) -> Result<(), Self::Error>

Close the protocol. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Agent

§

impl !UnwindSafe for Agent

§

impl Forget for Agent

§

impl Freeze for Agent

§

impl Move for Agent

§

impl Send for Agent

§

impl Sync for Agent

§

impl Unpin for Agent

§

impl UnsafeUnpin for Agent

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.