Skip to content

Commit

Permalink
Add the UtunIfname sockopt (UTUN_OPT_IFNAME) (nix-rust#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
friedrich authored Mar 3, 2024
1 parent e0f1965 commit 7badbee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2325.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add socket option UtunIfname.
11 changes: 11 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,17 @@ sockopt_impl!(
libc::IPV6_DONTFRAG,
bool
);
#[cfg(apple_targets)]
#[cfg(feature = "net")]
sockopt_impl!(
/// Get the utun interface name.
UtunIfname,
GetOnly,
libc::SYSPROTO_CONTROL,
libc::UTUN_OPT_IFNAME,
OsString,
GetOsString<[u8; libc::IFNAMSIZ]>
);

#[allow(missing_docs)]
// Not documented by Linux!
Expand Down
31 changes: 31 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,34 @@ fn test_ktls() {
Err(err) => panic!("{err:?}"),
}
}

#[test]
#[cfg(apple_targets)]
fn test_utun_ifname() {
use nix::sys::socket::connect;
use nix::sys::socket::SysControlAddr;

let fd = socket(
AddressFamily::System,
SockType::Datagram,
SockFlag::empty(),
SockProtocol::KextControl,
)
.unwrap();

let unit = 123;
let addr = SysControlAddr::from_name(
fd.as_raw_fd(),
"com.apple.net.utun_control",
unit,
)
.unwrap();

connect(fd.as_raw_fd(), &addr).unwrap();

let name = getsockopt(&fd, sockopt::UtunIfname)
.expect("getting UTUN_OPT_IFNAME on a utun interface should succeed");

let expected_name = format!("utun{}\0", unit - 1);
assert_eq!(name.into_string(), Ok(expected_name));
}

0 comments on commit 7badbee

Please sign in to comment.