use ice::agent::MediaPath;
use shared::{TransportContext, TransportProtocol};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PreviousPathRoute {
Current,
Previous,
Either,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PreviousAssociationPath {
path: MediaPath,
new_received: bool,
new_sent: bool,
}
impl PreviousAssociationPath {
pub(crate) fn new(path: MediaPath) -> Self {
Self {
path,
new_received: false,
new_sent: false,
}
}
fn ordered(&self) -> bool {
self.path.transport_protocol == TransportProtocol::TCP
}
fn is(
&self,
local_addr: std::net::SocketAddr,
peer_addr: std::net::SocketAddr,
protocol: TransportProtocol,
) -> bool {
self.path.local_addr == local_addr
&& self.path.peer_addr == peer_addr
&& self.path.transport_protocol == protocol
}
pub(crate) fn route_read(
&mut self,
transport: &TransportContext,
datagram: &[u8],
) -> Option<PreviousPathRoute> {
if !self.is(
transport.local_addr,
transport.peer_addr,
transport.transport_protocol,
) {
return None;
}
let begins_handshake = plaintext_epoch_zero(datagram);
if self.ordered() {
if self.new_received || begins_handshake {
self.new_received = true;
return Some(PreviousPathRoute::Current);
}
return Some(PreviousPathRoute::Previous);
}
Some(if begins_handshake {
PreviousPathRoute::Current
} else {
PreviousPathRoute::Either
})
}
pub(crate) fn sent_current(&mut self, path: &MediaPath) {
if self.ordered() && *path == self.path {
self.new_sent = true;
}
}
pub(crate) fn may_send_previous(&self, path: &MediaPath) -> bool {
*path == self.path && !(self.ordered() && self.new_sent)
}
}
pub(crate) fn plaintext_epoch_zero(datagram: &[u8]) -> bool {
datagram.len() >= 13
&& (20..=26).contains(&datagram[0])
&& datagram[1] == 0xfe
&& datagram[3] == 0
&& datagram[4] == 0
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::SocketAddr;
fn addr(s: &str) -> SocketAddr {
s.parse().unwrap()
}
fn path(protocol: TransportProtocol) -> MediaPath {
MediaPath {
local_addr: addr("10.0.0.1:5000"),
peer_addr: addr("10.0.0.2:5000"),
transport_protocol: protocol,
}
}
fn on(path: &MediaPath) -> TransportContext {
TransportContext {
local_addr: path.local_addr,
peer_addr: path.peer_addr,
transport_protocol: path.transport_protocol,
ecn: None,
}
}
const CLIENT_HELLO: [u8; 13] = [22, 0xfe, 0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 40];
const PROTECTED_12: [u8; 13] = [23, 0xfe, 0xfd, 0, 1, 0, 0, 0, 0, 0, 7, 0, 40];
const PROTECTED_13: [u8; 13] = [0x2f, 0, 7, 0, 40, 1, 2, 3, 4, 5, 6, 7, 8];
#[test]
fn a_record_in_the_clear_is_told_from_a_protected_one() {
assert!(plaintext_epoch_zero(&CLIENT_HELLO));
assert!(plaintext_epoch_zero(&[
20, 0xfe, 0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
]));
assert!(!plaintext_epoch_zero(&PROTECTED_12));
assert!(!plaintext_epoch_zero(&PROTECTED_13));
assert!(!plaintext_epoch_zero(&CLIENT_HELLO[..12]));
}
#[test]
fn on_a_tcp_connection_the_order_tells_the_associations_apart() {
let tcp = path(TransportProtocol::TCP);
let mut p = PreviousAssociationPath::new(tcp);
assert_eq!(
p.route_read(&on(&tcp), &PROTECTED_12),
Some(PreviousPathRoute::Previous)
);
assert_eq!(
p.route_read(&on(&tcp), &PROTECTED_13),
Some(PreviousPathRoute::Previous)
);
assert_eq!(
p.route_read(&on(&tcp), &CLIENT_HELLO),
Some(PreviousPathRoute::Current),
"the new association's first record"
);
assert_eq!(
p.route_read(&on(&tcp), &PROTECTED_12),
Some(PreviousPathRoute::Current),
"after it, everything on the connection is the new association's"
);
let mut other = on(&tcp);
other.peer_addr = addr("10.0.0.2:5001");
assert_eq!(
p.route_read(&other, &PROTECTED_12),
None,
"another connection"
);
}
#[test]
fn on_a_tcp_connection_the_replaced_association_sends_only_before_the_new_one() {
let tcp = path(TransportProtocol::TCP);
let mut p = PreviousAssociationPath::new(tcp);
assert!(p.may_send_previous(&tcp));
let mut elsewhere = tcp;
elsewhere.peer_addr = addr("10.0.0.2:5001");
p.sent_current(&elsewhere);
assert!(
p.may_send_previous(&tcp),
"the new association on another connection"
);
assert!(
!p.may_send_previous(&elsewhere),
"not the replaced association's path"
);
p.sent_current(&tcp);
assert!(
!p.may_send_previous(&tcp),
"the new association began on the connection"
);
}
#[test]
fn on_a_shared_udp_five_tuple_the_records_tell_the_associations_apart() {
let udp = path(TransportProtocol::UDP);
let mut p = PreviousAssociationPath::new(udp);
assert_eq!(
p.route_read(&on(&udp), &CLIENT_HELLO),
Some(PreviousPathRoute::Current)
);
assert_eq!(
p.route_read(&on(&udp), &PROTECTED_12),
Some(PreviousPathRoute::Either)
);
assert_eq!(
p.route_read(&on(&udp), &PROTECTED_13),
Some(PreviousPathRoute::Either),
"no order on UDP: the clear record did not switch the path"
);
p.sent_current(&udp);
assert!(p.may_send_previous(&udp));
}
}