From f4dfa75e4c42ae99bbdafd8bf496538b9ed6c30c Mon Sep 17 00:00:00 2001 From: EAimTY Date: Sat, 4 Feb 2023 16:41:56 +0900 Subject: [PATCH] mark outbound UDP socket IPv6 only --- tuic-server/src/server.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tuic-server/src/server.rs b/tuic-server/src/server.rs index 093585f..88537e1 100644 --- a/tuic-server/src/server.rs +++ b/tuic-server/src/server.rs @@ -4,11 +4,12 @@ use crossbeam_utils::atomic::AtomicCell; use parking_lot::Mutex; use quinn::{Connecting, Connection as QuinnConnection, Endpoint, RecvStream, SendStream, VarInt}; use register_count::{Counter, Register}; +use socket2::{Domain, Protocol, SockAddr, Socket, Type}; use std::{ collections::{hash_map::Entry, HashMap}, future::Future, io::{Error as IoError, ErrorKind}, - net::{Ipv4Addr, Ipv6Addr, SocketAddr}, + net::{Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket as StdUdpSocket}, pin::Pin, sync::{ atomic::{AtomicBool, AtomicUsize, Ordering}, @@ -440,9 +441,13 @@ impl UdpSession { let socket_v4 = Arc::new(UdpSocket::bind(SocketAddr::from((Ipv4Addr::UNSPECIFIED, 0))).await?); let socket_v6 = if udp_relay_ipv6 { - Some(Arc::new( - UdpSocket::bind(SocketAddr::from((Ipv6Addr::UNSPECIFIED, 0))).await?, - )) + let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::UDP))?; + socket.set_only_v6(true)?; + socket.bind(&SockAddr::from(SocketAddr::from(( + Ipv6Addr::UNSPECIFIED, + 0, + ))))?; + Some(Arc::new(UdpSocket::from_std(StdUdpSocket::from(socket))?)) } else { None };