1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use rustix::net::{AddressFamily, SocketAddrUnix, SocketType};
use crate::{context::Context, net::socket::Socket, state::State};
/// A Unix datagram socket.
#[must_use]
pub struct UnixDatagram {
socket: Socket,
}
impl UnixDatagram {
/// Binds on a given address.
///
/// # Errors
///
/// Returns an error if a syscall fails.
pub fn bind(addr: &SocketAddrUnix) -> rustix::io::Result<Self> {
let socket = Socket::new(AddressFamily::UNIX, SocketType::DGRAM)?;
socket.bind(addr)?;
Ok(Self { socket })
}
/// Creates a socket that is not bound to any address.
///
/// # Errors
///
/// Returns an error if a syscall fails.
pub fn unbound() -> rustix::io::Result<Self> {
let socket = Socket::new(AddressFamily::UNIX, SocketType::DGRAM)?;
Ok(Self { socket })
}
/// Sends a given buffer to a given address.
///
/// # Errors
///
/// Returns an error if a syscall fails.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub fn send_to<
'a,
S: State,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
buf: &'a [u8],
addr: &'a SocketAddrUnix,
) -> impl Future<Output = rustix::io::Result<usize>> {
self.socket.send_to(ctx, buf, addr)
}
/// Receives a datagram from any sender into a given buffer,
/// returns its size and the sender address.
///
/// # Errors
///
/// Returns an error if a syscall fails.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub fn recv_from<
'a,
S: State,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
buf: &'a mut [u8],
) -> impl Future<Output = rustix::io::Result<(usize, SocketAddrUnix)>> {
let recv = self.socket.recv_from(ctx, buf);
async move {
let (len, addr) = recv.await?;
let addr = match addr {
Some(addr) => addr.try_into()?,
None => SocketAddrUnix::new("")?,
};
Ok((len, addr))
}
}
/// Connects to a given address, which makes it the peer of [`send`](Self::send) and [`recv`](Self::recv).
///
/// # Errors
///
/// Returns an error if a syscall fails.
pub fn connect(&self, addr: &SocketAddrUnix) -> rustix::io::Result<()> {
self.socket.connect(addr)
}
/// Sends a given buffer to the connected peer, see [`connect`](Self::connect).
///
/// # Errors
///
/// Returns an error if a syscall fails.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub fn send<
'a,
S: State,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
buf: &'a [u8],
) -> impl Future<Output = rustix::io::Result<usize>> {
self.socket.send(ctx, buf)
}
/// Receives a datagram from the connected peer into a given buffer, see [`connect`](Self::connect).
///
/// # Errors
///
/// Returns an error if a syscall fails.
///
/// # Cancel safety
///
/// This method is cancel safe.
pub fn recv<
'a,
S: State,
const TASKS: usize,
const TASK_SIZE: usize,
const ROOT_TASK_SIZE: usize,
const TIMERS: usize,
const FDS: usize,
>(
&'a mut self,
ctx: &'a Context<'_, S, TASKS, TASK_SIZE, ROOT_TASK_SIZE, TIMERS, FDS>,
buf: &'a mut [u8],
) -> impl Future<Output = rustix::io::Result<usize>> {
self.socket.recv(ctx, buf)
}
/// Returns local address.
///
/// # Errors
///
/// Returns an error if a syscall fails.
pub fn local_addr(&self) -> rustix::io::Result<SocketAddrUnix> {
self.socket.local_addr()
}
}
impl core::fmt::Debug for UnixDatagram {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("UnixDatagram")
}
}