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`.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: documentation should come before the cfg attributes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem, I'll change it. It made more sense in my head to put everything after the cfg since all of it should be blocked, but I should have looked at the rest of the code to see how you're doing it for consistency.

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
23 changes: 23 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
}

/// Returns true if this address is of an IUCV socket, false otherwise
#[cfg(all(
feature = "all",
any(target_arch = "x86_64", target_arch = "s390x"),
target_env = "gnu"
))]
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
53 changes: 53 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,51 @@ 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"
))]
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