in src/linux.rs [308:348]
fn if_name_mtu(if_index: i32, fd: &mut RouteSocket) -> Result<(String, usize)> {
// Send RTM_GETLINK message to get interface information for the given interface index.
let msg_seq = RouteSocket::new_seq();
let msg = IfInfoMsg::new(if_index, msg_seq);
fd.write_all((&msg).into())?;
// Receive RTM_GETLINK response.
let (_hdr, mut buf) = read_msg_with_seq(fd, msg_seq, RTM_NEWLINK)?;
debug_assert!(size_of::<ifinfomsg>() <= buf.len());
let buf = buf.split_off(size_of::<ifinfomsg>());
// Parse through the attributes to find the interface name and MTU.
let mut ifname = None;
let mut mtu = None;
for attr in RtAttrs(buf.as_slice()).by_ref() {
match attr.hdr.rta_type {
IFLA_IFNAME => {
let name = CStr::from_bytes_until_nul(attr.msg)
.map_err(|err| Error::new(ErrorKind::Other, err))?;
ifname = Some(
name.to_str()
.map_err(|err| Error::new(ErrorKind::Other, err))?
.to_string(),
);
}
IFLA_MTU => {
mtu = Some(
parse_c_int(attr.msg)?
.try_into()
.map_err(|e: TryFromIntError| unlikely_err(e.to_string()))?,
);
}
_ => (),
}
if let (Some(ifname), Some(mtu)) = (ifname.as_ref(), mtu.as_ref()) {
return Ok((ifname.clone(), *mtu));
}
}
Err(default_err())
}