Skip to content

Commit

Permalink
Add SO_BINDTODEVICE sockopt
Browse files Browse the repository at this point in the history
This is available only on Linux as far I know,
[socket(7)](https://linux.die.net/man/7/socket) has some information
about the `SO_BINDTODEVICE` sockopt. In simple words it binds a socket
to an specific network device (specified as an string like "wlo1",
"eth0", etc.), to only process packets from that device.

Signed-off-by: Jean Pierre Dudey <jeandudey@hotmail.com>
  • Loading branch information
jeandudey committed May 8, 2020
1 parent a937988 commit 10ebf45
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -27,6 +27,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
(#[1208](https://github.com/nix-rust/nix/pull/1208))
- Added support for `SCM_CREDS` messages (`UnixCredentials`) on FreeBSD/DragonFly
(#[1216](https://github.com/nix-rust/nix/pull/1216))
- Added `BindToDevice` socket option (sockopt) on Linux
(#[1233](https://github.com/nix-rust/nix/pull/1233))

### Changed
- Changed `fallocate` return type from `c_int` to `()` (#[1201](https://github.com/nix-rust/nix/pull/1201))
Expand Down
2 changes: 2 additions & 0 deletions src/sys/socket/sockopt.rs
Expand Up @@ -259,6 +259,8 @@ sockopt_impl!(SetOnly, RcvBufForce, libc::SOL_SOCKET, libc::SO_RCVBUFFORCE, usiz
sockopt_impl!(SetOnly, SndBufForce, libc::SOL_SOCKET, libc::SO_SNDBUFFORCE, usize);
sockopt_impl!(GetOnly, SockType, libc::SOL_SOCKET, libc::SO_TYPE, super::SockType);
sockopt_impl!(GetOnly, AcceptConn, libc::SOL_SOCKET, libc::SO_ACCEPTCONN, bool);
#[cfg(target_os = "linux")]
sockopt_impl!(Both, BindToDevice, libc::SOL_SOCKET, libc::SO_BINDTODEVICE, OsString<[u8; libc::IFNAMSIZ]>);
#[cfg(any(target_os = "android", target_os = "linux"))]
sockopt_impl!(GetOnly, OriginalDst, libc::SOL_IP, libc::SO_ORIGINAL_DST, libc::sockaddr_in);
sockopt_impl!(Both, ReceiveTimestamp, libc::SOL_SOCKET, libc::SO_TIMESTAMP, bool);
Expand Down
23 changes: 23 additions & 0 deletions test/sys/test_sockopt.rs
Expand Up @@ -51,3 +51,26 @@ fn test_tcp_congestion() {
val
);
}

#[test]
#[cfg(target_os = "linux")]
fn test_bindtodevice() {
use nix::Error;
use nix::errno::Errno;

let fd = socket(AddressFamily::Inet, SockType::Stream, SockFlag::empty(), None).unwrap();

let val = getsockopt(fd, sockopt::BindToDevice).unwrap();
match setsockopt(fd, sockopt::BindToDevice, &val) {
// SO_BINDTODEVICE requires root, ignore the error as this could break
// the CI.
Err(Error::Sys(Errno::EPERM)) => (),
Err(e) => panic!("{}", e),
Ok(()) => (),
}

assert_eq!(
getsockopt(fd, sockopt::BindToDevice).unwrap(),
val
);
}

0 comments on commit 10ebf45

Please sign in to comment.