Skip to content

Commit

Permalink
fix build error (linux/arm/whatever) (#30)
Browse files Browse the repository at this point in the history
* implement EUI-48 -> EUI-64 (modified) myself: instead of relying on to_modified_eui64 from advmac, because it doesn't compile on arm
* switch from mac_address2 to mac_address due to a bug returning loopback mac on non-windows platforms
* make ipv6 selection more readable in code
  • Loading branch information
masterflitzer authored May 18, 2023
1 parent 1d359a5 commit 44fb26c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 94 deletions.
107 changes: 31 additions & 76 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
advmac = "1.0.3"
clap = { version = "4.2.7", features = ["derive"] }
directories = "5.0.1"
local-ip-address = "0.5.3"
mac_address2 = "2.0.0"
mac_address = "1.1.4"
reqwest = { version = "0.11.17", default-features = false, features = [
"json",
"rustls-tls",
Expand Down
41 changes: 25 additions & 16 deletions src/ip.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::structs::{config::Config, Ipify};
use advmac::MacAddr6;
use local_ip_address::list_afinet_netifas;
use mac_address2::get_mac_address;
use mac_address::get_mac_address;
use reqwest::Client as HttpClient;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

Expand Down Expand Up @@ -74,9 +73,7 @@ pub(crate) async fn determine_ipv6(config: &Config) -> Option<Ipv6Addr> {

if config.ipv6.prefer_eui64 {
let mac = get_mac_address().ok()?;
let eui48 = MacAddr6::new(mac?.bytes());
let eui64 = eui48.to_modified_eui64();
let suffix = eui64.to_array();
let suffix = eui48_to_modified_eui64(&mac?.bytes())?;
if let Some(x) = ipv6_addresses.iter().find(|ip| match split_ipv6(ip) {
Some((_, s)) => s == suffix,
None => false,
Expand All @@ -85,22 +82,34 @@ pub(crate) async fn determine_ipv6(config: &Config) -> Option<Ipv6Addr> {
};
}

if !config.ipv6.prefer_outgoing {
return ipv6_addresses
.into_iter()
.filter(|ip| ip != &ipv6)
.collect::<Vec<_>>()
.first()
.cloned();
if config.ipv6.prefer_outgoing {
return Some(ipv6);
}

Some(ipv6)
return ipv6_addresses
.into_iter()
.filter(|ip| ip != &ipv6)
.collect::<Vec<_>>()
.first()
.cloned();
}

fn split_ipv6(ipv6: &Ipv6Addr) -> Option<([u8; 8], [u8; 8])> {
let octets = ipv6.octets();
let (octets_prefix, octets_suffix) = octets.split_at(8);
let prefix: [u8; 8] = octets_prefix.try_into().ok()?;
let suffix: [u8; 8] = octets_suffix.try_into().ok()?;
let (p, s) = octets.split_at(8);
let prefix: [u8; 8] = p.try_into().ok()?;
let suffix: [u8; 8] = s.try_into().ok()?;
Some((prefix, suffix))
}

fn eui48_to_modified_eui64(eui48: &[u8; 6]) -> Option<[u8; 8]> {
let (p, s) = eui48.split_at(3);
let prefix: [u8; 3] = p.try_into().ok()?;
let suffix: [u8; 3] = s.try_into().ok()?;
let eui64 = [
prefix[0], prefix[1], prefix[2], 0xff, 0xfe, suffix[0], suffix[1], suffix[2],
];
let mut modified_eui64 = eui64;
modified_eui64[0] ^= 0b0000_0010;
Some(modified_eui64)
}

0 comments on commit 44fb26c

Please sign in to comment.