Skip to main content

dope_core/io/socket/raw/
inet.rs

1use std::net;
2
3use crate::backend;
4
5#[derive(Clone, Copy)]
6#[repr(C)]
7union InetStorage {
8    v4: libc::sockaddr_in,
9    v6: libc::sockaddr_in6,
10}
11
12#[derive(Clone, Copy)]
13pub struct Inet {
14    storage: InetStorage,
15    len: libc::socklen_t,
16}
17
18impl Inet {
19    pub fn from_std(addr: net::SocketAddr) -> Self {
20        match addr {
21            net::SocketAddr::V4(v4) => Self {
22                storage: InetStorage {
23                    v4: <backend::Backend as backend::Socket>::encode_v4(v4),
24                },
25                len: size_of::<libc::sockaddr_in>() as libc::socklen_t,
26            },
27            net::SocketAddr::V6(v6) => Self {
28                storage: InetStorage {
29                    v6: <backend::Backend as backend::Socket>::encode_v6(v6),
30                },
31                len: size_of::<libc::sockaddr_in6>() as libc::socklen_t,
32            },
33        }
34    }
35
36    pub fn mut_ptr(&mut self) -> *mut libc::sockaddr {
37        &raw mut self.storage as *mut libc::sockaddr
38    }
39
40    pub fn ptr(&self) -> *const libc::sockaddr {
41        &raw const self.storage as *const libc::sockaddr
42    }
43
44    pub fn socklen(&self) -> libc::socklen_t {
45        self.len
46    }
47}