| 1 |
//! The BSD sockets API requires us to read the `sa_family` field before we can |
| 2 |
//! interpret the rest of a `sockaddr` produced by the kernel. |
| 3 |
|
| 4 |
use super::ext::{in6_addr_new, in_addr_new, sockaddr_in6_new}; |
| 5 |
use crate::backend::c; |
| 6 |
use crate::net::{SocketAddrV4, SocketAddrV6}; |
| 7 |
|
| 8 |
pub(crate) fn encode_sockaddr_v4(v4: &SocketAddrV4) -> c::sockaddr_in { |
| 9 |
c::sockaddr_in { |
| 10 |
#[cfg(any( |
| 11 |
bsd, |
| 12 |
target_os = "aix", |
| 13 |
target_os = "espidf", |
| 14 |
target_os = "haiku", |
| 15 |
target_os = "hurd", |
| 16 |
target_os = "nto", |
| 17 |
target_os = "vita", |
| 18 |
))] |
| 19 |
sin_len: core::mem::size_of::<c::sockaddr_in>() as _, |
| 20 |
sin_family: c::AF_INET as _, |
| 21 |
sin_port: u16::to_be(v4.port()), |
| 22 |
sin_addr: in_addr_new(u32::from_ne_bytes(v4.ip().octets())), |
| 23 |
#[cfg(not(any(target_os = "haiku", target_os = "vita")))] |
| 24 |
sin_zero: [0; 8_usize], |
| 25 |
#[cfg(target_os = "haiku")] |
| 26 |
sin_zero: [0; 24_usize], |
| 27 |
#[cfg(target_os = "vita")] |
| 28 |
sin_zero: [0; 6_usize], |
| 29 |
#[cfg(target_os = "vita")] |
| 30 |
sin_vport: 0, |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
pub(crate) fn encode_sockaddr_v6(v6: &SocketAddrV6) -> c::sockaddr_in6 { |
| 35 |
#[cfg(any( |
| 36 |
bsd, |
| 37 |
target_os = "aix", |
| 38 |
target_os = "espidf", |
| 39 |
target_os = "haiku", |
| 40 |
target_os = "hurd", |
| 41 |
target_os = "nto", |
| 42 |
target_os = "vita" |
| 43 |
))] |
| 44 |
{ |
| 45 |
sockaddr_in6_new( |
| 46 |
core::mem::size_of::<c::sockaddr_in6>() as _, |
| 47 |
c::AF_INET6 as _, |
| 48 |
u16::to_be(v6.port()), |
| 49 |
u32::to_be(v6.flowinfo()), |
| 50 |
in6_addr_new(v6.ip().octets()), |
| 51 |
v6.scope_id(), |
| 52 |
) |
| 53 |
} |
| 54 |
#[cfg(not(any( |
| 55 |
bsd, |
| 56 |
target_os = "aix", |
| 57 |
target_os = "espidf", |
| 58 |
target_os = "haiku", |
| 59 |
target_os = "hurd", |
| 60 |
target_os = "nto", |
| 61 |
target_os = "vita" |
| 62 |
)))] |
| 63 |
{ |
| 64 |
sockaddr_in6_new( |
| 65 |
c::AF_INET6 as _, |
| 66 |
u16::to_be(v6.port()), |
| 67 |
u32::to_be(v6.flowinfo()), |
| 68 |
in6_addr_new(v6.ip().octets()), |
| 69 |
v6.scope_id(), |
| 70 |
) |
| 71 |
} |
| 72 |
} |