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