Pass can reuse to bind_socket

This commit is contained in:
Yuke Peng
2024-08-24 12:01:08 +08:00
committed by Tate, Hongliang Tian
parent dc4df47007
commit 434f143e30
5 changed files with 21 additions and 9 deletions

View File

@ -94,6 +94,7 @@ impl IfaceCommon {
let mut used_ports = self.used_ports.write(); let mut used_ports = self.used_ports.write();
if let Some(used_times) = used_ports.get_mut(&port) { if let Some(used_times) = used_ports.get_mut(&port) {
if *used_times == 0 || can_reuse { if *used_times == 0 || can_reuse {
// FIXME: Check if the previous socket was bound with SO_REUSEADDR.
*used_times += 1; *used_times += 1;
} else { } else {
return_errno_with_message!(Errno::EADDRINUSE, "the address is already in use"); return_errno_with_message!(Errno::EADDRINUSE, "the address is already in use");

View File

@ -56,7 +56,11 @@ enum Inner {
} }
impl Inner { impl Inner {
fn bind(self, endpoint: &IpEndpoint) -> core::result::Result<BoundDatagram, (Error, Self)> { fn bind(
self,
endpoint: &IpEndpoint,
can_reuse: bool,
) -> core::result::Result<BoundDatagram, (Error, Self)> {
let unbound_datagram = match self { let unbound_datagram = match self {
Inner::Unbound(unbound_datagram) => unbound_datagram, Inner::Unbound(unbound_datagram) => unbound_datagram,
Inner::Bound(bound_datagram) => { Inner::Bound(bound_datagram) => {
@ -67,7 +71,7 @@ impl Inner {
} }
}; };
let bound_datagram = match unbound_datagram.bind(endpoint) { let bound_datagram = match unbound_datagram.bind(endpoint, can_reuse) {
Ok(bound_datagram) => bound_datagram, Ok(bound_datagram) => bound_datagram,
Err((err, unbound_datagram)) => return Err((err, Inner::Unbound(unbound_datagram))), Err((err, unbound_datagram)) => return Err((err, Inner::Unbound(unbound_datagram))),
}; };
@ -83,7 +87,7 @@ impl Inner {
} }
let endpoint = get_ephemeral_endpoint(remote_endpoint); let endpoint = get_ephemeral_endpoint(remote_endpoint);
self.bind(&endpoint) self.bind(&endpoint, false)
} }
} }
@ -271,9 +275,10 @@ impl Socket for DatagramSocket {
fn bind(&self, socket_addr: SocketAddr) -> Result<()> { fn bind(&self, socket_addr: SocketAddr) -> Result<()> {
let endpoint = socket_addr.try_into()?; let endpoint = socket_addr.try_into()?;
let can_reuse = self.options.read().socket.reuse_addr();
let mut inner = self.inner.write(); let mut inner = self.inner.write();
inner.borrow_result(|owned_inner| { inner.borrow_result(|owned_inner| {
let bound_datagram = match owned_inner.bind(&endpoint) { let bound_datagram = match owned_inner.bind(&endpoint, can_reuse) {
Ok(bound_datagram) => bound_datagram, Ok(bound_datagram) => bound_datagram,
Err((err, err_inner)) => { Err((err, err_inner)) => {
return (err_inner, Err(err)); return (err_inner, Err(err));

View File

@ -24,8 +24,12 @@ impl UnboundDatagram {
} }
} }
pub fn bind(self, endpoint: &IpEndpoint) -> core::result::Result<BoundDatagram, (Error, Self)> { pub fn bind(
let bound_socket = match bind_socket(self.unbound_socket, endpoint, false) { self,
endpoint: &IpEndpoint,
can_reuse: bool,
) -> core::result::Result<BoundDatagram, (Error, Self)> {
let bound_socket = match bind_socket(self.unbound_socket, endpoint, can_reuse) {
Ok(bound_socket) => bound_socket, Ok(bound_socket) => bound_socket,
Err((err, unbound_socket)) => return Err((err, Self { unbound_socket })), Err((err, unbound_socket)) => return Err((err, Self { unbound_socket })),
}; };

View File

@ -30,6 +30,7 @@ impl InitStream {
pub fn bind( pub fn bind(
self, self,
endpoint: &IpEndpoint, endpoint: &IpEndpoint,
can_reuse: bool,
) -> core::result::Result<AnyBoundSocket, (Error, Self)> { ) -> core::result::Result<AnyBoundSocket, (Error, Self)> {
let unbound_socket = match self { let unbound_socket = match self {
InitStream::Unbound(unbound_socket) => unbound_socket, InitStream::Unbound(unbound_socket) => unbound_socket,
@ -40,7 +41,7 @@ impl InitStream {
)); ));
} }
}; };
let bound_socket = match bind_socket(unbound_socket, endpoint, false) { let bound_socket = match bind_socket(unbound_socket, endpoint, can_reuse) {
Ok(bound_socket) => bound_socket, Ok(bound_socket) => bound_socket,
Err((err, unbound_socket)) => return Err((err, InitStream::Unbound(unbound_socket))), Err((err, unbound_socket)) => return Err((err, InitStream::Unbound(unbound_socket))),
}; };
@ -52,7 +53,7 @@ impl InitStream {
remote_endpoint: &IpEndpoint, remote_endpoint: &IpEndpoint,
) -> core::result::Result<AnyBoundSocket, (Error, Self)> { ) -> core::result::Result<AnyBoundSocket, (Error, Self)> {
let endpoint = get_ephemeral_endpoint(remote_endpoint); let endpoint = get_ephemeral_endpoint(remote_endpoint);
self.bind(&endpoint) self.bind(&endpoint, false)
} }
pub fn connect( pub fn connect(

View File

@ -398,6 +398,7 @@ impl Socket for StreamSocket {
fn bind(&self, socket_addr: SocketAddr) -> Result<()> { fn bind(&self, socket_addr: SocketAddr) -> Result<()> {
let endpoint = socket_addr.try_into()?; let endpoint = socket_addr.try_into()?;
let can_reuse = self.options.read().socket.reuse_addr();
let mut state = self.state.write(); let mut state = self.state.write();
state.borrow_result(|owned_state| { state.borrow_result(|owned_state| {
@ -411,7 +412,7 @@ impl Socket for StreamSocket {
); );
}; };
let bound_socket = match init_stream.bind(&endpoint) { let bound_socket = match init_stream.bind(&endpoint, can_reuse) {
Ok(bound_socket) => bound_socket, Ok(bound_socket) => bound_socket,
Err((err, init_stream)) => { Err((err, init_stream)) => {
return (State::Init(init_stream), Err(err)); return (State::Init(init_stream), Err(err));