Skip to content

Commit

Permalink
net adding set_fib call to set FIB route on FreeBSD.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Jul 31, 2023
1 parent b3df56a commit 548c3d8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
22 changes: 22 additions & 0 deletions library/std/src/os/unix/net/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,28 @@ impl UnixDatagram {
self.0.set_mark(mark)
}

/// Set the route FIB table id
///
/// The kernel allows up to 65536 distinct routes
/// (visible via net.fibs sysctl), this socket option
/// allows to set the id programmatically
#[cfg_attr(target_os = "freebsd", doc = "```no_run")]
#[cfg_attr(not(target_os = "freebsd"), doc = "```ignore")]
/// #![feature(unix_set_fib)]
/// use std::os::unix::net::UnixDatagram;
///
/// fn main() -> std::io::Result<()> {
/// let sock = UnixDatagram::unbound()?;
/// sock.set_fib(1)?;
/// Ok(())
/// }
/// ```
#[cfg(any(doc, target_os = "freebsd"))]
#[unstable(feature = "unix_set_fib", issue = "none")]
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
self.0.set_fib(fib)
}

/// Returns the value of the `SO_ERROR` option.
///
/// # Examples
Expand Down
22 changes: 22 additions & 0 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,28 @@ impl UnixStream {
self.0.set_mark(mark)
}

/// Set the route FIB table id
///
/// The kernel allows up to 65536 distinct routes
/// (visible via net.fibs sysctl), this socket option
/// allows to set the id programmatically
#[cfg_attr(target_os = "freebsd", doc = "```no_run")]
#[cfg_attr(not(target_os = "freebsd"), doc = "```ignore")]
/// #![feature(unix_set_fib)]
/// use std::os::unix::net::UnixStream;
///
/// fn main() -> std::io::Result<()> {
/// let sock = UnixStream::connect("/tmp/sock")?;
/// sock.set_fib(1)?;
/// Ok(())
/// }
/// ```
#[cfg(any(doc, target_os = "freebsd",))]
#[unstable(feature = "unix_set_fib", issue = "none")]
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
self.0.set_fib(fib)
}

/// Returns the value of the `SO_ERROR` option.
///
/// # Examples
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@ impl Socket {
setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int)
}

#[cfg(target_os = "freebsd")]
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
// Allows to bind the socket to special routing rules via ipfw
setsockopt(self, libc::SOL_SOCKET, libc::SO_SETFIB, fib as libc::c_int)
}

pub fn take_error(&self) -> io::Result<Option<io::Error>> {
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }
Expand Down

0 comments on commit 548c3d8

Please sign in to comment.