From e78768abdcd98ebc730aca2279455eb5971c14ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hellmann=20Zolt=C3=A1n?= Date: Sat, 6 Apr 2024 15:41:31 +0200 Subject: [PATCH] Added a function to determine if an interface is used for mobile data and prevent its address from being returned as the local ip address --- src/lib.rs | 4 ++-- src/unix.rs | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2327e20..7f071c5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,7 +124,7 @@ pub fn local_ip() -> Result { ifas.into_iter() .find_map(|ifa| { - if !ifa.is_loopback && ifa.addr.is_ipv4() { + if !ifa.is_loopback && ifa.addr.is_ipv4() && !ifa.is_mobile_data() { Some(ifa.addr) } else { None @@ -196,7 +196,7 @@ pub fn local_ipv6() -> Result { ifas.into_iter() .find_map(|ifa| { - if !ifa.is_loopback && ifa.addr.is_ipv6() { + if !ifa.is_loopback && ifa.addr.is_ipv6() && !ifa.is_mobile_data() { Some(ifa.addr) } else { None diff --git a/src/unix.rs b/src/unix.rs index 109895c..4cc72ff 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -45,6 +45,13 @@ pub(crate) struct AfInetInfo { pub is_loopback: bool, } +impl AfInetInfo { + /// Determines if an interface is used for mobile_data + pub(crate) fn is_mobile_data(&self) -> bool { + self.iname.contains("rmnet_data") + } +} + // Internal method to list AF_INET info in a struct. This method is used by // list_afiinet_netifas and local_ip, pub(crate) fn list_afinet_netifas_info() -> Result, Error> {