From 47857fc10fbfbee4a1ab69f48f31da884af811a0 Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Sat, 27 Jul 2024 09:39:54 +0800 Subject: [PATCH] Fix remote addresses in connected UNIX sockets --- .../src/net/socket/unix/stream/init.rs | 10 ++---- .../src/net/socket/unix/stream/listener.rs | 34 ++++++++++++++----- test/apps/network/unix_err.c | 19 ++++++----- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/kernel/aster-nix/src/net/socket/unix/stream/init.rs b/kernel/aster-nix/src/net/socket/unix/stream/init.rs index 2284e27a..75c8b68a 100644 --- a/kernel/aster-nix/src/net/socket/unix/stream/init.rs +++ b/kernel/aster-nix/src/net/socket/unix/stream/init.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MPL-2.0 -use super::{connected::Connected, endpoint::Endpoint, listener::push_incoming}; +use super::{connected::Connected, listener::push_incoming}; use crate::{ events::{IoEvents, Observer}, fs::{ @@ -45,12 +45,8 @@ impl Init { } pub(super) fn connect(&self, remote_addr: &UnixSocketAddrBound) -> Result { - let (this_end, remote_end) = - Endpoint::new_pair(self.addr.clone(), Some(remote_addr.clone())); - - push_incoming(remote_addr, remote_end)?; - - Ok(Connected::new(this_end)) + let endpoint = push_incoming(remote_addr, self.addr.clone())?; + Ok(Connected::new(endpoint)) } pub(super) fn addr(&self) -> Option<&UnixSocketAddrBound> { diff --git a/kernel/aster-nix/src/net/socket/unix/stream/listener.rs b/kernel/aster-nix/src/net/socket/unix/stream/listener.rs index 727dc1bd..2d217acf 100644 --- a/kernel/aster-nix/src/net/socket/unix/stream/listener.rs +++ b/kernel/aster-nix/src/net/socket/unix/stream/listener.rs @@ -118,15 +118,19 @@ impl BacklogTable { .ok_or_else(|| Error::with_message(Errno::EINVAL, "the socket is not listened")) } - fn push_incoming(&self, addr: &UnixSocketAddrBound, endpoint: Endpoint) -> Result<()> { - let backlog = self.get_backlog(addr).map_err(|_| { + fn push_incoming( + &self, + server_addr: &UnixSocketAddrBound, + client_addr: Option, + ) -> Result { + let backlog = self.get_backlog(server_addr).map_err(|_| { Error::with_message( Errno::ECONNREFUSED, "no socket is listened at the remote address", ) })?; - backlog.push_incoming(endpoint) + backlog.push_incoming(client_addr) } fn remove_backlog(&self, addr: &UnixSocketAddrBound) { @@ -160,14 +164,23 @@ impl Backlog { &self.addr } - fn push_incoming(&self, endpoint: Endpoint) -> Result<()> { + fn push_incoming(&self, client_addr: Option) -> Result { let mut endpoints = self.incoming_endpoints.lock(); + if endpoints.len() >= self.backlog.load(Ordering::Relaxed) { - return_errno_with_message!(Errno::ECONNREFUSED, "incoming_endpoints is full"); + return_errno_with_message!( + Errno::ECONNREFUSED, + "the pending connection queue on the listening socket is full" + ); } - endpoints.push_back(endpoint); + + let (server_endpoint, client_endpoint) = + Endpoint::new_pair(Some(self.addr.clone()), client_addr); + endpoints.push_back(server_endpoint); + self.pollee.add_events(IoEvents::IN); - Ok(()) + + Ok(client_endpoint) } fn pop_incoming(&self) -> Result { @@ -216,6 +229,9 @@ fn unregister_backlog(addr: &UnixSocketAddrBound) { BACKLOG_TABLE.remove_backlog(addr); } -pub(super) fn push_incoming(remote_addr: &UnixSocketAddrBound, remote_end: Endpoint) -> Result<()> { - BACKLOG_TABLE.push_incoming(remote_addr, remote_end) +pub(super) fn push_incoming( + server_addr: &UnixSocketAddrBound, + client_addr: Option, +) -> Result { + BACKLOG_TABLE.push_incoming(server_addr, client_addr) } diff --git a/test/apps/network/unix_err.c b/test/apps/network/unix_err.c index 00aa4288..e6df2abd 100644 --- a/test/apps/network/unix_err.c +++ b/test/apps/network/unix_err.c @@ -83,18 +83,21 @@ static int sk_listen; static int sk_connected; static int sk_accepted; -#define UNNAMED_ADDR \ - ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "" }) +#define UNIX_ADDR(path) \ + ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = path }) + +#define UNNAMED_ADDR UNIX_ADDR("") #define UNNAMED_ADDRLEN PATH_OFFSET -#define BOUND_ADDR \ - ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "//tmp/B0" }) +#define BOUND_ADDR UNIX_ADDR("//tmp/B0") #define BOUND_ADDRLEN (PATH_OFFSET + 9) -#define LISTEN_ADDR \ - ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp//L0" }) +#define LISTEN_ADDR UNIX_ADDR("/tmp//L0") #define LISTEN_ADDRLEN (PATH_OFFSET + 9) +#define LISTEN_ADDR2 UNIX_ADDR("/tmp/L0") +#define LISTEN_ADDRLEN2 (PATH_OFFSET + 8) + FN_SETUP(unbound) { sk_unbound = CHECK(socket(PF_UNIX, SOCK_STREAM, 0)); @@ -123,8 +126,8 @@ FN_SETUP(connected) { sk_connected = CHECK(socket(PF_UNIX, SOCK_STREAM, 0)); - CHECK(connect(sk_connected, (struct sockaddr *)&LISTEN_ADDR, - LISTEN_ADDRLEN)); + CHECK(connect(sk_connected, (struct sockaddr *)&LISTEN_ADDR2, + LISTEN_ADDRLEN2)); } END_SETUP()