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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//! Network operations (accept, send, recv) for the Ring.
use super::Ring;
use crate::error::{Result, SaferRingError};
use crate::future::{AcceptFuture, RecvFuture, SendFuture};
use crate::operation::{Building, Operation, OperationType};
use std::os::unix::io::RawFd;
use std::pin::Pin;
impl<'ring> Ring<'ring> {
/// Accept a connection on a listening socket.
///
/// This method accepts an incoming connection on a listening socket and
/// returns a future that resolves to the new client file descriptor.
///
/// # Arguments
///
/// * `listening_fd` - File descriptor of the listening socket
///
/// # Returns
///
/// Returns an AcceptFuture that resolves to the new client file descriptor.
///
/// # Errors
///
/// Returns an error if the operation cannot be submitted.
///
/// # Security Considerations
///
/// **IMPORTANT**: The caller is responsible for ensuring the file descriptor is:
/// - A valid socket file descriptor in listening state
/// - Owned by the current process with appropriate permissions
/// - Properly bound to the intended network interface and port
/// - Configured with appropriate socket options (e.g., SO_REUSEADDR)
/// - Not subject to race conditions from other threads
///
/// This library does **NOT** perform socket validation, permission checks,
/// or network security controls. Using an invalid or malicious file descriptor
/// may result in:
/// - Accepting connections on unintended sockets or ports
/// - Security vulnerabilities or unauthorized network access
/// - System errors or crashes
///
/// Always validate socket file descriptors and implement proper network
/// security controls at the application level.
///
/// # Example
///
/// ```rust,ignore
/// # use safer_ring::Ring;
/// # use std::os::unix::io::RawFd;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut ring = Ring::new(32)?;
/// let listening_fd: RawFd = 3; // Assume we have a listening socket
///
/// let client_fd = ring.accept(listening_fd)?.await?;
/// println!("Accepted connection: fd {}", client_fd);
/// # Ok(())
/// # }
/// ```
pub fn accept(&'ring mut self, fd: std::os::unix::io::RawFd) -> Result<AcceptFuture<'ring>> {
let operation = Operation::accept().fd(fd);
self.submit_accept(operation)
}
/// Send data on a socket.
///
/// ⚠️ **NOT RECOMMENDED**: This API is fundamentally limited and should not be used in application code.
/// **Always use [`OwnedBuffer`](crate::OwnedBuffer) with the ownership transfer pattern instead.**
///
/// This pin-based API returns a `Future` that holds a mutable borrow of the `Ring`.
/// Due to Rust's lifetime rules, this makes it **impossible to use this method in a loop**
/// or for multiple concurrent operations on the same `Ring`, as the borrow checker will
/// prevent subsequent calls.
///
/// This method exists for educational purposes and for building low-level abstractions.
/// For all practical application logic, use the "hot potato" pattern with [`OwnedBuffer`](crate::OwnedBuffer).
///
/// This method sends data from a buffer to a connected socket and
/// returns a future that resolves to the number of bytes sent and
/// the buffer ownership.
///
/// # Arguments
///
/// * `socket_fd` - File descriptor of the connected socket
/// * `buffer` - Pinned buffer containing data to send
///
/// # Returns
///
/// Returns a SendFuture that resolves to (bytes_sent, buffer).
///
/// # Errors
///
/// Returns an error if the operation cannot be submitted.
///
/// # Security Considerations
///
/// **IMPORTANT**: The caller is responsible for ensuring the file descriptor is:
/// - A valid, connected socket file descriptor
/// - Owned by the current process with appropriate send permissions
/// - Connected to the intended remote endpoint
/// - Not subject to race conditions from other threads
/// - Properly authenticated and authorized for data transmission
///
/// The caller must also ensure the buffer contains only authorized data:
/// - No sensitive information that should not be transmitted
/// - Properly validated and sanitized content
/// - Data appropriate for the connected peer
///
/// This library does **NOT** perform socket validation, connection verification,
/// data filtering, or access control. Using an invalid file descriptor or
/// sending unauthorized data may result in:
/// - Data transmission to unintended recipients
/// - Information disclosure or data leaks
/// - Security vulnerabilities or protocol violations
/// - System errors or network failures
///
/// Always implement proper network security controls and data validation.
///
/// # Example
///
/// ```rust,ignore
/// # use safer_ring::{Ring, PinnedBuffer};
/// # use std::os::unix::io::RawFd;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut ring = Ring::new(32)?;
/// let socket_fd: RawFd = 4; // Assume we have a connected socket
/// let mut buffer = PinnedBuffer::from_slice(b"Hello, client!");
///
/// let (bytes_sent, buffer) = ring.send(socket_fd, buffer.as_mut_slice())?.await?;
/// println!("Sent {} bytes", bytes_sent);
/// # Ok(())
/// # }
/// ```
pub fn send<'buf>(
&'ring mut self,
fd: RawFd,
buffer: Pin<&'buf mut [u8]>,
) -> Result<SendFuture<'ring, 'buf>>
where
'buf: 'ring,
{
let operation = Operation::send().fd(fd).buffer(buffer);
self.submit_send(operation)
}
/// Receive data from a socket.
///
/// ⚠️ **NOT RECOMMENDED**: This API is fundamentally limited and should not be used in application code.
/// **Always use [`OwnedBuffer`](crate::OwnedBuffer) with the ownership transfer pattern instead.**
///
/// This pin-based API returns a `Future` that holds a mutable borrow of the `Ring`.
/// Due to Rust's lifetime rules, this makes it **impossible to use this method in a loop**
/// or for multiple concurrent operations on the same `Ring`, as the borrow checker will
/// prevent subsequent calls.
///
/// This method exists for educational purposes and for building low-level abstractions.
/// For all practical application logic, use the "hot potato" pattern with [`OwnedBuffer`](crate::OwnedBuffer).
///
/// This method receives data from a connected socket into a buffer and
/// returns a future that resolves to the number of bytes received and
/// the buffer ownership.
///
/// # Arguments
///
/// * `socket_fd` - File descriptor of the connected socket
/// * `buffer` - Pinned buffer to receive data into
///
/// # Returns
///
/// Returns a RecvFuture that resolves to (bytes_received, buffer).
///
/// # Errors
///
/// Returns an error if the operation cannot be submitted.
///
/// # Security Considerations
///
/// **IMPORTANT**: The caller is responsible for ensuring the file descriptor is:
/// - A valid, connected socket file descriptor
/// - Owned by the current process with appropriate receive permissions
/// - Connected to a trusted or properly authenticated remote endpoint
/// - Not subject to race conditions from other threads
/// - Properly configured for the expected protocol and data format
///
/// After receiving data, the caller must:
/// - Validate all received data before processing
/// - Implement proper input sanitization and bounds checking
/// - Handle untrusted data appropriately for the application protocol
/// - Consider data confidentiality and integrity requirements
///
/// This library does **NOT** perform socket validation, connection verification,
/// data validation, or content filtering. Using an invalid file descriptor or
/// processing untrusted received data may result in:
/// - Buffer overflow or memory corruption vulnerabilities
/// - Injection attacks or protocol exploitation
/// - Information disclosure or data corruption
/// - System compromise or privilege escalation
///
/// Always implement proper input validation and network security controls.
///
/// # Example
///
/// ```rust,ignore
/// # use safer_ring::{Ring, PinnedBuffer};
/// # use std::os::unix::io::RawFd;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let mut ring = Ring::new(32)?;
/// let socket_fd: RawFd = 4; // Assume we have a connected socket
/// let mut buffer = PinnedBuffer::with_capacity(1024);
///
/// let (bytes_received, buffer) = ring.recv(socket_fd, buffer.as_mut_slice())?.await?;
/// println!("Received {} bytes", bytes_received);
/// # Ok(())
/// # }
/// ```
pub fn recv<'buf>(
&'ring mut self,
socket_fd: std::os::unix::io::RawFd,
buffer: std::pin::Pin<&'buf mut [u8]>,
) -> Result<RecvFuture<'ring, 'buf>>
where
'buf: 'ring,
{
let operation = Operation::recv().fd(socket_fd).buffer(buffer);
self.submit_recv(operation)
}
/// Submit an accept operation and return a future.
///
/// This is a convenience method that combines operation submission with
/// future creation for accept operations.
///
/// # Arguments
///
/// * `operation` - Accept operation in Building state
///
/// # Returns
///
/// Returns an AcceptFuture that can be awaited to get the client fd.
///
/// # Errors
///
/// Returns an error if operation submission fails.
pub fn submit_accept(
&'ring mut self,
operation: Operation<'ring, 'static, Building>,
) -> Result<AcceptFuture<'ring>> {
// Validate that this is actually an accept operation
if operation.get_type() != OperationType::Accept {
return Err(SaferRingError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Operation must be an accept operation",
)));
}
let submitted = self.submit(operation)?;
Ok(AcceptFuture::new(
submitted,
self,
self.waker_registry.clone(),
))
}
/// Submit a send operation and return a future.
///
/// This is a convenience method that combines operation submission with
/// future creation for send operations.
///
/// # Arguments
///
/// * `operation` - Send operation in Building state
///
/// # Returns
///
/// Returns a SendFuture that can be awaited to get (bytes_sent, buffer).
///
/// # Errors
///
/// Returns an error if operation submission fails.
pub fn submit_send<'buf>(
&'ring mut self,
operation: Operation<'ring, 'buf, Building>,
) -> Result<SendFuture<'ring, 'buf>>
where
'buf: 'ring,
{
// Validate that this is actually a send operation
if operation.get_type() != OperationType::Send {
return Err(SaferRingError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Operation must be a send operation",
)));
}
let submitted = self.submit(operation)?;
Ok(SendFuture::new(
submitted,
self,
self.waker_registry.clone(),
))
}
/// Submit a receive operation and return a future.
///
/// This is a convenience method that combines operation submission with
/// future creation for receive operations.
///
/// # Arguments
///
/// * `operation` - Receive operation in Building state
///
/// # Returns
///
/// Returns a RecvFuture that can be awaited to get (bytes_received, buffer).
///
/// # Errors
///
/// Returns an error if operation submission fails.
pub fn submit_recv<'buf>(
&'ring mut self,
operation: Operation<'ring, 'buf, Building>,
) -> Result<RecvFuture<'ring, 'buf>>
where
'buf: 'ring,
{
// Validate that this is actually a receive operation
if operation.get_type() != OperationType::Recv {
return Err(SaferRingError::Io(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Operation must be a receive operation",
)));
}
let submitted = self.submit(operation)?;
Ok(RecvFuture::new(
submitted,
self,
self.waker_registry.clone(),
))
}
}