Skip to content

Commit 7badbee

Browse files
authored
Add the UtunIfname sockopt (UTUN_OPT_IFNAME) (#2325)
1 parent e0f1965 commit 7badbee

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

changelog/2325.added.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add socket option UtunIfname.

src/sys/socket/sockopt.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,17 @@ sockopt_impl!(
10651065
libc::IPV6_DONTFRAG,
10661066
bool
10671067
);
1068+
#[cfg(apple_targets)]
1069+
#[cfg(feature = "net")]
1070+
sockopt_impl!(
1071+
/// Get the utun interface name.
1072+
UtunIfname,
1073+
GetOnly,
1074+
libc::SYSPROTO_CONTROL,
1075+
libc::UTUN_OPT_IFNAME,
1076+
OsString,
1077+
GetOsString<[u8; libc::IFNAMSIZ]>
1078+
);
10681079

10691080
#[allow(missing_docs)]
10701081
// Not documented by Linux!

test/sys/test_sockopt.rs

+31
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,34 @@ fn test_ktls() {
828828
Err(err) => panic!("{err:?}"),
829829
}
830830
}
831+
832+
#[test]
833+
#[cfg(apple_targets)]
834+
fn test_utun_ifname() {
835+
use nix::sys::socket::connect;
836+
use nix::sys::socket::SysControlAddr;
837+
838+
let fd = socket(
839+
AddressFamily::System,
840+
SockType::Datagram,
841+
SockFlag::empty(),
842+
SockProtocol::KextControl,
843+
)
844+
.unwrap();
845+
846+
let unit = 123;
847+
let addr = SysControlAddr::from_name(
848+
fd.as_raw_fd(),
849+
"com.apple.net.utun_control",
850+
unit,
851+
)
852+
.unwrap();
853+
854+
connect(fd.as_raw_fd(), &addr).unwrap();
855+
856+
let name = getsockopt(&fd, sockopt::UtunIfname)
857+
.expect("getting UTUN_OPT_IFNAME on a utun interface should succeed");
858+
859+
let expected_name = format!("utun{}\0", unit - 1);
860+
assert_eq!(name.into_string(), Ok(expected_name));
861+
}

0 commit comments

Comments
 (0)