rama-socks5 0.4.0

SOCKS5 support for rama
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
use std::io::ErrorKind;

use rama_core::bytes::{Bytes, BytesMut};
use rama_core::error::{BoxError, ErrorExt};
use rama_core::telemetry::tracing;
use rama_net::address::{HostWithPort, SocketAddress};

use rama_udp::UdpSocket;

#[cfg(feature = "dns")]
use super::MaybeDnsResolver;
use crate::proto::udp::UdpHeader;

#[cfg(feature = "dns")]
use ::{
    rama_core::{error::ErrorContext, extensions::Extensions},
    rama_net::mode::DnsResolveIpMode,
};

use std::net::IpAddr;

#[derive(Debug)]
pub(super) struct UdpSocketRelay {
    client_address: SocketAddress,
    pinned_client_address: Option<SocketAddress>,
    unspecified_client_address_policy: UnspecifiedClientUdpAddressPolicy,
    tcp_peer_ip: Option<IpAddr>,

    north: UdpSocket,
    north_max_size: usize,
    south: UdpSocket,
    south_max_size: usize,

    north_read_buf: BytesMut,
    south_read_buf: BytesMut,

    north_write_buf: BytesMut,

    #[cfg(feature = "dns")]
    dns_resolve_mode: DnsResolveIpMode,
    #[cfg(feature = "dns")]
    dns_resolver: MaybeDnsResolver,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[non_exhaustive]
pub enum UnspecifiedClientUdpAddressPolicy {
    #[default]
    /// Accept the first UDP packet only when it matches the TCP peer IP, if known,
    /// then pin to that packet's full UDP source address.
    ///
    /// The IP match is canonicalized, so a v4-mapped IPv6 TCP peer matches the
    /// equivalent plain IPv4 UDP source. When the TCP peer IP is unknown (no
    /// [`SocketInfo`] in the extensions) this degrades to [`Self::PinToFirstPacket`].
    ///
    /// [`SocketInfo`]: rama_net::stream::SocketInfo
    PinToTcpPeerIp,
    /// Accept the first UDP packet from any source, then pin to that packet's full
    /// UDP source address.
    PinToFirstPacket,
    /// Accept UDP packets from any source. Replies are sent to the most recent
    /// accepted source address.
    AnySource,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(super) enum UdpRelayState {
    ReadNorth(SocketAddress),
    ReadSouth(SocketAddress),
}

impl UdpSocketRelay {
    pub(super) fn new(
        client_address: SocketAddress,
        north: UdpSocket,
        north_read_buf_size: usize,
        south: UdpSocket,
        south_read_buf_size: usize,
    ) -> Self {
        Self {
            client_address,
            pinned_client_address: None,
            unspecified_client_address_policy: UnspecifiedClientUdpAddressPolicy::default(),
            tcp_peer_ip: None,

            north,
            north_max_size: south_read_buf_size,
            north_read_buf: {
                let mut b = BytesMut::with_capacity(north_read_buf_size);
                b.resize(north_read_buf_size, 0);
                b
            },
            north_write_buf: BytesMut::new(),

            south,
            south_max_size: north_read_buf_size,
            south_read_buf: {
                let mut b = BytesMut::with_capacity(south_read_buf_size);
                b.resize(south_read_buf_size, 0);
                b
            },

            #[cfg(feature = "dns")]
            dns_resolve_mode: DnsResolveIpMode::default(),
            #[cfg(feature = "dns")]
            dns_resolver: Default::default(),
        }
    }

    pub(super) fn with_unspecified_client_address_policy(
        mut self,
        policy: UnspecifiedClientUdpAddressPolicy,
        tcp_peer_ip: Option<IpAddr>,
    ) -> Self {
        self.unspecified_client_address_policy = policy;
        self.tcp_peer_ip = tcp_peer_ip;
        self
    }

    pub(super) fn north_read_buf_slice(&self) -> &[u8] {
        &self.north_read_buf[..]
    }

    pub(super) fn south_read_buf_slice(&self) -> &[u8] {
        &self.south_read_buf[..]
    }

    pub(super) async fn recv(&mut self) -> Result<Option<UdpRelayState>, BoxError> {
        self.north_read_buf.clear();
        self.north_read_buf.resize(self.north_max_size, 0);

        self.south_read_buf.clear();
        self.south_read_buf.resize(self.south_max_size, 0);

        tokio::select! {
            result = self.north.recv_from(&mut self.north_read_buf[..]) => {
                match result {
                    Ok((len, src)) => {
                        tracing::trace!(
                            "north socket: received packet (len = {len}; src = {src})",
                        );

                        if !self.accept_north_source(src.into()) {
                            tracing::debug!(
                                network.peer.address = %self.client_address.ip_addr,
                                network.peer.port = %self.client_address.port,
                                "north socket: drop packet non-client packet (len = {len}; src = {src})",
                            );
                            return Ok(None);
                        }

                        let mut buf = &self.north_read_buf[..len];
                        let target_authority = match UdpHeader::read_from(&mut buf).await {
                            Ok(header) => {
                                if header.fragment_number != 0 {
                                    tracing::debug!(
                                        network.peer.address = %self.client_address.ip_addr,
                                        network.peer.port = %self.client_address.port,
                                        "received north packet with non-zero fragment number {}: drop it",
                                        header.fragment_number,
                                    );
                                    return Ok(None);
                                }
                                header.destination
                            }
                            Err(err) => {
                                tracing::debug!(
                                    network.peer.address = %self.client_address.ip_addr,
                                    network.peer.port = %self.client_address.port,
                                    "received invalid north packet: drop it: err = {err:?}",
                                );
                                return Ok(None);
                            }
                        };

                        let server_address = match self.authority_to_socket_address(target_authority).await {
                            Ok(addr) => addr,
                            Err(err) => {
                                tracing::debug!(
                                    network.peer.address = %self.client_address.ip_addr,
                                    network.peer.port = %self.client_address.port,
                                    "north packet's destination authority failed to (dns) resolve: {err:?}",
                                );
                                return Ok(None);
                            },
                        };

                        // remove header from payload
                        let offset = len - buf.len();
                        self.north_read_buf.copy_within(offset.., 0);
                        self.north_read_buf.truncate(len-offset);

                        Ok(Some(UdpRelayState::ReadNorth(server_address)))
                    }

                    Err(err) if is_transient_udp_io_error(&err) => {
                        tracing::debug!("north socket: non-fatal error: retry again: {err:?}");
                        Ok(None)
                    }

                    Err(err) => {
                        Err(err.context("north socket: fatal error"))
                    }
                }
            }

            result = self.south.recv_from(&mut self.south_read_buf[..]) => {
                match result {
                    Ok((len, src)) => {
                        tracing::trace!(
                            "south socket: received packet (len = {len}; src = {src})",
                        );
                        self.south_read_buf.truncate(len);
                        Ok(Some(UdpRelayState::ReadSouth(src.into())))
                    }

                    Err(err) if is_transient_udp_io_error(&err) => {
                        tracing::debug!("south socket: non-fatal error: retry again: {err:?}");
                        Ok(None)
                    }

                    Err(err) => {
                        Err(err.context("south socket: fatal error"))
                    }
                }
            }
        }
    }

    #[expect(clippy::needless_pass_by_ref_mut)]
    pub(super) async fn send_to_south(
        &mut self,
        data: Option<Bytes>,
        server_address: SocketAddress,
    ) -> Result<(), BoxError> {
        let result = if let Some(data) = data {
            tracing::trace!(
                network.peer.address = %self.client_address.ip_addr,
                network.peer.port = %self.client_address.port,
                server.address = %server_address.ip_addr,
                server.port = %server_address.port,
                "send packet south: data from input (len = {})",
                data.len()
            );
            if data.len() > self.south_max_size {
                tracing::trace!(
                    network.peer.address = %self.client_address.ip_addr,
                    network.peer.port = %self.client_address.port,
                    server.address = %server_address.ip_addr,
                    server.port = %server_address.port,
                    "drop packet south: length is too large for defined limit (len = {}; max len = {})",
                    data.len(),
                    self.south_max_size,
                );
                return Ok(());
            }
            self.south.send_to(&data, server_address.into_std()).await
        } else {
            tracing::trace!(
                network.peer.address = %self.client_address.ip_addr,
                network.peer.port = %self.client_address.port,
                server.address = %server_address.ip_addr,
                server.port = %server_address.port,
                "send packet south: data from north socket (len = {})",
                self.north_read_buf.len(),
            );
            self.south
                .send_to(&self.north_read_buf, server_address.into_std())
                .await
        };

        match result {
            Ok(len) => {
                tracing::trace!(
                    network.peer.address = %self.client_address.ip_addr,
                    network.peer.port = %self.client_address.port,
                    server.address = %server_address.ip_addr,
                    server.port = %server_address.port,
                    "send packet south: complete (len = {}; write len = {})",
                    self.north_read_buf.len(),
                    len
                );
                Ok(())
            }

            Err(err) => match err.downcast::<std::io::Error>() {
                Ok(err) if is_transient_udp_io_error(&err) => {
                    tracing::debug!(
                        "south socket: write error: packet lost but relay continues: {err:?}"
                    );
                    Ok(())
                }
                Ok(err) => {
                    tracing::debug!(?err, "south socket: fatal I/O write error: {err:?}");
                    Err(err.context("south socket fatal I/O write error"))
                }
                Err(err) => {
                    tracing::debug!("south socket: fatal unknown write error: {err:?}");
                    Err(err.context("south socket fatal unknown write error"))
                }
            },
        }
    }

    pub(super) async fn send_to_north(
        &mut self,
        data: Option<Bytes>,
        server_address: SocketAddress,
    ) -> Result<(), BoxError> {
        let Some(client_address) = self.north_send_address() else {
            tracing::trace!(
                network.peer.address = %self.client_address.ip_addr,
                network.peer.port = %self.client_address.port,
                server.address = %server_address.ip_addr,
                server.port = %server_address.port,
                "drop packet north: client UDP address is not known yet",
            );
            return Ok(());
        };

        let header = UdpHeader {
            fragment_number: 0,
            destination: server_address.into(),
        };

        self.north_write_buf.truncate(0);

        if let Some(data) = data {
            tracing::trace!(
                network.peer.address = %self.client_address.ip_addr,
                network.peer.port = %self.client_address.port,
                server.address = %server_address.ip_addr,
                server.port = %server_address.port,
                "send packet north: data from input (len = {})",
                data.len(),
            );

            if data.len() > self.north_max_size {
                tracing::trace!(
                    network.peer.address = %self.client_address.ip_addr,
                    network.peer.port = %self.client_address.port,
                    server.address = %server_address.ip_addr,
                    server.port = %server_address.port,
                    "drop packet north: length is too large for defined limit (len = {}; max len = {})",
                    data.len(),
                    self.north_max_size,
                );
                return Ok(());
            }

            header.write_to_buf(&mut self.north_write_buf)?;
            self.north_write_buf.extend_from_slice(&data);
        } else {
            tracing::trace!(
                network.peer.address = %self.client_address.ip_addr,
                network.peer.port = %self.client_address.port,
                server.address = %server_address.ip_addr,
                server.port = %server_address.port,
                "send packet north: data from south socket (len = {})",
                self.south_read_buf.len(),
            );
            header.write_to_buf(&mut self.north_write_buf)?;
            self.north_write_buf.extend_from_slice(&self.south_read_buf);
        };

        match self
            .north
            .send_to(&self.north_write_buf, client_address.into_std())
            .await
        {
            Ok(len) => {
                tracing::trace!(
                    network.peer.address = %self.client_address.ip_addr,
                    network.peer.port = %self.client_address.port,
                    server.address = %server_address.ip_addr,
                    server.port = %server_address.port,
                    "send packet north: complete (len = {}; write len = {})",
                    self.north_write_buf.len(),
                    len,
                );
                Ok(())
            }

            Err(err) => match err.downcast::<std::io::Error>() {
                Ok(err) if is_transient_udp_io_error(&err) => {
                    tracing::debug!(
                        "north socket: write error: packet lost but relay continues: {err:?}"
                    );
                    Ok(())
                }
                Ok(err) => {
                    tracing::debug!("north socket: fatal I/O write error: {err:?}");
                    Err(err.context("north socket fatal I/O write error"))
                }
                Err(err) => {
                    tracing::debug!("north socket: fatal unknown write error: {err:?}");
                    Err(err.context("north socket fatal unknown write error"))
                }
            },
        }
    }
}

impl UdpSocketRelay {
    fn accept_north_source(&mut self, src: SocketAddress) -> bool {
        if !self.client_address.ip_addr.is_unspecified() {
            return self.client_address.eq(&src);
        }

        match self.unspecified_client_address_policy {
            UnspecifiedClientUdpAddressPolicy::PinToTcpPeerIp => {
                // Compare canonical forms so a v4-mapped IPv6 TCP peer
                // (`::ffff:a.b.c.d`, e.g. an IPv4 client on a dual-stack TCP
                // listener) matches the plain IPv4 source the UDP socket reports.
                if let Some(tcp_peer_ip) = self.tcp_peer_ip
                    && src.ip_addr.to_canonical() != tcp_peer_ip.to_canonical()
                {
                    return false;
                }
                self.pin_or_match_client_source(src)
            }
            UnspecifiedClientUdpAddressPolicy::PinToFirstPacket => {
                self.pin_or_match_client_source(src)
            }
            UnspecifiedClientUdpAddressPolicy::AnySource => {
                self.pinned_client_address = Some(src);
                true
            }
        }
    }

    fn pin_or_match_client_source(&mut self, src: SocketAddress) -> bool {
        if let Some(address) = self.pinned_client_address {
            address == src
        } else {
            self.pinned_client_address = Some(src);
            true
        }
    }

    fn north_send_address(&self) -> Option<SocketAddress> {
        if self.client_address.ip_addr.is_unspecified() {
            self.pinned_client_address
        } else {
            Some(self.client_address)
        }
    }
}

fn is_transient_udp_io_error(err: &std::io::Error) -> bool {
    matches!(
        err.kind(),
        ErrorKind::WouldBlock
            | ErrorKind::TimedOut
            | ErrorKind::Interrupted
            | ErrorKind::ConnectionReset
            | ErrorKind::AddrNotAvailable
            | ErrorKind::PermissionDenied
    )
}

impl UdpSocketRelay {
    #[cfg(feature = "dns")]
    pub(super) fn with_dns_resolver(
        mut self,
        extensions: &Extensions,
        resolver: MaybeDnsResolver,
    ) -> Self {
        self.dns_resolver = resolver;
        if let Some(mode) = extensions.get_ref().copied() {
            self.dns_resolve_mode = mode;
        }
        self
    }

    pub(super) async fn authority_to_socket_address(
        &self,
        authority: HostWithPort,
    ) -> Result<SocketAddress, BoxError> {
        let HostWithPort { host, port } = authority;
        // IP fast path first — `try_as_ip` also bridges pct-encoded
        // dotted-quad inside `Uninterpreted`. Fall through to DNS
        // resolution of a typed `Domain` (bridged from `Uninterpreted`
        // via pct-decode + IDN). Non-promotable inputs (sub-delim,
        // IPvFuture) hit the error path inside `try_into_domain`.
        let ip_addr = match host.try_as_ip() {
            Ok(ip) => ip,
            Err(err) => self.resolve_domain_host(host, err.into_box_error()).await?,
        };
        Ok((ip_addr, port).into())
    }

    #[cfg(feature = "dns")]
    async fn resolve_domain_host(
        &self,
        host: rama_net::address::Host,
        source: BoxError,
    ) -> Result<IpAddr, BoxError> {
        use rama_dns::client::resolver::DnsAddressResolver as _;
        let _ = source;

        let domain = host
            .try_into_domain()
            .context("host is not resolvable as a domain via SOCKS5 udp relay")?;
        let dns_resolver = self
            .dns_resolver
            .as_ref()
            .context("domain cannot be resolved: no dns resolver defined")?;

        match self.dns_resolve_mode {
            DnsResolveIpMode::SingleIpV4 => Ok(IpAddr::V4(
                dns_resolver
                    .lookup_ipv4_rand(domain.clone())
                    .await
                    .context("no ipv4 addresses found during DNS lookup")?
                    .context("ipv4 dns lookup")?,
            )),
            DnsResolveIpMode::SingleIpV6 => Ok(IpAddr::V6(
                dns_resolver
                    .lookup_ipv6_rand(domain.clone())
                    .await
                    .context("no ipv6 addresses found during DNS lookup")?
                    .context("ipv6 dns lookup")?,
            )),
            DnsResolveIpMode::Dual | DnsResolveIpMode::DualPreferIpV4 => {
                crate::dns::race_resolve_dual(dns_resolver, domain.clone(), self.dns_resolve_mode)
                    .await
                    .context("receive resolved ip address")
            }
        }
    }

    #[cfg(not(feature = "dns"))]
    async fn resolve_domain_host(
        &self,
        host: rama_net::address::Host,
        source: BoxError,
    ) -> Result<IpAddr, BoxError> {
        let _ = self;
        let _ = host;
        Err(source.context("domain cannot be resolved: rama-socks5 dns feature is disabled"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Regression for Bug 2: send_to_north(None) was calling BytesMut::resize() after
    // truncate(0), which filled the buffer with N zeros before appending header+data,
    // producing [zeros][header][data] instead of the correct [header][data].
    #[tokio::test]
    async fn test_send_to_north_none_produces_header_then_data_no_leading_zeros() {
        // client_socket receives north-bound packets
        let client = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let client_addr: std::net::SocketAddr = client.local_addr().unwrap();
        let client_socket_addr: SocketAddress = client_addr.into();

        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south_addr = south.local_addr().unwrap();

        // server sends raw payload to south socket (simulates south data)
        let server = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let server_addr = server.local_addr().unwrap();
        let server_socket_addr: SocketAddress = server_addr.into();

        let payload = b"regression bug2 payload";
        server.send_to(payload, south_addr).await.unwrap();

        let mut relay = UdpSocketRelay::new(client_socket_addr, north, 4096, south, 4096);

        // recv() reads south packet and stores raw payload in south_read_buf
        let state = relay.recv().await.unwrap().unwrap();
        assert!(
            matches!(state, UdpRelayState::ReadSouth(addr) if addr == server_socket_addr),
            "expected ReadSouth from server address"
        );

        // send_to_north(None) should relay [socks5_header][payload] to client, no leading zeros
        relay.send_to_north(None, server_socket_addr).await.unwrap();

        let mut buf = vec![0u8; 4096];
        let (n, _) = tokio::time::timeout(
            std::time::Duration::from_millis(500),
            client.recv_from(&mut buf),
        )
        .await
        .expect("timed out waiting for north packet")
        .unwrap();
        let received = &buf[..n];

        let expected_header = UdpHeader {
            fragment_number: 0,
            destination: server_socket_addr.into(),
        };
        let mut expected = BytesMut::new();
        expected_header.write_to_buf(&mut expected).unwrap();
        expected.extend_from_slice(payload);

        assert_eq!(
            received,
            &expected[..],
            "send_to_north(None) must produce exactly [header][data]; \
             got {} bytes, expected {} bytes",
            received.len(),
            expected.len(),
        );
    }

    // Regression for Bug 3: when the client sends 0.0.0.0:0 in the UDP ASSOCIATE
    // request (RFC 1928 §7 allows this when the client doesn't know its address),
    // the relay accepts and pins the first valid UDP source instead of dropping
    // everything or accepting every later source.
    #[tokio::test]
    async fn test_recv_north_unspecified_client_addr_pins_first_source() {
        // client_address = 0.0.0.0:0 — the RFC 1928 §7 all-zeros sentinel
        let client_addr = SocketAddress::default_ipv4(0);

        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let north_addr = north.local_addr().unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096);

        // Any sender (127.0.0.1:OS-assigned) sends a valid SOCKS5 UDP packet to north
        let sender = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let target: SocketAddress = SocketAddress::local_ipv4(9999);
        let header = UdpHeader {
            fragment_number: 0,
            destination: target.into(),
        };
        let mut packet = BytesMut::new();
        header.write_to_buf(&mut packet).unwrap();
        packet.extend_from_slice(b"data");
        sender.send_to(&packet, north_addr).await.unwrap();

        let state = tokio::time::timeout(std::time::Duration::from_millis(500), relay.recv())
            .await
            .expect("timed out")
            .unwrap()
            .expect("first packet must not be dropped when client_address is 0.0.0.0:0");

        assert!(
            matches!(state, UdpRelayState::ReadNorth(_)),
            "RFC 1928 §7: the first packet selects the client UDP source when client_address is 0.0.0.0:0"
        );

        let second_sender = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        second_sender.send_to(&packet, north_addr).await.unwrap();
        let state = tokio::time::timeout(std::time::Duration::from_millis(500), relay.recv())
            .await
            .expect("timed out")
            .unwrap();

        assert!(
            state.is_none(),
            "packets from a different UDP source must be dropped after the first source is pinned"
        );
    }

    #[tokio::test]
    async fn test_recv_north_unspecified_client_addr_any_source_policy() {
        let client_addr = SocketAddress::default_ipv4(0);

        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let north_addr = north.local_addr().unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096)
            .with_unspecified_client_address_policy(
                UnspecifiedClientUdpAddressPolicy::AnySource,
                None,
            );

        let target: SocketAddress = SocketAddress::local_ipv4(9999);
        let header = UdpHeader {
            fragment_number: 0,
            destination: target.into(),
        };
        let mut packet = BytesMut::new();
        header.write_to_buf(&mut packet).unwrap();
        packet.extend_from_slice(b"data");

        for _ in 0..2 {
            let sender = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
            sender.send_to(&packet, north_addr).await.unwrap();

            let state = tokio::time::timeout(std::time::Duration::from_millis(500), relay.recv())
                .await
                .expect("timed out")
                .unwrap();

            assert!(
                matches!(state, Some(UdpRelayState::ReadNorth(_))),
                "AnySource policy must preserve the historical accept-any-source behavior"
            );
        }
    }

    #[tokio::test]
    async fn test_unspecified_client_addr_tcp_peer_ip_policy_rejects_other_ip() {
        let client_addr = SocketAddress::default_ipv4(0);
        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096)
            .with_unspecified_client_address_policy(
                UnspecifiedClientUdpAddressPolicy::PinToTcpPeerIp,
                Some(std::net::IpAddr::from([127, 0, 0, 2])),
            );

        assert!(!relay.accept_north_source(SocketAddress::local_ipv4(12345)));
    }

    #[tokio::test]
    async fn test_unspecified_client_addr_tcp_peer_ip_policy_accepts_and_pins_matching_ip() {
        let client_addr = SocketAddress::default_ipv4(0);
        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096)
            .with_unspecified_client_address_policy(
                UnspecifiedClientUdpAddressPolicy::PinToTcpPeerIp,
                Some(std::net::IpAddr::from([127, 0, 0, 1])),
            );

        // First packet from the matching TCP peer IP is accepted and pins the
        // full (ip, port) source.
        assert!(relay.accept_north_source(SocketAddress::local_ipv4(40000)));
        // A later packet from the same IP but a different port is rejected.
        assert!(!relay.accept_north_source(SocketAddress::local_ipv4(40001)));
        // North-bound replies now target the pinned source.
        assert_eq!(
            relay.north_send_address(),
            Some(SocketAddress::local_ipv4(40000))
        );
    }

    #[tokio::test]
    async fn test_unspecified_client_addr_tcp_peer_ip_policy_canonicalizes_v4_mapped() {
        let client_addr = SocketAddress::default_ipv4(0);
        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        // A v4-mapped IPv6 TCP peer (as a dual-stack listener reports an IPv4
        // client) must match the plain IPv4 UDP source.
        let v4_mapped = std::net::Ipv4Addr::new(127, 0, 0, 1).to_ipv6_mapped();
        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096)
            .with_unspecified_client_address_policy(
                UnspecifiedClientUdpAddressPolicy::PinToTcpPeerIp,
                Some(std::net::IpAddr::V6(v4_mapped)),
            );

        assert!(relay.accept_north_source(SocketAddress::local_ipv4(40000)));
    }

    #[tokio::test]
    async fn test_send_to_north_dropped_when_unspecified_client_not_yet_pinned() {
        let client_addr = SocketAddress::default_ipv4(0);
        let north = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();
        let south = tokio::net::UdpSocket::bind("127.0.0.1:0").await.unwrap();

        let mut relay = UdpSocketRelay::new(client_addr, north, 4096, south, 4096);

        // No north packet has pinned the client yet, so the reply target is unknown.
        assert_eq!(relay.north_send_address(), None);
        // Sending north in that state is a silent no-op (dropped), not an error.
        relay
            .send_to_north(
                Some(Bytes::from_static(b"data")),
                SocketAddress::local_ipv4(9999),
            )
            .await
            .unwrap();
        assert_eq!(relay.north_send_address(), None);
    }
}