Debian Codegraph @

third_party/rust/neqo-bin/src/udp.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 #![expect(clippy::missing_errors_doc, reason = "Passing up tokio errors.")]
8
9 use std::{io, net::SocketAddr};
10
11 use neqo_common::{qdebug, Datagram};
12 use neqo_udp::{DatagramIter, RecvBuf};
13
14 /// Ideally this would live in [`neqo-udp`]. [`neqo-udp`] is used in Firefox.
15 ///
16 /// Firefox uses `cargo vet`. [`tokio`] the dependency of [`neqo-udp`] is not
17 /// audited as `safe-to-deploy`. `cargo vet` will require `safe-to-deploy` for
18 /// [`tokio`] even when behind a feature flag.
19 ///
20 /// See <https://github.com/mozilla/cargo-vet/issues/626>.
21 pub struct Socket {
22 state: quinn_udp::UdpSocketState,
23 inner: tokio::net::UdpSocket,
24 }
25
26 impl Socket {
27 /// Create a new [`Socket`] bound to the provided address, not managed externally.
28 pub fn bind<A: std::net::ToSocketAddrs>(addr: A) -> Result<Self, io::Error> {
29 const ONE_MB: usize = 1 << 20;
30 let socket = std::net::UdpSocket::bind(addr)?;
31 let state = quinn_udp::UdpSocketState::new((&socket).into())?;
32
33 // FIXME: We need to experiment if increasing this actually improves performance.
34 // Also, on BSD and Apple targets, this seems to increase the `net.inet.udp.maxdgram`
35 // sysctl, which is not the same as the socket buffer.
36 // if send_buf_before < ONE_MB {
37 // state.set_send_buffer_size((&socket).into(), ONE_MB)?;
38 // let send_buf_after = state.send_buffer_size((&socket).into())?;
39 // qdebug!("Increasing socket send buffer size from {send_buf_before} to {ONE_MB}, now:
40 // {send_buf_after}"); } else {
41 // qdebug!("Default socket send buffer size is {send_buf_before}, not changing");
42 // }
43 qdebug!(
44 "Default socket send buffer size is {:?}",
45 state.send_buffer_size((&socket).into())
46 );
47
48 let recv_buf_before = state.recv_buffer_size((&socket).into())?;
49 if recv_buf_before < ONE_MB {
50 // Same as Firefox.
51 // <https://searchfox.org/mozilla-central/rev/fa5b44a4ea5c98b6a15f39638ea4cd04dc271f3d/modules/libpref/init/StaticPrefList.yaml#13474-13477>
52 state.set_recv_buffer_size((&socket).into(), ONE_MB)?;
53 qdebug!(
54 "Increasing socket recv buffer size from {recv_buf_before} to {ONE_MB}, now: {:?}",
55 state.recv_buffer_size((&socket).into())
56 );
57 } else {
58 qdebug!("Default socket receive buffer size is {recv_buf_before}, not changing");
59 }
60
61 Ok(Self {
62 state,
63 inner: tokio::net::UdpSocket::from_std(socket)?,
64 })
65 }
66
67 /// See [`tokio::net::UdpSocket::local_addr`].
68 pub fn local_addr(&self) -> io::Result<SocketAddr> {
69 self.inner.local_addr()
70 }
71
72 /// See [`tokio::net::UdpSocket::writable`].
73 pub async fn writable(&self) -> Result<(), io::Error> {
74 self.inner.writable().await
75 }
76
77 /// See [`tokio::net::UdpSocket::readable`].
78 pub async fn readable(&self) -> Result<(), io::Error> {
79 self.inner.readable().await
80 }
81
82 /// Send a [`Datagram`] on the given [`Socket`].
83 pub fn send(&self, d: &Datagram) -> io::Result<()> {
84 self.inner.try_io(tokio::io::Interest::WRITABLE, || {
85 neqo_udp::send_inner(&self.state, (&self.inner).into(), d)
86 })
87 }
88
89 /// Receive a batch of [`Datagram`]s on the given [`Socket`], each set with
90 /// the provided local address.
91 pub fn recv<'a>(
92 &self,
93 local_address: SocketAddr,
94 recv_buf: &'a mut RecvBuf,
95 ) -> Result<Option<DatagramIter<'a>>, io::Error> {
96 self.inner
97 .try_io(tokio::io::Interest::READABLE, || {
98 neqo_udp::recv_inner(local_address, &self.state, &self.inner, recv_buf)
99 })
100 .map(Some)
101 .or_else(|e| {
102 if e.kind() == io::ErrorKind::WouldBlock {
103 Ok(None)
104 } else {
105 Err(e)
106 }
107 })
108 }
109 }