1use 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#[non_exhaustive]
29#[derive(Clone, Debug)]
30pub struct ListenerInfo {
31 pub id: ListenerId,
33 pub name: Option<String>,
35 pub local: Endpoint,
37 pub backlog: Option<u32>,
39 pub transport: Transport,
41 pub proxy: ProxyMode,
43 pub tls: bool,
45}
46
47pub(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 Some(requested.min(65_535))
57 } else {
58 None
59 }
60}
61
62const 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#[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 pub fn bind<A: ToSocketAddrs>(addr: A) -> Result<TcpListener, NetError> {
136 TcpListener::bind_with(addr, ListenOptions::new())
137 }
138
139 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 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 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 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 pub(crate) fn set_tls(&mut self, on: bool) {
281 self.info.tls = on;
282 }
283
284 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 #[must_use]
295 pub fn info(&self) -> &ListenerInfo {
296 &self.info
297 }
298
299 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 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 #[must_use]
350 pub fn into_std(self) -> std::net::TcpListener {
351 self.inner
352 }
353
354 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#[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 pub fn new() -> ConnectOptions {
454 ConnectOptions::default()
455 }
456
457 pub fn timeout(mut self, total: Duration) -> ConnectOptions {
459 self.timeout = total;
460 self
461 }
462
463 pub fn local_addr(mut self, addr: SocketAddr) -> ConnectOptions {
465 self.local_addr = Some(addr);
466 self
467 }
468
469 pub fn tcp_options(mut self, options: TcpOptions) -> ConnectOptions {
471 self.tcp = options;
472 self
473 }
474
475 pub fn limits(mut self, limits: Limits) -> ConnectOptions {
477 self.limits = limits;
478 self
479 }
480
481 pub fn happy_eyeballs(mut self, on: bool) -> ConnectOptions {
486 self.happy_eyeballs = on;
487 self
488 }
489
490 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
498pub struct TcpConnection {
522 link: Option<Link>,
523}
524
525impl TcpConnection {
526 pub fn connect<A: ToSocketAddrs>(addr: A) -> Result<TcpConnection, NetError> {
532 TcpConnection::connect_with(addr, ConnectOptions::new())
533 }
534
535 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 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 #[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 None => unreachable_link(),
664 }
665 }
666
667 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 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 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 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 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 pub fn send(&mut self, buf: &[u8]) -> Result<usize, NetError> {
750 self.link()?.write(buf)
751 }
752
753 pub fn send_all(&mut self, buf: &[u8]) -> Result<(), NetError> {
760 self.link()?.write_all(buf)
761 }
762
763 pub fn send_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, NetError> {
769 self.link()?.write_vectored(bufs)
770 }
771
772 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 pub fn send_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), NetError> {
796 self.link()?.write_all_vectored(bufs)
797 }
798
799 pub fn shutdown(&mut self, how: Shutdown) -> Result<(), NetError> {
805 self.link()?.shutdown(how)
806 }
807
808 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 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 #[must_use]
837 pub fn info(&self) -> &ConnectionInfo {
838 &self.link_ref().info
839 }
840
841 #[must_use]
843 pub fn id(&self) -> ConnectionId {
844 self.link_ref().info.id
845 }
846
847 #[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 #[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 #[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 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 pub fn set_phase(&mut self, phase: Phase) {
888 if let Ok(link) = self.link() {
889 link.set_phase(phase);
890 }
891 }
892
893 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 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 #[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 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 std::process::abort()
972}
973
974fn 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
991fn 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
1004const MAX_RACING: usize = 16;
1006const RACE_SPACING: Duration = Duration::from_millis(10);
1007
1008fn 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
1133fn 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 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 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 #[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 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 #[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 #[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}