publicip: fix IPv4 resolution on macOS (#275)

pull/281/head
Jake McGinty 2023-07-10 02:09:32 -05:00 committed by GitHub
parent dc7ae0e15c
commit 5eaad112b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 4 deletions

View File

@ -59,9 +59,17 @@ struct Request<T> {
impl<T: Into<IpAddr> + FromStr<Err = AddrParseError>> Request<T> { impl<T: Into<IpAddr> + FromStr<Err = AddrParseError>> Request<T> {
fn start(resolver: T) -> Result<Self, Error> { fn start(resolver: T) -> Result<Self, Error> {
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)))?; 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 id = get_id()?;
let mut buf = [0u8; 1500]; let mut buf = [0u8; 1500];
@ -189,8 +197,7 @@ mod tests {
let (v4, v6) = get_both(); let (v4, v6) = get_both();
println!("Done in {}ms", now.elapsed().as_millis()); println!("Done in {}ms", now.elapsed().as_millis());
println!("v4: {v4:?}, v6: {v6:?}"); println!("v4: {v4:?}, v6: {v6:?}");
assert!(v4.is_some()); assert!(v4.is_some() || v6.is_some());
assert!(v6.is_some());
Ok(()) Ok(())
} }
} }