tun: Initial work on getting/setting netmask
Seems to run into an issue with setting netmasks like 255.0.0.0 with an "AddressNotAvailable" error
This commit is contained in:
parent
c444bf293e
commit
6ea4b596c2
2 changed files with 36 additions and 0 deletions
|
|
@ -75,6 +75,16 @@ impl TunInterface {
|
|||
self.perform(|fd| unsafe { sys::if_set_mtu(fd, &iff) })?;
|
||||
}
|
||||
|
||||
#[throws]
|
||||
pub fn set_netmask(&self, addr: Ipv4Addr) {
|
||||
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
|
||||
|
||||
let mut iff = self.ifreq()?;
|
||||
iff.ifr_ifru.ifru_netmask = unsafe { *addr.as_ptr() };
|
||||
|
||||
self.perform(|fd| unsafe { sys::if_set_netmask(fd, &iff) })?;
|
||||
}
|
||||
|
||||
#[throws]
|
||||
pub fn ipv4_addr(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
|
|
@ -92,6 +102,17 @@ impl TunInterface {
|
|||
mtu
|
||||
}
|
||||
|
||||
#[throws]
|
||||
pub fn netmask(&self) -> Ipv4Addr {
|
||||
let mut iff = self.ifreq()?;
|
||||
self.perform(|fd| unsafe { sys::if_get_netmask(fd, &mut iff) })?;
|
||||
|
||||
let netmask =
|
||||
unsafe { *(&iff.ifr_ifru.ifru_netmask as *const _ as *const sys::sockaddr_in) };
|
||||
|
||||
Ipv4Addr::from(u32::from_be(netmask.sin_addr.s_addr))
|
||||
}
|
||||
|
||||
#[throws]
|
||||
fn perform<R>(&self, perform: impl FnOnce(RawFd) -> Result<R, nix::Error>) -> R {
|
||||
let socket = Socket::new(Domain::IPV4, Type::DGRAM, None)?;
|
||||
|
|
@ -101,6 +122,7 @@ impl TunInterface {
|
|||
|
||||
mod test {
|
||||
use super::TunInterface;
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
#[test]
|
||||
fn mtu() {
|
||||
|
|
@ -110,4 +132,15 @@ mod test {
|
|||
|
||||
assert_eq!(interf.mtu().unwrap(), 500);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn netmask() {
|
||||
let interf = TunInterface::new().unwrap();
|
||||
|
||||
let addr = Ipv4Addr::new(255, 0, 0, 0);
|
||||
|
||||
interf.set_netmask(addr);
|
||||
|
||||
assert_eq!(interf.netmask().unwrap(), addr);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue