Skip to content

Commit 14d288f

Browse files
committed
socket set_mark addition.
to be able to set a marker/id on the socket for network filtering (iptables/ipfw here) purpose.
1 parent 5fb8a39 commit 14d288f

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ impl UnixStream {
424424
self.0.passcred()
425425
}
426426

427+
#[cfg(any(doc, target_os = "linux", target_os = "freebsd",))]
428+
#[unstable(feature = "unix_set_mark", issue = "none")]
429+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
430+
self.0.set_mark(mark)
431+
}
432+
427433
/// Returns the value of the `SO_ERROR` option.
428434
///
429435
/// # Examples

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ impl Socket {
427427
self.0.set_nonblocking(nonblocking)
428428
}
429429

430+
#[cfg(target_os = "linux")]
431+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
432+
setsockopt(self, libc::SOL_SOCKET, libc::SO_MARK, mark as libc::c_int)
433+
}
434+
435+
#[cfg(target_os = "freebsd")]
436+
pub fn set_mark(&self, mark: u32) -> io::Result<()> {
437+
setsockopt(self, libc::SOL_SOCKET, libc::SO_USER_COOKIE, mark)
438+
}
439+
430440
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
431441
let raw: c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_ERROR)?;
432442
if raw == 0 { Ok(None) } else { Ok(Some(io::Error::from_raw_os_error(raw as i32))) }

0 commit comments

Comments
 (0)