From 8884ea38ec80d838138070e941c080b0b573575a Mon Sep 17 00:00:00 2001 From: Steven Engler Date: Mon, 7 Nov 2022 15:17:29 -0500 Subject: Added better support for unnamed unix socket addrs --- src/sys/socket/addr.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src/sys') diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index c7b5f29e..f051d205 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -885,6 +885,29 @@ impl UnixAddr { } } + /// Create a new `sockaddr_un` representing an "unnamed" unix socket address. + pub fn new_unnamed() -> UnixAddr { + #[allow(unused)] + let mut ret = libc::sockaddr_un { + sun_family: AddressFamily::Unix as sa_family_t, + .. unsafe { mem::zeroed() } + }; + + let sun_len: u8 = offset_of!(libc::sockaddr_un, sun_path).try_into().unwrap(); + + #[cfg(any(target_os = "dragonfly", + target_os = "freebsd", + target_os = "ios", + target_os = "macos", + target_os = "netbsd", + target_os = "openbsd"))] + { + ret.sun_len = sun_len; + } + + unsafe { UnixAddr::from_raw_parts(ret, sun_len) } + } + /// Create a UnixAddr from a raw `sockaddr_un` struct and a size. `sun_len` /// is the size of the valid portion of the struct, excluding any trailing /// NUL. @@ -941,6 +964,12 @@ impl UnixAddr { } } + /// Check if this address is an "unnamed" unix socket address. + #[inline] + pub fn is_unnamed(&self) -> bool { + matches!(self.kind(), UnixAddrKind::Unnamed) + } + /// Returns the addrlen of this socket - `offsetof(struct sockaddr_un, sun_path)` #[inline] pub fn path_len(&self) -> usize { -- cgit v1.2.3