diff options
author | rustdesk <[email protected]> | 2023-01-06 20:31:15 +0800 |
---|---|---|
committer | rustdesk <[email protected]> | 2023-01-06 20:31:15 +0800 |
commit | e70d82b30f3a3982f9772175f121d735969494e8 (patch) | |
tree | 533f528c01b791a0f7d98504a5eaa26b255ac816 /libs | |
parent | d7b2060a5b179a0c9f4c905f39c7497faab0fa9b (diff) | |
download | rustdesk-server-e70d82b30f3a3982f9772175f121d735969494e8.tar.gz rustdesk-server-e70d82b30f3a3982f9772175f121d735969494e8.zip |
ipv6 support draft
Diffstat (limited to 'libs')
-rw-r--r-- | libs/hbb_common/src/udp.rs | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/libs/hbb_common/src/udp.rs b/libs/hbb_common/src/udp.rs index 38121a4..bfed849 100644 --- a/libs/hbb_common/src/udp.rs +++ b/libs/hbb_common/src/udp.rs @@ -5,7 +5,7 @@ use futures::{SinkExt, StreamExt}; use protobuf::Message; use socket2::{Domain, Socket, Type}; use std::net::SocketAddr; -use tokio::net::{ToSocketAddrs, UdpSocket}; +use tokio::net::{lookup_host, ToSocketAddrs, UdpSocket}; use tokio_socks::{udp::Socks5UdpFramed, IntoTargetAddr, TargetAddr, ToProxyAddrs}; use tokio_util::{codec::BytesCodec, udp::UdpFramed}; @@ -37,35 +37,28 @@ fn new_socket(addr: SocketAddr, reuse: bool, buf_size: usize) -> Result<Socket, addr, socket.recv_buffer_size() ); + if addr.is_ipv6() && addr.ip().is_unspecified() && addr.port() > 0 { + socket.set_only_v6(false).ok(); + } socket.bind(&addr.into())?; Ok(socket) } impl FramedSocket { pub async fn new<T: ToSocketAddrs>(addr: T) -> ResultType<Self> { - let socket = UdpSocket::bind(addr).await?; - Ok(Self::Direct(UdpFramed::new(socket, BytesCodec::new()))) + Self::new_reuse(addr, false, 0).await } #[allow(clippy::never_loop)] - pub async fn new_reuse<T: std::net::ToSocketAddrs>(addr: T) -> ResultType<Self> { - for addr in addr.to_socket_addrs()? { - let socket = new_socket(addr, true, 0)?.into_udp_socket(); - return Ok(Self::Direct(UdpFramed::new( - UdpSocket::from_std(socket)?, - BytesCodec::new(), - ))); - } - bail!("could not resolve to any address"); - } - - pub async fn new_with_buf_size<T: std::net::ToSocketAddrs>( + pub async fn new_reuse<T: ToSocketAddrs>( addr: T, + reuse: bool, buf_size: usize, ) -> ResultType<Self> { - for addr in addr.to_socket_addrs()? { + for addr in lookup_host(&addr).await? { + let socket = new_socket(addr, reuse, buf_size)?.into_udp_socket(); return Ok(Self::Direct(UdpFramed::new( - UdpSocket::from_std(new_socket(addr, false, buf_size)?.into_udp_socket())?, + UdpSocket::from_std(socket)?, BytesCodec::new(), ))); } @@ -165,12 +158,12 @@ impl FramedSocket { } } - pub fn is_ipv4(&self) -> bool { + pub fn local_addr(&self) -> Option<SocketAddr> { if let FramedSocket::Direct(x) = self { if let Ok(v) = x.get_ref().local_addr() { - return v.is_ipv4(); + return Some(v); } } - true + None } } |