nexora 0.0.1

Single-threaded io_uring HTTP/1.1, HTTP/2, WebSocket, and TLS proactor client and server SDK
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
//! Session completion handlers and socket plumbing for the WebSocket client.

use nexora_proto::Http1KeepAlive;

use super::*;

impl WebSocketClient {
    /// Routes one completion to the resolver or to its session state machine.
    pub(super) fn on_completion(
        &mut self,
        completion: Completion,
        submissions: &mut SubmissionContext<'_>,
    ) -> Result<()> {
        let subsystem = completion.operation_id.subsystem_id();
        if subsystem == self.config.dns_subsystem {
            return self
                .dns
                .dispatch(completion, submissions)
                .map_err(Error::from);
        }
        if subsystem != self.config.net_subsystem {
            return Ok(());
        }
        submissions.claim_completion(completion)?;
        let (tag, raw) = decode_op(completion.operation_id.payload());
        if !(TAG_CONNECT..=TAG_PING_TIMEOUT).contains(&tag) {
            return Ok(());
        }
        let id = WebSocketSessionId::new(raw);
        let Some(mut owned) = self.sessions.remove(&id) else {
            return Ok(());
        };
        owned.inflight = owned.inflight.saturating_sub(1);
        match tag {
            TAG_CONNECT => self.on_connect(&mut owned, completion.result),
            TAG_RECV => self.on_recv(&mut owned, completion.result),
            TAG_SEND => self.on_send(&mut owned, completion.result),
            TAG_CLOSE => {
                owned.close_op = None;
                owned.fd = -1;
                owned.state = State::Closed;
            }
            TAG_CONNECT_TIMEOUT | TAG_PING_TIMEOUT => {
                self.on_timer(&mut owned, tag, completion.result)
            }
            _ => {}
        }
        if owned.state != State::Closed || owned.inflight != 0 {
            self.sessions.insert(id, owned);
        } else {
            self.handlers.remove(&id);
        }
        Ok(())
    }

    /// Handles the connect completion and starts TLS or the upgrade.
    fn on_connect(&mut self, session: &mut Session, result: i32) {
        session.connect_op = None;
        session.peer = None;
        // The deadline is disarmed whenever the attempt ends; its operand stays
        // owned until the timeout's own CQE.
        if let Some(op) = session.timer_op.take() {
            session.cancels.push(op);
        }
        if session.teardown {
            return;
        }
        if result < 0 {
            return self.fail(session, Error::Io(io_error(result)));
        }
        if !session.uri.scheme().is_tls() {
            return self.start_upgrade(session);
        }
        let Some(config) = self.config.tls_config.clone() else {
            return self.fail(
                session,
                invalid("a wss URL requires a TLS client configuration"),
            );
        };
        let host = session.uri.host().to_owned();
        match TlsSession::new_client(&config, &host) {
            Ok(tls) => {
                session.tls = Some(tls);
                session.state = State::Handshaking;
                self.drain_tls(session);
            }
            Err(error) => self.fail(session, Error::Proto(error)),
        }
    }

    /// Handles a receive completion and advances the protocol machines.
    fn on_recv(&mut self, session: &mut Session, result: i32) {
        session.recv_op = None;
        if session.teardown {
            return;
        }
        if result <= 0 {
            let error = if result == 0 {
                Error::ConnectionClosed
            } else {
                Error::Io(io_error(result))
            };
            return self.fail(session, error);
        }
        let read = result as usize;
        if read > session.rx.len() {
            return self.fail(session, invalid("receive completion exceeds the buffer"));
        }
        let mut failure = None;
        if let Some(tls) = session.tls.as_mut() {
            if let Err(error) = tls.feed_ciphertext(&session.rx[..read]) {
                failure = Some(error);
            } else {
                loop {
                    match tls.read_plaintext(&mut session.plain) {
                        Ok(0) => break,
                        Ok(_) => {}
                        Err(error) => {
                            failure = Some(error);
                            break;
                        }
                    }
                }
            }
        } else {
            session.plain.extend_from_slice(&session.rx[..read]);
        }
        if let Some(error) = failure {
            return self.fail(session, Error::Proto(error));
        }
        self.drain_tls(session);
        if session.state == State::Handshaking
            && session
                .tls
                .as_ref()
                .is_some_and(|tls| !tls.is_handshaking())
        {
            self.finish_handshake(session);
            if session.teardown {
                return;
            }
        }
        self.advance(session);
    }

    /// Handles a send completion and releases the transmitted prefix.
    fn on_send(&mut self, session: &mut Session, result: i32) {
        session.send_op = None;
        if result < 0 {
            if !session.teardown {
                self.fail(session, Error::Io(io_error(result)));
            }
            return;
        }
        if result == 0 {
            // A staged send carries at least one byte, so no progress means the
            // transport cannot deliver the queued data.
            return self.fail(session, Error::ConnectionClosed);
        }
        let sent = (result as usize).min(session.wire.len());
        session.wire.advance(sent);
        if session.wire.is_empty() && !session.deferred.is_empty() {
            let deferred = std::mem::take(&mut session.deferred);
            session.wire.extend_from_slice(&deferred);
        }
    }

    /// Handles a connect or keepalive deadline.
    fn on_timer(&mut self, session: &mut Session, tag: u64, result: i32) {
        drop(session.timer.take());
        session.timer_op = None;
        if session.teardown || result != -libc::ETIME {
            return;
        }
        match tag {
            TAG_CONNECT_TIMEOUT if session.state == State::Connecting => {
                if let Some(op) = session.connect_op.take() {
                    session.cancels.push(op);
                }
                self.fail(session, Error::Timeout);
            }
            TAG_PING_TIMEOUT if session.state == State::Open && !session.closing => {
                if let Err(error) = session.queue_message(WebSocketMessage::Ping(Bytes::new())) {
                    self.fail(session, Error::Proto(error));
                }
            }
            _ => {}
        }
    }

    /// Validates ALPN and starts the HTTP upgrade after the TLS handshake.
    fn finish_handshake(&mut self, session: &mut Session) {
        let alpn = session.tls.as_ref().and_then(|tls| tls.alpn_protocol());
        if alpn == Some(AlpnProtocol::Http2) {
            return self.fail(
                session,
                Error::Proto(ProtoError::Tls(String::from(
                    "the peer selected ALPN h2; a WebSocket upgrade requires http/1.1",
                ))),
            );
        }
        self.start_upgrade(session);
    }

    /// Queues the HTTP upgrade request.
    fn start_upgrade(&mut self, session: &mut Session) {
        let request =
            WebSocketHandshake::build_upgrade_request(&session.uri, &session.key, &session.extra);
        let mut bytes = BytesMut::with_capacity(256);
        let mut http = Http1ClientCodec::default();
        let encoded = http
            .encode_request(&request, &mut bytes)
            .and_then(|()| session.queue_app(&bytes));
        if let Err(error) = encoded {
            return self.fail(session, Error::Proto(error));
        }
        session.state = State::Upgrading;
    }

    /// Advances the HTTP upgrade or WebSocket message state machine.
    fn advance(&mut self, session: &mut Session) {
        if session.state == State::Upgrading {
            let mut http = Http1ClientCodec::default();
            match http.decode_response(&mut session.plain, Method::Get) {
                Ok(Some((response, Http1KeepAlive::Upgrade))) => {
                    if let Err(error) =
                        WebSocketHandshake::validate_upgrade_response(&response, &session.key)
                    {
                        return self.fail(session, Error::Proto(error));
                    }
                    session.state = State::Open;
                    if let Some(callback) = session.on_open.take() {
                        let mut ctx = client_context(&self.inbox);
                        callback(&mut ctx, session.id, Ok(()));
                    }
                }
                Ok(Some(_)) => {
                    return self.fail(
                        session,
                        Error::Proto(ProtoError::WebSocketProtocol(String::from(
                            "the server did not switch protocols",
                        ))),
                    );
                }
                Ok(None) => {}
                Err(error) => return self.fail(session, Error::Proto(error)),
            }
        }
        // Once either side has started the close handshake, further bytes are
        // not application data; decoding them would turn a peer's trailing junk
        // into a protocol failure that aborts the close reply still to be sent.
        if session.state != State::Open || session.closing {
            return;
        }
        loop {
            match session.websocket.decode_message(&mut session.plain) {
                Ok(None) => return,
                Ok(Some(message)) => {
                    if !self.dispatch(session, message) {
                        return;
                    }
                }
                Err(error) => return self.fail(session, Error::Proto(error)),
            }
            if session.state != State::Open || session.teardown {
                return;
            }
        }
    }

    /// Handles one decoded message; returns whether decoding may continue.
    fn dispatch(&mut self, session: &mut Session, message: WebSocketMessage) -> bool {
        match message {
            WebSocketMessage::Text(text) => self.emit(session, WebSocketEvent::Text(text)),
            WebSocketMessage::Binary(data) => self.emit(session, WebSocketEvent::Binary(data)),
            WebSocketMessage::Ping(data) => {
                if let Err(error) = session.queue_message(WebSocketMessage::Pong(data.clone())) {
                    self.fail(session, Error::Proto(error));
                    return false;
                }
                self.emit(session, WebSocketEvent::Ping(data));
            }
            WebSocketMessage::Pong(data) => self.emit(session, WebSocketEvent::Pong(data)),
            WebSocketMessage::Close(close) => {
                // The codec rejects a second close frame, so a session that
                // already started the handshake only records the peer's reply.
                // RFC 6455 recommends mirroring the received status code.
                let echo = WebSocketCloseFrame {
                    code: close
                        .as_ref()
                        .map_or(WebSocketCloseCode::Normal, |frame| frame.code),
                    reason: String::new(),
                };
                let reply = !session.closing;
                session.closing = true;
                self.emit(session, WebSocketEvent::Close(close));
                if reply
                    && let Err(error) = session.queue_message(WebSocketMessage::Close(Some(echo)))
                {
                    self.fail(session, Error::Proto(error));
                }
                return false;
            }
        }
        true
    }

    /// Delivers one event to the registered sink.
    fn emit(&mut self, session: &mut Session, event: WebSocketEvent) {
        if matches!(event, WebSocketEvent::Close(_)) {
            if session.close_event_sent {
                return;
            }
            session.close_event_sent = true;
        }
        let id = session.id;
        let Some(handler) = self.handlers.get_mut(&id) else {
            return;
        };
        let mut ctx = client_context(&self.inbox);
        let mut view = WebSocketSession { id };
        handler(&mut ctx, &mut view, event);
    }

    /// Reports a terminal error and starts teardown.
    pub(super) fn fail(&mut self, session: &mut Session, error: Error) {
        if session.teardown {
            return;
        }
        match session.on_open.take() {
            Some(callback) => {
                let mut ctx = client_context(&self.inbox);
                callback(&mut ctx, session.id, Err(error));
            }
            None => self.emit(session, WebSocketEvent::Close(None)),
        }
        // A lookup still in flight will never be applied to this session.
        if let Some(lookup) = session.lookup.take() {
            self.dns_waiters.remove(&lookup);
        }
        session.teardown = true;
        session.closing = true;
    }

    /// Stages pending cancels, sends, receives, timers, and teardown work.
    pub(super) fn pump(&mut self, proactor: &mut Proactor) -> Result<()> {
        let mut sessions = std::mem::take(&mut self.sessions);
        let mut failure: Option<Error> = None;
        for (id, mut session) in sessions.drain() {
            if let Err(error) = self.pump_session(&mut session, proactor)
                && failure.is_none()
            {
                failure = Some(error);
            }
            if session.state != State::Closed || session.inflight != 0 {
                self.sessions.insert(id, session);
            } else {
                self.handlers.remove(&id);
            }
        }
        match failure {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    /// Stages the next operation for one session.
    fn pump_session(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        if session.state == State::Closed {
            return Ok(());
        }
        self.stage_cancels(session, proactor)?;
        if session.teardown {
            return self.pump_close(session, proactor);
        }
        if session.send_op.is_none() && !session.wire.is_empty() {
            self.stage_send(session, proactor)?;
        }
        let framed = matches!(
            session.state,
            State::Handshaking | State::Upgrading | State::Open
        );
        // A closing session needs no more peer bytes: the remaining work is the
        // close reply already on the wire, and continuing to receive would grow
        // `plain` with data that the codec must reject.
        if framed && !session.closing && session.recv_op.is_none() {
            self.stage_recv(session, proactor)?;
        }
        let keepalive = self
            .config
            .ping_interval
            .is_some_and(|interval| !interval.is_zero());
        if session.state == State::Open
            && keepalive
            && !session.closing
            && session.timer.is_none()
            && session.timer_op.is_none()
        {
            self.stage_ping_timer(session, proactor)?;
        }
        if session.closing
            && session.send_op.is_none()
            && session.wire.is_empty()
            && session.deferred.is_empty()
        {
            session.teardown = true;
            self.pump_close(session, proactor)?;
        }
        Ok(())
    }

    /// Stages every queued cancellation and tracks the resulting completions.
    fn stage_cancels(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        let targets = std::mem::take(&mut session.cancels);
        for target in targets {
            match stage_cancel(proactor, self.config.net_subsystem, target) {
                Ok(()) => session.inflight += 1,
                Err(error) => {
                    session.cancels.push(target);
                    return Err(error);
                }
            }
        }
        Ok(())
    }

    /// Cancels outstanding work and closes the descriptor once it is idle.
    fn pump_close(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        let mut targets: Vec<OperationId> = [
            session.recv_op.take(),
            session.send_op.take(),
            session.connect_op.take(),
            session.timer_op.take(),
        ]
        .into_iter()
        .flatten()
        .collect();
        targets.append(&mut session.cancels);
        for target in targets {
            match stage_cancel(proactor, self.config.net_subsystem, target) {
                Ok(()) => session.inflight += 1,
                Err(error) => {
                    session.cancels.push(target);
                    return Err(error);
                }
            }
        }
        if session.inflight != 0 {
            return Ok(());
        }
        if session.fd < 0 || session.close_op.is_some() {
            if session.close_op.is_none() {
                session.state = State::Closed;
            }
            return Ok(());
        }
        let fd = session.fd;
        let op = operation_id(self.config.net_subsystem, TAG_CLOSE, session.id);
        proactor.prepare_sqe_with(op, |sqe| {
            // SAFETY: This is the only close submitted for `fd`: every earlier
            // operation on it delivered its final CQE because `inflight`
            // reached zero, so close ownership transfers exactly once.
            unsafe { net::prep_close(sqe, fd) }
        })?;
        session.close_op = Some(op);
        session.inflight += 1;
        session.fd = -1;
        Ok(())
    }

    /// Stages one receive into the session's fixed buffer.
    fn stage_recv(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        let fd = session.fd;
        if fd < 0 {
            return Ok(());
        }
        let op = operation_id(self.config.net_subsystem, TAG_RECV, session.id);
        let ptr = session.rx.as_mut_ptr();
        let len = session.rx.len();
        proactor.prepare_sqe_with(op, |sqe| {
            // SAFETY: `rx` is a fixed-address heap allocation owned by the
            // session and is never resized, reused, or freed until this
            // receive's final CQE has been observed.
            unsafe { net::prep_recv(sqe, fd, ptr, len, 0) }
        })?;
        session.recv_op = Some(op);
        session.inflight += 1;
        Ok(())
    }

    /// Stages one send of the queued wire prefix.
    fn stage_send(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        let fd = session.fd;
        if fd < 0 || session.wire.is_empty() {
            return Ok(());
        }
        let op = operation_id(self.config.net_subsystem, TAG_SEND, session.id);
        let ptr = session.wire.as_slice().as_ptr();
        let len = session.wire.len();
        proactor.prepare_sqe_with(op, |sqe| {
            // SAFETY: While `send_op` is set, `queue_app` appends new bytes to
            // `deferred`, so this buffer's pointer and length stay valid and
            // readable until the final send CQE. `MSG_NOSIGNAL` keeps a peer
            // reset from raising SIGPIPE.
            unsafe { net::prep_send(sqe, fd, ptr, len, libc::MSG_NOSIGNAL) }
        })?;
        session.send_op = Some(op);
        session.inflight += 1;
        Ok(())
    }

    /// Stages one keepalive deadline.
    fn stage_ping_timer(&mut self, session: &mut Session, proactor: &mut Proactor) -> Result<()> {
        let Some(interval) = self
            .config
            .ping_interval
            .filter(|interval| !interval.is_zero())
        else {
            return Ok(());
        };
        let timeout = Timeout::new(interval)?;
        let op = operation_id(self.config.net_subsystem, TAG_PING_TIMEOUT, session.id);
        proactor.prepare_sqe_with(op, |sqe| {
            // SAFETY: The `Timeout` owner stays in `session.timer` until its
            // own final CQE, including after cancellation.
            unsafe { timeout.prepare(sqe) }
        })?;
        session.timer = Some(timeout);
        session.timer_op = Some(op);
        session.inflight += 1;
        Ok(())
    }

    /// Moves queued TLS output into the wire buffers, failing the session on error.
    fn drain_tls(&mut self, session: &mut Session) {
        let outcome = {
            let Session {
                tls,
                wire,
                deferred,
                send_op,
                ..
            } = &mut *session;
            tls.as_mut()
                .map(|tls| drain_tls(tls, wire, deferred, send_op.is_some()))
        };
        if let Some(Err(error)) = outcome {
            self.fail(session, Error::Proto(error));
        }
    }

    /// Applies completed family lookups to their sessions.
    pub(super) fn apply_dns(&mut self, proactor: &mut Proactor) -> Result<usize> {
        let mut applied = 0;
        loop {
            let popped = self
                .dns_outcomes
                .try_borrow_mut()
                .ok()
                .and_then(|mut queue| queue.pop_front());
            let Some((lookup, addrs, error)) = popped else {
                break;
            };
            applied += 1;
            let Some(id) = self.dns_waiters.get(&lookup).copied() else {
                continue;
            };
            let Some(mut owned) = self.sessions.remove(&id) else {
                self.dns_waiters.remove(&lookup);
                continue;
            };
            if owned.lookup != Some(lookup) {
                self.dns_waiters.remove(&lookup);
                self.sessions.insert(id, owned);
                continue;
            }
            owned.dns_left = owned.dns_left.saturating_sub(1);
            if let Some(error) = error
                && owned.dns_reason.is_none()
            {
                owned.dns_reason = Some(error);
            }
            if let Some(addr) = addrs.first().copied() {
                owned.lookup = None;
                owned.state = State::Connecting;
                self.sessions.insert(id, owned);
                self.dns_waiters.remove(&lookup);
                // The drive loop has no caller to return this error to, so the
                // open callback is the only place the failure can surface.
                if let Err(error) = self.begin_connect(proactor, id, addr)
                    && let Some(mut failed) = self.sessions.remove(&id)
                {
                    self.fail(&mut failed, error);
                    self.sessions.insert(id, failed);
                }
            } else if owned.dns_left == 0 {
                owned.lookup = None;
                let host = owned.uri.host().to_owned();
                let reason = owned
                    .dns_reason
                    .take()
                    .unwrap_or_else(|| String::from("no records"));
                self.fail(&mut owned, Error::DnsResolutionFailed { host, reason });
                self.dns_waiters.remove(&lookup);
                self.sessions.insert(id, owned);
            } else {
                self.sessions.insert(id, owned);
            }
        }
        Ok(applied)
    }
}

/// A socket address laid out as the `IORING_OP_CONNECT` operand.
pub(super) enum PeerAddr {
    V4(libc::sockaddr_in),
    V6(libc::sockaddr_in6),
}

impl PeerAddr {
    pub(super) fn new(addr: SocketAddr) -> Self {
        match addr {
            SocketAddr::V4(v4) => Self::V4(libc::sockaddr_in {
                sin_family: libc::AF_INET as libc::sa_family_t,
                sin_port: v4.port().to_be(),
                sin_addr: libc::in_addr {
                    s_addr: u32::from_ne_bytes(v4.ip().octets()),
                },
                sin_zero: [0; 8],
            }),
            SocketAddr::V6(v6) => Self::V6(libc::sockaddr_in6 {
                sin6_family: libc::AF_INET6 as libc::sa_family_t,
                sin6_port: v6.port().to_be(),
                sin6_flowinfo: v6.flowinfo(),
                sin6_addr: libc::in6_addr {
                    s6_addr: v6.ip().octets(),
                },
                sin6_scope_id: v6.scope_id(),
            }),
        }
    }

    pub(super) fn raw(&self) -> (*const libc::sockaddr, libc::socklen_t) {
        match self {
            Self::V4(value) => (
                std::ptr::from_ref(value).cast(),
                std::mem::size_of::<libc::sockaddr_in>() as libc::socklen_t,
            ),
            Self::V6(value) => (
                std::ptr::from_ref(value).cast(),
                std::mem::size_of::<libc::sockaddr_in6>() as libc::socklen_t,
            ),
        }
    }
}

/// Builds the callback handle used to stage inbox commands.
pub(super) fn client_context(inbox: &SubsystemInbox) -> ClientContext {
    inbox.context()
}

/// Queues wire bytes without moving the buffer a send currently references.
pub(super) fn push_wire(
    wire: &mut BytesMut,
    deferred: &mut BytesMut,
    in_flight: bool,
    bytes: &[u8],
) {
    if in_flight {
        deferred.extend_from_slice(bytes);
    } else {
        wire.extend_from_slice(bytes);
    }
}

/// Appends all queued TLS ciphertext to the wire buffers.
pub(super) fn drain_tls(
    tls: &mut TlsSession,
    wire: &mut BytesMut,
    deferred: &mut BytesMut,
    in_flight: bool,
) -> std::result::Result<(), ProtoError> {
    let mut chunk = BytesMut::with_capacity(TLS_CHUNK_SIZE);
    tls.take_ciphertext(&mut chunk)?;
    if !chunk.is_empty() {
        push_wire(wire, deferred, in_flight, &chunk);
    }
    Ok(())
}

/// Splits a completion payload into its tag and session identifier.
pub(super) fn decode_op(payload: OperationPayload) -> (u64, u64) {
    let raw = payload.raw();
    (raw >> TAG_SHIFT, raw & ID_MASK)
}

/// Packs one tagged session operation identifier.
pub(super) fn operation_id(
    subsystem: SubsystemId,
    tag: u64,
    id: WebSocketSessionId,
) -> OperationId {
    OperationId::pack(
        subsystem,
        OperationPayload::new_unchecked((tag << TAG_SHIFT) | (id.raw() & ID_MASK)),
    )
}

/// Stages cancellation of one target operation.
pub(super) fn stage_cancel(
    proactor: &mut Proactor,
    subsystem: SubsystemId,
    target: OperationId,
) -> Result<()> {
    // A cancel completion still routes to the session that owned the target,
    // so its payload carries the session id rather than the target's tag.
    let session = WebSocketSessionId::new(target.payload().raw() & ID_MASK);
    let op = operation_id(subsystem, TAG_CANCEL, session);
    proactor.prepare_sqe_with(op, |sqe| {
        // SAFETY: Cancellation matches the target's exact user-data key and
        // references no buffer. The target's storage stays alive in the session
        // until its final CQE, which is counted in `inflight`.
        unsafe { net::prep_cancel(sqe, target, 0) }
    })?;
    Ok(())
}

/// Creates a non-blocking TCP descriptor for one address family.
pub(super) fn open_socket(addr: &SocketAddr) -> Result<RawFd> {
    let family = match addr {
        SocketAddr::V4(_) => libc::AF_INET,
        SocketAddr::V6(_) => libc::AF_INET6,
    };
    // SAFETY: `socket` takes only scalar arguments and returns a descriptor
    // owned by the caller. It is closed exactly once, either by `close_raw`
    // before any submission or by the io_uring close staged after all other
    // operations on it completed.
    let fd = unsafe {
        libc::socket(
            family,
            libc::SOCK_STREAM | libc::SOCK_NONBLOCK | libc::SOCK_CLOEXEC,
            0,
        )
    };
    if fd < 0 {
        return Err(Error::Io(std::io::Error::last_os_error()));
    }
    Ok(fd)
}

/// Closes a descriptor that has no submitted io_uring operation.
pub(super) fn close_raw(fd: RawFd) {
    if fd < 0 {
        return;
    }
    // SAFETY: The descriptor was created by `open_socket` and either never
    // submitted or already completed its connect, so this is its only close.
    unsafe { libc::close(fd) };
}

/// Rebuilds `errno` from a negative completion result.
pub(super) fn io_error(result: i32) -> std::io::Error {
    std::io::Error::from_raw_os_error(result.saturating_neg())
}

/// Builds an invalid-state error.
pub(super) fn invalid(reason: &str) -> Error {
    Error::InvalidState(reason.to_owned())
}

/// Produces a handshake nonce seed from the standard library's OS-seeded hasher.
pub(super) fn handshake_seed(id: WebSocketSessionId) -> u64 {
    let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
    hasher.write_u64(id.raw());
    hasher.finish()
}

/// Reads nameservers from the system resolver configuration.
pub(super) fn system_servers_csv() -> String {
    const FALLBACK: &str = "127.0.0.1";
    let Ok(contents) = std::fs::read_to_string("/etc/resolv.conf") else {
        return String::from(FALLBACK);
    };
    let mut servers: Vec<String> = Vec::new();
    for line in contents.lines() {
        let mut fields = line.split_whitespace();
        if fields.next() != Some("nameserver") {
            continue;
        }
        if let Some(server) = fields.next() {
            servers.push(server.to_owned());
        }
    }
    if servers.is_empty() {
        String::from(FALLBACK)
    } else {
        servers.join(",")
    }
}