Skip to main content

dope_core/io/socket/
mod.rs

1pub mod addr;
2pub mod msg;
3
4use core::mem::MaybeUninit;
5use std::net::SocketAddr;
6
7#[derive(Clone, Copy, Debug)]
8pub struct ListenerConfig {
9    pub reuse_addr: bool,
10    pub reuse_port: bool,
11    pub fast_open_backlog: Option<u32>,
12    pub defer_accept_secs: Option<u32>,
13}
14
15impl Default for ListenerConfig {
16    fn default() -> Self {
17        Self {
18            reuse_addr: true,
19            reuse_port: false,
20            fast_open_backlog: None,
21            defer_accept_secs: None,
22        }
23    }
24}
25
26impl ListenerConfig {
27    pub fn for_datagram(addr: &SocketAddr) -> Self {
28        let reuse = addr.port() != 0;
29        Self {
30            reuse_addr: reuse,
31            reuse_port: reuse,
32            ..Self::default()
33        }
34    }
35}
36
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum Domain {
39    Inet4,
40    Inet6,
41}
42
43impl Domain {
44    pub fn for_addr(addr: &SocketAddr) -> Self {
45        match addr {
46            SocketAddr::V4(_) => Self::Inet4,
47            SocketAddr::V6(_) => Self::Inet6,
48        }
49    }
50
51    pub const fn raw(self) -> libc::c_int {
52        match self {
53            Self::Inet4 => libc::AF_INET,
54            Self::Inet6 => libc::AF_INET6,
55        }
56    }
57}
58
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60pub enum Kind {
61    Stream,
62    Dgram,
63}
64
65impl Kind {
66    pub const fn raw(self) -> libc::c_int {
67        match self {
68            Self::Stream => libc::SOCK_STREAM,
69            Self::Dgram => libc::SOCK_DGRAM,
70        }
71    }
72}
73
74/// # Safety
75/// Implementors must be valid when every byte is zero.
76pub(crate) unsafe trait Pod: Sized {
77    fn zeroed() -> Self {
78        unsafe { MaybeUninit::<Self>::zeroed().assume_init() }
79    }
80}
81
82unsafe impl Pod for libc::sockaddr_in {}
83unsafe impl Pod for libc::sockaddr_in6 {}
84unsafe impl Pod for libc::sockaddr_un {}
85unsafe impl Pod for libc::sockaddr_storage {}