Skip to main content

net_io/
tcp.rs

1//! TCP listeners and connections.
2
3use std::io::{self, IoSlice, IoSliceMut, Read, Write};
4use std::net::{Shutdown, SocketAddr, ToSocketAddrs};
5use std::sync::{Arc, Mutex, PoisonError};
6use std::time::{Duration, Instant};
7
8use crate::accept::{Accepted, Acceptor};
9use crate::driver::DriverKind;
10use crate::error::{CloseReason, ConfigReason, LimitKind, NetError, RefusalCause, TimeoutKind};
11use crate::gate::{AdmitStage, Gate, GateCell, KillHandle, guarded};
12use crate::id::{ConnectionId, ListenerId, WorkerId};
13use crate::info::{ConnectionInfo, Endpoint, Origin, Transport};
14use crate::limits::{Effective, Limits};
15use crate::link::{ClockRef, Got, Link, WakeSource};
16use crate::opts::{ListenOptions, SocketOptions, TcpOptions};
17use crate::proxy::ProxyMode;
18use crate::session::Phase;
19use crate::sys::{self, AddrFailure, AsSock, Interest, KillSlot, Ready};
20
21/// Facts about one bound listener.
22///
23/// ```
24/// let listener = net_io::listen("127.0.0.1:0")?;
25/// assert!(listener.info().local.socket_addr().is_some());
26/// # Ok::<(), net_io::NetError>(())
27/// ```
28#[non_exhaustive]
29#[derive(Clone, Debug)]
30pub struct ListenerInfo {
31    /// The listener's index in its server (0 for a plain listener).
32    pub id: ListenerId,
33    /// Its name, if one was given.
34    pub name: Option<String>,
35    /// The bound address.
36    pub local: Endpoint,
37    /// The effective backlog; `None` when it cannot be read (macOS, adopted sockets).
38    pub backlog: Option<u32>,
39    /// The kind of socket.
40    pub transport: Transport,
41    /// Whether it expects a PROXY header.
42    pub proxy: ProxyMode,
43    /// Whether it serves TLS.
44    pub tls: bool,
45}
46
47/// Linux silently caps the backlog at `net.core.somaxconn`; read it once.
48pub(crate) fn effective_backlog(requested: u32) -> Option<u32> {
49    if cfg!(target_os = "linux") {
50        let cap = std::fs::read_to_string("/proc/sys/net/core/somaxconn")
51            .ok()
52            .and_then(|s| s.trim().parse::<u32>().ok());
53        Some(cap.map_or(requested, |c| requested.min(c)))
54    } else if cfg!(windows) {
55        // Asked for as `SOMAXCONN_HINT` past 200, which Windows holds to 65,535.
56        Some(requested.min(65_535))
57    } else {
58        None
59    }
60}
61
62/// Whether accepted sockets inherit the listener's options (spike S-4, decision D-055).
63const OPTIONS_INHERITED: bool = cfg!(any(target_os = "linux", windows));
64
65pub(crate) fn bind_error(addr: SocketAddr, e: io::Error) -> NetError {
66    match sys::classify_addr_error(&e) {
67        AddrFailure::InUse => NetError::AddrInUse { addr },
68        AddrFailure::NotAvailable => NetError::AddrNotAvailable { addr },
69        AddrFailure::Permission => NetError::PermissionDenied { what: "bind" },
70        _ if e.kind() == io::ErrorKind::Unsupported => NetError::Unsupported {
71            feature: "a listen option this OS does not have",
72            platform: std::env::consts::OS,
73        },
74        _ => NetError::Bind { addr, source: e },
75    }
76}
77
78fn connect_error(addr: SocketAddr, after: Duration, e: io::Error) -> NetError {
79    match sys::classify_addr_error(&e) {
80        AddrFailure::Refused => NetError::ConnectionRefused { addr },
81        AddrFailure::HostUnreachable => NetError::HostUnreachable { addr },
82        AddrFailure::NetworkUnreachable => NetError::NetworkUnreachable { addr },
83        AddrFailure::TimedOut => NetError::ConnectTimeout { addr, after },
84        AddrFailure::NotAvailable => NetError::AddrNotAvailable { addr },
85        AddrFailure::Permission => NetError::PermissionDenied { what: "connect" },
86        _ => NetError::Os {
87            op: "connect",
88            source: e,
89        },
90    }
91}
92
93fn resolve<A: ToSocketAddrs>(addr: A) -> Result<Vec<SocketAddr>, NetError> {
94    let addrs: Vec<SocketAddr> = addr
95        .to_socket_addrs()
96        .map_err(|e| NetError::Os {
97            op: "resolve",
98            source: e,
99        })?
100        .collect();
101    if addrs.is_empty() {
102        return Err(NetError::NoAddresses);
103    }
104    Ok(addrs)
105}
106
107/// A listening TCP socket created by net-io, so options that must precede `bind` can be set.
108///
109/// # Examples
110///
111/// ```no_run
112/// let listener = net_io::listen("0.0.0.0:7000")?;
113/// loop {
114///     let (mut conn, info) = listener.accept()?;
115///     println!("connection {} from {:?}", info.id, info.client_addr());
116///     conn.send_all(b"hello\n")?;
117///     conn.close()?;
118/// }
119/// # Ok::<(), net_io::NetError>(())
120/// ```
121#[derive(Debug)]
122pub struct TcpListener {
123    inner: std::net::TcpListener,
124    info: ListenerInfo,
125    tcp: TcpOptions,
126    acceptor: Mutex<Acceptor>,
127}
128
129impl TcpListener {
130    /// Binds with safe defaults (see [`crate::listen`]).
131    ///
132    /// # Errors
133    ///
134    /// `AddrInUse`, `PermissionDenied`, `AddrNotAvailable`, `NoAddresses`, `Bind`.
135    pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener, NetError> {
136        TcpListener::bind_with(addr, ListenOptions::new())
137    }
138
139    /// Binds with `options`, applied in the order `docs/API.md` gives.
140    ///
141    /// # Errors
142    ///
143    /// As [`TcpListener::bind`]; `Unsupported` for an option the platform lacks.
144    pub fn bind_with<A: ToSocketAddrs>(
145        addr: A,
146        options: ListenOptions,
147    ) -> Result<TcpListener, NetError> {
148        let mut last = NetError::NoAddresses;
149        for a in resolve(addr)? {
150            match sys::tcp_listener(a, &options.params()) {
151                Ok(l) => return TcpListener::finish(l, options, ListenerId(0), None),
152                Err(e) => last = bind_error(a, e),
153            }
154        }
155        Err(last)
156    }
157
158    pub(crate) fn finish(
159        inner: std::net::TcpListener,
160        options: ListenOptions,
161        id: ListenerId,
162        name: Option<String>,
163    ) -> Result<TcpListener, NetError> {
164        let local = inner
165            .local_addr()
166            .map_err(|e| NetError::from_os("getsockname", e))?;
167        if OPTIONS_INHERITED && !options.tcp.is_empty() {
168            options.tcp.apply(&inner, local.is_ipv6())?;
169        }
170        Ok(TcpListener {
171            info: ListenerInfo {
172                id,
173                name,
174                local: Endpoint::Ip(local),
175                backlog: effective_backlog(options.backlog),
176                transport: Transport::Tcp,
177                proxy: ProxyMode::Off,
178                tls: false,
179            },
180            inner,
181            tcp: options.tcp,
182            acceptor: Mutex::new(Acceptor::new()),
183        })
184    }
185
186    /// Blocks until a connection arrives; applies per-connection options, normalizes
187    /// the peer, issues a `ConnectionId`. No admission: a plain listener has no gate.
188    ///
189    /// # Errors
190    ///
191    /// `Limit { which: Descriptors }` when exhaustion persists past one backoff step; `Os`.
192    pub fn accept(&self) -> Result<(TcpConnection, ConnectionInfo), NetError> {
193        match self.accept_until(None)? {
194            Some(pair) => Ok(pair),
195            None => Err(NetError::TimedOut {
196                kind: TimeoutKind::Read,
197            }),
198        }
199    }
200
201    /// As [`TcpListener::accept`], but `Ok(None)` when nothing arrived within `timeout`.
202    ///
203    /// # Errors
204    ///
205    /// As [`TcpListener::accept`].
206    ///
207    /// ```
208    /// let listener = net_io::listen("127.0.0.1:0")?;
209    /// let got = listener.accept_timeout(std::time::Duration::from_millis(10))?;
210    /// assert!(got.is_none());
211    /// # Ok::<(), net_io::NetError>(())
212    /// ```
213    pub fn accept_timeout(
214        &self,
215        timeout: Duration,
216    ) -> Result<Option<(TcpConnection, ConnectionInfo)>, NetError> {
217        self.accept_until(Some(Instant::now() + timeout))
218    }
219
220    fn accept_until(
221        &self,
222        deadline: Option<Instant>,
223    ) -> Result<Option<(TcpConnection, ConnectionInfo)>, NetError> {
224        let mut paused_once = false;
225        loop {
226            let outcome = self
227                .acceptor
228                .lock()
229                .unwrap_or_else(PoisonError::into_inner)
230                .accept(&self.inner);
231            match outcome {
232                Accepted::Conn((stream, peer)) => {
233                    let conn = TcpConnection::accepted(stream, peer, &self.tcp, self.info.id)?;
234                    let info = conn.info().clone();
235                    return Ok(Some((conn, info)));
236                }
237                Accepted::Skipped(_) => continue,
238                Accepted::Again => {
239                    let timeout = match deadline {
240                        None => None,
241                        Some(d) => {
242                            let left = d.saturating_duration_since(Instant::now());
243                            if left.is_zero() {
244                                return Ok(None);
245                            }
246                            Some(left)
247                        }
248                    };
249                    match sys::wait(self.inner.sock(), Interest::Read, None, timeout) {
250                        Ok(Ready::TimedOut) => return Ok(None),
251                        Ok(_) => continue,
252                        Err(e) => return Err(NetError::from_os("poll", e)),
253                    }
254                }
255                Accepted::Pause(backoff) => {
256                    if paused_once {
257                        return Err(NetError::Limit {
258                            which: LimitKind::Descriptors,
259                        });
260                    }
261                    paused_once = true;
262                    std::thread::sleep(backoff);
263                }
264                Accepted::Failed(e) => return Err(NetError::from_os("accept", e)),
265            }
266        }
267    }
268
269    pub(crate) fn std(&self) -> &std::net::TcpListener {
270        &self.inner
271    }
272
273    /// Records the listener's id and name in its facts (adopted listeners).
274    pub(crate) fn set_identity(&mut self, id: ListenerId, name: Option<String>) {
275        self.info.id = id;
276        self.info.name = name;
277    }
278
279    /// Records whether the listener serves TLS in its facts.
280    pub(crate) fn set_tls(&mut self, on: bool) {
281        self.info.tls = on;
282    }
283
284    /// Records the listener's PROXY requirement in its facts.
285    pub(crate) fn set_proxy_mode(&mut self, mode: crate::proxy::ProxyMode) {
286        self.info.proxy = mode;
287    }
288
289    pub(crate) fn tcp_options(&self) -> &TcpOptions {
290        &self.tcp
291    }
292
293    /// Facts about this listener.
294    #[must_use]
295    pub fn info(&self) -> &ListenerInfo {
296        &self.info
297    }
298
299    /// The bound address.
300    ///
301    /// # Errors
302    ///
303    /// `Os` when `getsockname` fails.
304    pub fn local_addr(&self) -> Result<SocketAddr, NetError> {
305        self.inner
306            .local_addr()
307            .map_err(|e| NetError::from_os("getsockname", e))
308    }
309
310    /// Adopts an existing listening socket (checked with `SO_ACCEPTCONN` and `SO_TYPE`).
311    ///
312    /// # Errors
313    ///
314    /// `InvalidConfig { field: "socket", reason: NotListening }`.
315    ///
316    /// ```
317    /// let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
318    /// let listener = net_io::TcpListener::from_std(std_listener)?;
319    /// assert!(listener.local_addr().is_ok());
320    /// # Ok::<(), net_io::NetError>(())
321    /// ```
322    pub fn from_std(listener: std::net::TcpListener) -> Result<TcpListener, NetError> {
323        match sys::is_listening_stream(listener.sock()) {
324            Ok(true) => {}
325            Ok(false) => {
326                return Err(NetError::InvalidConfig {
327                    field: "socket",
328                    reason: ConfigReason::NotListening,
329                });
330            }
331            Err(e) => return Err(NetError::from_os("getsockopt", e)),
332        }
333        listener
334            .set_nonblocking(true)
335            .map_err(|e| NetError::from_os("ioctl", e))?;
336        TcpListener::finish(
337            listener,
338            ListenOptions::new().tcp_options(TcpOptions::new()),
339            ListenerId(0),
340            None,
341        )
342        .map(|mut l| {
343            l.info.backlog = None;
344            l
345        })
346    }
347
348    /// Gives the socket up (it stays non-blocking).
349    #[must_use]
350    pub fn into_std(self) -> std::net::TcpListener {
351        self.inner
352    }
353
354    /// A second handle to the same listening socket.
355    ///
356    /// # Errors
357    ///
358    /// `Os` when duplication fails.
359    pub fn try_clone(&self) -> Result<TcpListener, NetError> {
360        let inner = self
361            .inner
362            .try_clone()
363            .map_err(|e| NetError::from_os("dup", e))?;
364        Ok(TcpListener {
365            inner,
366            info: self.info.clone(),
367            tcp: self.tcp,
368            acceptor: Mutex::new(Acceptor::new()),
369        })
370    }
371}
372
373#[cfg(unix)]
374impl std::os::fd::AsFd for TcpListener {
375    fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
376        self.inner.as_fd()
377    }
378}
379
380#[cfg(unix)]
381impl From<TcpListener> for std::os::fd::OwnedFd {
382    fn from(l: TcpListener) -> std::os::fd::OwnedFd {
383        l.inner.into()
384    }
385}
386
387#[cfg(unix)]
388impl TryFrom<std::os::fd::OwnedFd> for TcpListener {
389    type Error = NetError;
390    fn try_from(fd: std::os::fd::OwnedFd) -> Result<TcpListener, NetError> {
391        TcpListener::from_std(std::net::TcpListener::from(fd))
392    }
393}
394
395#[cfg(windows)]
396impl std::os::windows::io::AsSocket for TcpListener {
397    fn as_socket(&self) -> std::os::windows::io::BorrowedSocket<'_> {
398        self.inner.as_socket()
399    }
400}
401
402#[cfg(windows)]
403impl From<TcpListener> for std::os::windows::io::OwnedSocket {
404    fn from(l: TcpListener) -> std::os::windows::io::OwnedSocket {
405        l.inner.into()
406    }
407}
408
409#[cfg(windows)]
410impl TryFrom<std::os::windows::io::OwnedSocket> for TcpListener {
411    type Error = NetError;
412    fn try_from(s: std::os::windows::io::OwnedSocket) -> Result<TcpListener, NetError> {
413        TcpListener::from_std(std::net::TcpListener::from(s))
414    }
415}
416
417/// Options for an outbound connection.
418///
419/// ```no_run
420/// use net_io::{ConnectOptions, TcpConnection};
421/// use std::time::Duration;
422/// let options = ConnectOptions::new().timeout(Duration::from_secs(3));
423/// let conn = TcpConnection::connect_host("example.com", 443, options)?;
424/// println!("connected to {}", conn.peer_addr());
425/// # Ok::<(), net_io::NetError>(())
426/// ```
427#[must_use]
428#[derive(Clone, Debug)]
429pub struct ConnectOptions {
430    timeout: Duration,
431    local_addr: Option<SocketAddr>,
432    tcp: TcpOptions,
433    limits: Limits,
434    happy_eyeballs: bool,
435    attempt_delay: Duration,
436}
437
438impl Default for ConnectOptions {
439    fn default() -> ConnectOptions {
440        ConnectOptions {
441            timeout: Duration::from_secs(10),
442            local_addr: None,
443            tcp: TcpOptions::new().nodelay(true),
444            limits: Limits::default(),
445            happy_eyeballs: false,
446            attempt_delay: Duration::from_millis(250),
447        }
448    }
449}
450
451impl ConnectOptions {
452    /// Timeout 10 s, `TCP_NODELAY` on, no local bind.
453    pub fn new() -> ConnectOptions {
454        ConnectOptions::default()
455    }
456
457    /// The total time for all attempts.
458    pub fn timeout(mut self, total: Duration) -> ConnectOptions {
459        self.timeout = total;
460        self
461    }
462
463    /// Binds to this address before connecting.
464    pub fn local_addr(mut self, addr: SocketAddr) -> ConnectOptions {
465        self.local_addr = Some(addr);
466        self
467    }
468
469    /// Options applied before the connect.
470    pub fn tcp_options(mut self, options: TcpOptions) -> ConnectOptions {
471        self.tcp = options;
472        self
473    }
474
475    /// Limits for the connection.
476    pub fn limits(mut self, limits: Limits) -> ConnectOptions {
477        self.limits = limits;
478        self
479    }
480
481    /// Happy Eyeballs (RFC 8305, simplified because `std` resolution returns both
482    /// families at once): attempts overlap instead of waiting for each other, IPv6
483    /// first and the families interleaved, and the first established connection wins.
484    /// The total timeout still bounds everything; the others are reset.
485    pub fn happy_eyeballs(mut self, on: bool) -> ConnectOptions {
486        self.happy_eyeballs = on;
487        self
488    }
489
490    /// How long an attempt runs alone before the next one starts (250 ms; held to
491    /// 100 ms through 2 s). An attempt that fails starts the next at once.
492    pub fn attempt_delay(mut self, d: Duration) -> ConnectOptions {
493        self.attempt_delay = d.clamp(Duration::from_millis(100), Duration::from_secs(2));
494        self
495    }
496}
497
498/// A connected TCP socket that carries its own limits.
499///
500/// Every read honours the current phase's deadline (a connection from `accept`
501/// starts `Idle` under the idle timeout; one from `connect` starts `Receiving`
502/// under the read timeout), the exchange deadline, and the inbound minimum
503/// rate; every write honours the write deadline and the outbound minimum rate.
504/// One owner: other threads act on it only through its [`KillHandle`].
505///
506/// # Examples
507///
508/// ```no_run
509/// use net_io::{Limits, Phase};
510/// use std::time::{Duration, Instant};
511///
512/// let mut conn = net_io::connect("127.0.0.1:7000")?;
513/// conn.set_limits(Limits::builder().read_timeout(Duration::from_secs(5)).build()?);
514/// conn.set_phase(Phase::Receiving);
515/// conn.set_exchange_deadline(Some(Instant::now() + Duration::from_secs(20)));
516/// let mut buf = [0u8; 1024];
517/// let n = conn.recv(&mut buf)?;
518/// # let _ = n;
519/// # Ok::<(), net_io::NetError>(())
520/// ```
521pub struct TcpConnection {
522    link: Option<Link>,
523}
524
525impl TcpConnection {
526    /// Connects with a 10 s timeout across every resolved address (see [`crate::connect`]).
527    ///
528    /// # Errors
529    ///
530    /// `ConnectTimeout`, `ConnectionRefused`, `HostUnreachable`, `NetworkUnreachable`, `NoAddresses`.
531    pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<TcpConnection, NetError> {
532        TcpConnection::connect_with(addr, ConnectOptions::new())
533    }
534
535    /// Connects with `options`.
536    ///
537    /// # Errors
538    ///
539    /// As [`TcpConnection::connect`].
540    pub fn connect_with<A: ToSocketAddrs>(
541        addr: A,
542        options: ConnectOptions,
543    ) -> Result<TcpConnection, NetError> {
544        let addrs = resolve(addr)?;
545        TcpConnection::connect_addrs(&addrs, &options)
546    }
547
548    /// Resolves `host` with the standard library and connects to the addresses in
549    /// order; each attempt gets the remaining time divided by the remaining
550    /// addresses (at least 1 s).
551    ///
552    /// # Errors
553    ///
554    /// `NoAddresses`, or the last connect error.
555    pub fn connect_host(
556        host: &str,
557        port: u16,
558        options: ConnectOptions,
559    ) -> Result<TcpConnection, NetError> {
560        let addrs = resolve((host, port))?;
561        TcpConnection::connect_addrs(&addrs, &options)
562    }
563
564    fn connect_addrs(
565        addrs: &[SocketAddr],
566        options: &ConnectOptions,
567    ) -> Result<TcpConnection, NetError> {
568        if options.happy_eyeballs && addrs.len() > 1 {
569            return connect_racing(addrs, options);
570        }
571        let start = Instant::now();
572        let deadline = start + options.timeout;
573        let mut last = NetError::NoAddresses;
574        for (i, addr) in addrs.iter().enumerate() {
575            let left = deadline.saturating_duration_since(Instant::now());
576            if left.is_zero() {
577                break;
578            }
579            let remaining = (addrs.len() - i) as u32;
580            let attempt = (left / remaining).max(Duration::from_secs(1)).min(left);
581            match connect_one(*addr, options, attempt) {
582                Ok(conn) => return Ok(conn),
583                Err(e) => last = e,
584            }
585        }
586        if matches!(last, NetError::NoAddresses) {
587            if let Some(addr) = addrs.first() {
588                last = NetError::ConnectTimeout {
589                    addr: *addr,
590                    after: options.timeout,
591                };
592            }
593        }
594        Err(last)
595    }
596
597    pub(crate) fn accepted(
598        stream: std::net::TcpStream,
599        peer: SocketAddr,
600        tcp: &TcpOptions,
601        listener: ListenerId,
602    ) -> Result<TcpConnection, NetError> {
603        let local = stream
604            .local_addr()
605            .map_err(|e| NetError::from_os("getsockname", e))?;
606        if !OPTIONS_INHERITED && !tcp.is_empty() {
607            tcp.apply(&stream, local.is_ipv6())?;
608        }
609        let info = ConnectionInfo::new_tcp(
610            crate::id::issue_single(),
611            Some(listener),
612            WorkerId(0),
613            Origin::Accepted,
614            local,
615            peer,
616            Instant::now(),
617        );
618        let limits = Arc::new(Limits::default().effective(DriverKind::Blocking));
619        Ok(TcpConnection::from_link(Link::new(
620            sys::Stream::Tcp(stream),
621            info,
622            limits,
623            ClockRef::Monotonic,
624            Arc::new(KillSlot::new()),
625            WakeSource::Thread,
626            Phase::Idle,
627        )))
628    }
629
630    pub(crate) fn from_link(link: Link) -> TcpConnection {
631        TcpConnection { link: Some(link) }
632    }
633
634    /// The process at an end of a named pipe.
635    #[cfg(all(windows, feature = "local"))]
636    pub(crate) fn pipe_pid(&self, server: bool) -> Result<u32, NetError> {
637        match &self.link_ref().stream {
638            sys::Stream::Pipe(p) => if server {
639                p.server_pid()
640            } else {
641                p.client_pid()
642            }
643            .map_err(|e| NetError::from_os("GetNamedPipeClientProcessId", e)),
644            _ => Err(NetError::Unsupported {
645                feature: "client_pid on a socket",
646                platform: std::env::consts::OS,
647            }),
648        }
649    }
650
651    pub(crate) fn take_link(&mut self) -> Option<Link> {
652        self.link.take()
653    }
654
655    pub(crate) fn link(&mut self) -> Result<&mut Link, NetError> {
656        self.link.as_mut().ok_or(NetError::Closed)
657    }
658
659    pub(crate) fn link_ref(&self) -> &Link {
660        match &self.link {
661            Some(l) => l,
662            // Only `take_link` (crate-internal, at the end of a handler) empties it.
663            None => unreachable_link(),
664        }
665    }
666
667    /// One receive; `Ok(0)` means the peer closed its side. A zero-length `buf`
668    /// returns `Ok(0)` without a system call (check `buf.len()`).
669    ///
670    /// # Errors
671    ///
672    /// `TimedOut { kind }`, `SlowPeer { In }`, `ConnectionReset`, `Killed`, `Closed`.
673    pub fn recv(&mut self, buf: &mut [u8]) -> Result<usize, NetError> {
674        let link = self.link()?;
675        loop {
676            match link.read(buf)? {
677                Got::Data(n) => return Ok(n),
678                Got::Eof => return Ok(0),
679                Got::Woken => continue,
680            }
681        }
682    }
683
684    /// Receives until `buf` is full.
685    ///
686    /// # Errors
687    ///
688    /// As [`TcpConnection::recv`]; `Closed` when the peer closes first.
689    pub fn recv_exact(&mut self, buf: &mut [u8]) -> Result<(), NetError> {
690        let mut filled = 0;
691        while filled < buf.len() {
692            match self.recv(&mut buf[filled..])? {
693                0 => return Err(NetError::Closed),
694                n => filled += n,
695            }
696        }
697        Ok(())
698    }
699
700    /// One receive into the first non-empty slice.
701    ///
702    /// # Errors
703    ///
704    /// As [`TcpConnection::recv`].
705    pub fn recv_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, NetError> {
706        match bufs.iter_mut().find(|b| !b.is_empty()) {
707            Some(first) => self.recv(first),
708            None => Ok(0),
709        }
710    }
711
712    /// Reads without consuming (`MSG_PEEK`), within the read timeout.
713    ///
714    /// # Errors
715    ///
716    /// As [`TcpConnection::recv`]; `Unsupported` on a TLS connection.
717    pub fn peek(&self, buf: &mut [u8]) -> Result<usize, NetError> {
718        let link = self.link_ref();
719        if buf.is_empty() {
720            return Ok(0);
721        }
722        if link.has_tls() {
723            // The socket holds ciphertext; peeking at it would mean nothing here.
724            return Err(NetError::Unsupported {
725                feature: "peek on a TLS connection",
726                platform: std::env::consts::OS,
727            });
728        }
729        let timeout = link.limits.read_timeout;
730        match link.stream.wait(Interest::Read, None, Some(timeout)) {
731            Ok(Ready::TimedOut) => {
732                return Err(NetError::TimedOut {
733                    kind: TimeoutKind::Read,
734                });
735            }
736            Ok(_) => {}
737            Err(e) => return Err(NetError::from_os("poll", e)),
738        }
739        link.stream
740            .peek(buf)
741            .map_err(|e| NetError::from_os("recv", e))
742    }
743
744    /// One send after the gate check; may be partial.
745    ///
746    /// # Errors
747    ///
748    /// `TimedOut`, `SlowPeer { Out }`, `BrokenPipe`, `ConnectionReset`, `EgressDenied`, `Killed`.
749    pub fn send(&mut self, buf: &[u8]) -> Result<usize, NetError> {
750        self.link()?.write(buf)
751    }
752
753    /// Sends all of `buf`, continuing from the exact offset; the gate check runs
754    /// before every system call.
755    ///
756    /// # Errors
757    ///
758    /// As [`TcpConnection::send`].
759    pub fn send_all(&mut self, buf: &[u8]) -> Result<(), NetError> {
760        self.link()?.write_all(buf)
761    }
762
763    /// One vectored send (up to 64 slices) after the gate check; may be partial.
764    ///
765    /// # Errors
766    ///
767    /// As [`TcpConnection::send`].
768    pub fn send_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, NetError> {
769        self.link()?.write_vectored(bufs)
770    }
771
772    /// Sends `len` bytes of `file` from `offset`: zero copy where the OS can (`sendfile`
773    /// on Unix), by reading and writing otherwise (TLS; Windows outside the reactor). A
774    /// gate check runs before each 1 MiB. The file's position does not matter, and on
775    /// Unix it does not move.
776    ///
777    /// # Errors
778    ///
779    /// As [`TcpConnection::send`]; `FileTruncated { sent }` when the file ends early,
780    /// and the connection is then reset. Send files that do not change while in flight.
781    pub fn send_file(
782        &mut self,
783        file: &std::fs::File,
784        offset: u64,
785        len: u64,
786    ) -> Result<u64, NetError> {
787        self.link()?.send_file(file, offset, len)
788    }
789
790    /// Sends every slice, advancing them in place (no copy).
791    ///
792    /// # Errors
793    ///
794    /// As [`TcpConnection::send`].
795    pub fn send_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), NetError> {
796        self.link()?.write_all_vectored(bufs)
797    }
798
799    /// Half-close: `Write` sends FIN and keeps reading; `Read` stops reading.
800    ///
801    /// # Errors
802    ///
803    /// `Closed`, `Os`.
804    pub fn shutdown(&mut self, how: Shutdown) -> Result<(), NetError> {
805        self.link()?.shutdown(how)
806    }
807
808    /// Graceful close: FIN, then reads and discards up to 64 KiB for up to 2 s so
809    /// unread input does not cause an RST that destroys the last response.
810    ///
811    /// # Errors
812    ///
813    /// None in this version; the signature leaves room for a flush error.
814    pub fn close(mut self) -> Result<(), NetError> {
815        if let Some(link) = self.link.take() {
816            let reason = link.reason_or(CloseReason::SessionClosed);
817            link.finish(reason);
818        }
819        Ok(())
820    }
821
822    /// Abortive close: RST, kernel resources freed at once.
823    pub fn abort(mut self) {
824        if let Some(link) = self.link.take() {
825            let reason = link.reason_or(CloseReason::SessionError);
826            let reason = if reason.is_abortive() {
827                reason
828            } else {
829                CloseReason::SessionError
830            };
831            link.finish(reason);
832        }
833    }
834
835    /// What net-io knows about this connection.
836    #[must_use]
837    pub fn info(&self) -> &ConnectionInfo {
838        &self.link_ref().info
839    }
840
841    /// The connection's id.
842    #[must_use]
843    pub fn id(&self) -> ConnectionId {
844        self.link_ref().info.id
845    }
846
847    /// The socket peer.
848    #[must_use]
849    pub fn peer_addr(&self) -> SocketAddr {
850        self.link_ref()
851            .info
852            .peer
853            .socket_addr()
854            .unwrap_or_else(|| SocketAddr::from(([0, 0, 0, 0], 0)))
855    }
856
857    /// Our end.
858    #[must_use]
859    pub fn local_addr(&self) -> SocketAddr {
860        self.link_ref()
861            .info
862            .local
863            .socket_addr()
864            .unwrap_or_else(|| SocketAddr::from(([0, 0, 0, 0], 0)))
865    }
866
867    /// A handle other threads can use to abort this connection.
868    #[must_use]
869    pub fn kill_handle(&self) -> KillHandle {
870        let link = self.link_ref();
871        KillHandle {
872            slot: link.slot.clone(),
873            generation: link.generation,
874            id: link.info.id,
875        }
876    }
877
878    /// Replaces the limits from the next operation on (unset values: blocking-driver defaults).
879    pub fn set_limits(&mut self, limits: Limits) {
880        if let Ok(link) = self.link() {
881            let e: Effective = limits.effective(DriverKind::Blocking);
882            link.set_limits(e);
883        }
884    }
885
886    /// Chooses which deadlines apply.
887    pub fn set_phase(&mut self, phase: Phase) {
888        if let Ok(link) = self.link() {
889            link.set_phase(phase);
890        }
891    }
892
893    /// The deadline for the current exchange (`None` clears it).
894    pub fn set_exchange_deadline(&mut self, at: Option<Instant>) {
895        if let Ok(link) = self.link() {
896            link.set_exchange_deadline(at);
897        }
898    }
899
900    /// Registers this dialed connection with `gate` (`Gate::admit` with
901    /// `AdmitStage::Dialed`): from then on `Gate::check` runs before every write
902    /// and `Gate::closed` runs once at close, so a back-channel or outbound
903    /// connection can be revoked like an accepted one.
904    ///
905    /// # Errors
906    ///
907    /// `Refused { cause: Gate(code) }` when the gate refuses (the connection is
908    /// then aborted and later calls return `Closed`); `InvalidConfig { field:
909    /// "gate" }` when a gate is already set; `Closed`.
910    ///
911    /// ```no_run
912    /// use std::sync::Arc;
913    /// let mut upstream = net_io::connect("10.0.0.9:9000")?;
914    /// upstream.set_gate(Arc::new(net_io::AllowAll))?;
915    /// upstream.send_all(b"PING\r\n")?;
916    /// # Ok::<(), net_io::NetError>(())
917    /// ```
918    pub fn set_gate<G: Gate>(&mut self, gate: Arc<G>) -> Result<(), NetError> {
919        let link = self.link()?;
920        if link.gate.is_some() {
921            return Err(NetError::InvalidConfig {
922                field: "gate",
923                reason: ConfigReason::Inconsistent,
924            });
925        }
926        let code = match guarded(|| gate.admit(&link.info, AdmitStage::Dialed)) {
927            Some(Ok(admitted)) => {
928                link.info.lane = admitted.lane;
929                link.info.gate = admitted.tag;
930                link.gate = Some(Box::new(GateCell {
931                    gate,
932                    ticket: Some(admitted.ticket),
933                }));
934                return Ok(());
935            }
936            Some(Err(refusal)) => refusal.code(),
937            None => 0,
938        };
939        let cause = RefusalCause::Gate(code);
940        if let Some(link) = self.link.take() {
941            link.finish(CloseReason::Refused(cause));
942        }
943        Err(NetError::Refused { cause })
944    }
945
946    /// Live socket options.
947    #[must_use]
948    pub fn options(&self) -> SocketOptions<'_> {
949        let link = self.link_ref();
950        SocketOptions {
951            sock: link.stream.sock(),
952            v6: link.info.local.socket_addr().is_some_and(|a| a.is_ipv6()),
953        }
954    }
955
956    /// The socket's pending error (`SO_ERROR`).
957    ///
958    /// # Errors
959    ///
960    /// `Os` when the OS call fails.
961    pub fn take_error(&self) -> Result<Option<io::Error>, NetError> {
962        sys::take_error(self.link_ref().stream.sock())
963            .map_err(|e| NetError::from_os("getsockopt", e))
964    }
965}
966
967#[cold]
968fn unreachable_link() -> ! {
969    // A TcpConnection is emptied only by the server after its handler returned,
970    // when no code can reach it any more.
971    std::process::abort()
972}
973
974/// A non-blocking socket ready to connect to `addr`.
975fn dial_socket(
976    addr: SocketAddr,
977    options: &ConnectOptions,
978) -> Result<std::net::TcpStream, NetError> {
979    let owned = sys::tcp_socket(addr.is_ipv6()).map_err(|e| NetError::from_os("socket", e))?;
980    let stream = std::net::TcpStream::from(owned);
981    stream
982        .set_nonblocking(true)
983        .map_err(|e| NetError::from_os("ioctl", e))?;
984    options.tcp.apply(&stream, addr.is_ipv6())?;
985    if let Some(local) = options.local_addr {
986        sys::bind_to(stream.sock(), local).map_err(|e| bind_error(local, e))?;
987    }
988    Ok(stream)
989}
990
991/// IPv6 first, then the families take turns.
992fn interleaved(addrs: &[SocketAddr]) -> Vec<SocketAddr> {
993    let (v6, v4): (Vec<_>, Vec<_>) = addrs.iter().copied().partition(SocketAddr::is_ipv6);
994    let (mut v6, mut v4) = (v6.into_iter(), v4.into_iter());
995    let mut out = Vec::with_capacity(addrs.len());
996    loop {
997        match (v6.next(), v4.next()) {
998            (None, None) => return out,
999            (a, b) => out.extend(a.into_iter().chain(b)),
1000        }
1001    }
1002}
1003
1004/// At most this many attempts run at once, and never two start closer than this.
1005const MAX_RACING: usize = 16;
1006const RACE_SPACING: Duration = Duration::from_millis(10);
1007
1008/// Happy Eyeballs: see [`ConnectOptions::happy_eyeballs`]. Dropping a losing attempt
1009/// resets it, so nothing lingers half open at the far end.
1010fn connect_racing(
1011    addrs: &[SocketAddr],
1012    options: &ConnectOptions,
1013) -> Result<TcpConnection, NetError> {
1014    let ordered = interleaved(addrs);
1015    let start = Instant::now();
1016    let deadline = start + options.timeout;
1017    let mut racing: Vec<(std::net::TcpStream, SocketAddr, Instant)> = Vec::new();
1018    let mut queue = ordered.iter().copied();
1019    let mut waiting = queue.next();
1020    let mut next_at = start;
1021    let mut last_start: Option<Instant> = None;
1022    let mut last = NetError::NoAddresses;
1023    loop {
1024        let now = Instant::now();
1025        if now >= deadline {
1026            break;
1027        }
1028        let spaced = last_start.map_or(now, |t| t + RACE_SPACING);
1029        if let Some(addr) = waiting.filter(|_| {
1030            racing.len() < MAX_RACING && now >= spaced && (now >= next_at || racing.is_empty())
1031        }) {
1032            waiting = queue.next();
1033            last_start = Some(now);
1034            next_at = now + options.attempt_delay;
1035            let stream = match dial_socket(addr, options) {
1036                Ok(s) => s,
1037                Err(e) => {
1038                    last = e;
1039                    next_at = now;
1040                    continue;
1041                }
1042            };
1043            match sys::connect_start(stream.sock(), addr) {
1044                Ok(false) => return dialed(stream, addr, options),
1045                Ok(true) => racing.push((stream, addr, now)),
1046                Err(e) => {
1047                    last = connect_error(addr, Duration::ZERO, e);
1048                    next_at = now;
1049                }
1050            }
1051            continue;
1052        }
1053        if racing.is_empty() && waiting.is_none() {
1054            break;
1055        }
1056        let mut until = deadline;
1057        if waiting.is_some() {
1058            until = until.min(next_at.max(spaced).max(now + Duration::from_millis(1)));
1059        }
1060        let wait = until.saturating_duration_since(now);
1061        if racing.is_empty() {
1062            std::thread::sleep(wait);
1063            continue;
1064        }
1065        let socks: Vec<_> = racing.iter().map(|(s, ..)| s.sock()).collect();
1066        let ready = sys::wait_writable(&socks, wait).map_err(|e| NetError::from_os("poll", e))?;
1067        drop(socks);
1068        for i in (0..racing.len()).rev() {
1069            if ready & (1 << i) == 0 {
1070                continue;
1071            }
1072            let (stream, addr, began) = racing.swap_remove(i);
1073            match sys::take_error(stream.sock()) {
1074                Ok(None) => {
1075                    for (loser, ..) in &racing {
1076                        let _ = sys::set_linger(loser.sock(), Some(Duration::ZERO));
1077                    }
1078                    return dialed(stream, addr, options);
1079                }
1080                Ok(Some(e)) => {
1081                    last = connect_error(addr, began.elapsed(), e);
1082                    next_at = Instant::now();
1083                }
1084                Err(e) => last = NetError::from_os("getsockopt", e),
1085            }
1086        }
1087    }
1088    for (loser, ..) in &racing {
1089        let _ = sys::set_linger(loser.sock(), Some(Duration::ZERO));
1090    }
1091    if matches!(last, NetError::NoAddresses) {
1092        if let Some(addr) = ordered.first() {
1093            last = NetError::ConnectTimeout {
1094                addr: *addr,
1095                after: options.timeout,
1096            };
1097        }
1098    }
1099    Err(last)
1100}
1101
1102fn connect_one(
1103    addr: SocketAddr,
1104    options: &ConnectOptions,
1105    attempt: Duration,
1106) -> Result<TcpConnection, NetError> {
1107    let stream = dial_socket(addr, options)?;
1108    let started = Instant::now();
1109    match sys::connect_start(stream.sock(), addr) {
1110        Ok(false) => {}
1111        Ok(true) => {
1112            match sys::wait(stream.sock(), Interest::Write, None, Some(attempt)) {
1113                Ok(Ready::TimedOut) => {
1114                    return Err(NetError::ConnectTimeout {
1115                        addr,
1116                        after: attempt,
1117                    });
1118                }
1119                Ok(_) => {}
1120                Err(e) => return Err(NetError::from_os("poll", e)),
1121            }
1122            match sys::take_error(stream.sock()) {
1123                Ok(None) => {}
1124                Ok(Some(e)) => return Err(connect_error(addr, started.elapsed(), e)),
1125                Err(e) => return Err(NetError::from_os("getsockopt", e)),
1126            }
1127        }
1128        Err(e) => return Err(connect_error(addr, started.elapsed(), e)),
1129    }
1130    dialed(stream, addr, options)
1131}
1132
1133/// A connection from a socket that just connected to `addr`.
1134fn dialed(
1135    stream: std::net::TcpStream,
1136    addr: SocketAddr,
1137    options: &ConnectOptions,
1138) -> Result<TcpConnection, NetError> {
1139    sys::prepare_dialed(&stream).map_err(|e| NetError::from_os("ioctl", e))?;
1140    let local = stream
1141        .local_addr()
1142        .map_err(|e| NetError::from_os("getsockname", e))?;
1143    let info = ConnectionInfo::new_tcp(
1144        crate::id::issue_single(),
1145        None,
1146        WorkerId(0),
1147        Origin::Dialed,
1148        local,
1149        crate::addr::normalize_socket(addr),
1150        Instant::now(),
1151    );
1152    Ok(TcpConnection::from_link(Link::new(
1153        sys::Stream::Tcp(stream),
1154        info,
1155        Arc::new(options.limits.effective(DriverKind::Blocking)),
1156        ClockRef::Monotonic,
1157        Arc::new(KillSlot::new()),
1158        WakeSource::Thread,
1159        Phase::Receiving,
1160    )))
1161}
1162
1163impl Read for TcpConnection {
1164    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
1165        self.recv(buf).map_err(io::Error::from)
1166    }
1167}
1168
1169impl Write for TcpConnection {
1170    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
1171        self.send(buf).map_err(io::Error::from)
1172    }
1173
1174    fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
1175        self.send_vectored(bufs).map_err(io::Error::from)
1176    }
1177
1178    fn flush(&mut self) -> io::Result<()> {
1179        Ok(())
1180    }
1181}
1182
1183impl Drop for TcpConnection {
1184    /// Never blocks: FIN, then close without the linger; use `close()` for the full graceful close.
1185    fn drop(&mut self) {
1186        if let Some(link) = self.link.take() {
1187            let reason = link.reason_or(CloseReason::SessionClosed);
1188            link.finish_quick(reason);
1189        }
1190    }
1191}
1192
1193impl std::fmt::Debug for TcpConnection {
1194    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1195        match &self.link {
1196            Some(l) => f
1197                .debug_struct("TcpConnection")
1198                .field("info", &l.info)
1199                .finish(),
1200            None => f.write_str("TcpConnection(closed)"),
1201        }
1202    }
1203}
1204
1205#[cfg(unix)]
1206impl std::os::fd::AsFd for TcpConnection {
1207    fn as_fd(&self) -> std::os::fd::BorrowedFd<'_> {
1208        std::os::fd::AsFd::as_fd(&self.link_ref().stream)
1209    }
1210}
1211
1212#[cfg(windows)]
1213impl std::os::windows::io::AsSocket for TcpConnection {
1214    fn as_socket(&self) -> std::os::windows::io::BorrowedSocket<'_> {
1215        std::os::windows::io::AsSocket::as_socket(&self.link_ref().stream)
1216    }
1217}
1218
1219#[cfg(test)]
1220mod tests {
1221    use super::*;
1222
1223    #[test]
1224    fn test_listen_accept_echo_round_trip() {
1225        let l = TcpListener::bind("127.0.0.1:0").unwrap();
1226        let addr = l.local_addr().unwrap();
1227        let t = std::thread::spawn(move || {
1228            let (mut c, info) = l.accept().unwrap();
1229            assert_eq!(info.origin, Origin::Accepted);
1230            let mut buf = [0u8; 4];
1231            c.recv_exact(&mut buf).unwrap();
1232            c.send_all(&buf).unwrap();
1233            c.close().unwrap();
1234        });
1235        let mut c = TcpConnection::connect(addr).unwrap();
1236        c.send_all(b"ping").unwrap();
1237        let mut buf = [0u8; 4];
1238        c.recv_exact(&mut buf).unwrap();
1239        assert_eq!(&buf, b"ping");
1240        assert_eq!(c.recv(&mut buf).unwrap(), 0);
1241        t.join().unwrap();
1242    }
1243
1244    #[test]
1245    fn test_connect_refused_maps_to_connection_refused() {
1246        // Bind then drop to get a free port with nothing listening.
1247        let port = std::net::TcpListener::bind("127.0.0.1:0")
1248            .unwrap()
1249            .local_addr()
1250            .unwrap()
1251            .port();
1252        let err = TcpConnection::connect_with(
1253            ("127.0.0.1", port),
1254            ConnectOptions::new().timeout(Duration::from_secs(5)),
1255        )
1256        .unwrap_err();
1257        assert!(
1258            matches!(
1259                err,
1260                NetError::ConnectionRefused { .. } | NetError::ConnectTimeout { .. }
1261            ),
1262            "{err:?}"
1263        );
1264    }
1265
1266    #[test]
1267    fn test_bind_in_use_maps_to_addr_in_use() {
1268        let a = TcpListener::bind("127.0.0.1:0").unwrap();
1269        let err = TcpListener::bind(a.local_addr().unwrap()).unwrap_err();
1270        assert!(matches!(err, NetError::AddrInUse { .. }), "{err:?}");
1271    }
1272
1273    /// Edge cases 127 and 128: bind errors map by class, whatever the OS's number.
1274    #[test]
1275    fn test_bind_errors_map_by_class() {
1276        #[cfg(unix)]
1277        use crate::sys::errno::{EACCES as ACCESS, EADDRNOTAVAIL as NOT_AVAILABLE};
1278        #[cfg(windows)]
1279        use crate::sys::errno::{WSAEACCES as ACCESS, WSAEADDRNOTAVAIL as NOT_AVAILABLE};
1280        let addr: SocketAddr = "[::1]:80".parse().unwrap();
1281        let e = bind_error(addr, io::Error::from_raw_os_error(NOT_AVAILABLE));
1282        assert!(matches!(e, NetError::AddrNotAvailable { .. }), "{e:?}");
1283        let e = bind_error(addr, io::Error::from_raw_os_error(ACCESS));
1284        assert!(matches!(e, NetError::PermissionDenied { .. }), "{e:?}");
1285    }
1286
1287    #[test]
1288    fn test_read_timeout_fires() {
1289        let l = TcpListener::bind("127.0.0.1:0").unwrap();
1290        let addr = l.local_addr().unwrap();
1291        let _server = std::thread::spawn(move || {
1292            let (c, _) = l.accept().unwrap();
1293            std::thread::sleep(Duration::from_secs(2));
1294            drop(c);
1295        });
1296        let limits = Limits::builder()
1297            .read_timeout(Duration::from_millis(200))
1298            .min_rate(crate::limits::MinRate::off())
1299            .build()
1300            .unwrap();
1301        let mut c =
1302            TcpConnection::connect_with(addr, ConnectOptions::new().limits(limits)).unwrap();
1303        let mut buf = [0u8; 8];
1304        let start = Instant::now();
1305        let err = c.recv(&mut buf).unwrap_err();
1306        assert!(
1307            matches!(
1308                err,
1309                NetError::TimedOut {
1310                    kind: TimeoutKind::Read
1311                }
1312            ),
1313            "{err:?}"
1314        );
1315        assert!(start.elapsed() < Duration::from_secs(2));
1316    }
1317
1318    #[test]
1319    fn test_kill_wakes_blocked_recv() {
1320        let l = TcpListener::bind("127.0.0.1:0").unwrap();
1321        let addr = l.local_addr().unwrap();
1322        let _server = std::thread::spawn(move || {
1323            let (c, _) = l.accept().unwrap();
1324            std::thread::sleep(Duration::from_secs(3));
1325            drop(c);
1326        });
1327        let mut c = TcpConnection::connect(addr).unwrap();
1328        let kill = c.kill_handle();
1329        std::thread::spawn(move || {
1330            std::thread::sleep(Duration::from_millis(100));
1331            assert!(kill.kill(crate::error::KillReason(4)));
1332        });
1333        let mut buf = [0u8; 8];
1334        let start = Instant::now();
1335        let err = c.recv(&mut buf).unwrap_err();
1336        assert!(matches!(err, NetError::Killed { .. }), "{err:?}");
1337        assert!(start.elapsed() < Duration::from_secs(2));
1338    }
1339
1340    #[test]
1341    fn test_interleaved_starts_with_v6_and_takes_turns() {
1342        let a = |t: &str| t.parse::<SocketAddr>().unwrap();
1343        let (v4a, v4b, v4c) = (a("1.1.1.1:1"), a("2.2.2.2:1"), a("3.3.3.3:1"));
1344        let (v6a, v6b) = (a("[::1]:1"), a("[::2]:1"));
1345        assert_eq!(
1346            interleaved(&[v4a, v4b, v4c, v6a, v6b]),
1347            [v6a, v4a, v6b, v4b, v4c]
1348        );
1349        assert_eq!(interleaved(&[v4a, v4b]), [v4a, v4b]);
1350        assert!(interleaved(&[]).is_empty());
1351    }
1352
1353    #[test]
1354    fn test_attempt_delay_is_held_to_its_range() {
1355        let low = ConnectOptions::new().attempt_delay(Duration::from_millis(1));
1356        assert_eq!(low.attempt_delay, Duration::from_millis(100));
1357        let high = ConnectOptions::new().attempt_delay(Duration::from_secs(9));
1358        assert_eq!(high.attempt_delay, Duration::from_secs(2));
1359    }
1360
1361    #[test]
1362    fn test_racing_a_refused_address_moves_on_at_once() {
1363        let closed = {
1364            let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1365            l.local_addr().unwrap()
1366        };
1367        let live = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1368        let options = ConnectOptions::new()
1369            .happy_eyeballs(true)
1370            .attempt_delay(Duration::from_secs(2));
1371        let started = Instant::now();
1372        let c = TcpConnection::connect_addrs(&[closed, live.local_addr().unwrap()], &options);
1373        assert_eq!(c.unwrap().peer_addr(), live.local_addr().unwrap());
1374        // Windows retries a refused connect for about 2 s before it reports it.
1375        if cfg!(unix) {
1376            assert!(
1377                started.elapsed() < Duration::from_secs(2),
1378                "no attempt delay"
1379            );
1380        }
1381    }
1382
1383    #[test]
1384    fn test_racing_every_address_refused_reports_the_last() {
1385        let closed: Vec<SocketAddr> = (0..2)
1386            .map(|_| {
1387                let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1388                l.local_addr().unwrap()
1389            })
1390            .collect();
1391        let options = ConnectOptions::new()
1392            .timeout(Duration::from_secs(5))
1393            .happy_eyeballs(true);
1394        let e = TcpConnection::connect_addrs(&closed, &options).err();
1395        assert!(
1396            matches!(e, Some(NetError::ConnectionRefused { .. })),
1397            "{e:?}"
1398        );
1399    }
1400
1401    /// A silent address (documentation range: nothing answers it) cannot hold the
1402    /// connection up longer than the attempt delay.
1403    #[test]
1404    fn test_racing_a_silent_address_starts_the_next_after_the_delay() {
1405        let silent: SocketAddr = "192.0.2.1:9".parse().unwrap();
1406        let live = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
1407        let options = ConnectOptions::new()
1408            .timeout(Duration::from_secs(5))
1409            .happy_eyeballs(true)
1410            .attempt_delay(Duration::from_millis(100));
1411        let started = Instant::now();
1412        let c = TcpConnection::connect_addrs(&[silent, live.local_addr().unwrap()], &options);
1413        assert_eq!(c.unwrap().peer_addr(), live.local_addr().unwrap());
1414        assert!(started.elapsed() < Duration::from_secs(2));
1415    }
1416
1417    #[test]
1418    fn test_listen_options_that_an_os_lacks_are_refused() {
1419        let bind = |o: ListenOptions| TcpListener::bind_with("127.0.0.1:0", o);
1420        let defer = ListenOptions::new().defer_accept(Some(Duration::from_secs(1)));
1421        let cpu = ListenOptions::new().incoming_cpu(Some(0));
1422        if cfg!(target_os = "linux") {
1423            assert!(bind(defer).is_ok());
1424            assert!(bind(cpu).is_ok());
1425        } else {
1426            assert!(matches!(bind(defer), Err(NetError::Unsupported { .. })));
1427            assert!(matches!(bind(cpu), Err(NetError::Unsupported { .. })));
1428        }
1429    }
1430
1431    /// Fast open is off by default on some hosts, and off means refused or ignored,
1432    /// never a broken listener.
1433    #[test]
1434    fn test_fast_open_listener_still_accepts() {
1435        let options = ListenOptions::new().fast_open(Some(16));
1436        if let Ok(l) = TcpListener::bind_with("127.0.0.1:0", options) {
1437            let addr = l.local_addr().unwrap();
1438            let _c = TcpConnection::connect(addr).unwrap();
1439            assert!(l.accept().is_ok());
1440        }
1441    }
1442
1443    #[test]
1444    fn test_user_timeout_where_the_os_has_it() {
1445        let l = TcpListener::bind("127.0.0.1:0").unwrap();
1446        let options = ConnectOptions::new()
1447            .tcp_options(TcpOptions::new().user_timeout(Duration::from_secs(5)));
1448        let c = TcpConnection::connect_with(l.local_addr().unwrap(), options);
1449        if cfg!(any(target_os = "linux", windows)) {
1450            assert!(c.is_ok());
1451        } else {
1452            assert!(matches!(c, Err(NetError::Unsupported { .. })));
1453        }
1454    }
1455}