Skip to content

Commit 8884ea3

Browse files
committed
Added better support for unnamed unix socket addrs
1 parent 33b5f92 commit 8884ea3

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/sys/socket/addr.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,29 @@ impl UnixAddr {
885885
}
886886
}
887887

888+
/// Create a new `sockaddr_un` representing an "unnamed" unix socket address.
889+
pub fn new_unnamed() -> UnixAddr {
890+
#[allow(unused)]
891+
let mut ret = libc::sockaddr_un {
892+
sun_family: AddressFamily::Unix as sa_family_t,
893+
.. unsafe { mem::zeroed() }
894+
};
895+
896+
let sun_len: u8 = offset_of!(libc::sockaddr_un, sun_path).try_into().unwrap();
897+
898+
#[cfg(any(target_os = "dragonfly",
899+
target_os = "freebsd",
900+
target_os = "ios",
901+
target_os = "macos",
902+
target_os = "netbsd",
903+
target_os = "openbsd"))]
904+
{
905+
ret.sun_len = sun_len;
906+
}
907+
908+
unsafe { UnixAddr::from_raw_parts(ret, sun_len) }
909+
}
910+
888911
/// Create a UnixAddr from a raw `sockaddr_un` struct and a size. `sun_len`
889912
/// is the size of the valid portion of the struct, excluding any trailing
890913
/// NUL.
@@ -941,6 +964,12 @@ impl UnixAddr {
941964
}
942965
}
943966

967+
/// Check if this address is an "unnamed" unix socket address.
968+
#[inline]
969+
pub fn is_unnamed(&self) -> bool {
970+
matches!(self.kind(), UnixAddrKind::Unnamed)
971+
}
972+
944973
/// Returns the addrlen of this socket - `offsetof(struct sockaddr_un, sun_path)`
945974
#[inline]
946975
pub fn path_len(&self) -> usize {

test/sys/test_socket.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,22 @@ pub fn test_abstract_uds_addr() {
244244
assert_eq!(unsafe { (*addr.as_ptr()).sun_path[0] }, 0);
245245
}
246246

247+
// Test getting an unnamed address (without unix socket creation)
248+
#[test]
249+
pub fn test_unnamed_uds_addr() {
250+
use crate::nix::sys::socket::SockaddrLike;
251+
252+
let addr = UnixAddr::new_unnamed();
253+
254+
assert!(addr.is_unnamed());
255+
assert_eq!(addr.len(), 2);
256+
assert!(addr.path().is_none());
257+
assert_eq!(addr.path_len(), 0);
258+
259+
#[cfg(target_os = "linux")]
260+
assert!(addr.as_abstract().is_none());
261+
}
262+
247263
#[test]
248264
pub fn test_getsockname() {
249265
use nix::sys::socket::bind;
@@ -1484,7 +1500,7 @@ fn test_impl_scm_credentials_and_rights(mut space: Vec<u8>) {
14841500

14851501
// Test creating and using named unix domain sockets
14861502
#[test]
1487-
pub fn test_unixdomain() {
1503+
pub fn test_named_unixdomain() {
14881504
use nix::sys::socket::{accept, bind, connect, listen, socket, UnixAddr};
14891505
use nix::sys::socket::{SockFlag, SockType};
14901506
use nix::unistd::{close, read, write};
@@ -1527,6 +1543,59 @@ pub fn test_unixdomain() {
15271543
assert_eq!(&buf[..], b"hello");
15281544
}
15291545

1546+
// Test using unnamed unix domain addresses
1547+
#[test]
1548+
pub fn test_unnamed_unixdomain() {
1549+
use nix::sys::socket::{getsockname, socketpair};
1550+
use nix::sys::socket::{SockFlag, SockType};
1551+
use nix::unistd::close;
1552+
1553+
let (fd_1, fd_2) = socketpair(
1554+
AddressFamily::Unix,
1555+
SockType::Stream,
1556+
None,
1557+
SockFlag::empty(),
1558+
)
1559+
.expect("socketpair failed");
1560+
1561+
let addr_1: UnixAddr = getsockname(fd_1).expect("getsockname failed");
1562+
assert!(addr_1.is_unnamed());
1563+
assert_eq!(addr_1, UnixAddr::new_unnamed());
1564+
1565+
close(fd_1).unwrap();
1566+
close(fd_2).unwrap();
1567+
}
1568+
1569+
// Test creating and using unnamed unix domain addresses for autobinding sockets
1570+
#[cfg(any(target_os = "android", target_os = "linux"))]
1571+
#[test]
1572+
pub fn test_unnamed_unixdomain_autobind() {
1573+
use nix::sys::socket::{bind, getsockname, socket};
1574+
use nix::sys::socket::{SockFlag, SockType};
1575+
use nix::unistd::close;
1576+
1577+
let fd = socket(
1578+
AddressFamily::Unix,
1579+
SockType::Stream,
1580+
SockFlag::empty(),
1581+
None,
1582+
)
1583+
.expect("socket failed");
1584+
1585+
// unix(7): "If a bind(2) call specifies addrlen as `sizeof(sa_family_t)`, or [...], then the
1586+
// socket is autobound to an abstract address"
1587+
bind(fd, &UnixAddr::new_unnamed()).expect("bind failed");
1588+
1589+
let addr: UnixAddr = getsockname(fd).expect("getsockname failed");
1590+
let addr = addr.as_abstract().unwrap();
1591+
1592+
// changed from 8 to 5 bytes in Linux 2.3.15, and rust's minimum supported Linux version is 3.2
1593+
// (as of 2022-11)
1594+
assert_eq!(addr.len(), 5);
1595+
1596+
close(fd).unwrap();
1597+
}
1598+
15301599
// Test creating and using named system control sockets
15311600
#[cfg(any(target_os = "macos", target_os = "ios"))]
15321601
#[test]

0 commit comments

Comments
 (0)