Skip to main content

iphone_cli/tcp/
adapter.rs

1//! Core TCP state machine.
2//!
3//! [`Adapter`] owns the underlying transport and manages all TCP connections.
4//! It is the entry point for both usage patterns exposed by this crate:
5//!
6//! - **Single-task use**:  keep the `Adapter` directly and open streams with
7//!   [`crate::tcp::stream::AdapterStream::connect`].
8//! - **Multi-task use**: call [`Adapter::to_async_handle`] to move the adapter
9//!   into a background task and obtain an [`crate::tcp::handle::AdapterHandle`] that
10//!   can be shared across threads.
11//!
12//! See the [crate-level documentation](crate) for a full comparison.
13
14use std::{
15    collections::{HashMap, VecDeque},
16    io::ErrorKind,
17    net::IpAddr,
18    sync::Arc,
19};
20
21use std::path::Path;
22
23use crate::tcp::time::Instant;
24use tokio::io::{AsyncReadExt, AsyncWriteExt};
25use tracing::{debug, trace, warn};
26
27use crate::tcp::packets::{Ipv4Packet, Ipv6Packet, ProtocolNumber, TcpFlags, TcpPacket, UdpPacket};
28use crate::tcp::{PcapLog, ReadWrite, packets::IpParseError};
29
30/// A received UDP datagram, addressed from `peer_ip:source_port` to the local
31/// `destination_port`. UDP is connectionless, so there is no socket state
32/// beyond the set of bound local ports.
33#[derive(Debug, Clone)]
34pub struct UdpDatagram {
35    pub source_port: u16,
36    pub destination_port: u16,
37    pub data: Vec<u8>,
38}
39
40/// Maximum number of retransmission attempts before a connection is killed.
41///
42/// iphone-cli: 6 instead of 5, and a first timeout of 500 ms instead of 200
43/// (below). Upstream gives a connection up after 12.6 s without an
44/// acknowledgement. This stack runs inside a tunnel that is itself a TCP
45/// stream to the phone, so nothing is ever lost on the way, only held up:
46/// when the phone's Wi-Fi stalls for longer than that, upstream closes
47/// connections that would have gone on working (seen 2026-11-11: "timed out
48/// after 5 retransmissions" on the accessibility connection over Wi-Fi).
49/// With these values a connection lasts 63.5 s without an acknowledgement.
50const MAX_RETRIES: u32 = 6;
51
52/// Initial retransmission timeout in milliseconds. Doubles on each retry.
53const INITIAL_RTO_MS: u64 = 500;
54
55const OUR_WSCALE: u8 = 8;
56
57/// Default cap on bytes in flight per connection, in bytes. Overridden via
58/// [`Adapter::set_send_window`].
59const DEFAULT_SEND_WINDOW: usize = 1 << 20; // 1 MiB
60
61// ---------------------------------------------------------------------------
62// Unacknowledged segment tracking
63// ---------------------------------------------------------------------------
64
65#[derive(Debug, Clone)]
66struct UnackedSegment {
67    seq: u32,
68    data: Arc<[u8]>,
69    sent_at: Instant,
70    retries: u32,
71}
72
73impl UnackedSegment {
74    fn rto(&self) -> std::time::Duration {
75        // 0.5, 1, 2, 4, 8, 16, 32 s (capped)
76        std::time::Duration::from_millis(INITIAL_RTO_MS << self.retries.min(6))
77    }
78
79    fn is_timed_out(&self) -> bool {
80        self.sent_at.elapsed() >= self.rto()
81    }
82}
83
84// ---------------------------------------------------------------------------
85// Connection state
86// ---------------------------------------------------------------------------
87
88#[derive(Debug, Clone)]
89struct ConnectionState {
90    /// Next sequence number we will send.
91    seq: u32,
92    /// Next sequence number we expect from the peer (= what we put in ACK field).
93    ack: u32,
94    host_port: u16,
95    peer_port: u16,
96    read_buffer: Vec<u8>,
97    write_buffer: VecDeque<u8>,
98    status: ConnectionStatus,
99    /// Segments we have sent but not yet received an ACK for, in send order.
100    unacked: VecDeque<UnackedSegment>,
101    bytes_in_flight: usize,
102    peer_window: u32,
103    peer_wscale: Option<u8>,
104}
105
106#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
107pub(crate) enum ConnectionStatus {
108    WaitingForSyn,
109    Connected,
110    Error(ErrorKind),
111}
112
113impl ConnectionState {
114    fn new(host_port: u16, peer_port: u16) -> Self {
115        Self {
116            seq: rand::random(),
117            ack: 0,
118            host_port,
119            peer_port,
120            read_buffer: Vec::new(),
121            write_buffer: VecDeque::new(),
122            status: ConnectionStatus::WaitingForSyn,
123            unacked: VecDeque::new(),
124            bytes_in_flight: 0,
125            peer_window: 0,
126            peer_wscale: None,
127        }
128    }
129}
130
131// ---------------------------------------------------------------------------
132// Sequence number helpers (wrapping arithmetic)
133// ---------------------------------------------------------------------------
134
135/// Returns true if `a < b` in TCP sequence-number space.
136#[inline]
137fn seq_lt(a: u32, b: u32) -> bool {
138    (b.wrapping_sub(a) as i32) > 0
139}
140
141/// Returns true if `a <= b` in TCP sequence-number space.
142#[inline]
143fn seq_lte(a: u32, b: u32) -> bool {
144    (b.wrapping_sub(a) as i32) >= 0
145}
146
147// ---------------------------------------------------------------------------
148// Adapter
149// ---------------------------------------------------------------------------
150
151/// A userspace TCP stack backed by an arbitrary [`crate::tcp::ReadWrite`] transport.
152///
153/// `Adapter` drives the TCP state machine: it sends and receives raw IPv4/IPv6
154/// packets over `peer`, maintains per-connection state, and implements
155/// stop-and-wait retransmission.
156///
157/// # Lifecycle
158///
159/// 1. Construct with [`Adapter::new`], supplying the framed transport and the
160///    local/remote IP addresses.
161/// 2. Open connections with [`crate::tcp::stream::AdapterStream::connect`] **or** call
162///    [`Adapter::to_async_handle`] to get a thread-safe [`crate::tcp::handle::AdapterHandle`].
163/// 3. Optionally enable PCAP logging with [`Adapter::pcap`].
164///
165/// See the [crate-level documentation](crate) for a full comparison of both
166/// usage patterns.
167/// Default MSS: IPv6 min MTU (1280) minus headers.
168const DEFAULT_MSS: usize = 1280 - 40 - 20;
169
170#[derive(Debug)]
171pub struct Adapter {
172    peer: Box<dyn ReadWrite>,
173    host_ip: IpAddr,
174    peer_ip: IpAddr,
175    states: HashMap<u16, ConnectionState>,
176    dropped: Vec<u16>,
177    read_buf: [u8; 65536],
178    bytes_in_buf: usize,
179    pcap: Option<PcapLog>,
180    /// Maximum Segment Size for TCP data (payload only, no headers).
181    mss: usize,
182    send_window: usize,
183    udp_sockets: HashMap<u16, VecDeque<UdpDatagram>>,
184    /// IP packets waiting to be written to `peer`, oldest first. Writes go
185    /// through here because the adapter's futures are raced in `select!`s and
186    /// may be dropped at any await: a cancelled `write_all` would leave a
187    /// partial IP packet on the transport and desync the peer's framing.
188    outbox: VecDeque<Vec<u8>>,
189    /// How much of `outbox.front()` has already been written.
190    outbox_offset: usize,
191}
192
193impl Adapter {
194    pub fn new(peer: Box<dyn ReadWrite>, host_ip: IpAddr, peer_ip: IpAddr) -> Self {
195        Self {
196            peer,
197            host_ip,
198            peer_ip,
199            states: HashMap::new(),
200            dropped: Vec::new(),
201            read_buf: [0u8; 65536],
202            bytes_in_buf: 0,
203            pcap: None,
204            mss: DEFAULT_MSS,
205            send_window: DEFAULT_SEND_WINDOW,
206            udp_sockets: HashMap::new(),
207            outbox: VecDeque::new(),
208            outbox_offset: 0,
209        }
210    }
211
212    /// Set the Maximum Segment Size (MSS) for outbound TCP segments.
213    ///
214    /// This should be `MTU - 40 (IPv6 header) - 20 (TCP header)` for the
215    /// tunnel you are using. Defaults to 1220 (based on IPv6 min MTU 1280).
216    pub fn set_mss(&mut self, mss: usize) -> &mut Self {
217        self.mss = mss;
218        self
219    }
220
221    pub fn set_send_window(&mut self, window: usize) -> &mut Self {
222        self.send_window = window;
223        self
224    }
225
226    /// The local (host) IP address of this tunnel endpoint.
227    pub fn host_ip(&self) -> IpAddr {
228        self.host_ip
229    }
230
231    /// The remote (peer/device) IP address of this tunnel endpoint.
232    pub fn peer_ip(&self) -> IpAddr {
233        self.peer_ip
234    }
235
236    /// Wraps this adapter in a thread-safe handle.
237    pub fn to_async_handle(self) -> crate::tcp::handle::AdapterHandle {
238        crate::tcp::handle::AdapterHandle::new(self)
239    }
240
241    // -----------------------------------------------------------------------
242    // Public API
243    // -----------------------------------------------------------------------
244
245    pub(crate) async fn connect(&mut self, port: u16) -> Result<u16, std::io::Error> {
246        let host_port = loop {
247            let p: u16 = rand::random();
248            if !self.states.contains_key(&p) {
249                break p;
250            }
251        };
252        let state = ConnectionState::new(host_port, port);
253        self.states.insert(host_port, state);
254
255        // Send initial SYN.
256        self.send_syn(host_port).await?;
257
258        // Wait for SYN-ACK, retransmitting the SYN on timeout.
259        let start = Instant::now();
260        let mut last_syn = Instant::now();
261        loop {
262            self.process_tcp_packet().await?;
263
264            if let Some(s) = self.states.get(&host_port) {
265                match s.status {
266                    ConnectionStatus::Connected => break,
267                    ConnectionStatus::Error(e) => {
268                        self.states.remove(&host_port);
269                        return Err(std::io::Error::new(e, "failed to connect"));
270                    }
271                    ConnectionStatus::WaitingForSyn => {
272                        if start.elapsed() > std::time::Duration::from_secs(10) {
273                            self.states.remove(&host_port);
274                            return Err(std::io::Error::new(ErrorKind::TimedOut, "SYN timed out"));
275                        }
276                        // Retransmit SYN every INITIAL_RTO_MS * 2^retry ms.
277                        if last_syn.elapsed() >= std::time::Duration::from_millis(INITIAL_RTO_MS) {
278                            debug!("retransmitting SYN for port {host_port}");
279                            self.send_syn(host_port).await?;
280                            last_syn = Instant::now();
281                        }
282                    }
283                }
284            }
285        }
286
287        Ok(host_port)
288    }
289
290        pub async fn pcap(&mut self, path: impl AsRef<Path>) -> Result<(), std::io::Error> {
291        let mut file = tokio::fs::File::create(path).await?;
292        file.write_all(&0xa1b2c3d4_u32.to_le_bytes()).await?;
293        file.write_all(&2_u16.to_le_bytes()).await?;
294        file.write_all(&4_u16.to_le_bytes()).await?;
295        file.write_all(&0_i32.to_le_bytes()).await?;
296        file.write_all(&0_u32.to_le_bytes()).await?;
297        file.write_all(&(u16::MAX as u32).to_le_bytes()).await?;
298        file.write_all(&101_u32.to_le_bytes()).await?;
299        self.pcap = Some(std::sync::Arc::new(tokio::sync::Mutex::new(file)));
300        Ok(())
301    }
302
303    pub(crate) async fn close(&mut self, host_port: u16) -> Result<(), std::io::Error> {
304        if let Some(state) = self.states.remove(&host_port) {
305            let tcp = TcpPacket::create(
306                self.host_ip,
307                self.peer_ip,
308                state.host_port,
309                state.peer_port,
310                state.seq,
311                state.ack,
312                TcpFlags {
313                    fin: true,
314                    ack: true,
315                    ..Default::default()
316                },
317                u16::MAX - 1,
318                &[],
319                &[],
320            );
321            let ip = self.ip_wrap(&tcp);
322            self.log_packet(&ip)?;
323            self.send_packet(ip).await?;
324            Ok(())
325        } else {
326            Err(std::io::Error::new(
327                ErrorKind::NotConnected,
328                "not connected",
329            ))
330        }
331    }
332
333    pub(crate) fn connection_drop(&mut self, host_port: u16) {
334        self.dropped.push(host_port);
335    }
336
337    pub(crate) fn queue_send(
338        &mut self,
339        payload: &[u8],
340        host_port: u16,
341    ) -> Result<(), std::io::Error> {
342        match self.states.get_mut(&host_port) {
343            Some(s) => {
344                s.write_buffer.extend(payload.iter().copied());
345                Ok(())
346            }
347            None => Err(std::io::Error::new(
348                ErrorKind::NotConnected,
349                "not connected",
350            )),
351        }
352    }
353
354    pub(crate) fn uncache(
355        &mut self,
356        to_copy: usize,
357        host_port: u16,
358    ) -> Result<Vec<u8>, std::io::Error> {
359        match self.states.get_mut(&host_port) {
360            Some(s) => {
361                let n = to_copy.min(s.read_buffer.len());
362                let out = s.read_buffer[..n].to_vec();
363                s.read_buffer.drain(..n);
364                Ok(out)
365            }
366            None => Err(std::io::Error::new(
367                ErrorKind::NotConnected,
368                "not connected",
369            )),
370        }
371    }
372
373    pub(crate) fn uncache_all(&mut self, host_port: u16) -> Result<Vec<u8>, std::io::Error> {
374        match self.states.get_mut(&host_port) {
375            Some(s) => {
376                let out = std::mem::take(&mut s.read_buffer);
377                Ok(out)
378            }
379            None => Err(std::io::Error::new(
380                ErrorKind::NotConnected,
381                "not connected",
382            )),
383        }
384    }
385
386    pub(crate) fn cache_read(
387        &mut self,
388        payload: &[u8],
389        host_port: u16,
390    ) -> Result<(), std::io::Error> {
391        match self.states.get_mut(&host_port) {
392            Some(s) => {
393                s.read_buffer.extend_from_slice(payload);
394                Ok(())
395            }
396            None => Err(std::io::Error::new(
397                ErrorKind::NotConnected,
398                "not connected",
399            )),
400        }
401    }
402
403    pub(crate) fn get_status(&self, host_port: u16) -> Result<ConnectionStatus, std::io::Error> {
404        match self.states.get(&host_port) {
405            Some(s) => Ok(s.status.clone()),
406            None => Err(std::io::Error::new(
407                ErrorKind::NotConnected,
408                "not connected",
409            )),
410        }
411    }
412
413    pub(crate) async fn recv(&mut self, host_port: u16) -> Result<Vec<u8>, std::io::Error> {
414        loop {
415            if let Some(state) = self.states.get_mut(&host_port) {
416                if !state.read_buffer.is_empty() {
417                    return Ok(std::mem::take(&mut state.read_buffer));
418                }
419                if let ConnectionStatus::Error(e) = state.status {
420                    return Err(std::io::Error::new(e, "socket io error"));
421                }
422            } else {
423                return Err(std::io::Error::new(
424                    ErrorKind::NotConnected,
425                    "not connected",
426                ));
427            }
428            self.process_tcp_packet().await?;
429        }
430    }
431
432    /// Queue an IP packet and write out as much of the outbox as possible.
433    async fn send_packet(&mut self, ip: Vec<u8>) -> Result<(), std::io::Error> {
434        self.outbox.push_back(ip);
435        self.flush_outbox().await
436    }
437
438    /// Write queued packets to the transport. Cancel safe: each `write` either
439    /// completes and is recorded in `outbox_offset`, or writes nothing.
440    async fn flush_outbox(&mut self) -> Result<(), std::io::Error> {
441        if self.outbox.is_empty() {
442            return Ok(());
443        }
444        while let Some(packet) = self.outbox.front() {
445            let n = self.peer.write(&packet[self.outbox_offset..]).await?;
446            if n == 0 {
447                return Err(std::io::Error::new(
448                    ErrorKind::WriteZero,
449                    "transport accepted no bytes",
450                ));
451            }
452            self.outbox_offset += n;
453            if self.outbox_offset == packet.len() {
454                self.outbox.pop_front();
455                self.outbox_offset = 0;
456            }
457        }
458        self.peer.flush().await
459    }
460
461    // -----------------------------------------------------------------------
462    // Flush / retransmit
463    // -----------------------------------------------------------------------
464
465    /// Returns true if any connection has unacked segments in flight, pending write-buffer bytes,
466    /// or connections waiting for SYN-ACK.
467    #[inline]
468    pub(crate) fn has_pending_work(&self) -> bool {
469        !self.dropped.is_empty()
470            || !self.outbox.is_empty()
471            || self.states.values().any(|s| {
472                !s.unacked.is_empty()
473                    || !s.write_buffer.is_empty()
474                    || matches!(s.status, ConnectionStatus::WaitingForSyn)
475            })
476    }
477
478    /// Drain pending write-buffer data into outbound segments, up to the
479    /// effective send window (`min(send_window, peer_window)`), then check
480    /// for timed-out retransmissions.
481    pub(crate) async fn write_buffer_flush(&mut self) -> Result<(), std::io::Error> {
482        // Finish writing anything a cancelled flush left behind first.
483        self.flush_outbox().await?;
484        // Check retransmissions first so a timed-out connection is marked before
485        // we attempt to send new data on it.
486        self.check_retransmissions().await?;
487
488        let host_ports: Vec<u16> = self.states.keys().cloned().collect();
489        for hp in host_ports {
490            // Inner loop: keep peeling off MSS-sized chunks until either the
491            // write buffer is empty or the window is full.
492            loop {
493                let chunk: Arc<[u8]> = {
494                    let Some(state) = self.states.get(&hp) else {
495                        break;
496                    };
497                    if state.write_buffer.is_empty() {
498                        break;
499                    }
500                    let cap = self.send_window.min(state.peer_window as usize);
501                    if state.bytes_in_flight >= cap {
502                        break;
503                    }
504                    let remaining = cap - state.bytes_in_flight;
505                    let n = state.write_buffer.len().min(self.mss).min(remaining);
506                    if n == 0 {
507                        break;
508                    }
509                    let v: Vec<u8> = state.write_buffer.iter().take(n).copied().collect();
510                    Arc::from(v)
511                };
512
513                let chunk_len = chunk.len();
514                if self.psh(chunk, hp).await.is_err() {
515                    break;
516                }
517                if let Some(state) = self.states.get_mut(&hp) {
518                    state.write_buffer.drain(..chunk_len);
519                }
520            }
521        }
522
523        // Reap connections that were dropped while we had the lock.
524        let dropped: Vec<u16> = std::mem::take(&mut self.dropped);
525        for hp in dropped {
526            if self.states.contains_key(&hp) {
527                self.close(hp).await.ok();
528            }
529        }
530
531        Ok(())
532    }
533
534    /// Check every connection for timed-out unacked segments. Retransmit the
535    /// oldest in-flight segment with exponential back-off; kill the connection
536    /// after MAX_RETRIES. Head-only retransmit suffices because TCP's
537    /// cumulative ACK semantics cause the peer to ACK everything they have
538    /// once the missing piece arrives.
539    async fn check_retransmissions(&mut self) -> Result<(), std::io::Error> {
540        let host_ports: Vec<u16> = self.states.keys().cloned().collect();
541
542        for hp in host_ports {
543            // Decide what to do before borrowing mutably.
544            enum Action {
545                Kill,
546                Retransmit { seq: u32, data: Arc<[u8]> },
547                None,
548            }
549
550            let action = match self.states.get(&hp) {
551                Some(state) => match state.unacked.front() {
552                    Some(seg) if seg.is_timed_out() => {
553                        if seg.retries >= MAX_RETRIES {
554                            Action::Kill
555                        } else {
556                            Action::Retransmit {
557                                seq: seg.seq,
558                                data: seg.data.clone(),
559                            }
560                        }
561                    }
562                    _ => Action::None,
563                },
564                None => Action::None,
565            };
566
567            match action {
568                Action::Kill => {
569                    warn!("hp={hp} timed out after {MAX_RETRIES} retransmissions; closing");
570                    if let Some(state) = self.states.get_mut(&hp) {
571                        state.status = ConnectionStatus::Error(ErrorKind::TimedOut);
572                        // Nothing will be sent on this connection again. Leaving the
573                        // segments queued would kill it again on every check and keep
574                        // reporting pending work forever.
575                        state.unacked.clear();
576                        state.bytes_in_flight = 0;
577                        state.write_buffer.clear();
578                    }
579                }
580                Action::Retransmit { seq, data } => {
581                    debug!("retransmitting {} bytes for hp={hp}", data.len());
582
583                    // Bump retry counter before the await so it's visible even if we're cancelled.
584                    if let Some(state) = self.states.get_mut(&hp)
585                        && let Some(seg) = state.unacked.front_mut()
586                    {
587                        seg.retries += 1;
588                        seg.sent_at = Instant::now();
589                    }
590
591                    let ip = {
592                        let Some(state) = self.states.get(&hp) else {
593                            continue;
594                        };
595                        let tcp = TcpPacket::create(
596                            self.host_ip,
597                            self.peer_ip,
598                            state.host_port,
599                            state.peer_port,
600                            seq,
601                            state.ack,
602                            TcpFlags {
603                                psh: true,
604                                ack: true,
605                                ..Default::default()
606                            },
607                            u16::MAX - 1,
608                            &[],
609                            &data,
610                        );
611                        self.ip_wrap(&tcp)
612                    };
613
614                    self.log_packet(&ip)?;
615                    self.send_packet(ip).await?;
616                }
617                Action::None => {}
618            }
619        }
620
621        Ok(())
622    }
623
624    // -----------------------------------------------------------------------
625    // Packet processing
626    // -----------------------------------------------------------------------
627
628    pub(crate) async fn process_tcp_packet(&mut self) -> Result<(), std::io::Error> {
629        tokio::select! {
630            ip_packet = self.read_ip_packet() => {
631                let (protocol, payload) = ip_packet?;
632                match protocol {
633                    p if p == ProtocolNumber::Tcp as u8 => {
634                        self.process_tcp_packet_from_payload(&payload).await
635                    }
636                    p if p == ProtocolNumber::Udp as u8 => {
637                        self.process_udp_packet(&payload);
638                        Ok(())
639                    }
640                    _ => Ok(()),
641                }
642            }
643            // Short timeout so retransmissions fire in the AdapterStream case
644            // (the AdapterHandle has a 1ms tick that calls write_buffer_flush directly).
645            _ = crate::tcp::time::sleep(std::time::Duration::from_millis(500)) => {
646                self.check_retransmissions().await
647            }
648        }
649    }
650
651    /// Parse an inbound UDP datagram and queue it if its destination port is
652    /// bound
653    fn process_udp_packet(&mut self, payload: &[u8]) {
654        let udp = match UdpPacket::parse(payload) {
655            Ok(u) => u,
656            Err(_) => return,
657        };
658        if let Some(queue) = self.udp_sockets.get_mut(&udp.destination_port) {
659            trace!(
660                "udp {} bytes -> local port {}",
661                udp.payload.len(),
662                udp.destination_port
663            );
664            queue.push_back(UdpDatagram {
665                source_port: udp.source_port,
666                destination_port: udp.destination_port,
667                data: udp.payload,
668            });
669        }
670    }
671
672    /// Bind a local UDP port so inbound datagrams to it are queued
673    pub(crate) fn bind_udp(&mut self, port: u16) -> u16 {
674        let port = if port == 0 {
675            loop {
676                let p: u16 = rand::random();
677                if p >= 1024 && !self.udp_sockets.contains_key(&p) {
678                    break p;
679                }
680            }
681        } else {
682            port
683        };
684        self.udp_sockets.entry(port).or_default();
685        port
686    }
687
688    pub(crate) fn unbind_udp(&mut self, port: u16) {
689        self.udp_sockets.remove(&port);
690    }
691
692    /// Send a UDP datagram from `source_port` to the peer's `destination_port`.
693    pub(crate) async fn send_udp(
694        &mut self,
695        source_port: u16,
696        destination_port: u16,
697        data: &[u8],
698    ) -> Result<(), std::io::Error> {
699        let udp = UdpPacket::create(
700            self.host_ip,
701            self.peer_ip,
702            source_port,
703            destination_port,
704            data,
705        );
706        let ip = self.ip_wrap_proto(&udp, ProtocolNumber::Udp);
707        self.log_packet(&ip)?;
708        self.send_packet(ip).await
709    }
710
711    /// Drain all queued inbound UDP datagrams across every bound port.
712    pub(crate) fn udp_drain(&mut self) -> Vec<UdpDatagram> {
713        let mut out = Vec::new();
714        for queue in self.udp_sockets.values_mut() {
715            out.extend(queue.drain(..));
716        }
717        out
718    }
719
720    pub(crate) async fn process_tcp_packet_from_payload(
721        &mut self,
722        payload: &[u8],
723    ) -> Result<(), std::io::Error> {
724        let res = TcpPacket::parse(payload)?;
725        let mut ack_me = None;
726
727        if let Some(state) = self.states.get_mut(&res.destination_port) {
728            // ------------------------------------------------------------------
729            // ACK processing: advance the unacked window.
730            // The peer's acknowledgment_number is the next seq they expect from us,
731            // so any segment with (seg.seq + seg.len) <= ack_num has been received.
732            // ------------------------------------------------------------------
733            if res.flags.ack {
734                let ack_num = res.acknowledgment_number;
735                while let Some(seg) = state.unacked.front() {
736                    let seg_end = seg.seq.wrapping_add(seg.data.len() as u32);
737                    if seq_lte(seg_end, ack_num) {
738                        let popped = state.unacked.pop_front().unwrap();
739                        state.bytes_in_flight =
740                            state.bytes_in_flight.saturating_sub(popped.data.len());
741                    } else {
742                        break;
743                    }
744                }
745            }
746
747            // Update peer's advertised receive window
748            if !(res.flags.syn) {
749                let shift = state.peer_wscale.unwrap_or(0);
750                state.peer_window = (res.window_size as u32) << shift;
751            }
752
753            // ------------------------------------------------------------------
754            // RST: hard close.
755            // ------------------------------------------------------------------
756            if res.flags.rst {
757                warn!("RST on hp={}", res.destination_port);
758                state.status = ConnectionStatus::Error(ErrorKind::ConnectionReset);
759                return Ok(());
760            }
761
762            match state.status {
763                ConnectionStatus::WaitingForSyn => {
764                    // We expect a SYN-ACK.
765                    if res.flags.syn && res.flags.ack {
766                        // SYN-ACK: peer's SYN consumes one seq number.
767                        state.ack = res.sequence_number.wrapping_add(1);
768                        // Our SYN consumed one sequence number on our side.
769                        state.seq = state.seq.wrapping_add(1);
770                        state.status = ConnectionStatus::Connected;
771                        state.peer_wscale = crate::tcp::packets::parse_window_scale(&res.options);
772                        let shift = state.peer_wscale.unwrap_or(0);
773                        state.peer_window = (res.window_size as u32) << shift;
774                        ack_me = Some(res.destination_port);
775                    }
776                    // Anything else while waiting for SYN-ACK is ignored.
777                }
778
779                ConnectionStatus::Connected => {
780                    // ----------------------------------------------------------
781                    // Keep-alive probe: seq == RCV.NXT - 1, no payload.
782                    // Just re-ACK without changing state.
783                    // ----------------------------------------------------------
784                    let is_keepalive = res.payload.is_empty()
785                        && !res.flags.fin
786                        && res.sequence_number.wrapping_add(1) == state.ack;
787
788                    let seg_end = res.sequence_number.wrapping_add(res.payload.len() as u32);
789
790                    if is_keepalive {
791                        debug!("keep-alive on hp={}", res.destination_port);
792                        ack_me = Some(res.destination_port);
793                    } else if !res.payload.is_empty() {
794                        // ------------------------------------------------------
795                        // Data segment: enforce in-order delivery.
796                        // ------------------------------------------------------
797                        if seq_lte(res.sequence_number, state.ack) && seq_lt(state.ack, seg_end) {
798                            let skip = state.ack.wrapping_sub(res.sequence_number) as usize;
799                            if skip > 0 {
800                                debug!(
801                                    "overlapping data seq={} expected={} hp={}",
802                                    res.sequence_number, state.ack, res.destination_port
803                                );
804                            }
805                            state.read_buffer.extend(&res.payload[skip..]);
806                            state.ack = seg_end;
807                        } else if seq_lt(res.sequence_number, state.ack) {
808                            // Duplicate (already received): re-ACK, don't buffer.
809                            debug!(
810                                "duplicate data seq={} expected={} hp={}",
811                                res.sequence_number, state.ack, res.destination_port
812                            );
813                        } else {
814                            debug!(
815                                "out-of-order seq={} expected={} hp={}",
816                                res.sequence_number, state.ack, res.destination_port
817                            );
818                        }
819                        ack_me = Some(res.destination_port);
820                    }
821
822                    if res.flags.fin {
823                        if seg_end == state.ack {
824                            state.ack = state.ack.wrapping_add(1);
825                            state.status = ConnectionStatus::Error(ErrorKind::UnexpectedEof);
826                        }
827                        ack_me = Some(res.destination_port);
828                    }
829                }
830
831                ConnectionStatus::Error(_) => {
832                    // Connection is already dead; ignore everything.
833                    trace!(
834                        "packet received on errored connection hp={}",
835                        res.destination_port
836                    );
837                }
838            }
839        }
840
841        if let Some(hp) = ack_me {
842            self.ack(hp).await?;
843        }
844        Ok(())
845    }
846
847    // -----------------------------------------------------------------------
848    // Low-level send helpers
849    // -----------------------------------------------------------------------
850
851    async fn send_syn(&mut self, host_port: u16) -> Result<(), std::io::Error> {
852        let Some(state) = self.states.get(&host_port) else {
853            return Err(std::io::Error::new(
854                ErrorKind::NotConnected,
855                "not connected",
856            ));
857        };
858        // Advertise window scaling so the peer can scale its window field.
859        let wscale = crate::tcp::packets::window_scale_option(OUR_WSCALE);
860        let tcp = TcpPacket::create(
861            self.host_ip,
862            self.peer_ip,
863            state.host_port,
864            state.peer_port,
865            state.seq,
866            0,
867            TcpFlags {
868                syn: true,
869                ..Default::default()
870            },
871            u16::MAX - 1,
872            &wscale,
873            &[],
874        );
875        let ip = self.ip_wrap(&tcp);
876        self.log_packet(&ip)?;
877        self.send_packet(ip).await
878    }
879
880    async fn ack(&mut self, host_port: u16) -> Result<(), std::io::Error> {
881        let Some(state) = self.states.get(&host_port) else {
882            return Err(std::io::Error::new(
883                ErrorKind::NotConnected,
884                "not connected",
885            ));
886        };
887        let tcp = TcpPacket::create(
888            self.host_ip,
889            self.peer_ip,
890            state.host_port,
891            state.peer_port,
892            state.seq,
893            state.ack,
894            TcpFlags {
895                ack: true,
896                ..Default::default()
897            },
898            u16::MAX - 1,
899            &[],
900            &[],
901        );
902        let ip = self.ip_wrap(&tcp);
903        let _ = state;
904        self.log_packet(&ip)?;
905        self.send_packet(ip).await
906    }
907
908    /// Send a PSH segment and record it in the unacked queue.
909    async fn psh(&mut self, data: Arc<[u8]>, host_port: u16) -> Result<(), std::io::Error> {
910        let Some(state) = self.states.get(&host_port) else {
911            return Err(std::io::Error::new(
912                ErrorKind::NotConnected,
913                "not connected",
914            ));
915        };
916        if let ConnectionStatus::Error(e) = state.status {
917            return Err(std::io::Error::new(e, "socket error"));
918        }
919        trace!("psh {} bytes on hp={host_port}", data.len());
920        let seq = state.seq;
921        let tcp = TcpPacket::create(
922            self.host_ip,
923            self.peer_ip,
924            state.host_port,
925            state.peer_port,
926            seq,
927            state.ack,
928            TcpFlags {
929                psh: true,
930                ack: true,
931                ..Default::default()
932            },
933            u16::MAX - 1,
934            &[],
935            &data,
936        );
937        let ip = self.ip_wrap(&tcp);
938        let _ = state;
939
940        // Record the segment before the await: the packet is queued either way, so
941        // a cancelled flush must not leave it to be sent again under a stale seq.
942        if let Some(state) = self.states.get_mut(&host_port) {
943            let len = data.len();
944            state.bytes_in_flight += len;
945            state.unacked.push_back(UnackedSegment {
946                seq,
947                data,
948                sent_at: Instant::now(),
949                retries: 0,
950            });
951            state.seq = state.seq.wrapping_add(len as u32);
952        }
953
954        self.log_packet(&ip)?;
955        self.send_packet(ip).await
956    }
957
958    async fn read_ip_packet(&mut self) -> Result<(u8, Vec<u8>), std::io::Error> {
959        self.write_buffer_flush().await?;
960        loop {
961            let parsed = match self.host_ip {
962                IpAddr::V4(_) => Self::try_parse_v4(&self.read_buf[..self.bytes_in_buf]),
963                IpAddr::V6(_) => {
964                    match Ipv6Packet::parse(&self.read_buf[..self.bytes_in_buf], &self.pcap) {
965                        IpParseError::Ok {
966                            packet,
967                            bytes_consumed,
968                        } => IpParseError::Ok {
969                            packet: (packet.next_header, packet.payload),
970                            bytes_consumed,
971                        },
972                        IpParseError::NotEnough => IpParseError::NotEnough,
973                        IpParseError::Invalid => IpParseError::Invalid,
974                    }
975                }
976            };
977            match parsed {
978                IpParseError::Ok {
979                    packet,
980                    bytes_consumed,
981                } => {
982                    if let Some(pcap) = &self.pcap
983                        && matches!(self.host_ip, IpAddr::V4(_))
984                    {
985                        crate::tcp::log_packet(pcap, &self.read_buf[..bytes_consumed]);
986                    }
987
988                    self.read_buf
989                        .copy_within(bytes_consumed..self.bytes_in_buf, 0);
990                    self.bytes_in_buf -= bytes_consumed;
991                    return Ok(packet);
992                }
993                IpParseError::NotEnough => {}
994                IpParseError::Invalid => {
995                    let kind = if matches!(self.host_ip, IpAddr::V4(_)) {
996                        "invalid IPv4 packet"
997                    } else {
998                        "invalid IPv6 packet"
999                    };
1000                    return Err(std::io::Error::new(ErrorKind::InvalidData, kind));
1001                }
1002            }
1003            let n = self
1004                .peer
1005                .read(&mut self.read_buf[self.bytes_in_buf..])
1006                .await?;
1007            if n == 0 {
1008                return Err(std::io::Error::new(
1009                    ErrorKind::UnexpectedEof,
1010                    "transport closed",
1011                ));
1012            }
1013            self.bytes_in_buf += n;
1014        }
1015    }
1016
1017    /// Inspect the buffer for a complete IPv4 packet. Returns `(protocol,
1018    /// payload)` and the number of bytes consumed.
1019    fn try_parse_v4(buf: &[u8]) -> IpParseError<(u8, Vec<u8>)> {
1020        if buf.len() < 20 {
1021            return IpParseError::NotEnough;
1022        }
1023        if (buf[0] >> 4) != 4 {
1024            return IpParseError::Invalid;
1025        }
1026        let ihl_bytes = ((buf[0] & 0x0F) as usize) * 4;
1027        let total_length = u16::from_be_bytes([buf[2], buf[3]]) as usize;
1028        if ihl_bytes < 20 || total_length < ihl_bytes {
1029            return IpParseError::Invalid;
1030        }
1031        if buf.len() < total_length {
1032            return IpParseError::NotEnough;
1033        }
1034        let packet = match Ipv4Packet::parse(&buf[..total_length]) {
1035            Some(p) => p,
1036            None => return IpParseError::Invalid,
1037        };
1038        IpParseError::Ok {
1039            packet: (packet.protocol, packet.payload),
1040            bytes_consumed: total_length,
1041        }
1042    }
1043
1044    fn log_packet(&self, packet: &[u8]) -> Result<(), std::io::Error> {
1045        if let Some(file) = &self.pcap {
1046            crate::tcp::log_packet(file, packet);
1047        }
1048        Ok(())
1049    }
1050
1051    fn ip_wrap(&self, packet: &[u8]) -> Vec<u8> {
1052        self.ip_wrap_proto(packet, ProtocolNumber::Tcp)
1053    }
1054
1055    fn ip_wrap_proto(&self, packet: &[u8], protocol: ProtocolNumber) -> Vec<u8> {
1056        match (self.host_ip, self.peer_ip) {
1057            (IpAddr::V4(src), IpAddr::V4(dst)) => {
1058                Ipv4Packet::create(src, dst, protocol, 255, packet)
1059            }
1060            (IpAddr::V6(src), IpAddr::V6(dst)) => {
1061                Ipv6Packet::create(src, dst, protocol, 255, packet)
1062            }
1063            _ => panic!("host_ip and peer_ip must be the same IP version"),
1064        }
1065    }
1066}
1067
1068// ---------------------------------------------------------------------------
1069// Tests
1070// ---------------------------------------------------------------------------
1071
1072#[cfg(test)]
1073mod tests {
1074    use super::*;
1075    use crate::tcp::packets::{IpParseError, ProtocolNumber, TcpFlags, TcpPacket};
1076    use std::net::{IpAddr, Ipv6Addr};
1077    use std::pin::Pin;
1078    use std::task::{Context, Poll};
1079    use std::time::Duration;
1080    use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, DuplexStream, ReadBuf};
1081
1082    const HOST_IP: Ipv6Addr = Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 1);
1083    const PEER_IP: Ipv6Addr = Ipv6Addr::new(0xfd00, 0, 0, 0, 0, 0, 0, 2);
1084    const PEER_PORT: u16 = 80;
1085    /// Peer's initial sequence number
1086    const PEER_ISN: u32 = 5000;
1087
1088    // ------------------------------------------------------------------
1089    // Test transport
1090    // ------------------------------------------------------------------
1091
1092    struct TestTransport(DuplexStream);
1093
1094    impl std::fmt::Debug for TestTransport {
1095        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1096            write!(f, "TestTransport")
1097        }
1098    }
1099
1100    impl AsyncRead for TestTransport {
1101        fn poll_read(
1102            mut self: Pin<&mut Self>,
1103            cx: &mut Context<'_>,
1104            buf: &mut ReadBuf<'_>,
1105        ) -> Poll<std::io::Result<()>> {
1106            Pin::new(&mut self.0).poll_read(cx, buf)
1107        }
1108    }
1109
1110    impl AsyncWrite for TestTransport {
1111        fn poll_write(
1112            mut self: Pin<&mut Self>,
1113            cx: &mut Context<'_>,
1114            buf: &[u8],
1115        ) -> Poll<std::io::Result<usize>> {
1116            Pin::new(&mut self.0).poll_write(cx, buf)
1117        }
1118        fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
1119            Pin::new(&mut self.0).poll_flush(cx)
1120        }
1121        fn poll_shutdown(
1122            mut self: Pin<&mut Self>,
1123            cx: &mut Context<'_>,
1124        ) -> Poll<std::io::Result<()>> {
1125            Pin::new(&mut self.0).poll_shutdown(cx)
1126        }
1127    }
1128
1129    // ------------------------------------------------------------------
1130    // Packet helpers
1131    // ------------------------------------------------------------------
1132
1133    /// Build a raw TCP segment (not IP-wrapped) from PEER→adapter.
1134    fn tcp_seg(dst_port: u16, seq: u32, ack_num: u32, flags: TcpFlags, payload: &[u8]) -> Vec<u8> {
1135        tcp_seg_with_opts(dst_port, seq, ack_num, flags, &[], payload)
1136    }
1137
1138    fn tcp_seg_with_opts(
1139        dst_port: u16,
1140        seq: u32,
1141        ack_num: u32,
1142        flags: TcpFlags,
1143        options: &[u8],
1144        payload: &[u8],
1145    ) -> Vec<u8> {
1146        TcpPacket::create(
1147            IpAddr::V6(PEER_IP),
1148            IpAddr::V6(HOST_IP),
1149            PEER_PORT,
1150            dst_port,
1151            seq,
1152            ack_num,
1153            flags,
1154            u16::MAX - 1,
1155            options,
1156            payload,
1157        )
1158    }
1159
1160    /// Build an IPv6-wrapped TCP packet from PEER→adapter (for injection into transport).
1161    fn peer_ipv6_pkt(
1162        dst_port: u16,
1163        seq: u32,
1164        ack_num: u32,
1165        flags: TcpFlags,
1166        payload: &[u8],
1167    ) -> Vec<u8> {
1168        let tcp = tcp_seg(dst_port, seq, ack_num, flags, payload);
1169        crate::tcp::packets::Ipv6Packet::create(PEER_IP, HOST_IP, ProtocolNumber::Tcp, 255, &tcp)
1170    }
1171
1172    /// Read one complete IPv6 packet the adapter wrote to the transport.
1173    async fn read_pkt(reader: &mut (impl AsyncRead + Unpin)) -> TcpPacket {
1174        let mut hdr = [0u8; 40];
1175        reader.read_exact(&mut hdr).await.unwrap();
1176        let plen = u16::from_be_bytes([hdr[4], hdr[5]]) as usize;
1177        let mut rest = vec![0u8; plen];
1178        reader.read_exact(&mut rest).await.unwrap();
1179        let mut full = hdr.to_vec();
1180        full.extend_from_slice(&rest);
1181        let ip = match crate::tcp::packets::Ipv6Packet::parse(&full, &None) {
1182            IpParseError::Ok { packet, .. } => packet,
1183            _ => panic!("not a valid IPv6 packet from adapter"),
1184        };
1185        TcpPacket::parse(&ip.payload).unwrap()
1186    }
1187
1188    // ------------------------------------------------------------------
1189    // Handshake helper
1190    // ------------------------------------------------------------------
1191
1192    /// Drive the adapter's `connect` and the test-peer response concurrently.
1193    /// Returns the host port the adapter chose.
1194    async fn handshake(
1195        adapter: &mut Adapter,
1196        test_rx: &mut (impl AsyncRead + Unpin),
1197        test_tx: &mut (impl AsyncWrite + Unpin),
1198    ) -> u16 {
1199        let (hp_result, peer_hp) = tokio::join!(adapter.connect(PEER_PORT), async {
1200            // Read the SYN the adapter sends immediately on connect().
1201            let syn = read_pkt(test_rx).await;
1202            assert!(syn.flags.syn, "first packet from adapter should be SYN");
1203            let hp = syn.source_port;
1204
1205            // Send SYN-ACK.
1206            test_tx
1207                .write_all(&peer_ipv6_pkt(
1208                    hp,
1209                    PEER_ISN,
1210                    syn.sequence_number.wrapping_add(1),
1211                    TcpFlags {
1212                        syn: true,
1213                        ack: true,
1214                        ..Default::default()
1215                    },
1216                    &[],
1217                ))
1218                .await
1219                .unwrap();
1220
1221            // Consume the ACK the adapter sends to complete the handshake.
1222            let ack = read_pkt(test_rx).await;
1223            assert!(
1224                ack.flags.ack,
1225                "adapter should send ACK to complete handshake"
1226            );
1227
1228            hp
1229        });
1230        let hp = hp_result.expect("connect failed");
1231        assert_eq!(hp, peer_hp);
1232        hp
1233    }
1234
1235    // ------------------------------------------------------------------
1236    // Tests
1237    // ------------------------------------------------------------------
1238
1239    /// After the RTO elapses without an ACK, the same segment is retransmitted
1240    /// with the same sequence number.
1241    #[tokio::test]
1242    async fn retransmit_fires_after_rto() {
1243        tokio::time::pause();
1244
1245        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1246        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1247        let mut adapter = Adapter::new(
1248            Box::new(TestTransport(adapter_end)),
1249            IpAddr::V6(HOST_IP),
1250            IpAddr::V6(PEER_IP),
1251        );
1252
1253        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1254
1255        // Queue and flush data; the adapter sends a PSH segment.
1256        adapter.queue_send(b"hello", hp).unwrap();
1257        adapter.write_buffer_flush().await.unwrap();
1258
1259        let psh1 = read_pkt(&mut test_rx).await;
1260        assert_eq!(psh1.payload, b"hello");
1261        let original_seq = psh1.sequence_number;
1262
1263        // Advance past the initial RTO without ACKing.
1264        tokio::time::advance(Duration::from_millis(INITIAL_RTO_MS + 1)).await;
1265        adapter.write_buffer_flush().await.unwrap();
1266
1267        // The adapter should retransmit the same segment.
1268        let psh2 = read_pkt(&mut test_rx).await;
1269        assert_eq!(
1270            psh2.payload, b"hello",
1271            "retransmit payload must match original"
1272        );
1273        assert_eq!(
1274            psh2.sequence_number, original_seq,
1275            "retransmit must reuse the original sequence number"
1276        );
1277    }
1278
1279    /// Advancing through all retry intervals marks the connection as
1280    /// `Error(TimedOut)` once MAX_RETRIES is exhausted.
1281    #[tokio::test]
1282    async fn connection_killed_after_max_retries() {
1283        tokio::time::pause();
1284
1285        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1286        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1287        let mut adapter = Adapter::new(
1288            Box::new(TestTransport(adapter_end)),
1289            IpAddr::V6(HOST_IP),
1290            IpAddr::V6(PEER_IP),
1291        );
1292
1293        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1294
1295        adapter.queue_send(b"data", hp).unwrap();
1296        adapter.write_buffer_flush().await.unwrap();
1297        read_pkt(&mut test_rx).await; // initial send
1298
1299        // Drive through MAX_RETRIES retransmits (each with its doubled RTO),
1300        // then one more check which should kill the connection.
1301        for i in 0..=MAX_RETRIES {
1302            let rto = Duration::from_millis(INITIAL_RTO_MS << i.min(6));
1303            tokio::time::advance(rto + Duration::from_millis(1)).await;
1304            adapter.write_buffer_flush().await.unwrap();
1305
1306            if i < MAX_RETRIES {
1307                // Drain the retransmit so the duplex buffer doesn't fill up.
1308                read_pkt(&mut test_rx).await;
1309            }
1310            // On the final iteration the connection is killed instead of retransmitting.
1311        }
1312
1313        assert_eq!(
1314            adapter.get_status(hp).unwrap(),
1315            ConnectionStatus::Error(ErrorKind::TimedOut),
1316            "connection should be Error(TimedOut) after exhausting retries"
1317        );
1318    }
1319
1320    #[tokio::test]
1321    async fn ack_clears_unacked_queue() {
1322        tokio::time::pause();
1323
1324        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1325        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1326        let mut adapter = Adapter::new(
1327            Box::new(TestTransport(adapter_end)),
1328            IpAddr::V6(HOST_IP),
1329            IpAddr::V6(PEER_IP),
1330        );
1331        adapter.set_send_window(5);
1332
1333        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1334
1335        adapter.queue_send(b"first", hp).unwrap();
1336        adapter.write_buffer_flush().await.unwrap();
1337        let psh1 = read_pkt(&mut test_rx).await;
1338        assert_eq!(psh1.payload, b"first");
1339
1340        // Queue second chunk; window is full, must be held.
1341        adapter.queue_send(b"secnd", hp).unwrap();
1342        adapter.write_buffer_flush().await.unwrap();
1343
1344        // Nothing should appear on the wire yet.
1345        let nothing = tokio::time::timeout(Duration::from_millis(10), read_pkt(&mut test_rx)).await;
1346        assert!(
1347            nothing.is_err(),
1348            "second chunk must not be sent while window is full"
1349        );
1350
1351        // ACK the first chunk (ack = psh1.seq + len).
1352        let peer_ack_num = psh1.sequence_number.wrapping_add(psh1.payload.len() as u32);
1353        adapter
1354            .process_tcp_packet_from_payload(&tcp_seg(
1355                hp,
1356                PEER_ISN + 1,
1357                peer_ack_num,
1358                TcpFlags {
1359                    ack: true,
1360                    ..Default::default()
1361                },
1362                &[],
1363            ))
1364            .await
1365            .unwrap();
1366
1367        // ACK frees window; second chunk goes out on the next flush.
1368        adapter.write_buffer_flush().await.unwrap();
1369        let psh2 = read_pkt(&mut test_rx).await;
1370        assert_eq!(psh2.payload, b"secnd");
1371        assert_eq!(
1372            psh2.sequence_number, peer_ack_num,
1373            "second segment starts where first left off"
1374        );
1375    }
1376
1377    /// With a window larger than `mss`, multiple segments go out back-to-back
1378    /// before any ACK is received.
1379    #[tokio::test]
1380    async fn sliding_window_sends_multiple_segments() {
1381        tokio::time::pause();
1382
1383        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1384        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1385        let mut adapter = Adapter::new(
1386            Box::new(TestTransport(adapter_end)),
1387            IpAddr::V6(HOST_IP),
1388            IpAddr::V6(PEER_IP),
1389        );
1390        adapter.set_mss(10);
1391        adapter.set_send_window(40); // 4 segments worth.
1392
1393        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1394
1395        adapter.queue_send(&[b'x'; 35], hp).unwrap();
1396        adapter.write_buffer_flush().await.unwrap();
1397
1398        let mut total = 0;
1399        for _ in 0..4 {
1400            let pkt = read_pkt(&mut test_rx).await;
1401            total += pkt.payload.len();
1402        }
1403        assert_eq!(total, 35, "all 35 bytes should be on the wire pre-ACK");
1404    }
1405
1406    /// `send_window` caps how much can be in flight; data past the cap waits.
1407    #[tokio::test]
1408    async fn send_window_caps_in_flight_bytes() {
1409        tokio::time::pause();
1410
1411        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1412        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1413        let mut adapter = Adapter::new(
1414            Box::new(TestTransport(adapter_end)),
1415            IpAddr::V6(HOST_IP),
1416            IpAddr::V6(PEER_IP),
1417        );
1418        adapter.set_mss(10);
1419        adapter.set_send_window(20);
1420
1421        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1422
1423        adapter.queue_send(&[b'x'; 50], hp).unwrap();
1424        adapter.write_buffer_flush().await.unwrap();
1425
1426        // First 20 bytes go out as two 10-byte segments.
1427        let p1 = read_pkt(&mut test_rx).await;
1428        let p2 = read_pkt(&mut test_rx).await;
1429        assert_eq!(p1.payload.len() + p2.payload.len(), 20);
1430
1431        // The next 30 bytes are blocked behind the window.
1432        let blocked = tokio::time::timeout(Duration::from_millis(10), read_pkt(&mut test_rx)).await;
1433        assert!(blocked.is_err(), "must not exceed send_window before ACK");
1434    }
1435
1436    /// Our outbound SYN advertises the Window Scale option (RFC 7323).
1437    #[tokio::test]
1438    async fn syn_advertises_window_scale() {
1439        tokio::time::pause();
1440
1441        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1442        let (mut test_rx, _test_tx) = tokio::io::split(test_end);
1443        let mut adapter = Adapter::new(
1444            Box::new(TestTransport(adapter_end)),
1445            IpAddr::V6(HOST_IP),
1446            IpAddr::V6(PEER_IP),
1447        );
1448
1449        // Drive connect() far enough that the SYN hits the wire.
1450        let connect_fut = adapter.connect(PEER_PORT);
1451        let (_unused, syn) = tokio::join!(connect_fut, async { read_pkt(&mut test_rx).await });
1452
1453        assert!(syn.flags.syn, "first packet must be SYN");
1454        let shift =
1455            crate::tcp::packets::parse_window_scale(&syn.options).expect("SYN should include WSopt");
1456        assert_eq!(shift, OUR_WSCALE, "advertised wscale should match constant");
1457    }
1458
1459    /// When the peer's SYN-ACK includes a window scale, subsequent window
1460    /// advertisements are interpreted as `field << peer_wscale`.
1461    #[tokio::test]
1462    async fn peer_window_scale_is_honored() {
1463        tokio::time::pause();
1464
1465        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1466        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1467        let mut adapter = Adapter::new(
1468            Box::new(TestTransport(adapter_end)),
1469            IpAddr::V6(HOST_IP),
1470            IpAddr::V6(PEER_IP),
1471        );
1472        adapter.set_mss(10);
1473        adapter.set_send_window(1 << 20); // effectively unlimited from our side.
1474
1475        let peer_wscale = 4u8;
1476        let peer_raw_window = 2u16; // scaled: 2 << 4 = 32 bytes.
1477
1478        // Drive connect() while we manually act as the peer.
1479        let (hp_result, peer_hp) = tokio::join!(adapter.connect(PEER_PORT), async {
1480            let syn = read_pkt(&mut test_rx).await;
1481            assert!(syn.flags.syn);
1482            let hp = syn.source_port;
1483
1484            // SYN-ACK with WSopt and a tiny scaled window.
1485            let wscale_opt = crate::tcp::packets::window_scale_option(peer_wscale);
1486            let tcp = TcpPacket::create(
1487                IpAddr::V6(PEER_IP),
1488                IpAddr::V6(HOST_IP),
1489                PEER_PORT,
1490                hp,
1491                PEER_ISN,
1492                syn.sequence_number.wrapping_add(1),
1493                TcpFlags {
1494                    syn: true,
1495                    ack: true,
1496                    ..Default::default()
1497                },
1498                peer_raw_window,
1499                &wscale_opt,
1500                &[],
1501            );
1502            let ip = crate::tcp::packets::Ipv6Packet::create(
1503                PEER_IP,
1504                HOST_IP,
1505                ProtocolNumber::Tcp,
1506                255,
1507                &tcp,
1508            );
1509            test_tx.write_all(&ip).await.unwrap();
1510
1511            // Consume the adapter's ACK.
1512            let _ack = read_pkt(&mut test_rx).await;
1513            hp
1514        });
1515        let hp = hp_result.expect("connect failed");
1516        assert_eq!(hp, peer_hp);
1517
1518        // Try to send 100 bytes. With peer_window=32 and mss=10, we expect
1519        // 3 full segments (30 bytes) plus one short segment (2 bytes) = 32
1520        // bytes on the wire; the remaining 68 bytes must be held.
1521        adapter.queue_send(&[b'a'; 100], hp).unwrap();
1522        adapter.write_buffer_flush().await.unwrap();
1523
1524        let mut sent = 0;
1525        while sent < 32 {
1526            let p = read_pkt(&mut test_rx).await;
1527            sent += p.payload.len();
1528            assert!(sent <= 32, "must never exceed peer_window={}", 32);
1529        }
1530        assert_eq!(sent, 32);
1531
1532        let blocked = tokio::time::timeout(Duration::from_millis(10), read_pkt(&mut test_rx)).await;
1533        assert!(blocked.is_err(), "must not exceed peer's scaled window");
1534    }
1535
1536    #[tokio::test]
1537    async fn out_of_order_packet_dropped() {
1538        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1539        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1540        let mut adapter = Adapter::new(
1541            Box::new(TestTransport(adapter_end)),
1542            IpAddr::V6(HOST_IP),
1543            IpAddr::V6(PEER_IP),
1544        );
1545
1546        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1547
1548        // After handshake the adapter expects seq = PEER_ISN + 1 from the peer.
1549        // Send seq = PEER_ISN + 100
1550        adapter
1551            .process_tcp_packet_from_payload(&tcp_seg(
1552                hp,
1553                PEER_ISN + 100,
1554                0,
1555                TcpFlags {
1556                    psh: true,
1557                    ack: true,
1558                    ..Default::default()
1559                },
1560                b"ooo",
1561            ))
1562            .await
1563            .unwrap();
1564
1565        // No data should have been buffered.
1566        let buffered = adapter.uncache_all(hp).unwrap();
1567        assert!(
1568            buffered.is_empty(),
1569            "out-of-order data must not be buffered"
1570        );
1571
1572        // A duplicate ACK for what we do have tells the peer about the gap.
1573        let dup_ack = read_pkt(&mut test_rx).await;
1574        assert!(dup_ack.flags.ack);
1575        assert_eq!(
1576            dup_ack.acknowledgment_number,
1577            PEER_ISN + 1,
1578            "out-of-order packet must be answered with a duplicate ACK"
1579        );
1580    }
1581
1582    /// A duplicate segment (seq < expected, already received) is re-ACKed but
1583    /// its payload is not buffered a second time.
1584    #[tokio::test]
1585    async fn duplicate_packet_reacked_not_buffered() {
1586        let (adapter_end, test_end) = tokio::io::duplex(1 << 16);
1587        let (mut test_rx, mut test_tx) = tokio::io::split(test_end);
1588        let mut adapter = Adapter::new(
1589            Box::new(TestTransport(adapter_end)),
1590            IpAddr::V6(HOST_IP),
1591            IpAddr::V6(PEER_IP),
1592        );
1593
1594        let hp = handshake(&mut adapter, &mut test_rx, &mut test_tx).await;
1595
1596        // Inject the expected data packet.
1597        let data_seq = PEER_ISN + 1;
1598        adapter
1599            .process_tcp_packet_from_payload(&tcp_seg(
1600                hp,
1601                data_seq,
1602                0,
1603                TcpFlags {
1604                    psh: true,
1605                    ack: true,
1606                    ..Default::default()
1607                },
1608                b"hi",
1609            ))
1610            .await
1611            .unwrap();
1612
1613        // Consume the ACK for the first delivery.
1614        let ack1 = read_pkt(&mut test_rx).await;
1615        assert!(ack1.flags.ack);
1616        assert_eq!(
1617            ack1.acknowledgment_number,
1618            data_seq.wrapping_add(2),
1619            "ACK should cover both bytes"
1620        );
1621
1622        // Drain the read buffer so we can check it is not doubled.
1623        let first_read = adapter.uncache_all(hp).unwrap();
1624        assert_eq!(first_read, b"hi");
1625
1626        // Re-inject the same segment (peer retransmit / duplicate).
1627        adapter
1628            .process_tcp_packet_from_payload(&tcp_seg(
1629                hp,
1630                data_seq,
1631                0,
1632                TcpFlags {
1633                    psh: true,
1634                    ack: true,
1635                    ..Default::default()
1636                },
1637                b"hi",
1638            ))
1639            .await
1640            .unwrap();
1641
1642        // Should get a re-ACK with the same acknowledgment number.
1643        let ack2 = read_pkt(&mut test_rx).await;
1644        assert!(ack2.flags.ack);
1645        assert_eq!(
1646            ack2.acknowledgment_number, ack1.acknowledgment_number,
1647            "re-ACK must use same ack number as original"
1648        );
1649
1650        // Data must NOT have been buffered again.
1651        let second_read = adapter.uncache_all(hp).unwrap();
1652        assert!(
1653            second_read.is_empty(),
1654            "duplicate payload must not be re-buffered"
1655        );
1656    }
1657}