use crate::peer_connection::configuration::RTCIceTransportPolicy;
use ice::candidate::Candidate;
use ice::tcp_type::TcpType;
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()) && candidate.tcp_type() != TcpType::Active {
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 an_active_tcp_candidates_discard_port_is_not_a_bad_port() {
let all = RTCIceTransportPolicy::All;
let tcp = |tcp_type: &str| {
unmarshal_candidate(&format!(
"1 1 tcp 2130706431 192.0.2.1 9 typ host tcptype {tcp_type}"
))
.expect("a candidate")
};
assert!(!is_administratively_prohibited(&tcp("active"), all));
assert!(is_administratively_prohibited(&tcp("passive"), all));
}
#[test]
fn a_check_from_a_bad_port_teaches_no_peer_reflexive_candidate() {
use crate::peer_connection::configuration::RTCConfiguration;
use crate::peer_connection::configuration::setting_engine::SettingEngineBuilder;
use ice::AddressPurpose;
let config = |settings: SettingEngineBuilder| {
crate::peer_connection::internal::ice_agent_config(
&RTCConfiguration::default(),
&settings.build(),
)
.expect("an agent configuration")
};
let policy = config(SettingEngineBuilder::new()).address_policy;
let at = |port: u16| std::net::SocketAddr::from(([192, 0, 2, 1], port));
for port in [5060, 6000, 10080] {
assert!(
!policy.allows(at(port), AddressPurpose::PeerReflexiveSource),
"{port}"
);
}
assert!(!BAD_PORTS.contains(&5062));
assert!(policy.allows(at(5062), AddressPurpose::PeerReflexiveSource));
assert!(policy.allows(at(5060), AddressPurpose::InboundDatagramSource));
let refusing =
config(SettingEngineBuilder::new().with_ice_address_policy(
ice::IceAddressPolicy::new(|addr, _, _| addr.port() != 7000),
))
.address_policy;
assert!(!refusing.allows(at(7000), AddressPurpose::PeerReflexiveSource));
assert!(!refusing.allows(at(5060), AddressPurpose::PeerReflexiveSource));
assert!(refusing.allows(at(7001), AddressPurpose::PeerReflexiveSource));
}
#[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
));
}
}