From 6b50d28ba1aca847b47e15c81713e32c2ff371da Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Fri, 26 Jul 2024 22:47:42 +0800 Subject: [PATCH] Remove unnecessary self-connecting check --- kernel/aster-nix/src/net/socket/unix/addr.rs | 12 +--------- .../src/net/socket/unix/stream/init.rs | 12 +++------- .../src/net/socket/unix/stream/socket.rs | 13 +++++++++- test/apps/network/unix_err.c | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/kernel/aster-nix/src/net/socket/unix/addr.rs b/kernel/aster-nix/src/net/socket/unix/addr.rs index 5c67c767f..778a62532 100644 --- a/kernel/aster-nix/src/net/socket/unix/addr.rs +++ b/kernel/aster-nix/src/net/socket/unix/addr.rs @@ -9,22 +9,12 @@ pub enum UnixSocketAddr { Abstract(Vec), } -#[derive(Clone)] +#[derive(Clone, Debug)] pub(super) enum UnixSocketAddrBound { Path(Arc), Abstract(Vec), } -impl PartialEq for UnixSocketAddrBound { - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Abstract(l0), Self::Abstract(r0)) => l0 == r0, - (Self::Path(l0), Self::Path(r0)) => Arc::ptr_eq(l0.inode(), r0.inode()), - _ => false, - } - } -} - impl TryFrom for UnixSocketAddr { type Error = Error; 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 ac9d83c4a..68567f83e 100644 --- a/kernel/aster-nix/src/net/socket/unix/stream/init.rs +++ b/kernel/aster-nix/src/net/socket/unix/stream/init.rs @@ -45,17 +45,11 @@ impl Init { } pub(super) fn connect(&self, remote_addr: &UnixSocketAddrBound) -> Result { - let addr = self.addr(); - - if let Some(addr) = addr { - if *addr == *remote_addr { - return_errno_with_message!(Errno::EINVAL, "try to connect to self is invalid"); - } - } - - let (this_end, remote_end) = Endpoint::new_pair(addr.cloned(), Some(remote_addr.clone())); + 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)) } diff --git a/kernel/aster-nix/src/net/socket/unix/stream/socket.rs b/kernel/aster-nix/src/net/socket/unix/stream/socket.rs index 930933bf3..db69f4c19 100644 --- a/kernel/aster-nix/src/net/socket/unix/stream/socket.rs +++ b/kernel/aster-nix/src/net/socket/unix/stream/socket.rs @@ -222,9 +222,20 @@ impl Socket for UnixStreamSocket { } }; + // Note that the Linux kernel implementation locks the remote socket and checks to see if + // it is listening first. This is different from our implementation, which locks the local + // socket and checks the state of the local socket first. + // + // The difference may result in different error codes, but it's doubtful that this will + // ever lead to real problems. + // + // See also . + let connected = match &*self.state.read() { State::Init(init) => init.connect(&remote_addr)?, - State::Listen(_) => return_errno_with_message!(Errno::EINVAL, "the socket is listened"), + State::Listen(_) => { + return_errno_with_message!(Errno::EINVAL, "the socket is listening") + } State::Connected(_) => { return_errno_with_message!(Errno::EISCONN, "the socket is connected") } diff --git a/test/apps/network/unix_err.c b/test/apps/network/unix_err.c index ac4872794..63c0efdcd 100644 --- a/test/apps/network/unix_err.c +++ b/test/apps/network/unix_err.c @@ -194,3 +194,27 @@ FN_TEST(getpeername) memcmp(&addr, &UNNAMED_ADDR, UNNAMED_ADDRLEN) == 0); } END_TEST() + +FN_TEST(connect) +{ + TEST_ERRNO(connect(sk_unbound, (struct sockaddr *)&BOUND_ADDR, + BOUND_ADDRLEN), + ECONNREFUSED); + + TEST_ERRNO(connect(sk_bound, (struct sockaddr *)&BOUND_ADDR, + BOUND_ADDRLEN), + ECONNREFUSED); + + TEST_ERRNO(connect(sk_listen, (struct sockaddr *)&LISTEN_ADDR, + LISTEN_ADDRLEN), + EINVAL); + + TEST_ERRNO(connect(sk_connected, (struct sockaddr *)&LISTEN_ADDR, + LISTEN_ADDRLEN), + EISCONN); + + TEST_ERRNO(connect(sk_connected, (struct sockaddr *)&LISTEN_ADDR, + LISTEN_ADDRLEN), + EISCONN); +} +END_TEST()