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