Skip to content

Commit 9a33523

Browse files
committed
fix: Disable mDNS when responder startup fails
Fixes INTIFACE-CENTRAL-10K
1 parent eca787d commit 9a33523

2 files changed

Lines changed: 39 additions & 4 deletions

File tree

crates/intiface_engine/src/engine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl IntifaceEngine {
117117
// TODO Unregister whenever we have a live connection
118118

119119
// TODO Support different services for engine versus repeater
120-
Some(IntifaceMdns::new())
120+
IntifaceMdns::new()
121121
} else {
122122
None
123123
};

crates/intiface_engine/src/mdns.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,63 @@
66
// for full license information.
77

88
use rand::distr::{Alphanumeric, SampleString};
9+
use std::{future::Future, io};
910

1011
pub struct IntifaceMdns {
1112
_responder: libmdns::Responder,
1213
_svc: libmdns::Service,
1314
}
1415

1516
impl IntifaceMdns {
16-
pub fn new() -> Self {
17+
pub fn new() -> Option<Self> {
1718
let random_suffix = Alphanumeric.sample_string(&mut rand::rng(), 6);
1819
let instance_name = format!("Intiface {}", random_suffix);
1920
info!(
2021
"Bringing up mDNS Advertisment using instance name {}",
2122
instance_name
2223
);
2324

24-
let (_responder, task) = libmdns::Responder::with_default_handle().unwrap();
25+
Self::from_responder_result(&instance_name, libmdns::Responder::with_default_handle())
26+
}
27+
28+
fn from_responder_result<T>(
29+
instance_name: &str,
30+
responder_result: io::Result<(libmdns::Responder, T)>,
31+
) -> Option<Self>
32+
where
33+
T: Future<Output = ()> + Send + 'static,
34+
{
35+
let (_responder, task) = match responder_result {
36+
Ok(result) => result,
37+
Err(err) => {
38+
warn!("Unable to bring up mDNS advertisement: {}", err);
39+
return None;
40+
}
41+
};
2542
let _svc = _responder.register("_intiface_engine._tcp", &instance_name, 12345, &["path=/"]);
2643
tokio::spawn(async move {
2744
info!("Entering up mDNS task");
2845
task.await;
2946
info!("Exiting mDNS task");
3047
});
31-
Self { _responder, _svc }
48+
Some(Self { _responder, _svc })
49+
}
50+
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn mdns_startup_error_disables_advertisement() {
58+
let result = IntifaceMdns::from_responder_result::<std::future::Pending<()>>(
59+
"Intiface Test",
60+
Err(io::Error::new(
61+
io::ErrorKind::AddrInUse,
62+
"Address already in use",
63+
)),
64+
);
65+
66+
assert!(result.is_none());
3267
}
3368
}

0 commit comments

Comments
 (0)