diff options
Diffstat (limited to 'server/src/network')
| -rw-r--r-- | server/src/network/mdns.rs | 15 | 
1 files changed, 10 insertions, 5 deletions
| diff --git a/server/src/network/mdns.rs b/server/src/network/mdns.rs index 43f7bc91..5bd71c60 100644 --- a/server/src/network/mdns.rs +++ b/server/src/network/mdns.rs @@ -22,10 +22,10 @@ use hurrycurry_protocol::VERSION;  use log::{info, warn};  use mdns_sd::{ServiceDaemon, ServiceInfo};  use rand::random; -use std::{collections::HashMap, sync::Arc, time::Duration}; +use std::{collections::HashMap, net::SocketAddr, sync::Arc, time::Duration};  use tokio::{sync::RwLock, time::interval}; -pub async fn mdns_loop(name: String, port: u16, state: Arc<RwLock<Server>>) { +pub async fn mdns_loop(name: String, listen_addr: SocketAddr, state: Arc<RwLock<Server>>) {      let d = match ServiceDaemon::new() {          Ok(d) => d,          Err(e) => { @@ -37,7 +37,7 @@ pub async fn mdns_loop(name: String, port: u16, state: Arc<RwLock<Server>>) {      let hostname = format!("hks-{}.local.", random::<u64>());      loop {          interval.tick().await; -        if let Err(e) = update_service(&d, &state, &name, &hostname, port).await { +        if let Err(e) = update_service(&d, &state, &name, &hostname, listen_addr).await {              warn!("mDNS service update failed: {e}");          }          info!("updated mDNS service record"); @@ -49,12 +49,17 @@ async fn update_service(      state: &Arc<RwLock<Server>>,      name: &str,      hostname: &str, -    port: u16, +    listen_addr: SocketAddr,  ) -> Result<()> {      let addrs = get_if_addrs()?          .into_iter()          .map(|e| e.addr.ip())          .filter(|a| !a.is_loopback()) +        .filter(|a| { +            (a.is_ipv6() && listen_addr.is_ipv6()) +                || (a.is_ipv4() && listen_addr.is_ipv4()) +                || (a.is_ipv4() && listen_addr.is_ipv6() && !cfg!(windows)) +        })          .collect::<Vec<_>>();      let players = state.read().await.count_chefs(); @@ -64,7 +69,7 @@ async fn update_service(          name,          hostname,          &*addrs, -        port, +        listen_addr.port(),          HashMap::from_iter([              ("players".to_string(), players.to_string()),              ("version".to_string(), env!("CARGO_PKG_VERSION").to_string()), | 
