Skip to content

net adding set_fib call to set FIB route on FreeBSD. #101213

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

Closed
wants to merge 1 commit into from
Closed
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 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/pal/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,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