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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use super::*;
use mdns::MDNS_PORT;
/// The line logged for a datagram whose local address is no local candidate's (RFC 8445
/// Section 7.3.1.4: the local candidate has "the transport address (as seen by the agent) on
/// which the STUN request was received"). An unspecified local address (`0.0.0.0:port`,
/// `[::]:port`) is never a candidate's transport address: it is what a socket bound to the
/// unspecified address reports as its own, and the line says so. The datagram is discarded
/// either way; only the wording differs.
pub(crate) fn unmatched_local_address_log(name: &str, transport: &TransportContext) -> String {
if transport.local_addr.ip().is_unspecified() {
format!(
"[{name}]: Discarded message from {}: its local address {} is unspecified. The local \
address of a datagram is the address of the local candidate it arrived on: a socket \
bound to the unspecified address must report the concrete address each datagram \
was received on (its destination address, IP_PKTINFO / IPV6_RECVPKTINFO), and a \
datagram relayed by a TURN allocation the application runs carries the relayed \
address",
transport.peer_addr, transport.local_addr,
)
} else {
format!(
"[{name}]: Discarded message, not a valid local candidate from {:?}:{}",
transport.transport_protocol, transport.local_addr,
)
}
}
impl sansio::Protocol<TaggedBytesMut, (), ()> for Agent {
type Rout = TaggedBytesMut;
type Wout = TaggedBytesMut;
type Eout = TaggedEvent;
type Error = Error;
type Time = Instant;
/// Reads one datagram. `msg.transport.local_addr` is the transport address of the local
/// candidate the datagram arrived on, matched exactly (RFC 8445 Section 7.3.1.4): the
/// address of a host candidate, or, for a datagram a TURN allocation relayed (taken out of
/// its Data indication or ChannelData), the relayed address of the relayed candidate. A
/// socket bound to the unspecified address reports the concrete address the datagram was
/// received on, not `0.0.0.0:port` / `[::]:port`. A datagram on an address that is no
/// local candidate's is discarded with [`Error::ErrUnhandledStunpacket`].
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!(
"{}",
unmatched_local_address_log(self.get_name(), &msg.transport)
);
Err(Error::ErrUnhandledStunpacket)
}
}
fn poll_read(&mut self) -> Option<Self::Rout> {
None
}
fn handle_write(&mut self, _msg: ()) -> std::result::Result<(), Self::Error> {
Ok(())
}
/// The next datagram to send. `transport.local_addr` is the base of the local candidate
/// it leaves from (RFC 8445 Section 6.1.2): a host candidate's address (send it from that
/// socket), or a relayed candidate's relayed address, in which case it goes through that
/// candidate's TURN allocation (a Send indication or ChannelData to `peer_addr`), not
/// directly from a socket. (mDNS datagrams leave from port 5353, the host's mDNS socket.)
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(())
}
}