rustix/backend/libc/net/
read_sockaddr.rs

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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! 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.

#[cfg(unix)]
use super::addr::SocketAddrUnix;
use super::ext::{in6_addr_s6_addr, in_addr_s_addr, sockaddr_in6_sin6_scope_id};
use crate::backend::c;
#[cfg(not(windows))]
use crate::ffi::CStr;
use crate::io;
#[cfg(target_os = "linux")]
use crate::net::xdp::{SockaddrXdpFlags, SocketAddrXdp};
use crate::net::{Ipv4Addr, Ipv6Addr, SocketAddrAny, SocketAddrV4, SocketAddrV6};
use core::mem::size_of;

// This must match the header of `sockaddr`.
#[repr(C)]
struct sockaddr_header {
    #[cfg(any(
        bsd,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita"
    ))]
    sa_len: u8,
    #[cfg(any(
        bsd,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita"
    ))]
    ss_family: u8,
    #[cfg(not(any(
        bsd,
        target_os = "aix",
        target_os = "espidf",
        target_os = "haiku",
        target_os = "nto",
        target_os = "vita"
    )))]
    ss_family: u16,
}

/// Read the `ss_family` field from a socket address returned from the OS.
///
/// # Safety
///
/// `storage` must point to a valid socket address returned from the OS.
#[inline]
unsafe fn read_ss_family(storage: *const c::sockaddr_storage) -> u16 {
    // Assert that we know the layout of `sockaddr`.
    let _ = c::sockaddr {
        #[cfg(any(
            bsd,
            target_os = "aix",
            target_os = "espidf",
            target_os = "haiku",
            target_os = "hurd",
            target_os = "nto",
            target_os = "vita"
        ))]
        sa_len: 0_u8,
        #[cfg(any(
            bsd,
            target_os = "aix",
            target_os = "espidf",
            target_os = "haiku",
            target_os = "hurd",
            target_os = "nto",
            target_os = "vita"
        ))]
        sa_family: 0_u8,
        #[cfg(not(any(
            bsd,
            target_os = "aix",
            target_os = "espidf",
            target_os = "haiku",
            target_os = "hurd",
            target_os = "nto",
            target_os = "vita"
        )))]
        sa_family: 0_u16,
        #[cfg(not(target_os = "haiku"))]
        sa_data: [0; 14],
        #[cfg(target_os = "haiku")]
        sa_data: [0; 30],
    };

    (*storage.cast::<sockaddr_header>()).ss_family.into()
}

/// Set the `ss_family` field of a socket address to `AF_UNSPEC`, so that we
/// can test for `AF_UNSPEC` to test whether it was stored to.
pub(crate) unsafe fn initialize_family_to_unspec(storage: *mut c::sockaddr_storage) {
    (*storage.cast::<sockaddr_header>()).ss_family = c::AF_UNSPEC as _;
}

/// Read a socket address encoded in a platform-specific format.
///
/// # Safety
///
/// `storage` must point to valid socket address storage.
pub(crate) unsafe fn read_sockaddr(
    storage: *const c::sockaddr_storage,
    len: usize,
) -> io::Result<SocketAddrAny> {
    #[cfg(unix)]
    let offsetof_sun_path = super::addr::offsetof_sun_path();

    if len < size_of::<c::sa_family_t>() {
        return Err(io::Errno::INVAL);
    }
    match read_ss_family(storage).into() {
        c::AF_INET => {
            if len < size_of::<c::sockaddr_in>() {
                return Err(io::Errno::INVAL);
            }
            let decode = &*storage.cast::<c::sockaddr_in>();
            Ok(SocketAddrAny::V4(SocketAddrV4::new(
                Ipv4Addr::from(u32::from_be(in_addr_s_addr(decode.sin_addr))),
                u16::from_be(decode.sin_port),
            )))
        }
        c::AF_INET6 => {
            if len < size_of::<c::sockaddr_in6>() {
                return Err(io::Errno::INVAL);
            }
            let decode = &*storage.cast::<c::sockaddr_in6>();
            #[cfg(not(windows))]
            let s6_addr = decode.sin6_addr.s6_addr;
            #[cfg(windows)]
            let s6_addr = decode.sin6_addr.u.Byte;
            #[cfg(not(windows))]
            let sin6_scope_id = decode.sin6_scope_id;
            #[cfg(windows)]
            let sin6_scope_id = decode.Anonymous.sin6_scope_id;
            Ok(SocketAddrAny::V6(SocketAddrV6::new(
                Ipv6Addr::from(s6_addr),
                u16::from_be(decode.sin6_port),
                u32::from_be(decode.sin6_flowinfo),
                sin6_scope_id,
            )))
        }
        #[cfg(unix)]
        c::AF_UNIX => {
            if len < offsetof_sun_path {
                return Err(io::Errno::INVAL);
            }
            if len == offsetof_sun_path {
                SocketAddrUnix::new(&[][..]).map(SocketAddrAny::Unix)
            } else {
                let decode = &*storage.cast::<c::sockaddr_un>();

                // On Linux check for Linux's [abstract namespace].
                //
                // [abstract namespace]: https://man7.org/linux/man-pages/man7/unix.7.html
                #[cfg(linux_kernel)]
                if decode.sun_path[0] == 0 {
                    return SocketAddrUnix::new_abstract_name(core::mem::transmute::<
                        &[c::c_char],
                        &[u8],
                    >(
                        &decode.sun_path[1..len - offsetof_sun_path],
                    ))
                    .map(SocketAddrAny::Unix);
                }

                // Otherwise we expect a NUL-terminated filesystem path.

                // Trim off unused bytes from the end of `path_bytes`.
                let path_bytes = if cfg!(any(solarish, target_os = "freebsd")) {
                    // FreeBSD and illumos sometimes set the length to longer
                    // than the length of the NUL-terminated string. Find the
                    // NUL and truncate the string accordingly.
                    &decode.sun_path[..decode
                        .sun_path
                        .iter()
                        .position(|b| *b == 0)
                        .ok_or(io::Errno::INVAL)?]
                } else {
                    // Otherwise, use the provided length.
                    let provided_len = len - 1 - offsetof_sun_path;
                    if decode.sun_path[provided_len] != 0 {
                        return Err(io::Errno::INVAL);
                    }
                    debug_assert_eq!(
                        CStr::from_ptr(decode.sun_path.as_ptr().cast())
                            .to_bytes()
                            .len(),
                        provided_len
                    );
                    &decode.sun_path[..provided_len]
                };

                SocketAddrUnix::new(core::mem::transmute::<&[c::c_char], &[u8]>(path_bytes))
                    .map(SocketAddrAny::Unix)
            }
        }
        #[cfg(target_os = "linux")]
        c::AF_XDP => {
            if len < size_of::<c::sockaddr_xdp>() {
                return Err(io::Errno::INVAL);
            }
            let decode = &*storage.cast::<c::sockaddr_xdp>();
            Ok(SocketAddrAny::Xdp(SocketAddrXdp::new(
                SockaddrXdpFlags::from_bits_retain(decode.sxdp_flags),
                u32::from_be(decode.sxdp_ifindex),
                u32::from_be(decode.sxdp_queue_id),
                u32::from_be(decode.sxdp_shared_umem_fd),
            )))
        }
        _ => Err(io::Errno::INVAL),
    }
}

/// Read an optional socket address returned from the OS.
///
/// # Safety
///
/// `storage` must point to a valid socket address returned from the OS.
pub(crate) unsafe fn maybe_read_sockaddr_os(
    storage: *const c::sockaddr_storage,
    len: usize,
) -> Option<SocketAddrAny> {
    if len == 0 {
        return None;
    }

    assert!(len >= size_of::<c::sa_family_t>());
    let family = read_ss_family(storage).into();
    if family == c::AF_UNSPEC {
        None
    } else {
        Some(inner_read_sockaddr_os(family, storage, len))
    }
}

/// Read a socket address returned from the OS.
///
/// # Safety
///
/// `storage` must point to a valid socket address returned from the OS.
pub(crate) unsafe fn read_sockaddr_os(
    storage: *const c::sockaddr_storage,
    len: usize,
) -> SocketAddrAny {
    assert!(len >= size_of::<c::sa_family_t>());
    let family = read_ss_family(storage).into();
    inner_read_sockaddr_os(family, storage, len)
}

unsafe fn inner_read_sockaddr_os(
    family: c::c_int,
    storage: *const c::sockaddr_storage,
    len: usize,
) -> SocketAddrAny {
    #[cfg(unix)]
    let offsetof_sun_path = super::addr::offsetof_sun_path();

    assert!(len >= size_of::<c::sa_family_t>());
    match family {
        c::AF_INET => {
            assert!(len >= size_of::<c::sockaddr_in>());
            let decode = &*storage.cast::<c::sockaddr_in>();
            SocketAddrAny::V4(SocketAddrV4::new(
                Ipv4Addr::from(u32::from_be(in_addr_s_addr(decode.sin_addr))),
                u16::from_be(decode.sin_port),
            ))
        }
        c::AF_INET6 => {
            assert!(len >= size_of::<c::sockaddr_in6>());
            let decode = &*storage.cast::<c::sockaddr_in6>();
            SocketAddrAny::V6(SocketAddrV6::new(
                Ipv6Addr::from(in6_addr_s6_addr(decode.sin6_addr)),
                u16::from_be(decode.sin6_port),
                u32::from_be(decode.sin6_flowinfo),
                sockaddr_in6_sin6_scope_id(decode),
            ))
        }
        #[cfg(unix)]
        c::AF_UNIX => {
            assert!(len >= offsetof_sun_path);
            if len == offsetof_sun_path {
                SocketAddrAny::Unix(SocketAddrUnix::new(&[][..]).unwrap())
            } else {
                let decode = &*storage.cast::<c::sockaddr_un>();

                // On Linux check for Linux's [abstract namespace].
                //
                // [abstract namespace]: https://man7.org/linux/man-pages/man7/unix.7.html
                #[cfg(linux_kernel)]
                if decode.sun_path[0] == 0 {
                    return SocketAddrAny::Unix(
                        SocketAddrUnix::new_abstract_name(core::mem::transmute::<
                            &[c::c_char],
                            &[u8],
                        >(
                            &decode.sun_path[1..len - offsetof_sun_path],
                        ))
                        .unwrap(),
                    );
                }

                // Otherwise we expect a NUL-terminated filesystem path.
                assert_eq!(decode.sun_path[len - 1 - offsetof_sun_path], 0);
                let path_bytes = &decode.sun_path[..len - 1 - offsetof_sun_path];

                // FreeBSD and illumos sometimes set the length to longer than
                // the length of the NUL-terminated string. Find the NUL and
                // truncate the string accordingly.
                #[cfg(any(solarish, target_os = "freebsd"))]
                let path_bytes = &path_bytes[..path_bytes.iter().position(|b| *b == 0).unwrap()];

                SocketAddrAny::Unix(
                    SocketAddrUnix::new(core::mem::transmute::<&[c::c_char], &[u8]>(path_bytes))
                        .unwrap(),
                )
            }
        }
        #[cfg(target_os = "linux")]
        c::AF_XDP => {
            assert!(len >= size_of::<c::sockaddr_xdp>());
            let decode = &*storage.cast::<c::sockaddr_xdp>();
            SocketAddrAny::Xdp(SocketAddrXdp::new(
                SockaddrXdpFlags::from_bits_retain(decode.sxdp_flags),
                u32::from_be(decode.sxdp_ifindex),
                u32::from_be(decode.sxdp_queue_id),
                u32::from_be(decode.sxdp_shared_umem_fd),
            ))
        }
        other => unimplemented!("{:?}", other),
    }
}