Skip to content

Commit

Permalink
Nix 0.27 (#41)
Browse files Browse the repository at this point in the history
* Update to nix 0.26

* some clippy fixes

* use latest version of `nix`

---------

Co-authored-by: Jelmer Vernooij <jelmer@jelmer.uk>
  • Loading branch information
repnop and jelmer authored Nov 14, 2023
1 parent a32d68d commit f2416eb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["mac", "address", "network", "interface"]
serde = { version = "1.0.117", features = ["derive"], optional = true }

[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "freebsd", target_os = "openbsd", target_os = "android"))'.dependencies]
nix = "0.23.1"
nix = { version = "0.27", features = ["net"] }

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winerror", "ws2def", "iphlpapi"] }
Expand Down
4 changes: 2 additions & 2 deletions examples/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ fn main() {
#[cfg(any(target_os = "linux", target_os = "macos"))]
let name = "eth0";

#[cfg(any(target_os = "freebsd"))]
#[cfg(target_os = "freebsd")]
let name = "em0";

#[cfg(any(target_os = "openbsd"))]
#[cfg(target_os = "openbsd")]
let name = "fxp0";

#[cfg(target_os = "windows")]
Expand Down
10 changes: 4 additions & 6 deletions src/iter/linux.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{MacAddress, MacAddressError};
use nix::{ifaddrs, sys::socket::SockAddr};
use nix::ifaddrs;

/// An iterator over all available MAC addresses on the system.
pub struct MacAddressIterator {
Expand All @@ -19,11 +19,9 @@ impl MacAddressIterator {
}

fn filter_macs(intf: ifaddrs::InterfaceAddress) -> Option<MacAddress> {
if let SockAddr::Link(link) = intf.address? {
Some(MacAddress::new(link.addr()))
} else {
None
}
intf.address?
.as_link_addr()
.and_then(|link| link.addr().map(MacAddress::new))
}

impl Iterator for MacAddressIterator {
Expand Down
32 changes: 19 additions & 13 deletions src/linux.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]

use crate::MacAddressError;
use nix::{ifaddrs::*, sys::socket::SockAddr};
use nix::ifaddrs::*;

/// Uses the `getifaddrs` call to retrieve a list of network interfaces on the
/// host device and returns the first MAC address listed that isn't
Expand All @@ -10,15 +10,19 @@ pub fn get_mac(name: Option<&str>) -> Result<Option<[u8; 6]>, MacAddressError> {
let ifiter = getifaddrs()?;

for interface in ifiter {
if let Some(SockAddr::Link(link)) = interface.address {
let bytes = link.addr();

if let Some(name) = name {
if interface.interface_name == name {
return Ok(Some(bytes));
if let Some(iface_address) = interface.address {
if let Some(link) = iface_address.as_link_addr() {
let bytes = link.addr();

if let Some(name) = name {
if interface.interface_name == name {
return Ok(bytes);
}
} else if let Some(bytes) = bytes {
if bytes.iter().any(|&x| x != 0) {
return Ok(Some(bytes));
}
}
} else if bytes.iter().any(|&x| x != 0) {
return Ok(Some(bytes));
}
}
}
Expand All @@ -30,11 +34,13 @@ pub fn get_ifname(mac: &[u8; 6]) -> Result<Option<String>, MacAddressError> {
let ifiter = getifaddrs()?;

for interface in ifiter {
if let Some(SockAddr::Link(link)) = interface.address {
let bytes = link.addr();
if let Some(iface_address) = interface.address {
if let Some(link) = iface_address.as_link_addr() {
let bytes = link.addr();

if &bytes == mac {
return Ok(Some(interface.interface_name));
if bytes == Some(*mac) {
return Ok(Some(interface.interface_name));
}
}
}
}
Expand Down

0 comments on commit f2416eb

Please sign in to comment.