Skip to content

Commit

Permalink
Merge #873
Browse files Browse the repository at this point in the history
873: add SO_MARK SetSockOpt for Linux r=asomers a=mcginty

SO_MARK allows traffic to be filtered by a "tag" using fwmark (see: https://www.linux.org/docs/man8/tc-fw.html).

Tested on Linux as root - the test will skip over when not being run as root, as I noticed a few other tests do this in the suite already.
  • Loading branch information
bors[bot] committed Mar 22, 2018
2 parents fdc5b83 + 3be575a commit 0418d34
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
## [Unreleased]

### Added
- Added `SO_MARK` on Linux.
- ([#873](https://github.com/nix-rust/nix/pull/873))
- Added `getsid` in `::nix::unistd`
([#850](https://github.com/nix-rust/nix/pull/850))
- Added `alarm`. ([#830](https://github.com/nix-rust/nix/pull/830))
Expand Down
22 changes: 22 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ sockopt_impl!(Both, IpTransparent, libc::SOL_IP, libc::IP_TRANSPARENT, bool);
sockopt_impl!(Both, BindAny, libc::SOL_SOCKET, libc::SO_BINDANY, bool);
#[cfg(target_os = "freebsd")]
sockopt_impl!(Both, BindAny, libc::IPPROTO_IP, libc::IP_BINDANY, bool);
#[cfg(target_os = "linux")]
sockopt_impl!(Both, Mark, libc::SOL_SOCKET, libc::SO_MARK, u32);

/*
*
Expand Down Expand Up @@ -527,4 +529,24 @@ mod test {
assert!(s_listening2);
close(s).unwrap();
}

#[cfg(target_os = "linux")]
#[test]
fn is_so_mark_functional() {
use super::super::*;
use ::unistd::Uid;
use ::std::io::{self, Write};

if !Uid::current().is_root() {
let stderr = io::stderr();
let mut handle = stderr.lock();
writeln!(handle, "SO_MARK requires root privileges. Skipping test.").unwrap();
return;
}

let s = socket(AddressFamily::Inet, SockType::Stream, SockFlag::empty(), None).unwrap();
setsockopt(s, super::Mark, &1337).unwrap();
let mark = getsockopt(s, super::Mark).unwrap();
assert_eq!(mark, 1337);
}
}

0 comments on commit 0418d34

Please sign in to comment.