Skip to content

Commit bf817bf

Browse files
committed
net adding set_fib call to set FIB route on FreeBSD.
1 parent c6c4abf commit bf817bf

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

library/std/src/os/unix/net/datagram.rs

+22
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,28 @@ impl UnixDatagram {
890890
self.0.set_mark(mark)
891891
}
892892

893+
/// Set the route FIB table id
894+
///
895+
/// The kernel allows up to 65536 distinct routes
896+
/// (visible via net.fibs sysctl), this socket option
897+
/// allows to set the id programmatically
898+
#[cfg_attr(target_os = "freebsd", doc = "```no_run")]
899+
#[cfg_attr(not(target_os = "freebsd"), doc = "```ignore")]
900+
/// #![feature(unix_set_fib)]
901+
/// use std::os::unix::net::UnixDatagram;
902+
///
903+
/// fn main() -> std::io::Result<()> {
904+
/// let sock = UnixDatagram::unbound()?;
905+
/// sock.set_fib(1)?;
906+
/// Ok(())
907+
/// }
908+
/// ```
909+
#[cfg(any(doc, target_os = "freebsd"))]
910+
#[unstable(feature = "unix_set_fib", issue = "none")]
911+
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
912+
self.0.set_fib(fib)
913+
}
914+
893915
/// Returns the value of the `SO_ERROR` option.
894916
///
895917
/// # Examples

library/std/src/os/unix/net/stream.rs

+22
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,28 @@ impl UnixStream {
482482
self.0.set_mark(mark)
483483
}
484484

485+
/// Set the route FIB table id
486+
///
487+
/// The kernel allows up to 65536 distinct routes
488+
/// (visible via net.fibs sysctl), this socket option
489+
/// allows to set the id programmatically
490+
#[cfg_attr(target_os = "freebsd", doc = "```no_run")]
491+
#[cfg_attr(not(target_os = "freebsd"), doc = "```ignore")]
492+
/// #![feature(unix_set_fib)]
493+
/// use std::os::unix::net::UnixStream;
494+
///
495+
/// fn main() -> std::io::Result<()> {
496+
/// let sock = UnixStream::connect("/tmp/sock")?;
497+
/// sock.set_fib(1)?;
498+
/// Ok(())
499+
/// }
500+
/// ```
501+
#[cfg(any(doc, target_os = "freebsd",))]
502+
#[unstable(feature = "unix_set_fib", issue = "none")]
503+
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
504+
self.0.set_fib(fib)
505+
}
506+
485507
/// Returns the value of the `SO_ERROR` option.
486508
///
487509
/// # Examples

library/std/src/sys/pal/unix/net.rs

+6
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ impl Socket {
504504
setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int)
505505
}
506506

507+
#[cfg(target_os = "freebsd")]
508+
pub fn set_fib(&self, fib: i32) -> io::Result<()> {
509+
// Allows to bind the socket to special routing rules via ipfw
510+
setsockopt(self, libc::SOL_SOCKET, libc::SO_SETFIB, fib as libc::c_int)
511+
}
512+
507513
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
508514
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
509515
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }

0 commit comments

Comments
 (0)