Skip to content

fix: wrong IP address on iOS #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ pub fn local_ip() -> Result<IpAddr, Error> {
target_os = "netbsd",
target_os = "dragonfly",
target_os = "macos",
target_os = "android",
target_os = "ios",
target_os = "android"
))]
{
let ifas = crate::unix::list_afinet_netifas_info()?;
Expand All @@ -133,6 +132,23 @@ pub fn local_ip() -> Result<IpAddr, Error> {
.ok_or(Error::LocalIpAddressNotFound)
}

#[cfg(any(
target_os = "ios"
))]
Comment on lines +135 to +137
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be condensed into:

Suggested change
#[cfg(any(
target_os = "ios"
))]
#[cfg(target_os = "ios")]

{
let ifas = crate::unix::list_afinet_netifas_info()?;

ifas.into_iter()
.find_map(|ifa| {
if !ifa.is_loopback && ifa.addr.is_ipv4() && ifa.iname == "en0" {
Some(ifa.addr)
} else {
None
}
})
.ok_or(Error::LocalIpAddressNotFound)
}

#[cfg(target_os = "windows")]
{
use windows_sys::Win32::Networking::WinSock::AF_INET;
Expand Down Expand Up @@ -188,8 +204,7 @@ pub fn local_ipv6() -> Result<IpAddr, Error> {
target_os = "netbsd",
target_os = "dragonfly",
target_os = "macos",
target_os = "android",
target_os = "ios",
target_os = "android"
))]
{
let ifas = crate::unix::list_afinet_netifas_info()?;
Expand All @@ -205,6 +220,23 @@ pub fn local_ipv6() -> Result<IpAddr, Error> {
.ok_or(Error::LocalIpAddressNotFound)
}

#[cfg(any(
target_os = "ios"
))]
Comment on lines +223 to +225
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be condensed into:

Suggested change
#[cfg(any(
target_os = "ios"
))]
#[cfg(target_os = "ios")]

{
let ifas = crate::unix::list_afinet_netifas_info()?;

ifas.into_iter()
.find_map(|ifa| {
if !ifa.is_loopback && ifa.addr.is_ipv6() && ifa.iname == "en0" {
Some(ifa.addr)
} else {
None
}
})
.ok_or(Error::LocalIpAddressNotFound)
}

#[cfg(target_os = "windows")]
{
use windows_sys::Win32::Networking::WinSock::AF_INET6;
Expand Down