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(()) } }