Skip to content

Commit

Permalink
net: unify SocketAddrUnix value extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Dec 19, 2024
1 parent 4c5dc02 commit 6c5cbcd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
38 changes: 19 additions & 19 deletions src/backend/libc/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,10 @@ impl SocketAddrUnix {
/// For a filesystem path address, return the path.
#[inline]
pub fn path(&self) -> Option<&CStr> {
let len = self.len();
if len != 0 && self.unix.sun_path[0] != 0 {
let end = len as usize - offsetof_sun_path();
let bytes = &self.unix.sun_path[..end];
// SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`.
// And `from_bytes_with_nul_unchecked` since the string is
// NUL-terminated.
unsafe {
Some(CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(
bytes.as_ptr().cast(),
bytes.len(),
)))
}
let bytes = self.bytes()?;
if !bytes.is_empty() && bytes[0] != 0 {
// SAFETY: `from_bytes_with_nul_unchecked` since the string is NUL-terminated.
Some(unsafe { CStr::from_bytes_with_nul_unchecked(bytes) })
} else {
None
}
Expand All @@ -143,11 +134,8 @@ impl SocketAddrUnix {
#[cfg(linux_kernel)]
#[inline]
pub fn abstract_name(&self) -> Option<&[u8]> {
let end = self.len().saturating_sub(offsetof_sun_path());
if end > 0 && self.unix.sun_path[0] as u8 == b'\0' {
let bytes = &self.unix.sun_path[1..end];
// SAFETY: Convert `&[c_char]` to `&[u8]`.
Some(unsafe { slice::from_raw_parts(bytes.as_ptr().cast::<u8>(), bytes.len()) })
if let [0, ref bytes @ ..] = self.bytes()? {
Some(bytes)
} else {
None
}
Expand All @@ -157,7 +145,7 @@ impl SocketAddrUnix {
#[cfg(linux_kernel)]
#[inline]
pub fn is_unnamed(&self) -> bool {
self.len() == offsetof_sun_path()
self.bytes() == Some(&[])
}

#[inline]
Expand All @@ -176,6 +164,18 @@ impl SocketAddrUnix {
pub(crate) fn len(&self) -> usize {
self.addr_len() as usize
}

#[inline]
fn bytes(&self) -> Option<&[u8]> {
let len = self.len() as usize;
if len != 0 {
let bytes = &self.unix.sun_path[..len - offsetof_sun_path()];
// SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`.
Some(unsafe { slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len()) })
} else {
None
}
}
}

#[cfg(unix)]
Expand Down
36 changes: 19 additions & 17 deletions src/backend/linux_raw/net/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,10 @@ impl SocketAddrUnix {
/// For a filesystem path address, return the path.
#[inline]
pub fn path(&self) -> Option<&CStr> {
let len = self.len();
if len != 0 && self.unix.sun_path[0] as u8 != b'\0' {
let end = len as usize - offsetof_sun_path();
let bytes = &self.unix.sun_path[..end];

// SAFETY: Convert `&[c_char]` to `&[u8]`.
let bytes = unsafe { slice::from_raw_parts(bytes.as_ptr().cast::<u8>(), bytes.len()) };

// SAFETY: `from_bytes_with_nul_unchecked` since the string is
// NUL-terminated.
unsafe { Some(CStr::from_bytes_with_nul_unchecked(bytes)) }
let bytes = self.bytes()?;
if !bytes.is_empty() && bytes[0] != 0 {
// SAFETY: `from_bytes_with_nul_unchecked` since the string is NUL-terminated.
Some(unsafe { CStr::from_bytes_with_nul_unchecked(bytes) })
} else {
None
}
Expand All @@ -111,11 +104,8 @@ impl SocketAddrUnix {
/// For an abstract address, return the identifier.
#[inline]
pub fn abstract_name(&self) -> Option<&[u8]> {
let end = self.len().saturating_sub(offsetof_sun_path());
if end > 0 && self.unix.sun_path[0] as u8 == b'\0' {
let bytes = &self.unix.sun_path[1..end];
// SAFETY: Convert `&[c_char]` to `&[u8]`.
Some(unsafe { slice::from_raw_parts(bytes.as_ptr().cast::<u8>(), bytes.len()) })
if let [0, ref bytes @ ..] = self.bytes()? {
Some(bytes)
} else {
None
}
Expand All @@ -125,7 +115,7 @@ impl SocketAddrUnix {
#[cfg(linux_kernel)]
#[inline]
pub fn is_unnamed(&self) -> bool {
self.len() == offsetof_sun_path()
self.bytes() == Some(&[])
}

#[inline]
Expand All @@ -137,6 +127,18 @@ impl SocketAddrUnix {
pub(crate) fn len(&self) -> usize {
self.addr_len() as usize
}

#[inline]
fn bytes(&self) -> Option<&[u8]> {
let len = self.len() as usize;
if len != 0 {
let bytes = &self.unix.sun_path[..len - offsetof_sun_path()];
// SAFETY: `from_raw_parts` to convert from `&[c_char]` to `&[u8]`.
Some(unsafe { slice::from_raw_parts(bytes.as_ptr().cast(), bytes.len()) })
} else {
None
}
}
}

impl PartialEq for SocketAddrUnix {
Expand Down

0 comments on commit 6c5cbcd

Please sign in to comment.