Skip to main content

dope_core/io/socket/
mod.rs

1use std::os::fd::{self, AsFd as _};
2
3pub use addr::{
4    Addr,
5    inet::{Inet, Listener},
6    prepared::{Prepared, View},
7    unix::Unix,
8};
9pub use backlog::Backlog;
10use dope_sys::{
11    Errno,
12    io::{
13        socket,
14        socket::{Socket as _, addr::Local, option::Source as _},
15    },
16};
17pub use error::Error;
18
19use crate::Backend;
20
21mod addr;
22mod backlog;
23mod error;
24
25pub mod install;
26pub mod msg;
27pub mod option;
28pub mod peer;
29pub mod pending;
30
31#[derive(Clone, Copy, PartialEq, Eq)]
32#[repr(transparent)]
33pub struct StreamSpec(socket::Domain);
34
35impl StreamSpec {
36    pub(crate) const fn domain(self) -> socket::Domain {
37        self.0
38    }
39}
40
41#[derive(Clone, Copy, PartialEq, Eq)]
42#[repr(u8)]
43pub enum Reuse {
44    Address,
45    Port,
46}
47
48#[derive(Clone, Copy)]
49pub struct Config {
50    pub reuse: Option<Reuse>,
51    pub(crate) fast_open: Option<Backlog>,
52}
53
54impl Default for Config {
55    fn default() -> Self {
56        Self {
57            reuse: Some(Reuse::Address),
58            fast_open: None,
59        }
60    }
61}
62
63impl Config {
64    pub const fn for_datagram(reuse: Option<Reuse>) -> Self {
65        Self {
66            reuse,
67            fast_open: None,
68        }
69    }
70
71    /// Builds the reuse and fast-open policy for a TCP listener.
72    /// `Some(backlog)` requires Fast Open before binding and fails creation
73    /// when the platform does not support it.
74    pub const fn for_tcp(reuse: Option<Reuse>, fast: Option<Backlog>) -> Self {
75        Self {
76            reuse,
77            fast_open: fast,
78        }
79    }
80
81    pub(crate) const fn fast_open(&self) -> Option<Backlog> {
82        self.fast_open
83    }
84
85    pub(crate) const fn has_addr(&self) -> bool {
86        matches!(self.reuse, Some(Reuse::Address | Reuse::Port))
87    }
88
89    pub(crate) const fn has_port(&self) -> bool {
90        matches!(self.reuse, Some(Reuse::Port))
91    }
92}
93
94const _: () = assert!(std::mem::size_of::<Option<Reuse>>() == std::mem::size_of::<bool>());
95
96#[repr(transparent)]
97pub(crate) struct Handle {
98    fd: <Backend as socket::Prepare>::Handle,
99}
100
101impl Handle {
102    pub(crate) const fn new(fd: <Backend as socket::Prepare>::Handle) -> Self {
103        Self { fd }
104    }
105
106    pub(crate) const fn owned(&self) -> &<Backend as socket::Prepare>::Handle {
107        &self.fd
108    }
109
110    pub(crate) fn into_inner(self) -> <Backend as socket::Prepare>::Handle {
111        self.fd
112    }
113
114    pub(crate) fn bind(&self, addr: &impl Prepared) -> Result<(), Errno> {
115        addr.view()
116            .native()
117            .try_bind::<Backend, Errno>(self.fd.as_fd())
118    }
119
120    pub(crate) fn listen(&self, backlog: i32) -> Result<(), Errno> {
121        Backend::listen(self.fd.as_fd(), backlog)
122    }
123
124    pub(crate) fn try_reuse(&self, config: &Config) -> Result<(), (Reuse, Errno)> {
125        if config.has_addr() {
126            Backend::set_reuse::<Errno>(self.fd.as_fd(), socket::Reuse::Address)
127                .map_err(|error| (Reuse::Address, error))?;
128        }
129        if !config.has_port() {
130            return Ok(());
131        }
132        Backend::set_reuse::<Errno>(self.fd.as_fd(), socket::Reuse::Port)
133            .map_err(|error| (Reuse::Port, error))
134    }
135}
136
137const _: () = {
138    assert!(size_of::<Handle>() == size_of::<fd::OwnedFd>());
139    assert!(align_of::<Handle>() == align_of::<fd::OwnedFd>());
140    assert!(size_of::<Result<(), Errno>>() == size_of::<i32>());
141    assert!(size_of::<Result<(), (Reuse, Errno)>>() == 2 * size_of::<i32>());
142};
143
144#[derive(Clone, Copy, Debug)]
145pub enum BindError {
146    Socket(<Backend as socket::Socket>::Error),
147    Bind(crate::Errno),
148    Listen(crate::Errno),
149    LocalAddress(Local<Errno>),
150    Address(Error),
151}