Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set/get broadcast address in TunInterface #138

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tun/src/unix/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ impl TunInterface {
Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr))
}

#[throws]
pub fn set_broadcast_addr(&self, addr: Ipv4Addr) {
let addr = SockAddr::from(SocketAddrV4::new(addr, 0));
let mut iff = self.ifreq()?;
iff.ifr_ifru.ifru_broadaddr = unsafe { *addr.as_ptr() };
self.perform(|fd| unsafe { sys::if_set_brdaddr(fd, &iff) })?;
info!(
"broadcast_addr_set: {:?} (fd: {:?})",
addr,
self.as_raw_fd()
)
}

#[throws]
pub fn broadcast_addr(&self) -> Ipv4Addr {
let mut iff = self.ifreq()?;
self.perform(|fd| unsafe { sys::if_get_brdaddr(fd, &mut iff) })?;
let addr =
unsafe { *(&iff.ifr_ifru.ifru_broadaddr as *const _ as *const sys::sockaddr_in) };
Ipv4Addr::from(u32::from_be(addr.sin_addr.s_addr))
}

#[throws]
pub fn set_ipv6_addr(&self, addr: Ipv6Addr) {
let mut iff = self.in6_ifreq()?;
Expand Down
2 changes: 2 additions & 0 deletions tun/src/unix/linux/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ 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_brdaddr, libc::SIOCGIFBRDADDR, libc::ifreq);
ioctl_read_bad!(if_get_mtu, libc::SIOCGIFMTU, libc::ifreq);
ioctl_read_bad!(if_get_netmask, libc::SIOCGIFNETMASK, libc::ifreq);

ioctl_write_ptr_bad!(if_set_addr, libc::SIOCSIFADDR, libc::ifreq);
ioctl_write_ptr_bad!(if_set_addr6, libc::SIOCSIFADDR, libc::in6_ifreq);
ioctl_write_ptr_bad!(if_set_brdaddr, libc::SIOCSIFBRDADDR, libc::ifreq);
ioctl_write_ptr_bad!(if_set_mtu, libc::SIOCSIFMTU, libc::ifreq);
ioctl_write_ptr_bad!(if_set_netmask, libc::SIOCSIFNETMASK, libc::ifreq);
15 changes: 15 additions & 0 deletions tun/tests/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ fn test_create() {
TunInterface::new()?;
}

#[test]
#[throws]
#[cfg(not(any(target_os = "windows", target_vendor = "apple")))]
fn test_set_get_broadcast_addr() {
let tun = TunInterface::new()?;
let addr = Ipv4Addr::new(10, 0, 0, 1);
tun.set_ipv4_addr(addr)?;

let broadcast_addr = Ipv4Addr::new(255, 255, 255, 0);
tun.set_broadcast_addr(broadcast_addr)?;
let result = tun.broadcast_addr()?;

assert_eq!(broadcast_addr, result);
}

#[test]
#[throws]
#[cfg(not(target_os = "windows"))]
Expand Down
Loading