Skip to content

Commit 7eaf0c9

Browse files
authored
Rollup merge of rust-lang#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 91a3ce8 + 10f5a19 commit 7eaf0c9

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

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

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

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

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

+18
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,24 @@ impl UnixStream {
424424
self.0.passcred()
425425
}
426426

427+
/// Set the id of the socket for network filtering purpose
428+
///
429+
/// ```no_run
430+
/// #![feature(unix_set_mark)]
431+
/// use std::os::unix::net::UnixStream;
432+
///
433+
/// fn main() -> std::io::Result<()> {
434+
/// let sock = UnixStream::connect("/tmp/sock")?;
435+
/// sock.set_mark(32)?;
436+
/// Ok(())
437+
/// }
438+
/// ```
439+
#[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
440+
#[unstable(feature = "unix_set_mark", issue = "96467")]
441+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
442+
self.0.set_mark(mark)
443+
}
444+
427445
/// Returns the value of the `SO_ERROR` option.
428446
///
429447
/// # 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)