1b8a62b91Sopenharmony_ci//! A socket address for any kind of socket. 2b8a62b91Sopenharmony_ci//! 3b8a62b91Sopenharmony_ci//! This is similar to [`std::net::SocketAddr`], but also supports Unix-domain 4b8a62b91Sopenharmony_ci//! socket addresses. 5b8a62b91Sopenharmony_ci//! 6b8a62b91Sopenharmony_ci//! # Safety 7b8a62b91Sopenharmony_ci//! 8b8a62b91Sopenharmony_ci//! The `read` and `write` functions allow decoding and encoding from and to 9b8a62b91Sopenharmony_ci//! OS-specific socket address representations in memory. 10b8a62b91Sopenharmony_ci#![allow(unsafe_code)] 11b8a62b91Sopenharmony_ci 12b8a62b91Sopenharmony_ci#[cfg(unix)] 13b8a62b91Sopenharmony_ciuse crate::net::SocketAddrUnix; 14b8a62b91Sopenharmony_ciuse crate::net::{AddressFamily, SocketAddrV4, SocketAddrV6}; 15b8a62b91Sopenharmony_ciuse crate::{backend, io}; 16b8a62b91Sopenharmony_ci#[cfg(feature = "std")] 17b8a62b91Sopenharmony_ciuse core::fmt; 18b8a62b91Sopenharmony_ci 19b8a62b91Sopenharmony_cipub use backend::net::addr::SocketAddrStorage; 20b8a62b91Sopenharmony_ci 21b8a62b91Sopenharmony_ci/// `struct sockaddr_storage` as a Rust enum. 22b8a62b91Sopenharmony_ci#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] 23b8a62b91Sopenharmony_ci#[doc(alias = "sockaddr")] 24b8a62b91Sopenharmony_ci#[non_exhaustive] 25b8a62b91Sopenharmony_cipub enum SocketAddrAny { 26b8a62b91Sopenharmony_ci /// `struct sockaddr_in` 27b8a62b91Sopenharmony_ci V4(SocketAddrV4), 28b8a62b91Sopenharmony_ci /// `struct sockaddr_in6` 29b8a62b91Sopenharmony_ci V6(SocketAddrV6), 30b8a62b91Sopenharmony_ci /// `struct sockaddr_un` 31b8a62b91Sopenharmony_ci #[cfg(unix)] 32b8a62b91Sopenharmony_ci Unix(SocketAddrUnix), 33b8a62b91Sopenharmony_ci} 34b8a62b91Sopenharmony_ci 35b8a62b91Sopenharmony_ciimpl From<SocketAddrV4> for SocketAddrAny { 36b8a62b91Sopenharmony_ci #[inline] 37b8a62b91Sopenharmony_ci fn from(from: SocketAddrV4) -> Self { 38b8a62b91Sopenharmony_ci Self::V4(from) 39b8a62b91Sopenharmony_ci } 40b8a62b91Sopenharmony_ci} 41b8a62b91Sopenharmony_ci 42b8a62b91Sopenharmony_ciimpl From<SocketAddrV6> for SocketAddrAny { 43b8a62b91Sopenharmony_ci #[inline] 44b8a62b91Sopenharmony_ci fn from(from: SocketAddrV6) -> Self { 45b8a62b91Sopenharmony_ci Self::V6(from) 46b8a62b91Sopenharmony_ci } 47b8a62b91Sopenharmony_ci} 48b8a62b91Sopenharmony_ci 49b8a62b91Sopenharmony_ci#[cfg(unix)] 50b8a62b91Sopenharmony_ciimpl From<SocketAddrUnix> for SocketAddrAny { 51b8a62b91Sopenharmony_ci #[inline] 52b8a62b91Sopenharmony_ci fn from(from: SocketAddrUnix) -> Self { 53b8a62b91Sopenharmony_ci Self::Unix(from) 54b8a62b91Sopenharmony_ci } 55b8a62b91Sopenharmony_ci} 56b8a62b91Sopenharmony_ci 57b8a62b91Sopenharmony_ciimpl SocketAddrAny { 58b8a62b91Sopenharmony_ci /// Return the address family of this socket address. 59b8a62b91Sopenharmony_ci #[inline] 60b8a62b91Sopenharmony_ci pub const fn address_family(&self) -> AddressFamily { 61b8a62b91Sopenharmony_ci match self { 62b8a62b91Sopenharmony_ci Self::V4(_) => AddressFamily::INET, 63b8a62b91Sopenharmony_ci Self::V6(_) => AddressFamily::INET6, 64b8a62b91Sopenharmony_ci #[cfg(unix)] 65b8a62b91Sopenharmony_ci Self::Unix(_) => AddressFamily::UNIX, 66b8a62b91Sopenharmony_ci } 67b8a62b91Sopenharmony_ci } 68b8a62b91Sopenharmony_ci 69b8a62b91Sopenharmony_ci /// Writes a platform-specific encoding of this socket address to 70b8a62b91Sopenharmony_ci /// the memory pointed to by `storage`, and returns the number of 71b8a62b91Sopenharmony_ci /// bytes used. 72b8a62b91Sopenharmony_ci /// 73b8a62b91Sopenharmony_ci /// # Safety 74b8a62b91Sopenharmony_ci /// 75b8a62b91Sopenharmony_ci /// `storage` must point to valid memory for encoding the socket 76b8a62b91Sopenharmony_ci /// address. 77b8a62b91Sopenharmony_ci pub unsafe fn write(&self, storage: *mut SocketAddrStorage) -> usize { 78b8a62b91Sopenharmony_ci backend::net::write_sockaddr::write_sockaddr(self, storage) 79b8a62b91Sopenharmony_ci } 80b8a62b91Sopenharmony_ci 81b8a62b91Sopenharmony_ci /// Reads a platform-specific encoding of a socket address from 82b8a62b91Sopenharmony_ci /// the memory pointed to by `storage`, which uses `len` bytes. 83b8a62b91Sopenharmony_ci /// 84b8a62b91Sopenharmony_ci /// # Safety 85b8a62b91Sopenharmony_ci /// 86b8a62b91Sopenharmony_ci /// `storage` must point to valid memory for decoding a socket 87b8a62b91Sopenharmony_ci /// address. 88b8a62b91Sopenharmony_ci pub unsafe fn read(storage: *const SocketAddrStorage, len: usize) -> io::Result<Self> { 89b8a62b91Sopenharmony_ci backend::net::read_sockaddr::read_sockaddr(storage, len) 90b8a62b91Sopenharmony_ci } 91b8a62b91Sopenharmony_ci} 92b8a62b91Sopenharmony_ci 93b8a62b91Sopenharmony_ci#[cfg(feature = "std")] 94b8a62b91Sopenharmony_ciimpl fmt::Debug for SocketAddrAny { 95b8a62b91Sopenharmony_ci fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { 96b8a62b91Sopenharmony_ci match self { 97b8a62b91Sopenharmony_ci Self::V4(v4) => v4.fmt(fmt), 98b8a62b91Sopenharmony_ci Self::V6(v6) => v6.fmt(fmt), 99b8a62b91Sopenharmony_ci #[cfg(unix)] 100b8a62b91Sopenharmony_ci Self::Unix(unix) => unix.fmt(fmt), 101b8a62b91Sopenharmony_ci } 102b8a62b91Sopenharmony_ci } 103b8a62b91Sopenharmony_ci} 104