diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-11-21 17:45:29 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-21 17:45:29 +0000 |
commit | 79f04fb3a34a4af6d8bdcc5807d63a980f483f94 (patch) | |
tree | b6cb3d43dd747048fe9cc842fb8af3f2e5c92656 /src/sys | |
parent | 9ea1493a813bf49a493a4528640f036b9dbcc7d8 (diff) | |
parent | f6a22198a48b138f43abeb186ebe9f4ebb1af415 (diff) | |
download | nix-79f04fb3a34a4af6d8bdcc5807d63a980f483f94.zip |
Merge #1857
1857: Add better support for unnamed unix socket addrs r=asomers a=stevenengler
This adds the following 2 functions/methods: `UnixAddr::new_unnamed` and `UnixAddr::is_unnamed`.
Closes #1585
unix(7) on Linux:
> unnamed: A stream socket that has not been bound to a pathname using bind(2) has no name. Likewise, the two sockets created by socketpair(2) are unnamed. When the address of an unnamed socket is returned, its length is `sizeof(sa_family_t)`, and `sun_path` should not be inspected.
**Edit:** This currently isn't working on BSD, but I see why. Will fix it shortly.
Co-authored-by: Steven Engler <opara@cs.georgetown.edu>
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/socket/addr.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/sys/socket/addr.rs b/src/sys/socket/addr.rs index b80d78c1..2b3e4919 100644 --- a/src/sys/socket/addr.rs +++ b/src/sys/socket/addr.rs @@ -885,6 +885,20 @@ impl UnixAddr { } } + /// Create a new `sockaddr_un` representing an "unnamed" unix socket address. + #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg_attr(docsrs, doc(cfg(all())))] + pub fn new_unnamed() -> UnixAddr { + let 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(); + + 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 +955,14 @@ impl UnixAddr { } } + /// Check if this address is an "unnamed" unix socket address. + #[cfg(any(target_os = "android", target_os = "linux"))] + #[cfg_attr(docsrs, doc(cfg(all())))] + #[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 { |