sapflow-srt 0.1.0

SRT (Secure Reliable Transport) for live streams in pure Rust, sans-IO: listener/receiver and caller/sender, optional AES-CTR encryption, no libsrt.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! Live caller, written from draft-sharabayko-srt-01 sections
//! 3.2.1 and 4.3.1 and our own packet/listener modules.
//!
//! The adapter owns its UDP socket and supplies monotonic time. Drain
//! `poll_transmit(now)` immediately to that socket, then wait for input or
//! `poll_timeout()`. A returned datagram counts as sent: the adapter must
//! stop the session if it cannot submit it. Do not buffer paced datagrams
//! in another unbounded queue. Qualification is still pending for
//! seamless reconnection, late-drop negotiation or adaptive bitrate.

use std::collections::{VecDeque, hash_map::RandomState};
use std::hash::BuildHasher;
use std::net::SocketAddr;
use std::time::{Duration, Instant};

use crate::crypto::{CryptoConfig, Encryptor};
use crate::listener::SRT_VERSION;
use crate::packet::{
    Control, ControlPacket, Extension, Handshake, HandshakeKind, MAX_STREAM_ID, Packet, SRT_MAGIC,
    SrtHandshake, ext_flags, srt_flags,
};
use crate::sender::{Failure, SendConfig, SendError, SendStats, Sender};
use crate::seq::Seq;

/// CRYPT is the specification's mandatory legacy KK-understanding flag,
/// NOT a claim of encryption. Configured sessions require verified KM.
/// We are send-only: do not advertise receiving or periodic NAK capability.
const FLAGS: u32 = srt_flags::TSBPDSND | srt_flags::CRYPT | srt_flags::REXMITFLG;

/// Settings for a [`Caller`]; [`Caller::new`] checks them and fails with a
/// [`ConfigError`] if any is invalid.
#[derive(Debug, Clone)]
pub struct CallerConfig {
    /// UTF-8, at most 512 bytes, no NUL. Sent in clear in this lab milestone.
    pub stream_id: String,
    /// Receiver latency (TSBPD delay) we ask the listener to use, rounded up
    /// to whole milliseconds; it may use more. 1 ms to 65535 ms; default 120
    /// ms.
    pub latency: Duration,
    /// How long to wait for the handshake to complete, counted from creation;
    /// at least `handshake_retry`, at most 60 s; default 5 s.
    pub connect_timeout: Duration,
    /// Interval between handshake packets while connecting; 10 ms to 5 s;
    /// default 250 ms.
    pub handshake_retry: Duration,
    /// Most handshake packets sent before giving up with
    /// [`CallerCloseReason::HandshakeLimit`]; 1 to 1024; default 32.
    pub max_handshake_packets: u32,
    /// A connected session ends after this long without a valid packet from
    /// the listener; 10 ms to 60 s; default 5 s.
    pub peer_idle: Duration,
    /// Limits and pacing for the sending window.
    pub send: SendConfig,
    /// None explicitly selects plaintext; Some requires encryption.
    pub crypto: Option<CryptoConfig>,
}

impl Default for CallerConfig {
    fn default() -> Self {
        Self {
            stream_id: String::new(),
            latency: Duration::from_millis(120),
            connect_timeout: Duration::from_secs(5),
            handshake_retry: Duration::from_millis(250),
            max_handshake_packets: 32,
            peer_idle: Duration::from_secs(5),
            send: SendConfig::default(),
            crypto: None,
        }
    }
}

/// Why a [`Caller`] could not be created.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigError {
    /// The stream ID is longer than 512 bytes or contains a NUL.
    StreamId,
    /// A time, packet, payload, bandwidth or byte limit is out of range.
    Limits,
    /// The caller socket ID is zero.
    SocketId,
    /// The peer address has port 0, or is unspecified or multicast.
    Peer,
    /// The crypto configuration is invalid, or secure randomness was
    /// unavailable.
    Crypto,
}
impl std::fmt::Display for ConfigError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::StreamId => "stream ID must contain at most 512 UTF-8 bytes and no NUL",
            Self::Limits => "invalid caller time, packet, payload, bandwidth or byte limit",
            Self::SocketId => "caller socket ID must be nonzero",
            Self::Crypto => "invalid crypto configuration or unavailable secure randomness",
            Self::Peer => "peer must have a nonzero port and a specified unicast address",
        })
    }
}
impl std::error::Error for ConfigError {}

/// Why a [`Caller`] session ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallerCloseReason {
    /// [`Caller::stop`] was called.
    Cancelled,
    /// The handshake did not complete within `connect_timeout`.
    ConnectTimeout,
    /// `max_handshake_packets` were sent without completing the handshake.
    HandshakeLimit,
    /// No valid packet from the listener for `peer_idle`.
    PeerIdle,
    /// The listener refused the handshake; the code is its rejection reason
    /// (see [`RejectCode`](crate::listener::RejectCode)).
    Rejected(u32),
    /// The listener's handshake is not one this live caller can use: wrong
    /// version, missing or unsupported extensions, or incompatible flags or
    /// sizes.
    UnsupportedPeer,
    /// Encryption failed: the listener's key material response did not match,
    /// or a key could not be used or rotated safely.
    Crypto,
    /// Queued media reached `send.max_age_us` before it was acknowledged.
    MediaExpired,
    /// A packet was retransmitted `send.max_retransmits` times.
    RetryLimit,
    /// The listener sent SHUTDOWN.
    PeerClosed,
    /// The listener sent a peer error; the code is the one it reported.
    PeerError(u32),
}

/// What happened to a [`Caller`] session; see [`Caller::poll_event`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CallerEvent {
    /// Handshake only; does not prove application acceptance or playback.
    Connected {
        /// The socket ID the listener gave the connection.
        peer_socket: u32,
        /// The negotiated TSBPD latency: the larger of ours and the
        /// listener's receiver delay, in whole milliseconds.
        latency: Duration,
        /// Largest message [`Caller::enqueue`] accepts, in bytes
        /// (`send.max_payload`).
        max_payload: usize,
    },
    /// The session ended.
    Closed {
        /// Why it ended.
        reason: CallerCloseReason,
    },
}

/// Counters for a [`Caller`].
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CallerStats {
    /// Handshake packets sent, repeats included.
    pub handshake_packets: u64,
    /// Datagrams dropped for being over 1500 bytes or not a well-formed
    /// control packet.
    pub malformed: u64,
    /// Datagrams ignored as not meant for this session: wrong source address,
    /// wrong destination socket, or unexpected in the current state.
    pub stray: u64,
    /// The sender's counters; all zero until connected, and kept after
    /// closing.
    pub send: SendStats,
}

enum State {
    Induction,
    Conclusion { cookie: u32, socket_id: u32 },
    Open(Box<Sender>),
    Closed,
}

/// A live SRT caller: connects to a listener, then sends queued messages
/// paced and bounded. Sans-IO; see the module docs.
pub struct Caller {
    config: CallerConfig,
    peer: SocketAddr,
    socket_id: u32,
    initial: Seq,
    epoch: Instant,
    latest: i64,
    state: State,
    next_handshake: i64,
    events: VecDeque<CallerEvent>,
    stats: CallerStats,
    shutdown: Option<u32>,
    crypto: Option<Encryptor>,
}

impl Caller {
    /// A caller for `peer` with a random socket ID and initial sequence
    /// number; `now` starts its clock. Fails if `config` or `peer` is not
    /// valid.
    pub fn new(config: CallerConfig, peer: SocketAddr, now: Instant) -> Result<Self, ConfigError> {
        let random = RandomState::new();
        let socket_id = (random.hash_one(("sapflow caller", peer)) as u32 & 0x3fff_ffff).max(1);
        let initial = Seq::new(random.hash_one(("sapflow sequence", peer)) as u32);
        Self::with_ids(config, peer, socket_id, initial, now)
    }

    /// Deterministic construction for tests. Production adapters should use
    /// `new`, or provide fresh independently random socket/sequence IDs.
    pub fn with_ids(
        config: CallerConfig,
        peer: SocketAddr,
        socket_id: u32,
        initial: Seq,
        now: Instant,
    ) -> Result<Self, ConfigError> {
        if config.stream_id.len() > MAX_STREAM_ID || config.stream_id.contains('\0') {
            return Err(ConfigError::StreamId);
        }
        if socket_id == 0 {
            return Err(ConfigError::SocketId);
        }
        if peer.port() == 0 || peer.ip().is_unspecified() || peer.ip().is_multicast() {
            return Err(ConfigError::Peer);
        }
        if !config.send.valid()
            || config.latency > Duration::from_millis(u64::from(u16::MAX))
            || config.latency < Duration::from_millis(1)
            || !(Duration::from_millis(10)..=Duration::from_secs(5))
                .contains(&config.handshake_retry)
            || config.connect_timeout < config.handshake_retry
            || config.connect_timeout > Duration::from_secs(60)
            || !(1..=1024).contains(&config.max_handshake_packets)
            || !(Duration::from_millis(10)..=Duration::from_secs(60)).contains(&config.peer_idle)
        {
            return Err(ConfigError::Limits);
        }
        let crypto = config
            .crypto
            .as_ref()
            .map(Encryptor::new)
            .transpose()
            .map_err(|_| ConfigError::Crypto)?;
        Ok(Self {
            crypto,
            config,
            peer,
            socket_id,
            initial,
            epoch: now,
            latest: 0,
            state: State::Induction,
            next_handshake: 0,
            events: VecDeque::new(),
            stats: CallerStats::default(),
            shutdown: None,
        })
    }

    fn micros(&mut self, now: Instant) -> i64 {
        self.latest = self.latest.max(
            i64::try_from(now.saturating_duration_since(self.epoch).as_micros())
                .unwrap_or(i64::MAX),
        );
        self.latest
    }

    /// Whether the handshake has completed and the session is still open.
    pub fn is_connected(&self) -> bool {
        matches!(self.state, State::Open(_))
    }
    /// Whether the session has ended, for any reason.
    pub fn is_closed(&self) -> bool {
        matches!(self.state, State::Closed)
    }
    /// The counters so far, with the sender's once connected.
    pub fn stats(&self) -> CallerStats {
        let mut stats = self.stats;
        if let State::Open(sender) = &self.state {
            stats.send = sender.stats();
        }
        stats
    }
    /// The next [`CallerEvent`], if any.
    pub fn poll_event(&mut self) -> Option<CallerEvent> {
        self.events.pop_front()
    }

    /// Transfers the payload's allocation into the bounded send window.
    /// QueueFull is backpressure; the rejected payload is discarded.
    pub fn enqueue(&mut self, now: Instant, payload: Vec<u8>) -> Result<(), SendError> {
        self.try_enqueue(now, payload).map_err(|(error, _)| error)
    }

    /// Transfers ownership on success; returns the identical Vec allocation
    /// on every rejection, including QueueFull. No payload clone or copy.
    pub fn try_enqueue(
        &mut self,
        now: Instant,
        payload: Vec<u8>,
    ) -> Result<(), (SendError, Vec<u8>)> {
        let t = self.micros(now);
        self.tick(t);
        if let State::Open(sender) = &mut self.state {
            sender.try_enqueue(t, payload)
        } else {
            Err((SendError::NotConnected, payload))
        }
    }

    /// Takes a datagram that arrived from `peer` at `now`. One that is not a
    /// well-formed control packet for this session from the listener is
    /// counted in [`CallerStats`] and ignored.
    pub fn handle_datagram(&mut self, now: Instant, peer: SocketAddr, bytes: &[u8]) {
        let t = self.micros(now);
        self.tick(t);
        if self.is_closed() {
            return;
        }
        if peer != self.peer {
            self.stats.stray += 1;
            return;
        }
        if bytes.len() > 1500 || !control_shape(bytes) {
            self.stats.malformed += 1;
            return;
        }
        let Ok(Packet::Control(packet)) = Packet::parse(bytes) else {
            self.stats.malformed += 1;
            return;
        };
        if packet.dest != self.socket_id {
            self.stats.stray += 1;
            return;
        }
        match packet.body {
            Control::Handshake(h) => self.handshake(t, h),
            control => {
                if let State::Open(sender) = &mut self.state {
                    sender.on_control(t, &control);
                } else {
                    self.stats.stray += 1;
                }
                self.tick(t);
            }
        }
    }

    /// Does what is due at `now`: connect and idle timeouts, the media age
    /// limit and retransmission timers.
    pub fn handle_timeout(&mut self, now: Instant) {
        let t = self.micros(now);
        self.tick(t);
    }

    /// One datagram at most. Repeated calls at the same time cannot burst
    /// data; control responses are coalesced to one ACKACK slot.
    pub fn poll_transmit(&mut self, now: Instant) -> Option<(SocketAddr, Vec<u8>)> {
        let t = self.micros(now);
        self.tick(t);
        if let Some(dest) = self.shutdown.take() {
            let mut bytes = Vec::with_capacity(16);
            ControlPacket::new(t as u32, dest, Control::Shutdown).encode(&mut bytes);
            return Some((self.peer, bytes));
        }
        let bytes = match &mut self.state {
            State::Open(sender) => sender.poll_transmit(t),
            State::Induction | State::Conclusion { .. } if t >= self.next_handshake => {
                if self.stats.handshake_packets >= u64::from(self.config.max_handshake_packets) {
                    self.close(CallerCloseReason::HandshakeLimit, false);
                    return None;
                }
                self.stats.handshake_packets += 1;
                self.next_handshake = t.saturating_add(duration_us(self.config.handshake_retry));
                Some(self.handshake_packet(t))
            }
            _ => None,
        };
        self.tick(t);
        bytes.map(|bytes| (self.peer, bytes))
    }

    /// When to call [`handle_timeout`](Self::handle_timeout) next; `None`
    /// once the session has closed.
    pub fn poll_timeout(&self) -> Option<Instant> {
        let t = match &self.state {
            State::Induction | State::Conclusion { .. } => self
                .next_handshake
                .min(duration_us(self.config.connect_timeout)),
            State::Open(sender) => sender.next_timer().min(
                sender
                    .last_receive()
                    .saturating_add(duration_us(self.config.peer_idle)),
            ),
            State::Closed => return None,
        };
        self.epoch
            .checked_add(Duration::from_micros(t.max(0) as u64))
    }

    /// Idempotent cancellation releases payloads/timers immediately. Poll
    /// once afterwards to send the optional best-effort shutdown datagram.
    pub fn stop(&mut self, now: Instant) {
        self.micros(now);
        self.close(CallerCloseReason::Cancelled, true);
    }

    fn tick(&mut self, t: i64) {
        let reason = match &mut self.state {
            State::Induction | State::Conclusion { .. }
                if t >= duration_us(self.config.connect_timeout) =>
            {
                Some(CallerCloseReason::ConnectTimeout)
            }
            State::Open(sender) => {
                sender.on_timer(t);
                sender
                    .failure()
                    .map(|failure| match failure {
                        Failure::Crypto => CallerCloseReason::Crypto,
                        Failure::MediaExpired => CallerCloseReason::MediaExpired,
                        Failure::RetryLimit => CallerCloseReason::RetryLimit,
                        Failure::PeerClosed => CallerCloseReason::PeerClosed,
                        Failure::PeerError(code) => CallerCloseReason::PeerError(code),
                    })
                    .or_else(|| {
                        (t.saturating_sub(sender.last_receive())
                            >= duration_us(self.config.peer_idle))
                        .then_some(CallerCloseReason::PeerIdle)
                    })
            }
            _ => None,
        };
        if let Some(reason) = reason {
            self.close(
                reason,
                !matches!(
                    reason,
                    CallerCloseReason::PeerClosed | CallerCloseReason::PeerError(_)
                ),
            );
        }
    }

    fn close(&mut self, reason: CallerCloseReason, notify: bool) {
        if self.is_closed() {
            return;
        }
        if let State::Open(sender) = &mut self.state {
            if notify {
                self.shutdown = Some(sender.peer_socket());
            }
            sender.clear();
            self.stats.send = sender.stats();
        }
        self.state = State::Closed;
        self.crypto = None;
        self.config.crypto = None;
        self.events.push_back(CallerEvent::Closed { reason });
    }

    fn handshake(&mut self, t: i64, h: Handshake) {
        if matches!(self.state, State::Open(_)) {
            self.stats.stray += 1;
            return;
        }
        if h.initial_seq != self.initial.get() {
            self.stats.stray += 1;
            return;
        }
        if let HandshakeKind::Rejection(code) = h.kind {
            self.close(CallerCloseReason::Rejected(code), false);
            return;
        }
        if h.version != 5 {
            self.close(CallerCloseReason::UnsupportedPeer, false);
            return;
        }
        match self.state {
            State::Induction if h.kind == HandshakeKind::Induction => {
                if h.extension != SRT_MAGIC
                    || (h.encryption != 0
                        && self
                            .config
                            .crypto
                            .as_ref()
                            .is_none_or(|c| c.key_size.field() != h.encryption))
                    || !h.extensions.is_empty()
                {
                    self.close(CallerCloseReason::UnsupportedPeer, false);
                    return;
                }
                self.state = State::Conclusion {
                    cookie: h.cookie,
                    // libsrt 1.5.4 echoes the caller ID in induction (observed
                    // on the wire), not an addressable listener connection.
                    // Its demultiplexer accepts the conclusion at socket 0.
                    socket_id: if h.socket_id == self.socket_id {
                        0
                    } else {
                        h.socket_id
                    },
                };
                self.next_handshake = t;
            }
            State::Conclusion { .. } if h.kind == HandshakeKind::Conclusion => {
                let responses: Vec<_> = h
                    .extensions
                    .iter()
                    .filter_map(|e| match e {
                        Extension::HsRsp(s) => Some(*s),
                        _ => None,
                    })
                    .collect();
                let Some(response) = responses.first().copied() else {
                    self.close(CallerCloseReason::UnsupportedPeer, false);
                    return;
                };
                let unsupported = h.extensions.iter().any(|e| match e {
                    Extension::KmReq(_)
                    | Extension::Filter(_)
                    | Extension::Group(_)
                    | Extension::HsReq(_) => true,
                    Extension::Congestion(c) => c.as_slice() != b"live",
                    _ => false,
                });
                if responses.len() != 1
                    || unsupported
                    || h.extension & ext_flags::HSREQ == 0
                    || h.socket_id == 0
                    || h.mtu < 44 + self.config.send.max_payload as u32
                    || h.mtu > 1500
                    || h.flow_window == 0
                    || h.flow_window >= 0x4000_0000
                    || response.version < 0x01_03_00
                    || response.flags & srt_flags::STREAM != 0
                    || response.flags & srt_flags::TSBPDRCV == 0
                    || response.flags & srt_flags::REXMITFLG == 0
                {
                    self.close(CallerCloseReason::UnsupportedPeer, false);
                    return;
                }
                let kms: Vec<_> = h
                    .extensions
                    .iter()
                    .filter_map(|e| match e {
                        Extension::KmRsp(b) => Some(b.as_slice()),
                        _ => None,
                    })
                    .collect();
                let crypto_ok = match &self.crypto {
                    Some(crypto) => {
                        kms.len() == 1
                            && h.extension & ext_flags::KMREQ != 0
                            && self.config.crypto.as_ref().is_some_and(|c| {
                                h.encryption == 0 || c.key_size.field() == h.encryption
                            })
                            && crypto.confirm_initial(kms.first().copied().unwrap_or_default())
                    }
                    None => {
                        kms.is_empty() && h.encryption == 0 && h.extension & ext_flags::KMREQ == 0
                    }
                };
                if !crypto_ok {
                    self.close(
                        if self.crypto.is_some() {
                            CallerCloseReason::Crypto
                        } else {
                            CallerCloseReason::UnsupportedPeer
                        },
                        false,
                    );
                    return;
                }
                let requested_ms = self.config.latency.as_micros().div_ceil(1000) as u64;
                let latency =
                    Duration::from_millis(requested_ms.max(u64::from(response.receiver_delay)));
                let mut sender = Sender::new(
                    self.config.send.clone(),
                    t,
                    h.socket_id,
                    self.initial,
                    (h.flow_window as usize).min(self.config.send.max_packets),
                );
                sender.set_crypto(self.crypto.take());
                let max_payload = sender.max_payload();
                self.state = State::Open(Box::new(sender));
                self.events.push_back(CallerEvent::Connected {
                    peer_socket: h.socket_id,
                    latency,
                    max_payload,
                });
            }
            _ => self.stats.stray += 1,
        }
    }

    fn handshake_packet(&self, t: i64) -> Vec<u8> {
        let (cookie, dest, conclusion) = match self.state {
            State::Conclusion { cookie, socket_id } => (cookie, socket_id, true),
            _ => (0, 0, false),
        };
        let mut extensions = Vec::new();
        if conclusion {
            extensions.push(Extension::HsReq(SrtHandshake {
                version: SRT_VERSION,
                flags: FLAGS,
                receiver_delay: 0,
                sender_delay: self.config.latency.as_micros().div_ceil(1000) as u16,
            }));
            if let Some(crypto) = &self.crypto
                && let Ok(km) = crypto.initial()
            {
                extensions.push(Extension::KmReq(km));
            }
            if !self.config.stream_id.is_empty() {
                extensions.push(Extension::StreamId(
                    self.config.stream_id.as_bytes().to_vec(),
                ));
            }
        }
        let h = Handshake {
            version: if conclusion { 5 } else { 4 },
            encryption: if conclusion {
                self.config
                    .crypto
                    .as_ref()
                    .map_or(0, |c| c.key_size.field())
            } else {
                0
            },
            extension: if conclusion {
                ext_flags::HSREQ
                    | if self.crypto.is_some() {
                        ext_flags::KMREQ
                    } else {
                        0
                    }
                    | if self.config.stream_id.is_empty() {
                        0
                    } else {
                        ext_flags::CONFIG
                    }
            } else {
                2
            },
            initial_seq: self.initial.get(),
            mtu: 1500,
            flow_window: self.config.send.max_packets as u32,
            kind: if conclusion {
                HandshakeKind::Conclusion
            } else {
                HandshakeKind::Induction
            },
            socket_id: self.socket_id,
            cookie,
            peer_ip: encode_ip(self.peer),
            extensions,
        };
        let mut bytes = Vec::with_capacity(84 + self.config.stream_id.len());
        ControlPacket::new(t as u32, dest, Control::Handshake(h)).encode(&mut bytes);
        bytes
    }
}

fn duration_us(d: Duration) -> i64 {
    i64::try_from(d.as_micros()).unwrap_or(i64::MAX)
}

fn encode_ip(peer: SocketAddr) -> [u8; 16] {
    let mut bytes = [0u8; 16];
    match peer.ip() {
        std::net::IpAddr::V4(ip) => {
            for (dst, src) in bytes.iter_mut().zip(ip.octets().iter().rev()) {
                *dst = *src;
            }
        }
        std::net::IpAddr::V6(ip) => {
            for (dst, src) in bytes.chunks_mut(4).zip(ip.octets().chunks(4)) {
                for (d, s) in dst.iter_mut().zip(src.iter().rev()) {
                    *d = *s;
                }
            }
        }
    }
    bytes
}

/// Reject truncated control bodies before the shared permissive packet
/// parser. Unknown control types cannot allocate beyond the datagram cap.
fn control_shape(bytes: &[u8]) -> bool {
    let Some(header) = bytes.get(..16) else {
        return false;
    };
    let mut words = header.chunks_exact(4);
    let Some(first) = words.next() else {
        return false;
    };
    let Ok(first) = <[u8; 4]>::try_from(first) else {
        return false;
    };
    let first = u32::from_be_bytes(first);
    if first & 0x8000_0000 == 0 {
        return false;
    }
    let kind = (first >> 16) & 0x7fff;
    if kind <= 8 && first & 0xffff != 0 {
        return false;
    }
    let size = bytes.len() - 16;
    match (first >> 16) & 0x7fff {
        0 => size >= 48 && size.is_multiple_of(4),
        2 => matches!(size, 4 | 16 | 24 | 28) && bytes.get(16).is_some_and(|b| b & 0x80 == 0),
        3 => {
            size > 0
                && size.is_multiple_of(4)
                && valid_loss_words(bytes.get(16..).unwrap_or_default())
        }
        1 | 4 | 5 | 6 | 8 => matches!(size, 0 | 4),
        7 => size == 8,
        _ => true,
    }
}

// A range endpoint is an ordinary 31-bit sequence, never another range
// marker. Check before Packet::parse masks the sequence-number flag bit.
fn valid_loss_words(body: &[u8]) -> bool {
    let mut endpoint = false;
    for word in body.chunks_exact(4) {
        let Some(first) = word.first() else {
            return false;
        };
        let marker = first & 0x80 != 0;
        if endpoint && marker {
            return false;
        }
        endpoint = marker && !endpoint;
    }
    !endpoint
}