net: fixed socket bind

This commit is contained in:
eric
2024-04-13 03:57:21 +08:00
parent 988b280a85
commit 4d7908229b

View File

@@ -8,7 +8,7 @@ use async_trait::async_trait;
use futures::future::select_ok;
use futures::stream::Stream;
use futures::TryFutureExt;
use socket2::SockRef;
use socket2::{Domain, SockRef, Socket, Type};
use thiserror::Error;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::net::{TcpSocket, TcpStream, UdpSocket};
@@ -302,13 +302,15 @@ async fn bind_socket<T: BindSocket>(socket: &T, indicator: &SocketAddr) -> io::R
// New UDP socket.
pub async fn new_udp_socket(indicator: &SocketAddr) -> io::Result<UdpSocket> {
use socket2::{Domain, Socket, Type};
let socket = match indicator {
SocketAddr::V4(..) => Socket::new(Domain::IPV4, Type::DGRAM, None)?,
SocketAddr::V6(..) => Socket::new(Domain::IPV6, Type::DGRAM, None)?,
};
socket.set_nonblocking(true)?;
bind_socket(&socket, indicator).await?;
#[cfg(target_os = "android")]
protect_socket(socket.as_raw_fd()).await?;