aboutsummaryrefslogtreecommitdiffhomepage
path: root/libs
diff options
context:
space:
mode:
authorRustDesk <[email protected]>2023-01-07 00:28:18 +0800
committerGitHub <[email protected]>2023-01-07 00:28:18 +0800
commit0e01cfcd3abf77a2aafba2e481b4b621469765d7 (patch)
treedd16dd25a4a23709743837b3ca0137ce6380aa9c /libs
parent60a6d672c5b148cada689046389984fdaaad9a1d (diff)
parente70d82b30f3a3982f9772175f121d735969494e8 (diff)
downloadrustdesk-server-0e01cfcd3abf77a2aafba2e481b4b621469765d7.tar.gz
rustdesk-server-0e01cfcd3abf77a2aafba2e481b4b621469765d7.zip
Merge branch 'master' into fix-clippy-warning
Diffstat (limited to 'libs')
-rw-r--r--libs/hbb_common/src/udp.rs34
1 files changed, 12 insertions, 22 deletions
diff --git a/libs/hbb_common/src/udp.rs b/libs/hbb_common/src/udp.rs
index 05e1037..54ef8bd 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,38 +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())))
- }
-
- pub async fn new_reuse<T: std::net::ToSocketAddrs>(addr: T) -> ResultType<Self> {
- let addr = addr
- .to_socket_addrs()?
- .next()
- .context("could not resolve to any address")?;
- let socket = new_socket(addr, true, 0)?.into_udp_socket();
- Ok(Self::Direct(UdpFramed::new(
- UdpSocket::from_std(socket)?,
- BytesCodec::new(),
- )))
+ Self::new_reuse(addr, false, 0).await
}
- 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> {
- let addr = addr
- .to_socket_addrs()?
+ let addr = lookup_host(&addr).await?
.next()
.context("could not resolve to any address")?;
Ok(Self::Direct(UdpFramed::new(
- UdpSocket::from_std(new_socket(addr, false, buf_size)?.into_udp_socket())?,
+ UdpSocket::from_std(new_socket(addr, reuse, buf_size)?.into_udp_socket())?,
BytesCodec::new(),
)))
}
@@ -168,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
}
}