-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
// Copyright (c) 2018 Paweł Zmarzły | ||
// | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
extern crate igd; | ||
extern crate get_if_addrs; | ||
extern crate igd; | ||
|
||
pub mod port_forwarder; | ||
pub mod query_interfaces; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,36 @@ | ||
// Copyright (c) 2018 Paweł Zmarzły | ||
// | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
use get_if_addrs::{ get_if_addrs, IfAddr }; | ||
use get_if_addrs::{get_if_addrs, IfAddr}; | ||
|
||
use std::io; | ||
use std::net::Ipv4Addr; | ||
|
||
#[derive(Debug)] | ||
pub struct Interface { | ||
pub name: String, | ||
pub addr: Ipv4Addr | ||
pub addr: Ipv4Addr, | ||
} | ||
|
||
pub fn get_network_interfaces() -> Result<Vec<Interface>, io::Error> { | ||
get_if_addrs().map(|interfaces| | ||
interfaces.into_iter().filter_map(|interface| | ||
if let IfAddr::V4(ref addr) = interface.addr { | ||
if addr.is_loopback() { return None } | ||
Some(Interface { name: interface.name, addr: addr.ip }) | ||
} else { | ||
None | ||
} | ||
).collect() | ||
) | ||
get_if_addrs().map(|interfaces| { | ||
interfaces | ||
.into_iter() | ||
.filter_map(|interface| { | ||
if let IfAddr::V4(ref addr) = interface.addr { | ||
if addr.is_loopback() { | ||
return None; | ||
} | ||
Some(Interface { | ||
name: interface.name, | ||
addr: addr.ip, | ||
}) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect() | ||
}) | ||
} |