summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/imp/libc/net/ext.rs
blob: 7b5313c51271327b9e0d304eae3c7b4afab60e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use super::super::c;

/// The windows `sockaddr_in6` type is a union with accessor functions which
/// are not `const fn`. Define our own layout-compatible version so that we
/// can transmute in and out of it.
#[cfg(windows)]
#[repr(C)]
struct sockaddr_in6 {
    sin6_family: u16,
    sin6_port: u16,
    sin6_flowinfo: u32,
    sin6_addr: c::in6_addr,
    sin6_scope_id: u32,
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 {
    addr.s_addr
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn in_addr_s_addr(addr: c::in_addr) -> u32 {
    // This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`.
    unsafe { core::mem::transmute(addr) }
}

// TODO: With Rust 1.55, we can use the above `in_addr_s_addr` definition that
// uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn in_addr_s_addr(addr: c::in_addr) -> u32 {
    // This should be `*addr.S_un.S_addr()`, except that isn't a `const fn`.
    unsafe { core::mem::transmute(addr) }
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr {
    c::in_addr { s_addr }
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn in_addr_new(s_addr: u32) -> c::in_addr {
    unsafe { core::mem::transmute(s_addr) }
}

// TODO: With Rust 1.55, we can use the above `in_addr_new` definition that
// uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn in_addr_new(s_addr: u32) -> c::in_addr {
    unsafe { core::mem::transmute(s_addr) }
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
    addr.s6_addr
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
    unsafe { core::mem::transmute(addr) }
}

// TODO: With Rust 1.55, we can use the above `in6_addr_s6_addr` definition
// that uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn in6_addr_s6_addr(addr: c::in6_addr) -> [u8; 16] {
    unsafe { core::mem::transmute(addr) }
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
    c::in6_addr { s6_addr }
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
    unsafe { core::mem::transmute(s6_addr) }
}

// TODO: With Rust 1.55, we can use the above `in6_addr_new` definition that
// uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn in6_addr_new(s6_addr: [u8; 16]) -> c::in6_addr {
    unsafe { core::mem::transmute(s6_addr) }
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
    addr.sin6_scope_id
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
    let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) };
    addr.sin6_scope_id
}

// TODO: With Rust 1.55, we can use the above `sockaddr_in6_sin6_scope_id`
// definition that uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn sockaddr_in6_sin6_scope_id(addr: c::sockaddr_in6) -> u32 {
    let addr: sockaddr_in6 = unsafe { core::mem::transmute(addr) };
    addr.sin6_scope_id
}

#[cfg(not(feature = "std"))]
#[cfg(not(windows))]
#[inline]
pub(crate) fn sockaddr_in6_sin6_scope_id_mut(addr: &mut c::sockaddr_in6) -> &mut u32 {
    &mut addr.sin6_scope_id
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) fn sockaddr_in6_sin6_scope_id_mut(addr: &mut c::sockaddr_in6) -> &mut u32 {
    let addr: &mut sockaddr_in6 = unsafe { core::mem::transmute(addr) };
    &mut addr.sin6_scope_id
}

#[cfg(not(windows))]
#[inline]
pub(crate) const fn sockaddr_in6_new(
    #[cfg(any(
        target_os = "dragonfly",
        target_os = "freebsd",
        target_os = "ios",
        target_os = "macos",
        target_os = "netbsd",
        target_os = "openbsd",
    ))]
    sin6_len: u8,
    sin6_family: c::sa_family_t,
    sin6_port: u16,
    sin6_flowinfo: u32,
    sin6_addr: c::in6_addr,
    sin6_scope_id: u32,
) -> c::sockaddr_in6 {
    c::sockaddr_in6 {
        #[cfg(any(
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "ios",
            target_os = "macos",
            target_os = "netbsd",
            target_os = "openbsd",
        ))]
        sin6_len,
        sin6_family,
        sin6_port,
        sin6_flowinfo,
        sin6_addr,
        sin6_scope_id,
        #[cfg(target_os = "illumos")]
        __sin6_src_id: 0,
    }
}

#[cfg(not(feature = "std"))]
#[cfg(windows)]
#[inline]
pub(crate) const fn sockaddr_in6_new(
    sin6_family: u16,
    sin6_port: u16,
    sin6_flowinfo: u32,
    sin6_addr: c::in6_addr,
    sin6_scope_id: u32,
) -> c::sockaddr_in6 {
    let addr = sockaddr_in6 {
        sin6_family,
        sin6_port,
        sin6_flowinfo,
        sin6_addr,
        sin6_scope_id,
    };
    unsafe { core::mem::transmute(addr) }
}

// TODO: With Rust 1.55, we can use the above `sockaddr_in6_new` definition
// that uses a const-fn transmute.
#[cfg(feature = "std")]
#[cfg(windows)]
#[inline]
pub(crate) fn sockaddr_in6_new(
    sin6_family: u16,
    sin6_port: u16,
    sin6_flowinfo: u32,
    sin6_addr: c::in6_addr,
    sin6_scope_id: u32,
) -> c::sockaddr_in6 {
    let addr = sockaddr_in6 {
        sin6_family,
        sin6_port,
        sin6_flowinfo,
        sin6_addr,
        sin6_scope_id,
    };
    unsafe { core::mem::transmute(addr) }
}