| 1 |
use crate::backend::c; |
| 2 |
|
| 3 |
/// The windows `sockaddr_in6` type is a union with accessor functions which |
| 4 |
/// are not `const fn`. Define our own layout-compatible version so that we |
| 5 |
/// can transmute in and out of it. |
| 6 |
#[cfg(windows)] |
| 7 |
#[repr(C)] |
| 8 |
struct sockaddr_in6 { |
| 9 |
sin6_family: u16, |
| 10 |
sin6_port: u16, |
| 11 |
sin6_flowinfo: u32, |
| 12 |
sin6_addr: c::in6_addr, |
| 13 |
sin6_scope_id: u32, |
| 14 |
} |
| 15 |
|
| 16 |
#[cfg(not(windows))] |
| 17 |
#[inline] |
| 18 |
pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 { |
| 19 |
addr.s_addr |
| 20 |
} |
| 21 |
|
| 22 |
#[cfg(windows)] |
| 23 |
#[inline] |
| 24 |
pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 { |
| 25 |
// This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`. |
| 26 |
unsafe { core::mem::transmute(addr) } |
| 27 |
} |
| 28 |
|
| 29 |
#[cfg(not(windows))] |
| 30 |
#[inline] |
| 31 |
pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr { |
| 32 |
c::in_addr { s_addr } |
| 33 |
} |
| 34 |
|
| 35 |
#[cfg(windows)] |
| 36 |
#[inline] |
| 37 |
pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr { |
| 38 |
unsafe { core::mem::transmute(s_addr) } |
| 39 |
} |
| 40 |
|
| 41 |
#[cfg(not(windows))] |
| 42 |
#[inline] |
| 43 |
pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] { |
| 44 |
addr.s6_addr |
| 45 |
} |
| 46 |
|
| 47 |
#[cfg(windows)] |
| 48 |
#[inline] |
| 49 |
pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] { |
| 50 |
unsafe { core::mem::transmute(addr) } |
| 51 |
} |
| 52 |
|
| 53 |
#[cfg(not(windows))] |
| 54 |
#[inline] |
| 55 |
pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr { |
| 56 |
c::in6_addr { s6_addr } |
| 57 |
} |
| 58 |
|
| 59 |
#[cfg(windows)] |
| 60 |
#[inline] |
| 61 |
pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr { |
| 62 |
unsafe { core::mem::transmute(s6_addr) } |
| 63 |
} |
| 64 |
|
| 65 |
#[cfg(not(windows))] |
| 66 |
#[inline] |
| 67 |
pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: &c::sockaddr_in6) -> u32 { |
| 68 |
addr.sin6_scope_id |
| 69 |
} |
| 70 |
|
| 71 |
#[cfg(windows)] |
| 72 |
#[inline] |
| 73 |
pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: &c::sockaddr_in6) -> u32 { |
| 74 |
let addr: &sockaddr_in6 = unsafe { core::mem::transmute(addr) }; |
| 75 |
addr.sin6_scope_id |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg(not(windows))] |
| 79 |
#[inline] |
| 80 |
pub(crate) const fn sockaddr_in6_new( |
| 81 |
#[cfg(any( |
| 82 |
bsd, |
| 83 |
target_os = "aix", |
| 84 |
target_os = "espidf", |
| 85 |
target_os = "haiku", |
| 86 |
target_os = "nto", |
| 87 |
target_os = "vita" |
| 88 |
))] |
| 89 |
sin6_len: u8, |
| 90 |
sin6_family: c::sa_family_t, |
| 91 |
sin6_port: u16, |
| 92 |
sin6_flowinfo: u32, |
| 93 |
sin6_addr: c::in6_addr, |
| 94 |
sin6_scope_id: u32, |
| 95 |
) -> c::sockaddr_in6 { |
| 96 |
c::sockaddr_in6 { |
| 97 |
#[cfg(any( |
| 98 |
bsd, |
| 99 |
target_os = "aix", |
| 100 |
target_os = "espidf", |
| 101 |
target_os = "haiku", |
| 102 |
target_os = "nto", |
| 103 |
target_os = "vita" |
| 104 |
))] |
| 105 |
sin6_len, |
| 106 |
sin6_family, |
| 107 |
sin6_port, |
| 108 |
sin6_flowinfo, |
| 109 |
sin6_addr, |
| 110 |
sin6_scope_id, |
| 111 |
#[cfg(solarish)] |
| 112 |
__sin6_src_id: 0, |
| 113 |
#[cfg(target_os = "vita")] |
| 114 |
sin6_vport: 0, |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[cfg(windows)] |
| 119 |
#[inline] |
| 120 |
pub(crate) const fn sockaddr_in6_new( |
| 121 |
sin6_family: u16, |
| 122 |
sin6_port: u16, |
| 123 |
sin6_flowinfo: u32, |
| 124 |
sin6_addr: c::in6_addr, |
| 125 |
sin6_scope_id: u32, |
| 126 |
) -> c::sockaddr_in6 { |
| 127 |
let addr = sockaddr_in6 { |
| 128 |
sin6_family, |
| 129 |
sin6_port, |
| 130 |
sin6_flowinfo, |
| 131 |
sin6_addr, |
| 132 |
sin6_scope_id, |
| 133 |
}; |
| 134 |
unsafe { core::mem::transmute(addr) } |
| 135 |
} |