tun: Enable setting/getting of MTU

Works similarly to getting and setting IP addresses, can pretty much be
copy-pasted for the rest of the settings.
This commit is contained in:
Cara Salter 2023-04-29 10:39:34 -04:00 committed by Conrad Kramer
parent 4b0965b846
commit a13b2243e6
2 changed files with 19 additions and 0 deletions

View file

@ -68,6 +68,13 @@ impl TunInterface {
self.perform(|fd| unsafe { sys::if_set_addr(fd, &iff) })?;
}
#[throws]
pub fn set_mtu(&self, mtu: i32) {
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_mtu = mtu;
self.perform(|fd| unsafe { sys::if_set_mtu(fd, &iff) })?;
}
#[throws]
pub fn ipv4_addr(&self) -> Ipv4Addr {
let mut iff = self.ifreq()?;
@ -76,6 +83,15 @@ impl TunInterface {
Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr))
}
#[throws]
pub fn mtu(&self) -> i32 {
let mut iff = self.ifreq()?;
self.perform(|fd| unsafe { sys::if_get_mtu(fd, &mut iff) })?;
let mtu = unsafe { iff.ifr_ifru.ifru_mtu };
mtu
}
#[throws]
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;

View file

@ -16,4 +16,7 @@ ioctl_read_bad!(
);
ioctl_read_bad!(if_get_index, libc::SIOCGIFINDEX, libc::ifreq);
ioctl_read_bad!(if_get_addr, libc::SIOCGIFADDR, libc::ifreq);
ioctl_read_bad!(if_get_mtu, libc::SIOCGIFMTU, libc::ifreq);
ioctl_write_ptr_bad!(if_set_addr, libc::SIOCSIFADDR, libc::ifreq);
ioctl_write_ptr_bad!(if_set_mtu, libc::SIOCSIFMTU, libc::ifreq);