Skip to content

Commit

Permalink
Added a function to determine if an interface is used for mobile data…
Browse files Browse the repository at this point in the history
… and prevent its address from being returned as the local ip address
  • Loading branch information
hzoltan01 committed Apr 6, 2024
1 parent 838aa0d commit e78768a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ pub fn local_ip() -> Result<IpAddr, Error> {

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
Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn local_ipv6() -> Result<IpAddr, Error> {

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
Expand Down
7 changes: 7 additions & 0 deletions src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<AfInetInfo>, Error> {
Expand Down

0 comments on commit e78768a

Please sign in to comment.