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