Skip to content
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

Refactor and improve documentation for BSD support #87

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
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
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<h1 align="center">local-ip-address</h1>
<h4 align="center">
Retrieve system's local IP address and Network Interfaces/Adapters on
Linux, macOS and Windows.
Linux, Windows, and macOS (and other BSD-based systems).
</h4>
</div>

Expand Down Expand Up @@ -51,9 +51,23 @@ may differ based on the running operative system.
OS | Approach
--- | ---
Linux | Establishes a Netlink socket interchange to retrieve network interfaces
macOS | Uses of `getifaddrs` to retrieve network interfaces
BSD-based | Uses of `getifaddrs` to retrieve network interfaces
Windows | Consumes Win32 API's to retrieve the network adapters table

## Operating System Support

Current supported platforms include:
- Linux (requires at least [v0.1.0](https:/EstebanBorai/local-ip-address/releases/tag/v0.1.0));
- macOS (requires at least [v0.1.0](https:/EstebanBorai/local-ip-address/releases/tag/v0.1.0));
- Windows (requires at least [v0.3.0](https:/EstebanBorai/local-ip-address/releases/tag/v0.3.0));
- Other BSD-based (requires at least [v0.5.0](https:/EstebanBorai/local-ip-address/releases/tag/v0.5.0)); including:
- FreeBSD
- OpenBSD
- NetBSD
- DragonFly

Please note that we only test the BSD implementation of this on macOS and FreeBSD, under the assumption that other BSD-based systems will behave similarly. If you have any complications using this library on the other BSD-based, please create an [issue](https:/EstebanBorai/local-ip-address/issues).

## Release

In order to create a release you must push a Git tag as follows
Expand Down
File renamed without changes.
42 changes: 33 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Local IP Address

Retrieve system's local IP address and Network Interfaces/Adapters on
Linux, macOS and Windows.
Linux, Windows, and macOS (and other BSD-based systems).

## Usage

Expand Down Expand Up @@ -35,9 +35,15 @@ may differ based on the running operative system.
OS | Approach
--- | ---
Linux | Establishes a Netlink socket interchange to retrieve network interfaces
macOS | Uses of `getifaddrs` to retrieve network interfaces
BSD-based | Uses of `getifaddrs` to retrieve network interfaces
Windows | Consumes Win32 API's to retrieve the network adapters table

Supported BSD-based systems include:
- macOS
- FreeBSD
- OpenBSD
- NetBSD
- DragonFly
*/
use std::net::IpAddr;

Expand All @@ -50,10 +56,22 @@ pub mod linux;
#[cfg(target_os = "linux")]
pub use crate::linux::*;

#[cfg(any(target_os = "macos", target_os = "openbsd"))]
pub mod macos;
#[cfg(any(target_os = "macos", target_os = "openbsd"))]
pub use crate::macos::*;
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly"
))]
pub mod bsd;
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly"
))]
pub use crate::bsd::*;

#[cfg(target_family = "windows")]
pub mod windows;
Expand All @@ -68,7 +86,7 @@ pub use crate::windows::*;
/// For linux based systems the Netlink socket communication is used to
/// retrieve the local network interface.
///
/// For macOS systems the `getifaddrs` approach is taken using `libc`
/// For BSD-based systems the `getifaddrs` approach is taken using `libc`
///
/// For Windows systems Win32's IP Helper is used to gather the Local IP
/// address
Expand All @@ -78,11 +96,17 @@ pub fn local_ip() -> Result<IpAddr, Error> {
crate::linux::local_ip()
}

#[cfg(any(target_os = "macos", target_os = "openbsd"))]
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
target_os = "dragonfly"
))]
{
use std::env;

let ifas = crate::macos::list_afinet_netifas()?;
let ifas = crate::bsd::list_afinet_netifas()?;

if let Some((_, ipaddr)) = find_ifa(ifas, "en0") {
return Ok(ipaddr);
Expand Down