rings-node 1.0.0-pre.8

Rings is a browser-native, structured peer-to-peer network: browser tabs and native daemons find each other by DID and talk over WebRTC, routed by a Chord DHT.
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
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//! `udp`: an unconnected OS UDP datagram session with stateful peer-IP pinholes (#1292).
//!
//! Each `onion::udp` session binds a dedicated OS UDP socket at the exit, announces its bound
//! external endpoint in an initial [`OnionUdpServerFrame::Bound`] frame, transmits client
//! [`OnionUdpClientFrame::SendDatagram`] payloads from that socket, and forwards inbound UDP
//! datagrams only from peer IPs holding a live pinhole in [`OnionUdpPinholeTable`].
//!
//! Laws:
//! - **Bound identity**: the initial server frame is [`OnionUdpServerFrame::Bound`] carrying a
//!   non-unspecified IP and non-zero port; exits without an advertisable address fail closed at
//!   open.
//! - **Inbound pinhole gate**: inbound datagrams from peer IPs absent from [`OnionUdpPinholeTable`]
//!   (or disallowed by egress policy) are dropped at the OS socket before consuming return SURBs
//!   or exit byte budget.
//! - **Open quantum**: exit socket bind outcomes (success or failure) are released at multiples of
//!   250 ms, matching `onion::tcp`.
//! - **Lock-free close**: [`NativeOnionUdpSession::close`] signals closure without awaiting any
//!   reader or writer lock, waking any parked [`NativeOnionUdpSession::recv_from`] immediately and
//!   dropping the underlying session stream so `abort` leaves toward the exit.

use std::net::IpAddr;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;
use std::net::SocketAddr;
#[cfg(rings_native)]
use std::sync::atomic::AtomicBool;
#[cfg(rings_native)]
use std::sync::atomic::Ordering;
#[cfg(rings_native)]
use std::sync::Arc;
#[cfg(rings_native)]
use std::sync::Mutex as SyncMutex;
#[cfg(rings_native)]
use std::time::Duration;

#[cfg(rings_native)]
use bytes::Bytes;
#[cfg(rings_native)]
use bytes::BytesMut;
#[cfg(rings_native)]
use rings_core::utils::SharedClock;
#[cfg(rings_native)]
use tokio::net::UdpSocket;
#[cfg(rings_native)]
use tokio::sync::Mutex as AsyncMutex;
#[cfg(rings_native)]
use tokio::sync::Notify;
#[cfg(rings_native)]
use tokio::time::timeout;
#[cfg(rings_native)]
use tokio::time::Instant;
use url::Url;

use crate::error::Error;
use crate::error::Result;
#[cfg(rings_native)]
use crate::onion::exit_accounting::OnionExitAccounting;
#[cfg(rings_native)]
use crate::onion::session::dial::OnionClientStream;
#[cfg(rings_native)]
use crate::onion::session::dial::OnionStreamEvent;
#[cfg(rings_native)]
use crate::onion::session::dial::OnionStreamReceiver;
#[cfg(rings_native)]
use crate::onion::session::dial::OnionStreamSender;
#[cfg(rings_native)]
use crate::onion::session::dial::ONION_SESSION_OPEN_TIMEOUT;
#[cfg(rings_native)]
use crate::onion::session::serve::OnionWorld;
#[cfg(rings_native)]
use crate::onion::session::serve::OnionWorldReader;
#[cfg(rings_native)]
use crate::onion::session::serve::OnionWorldWriter;
#[cfg(rings_native)]
use crate::onion::OnionExitPolicy;
use crate::onion::OnionProxyTarget;
use crate::onion::OnionRouteError;
#[cfg(rings_native)]
use crate::sync_lock::lock;

pub mod codec;
pub mod pinhole;

pub use codec::decode_next_client_frame;
pub use codec::decode_next_server_frame;
pub use codec::encode_client_frame;
pub use codec::encode_server_frame;
pub use codec::OnionUdpClientFrame;
pub use codec::OnionUdpServerFrame;
pub use codec::MAX_ONION_UDP_FRAME_BYTES;
pub use codec::MAX_ONION_UDP_PAYLOAD_BYTES;
pub use codec::ONION_UDP_FRAME_LEN_BYTES;
pub use pinhole::OnionUdpPinholeTable;
pub use pinhole::MAX_PINHOLES_PER_SESSION;
pub use pinhole::MAX_PINHOLE_TTL_MS;

/// Socket-bind request mode encoded as the canonical `OnionProxyTarget` of an `onion::udp` open.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum OnionUdpBindMode {
    /// Bind an IPv4 UDP socket on any available port (`0.0.0.0:1`).
    #[default]
    Ipv4AnyPort,
    /// Bind an IPv4 UDP socket on an even port (`0.0.0.0:2`).
    Ipv4EvenPort,
    /// Bind an IPv6 UDP socket on any available port (`[::]:1`).
    Ipv6AnyPort,
    /// Bind an IPv6 UDP socket on an even port (`[::]:2`).
    Ipv6EvenPort,
}

impl OnionUdpBindMode {
    /// Encode this bind mode as a canonical [`OnionProxyTarget`] authority.
    pub fn to_target(self) -> OnionProxyTarget {
        let (host, port) = match self {
            Self::Ipv4AnyPort => ("0.0.0.0", 1),
            Self::Ipv4EvenPort => ("0.0.0.0", 2),
            Self::Ipv6AnyPort => ("::", 1),
            Self::Ipv6EvenPort => ("::", 2),
        };
        OnionProxyTarget::from_canonical_literal(host, port)
    }

    /// Parse an [`OnionProxyTarget`] authority into an [`OnionUdpBindMode`].
    pub fn from_target(target: &OnionProxyTarget) -> Result<Self> {
        match (target.host(), target.port()) {
            ("0.0.0.0", 1) => Ok(Self::Ipv4AnyPort),
            ("0.0.0.0", 2) => Ok(Self::Ipv4EvenPort),
            ("::", 1) => Ok(Self::Ipv6AnyPort),
            ("::", 2) => Ok(Self::Ipv6EvenPort),
            _ => Err(Error::OnionRouteError(OnionRouteError::UdpTarget(format!(
                "unsupported udp bind target {}",
                target.authority()
            )))),
        }
    }

    /// Return whether this bind mode requests an IPv6 socket.
    pub const fn is_ipv6(self) -> bool {
        matches!(self, Self::Ipv6AnyPort | Self::Ipv6EvenPort)
    }

    /// Return whether this bind mode requires an even UDP local port.
    pub const fn requires_even_port(self) -> bool {
        matches!(self, Self::Ipv4EvenPort | Self::Ipv6EvenPort)
    }
}

/// Socket binding and peer-IP admission settings for native `onion::udp` exits.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct OnionUdpBindConfig {
    /// Local IPv4 interface bound by IPv4 `onion::udp` sessions (`0.0.0.0` by default).
    pub bind_ipv4: Ipv4Addr,
    /// Local IPv6 interface bound by IPv6 `onion::udp` sessions (`::` by default).
    pub bind_ipv6: Ipv6Addr,
    /// Externally reachable IPv4 reported in [`OnionUdpServerFrame::Bound`] when `bind_ipv4` is
    /// unspecified (`0.0.0.0`).
    pub advertised_ipv4: Option<Ipv4Addr>,
    /// Externally reachable IPv6 reported in [`OnionUdpServerFrame::Bound`] when `bind_ipv6` is
    /// unspecified (`::`).
    pub advertised_ipv6: Option<Ipv6Addr>,
    /// Optional inclusive UDP port range `(min_port, max_port)` for bound exit sockets.
    pub port_range: Option<(u16, u16)>,
    /// Admit loopback/RFC1918 peer and advertised IPs for controlled local/integration tests.
    pub allow_private_peers: bool,
}

impl Default for OnionUdpBindConfig {
    fn default() -> Self {
        Self::new()
    }
}

impl OnionUdpBindConfig {
    /// Default production configuration: wildcard bind, public-only peer IPs, and advertised IP
    /// populated from the node's endpoint hint or explicit override.
    pub const fn new() -> Self {
        Self {
            bind_ipv4: Ipv4Addr::UNSPECIFIED,
            bind_ipv6: Ipv6Addr::UNSPECIFIED,
            advertised_ipv4: None,
            advertised_ipv6: None,
            port_range: None,
            allow_private_peers: false,
        }
    }

    /// Local test configuration binding and advertising loopback (`127.0.0.1` / `::1`) with
    /// private/loopback peers enabled.
    pub const fn loopback_for_test() -> Self {
        Self {
            bind_ipv4: Ipv4Addr::LOCALHOST,
            bind_ipv6: Ipv6Addr::LOCALHOST,
            advertised_ipv4: Some(Ipv4Addr::LOCALHOST),
            advertised_ipv6: Some(Ipv6Addr::LOCALHOST),
            port_range: None,
            allow_private_peers: true,
        }
    }

    /// Populate unset `advertised_ipv4` / `advertised_ipv6` from an endpoint-hint IP literal if
    /// present.
    pub(crate) fn with_endpoint_hint_fallback(mut self, endpoint_hint: Option<&str>) -> Self {
        let Some(hint_ip) = endpoint_hint.and_then(parse_ip_from_endpoint_hint) else {
            return self;
        };
        if hint_ip.is_unspecified() {
            return self;
        }
        match hint_ip {
            IpAddr::V4(ip) if self.advertised_ipv4.is_none() => {
                self.advertised_ipv4 = Some(ip);
            }
            IpAddr::V6(ip) if self.advertised_ipv6.is_none() => {
                self.advertised_ipv6 = Some(ip);
            }
            _ => {}
        }
        self
    }

    /// Return whether `ip` is permitted as a peer or advertised IP under this configuration.
    pub fn allows_peer_ip(&self, ip: IpAddr) -> bool {
        !ip.is_unspecified() && (self.allow_private_peers || rings_network_policy::is_public_ip(ip))
    }

    /// Resolve the externally reachable IP address to report for a bound socket of `ipv6` family.
    pub fn resolve_advertised_ip(&self, ipv6: bool) -> Result<IpAddr> {
        let candidate = if ipv6 {
            self.advertised_ipv6
                .or_else(|| (!self.bind_ipv6.is_unspecified()).then_some(self.bind_ipv6))
                .map(IpAddr::V6)
        } else {
            self.advertised_ipv4
                .or_else(|| (!self.bind_ipv4.is_unspecified()).then_some(self.bind_ipv4))
                .map(IpAddr::V4)
        };
        let Some(ip) = candidate else {
            return Err(Error::OnionRouteError(OnionRouteError::UdpTarget(
                "onion udp exit has no advertisable external IP address".to_string(),
            )));
        };
        if !self.allows_peer_ip(ip) {
            return Err(Error::NoPermission);
        }
        Ok(ip)
    }
}

fn parse_ip_from_endpoint_hint(raw: &str) -> Option<IpAddr> {
    let trimmed = raw.trim();
    if trimmed.is_empty() {
        return None;
    }
    if let Ok(ip) = trimmed.trim_matches(['[', ']']).parse::<IpAddr>() {
        return Some(ip);
    }
    if let Ok(addr) = trimmed.parse::<SocketAddr>() {
        return Some(addr.ip());
    }
    let parsed = Url::parse(trimmed).ok()?;
    let host = parsed.host_str()?.trim_matches(['[', ']']);
    host.parse::<IpAddr>().ok()
}

/// Maximum outbound UDP datagrams one `onion::udp` exit session may transmit per 1-second clock
/// window (`2,048` pps), preventing small-frame cell packing from turning an exit into a packet-rate
/// amplifier while leaving headroom for bidirectional real-time media bursts.
pub const MAX_OUTBOUND_DATAGRAMS_PER_SEC: u32 = 2_048;

#[cfg(rings_native)]
const UDP_OPEN_TIMEOUT: Duration = Duration::from_secs(10);

#[cfg(rings_native)]
const UDP_OPEN_RESPONSE_QUANTUM_MS: u128 = 250;

#[cfg(rings_native)]
const MAX_EVEN_PORT_EPHEMERAL_TRIES: usize = 64;

#[cfg(rings_native)]
const MAX_UDP_RECV_DATAGRAM_BYTES: usize = MAX_ONION_UDP_PAYLOAD_BYTES;

#[cfg(rings_native)]
const MAX_WRITER_REASSEMBLY_BYTES: usize =
    (MAX_ONION_UDP_FRAME_BYTES + ONION_UDP_FRAME_LEN_BYTES) * 4;

#[cfg(rings_native)]
const MAX_READER_REASSEMBLY_BYTES: usize =
    (MAX_ONION_UDP_FRAME_BYTES + ONION_UDP_FRAME_LEN_BYTES) * 4;

#[cfg(rings_native)]
fn udp_target(reason: String) -> Error {
    Error::OnionRouteError(OnionRouteError::UdpTarget(reason))
}

#[cfg(rings_native)]
fn open_response_deadline(opened_at: Instant, now: Instant) -> Instant {
    let elapsed_ms = now.saturating_duration_since(opened_at).as_millis();
    let quanta = elapsed_ms.div_ceil(UDP_OPEN_RESPONSE_QUANTUM_MS).max(1);
    let deadline_ms =
        u64::try_from(quanta.saturating_mul(UDP_OPEN_RESPONSE_QUANTUM_MS)).unwrap_or(u64::MAX);
    opened_at
        .checked_add(Duration::from_millis(deadline_ms))
        .unwrap_or(now)
}

#[cfg(rings_native)]
fn socket_addr_target(addr: SocketAddr) -> Option<OnionProxyTarget> {
    let host = addr.ip().to_string();
    OnionProxyTarget::new(&host, addr.port()).ok()
}

#[cfg(rings_native)]
async fn bind_udp_socket(mode: OnionUdpBindMode, config: &OnionUdpBindConfig) -> Result<UdpSocket> {
    let bind_ip: IpAddr = if mode.is_ipv6() {
        IpAddr::V6(config.bind_ipv6)
    } else {
        IpAddr::V4(config.bind_ipv4)
    };
    let even_only = mode.requires_even_port();
    if let Some((min_port, max_port)) = config.port_range {
        if min_port == 0 || min_port > max_port {
            return Err(udp_target(format!(
                "invalid udp port range {min_port}..={max_port}"
            )));
        }
        let mut last_err = None;
        for port in min_port..=max_port {
            if even_only && port % 2 != 0 {
                continue;
            }
            match UdpSocket::bind(SocketAddr::new(bind_ip, port)).await {
                Ok(sock) => return Ok(sock),
                Err(err) => last_err = Some(err),
            }
        }
        return Err(udp_target(format!(
            "no free udp port in {min_port}..={max_port}: {last_err:?}"
        )));
    }

    if !even_only {
        return UdpSocket::bind(SocketAddr::new(bind_ip, 0))
            .await
            .map_err(|err| udp_target(format!("bind {bind_ip}:0 failed: {err}")));
    }

    let mut held_odd_sockets = Vec::with_capacity(MAX_EVEN_PORT_EPHEMERAL_TRIES);
    for _ in 0..MAX_EVEN_PORT_EPHEMERAL_TRIES {
        let sock = UdpSocket::bind(SocketAddr::new(bind_ip, 0))
            .await
            .map_err(|err| udp_target(format!("bind {bind_ip}:0 failed: {err}")))?;
        let local = sock
            .local_addr()
            .map_err(|err| udp_target(format!("local_addr failed: {err}")))?;
        if local.port() != 0 && local.port() % 2 == 0 {
            return Ok(sock);
        }
        held_odd_sockets.push(sock);
    }
    Err(udp_target(
        "failed to bind an even ephemeral UDP port".to_string(),
    ))
}

/// Native OS socket world of an `onion::udp` exit.
#[cfg(rings_native)]
pub(crate) struct OnionUdpWorld {
    policy: OnionExitPolicy,
    accounting: OnionExitAccounting,
    config: OnionUdpBindConfig,
    clock: SharedClock,
}

#[cfg(rings_native)]
impl OnionUdpWorld {
    /// Construct an `onion::udp` exit world under `policy`, `accounting`, `config`, and `clock`.
    pub(crate) fn new(
        policy: OnionExitPolicy,
        accounting: OnionExitAccounting,
        config: OnionUdpBindConfig,
        clock: SharedClock,
    ) -> Self {
        Self {
            policy,
            accounting,
            config,
            clock,
        }
    }

    async fn bind_session(
        &self,
        target: &OnionProxyTarget,
    ) -> Result<(OnionUdpReader, OnionUdpWriter)> {
        let mode = OnionUdpBindMode::from_target(target)?;
        if !self.policy.allows_udp_bind(mode) {
            return Err(Error::NoPermission);
        }
        let advertised_ip = self.config.resolve_advertised_ip(mode.is_ipv6())?;
        let socket = bind_udp_socket(mode, &self.config).await?;
        let os_local = socket
            .local_addr()
            .map_err(|err| udp_target(format!("local_addr: {err}")))?;
        if os_local.port() == 0 {
            return Err(udp_target("bound UDP socket reported port 0".to_string()));
        }
        let local_addr = SocketAddr::new(advertised_ip, os_local.port());
        let bound_bytes = Bytes::from(encode_server_frame(&OnionUdpServerFrame::Bound {
            local_addr,
        })?);
        let shared_socket = Arc::new(socket);
        let pinholes = Arc::new(SyncMutex::new(OnionUdpPinholeTable::new()));
        Ok((
            OnionUdpReader {
                socket: Arc::clone(&shared_socket),
                local_addr,
                policy: self.policy.clone(),
                accounting: self.accounting.clone(),
                config: self.config,
                clock: self.clock.clone(),
                pinholes: Arc::clone(&pinholes),
                pending: bound_bytes,
                recv_buf: vec![0_u8; MAX_UDP_RECV_DATAGRAM_BYTES],
            },
            OnionUdpWriter {
                socket: shared_socket,
                local_addr,
                policy: self.policy.clone(),
                config: self.config,
                clock: self.clock.clone(),
                pinholes,
                buffer: BytesMut::new(),
                rate_window: OutboundDatagramWindow::default(),
            },
        ))
    }
}

#[cfg(rings_native)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
struct OutboundDatagramWindow {
    second: u128,
    count: u32,
}

#[cfg(rings_native)]
impl OutboundDatagramWindow {
    fn try_admit(&mut self, now_ms: u128) -> bool {
        let sec = now_ms / 1_000;
        if self.second != sec {
            self.second = sec;
            self.count = 0;
        }
        if self.count >= MAX_OUTBOUND_DATAGRAMS_PER_SEC {
            return false;
        }
        self.count = self.count.saturating_add(1);
        true
    }
}

/// Read half of an `onion::udp` exit session, emitting [`OnionUdpServerFrame::Bound`] followed by
/// pinhole-admitted [`OnionUdpServerFrame::RecvDatagram`] frames.
#[cfg(rings_native)]
pub(crate) struct OnionUdpReader {
    socket: Arc<UdpSocket>,
    local_addr: SocketAddr,
    policy: OnionExitPolicy,
    accounting: OnionExitAccounting,
    config: OnionUdpBindConfig,
    clock: SharedClock,
    pinholes: Arc<SyncMutex<OnionUdpPinholeTable>>,
    pending: Bytes,
    recv_buf: Vec<u8>,
}

/// Write half of an `onion::udp` exit session, applying [`OnionUdpClientFrame::AllowPeerIp`]
/// pinholes and sending [`OnionUdpClientFrame::SendDatagram`] payloads.
#[cfg(rings_native)]
pub(crate) struct OnionUdpWriter {
    socket: Arc<UdpSocket>,
    local_addr: SocketAddr,
    policy: OnionExitPolicy,
    config: OnionUdpBindConfig,
    clock: SharedClock,
    pinholes: Arc<SyncMutex<OnionUdpPinholeTable>>,
    buffer: BytesMut,
    rate_window: OutboundDatagramWindow,
}

#[cfg(rings_native)]
#[async_trait::async_trait]
impl OnionWorld for OnionUdpWorld {
    type Reader = OnionUdpReader;
    type Writer = OnionUdpWriter;

    fn admits_open(&self, policy: &OnionExitPolicy, target: &OnionProxyTarget) -> bool {
        OnionUdpBindMode::from_target(target).is_ok_and(|mode| policy.allows_udp_bind(mode))
    }

    async fn open(&self, target: &OnionProxyTarget) -> Result<(Self::Reader, Self::Writer)> {
        let opened_at = Instant::now();
        let outcome = match timeout(UDP_OPEN_TIMEOUT, self.bind_session(target)).await {
            Ok(res) => res,
            Err(_) => Err(Error::OnionRouteError(OnionRouteError::UdpOpenTimedOut)),
        };
        tokio::time::sleep_until(open_response_deadline(opened_at, Instant::now())).await;
        outcome
    }
}

#[cfg(rings_native)]
#[async_trait::async_trait]
impl OnionWorldReader for OnionUdpReader {
    async fn read(&mut self, max: usize) -> Result<Option<Bytes>> {
        let max = max.max(1);
        loop {
            if !self.pending.is_empty() {
                let take = max.min(self.pending.len());
                return Ok(Some(self.pending.split_to(take)));
            }
            let (len, peer) = self
                .socket
                .recv_from(&mut self.recv_buf)
                .await
                .map_err(|err| udp_target(format!("recv_from: {err}")))?;
            if peer.port() == 0
                || peer.is_ipv6() != self.local_addr.is_ipv6()
                || !self.config.allows_peer_ip(peer.ip())
            {
                continue;
            }
            let now_ms = self.clock.now_ms();
            if !lock(&self.pinholes)?.allows(peer.ip(), now_ms) {
                continue;
            }
            let Some(peer_target) = socket_addr_target(peer) else {
                continue;
            };
            if !self.policy.allows_target(&peer_target) {
                continue;
            }
            let Some(payload) = self.recv_buf.get(..len).map(<[u8]>::to_vec) else {
                continue;
            };
            let frame = OnionUdpServerFrame::RecvDatagram { peer, payload };
            let encoded = encode_server_frame(&frame)?;
            let byte_count = u64::try_from(encoded.len()).map_err(|_| Error::InvalidData)?;
            self.accounting
                .record_bytes(&self.policy, byte_count, self.accounting.now_ms())?;
            self.pending = Bytes::from(encoded);
        }
    }
}

#[cfg(rings_native)]
#[async_trait::async_trait]
impl OnionWorldWriter for OnionUdpWriter {
    async fn write(&mut self, bytes: Bytes) -> Result<()> {
        if self.buffer.len().saturating_add(bytes.len()) > MAX_WRITER_REASSEMBLY_BYTES {
            return Err(Error::InvalidData);
        }
        self.buffer.extend_from_slice(&bytes);
        while let Some(frame) = decode_next_client_frame(&mut self.buffer)? {
            match frame {
                OnionUdpClientFrame::AllowPeerIp { peer_ip, ttl_ms } => {
                    if peer_ip.is_ipv6() != self.local_addr.is_ipv6()
                        || !self.config.allows_peer_ip(peer_ip)
                    {
                        tracing::debug!(
                            %peer_ip,
                            "onion udp exit ignored pinhole for disallowed peer IP"
                        );
                        continue;
                    }
                    let now_ms = self.clock.now_ms();
                    let _ = lock(&self.pinholes)?.allow_ip(peer_ip, ttl_ms, now_ms);
                }
                OnionUdpClientFrame::SendDatagram { peer, payload } => {
                    if peer.port() == 0
                        || peer.is_ipv6() != self.local_addr.is_ipv6()
                        || !self.config.allows_peer_ip(peer.ip())
                    {
                        tracing::debug!(
                            %peer,
                            "onion udp exit dropped outbound datagram to disallowed peer"
                        );
                        continue;
                    }
                    let Some(peer_target) = socket_addr_target(peer) else {
                        continue;
                    };
                    if !self.policy.allows_target(&peer_target) {
                        tracing::debug!(
                            %peer,
                            "onion udp exit policy denied outbound datagram target"
                        );
                        continue;
                    }
                    if !self.rate_window.try_admit(self.clock.now_ms()) {
                        tracing::debug!(
                            %peer,
                            max_pps = MAX_OUTBOUND_DATAGRAMS_PER_SEC,
                            "onion udp exit dropped outbound datagram exceeding per-second rate cap"
                        );
                        continue;
                    }
                    self.socket
                        .send_to(&payload, peer)
                        .await
                        .map_err(|err| udp_target(format!("send_to {peer}: {err}")))?;
                }
            }
        }
        Ok(())
    }

    async fn shutdown(&mut self) -> Result<()> {
        Ok(())
    }
}

#[cfg(rings_native)]
struct UdpSessionReaderState {
    receiver: OnionStreamReceiver,
    buffer: BytesMut,
}

/// Cancel-safe RAII lease over a synchronous `Option<T>` slot.
///
/// If the owning async operation (`recv_from` or `send_framed`) returns a live outcome or is
/// cancelled while awaiting an event or queue slot, `Drop` restores `T` into `slot` unless
/// [`NativeOnionUdpSession::close`] has set `closed`. Terminal stream states (`Fin`, `Failed`, or
/// framing error) call [`Self::retire`] so dead stream halves are dropped immediately.
#[cfg(rings_native)]
struct SlotLease<'a, T> {
    slot: &'a SyncMutex<Option<T>>,
    closed: &'a AtomicBool,
    value: Option<T>,
}

#[cfg(rings_native)]
impl<'a, T> SlotLease<'a, T> {
    fn acquire(slot: &'a SyncMutex<Option<T>>, closed: &'a AtomicBool) -> Result<Option<Self>> {
        if closed.load(Ordering::SeqCst) {
            if let Ok(mut guard) = lock(slot) {
                let _ = guard.take();
            }
            return Ok(None);
        }
        let Some(val) = lock(slot)?.take() else {
            return Ok(None);
        };
        Ok(Some(Self {
            slot,
            closed,
            value: Some(val),
        }))
    }

    fn retire(&mut self) {
        let _ = self.value.take();
    }
}

#[cfg(rings_native)]
impl<T> Drop for SlotLease<'_, T> {
    fn drop(&mut self) {
        let Some(val) = self.value.take() else {
            return;
        };
        if self.closed.load(Ordering::SeqCst) {
            return;
        }
        if let Ok(mut guard) = lock(self.slot) {
            *guard = Some(val);
        }
        if self.closed.load(Ordering::SeqCst) {
            if let Ok(mut guard) = lock(self.slot) {
                let _ = guard.take();
            }
        }
    }
}

/// Client handle to an opened `onion::udp` datagram session whose exit has bound an OS UDP socket.
#[cfg(rings_native)]
pub struct NativeOnionUdpSession {
    local_addr: SocketAddr,
    closed: AtomicBool,
    closed_notify: Notify,
    send_gate: AsyncMutex<()>,
    sender_slot: SyncMutex<Option<OnionStreamSender>>,
    recv_gate: AsyncMutex<()>,
    receiver_slot: SyncMutex<Option<UdpSessionReaderState>>,
}

#[cfg(rings_native)]
impl NativeOnionUdpSession {
    /// Complete the `onion::udp` open handshake by awaiting the exit's initial
    /// [`OnionUdpServerFrame::Bound`] frame.
    pub(crate) async fn from_stream(stream: OnionClientStream) -> Result<Self> {
        let (sender, mut receiver) = stream.split();
        let mut buffer = BytesMut::new();
        let bound_addr = timeout(ONION_SESSION_OPEN_TIMEOUT, async {
            loop {
                if let Some(frame) = decode_next_server_frame(&mut buffer)? {
                    return match frame {
                        OnionUdpServerFrame::Bound { local_addr }
                            if !local_addr.ip().is_unspecified() && local_addr.port() != 0 =>
                        {
                            Ok(local_addr)
                        }
                        OnionUdpServerFrame::Bound { local_addr } => Err(udp_target(format!(
                            "exit reported invalid bound address {local_addr}"
                        ))),
                        OnionUdpServerFrame::RecvDatagram { .. } => Err(udp_target(
                            "exit sent datagram before initial Bound frame".to_string(),
                        )),
                    };
                }
                match receiver.next().await {
                    Some(OnionStreamEvent::Data(chunk)) => {
                        if buffer.len().saturating_add(chunk.len()) > MAX_READER_REASSEMBLY_BYTES {
                            return Err(Error::InvalidData);
                        }
                        buffer.extend_from_slice(&chunk);
                    }
                    Some(OnionStreamEvent::Fin | OnionStreamEvent::Failed) | None => {
                        return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
                    }
                }
            }
        })
        .await
        .map_err(|_| Error::OnionRouteError(OnionRouteError::UdpOpenTimedOut))??;

        Ok(Self {
            local_addr: bound_addr,
            closed: AtomicBool::new(false),
            closed_notify: Notify::new(),
            send_gate: AsyncMutex::new(()),
            sender_slot: SyncMutex::new(Some(sender)),
            recv_gate: AsyncMutex::new(()),
            receiver_slot: SyncMutex::new(Some(UdpSessionReaderState { receiver, buffer })),
        })
    }

    /// Externally reachable UDP socket address bound by the exit for this session.
    pub const fn local_addr(&self) -> SocketAddr {
        self.local_addr
    }

    /// Open or refresh an inbound pinhole for `peer_ip` for `ttl` at the exit socket.
    pub async fn allow_peer_ip(&self, peer_ip: IpAddr, ttl: Duration) -> Result<()> {
        let ttl_ms = u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX);
        let framed = encode_client_frame(&OnionUdpClientFrame::AllowPeerIp { peer_ip, ttl_ms })?;
        self.send_framed(Bytes::from(framed)).await
    }

    /// Transmit one UDP datagram from the exit's bound socket to `peer`.
    pub async fn send_to(&self, payload: &[u8], peer: SocketAddr) -> Result<usize> {
        let framed = encode_client_frame(&OnionUdpClientFrame::SendDatagram {
            peer,
            payload: payload.to_vec(),
        })?;
        self.send_framed(Bytes::from(framed)).await?;
        Ok(payload.len())
    }

    /// Receive the next inbound UDP datagram `(payload, peer)` from the exit's bound socket, or
    /// `Ok(None)` when the session closes cleanly or via [`Self::close`].
    ///
    /// Cancel-safe: dropping a pending `recv_from` future (for example inside a losing
    /// `tokio::select!` branch) preserves both the underlying stream receiver and any partially
    /// buffered frame bytes for the next call.
    pub async fn recv_from(&self) -> Result<Option<(Bytes, SocketAddr)>> {
        let _guard = self.recv_gate.lock().await;
        let Some(mut lease) = SlotLease::acquire(&self.receiver_slot, &self.closed)? else {
            return Ok(None);
        };
        loop {
            let Some(state) = lease.value.as_mut() else {
                return Ok(None);
            };
            match decode_next_server_frame(&mut state.buffer) {
                Ok(Some(OnionUdpServerFrame::RecvDatagram { peer, payload })) => {
                    return Ok(Some((Bytes::from(payload), peer)));
                }
                Ok(Some(OnionUdpServerFrame::Bound { .. })) => {
                    lease.retire();
                    return Err(Error::InvalidData);
                }
                Ok(None) => {}
                Err(err) => {
                    lease.retire();
                    return Err(err);
                }
            }
            let notified = self.closed_notify.notified();
            tokio::pin!(notified);
            if self.closed.load(Ordering::SeqCst) {
                lease.retire();
                return Ok(None);
            }
            let event = tokio::select! {
                () = &mut notified => None,
                ev = state.receiver.next() => ev,
            };
            match event {
                Some(OnionStreamEvent::Data(chunk)) => {
                    if state.buffer.len().saturating_add(chunk.len()) > MAX_READER_REASSEMBLY_BYTES
                    {
                        lease.retire();
                        return Err(Error::InvalidData);
                    }
                    state.buffer.extend_from_slice(&chunk);
                }
                Some(OnionStreamEvent::Fin) | None => {
                    lease.retire();
                    return Ok(None);
                }
                Some(OnionStreamEvent::Failed) => {
                    lease.retire();
                    return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
                }
            }
        }
    }

    /// Synchronously signal closure, drop idle stream halves immediately, and wake any parked
    /// [`Self::recv_from`] or [`Self::send_to`] without blocking on reader/writer locks.
    pub fn close(&self) {
        self.closed.store(true, Ordering::SeqCst);
        self.drop_sender_slot();
        self.drop_receiver_slot();
        self.closed_notify.notify_waiters();
    }

    async fn send_framed(&self, framed: Bytes) -> Result<()> {
        let _guard = self.send_gate.lock().await;
        let Some(mut lease) = SlotLease::acquire(&self.sender_slot, &self.closed)? else {
            return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
        };
        let Some(sender) = lease.value.as_mut() else {
            return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
        };
        let notified = self.closed_notify.notified();
        tokio::pin!(notified);
        if self.closed.load(Ordering::SeqCst) {
            lease.retire();
            return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
        }
        let ready = tokio::select! {
            () = &mut notified => Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed)),
            res = sender.ready() => res.map_err(|_| Error::OnionRouteError(OnionRouteError::UdpSessionClosed)),
        };
        if let Err(err) = ready {
            lease.retire();
            return Err(err);
        }
        if sender.start_send(framed).is_err() {
            lease.retire();
            return Err(Error::OnionRouteError(OnionRouteError::UdpSessionClosed));
        }
        Ok(())
    }

    fn drop_sender_slot(&self) {
        if let Ok(mut slot) = lock(&self.sender_slot) {
            let _ = slot.take();
        }
    }

    fn drop_receiver_slot(&self) {
        if let Ok(mut slot) = lock(&self.receiver_slot) {
            let _ = slot.take();
        }
    }
}

#[cfg(test)]
mod tests;