From 65d94287a30114e32aada3a6453578446ec9671b Mon Sep 17 00:00:00 2001 From: Negezor Date: Mon, 1 Apr 2024 00:08:54 +1100 Subject: [PATCH 1/5] chore(deps): replace pnet by pnet_datalink --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 45c9f0a..f186931 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ edition = "2021" [dependencies] chrono = "0.4" thiserror = "1.0" -pnet = "0.33" +pnet_datalink = "0.34" [dev-dependencies] bencher = "0.1" From 15d720892b273d7f3fb7c56e9d3cfca54ab7511a Mon Sep 17 00:00:00 2001 From: Negezor Date: Mon, 1 Apr 2024 00:09:15 +1100 Subject: [PATCH 2/5] refactor: use pnet_datalink directly --- src/builder.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index a5c6623..2e96501 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,5 +1,4 @@ use chrono::prelude::*; -use pnet::datalink; use std::{ net::{IpAddr, Ipv4Addr}, sync::{Arc, Mutex}, @@ -100,7 +99,7 @@ impl<'a> Builder<'a> { } fn private_ipv4() -> Option { - datalink::interfaces() + pnet_datalink::interfaces() .iter() .filter(|interface| interface.is_up() && !interface.is_loopback()) .map(|interface| { From fc37b39b91b2a45783c920fe686a05dc3f5089eb Mon Sep 17 00:00:00 2001 From: Negezor Date: Mon, 1 Apr 2024 00:29:06 +1100 Subject: [PATCH 3/5] chore(deps): disable default features for pnet_datalink --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index f186931..3bc0675 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ edition = "2021" [dependencies] chrono = "0.4" thiserror = "1.0" -pnet_datalink = "0.34" +pnet_datalink = { version = "0.34", default-features = false } [dev-dependencies] bencher = "0.1" From 1c3320a559755f845ac5a67df9c0b5ad91aec6a4 Mon Sep 17 00:00:00 2001 From: Negezor Date: Mon, 1 Apr 2024 00:39:12 +1100 Subject: [PATCH 4/5] refactor: simplify get private_ipv4 --- src/builder.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 2e96501..3094e76 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -102,25 +102,15 @@ fn private_ipv4() -> Option { pnet_datalink::interfaces() .iter() .filter(|interface| interface.is_up() && !interface.is_loopback()) - .map(|interface| { - interface - .ips - .iter() - .map(|ip_addr| ip_addr.ip()) // convert to std - .find(|ip_addr| match ip_addr { - IpAddr::V4(ipv4) => is_private_ipv4(*ipv4), - IpAddr::V6(_) => false, - }) - .and_then(|ip_addr| match ip_addr { - IpAddr::V4(ipv4) => Some(ipv4), // make sure the return type is Ipv4Addr - _ => None, - }) + .flat_map(|interface| interface.ips.iter()) + .filter_map(|network| match network.ip() { + IpAddr::V4(ipv4) => Some(ipv4), + IpAddr::V6(_) => None, }) - .find(|ip| ip.is_some()) - .flatten() + .find(is_private_ipv4) } -fn is_private_ipv4(ip: Ipv4Addr) -> bool { +fn is_private_ipv4(ip: &Ipv4Addr) -> bool { let octets = ip.octets(); octets[0] == 10 || octets[0] == 172 && (octets[1] >= 16 && octets[1] < 32) From 0c8704f723230aedbf03f065a8a10e9e478ebaf0 Mon Sep 17 00:00:00 2001 From: Negezor Date: Tue, 4 Jun 2024 00:54:20 +1100 Subject: [PATCH 5/5] chore(deps): bump pnet_datalink to 0.35 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 3bc0675..d21b639 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ edition = "2021" [dependencies] chrono = "0.4" thiserror = "1.0" -pnet_datalink = { version = "0.34", default-features = false } +pnet_datalink = { version = "0.35", default-features = false } [dev-dependencies] bencher = "0.1"