From 5eaad112b0023a4f517efdc060842c9a302c3f09 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Mon, 10 Jul 2023 02:09:32 -0500 Subject: [PATCH] publicip: fix IPv4 resolution on macOS (#275) --- publicip/src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/publicip/src/lib.rs b/publicip/src/lib.rs index aa12c36..962736c 100644 --- a/publicip/src/lib.rs +++ b/publicip/src/lib.rs @@ -59,9 +59,17 @@ struct Request { impl + FromStr> Request { fn start(resolver: T) -> Result { - let socket = UdpSocket::bind(SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 0))?; + let resolver_ip = resolver.into(); + let socket = UdpSocket::bind(SocketAddr::new( + if resolver_ip.is_ipv4() { + Ipv4Addr::UNSPECIFIED.into() + } else { + Ipv6Addr::UNSPECIFIED.into() + }, + 0, + ))?; socket.set_read_timeout(Some(Duration::from_millis(500)))?; - let endpoint = SocketAddr::new(resolver.into(), 53); + let endpoint = SocketAddr::new(resolver_ip, 53); let id = get_id()?; let mut buf = [0u8; 1500]; @@ -189,8 +197,7 @@ mod tests { let (v4, v6) = get_both(); println!("Done in {}ms", now.elapsed().as_millis()); println!("v4: {v4:?}, v6: {v6:?}"); - assert!(v4.is_some()); - assert!(v6.is_some()); + assert!(v4.is_some() || v6.is_some()); Ok(()) } }