Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ impl Domain {
/// Domain for IPv6 communication, corresponding to `AF_INET6`.
pub const IPV6: Domain = Domain(sys::AF_INET6);

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
/// Domain for IUCV socket communication, corresponding to `AF_IUCV`.
pub const IUCV: Domain = Domain(sys::AF_IUCV);

/// Domain for Unix socket communication, corresponding to `AF_UNIX`.
#[cfg(not(target_os = "wasi"))]
pub const UNIX: Domain = Domain(sys::AF_UNIX);
Expand Down
43 changes: 43 additions & 0 deletions src/sockaddr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ impl SockAddr {
crate::sys::unix_sockaddr(path.as_ref())
}

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
/// Constructs a `SockAddr` with the family `AF_IUCV` and the provided
/// components.
///
/// Returns an error if the path is too long.
pub fn iucv(userid: &str, name: &str) -> io::Result<SockAddr> {
crate::sys::iucv_sockaddr(userid, name)
}

/// Set the length of the address.
///
/// # Safety
Expand Down Expand Up @@ -278,6 +291,16 @@ impl SockAddr {
self.storage.ss_family == AF_UNIX as sa_family_t
}

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
/// Returns true if this address is of an IUCV socket, false otherwise
pub const fn is_iucv(&self) -> bool {
self.storage.ss_family == libc::AF_IUCV as sa_family_t
}

/// Returns this address as a `SocketAddr` if it is in the `AF_INET` (IPv4)
/// or `AF_INET6` (IPv6) family, otherwise returns `None`.
pub fn as_socket(&self) -> Option<SocketAddr> {
Expand Down Expand Up @@ -335,6 +358,26 @@ impl SockAddr {
// bytes by using `self.len`.
unsafe { std::slice::from_raw_parts(self.as_ptr().cast(), self.len as usize) }
}

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
/// Returns this address as a [`sockaddr_iucv`](https://docs.rs/libc/latest/libc/struct.sockaddr_iucv.html) if it is in the 'AF_IUCV' family
pub fn as_socket_iucv(&self) -> Option<libc::sockaddr_iucv> {
use libc::sockaddr_iucv;

if self.storage.ss_family == libc::AF_IUCV as sa_family_t
&& self.len == (mem::size_of::<sockaddr_iucv>() as u32)
{
let s_iucv_ptr = &self.storage as *const _ as *const libc::sockaddr_iucv;
// SAFETY: We checked that the Address Family and size were correct
let s_iucv = unsafe { *s_iucv_ptr };
return Some(s_iucv);
}
None
}
}

impl From<SocketAddr> for SockAddr {
Expand Down
54 changes: 54 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ pub(crate) use std::ffi::c_int;
#[cfg(not(target_os = "wasi"))]
pub(crate) use libc::AF_UNIX;
pub(crate) use libc::{AF_INET, AF_INET6};

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
pub(crate) use libc::AF_IUCV;

// Used in `Type`.
#[cfg(all(feature = "all", target_os = "linux"))]
pub(crate) use libc::SOCK_DCCP;
Expand Down Expand Up @@ -765,6 +773,52 @@ pub(crate) fn unix_sockaddr(path: &Path) -> io::Result<SockAddr> {
Ok(unsafe { SockAddr::new(storage, len as socklen_t) })
}

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
fn copy_and_pad_with_spaces(src: &str, target: &mut [libc::c_char; 8]) -> io::Result<()> {
let bytes = src.as_bytes();
if bytes.len() > target.len() {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"IUCV address component is too long",
));
}
let mut bytes_iter = bytes.iter();
for c in target {
if let Some(b) = bytes_iter.next() {
*c = *b as libc::c_char;
} else {
*c = b' ' as libc::c_char;
}
}
Ok(())
}

#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
#[allow(unsafe_op_in_unsafe_fn)]
pub(crate) fn iucv_sockaddr(userid: &str, name: &str) -> io::Result<SockAddr> {
let mut storage = SockAddrStorage::zeroed();
{
// SAFETY: sockaddr_iucv is one of the sockaddr_* types defined by this platform.
let storage = unsafe { storage.view_as::<libc::sockaddr_iucv>() };

storage.siucv_family = libc::AF_IUCV as sa_family_t;
// Note that storage.siucv_port is initialized to zero above, which is required for the reserved field
// Note that storage.siucv_addr is initialized to zero above, which is required for the reserved field
storage.siucv_nodeid = [b' ' as libc::c_char; 8]; // Required for the reserved field
copy_and_pad_with_spaces(userid, &mut storage.siucv_user_id)?;
copy_and_pad_with_spaces(name, &mut storage.siucv_name)?;
}
Ok(unsafe { SockAddr::new(storage, mem::size_of::<libc::sockaddr_iucv>() as socklen_t) })
}

// Used in `MsgHdr`.
#[cfg(not(any(target_os = "redox", target_os = "wasi", target_os = "horizon")))]
pub(crate) use libc::msghdr;
Expand Down
Loading