Struct interfaces::Interface
source · pub struct Interface {
pub name: String,
pub addresses: Vec<Address>,
pub flags: InterfaceFlags,
/* private fields */
}
Expand description
The Interface
structure represents a single interface on the system. It also contains
methods to control the interface.
Fields§
§name: String
The name of this interface.
addresses: Vec<Address>
All addresses for this interface.
flags: InterfaceFlags
Interface flags.
NOTE: The underlying API returns this value for each address of an interface, not each interface itself. We assume that they are all equal and take the first set of flags (from the first address).
Implementations§
source§impl Interface
impl Interface
sourcepub fn get_all() -> Result<Vec<Interface>>
pub fn get_all() -> Result<Vec<Interface>>
Retrieve a list of all interfaces on this system.
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn main() {
let mut ifs = Interface::get_all().expect("could not get interfaces");
ifs.sort_by(|a, b| a.name.cmp(&b.name));
// Find the maximum alignment for our interface names.
let max_align = ifs.iter().map(|i| i.name.len() + 2).max().unwrap();
let full_align = iter::repeat(' ').take(max_align).collect::<String>();
for i in ifs.iter() {
let name_align = iter::repeat(' ')
.take(max_align - i.name.len() - 2)
.collect::<String>();
// Build the first line by printing the interface flags.
let first_line = {
let mut buf = String::new();
buf.push_str(&*format!("flags={} <", i.flags.bits()));
let mut flag_strs = vec![];
for &(f, s) in NAME_MAPPINGS.iter() {
if i.flags.contains(f) {
flag_strs.push(s);
}
}
buf.push_str(&*flag_strs.join(","));
buf.push_str(&*format!("> mtu {}", i.get_mtu().unwrap_or(0)));
buf
};
println!("{}: {}{}", i.name, name_align, first_line);
if i.flags.contains(InterfaceFlags::IFF_LOOPBACK) {
println!("{}loop (Local Loopback)", full_align);
} else {
if let Ok(addr) = i.hardware_addr() {
println!("{}ether {}", full_align, addr);
}
}
for addr in i.addresses.iter() {
let raddr = match addr.addr {
Some(a) => a,
None => continue,
};
let prefix = match addr.kind {
Kind::Ipv4 => "inet",
Kind::Ipv6 => "inet6",
_ => continue,
};
println!("{}{} {}", full_align, prefix, format_addr(&raddr));
}
}
}
sourcepub fn get_by_name(name: &str) -> Result<Option<Interface>>
pub fn get_by_name(name: &str) -> Result<Option<Interface>>
Returns an Interface
instance representing the interface with the given name. Will
return Ok(Some(Interface))
on success, Ok(None)
if there is no such interface, and
Err(..)
on failure.
let iface = Interface::get_by_name("lo")?;
if let Some(ref lo) = iface {
assert!(lo.is_loopback());
} else {
println!("Could not find loopback interface");
}
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let args = env::args().collect::<Vec<_>>();
if args.len() != 3 {
usage();
}
let new_status = match &*args[1] {
"up" => true,
"down" => false,
_ => usage(),
};
let ifname = &args[2];
let mut i = match Interface::get_by_name(ifname) {
Ok(Some(i)) => i,
Ok(None) => {
println!("Could not find an interface named: {}", ifname);
return;
}
Err(e) => {
println!("An error occured fetching interfaces: {:?}", e);
return;
}
};
println!(
"Interface {} was {}",
i.name,
if i.is_up() { "up" } else { "down" }
);
match i.set_up(new_status) {
Ok(_) => {
println!("Successfully set interface status");
println!(
"Interface is now {}",
if new_status { "up" } else { "down" }
);
}
Err(e) => {
println!("Could not set interface status: {:?}", e);
}
};
}
sourcepub fn is_up(&self) -> bool
pub fn is_up(&self) -> bool
Returns whether this interface is up.
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let args = env::args().collect::<Vec<_>>();
if args.len() != 3 {
usage();
}
let new_status = match &*args[1] {
"up" => true,
"down" => false,
_ => usage(),
};
let ifname = &args[2];
let mut i = match Interface::get_by_name(ifname) {
Ok(Some(i)) => i,
Ok(None) => {
println!("Could not find an interface named: {}", ifname);
return;
}
Err(e) => {
println!("An error occured fetching interfaces: {:?}", e);
return;
}
};
println!(
"Interface {} was {}",
i.name,
if i.is_up() { "up" } else { "down" }
);
match i.set_up(new_status) {
Ok(_) => {
println!("Successfully set interface status");
println!(
"Interface is now {}",
if new_status { "up" } else { "down" }
);
}
Err(e) => {
println!("Could not set interface status: {:?}", e);
}
};
}
sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
Returns whether this interface is ready for transfer.
sourcepub fn is_loopback(&self) -> bool
pub fn is_loopback(&self) -> bool
Returns whether this interface is a loopback address.
sourcepub fn hardware_addr(&self) -> Result<HardwareAddr>
pub fn hardware_addr(&self) -> Result<HardwareAddr>
Retrieves the hardware address of this interface.
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn main() {
let mut ifs = Interface::get_all().expect("could not get interfaces");
ifs.sort_by(|a, b| a.name.cmp(&b.name));
// Find the maximum alignment for our interface names.
let max_align = ifs.iter().map(|i| i.name.len() + 2).max().unwrap();
let full_align = iter::repeat(' ').take(max_align).collect::<String>();
for i in ifs.iter() {
let name_align = iter::repeat(' ')
.take(max_align - i.name.len() - 2)
.collect::<String>();
// Build the first line by printing the interface flags.
let first_line = {
let mut buf = String::new();
buf.push_str(&*format!("flags={} <", i.flags.bits()));
let mut flag_strs = vec![];
for &(f, s) in NAME_MAPPINGS.iter() {
if i.flags.contains(f) {
flag_strs.push(s);
}
}
buf.push_str(&*flag_strs.join(","));
buf.push_str(&*format!("> mtu {}", i.get_mtu().unwrap_or(0)));
buf
};
println!("{}: {}{}", i.name, name_align, first_line);
if i.flags.contains(InterfaceFlags::IFF_LOOPBACK) {
println!("{}loop (Local Loopback)", full_align);
} else {
if let Ok(addr) = i.hardware_addr() {
println!("{}ether {}", full_align, addr);
}
}
for addr in i.addresses.iter() {
let raddr = match addr.addr {
Some(a) => a,
None => continue,
};
let prefix = match addr.kind {
Kind::Ipv4 => "inet",
Kind::Ipv6 => "inet6",
_ => continue,
};
println!("{}{} {}", full_align, prefix, format_addr(&raddr));
}
}
}
sourcepub fn set_up(&mut self, up: bool) -> Result<()>
pub fn set_up(&mut self, up: bool) -> Result<()>
Sets the interface as up or down. This will change the status of the given interface in
the system, and update the flags of this Interface
instance.
Examples found in repository?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() {
let args = env::args().collect::<Vec<_>>();
if args.len() != 3 {
usage();
}
let new_status = match &*args[1] {
"up" => true,
"down" => false,
_ => usage(),
};
let ifname = &args[2];
let mut i = match Interface::get_by_name(ifname) {
Ok(Some(i)) => i,
Ok(None) => {
println!("Could not find an interface named: {}", ifname);
return;
}
Err(e) => {
println!("An error occured fetching interfaces: {:?}", e);
return;
}
};
println!(
"Interface {} was {}",
i.name,
if i.is_up() { "up" } else { "down" }
);
match i.set_up(new_status) {
Ok(_) => {
println!("Successfully set interface status");
println!(
"Interface is now {}",
if new_status { "up" } else { "down" }
);
}
Err(e) => {
println!("Could not set interface status: {:?}", e);
}
};
}
sourcepub fn get_mtu(&self) -> Result<u32>
pub fn get_mtu(&self) -> Result<u32>
Retrieve the MTU of this interface.
Examples found in repository?
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
fn main() {
let mut ifs = Interface::get_all().expect("could not get interfaces");
ifs.sort_by(|a, b| a.name.cmp(&b.name));
// Find the maximum alignment for our interface names.
let max_align = ifs.iter().map(|i| i.name.len() + 2).max().unwrap();
let full_align = iter::repeat(' ').take(max_align).collect::<String>();
for i in ifs.iter() {
let name_align = iter::repeat(' ')
.take(max_align - i.name.len() - 2)
.collect::<String>();
// Build the first line by printing the interface flags.
let first_line = {
let mut buf = String::new();
buf.push_str(&*format!("flags={} <", i.flags.bits()));
let mut flag_strs = vec![];
for &(f, s) in NAME_MAPPINGS.iter() {
if i.flags.contains(f) {
flag_strs.push(s);
}
}
buf.push_str(&*flag_strs.join(","));
buf.push_str(&*format!("> mtu {}", i.get_mtu().unwrap_or(0)));
buf
};
println!("{}: {}{}", i.name, name_align, first_line);
if i.flags.contains(InterfaceFlags::IFF_LOOPBACK) {
println!("{}loop (Local Loopback)", full_align);
} else {
if let Ok(addr) = i.hardware_addr() {
println!("{}ether {}", full_align, addr);
}
}
for addr in i.addresses.iter() {
let raddr = match addr.addr {
Some(a) => a,
None => continue,
};
let prefix = match addr.kind {
Kind::Ipv4 => "inet",
Kind::Ipv6 => "inet6",
_ => continue,
};
println!("{}{} {}", full_align, prefix, format_addr(&raddr));
}
}
}