#![cfg(any(target_os = "linux", target_os = "windows"))]
use rtci::ice::mdns::MulticastDnsMode;
use rtci::mdns::MdnsRateLimit;
use rtci::peer_connection::RTCPeerConnectionBuilder;
use rtci::peer_connection::configuration::RTCConfigurationBuilder;
use rtci::peer_connection::configuration::media_engine::MediaEngine;
use rtci::peer_connection::configuration::setting_engine::SettingEngineBuilder;
use rtci::peer_connection::event::RTCPeerConnectionEvent;
use rtci::peer_connection::state::RTCPeerConnectionState;
use rtci::peer_connection::transport::RTCIceCandidateInit;
use rtci::rtp_transceiver::RTCRtpTransceiverDirection;
use rtci::rtp_transceiver::RTCRtpTransceiverInit;
use rtci::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RtpCodecKind};
use rtci::sansio::Protocol;
use rtci_stream_driver::peer::{PeerDriver, PeerDriverConfig};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::time::{Duration, Instant};
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
static MDNS_GROUP_IN_USE: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
fn mdns_egress_ip() -> Option<IpAddr> {
std::net::UdpSocket::bind("0.0.0.0:0")
.and_then(|socket| {
socket.connect("192.0.2.1:9")?;
socket.local_addr()
})
.ok()
.map(|addr| addr.ip())
.filter(|ip| !ip.is_unspecified() && !ip.is_loopback())
}
fn group_answer() -> Option<IpAddr> {
std::net::UdpSocket::bind("0.0.0.0:0")
.and_then(|socket| {
socket.connect((rtci::mdns::MDNS_MULTICAST_IPV4, rtci::mdns::MDNS_PORT))?;
socket.local_addr()
})
.ok()
.map(|addr| addr.ip())
}
fn mdns_egress_or_skip(test: &str) -> Option<IpAddr> {
let ip = mdns_egress_ip();
if ip.is_none() {
assert!(
!cfg!(target_os = "linux"),
"{test} needs an interface that carries multicast to the mDNS group"
);
println!("SKIPPED {test}: no interface on this host carries multicast to the mDNS group");
return None;
}
ip.filter(|ip| multicast_or_skip(test, *ip))
}
fn host_loops_multicast(ip: IpAddr) -> bool {
let IpAddr::V4(interface) = ip else {
return false;
};
let probe = || -> std::io::Result<bool> {
let group = SocketAddr::from((rtci::mdns::MDNS_MULTICAST_IPV4, rtci::mdns::MDNS_PORT));
let receiver: std::net::UdpSocket = rtci::mdns::MulticastSocket::new()
.with_interface(interface)
.into_std()?;
receiver.set_nonblocking(false)?;
receiver.set_read_timeout(Some(Duration::from_millis(200)))?;
let sender = socket2::Socket::new(
socket2::Domain::IPV4,
socket2::Type::DGRAM,
Some(socket2::Protocol::UDP),
)?;
sender.set_multicast_if_v4(&interface)?;
sender.set_multicast_loop_v4(true)?;
sender.bind(&SocketAddr::new(ip, 0).into())?;
let sender: std::net::UdpSocket = sender.into();
let nonce = std::process::id()
^ std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.subsec_nanos())
.unwrap_or_default();
let [id0, id1, ..] = nonce.to_be_bytes();
let message = [id0, id1, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let mut buf = [0u8; 1500];
for _ in 0..10 {
sender.send_to(&message, group)?;
let until = Instant::now() + Duration::from_millis(200);
while Instant::now() < until {
match receiver.recv_from(&mut buf) {
Ok((n, _)) if buf[..n] == message => return Ok(true),
Ok(_) => continue,
Err(e)
if matches!(
e.kind(),
std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut
) =>
{
break;
}
Err(e) => return Err(e),
}
}
}
Ok(false)
};
probe().unwrap_or_else(|e| {
println!("multicast probe on {ip}: {e}");
false
})
}
fn host_multicast_expected() -> bool {
std::env::var("RTCI_EXPECT_HOST_MULTICAST")
.is_ok_and(|v| v.split(',').any(|os| os.trim() == std::env::consts::OS))
}
fn multicast_or_skip(test: &str, ip: IpAddr) -> bool {
if host_loops_multicast(ip) {
return true;
}
if host_multicast_expected() {
panic!(
"{test}: this host does not deliver multicast to itself on {ip}, and the job says it \
does (RTCI_EXPECT_HOST_MULTICAST): the real-socket mDNS tests must run here"
);
}
println!("SKIPPED {test}: the host does not loop multicast on {ip} back to this process");
false
}
struct Side {
driver: PeerDriver,
outgoing: Vec<RTCIceCandidateInit>,
signalled: Vec<String>,
}
impl Side {
fn new(ip: IpAddr, limit: Option<MdnsRateLimit>) -> Result<Self> {
Self::with_config(PeerDriverConfig {
mdns_rate_limit: limit,
..PeerDriverConfig::on(vec![ip])
})
}
fn asking_for_loopback_mdns(ip: IpAddr, limit: Option<MdnsRateLimit>) -> Result<Self> {
Self::with_config(PeerDriverConfig {
mdns_rate_limit: limit,
mdns_on_loopback_on_windows: true,
..PeerDriverConfig::on(vec![ip])
})
}
fn with_config(config: PeerDriverConfig) -> Result<Self> {
let _ = env_logger::builder().is_test(true).try_init();
let mut media_engine = MediaEngine::default();
media_engine.register_default_codecs()?;
let settings =
SettingEngineBuilder::new().with_multicast_dns_mode(MulticastDnsMode::QueryAndGather);
let pc = RTCPeerConnectionBuilder::new()
.with_configuration(RTCConfigurationBuilder::new().build())
.with_media_engine(media_engine)
.with_setting_engine(settings.build())
.build(Instant::now())?;
Ok(Self {
driver: PeerDriver::new(pc, config),
outgoing: vec![],
signalled: vec![],
})
}
fn collect(&mut self) {
while let Some(event) = self.driver.poll_event() {
if let RTCPeerConnectionEvent::OnIceCandidateEvent(event) = event
&& let Some(candidate) = event.candidate.filter(|c| !c.end_of_candidates)
&& let Ok(init) = candidate.to_json()
{
self.signalled.push(init.candidate.clone());
self.outgoing.push(init);
}
}
while self.driver.pc_mut().poll_read().is_some() {}
}
fn connected(&self) -> bool {
self.driver.pc().connection_state() == RTCPeerConnectionState::Connected
}
}
async fn round(a: &mut Side, b: &mut Side) -> Result<()> {
a.driver.step(Duration::from_millis(5)).await?;
b.driver.step(Duration::from_millis(5)).await?;
a.collect();
b.collect();
for candidate in std::mem::take(&mut a.outgoing) {
b.driver.pc_mut().add_remote_candidate(candidate)?;
}
for candidate in std::mem::take(&mut b.outgoing) {
a.driver.pc_mut().add_remote_candidate(candidate)?;
}
Ok(())
}
async fn connect(a: &mut Side, b: &mut Side, limit: Duration) -> Result<bool> {
a.driver.pc_mut().add_transceiver_from_kind(
RtpCodecKind::Audio,
Some(RTCRtpTransceiverInit {
direction: RTCRtpTransceiverDirection::Sendrecv,
streams: vec![],
send_encodings: vec![RTCRtpEncodingParameters {
active: true,
..Default::default()
}],
}),
)?;
let now = Instant::now();
let offer = a.driver.pc_mut().create_offer(None)?;
a.driver
.pc_mut()
.set_local_description(now, offer.clone())?;
b.driver.pc_mut().set_remote_description(now, offer)?;
let answer = b.driver.pc_mut().create_answer(None)?;
b.driver
.pc_mut()
.set_local_description(now, answer.clone())?;
a.driver.pc_mut().set_remote_description(now, answer)?;
let until = Instant::now() + limit;
while Instant::now() < until {
round(a, b).await?;
if a.connected() && b.connected() {
return Ok(true);
}
}
Ok(false)
}
fn assert_only_local_names(who: &str, side: &Side, ip: IpAddr) {
assert!(!side.signalled.is_empty(), "{who}: candidates signalled");
for candidate in &side.signalled {
assert!(
candidate.contains(".local ") && !candidate.contains(&ip.to_string()),
"{who}: the host address is hidden behind a .local name: {candidate}"
);
}
}
#[tokio::test]
async fn driven_connections_resolve_each_others_mdns_candidates_and_connect() -> Result<()> {
let _group = MDNS_GROUP_IN_USE.lock().await;
let Some(ip) =
mdns_egress_or_skip("driven_connections_resolve_each_others_mdns_candidates_and_connect")
else {
return Ok(());
};
let limit = MdnsRateLimit::default();
let mut a = Side::new(ip, Some(limit.clone()))?;
let mut b = Side::new(ip, Some(limit))?;
let up = connect(&mut a, &mut b, Duration::from_secs(30)).await?;
assert_only_local_names("a", &a, ip);
assert_only_local_names("b", &b, ip);
assert!(
a.driver.mdns_datagrams_sent() > 0 && b.driver.mdns_datagrams_sent() > 0,
"both drivers sent mDNS: {} and {}",
a.driver.mdns_datagrams_sent(),
b.driver.mdns_datagrams_sent()
);
for (who, side) in [("a", &a), ("b", &b)] {
assert!(
side.driver.mdns_datagrams_received() > 0,
"{who}: the driver's mDNS sockets received the group's datagrams"
);
}
assert!(
up,
"connected over the resolved .local candidates: a {:?} / b {:?}",
a.driver.pc().connection_state(),
b.driver.pc().connection_state()
);
Ok(())
}
#[tokio::test]
#[cfg_attr(
windows,
ignore = "Windows: mDNS on loopback leaves the group's destination-cache route on loopback for the whole host; run by scripts/windows-poisoned-mdns-route.sh (cargo.yml, GitHub-hosted Windows), which deletes the entry"
)]
async fn drivers_on_loopback_resolve_each_other_on_the_loopback_interface() -> Result<()> {
let _group = MDNS_GROUP_IN_USE.lock().await;
let ip = IpAddr::from(Ipv4Addr::LOCALHOST);
if !multicast_or_skip(
"drivers_on_loopback_resolve_each_other_on_the_loopback_interface",
ip,
) {
return Ok(());
}
let limit = MdnsRateLimit::default();
let mut a = Side::asking_for_loopback_mdns(ip, Some(limit.clone()))?;
let mut b = Side::asking_for_loopback_mdns(ip, Some(limit))?;
let up = connect(&mut a, &mut b, Duration::from_secs(30)).await?;
assert_only_local_names("a", &a, ip);
assert_only_local_names("b", &b, ip);
assert!(
up,
"connected over the resolved .local candidates on loopback: a {:?} (mDNS sent {}, received {}) / b {:?} (sent {}, received {})",
a.driver.pc().connection_state(),
a.driver.mdns_datagrams_sent(),
a.driver.mdns_datagrams_received(),
b.driver.pc().connection_state(),
b.driver.mdns_datagrams_sent(),
b.driver.mdns_datagrams_received()
);
Ok(())
}
#[cfg(windows)]
#[tokio::test]
#[ignore = "needs a Windows host whose route for 224.0.0.251 is not on loopback; run by scripts/windows-poisoned-mdns-route.sh (cargo.yml, GitHub-hosted Windows) before it moves the route"]
async fn by_default_drivers_on_loopback_leave_the_group_route_off_loopback() -> Result<()> {
let _group = MDNS_GROUP_IN_USE.lock().await;
let ip = IpAddr::from(Ipv4Addr::LOCALHOST);
let before = group_answer();
assert!(
before.is_some_and(|ip| !ip.is_loopback()),
"PRECONDITION: the host's answer for the group is {before:?} before the test; it must not \
be loopback, or the test proves nothing"
);
let mut a = Side::new(ip, Some(MdnsRateLimit::default()))?;
let mut b = Side::new(ip, Some(MdnsRateLimit::default()))?;
let _ = connect(&mut a, &mut b, Duration::from_secs(5)).await?;
let seen = [("a", &a), ("b", &b)].map(|(who, side)| {
(
who,
side.driver.unsent_datagrams(),
side.driver.mdns_available(false),
)
});
drop((a, b));
let after = group_answer();
println!("### group answer before {before:?}, after the default drivers on loopback {after:?}");
assert!(
after.is_some_and(|ip| !ip.is_loopback()),
"the default drivers on loopback left the group's answer at {after:?} (was {before:?})"
);
for (who, unsent, available) in seen {
assert!(
unsent > 0,
"{who}: the connection had mDNS for its loopback name to send"
);
assert!(!available, "{who}: no IPv4 mDNS socket for loopback only");
}
Ok(())
}