Skip to main content

rtci_ice/agent/
agent_proto.rs

1use super::*;
2use mdns::MDNS_PORT;
3
4impl sansio::Protocol<TaggedBytesMut, (), ()> for Agent {
5    type Rout = TaggedBytesMut;
6    type Wout = TaggedBytesMut;
7    type Eout = TaggedEvent;
8    type Error = Error;
9    type Time = Instant;
10
11    fn handle_read(&mut self, msg: TaggedBytesMut) -> Result<()> {
12        self.clock = self.clock.max(msg.now);
13        // The embedder's address policy: a datagram from a source it refuses is dropped before
14        // anything reads it, as a firewall would drop it: no response, no peer-reflexive
15        // candidate, no consent, no pair (`AgentConfig::address_policy`).
16        if !self.remote_policy.allows_source(msg.transport.peer_addr) {
17            debug!(
18                "[{}]: dropped a datagram from {}: the address policy refuses the source",
19                self.get_name(),
20                msg.transport.peer_addr
21            );
22            return Ok(());
23        }
24        // mDNS arrives on the host's mDNS socket, the one bound to MDNS_PORT that this
25        // agent's queries leave from. A source port says nothing: anyone can send from 5353 to
26        // an ICE socket, and an answer taken from there could point a `.local` candidate at an
27        // address of the sender's choosing. Datagrams on ICE sockets are ICE traffic, whatever
28        // their source port.
29        // (Whatever arrives on the mDNS socket goes to the mDNS connection, from any source
30        // port: a query from another port is a legacy unicast query, which RFC 6762 Section
31        // 6.7 has answered by unicast; a response from another port is refused there. With
32        // `multicast_dns_destination_reported` the local address is the datagram's
33        // destination, the mDNS group's or the host's own.)
34        if msg.transport.local_addr.port() == MDNS_PORT
35            && self
36                .find_local_candidate(msg.transport.local_addr, msg.transport.transport_protocol)
37                .is_none()
38        {
39            let now = msg.now;
40            if let Some(mdns_conn) = &mut self.mdns {
41                mdns_conn.handle_read(msg)?;
42            }
43            let remote_candidates = self.drain_mdns_events(now);
44            self.trigger_request_connectivity_check(remote_candidates);
45
46            Ok(())
47        } else if self.handle_previous_generation(&msg) {
48            // Consent traffic of the generation an ICE restart replaced, on the path that is
49            // still in use.
50            Ok(())
51        } else if let Some(local_index) =
52            self.find_local_candidate(msg.transport.local_addr, msg.transport.transport_protocol)
53        {
54            self.handle_inbound_candidate_msg(local_index, msg)
55        } else {
56            warn!(
57                "[{}]: Discarded message, not a valid local candidate from {:?}:{}",
58                self.get_name(),
59                msg.transport.transport_protocol,
60                msg.transport.local_addr,
61            );
62            Err(Error::ErrUnhandledStunpacket)
63        }
64    }
65
66    fn poll_read(&mut self) -> Option<Self::Rout> {
67        None
68    }
69
70    fn handle_write(&mut self, _msg: ()) -> std::result::Result<(), Self::Error> {
71        Ok(())
72    }
73
74    fn poll_write(&mut self) -> Option<Self::Wout> {
75        if let Some(mdns_conn) = &mut self.mdns {
76            while let Some(msg) = mdns_conn.poll_write() {
77                self.write_outs.push_back(msg);
78            }
79        }
80
81        self.write_outs.pop_front()
82    }
83
84    fn handle_event(&mut self, _evt: ()) -> std::result::Result<(), Self::Error> {
85        Ok(())
86    }
87
88    fn poll_event(&mut self) -> Option<Self::Eout> {
89        self.event_outs.pop_front()
90    }
91
92    fn handle_timeout(&mut self, now: Self::Time) -> std::result::Result<(), Self::Error> {
93        self.clock = self.clock.max(now);
94        if let Some(mdns_conn) = &mut self.mdns {
95            let _ = mdns_conn.handle_timeout(now);
96        }
97        let remote_candidates = self.drain_mdns_events(now);
98        self.trigger_request_connectivity_check(remote_candidates);
99
100        // Requests that were answered before the peer's credentials arrived (RFC 8445
101        // Section 7.3) are processed now that there are some.
102        if !self.early_requests.is_empty() {
103            self.process_early_requests(now);
104        }
105
106        // The path an ICE restart left in use keeps its consent checks, with or without the
107        // peer's new credentials.
108        self.maintain_previous_generation(now);
109
110        if self.ice_deadline().is_some_and(|deadline| deadline <= now) {
111            self.contact(now);
112        }
113        Ok(())
114    }
115
116    fn poll_timeout(&mut self) -> Option<Self::Time> {
117        let mdns_timeout = if let Some(mdns_conn) = &mut self.mdns {
118            mdns_conn.poll_timeout()
119        } else {
120            None
121        };
122
123        let ice_timeout = self.ice_deadline();
124
125        // This treats the options as a collection and picks the minimum
126        [
127            mdns_timeout,
128            ice_timeout,
129            self.previous_generation_deadline(),
130        ]
131        .into_iter()
132        .flatten()
133        .min()
134    }
135
136    fn close(&mut self) -> std::result::Result<(), Self::Error> {
137        self.set_selected_pair(None, None);
138        self.delete_all_candidates(false);
139        self.update_connection_state(None, ConnectionState::Closed);
140        if let Some(mdns_conn) = &mut self.mdns {
141            mdns_conn.close()?;
142        }
143        Ok(())
144    }
145}