Skip to content

Commit a96b44c

Browse files
authored
Rollup merge of #96334 - devnexen:socket_mark, r=dtolnay
socket `set_mark` addition. to be able to set a marker/id on the socket for network filtering (iptables/ipfw here) purpose.
2 parents 52016a1 + f6efb0b commit a96b44c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

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

+25
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,31 @@ impl UnixDatagram {
838838
self.0.passcred()
839839
}
840840

841+
/// Set the id of the socket for network filtering purpose
842+
///
843+
#[cfg_attr(
844+
any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"),
845+
doc = "```no_run"
846+
)]
847+
#[cfg_attr(
848+
not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")),
849+
doc = "```ignore"
850+
)]
851+
/// #![feature(unix_set_mark)]
852+
/// use std::os::unix::net::UnixDatagram;
853+
///
854+
/// fn main() -> std::io::Result<()> {
855+
/// let sock = UnixDatagram::unbound()?;
856+
/// sock.set_mark(32)?;
857+
/// Ok(())
858+
/// }
859+
/// ```
860+
#[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
861+
#[unstable(feature = "unix_set_mark", issue = "96467")]
862+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
863+
self.0.set_mark(mark)
864+
}
865+
841866
/// Returns the value of the `SO_ERROR` option.
842867
///
843868
/// # Examples

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

+25
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,31 @@ impl UnixStream {
427427
self.0.passcred()
428428
}
429429

430+
/// Set the id of the socket for network filtering purpose
431+
///
432+
#[cfg_attr(
433+
any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"),
434+
doc = "```no_run"
435+
)]
436+
#[cfg_attr(
437+
not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")),
438+
doc = "```ignore"
439+
)]
440+
/// #![feature(unix_set_mark)]
441+
/// use std::os::unix::net::UnixStream;
442+
///
443+
/// fn main() -> std::io::Result<()> {
444+
/// let sock = UnixStream::connect("/tmp/sock")?;
445+
/// sock.set_mark(32)?;
446+
/// Ok(())
447+
/// }
448+
/// ```
449+
#[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
450+
#[unstable(feature = "unix_set_mark", issue = "96467")]
451+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
452+
self.0.set_mark(mark)
453+
}
454+
430455
/// Returns the value of the `SO_ERROR` option.
431456
///
432457
/// # Examples

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

+11
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,17 @@ impl Socket {
438438
self.0.set_nonblocking(nonblocking)
439439
}
440440

441+
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
442+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
443+
#[cfg(target_os = "linux")]
444+
let option = libc::SO_MARK;
445+
#[cfg(target_os = "freebsd")]
446+
let option = libc::SO_USER_COOKIE;
447+
#[cfg(target_os = "openbsd")]
448+
let option = libc::SO_RTABLE;
449+
setsockopt(self, libc::SOL_SOCKET, option, mark as libc::c_int)
450+
}
451+
441452
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
442453
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
443454
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }

0 commit comments

Comments
 (0)