-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impl Display for InterfaceFlags (#2206)
And add a full example for getifaddrs.
- Loading branch information
Showing
3 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
impl Display for InterfaceFlags |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//! Print all interfaces and interface addresses on the system, in a format | ||
//! similar to ifconfig(8). | ||
#![cfg(feature = "net")] | ||
#[cfg(any(bsd, linux_android, target_os = "illumos"))] | ||
fn main() { | ||
use nix::ifaddrs::getifaddrs; | ||
use nix::sys::socket::{SockaddrLike, SockaddrStorage}; | ||
|
||
let addrs = getifaddrs().unwrap(); | ||
let mut ifname = None; | ||
for addr in addrs { | ||
if ifname.as_ref() != Some(&addr.interface_name) { | ||
if ifname.is_some() { | ||
println!(); | ||
} | ||
ifname = Some(addr.interface_name.clone()); | ||
println!( | ||
"{}: flags={:x}<{}>", | ||
addr.interface_name, | ||
addr.flags.bits(), | ||
addr.flags | ||
); | ||
} | ||
if let Some(dl) = addr.address.as_ref().unwrap().as_link_addr() { | ||
if dl.addr().is_none() { | ||
continue; | ||
} | ||
} | ||
let family = addr | ||
.address | ||
.as_ref() | ||
.and_then(SockaddrStorage::family) | ||
.map(|af| format!("{:?}", af)) | ||
.unwrap_or("".to_owned()); | ||
match ( | ||
&addr.address, | ||
&addr.netmask, | ||
&addr.broadcast, | ||
&addr.destination, | ||
) { | ||
(Some(a), Some(nm), Some(b), None) => { | ||
println!("\t{} {} netmask {} broadcast {}", family, a, nm, b) | ||
} | ||
(Some(a), Some(nm), None, None) => { | ||
println!("\t{} {} netmask {}", family, a, nm) | ||
} | ||
(Some(a), None, None, None) => println!("\t{} {}", family, a), | ||
(Some(a), None, None, Some(d)) => { | ||
println!("\t{} {} -> {}", family, a, d) | ||
} | ||
x => todo!("{:?}", x), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(any(bsd, linux_android, target_os = "illumos")))] | ||
fn main() {} |
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