1//! The BSD sockets API requires us to read the `ss_family` field before we can
2//! interpret the rest of a `sockaddr` produced by the kernel.
3#![allow(unsafe_code)]
4
5use crate::backend::c;
6#[cfg(target_os = "linux")]
7use crate::net::xdp::SocketAddrXdp;
8use crate::net::{SocketAddrAny, SocketAddrStorage, SocketAddrUnix, SocketAddrV4, SocketAddrV6};
9use core::mem::size_of;
10
11pub(crate) unsafe fn write_sockaddr(
12 addr: &SocketAddrAny,
13 storage: *mut SocketAddrStorage,
14) -> usize {
15 match addr {
16 SocketAddrAny::V4(v4: &SocketAddrV4) => write_sockaddr_v4(v4, storage),
17 SocketAddrAny::V6(v6: &SocketAddrV6) => write_sockaddr_v6(v6, storage),
18 SocketAddrAny::Unix(unix: &SocketAddrUnix) => write_sockaddr_unix(unix, storage),
19 #[cfg(target_os = "linux")]
20 SocketAddrAny::Xdp(xdp: &SocketAddrXdp) => write_sockaddr_xdp(xdp, storage),
21 }
22}
23
24pub(crate) fn encode_sockaddr_v4(v4: &SocketAddrV4) -> c::sockaddr_in {
25 c::sockaddr_in {
26 sin_family: c::AF_INET as _,
27 sin_port: u16::to_be(self:v4.port()),
28 sin_addr: c::in_addr {
29 s_addr: u32::from_ne_bytes(v4.ip().octets()),
30 },
31 __pad: [0_u8; 8],
32 }
33}
34
35unsafe fn write_sockaddr_v4(v4: &SocketAddrV4, storage: *mut SocketAddrStorage) -> usize {
36 let encoded: sockaddr_in = encode_sockaddr_v4(v4);
37 core::ptr::write(dst:storage.cast(), src:encoded);
38 size_of::<c::sockaddr_in>()
39}
40
41pub(crate) fn encode_sockaddr_v6(v6: &SocketAddrV6) -> c::sockaddr_in6 {
42 c::sockaddr_in6 {
43 sin6_family: c::AF_INET6 as _,
44 sin6_port: u16::to_be(self:v6.port()),
45 sin6_flowinfo: u32::to_be(self:v6.flowinfo()),
46 sin6_addr: c::in6_addr {
47 in6_u: linux_raw_sys::net::in6_addr__bindgen_ty_1 {
48 u6_addr8: v6.ip().octets(),
49 },
50 },
51 sin6_scope_id: v6.scope_id(),
52 }
53}
54
55unsafe fn write_sockaddr_v6(v6: &SocketAddrV6, storage: *mut SocketAddrStorage) -> usize {
56 let encoded: sockaddr_in6 = encode_sockaddr_v6(v6);
57 core::ptr::write(dst:storage.cast(), src:encoded);
58 size_of::<c::sockaddr_in6>()
59}
60
61unsafe fn write_sockaddr_unix(unix: &SocketAddrUnix, storage: *mut SocketAddrStorage) -> usize {
62 core::ptr::write(dst:storage.cast(), src:unix.unix);
63 unix.len()
64}
65
66#[cfg(target_os = "linux")]
67pub(crate) fn encode_sockaddr_xdp(xdp: &SocketAddrXdp) -> c::sockaddr_xdp {
68 c::sockaddr_xdp {
69 sxdp_family: c::AF_XDP as _,
70 sxdp_flags: xdp.flags().bits(),
71 sxdp_ifindex: xdp.interface_index(),
72 sxdp_queue_id: xdp.queue_id(),
73 sxdp_shared_umem_fd: xdp.shared_umem_fd(),
74 }
75}
76
77#[cfg(target_os = "linux")]
78unsafe fn write_sockaddr_xdp(xdp: &SocketAddrXdp, storage: *mut SocketAddrStorage) -> usize {
79 let encoded: sockaddr_xdp = encode_sockaddr_xdp(xdp);
80 core::ptr::write(dst:storage.cast(), src:encoded);
81 size_of::<c::sockaddr_xdp>()
82}
83