aboutsummaryrefslogtreecommitdiff
path: root/vendor/rustix/src/backend/libc/net/write_sockaddr.rs
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
committerValentin Popov <valentin@popov.link>2024-01-08 00:21:28 +0300
commit1b6a04ca5504955c571d1c97504fb45ea0befee4 (patch)
tree7579f518b23313e8a9748a88ab6173d5e030b227 /vendor/rustix/src/backend/libc/net/write_sockaddr.rs
parent5ecd8cf2cba827454317368b68571df0d13d7842 (diff)
downloadfparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.tar.xz
fparkan-1b6a04ca5504955c571d1c97504fb45ea0befee4.zip
Initial vendor packages
Signed-off-by: Valentin Popov <valentin@popov.link>
Diffstat (limited to 'vendor/rustix/src/backend/libc/net/write_sockaddr.rs')
-rw-r--r--vendor/rustix/src/backend/libc/net/write_sockaddr.rs103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/rustix/src/backend/libc/net/write_sockaddr.rs b/vendor/rustix/src/backend/libc/net/write_sockaddr.rs
new file mode 100644
index 0000000..2eee98c
--- /dev/null
+++ b/vendor/rustix/src/backend/libc/net/write_sockaddr.rs
@@ -0,0 +1,103 @@
+//! The BSD sockets API requires us to read the `ss_family` field before we can
+//! interpret the rest of a `sockaddr` produced by the kernel.
+
+use super::addr::SocketAddrStorage;
+#[cfg(unix)]
+use super::addr::SocketAddrUnix;
+use super::ext::{in6_addr_new, in_addr_new, sockaddr_in6_new};
+use crate::backend::c;
+use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
+use core::mem::size_of;
+
+pub(crate) unsafe fn write_sockaddr(
+ addr: &SocketAddrAny,
+ storage: *mut SocketAddrStorage,
+) -> usize {
+ match addr {
+ SocketAddrAny::V4(v4) => write_sockaddr_v4(v4, storage),
+ SocketAddrAny::V6(v6) => write_sockaddr_v6(v6, storage),
+ #[cfg(unix)]
+ SocketAddrAny::Unix(unix) => write_sockaddr_unix(unix, storage),
+ }
+}
+
+pub(crate) fn encode_sockaddr_v4(v4: &SocketAddrV4) -> c::sockaddr_in {
+ c::sockaddr_in {
+ #[cfg(any(
+ bsd,
+ target_os = "aix",
+ target_os = "espidf",
+ target_os = "haiku",
+ target_os = "nto",
+ target_os = "vita",
+ ))]
+ sin_len: size_of::<c::sockaddr_in>() as _,
+ sin_family: c::AF_INET as _,
+ sin_port: u16::to_be(v4.port()),
+ sin_addr: in_addr_new(u32::from_ne_bytes(v4.ip().octets())),
+ #[cfg(not(any(target_os = "haiku", target_os = "vita")))]
+ sin_zero: [0; 8_usize],
+ #[cfg(target_os = "haiku")]
+ sin_zero: [0; 24_usize],
+ #[cfg(target_os = "vita")]
+ sin_zero: [0; 6_usize],
+ #[cfg(target_os = "vita")]
+ sin_vport: 0,
+ }
+}
+
+unsafe fn write_sockaddr_v4(v4: &SocketAddrV4, storage: *mut SocketAddrStorage) -> usize {
+ let encoded = encode_sockaddr_v4(v4);
+ core::ptr::write(storage.cast(), encoded);
+ size_of::<c::sockaddr_in>()
+}
+
+pub(crate) fn encode_sockaddr_v6(v6: &SocketAddrV6) -> c::sockaddr_in6 {
+ #[cfg(any(
+ bsd,
+ target_os = "aix",
+ target_os = "espidf",
+ target_os = "haiku",
+ target_os = "nto",
+ target_os = "vita"
+ ))]
+ {
+ sockaddr_in6_new(
+ size_of::<c::sockaddr_in6>() as _,
+ c::AF_INET6 as _,
+ u16::to_be(v6.port()),
+ u32::to_be(v6.flowinfo()),
+ in6_addr_new(v6.ip().octets()),
+ v6.scope_id(),
+ )
+ }
+ #[cfg(not(any(
+ bsd,
+ target_os = "aix",
+ target_os = "espidf",
+ target_os = "haiku",
+ target_os = "nto",
+ target_os = "vita"
+ )))]
+ {
+ sockaddr_in6_new(
+ c::AF_INET6 as _,
+ u16::to_be(v6.port()),
+ u32::to_be(v6.flowinfo()),
+ in6_addr_new(v6.ip().octets()),
+ v6.scope_id(),
+ )
+ }
+}
+
+unsafe fn write_sockaddr_v6(v6: &SocketAddrV6, storage: *mut SocketAddrStorage) -> usize {
+ let encoded = encode_sockaddr_v6(v6);
+ core::ptr::write(storage.cast(), encoded);
+ size_of::<c::sockaddr_in6>()
+}
+
+#[cfg(unix)]
+unsafe fn write_sockaddr_unix(unix: &SocketAddrUnix, storage: *mut SocketAddrStorage) -> usize {
+ core::ptr::write(storage.cast(), unix.unix);
+ unix.len()
+}