use crate::peer_connection::configuration::RTCIceTransportPolicy;
use ice::candidate::Candidate;
use std::net::IpAddr;
pub(crate) const BAD_PORTS: [u16; 83] = [
0, 1, 7, 9, 11, 13, 15, 17, 19, 20, 21, 22, 23, 25, 37, 42, 43, 53, 69, 77, 79, 87, 95, 101,
102, 103, 104, 109, 110, 111, 113, 115, 117, 119, 123, 135, 137, 139, 143, 161, 179, 389, 427,
465, 512, 513, 514, 515, 526, 530, 531, 532, 540, 548, 554, 556, 563, 587, 601, 636, 989, 990,
993, 995, 1719, 1720, 1723, 2049, 3659, 4045, 4190, 5060, 5061, 6000, 6566, 6665, 6666, 6667,
6668, 6669, 6679, 6697, 10080,
];
pub(crate) fn is_administratively_prohibited(
candidate: &Candidate,
policy: RTCIceTransportPolicy,
) -> bool {
if BAD_PORTS.contains(&candidate.port()) {
return true;
}
policy == RTCIceTransportPolicy::Relay && candidate.address().parse::<IpAddr>().is_err()
}
#[cfg(test)]
mod tests {
use super::*;
use ice::candidate::unmarshal_candidate;
fn candidate(address: &str, port: u16) -> Candidate {
unmarshal_candidate(&format!("1 1 udp 2130706431 {address} {port} typ host"))
.expect("a candidate")
}
#[test]
fn bad_ports_and_names_under_the_relay_policy_are_prohibited() {
let all = RTCIceTransportPolicy::All;
let relay = RTCIceTransportPolicy::Relay;
for port in [1, 6000, 5060, 10080] {
assert!(is_administratively_prohibited(
&candidate("192.0.2.1", port),
all
));
}
assert!(!is_administratively_prohibited(
&candidate("192.0.2.1", 6001),
all
));
assert!(!is_administratively_prohibited(
&candidate("2001:db8::1", 50000),
relay
));
assert!(!is_administratively_prohibited(
&candidate("192.0.2.1", 50000),
relay
));
let mdns = "0f0e0d0c-0b0a-0908-0706-050403020100.local";
assert!(!is_administratively_prohibited(
&candidate(mdns, 50000),
all
));
assert!(is_administratively_prohibited(
&candidate(mdns, 50000),
relay
));
}
}