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