From 1238562566cb9d5dde948073591226680f70f686 Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sat, 30 Dec 2023 21:21:00 -0800 Subject: [PATCH] udp: tolerate true IPv4 dest addrs when dual-stack --- quinn-udp/tests/tests.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/quinn-udp/tests/tests.rs b/quinn-udp/tests/tests.rs index a2eb18a2fe..65c91095c0 100644 --- a/quinn-udp/tests/tests.rs +++ b/quinn-udp/tests/tests.rs @@ -1,6 +1,6 @@ use std::{ io::IoSliceMut, - net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, UdpSocket}, + net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, UdpSocket}, slice, }; @@ -125,7 +125,9 @@ fn test_send_recv(send: &Socket, recv: &Socket, transmit: Transmit) { let dst = meta.dst_ip.unwrap(); match (send_v6, recv_v6) { (_, false) => assert_eq!(dst, Ipv4Addr::LOCALHOST), - (false, true) => assert_eq!(dst, Ipv4Addr::LOCALHOST.to_ipv6_mapped()), + // Windows gives us real IPv4 addrs, whereas *nix use IPv6-mapped IPv4 + // addrs. Canonicalize to IPv6-mapped for robustness. + (false, true) => assert_eq!(ip_to_v6_mapped(dst), Ipv4Addr::LOCALHOST.to_ipv6_mapped()), (true, true) => assert_eq!(dst, Ipv6Addr::LOCALHOST), } } @@ -136,3 +138,10 @@ fn to_v6_mapped(x: SocketAddr) -> SocketAddr { SocketAddr::V6(_) => x, } } + +fn ip_to_v6_mapped(x: IpAddr) -> IpAddr { + match x { + IpAddr::V4(x) => IpAddr::V6(x.to_ipv6_mapped()), + IpAddr::V6(_) => x, + } +}