cap_net_ext/lib.rs
1//! Extension traits for `TcpListener`, `UdpSocket`, and `Pool`.
2//!
3//! cap-std's [`TcpListener`], following the Rust standard library
4//! `TcpListener`, combines the `socket`, `bind`, `listen`, and `connect`
5//! operations of the POSIX socket API into a single `bind` or `connect`
6//! operation. In some use cases, it's desirable to perform the steps
7//! separately.
8//!
9//! This API adds extension traits to cap-std's `TcpListener`, `UdpSocket`,
10//! and `Pool` which support the following sequence for accepting incoming
11//! connections:
12//!
13//! - [`TcpListenerExt::new`] performs a `socket` and returns a new
14//! `TcpListener` that is not yet bound.
15//! - [`Pool::bind_existing_tcp_listener`] performs a `bind`, checking that
16//! the address is in the `Pool`.
17//! - [`TcpListenerExt::listen`] performs a `listen`.
18//! - Then, the regular [`TcpListener::accept`] may be used to accept new
19//! connections. Alternatively, [`TcpListener::accept_with`] may be used.
20//!
21//! and the following sequence for initiating outgoing connections:
22//!
23//! - [`TcpListenerExt::new`] performs a `socket` and returns a new
24//! `TcpListener` that is not yet connected.
25//! - [`Pool::connect_into_tcp_stream`] performs a `connect`, checking that
26//! the address is in the `Pool`.
27//!
28//! [`TcpListenerExt::new`] and [`TcpListener::accept_with`] additionally
29//! have [`Blocking`] arguments for requesting non-blocking operation.
30//!
31//! Similar API adaptations are available for UDP sockets as well.
32
33#![deny(missing_docs)]
34#![forbid(unsafe_code)]
35#![doc(
36 html_logo_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.svg"
37)]
38#![doc(
39 html_favicon_url = "https://raw.githubusercontent.com/bytecodealliance/cap-std/main/media/cap-std.ico"
40)]
41
42use cap_primitives::net::no_socket_addrs;
43use cap_std::net::{IpAddr, Pool, SocketAddr, TcpListener, TcpStream, ToSocketAddrs, UdpSocket};
44use rustix::fd::OwnedFd;
45use std::io;
46
47/// Address families supported by [`TcpListenerExt::new`] and
48/// [`UdpSocketExt::new`].
49#[derive(Copy, Clone, Debug, Eq, PartialEq)]
50pub enum AddressFamily {
51 /// IPv4
52 Ipv4,
53
54 /// IPv6
55 Ipv6,
56}
57
58impl AddressFamily {
59 /// Return the `AddressFamily` of an IP address.
60 pub fn of_ip_addr(ip_addr: IpAddr) -> Self {
61 match ip_addr {
62 IpAddr::V4(_) => AddressFamily::Ipv4,
63 IpAddr::V6(_) => AddressFamily::Ipv6,
64 }
65 }
66
67 /// Return the `AddressFamily` of a socket address.
68 pub fn of_socket_addr(socket_addr: SocketAddr) -> Self {
69 match socket_addr {
70 SocketAddr::V4(_) => AddressFamily::Ipv4,
71 SocketAddr::V6(_) => AddressFamily::Ipv6,
72 }
73 }
74}
75
76impl From<AddressFamily> for rustix::net::AddressFamily {
77 fn from(address_family: AddressFamily) -> Self {
78 match address_family {
79 AddressFamily::Ipv4 => rustix::net::AddressFamily::INET,
80 AddressFamily::Ipv6 => rustix::net::AddressFamily::INET6,
81 }
82 }
83}
84
85/// Select blocking or non-blocking mode.
86#[derive(Copy, Clone, Debug, Eq, PartialEq)]
87pub enum Blocking {
88 /// Non-blocking
89 No,
90
91 /// Blocking
92 Yes,
93}
94
95/// A trait for extending `TcpListener` types.
96pub trait TcpListenerExt: private::Sealed + Sized {
97 /// Creates a new TCP socket with the given address family.
98 ///
99 /// The created socket is not bound or connected to any address and may be
100 /// used for either listening or connecting. Use
101 /// [`PoolExt::bind_existing_tcp_listener`] to bind it in preparation for
102 /// listening, or [`PoolExt::connect_into_tcp_stream`] to initiate a
103 /// connection.
104 ///
105 /// This is similar to [`Pool::bind_tcp_listener`] in that it creates a TCP
106 /// socket, however it does not perform the `bind` or `listen` steps. And,
107 /// it has a `blocking` argument to select blocking or non-blocking mode
108 /// for the created socket.
109 ///
110 /// And it's similar to [`Pool::connect_tcp_stream`] in that it creates a
111 /// TCP socket, however it does not perform the `connect` step. And, it has
112 /// a `blocking` argument to select blocking or non-blocking mode for the
113 /// created socket.
114 fn new(address_family: AddressFamily, blocking: Blocking) -> io::Result<Self>;
115
116 /// Enble listening in a `TcpListener`.
117 ///
118 /// A newly-created [`TcpListener`] created with [`TcpListenerExt::new`]
119 /// and bound with [`PoolExt::bind_existing_tcp_listener`] is not yet
120 /// listening; this function enables listening. After this, the listener
121 /// may accept new connections with [`accept`] or [`accept_with`].
122 ///
123 /// This is similar to [`Pool::bind_tcp_listener`] in that it performs the
124 /// `listen` step, however it does not create the socket itself, or bind
125 /// it.
126 ///
127 /// The `backlog` argument specifies an optional hint to the implementation
128 /// about how many connections can be waiting before new connections are
129 /// refused or ignored.
130 ///
131 /// [`accept`]: TcpListener::accept
132 /// [`accept_with`]: TcpListenerExt::accept_with
133 fn listen(&self, backlog: Option<i32>) -> io::Result<()>;
134
135 /// Similar to [`accept`], but the resulting TCP connection are optionally
136 /// set to non-blocking mode.
137 ///
138 /// The `accept` call itself may still block, if the socket is in blocking
139 /// mode.
140 ///
141 /// [`accept`]: TcpListener::accept
142 fn accept_with(&self, blocking: Blocking) -> io::Result<(TcpStream, SocketAddr)>;
143}
144
145impl TcpListenerExt for TcpListener {
146 fn new(address_family: AddressFamily, blocking: Blocking) -> io::Result<Self> {
147 socket(address_family, blocking, rustix::net::SocketType::STREAM).map(Self::from)
148 }
149
150 fn listen(&self, backlog: Option<i32>) -> io::Result<()> {
151 let backlog = backlog.unwrap_or_else(default_backlog);
152
153 Ok(rustix::net::listen(self, backlog)?)
154 }
155
156 fn accept_with(&self, blocking: Blocking) -> io::Result<(TcpStream, SocketAddr)> {
157 let (stream, addr) = rustix::net::acceptfrom_with(self, socket_flags(blocking))?;
158 set_socket_flags(&stream, blocking)?;
159
160 // We know have a TCP socket, so we know we'll get an IP address.
161 let addr = SocketAddr::try_from(addr.unwrap()).unwrap();
162
163 Ok((TcpStream::from(stream), addr))
164 }
165}
166
167/// A trait for extending `UdpSocket` types.
168pub trait UdpSocketExt: private::Sealed + Sized {
169 /// Creates a new `UdpSocket` with the given address family.
170 ///
171 /// The created socket is initially not bound or connected to any address.
172 /// Use [`PoolExt::bind_existing_udp_socket`] to bind it, or
173 /// [`PoolExt::connect_existing_udp_socket`] to initiate a connection.
174 ///
175 /// This is similar to [`Pool::bind_udp_socket`] in that it creates a UDP
176 /// socket, however it does not perform the `bind`. And, it has a
177 /// `blocking` argument to select blocking or non-blocking mode for the
178 /// created socket.
179 ///
180 /// And it's similar to [`Pool::connect_udp_socket`] in that it creates a
181 /// UDP socket, however it does not perform the `connect` step. And, it has
182 /// a `blocking` argument to select blocking or non-blocking mode for the
183 /// created socket.
184 fn new(address_family: AddressFamily, blocking: Blocking) -> io::Result<Self>;
185}
186
187impl UdpSocketExt for UdpSocket {
188 fn new(address_family: AddressFamily, blocking: Blocking) -> io::Result<Self> {
189 socket(address_family, blocking, rustix::net::SocketType::DGRAM).map(Self::from)
190 }
191}
192
193/// A trait for extending `Pool` types.
194///
195/// These functions have a `ToSocketAddrs` argument, which can return either
196/// IPv4 or IPv6 addresses, however they also require the socket to be created
197/// with a specific address family up front. Consequently, it's recommended to
198/// do address resolution outside of this API and just pass resolved
199/// `SocketAddr`s in.
200pub trait PoolExt: private::Sealed {
201 /// Bind a [`TcpListener`] to the specified address.
202 ///
203 /// A newly-created `TcpListener` created with [`TcpListenerExt::new`]
204 /// has not been bound yet; this function binds it. Before it can accept
205 /// connections, it must be marked for listening with
206 /// [`TcpListenerExt::listen`].
207 ///
208 /// This is similar to [`Pool::bind_tcp_listener`] in that it binds a TCP
209 /// socket, however it does not create the socket itself, or perform the
210 /// `listen` step.
211 ///
212 /// This function ensures that the address to be bound is permitted by the
213 /// pool, and performs the bind. To perform these steps separately, create
214 /// a [`TcpBinder`] with [`Self::tcp_binder`] and use
215 /// [`TcpBinder::bind_existing_tcp_listener`].
216 fn bind_existing_tcp_listener<A: ToSocketAddrs>(
217 &self,
218 listener: &TcpListener,
219 addrs: A,
220 ) -> io::Result<()>;
221
222 /// Bind a [`UdpSocket`] to the specified address.
223 ///
224 /// A newly-created `UdpSocket` created with [`UdpSocketExt::new`] has not
225 /// been bound yet; this function binds it.
226 ///
227 /// This is similar to [`Pool::bind_udp_socket`] in that it binds a UDP
228 /// socket, however it does not create the socket itself.
229 ///
230 /// This function ensures that the address to be bound is permitted by the
231 /// pool, and performs the bind. To perform these steps separately, create
232 /// a [`UdpBinder`] with [`Self::udp_binder`] and use
233 /// [`UdpBinder::bind_existing_udp_socket`].
234 fn bind_existing_udp_socket<A: ToSocketAddrs>(
235 &self,
236 socket: &UdpSocket,
237 addrs: A,
238 ) -> io::Result<()>;
239
240 /// Initiate a TCP connection, converting a [`TcpListener`] to a
241 /// [`TcpStream`].
242 ///
243 /// This is simlar to [`Pool::connect_tcp_stream`] in that it performs a
244 /// TCP connection, but instead of creating a new socket itself it takes a
245 /// [`TcpListener`], such as one created with [`TcpListenerExt::new`].
246 ///
247 /// Despite the name, this function uses the `TcpListener` type as a
248 /// generic socket container.
249 ///
250 /// This function ensures that the address to connect to is permitted by
251 /// the pool, and performs the connect. To perform these steps separately,
252 /// create a [`TcpConnecter`] with [`Self::tcp_connecter`] and use
253 /// [`TcpConnecter::connect_into_tcp_stream`].
254 fn connect_into_tcp_stream<A: ToSocketAddrs>(
255 &self,
256 socket: TcpListener,
257 addrs: A,
258 ) -> io::Result<TcpStream>;
259
260 /// Initiate a TCP connection on a socket.
261 ///
262 /// This is simlar to [`Self::connect_into_tcp_stream`], however instead of
263 /// converting a `TcpListener` to a `TcpStream`, it leaves fd in the
264 /// existing `TcpListener`.
265 ///
266 /// This function ensures that the address to connect to is permitted by
267 /// the pool, and performs the connect. To perform these steps separately,
268 /// create a [`TcpConnecter`] with [`Self::tcp_connecter`] and use
269 /// [`TcpConnecter::connect_existing_tcp_listener`].
270 fn connect_existing_tcp_listener<A: ToSocketAddrs>(
271 &self,
272 socket: &TcpListener,
273 addrs: A,
274 ) -> io::Result<()>;
275
276 /// Initiate a UDP connection.
277 ///
278 /// This is simlar to [`Pool::connect_udp_socket`] in that it performs a
279 /// UDP connection, but instead of creating a new socket itself it takes a
280 /// [`UdpSocket`], such as one created with [`UdpSocketExt::new`].
281 ///
282 /// This function ensures that the address to connect to is permitted by
283 /// the pool, and performs the connect. To perform these steps separately,
284 /// create a [`UdpConnecter`] with [`Self::udp_connecter`] and use
285 /// [`UdpConnecter::connect_existing_udp_socket`].
286 fn connect_existing_udp_socket<A: ToSocketAddrs>(
287 &self,
288 socket: &UdpSocket,
289 addrs: A,
290 ) -> io::Result<()>;
291
292 /// Create a TCP binder.
293 ///
294 /// This is an alternative to [`Self::bind_existing_tcp_listener`]. It
295 /// checks that all the addresses in `addrs` are permitted for TCP binding
296 /// up front, and then records them in a [`TcpBinder`] which can then be
297 /// used to make repeated [`TcpBinder::bind_existing_tcp_listener`] calls.
298 fn tcp_binder<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<TcpBinder>;
299
300 /// Create a UDP binder.
301 ///
302 /// This is an alternative to [`Self::bind_existing_udp_socket`]. It checks
303 /// that all the addresses in `addrs` are permitted for UDP binding up
304 /// front, and then records them in a [`UdpBinder`] which can then be used
305 /// to make repeated [`UdpBinder::bind_existing_udp_socket`] calls.
306 fn udp_binder<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<UdpBinder>;
307
308 /// Create a TCP connecter.
309 ///
310 /// This is an alternative to [`Self::connect_into_tcp_stream`] and
311 /// [`Self::connect_existing_tcp_listener`]. It checks that all the
312 /// addresses in `addrs` are permitted for TCP connecting up front, and
313 /// then records them in a [`TcpConnecter`] which can then be used to make
314 /// repeated [`TcpConnecter::connect_into_tcp_stream`] and
315 /// [`TcpConnecter::connect_existing_tcp_listener`] calls.
316 fn tcp_connecter<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<TcpConnecter>;
317
318 /// Create a UDP connecter.
319 ///
320 /// This is an alternative to [`Self::connect_existing_udp_socket`]. It
321 /// checks that all the addresses in `addrs` are permitted for UDP
322 /// connecting up front, and then records them in a [`UdpConnecter`] which
323 /// can then be used to make repeated
324 /// [`UdpConnecter::connect_existing_udp_socket`] calls.
325 fn udp_connecter<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<UdpConnecter>;
326}
327
328impl PoolExt for Pool {
329 fn bind_existing_tcp_listener<A: ToSocketAddrs>(
330 &self,
331 listener: &TcpListener,
332 addrs: A,
333 ) -> io::Result<()> {
334 let addrs = addrs.to_socket_addrs()?;
335
336 let mut last_err = None;
337 for addr in addrs {
338 self._pool().check_addr(&addr)?;
339
340 set_reuseaddr(listener)?;
341
342 match rustix::net::bind(listener, &addr) {
343 Ok(()) => return Ok(()),
344 Err(err) => last_err = Some(err.into()),
345 }
346 }
347 match last_err {
348 Some(err) => Err(err),
349 None => Err(no_socket_addrs()),
350 }
351 }
352
353 fn bind_existing_udp_socket<A: ToSocketAddrs>(
354 &self,
355 socket: &UdpSocket,
356 addrs: A,
357 ) -> io::Result<()> {
358 let addrs = addrs.to_socket_addrs()?;
359
360 let mut last_err = None;
361 for addr in addrs {
362 self._pool().check_addr(&addr)?;
363
364 match rustix::net::bind(socket, &addr) {
365 Ok(()) => return Ok(()),
366 Err(err) => last_err = Some(err),
367 }
368 }
369 match last_err {
370 Some(err) => Err(err.into()),
371 None => Err(no_socket_addrs()),
372 }
373 }
374
375 fn connect_into_tcp_stream<A: ToSocketAddrs>(
376 &self,
377 socket: TcpListener,
378 addrs: A,
379 ) -> io::Result<TcpStream> {
380 self.connect_existing_tcp_listener(&socket, addrs)?;
381 Ok(TcpStream::from(OwnedFd::from(socket)))
382 }
383
384 fn connect_existing_tcp_listener<A: ToSocketAddrs>(
385 &self,
386 socket: &TcpListener,
387 addrs: A,
388 ) -> io::Result<()> {
389 let addrs = addrs.to_socket_addrs()?;
390
391 let mut last_err = None;
392 for addr in addrs {
393 self._pool().check_addr(&addr)?;
394
395 match rustix::net::connect(socket, &addr) {
396 Ok(()) => return Ok(()),
397 Err(err) => last_err = Some(err),
398 }
399 }
400 match last_err {
401 Some(err) => Err(err.into()),
402 None => Err(no_socket_addrs()),
403 }
404 }
405
406 fn connect_existing_udp_socket<A: ToSocketAddrs>(
407 &self,
408 socket: &UdpSocket,
409 addrs: A,
410 ) -> io::Result<()> {
411 let addrs = addrs.to_socket_addrs()?;
412
413 let mut last_err = None;
414 for addr in addrs {
415 self._pool().check_addr(&addr)?;
416
417 match rustix::net::connect(socket, &addr) {
418 Ok(()) => return Ok(()),
419 Err(err) => last_err = Some(err),
420 }
421 }
422 match last_err {
423 Some(err) => Err(err.into()),
424 None => Err(no_socket_addrs()),
425 }
426 }
427
428 fn tcp_binder<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<TcpBinder> {
429 Ok(TcpBinder(check_addrs(self._pool(), addrs)?))
430 }
431
432 fn udp_binder<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<UdpBinder> {
433 Ok(UdpBinder(check_addrs(self._pool(), addrs)?))
434 }
435
436 fn tcp_connecter<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<TcpConnecter> {
437 Ok(TcpConnecter(check_addrs(self._pool(), addrs)?))
438 }
439
440 fn udp_connecter<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<UdpConnecter> {
441 Ok(UdpConnecter(check_addrs(self._pool(), addrs)?))
442 }
443}
444
445/// Check all the addresses in `addrs` and return a new list of them.
446fn check_addrs<A: ToSocketAddrs>(
447 pool: &cap_primitives::net::Pool,
448 addrs: A,
449) -> io::Result<smallvec::SmallVec<[SocketAddr; 1]>> {
450 let mut checked = smallvec::SmallVec::new();
451 for addr in addrs.to_socket_addrs()? {
452 pool.check_addr(&addr)?;
453 checked.push(addr);
454 }
455 Ok(checked)
456}
457
458/// A utility for binding TCP listeners.
459///
460/// See [`PoolExt::tcp_binder`] for details.
461pub struct TcpBinder(smallvec::SmallVec<[SocketAddr; 1]>);
462
463impl TcpBinder {
464 /// Bind a [`TcpListener`].
465 ///
466 /// A newly-created `TcpListener` created with [`TcpListenerExt::new`]
467 /// has not been bound yet; this function binds it. Before it can accept
468 /// connections, it must be marked for listening with
469 /// [`TcpListenerExt::listen`].
470 ///
471 /// This is similar to [`Pool::bind_tcp_listener`] in that it binds a TCP
472 /// socket, however it does not create the socket itself, or perform the
473 /// `listen` step.
474 ///
475 /// This is similar to [`PoolExt::bind_existing_tcp_listener`] except that
476 /// it uses a `TcpBinder` which contains addresses that have already been
477 /// checked against a `Pool`.
478 pub fn bind_existing_tcp_listener(&self, listener: &TcpListener) -> io::Result<()> {
479 let mut last_err = None;
480 for addr in &self.0 {
481 set_reuseaddr(listener)?;
482
483 match rustix::net::bind(listener, addr) {
484 Ok(()) => return Ok(()),
485 Err(err) => last_err = Some(err.into()),
486 }
487 }
488 match last_err {
489 Some(err) => Err(err),
490 None => Err(no_socket_addrs()),
491 }
492 }
493}
494
495/// A utility for binding UDP sockets.
496///
497/// See [`PoolExt::udp_binder`] for details.
498pub struct UdpBinder(smallvec::SmallVec<[SocketAddr; 1]>);
499
500impl UdpBinder {
501 /// Bind a [`UdpSocket`] to the specified address.
502 ///
503 /// A newly-created `UdpSocket` created with [`UdpSocketExt::new`] has not
504 /// been bound yet; this function binds it.
505 ///
506 /// This is similar to [`Pool::bind_udp_socket`] in that it binds a UDP
507 /// socket, however it does not create the socket itself.
508 ///
509 /// This is similar to [`PoolExt::bind_existing_udp_socket`] except that
510 /// it uses a `UdpBinder` which contains addresses that have already been
511 /// checked against a `Pool`.
512 pub fn bind_existing_udp_socket(&self, socket: &UdpSocket) -> io::Result<()> {
513 let mut last_err = None;
514 for addr in &self.0 {
515 match rustix::net::bind(socket, addr) {
516 Ok(()) => return Ok(()),
517 Err(err) => last_err = Some(err.into()),
518 }
519 }
520 match last_err {
521 Some(err) => Err(err),
522 None => Err(no_socket_addrs()),
523 }
524 }
525}
526
527/// A utility for making TCP connections.
528///
529/// See [`PoolExt::tcp_connecter`] for details.
530pub struct TcpConnecter(smallvec::SmallVec<[SocketAddr; 1]>);
531
532impl TcpConnecter {
533 /// Initiate a TCP connection, converting a [`TcpListener`] to a
534 /// [`TcpStream`].
535 ///
536 /// This is simlar to [`Pool::connect_tcp_stream`] in that it performs a
537 /// TCP connection, but instead of creating a new socket itself it takes a
538 /// [`TcpListener`], such as one created with [`TcpListenerExt::new`].
539 ///
540 /// Despite the name, this function uses the `TcpListener` type as a
541 /// generic socket container.
542 ///
543 /// This is similar to [`PoolExt::connect_into_tcp_stream`] except that
544 /// it uses a `TcpConnecter` which contains addresses that have already
545 /// been checked against a `Pool`.
546 pub fn connect_into_tcp_stream(&self, socket: TcpListener) -> io::Result<TcpStream> {
547 self.connect_existing_tcp_listener(&socket)?;
548 Ok(TcpStream::from(OwnedFd::from(socket)))
549 }
550
551 /// Initiate a TCP connection on a socket.
552 ///
553 /// This is simlar to [`Pool::connect_into_tcp_stream`], however instead of
554 /// converting a `TcpListener` to a `TcpStream`, it leaves fd in the
555 /// existing `TcpListener`.
556 ///
557 /// This is similar to [`PoolExt::connect_existing_tcp_listener`] except
558 /// that it uses a `TcpConnecter` which contains addresses that have
559 /// already been checked against a `Pool`.
560 pub fn connect_existing_tcp_listener(&self, socket: &TcpListener) -> io::Result<()> {
561 let mut last_err = None;
562 for addr in &self.0 {
563 match rustix::net::connect(socket, addr) {
564 Ok(()) => return Ok(()),
565 Err(err) => last_err = Some(err),
566 }
567 }
568 match last_err {
569 Some(err) => Err(err.into()),
570 None => Err(no_socket_addrs()),
571 }
572 }
573}
574
575/// A utility for making UDP connections.
576///
577/// See [`PoolExt::udp_connecter`] for details.
578pub struct UdpConnecter(smallvec::SmallVec<[SocketAddr; 1]>);
579
580impl UdpConnecter {
581 /// Initiate a UDP connection.
582 ///
583 /// This is simlar to [`Pool::connect_udp_socket`] in that it performs a
584 /// UDP connection, but instead of creating a new socket itself it takes a
585 /// [`UdpSocket`], such as one created with [`UdpSocketExt::new`].
586 ///
587 /// This is similar to [`PoolExt::connect_existing_udp_socket`] except that
588 /// it uses a `UdpConnecter` which contains addresses that have already
589 /// been checked against a `Pool`.
590 pub fn connect_existing_udp_socket(&self, socket: &UdpSocket) -> io::Result<()> {
591 let mut last_err = None;
592 for addr in &self.0 {
593 match rustix::net::connect(socket, addr) {
594 Ok(()) => return Ok(()),
595 Err(err) => last_err = Some(err),
596 }
597 }
598 match last_err {
599 Some(err) => Err(err.into()),
600 None => Err(no_socket_addrs()),
601 }
602 }
603}
604
605fn socket(
606 address_family: AddressFamily,
607 blocking: Blocking,
608 socket_type: rustix::net::SocketType,
609) -> io::Result<OwnedFd> {
610 // The Rust standard library has code to call `WSAStartup`, which is needed
611 // on Windows before we do any other Winsock2 calls, so just make a useless
612 // API call once.
613 #[cfg(windows)]
614 {
615 use std::sync::Once;
616 static START: Once = Once::new();
617 START.call_once(|| {
618 std::net::TcpStream::connect(std::net::SocketAddrV4::new(
619 std::net::Ipv4Addr::UNSPECIFIED,
620 0,
621 ))
622 .unwrap_err();
623 });
624 }
625
626 // Create the socket, using the desired flags if we can.
627 let socket = rustix::net::socket_with(
628 address_family.into(),
629 socket_type,
630 socket_flags(blocking),
631 None,
632 )?;
633
634 // Set the desired flags if we couldn't set them at creation.
635 set_socket_flags(&socket, blocking)?;
636
637 Ok(socket)
638}
639
640/// Compute flags to pass to socket calls.
641fn socket_flags(blocking: Blocking) -> rustix::net::SocketFlags {
642 let _ = blocking;
643
644 #[allow(unused_mut)]
645 let mut socket_flags = rustix::net::SocketFlags::empty();
646
647 // On platforms which do support `SOCK_CLOEXEC`, use it.
648 #[cfg(not(any(
649 windows,
650 target_os = "macos",
651 target_os = "ios",
652 target_os = "tvos",
653 target_os = "watchos",
654 target_os = "visionos",
655 target_os = "haiku"
656 )))]
657 {
658 socket_flags |= rustix::net::SocketFlags::CLOEXEC;
659 }
660
661 // On platforms which do support `SOCK_NONBLOCK`, use it.
662 #[cfg(not(any(
663 windows,
664 target_os = "macos",
665 target_os = "ios",
666 target_os = "tvos",
667 target_os = "watchos",
668 target_os = "visionos",
669 target_os = "haiku"
670 )))]
671 match blocking {
672 Blocking::Yes => (),
673 Blocking::No => socket_flags |= rustix::net::SocketFlags::NONBLOCK,
674 }
675
676 socket_flags
677}
678
679/// On platforms which don't support `SOCK_CLOEXEC` or `SOCK_NONBLOCK, set them
680/// after creating the socket.
681fn set_socket_flags(fd: &OwnedFd, blocking: Blocking) -> io::Result<()> {
682 let _ = fd;
683 let _ = blocking;
684
685 #[cfg(any(
686 target_os = "macos",
687 target_os = "ios",
688 target_os = "tvos",
689 target_os = "watchos",
690 target_os = "visionos",
691 ))]
692 {
693 rustix::io::ioctl_fioclex(fd)?;
694 }
695
696 #[cfg(any(
697 windows,
698 target_os = "macos",
699 target_os = "ios",
700 target_os = "tvos",
701 target_os = "watchos",
702 target_os = "visionos"
703 ))]
704 match blocking {
705 Blocking::Yes => (),
706 Blocking::No => rustix::io::ioctl_fionbio(fd, true)?,
707 }
708
709 #[cfg(target_os = "haiku")]
710 {
711 let mut flags = rustix::fs::fcntl_getfd(fd)?;
712 flags |= rustix::fs::OFlags::CLOEXEC;
713 match blocking {
714 Blocking::Yes => (),
715 Blocking::No => flags |= rustix::fs::OFlags::NONBLOCK,
716 }
717 rustix::fs::fcntl_setfd(fd, flags)?;
718 }
719
720 Ok(())
721}
722
723/// On platforms where it's desirable, set the `SO_REUSEADDR` option.
724fn set_reuseaddr(listener: &TcpListener) -> io::Result<()> {
725 let _ = listener;
726
727 // The following logic is from
728 // <https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/net.rs>
729 // at revision defa2456246a8272ceace9c1cdccdf2e4c36175e.
730
731 // On platforms with Berkeley-derived sockets, this allows to quickly
732 // rebind a socket, without needing to wait for the OS to clean up the
733 // previous one.
734 //
735 // On Windows, this allows rebinding sockets which are actively in use,
736 // which allows “socket hijacking”, so we explicitly don't set it here.
737 // https://docs.microsoft.com/en-us/windows/win32/winsock/using-so-reuseaddr-and-so-exclusiveaddruse
738 #[cfg(not(windows))]
739 rustix::net::sockopt::set_socket_reuseaddr(listener, true)?;
740
741 Ok(())
742}
743
744/// Determine the platform-specific default backlog value.
745fn default_backlog() -> i32 {
746 // The following logic is from
747 // <https://github.com/rust-lang/rust/blob/master/library/std/src/sys_common/net.rs>
748 // at revision defa2456246a8272ceace9c1cdccdf2e4c36175e.
749
750 // The 3DS doesn't support a big connection backlog. Sometimes
751 // it allows up to about 37, but other times it doesn't even
752 // accept 32. There may be a global limitation causing this.
753 #[cfg(target_os = "horizon")]
754 let backlog = 20;
755
756 // The default for all other platforms
757 #[cfg(not(target_os = "horizon"))]
758 let backlog = 128;
759
760 backlog
761}
762
763/// Seal the public traits for [future-proofing].
764///
765/// [future-proofing]: https://rust-lang.github.io/api-guidelines/future-proofing.html
766mod private {
767 pub trait Sealed {}
768 impl Sealed for super::TcpListener {}
769 impl Sealed for super::UdpSocket {}
770 impl Sealed for super::Pool {}
771}