From 0e8106abfa75252e00e7c5afe4f4e6c2566aa47d Mon Sep 17 00:00:00 2001 From: Ruihan Li Date: Thu, 12 Jun 2025 23:54:43 +0800 Subject: [PATCH] Restrict visibility for TCP internals --- kernel/src/net/socket/ip/stream/connected.rs | 18 +++++++-------- kernel/src/net/socket/ip/stream/connecting.rs | 16 +++++++------- kernel/src/net/socket/ip/stream/init.rs | 22 +++++++++---------- kernel/src/net/socket/ip/stream/listen.rs | 10 ++++----- kernel/src/net/socket/ip/stream/util.rs | 18 +++++++-------- 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/kernel/src/net/socket/ip/stream/connected.rs b/kernel/src/net/socket/ip/stream/connected.rs index 3c5d13ace..b6de2ed67 100644 --- a/kernel/src/net/socket/ip/stream/connected.rs +++ b/kernel/src/net/socket/ip/stream/connected.rs @@ -18,7 +18,7 @@ use crate::{ util::{MultiRead, MultiWrite}, }; -pub struct ConnectedStream { +pub(super) struct ConnectedStream { tcp_conn: TcpConnection, remote_endpoint: IpEndpoint, /// Indicates whether this connection is "new" in a `connect()` system call. @@ -35,7 +35,7 @@ pub struct ConnectedStream { } impl ConnectedStream { - pub fn new( + pub(super) fn new( tcp_conn: TcpConnection, remote_endpoint: IpEndpoint, is_new_connection: bool, @@ -47,7 +47,7 @@ impl ConnectedStream { } } - pub fn shutdown(&self, cmd: SockShutdownCmd, pollee: &Pollee) -> Result<()> { + pub(super) fn shutdown(&self, cmd: SockShutdownCmd, pollee: &Pollee) -> Result<()> { let mut events = IoEvents::empty(); if cmd.shut_read() { @@ -69,7 +69,7 @@ impl ConnectedStream { Ok(()) } - pub fn try_recv( + pub(super) fn try_recv( &self, writer: &mut dyn MultiWrite, _flags: SendRecvFlags, @@ -102,7 +102,7 @@ impl ConnectedStream { } } - pub fn try_send( + pub(super) fn try_send( &self, reader: &mut dyn MultiRead, _flags: SendRecvFlags, @@ -133,19 +133,19 @@ impl ConnectedStream { } } - pub fn local_endpoint(&self) -> IpEndpoint { + pub(super) fn local_endpoint(&self) -> IpEndpoint { self.tcp_conn.local_endpoint().unwrap() } - pub fn remote_endpoint(&self) -> IpEndpoint { + pub(super) fn remote_endpoint(&self) -> IpEndpoint { self.remote_endpoint } - pub fn iface(&self) -> &Arc { + pub(super) fn iface(&self) -> &Arc { self.tcp_conn.iface() } - pub fn finish_last_connect(&mut self) -> Result<()> { + pub(super) fn finish_last_connect(&mut self) -> Result<()> { if !self.is_new_connection { return_errno_with_message!(Errno::EISCONN, "the socket is already connected"); } diff --git a/kernel/src/net/socket/ip/stream/connecting.rs b/kernel/src/net/socket/ip/stream/connecting.rs index 4e1bd7a93..b370b52bb 100644 --- a/kernel/src/net/socket/ip/stream/connecting.rs +++ b/kernel/src/net/socket/ip/stream/connecting.rs @@ -13,19 +13,19 @@ use crate::{ prelude::*, }; -pub struct ConnectingStream { +pub(super) struct ConnectingStream { tcp_conn: TcpConnection, remote_endpoint: IpEndpoint, } -pub enum ConnResult { +pub(super) enum ConnResult { Connecting(ConnectingStream), Connected(ConnectedStream), Refused(InitStream), } impl ConnectingStream { - pub fn new( + pub(super) fn new( bound_port: BoundPort, remote_endpoint: IpEndpoint, option: &RawTcpOption, @@ -64,7 +64,7 @@ impl ConnectingStream { }) } - pub fn has_result(&self) -> bool { + pub(super) fn has_result(&self) -> bool { match self.tcp_conn.connect_state() { ConnectState::Connecting => false, ConnectState::Connected => true, @@ -72,7 +72,7 @@ impl ConnectingStream { } } - pub fn into_result(self) -> ConnResult { + pub(super) fn into_result(self) -> ConnResult { let next_state = self.tcp_conn.connect_state(); match next_state { @@ -88,15 +88,15 @@ impl ConnectingStream { } } - pub fn local_endpoint(&self) -> IpEndpoint { + pub(super) fn local_endpoint(&self) -> IpEndpoint { self.tcp_conn.local_endpoint().unwrap() } - pub fn remote_endpoint(&self) -> IpEndpoint { + pub(super) fn remote_endpoint(&self) -> IpEndpoint { self.remote_endpoint } - pub fn iface(&self) -> &Arc { + pub(super) fn iface(&self) -> &Arc { self.tcp_conn.iface() } diff --git a/kernel/src/net/socket/ip/stream/init.rs b/kernel/src/net/socket/ip/stream/init.rs index f8964aef5..76fa65404 100644 --- a/kernel/src/net/socket/ip/stream/init.rs +++ b/kernel/src/net/socket/ip/stream/init.rs @@ -20,7 +20,7 @@ use crate::{ prelude::*, }; -pub struct InitStream { +pub(super) struct InitStream { bound_port: Option, /// Indicates if the last `connect()` is considered to be done. /// @@ -45,7 +45,7 @@ pub struct InitStream { } impl InitStream { - pub fn new() -> Self { + pub(super) fn new() -> Self { Self { bound_port: None, is_connect_done: true, @@ -53,7 +53,7 @@ impl InitStream { } } - pub fn new_bound(bound_port: BoundPort) -> Self { + pub(super) fn new_bound(bound_port: BoundPort) -> Self { Self { bound_port: Some(bound_port), is_connect_done: true, @@ -61,7 +61,7 @@ impl InitStream { } } - pub fn new_refused(bound_port: BoundPort) -> Self { + pub(super) fn new_refused(bound_port: BoundPort) -> Self { Self { bound_port: Some(bound_port), is_connect_done: false, @@ -69,7 +69,7 @@ impl InitStream { } } - pub fn bind(&mut self, endpoint: &IpEndpoint, can_reuse: bool) -> Result<()> { + pub(super) fn bind(&mut self, endpoint: &IpEndpoint, can_reuse: bool) -> Result<()> { if self.bound_port.is_some() { return_errno_with_message!(Errno::EINVAL, "the socket is already bound to an address"); } @@ -79,7 +79,7 @@ impl InitStream { Ok(()) } - pub fn connect( + pub(super) fn connect( self, remote_endpoint: &IpEndpoint, option: &RawTcpOption, @@ -111,7 +111,7 @@ impl InitStream { ) } - pub fn finish_last_connect(&mut self) -> Result<()> { + pub(super) fn finish_last_connect(&mut self) -> Result<()> { if self.is_connect_done { return Ok(()); } @@ -130,7 +130,7 @@ impl InitStream { } } - pub fn listen( + pub(super) fn listen( self, backlog: usize, option: &RawTcpOption, @@ -164,7 +164,7 @@ impl InitStream { } } - pub fn try_recv(&self) -> Result<(usize, SocketAddr)> { + pub(super) fn try_recv(&self) -> Result<(usize, SocketAddr)> { // FIXME: Linux does not return addresses for `recvfrom` on connection-oriented sockets. // This is a placeholder that has no Linux equivalent. (Note also that in this case // `getpeeraddr` will simply fail with `ENOTCONN`). @@ -183,7 +183,7 @@ impl InitStream { Ok((0, UNSPECIFIED_SOCKET_ADDR)) } - pub fn try_send(&self) -> Result { + pub(super) fn try_send(&self) -> Result { if let Some(err) = self.test_and_clear_error() { return Err(err); } @@ -191,7 +191,7 @@ impl InitStream { return_errno_with_message!(Errno::EPIPE, "the socket is not connected"); } - pub fn local_endpoint(&self) -> Option { + pub(super) fn local_endpoint(&self) -> Option { self.bound_port .as_ref() .map(|bound_port| bound_port.endpoint().unwrap()) diff --git a/kernel/src/net/socket/ip/stream/listen.rs b/kernel/src/net/socket/ip/stream/listen.rs index e2684854c..57a80422c 100644 --- a/kernel/src/net/socket/ip/stream/listen.rs +++ b/kernel/src/net/socket/ip/stream/listen.rs @@ -13,12 +13,12 @@ use crate::{ prelude::*, }; -pub struct ListenStream { +pub(super) struct ListenStream { tcp_listener: TcpListener, } impl ListenStream { - pub fn new( + pub(super) fn new( bound_port: BoundPort, backlog: usize, option: &RawTcpOption, @@ -39,7 +39,7 @@ impl ListenStream { } } - pub fn try_accept(&self) -> Result { + pub(super) fn try_accept(&self) -> Result { let (new_conn, remote_endpoint) = self.tcp_listener.accept().ok_or_else(|| { Error::with_message(Errno::EAGAIN, "no pending connection is available") })?; @@ -47,11 +47,11 @@ impl ListenStream { Ok(ConnectedStream::new(new_conn, remote_endpoint, false)) } - pub fn local_endpoint(&self) -> IpEndpoint { + pub(super) fn local_endpoint(&self) -> IpEndpoint { self.tcp_listener.local_endpoint().unwrap() } - pub fn iface(&self) -> &Arc { + pub(super) fn iface(&self) -> &Arc { self.tcp_listener.iface() } diff --git a/kernel/src/net/socket/ip/stream/util.rs b/kernel/src/net/socket/ip/stream/util.rs index e98d84a68..e2764cdb5 100644 --- a/kernel/src/net/socket/ip/stream/util.rs +++ b/kernel/src/net/socket/ip/stream/util.rs @@ -8,7 +8,7 @@ use crate::prelude::*; #[derive(Debug, Clone, Copy, CopyGetters, Setters)] #[get_copy = "pub"] #[set = "pub"] -pub struct TcpOptionSet { +pub(super) struct TcpOptionSet { no_delay: bool, maxseg: u32, keep_idle: u32, @@ -20,13 +20,13 @@ pub struct TcpOptionSet { receive_inq: bool, } -pub const DEFAULT_MAXSEG: u32 = 536; -pub const DEFAULT_KEEP_IDLE: u32 = 7200; -pub const DEFAULT_SYN_CNT: u8 = 6; -pub const DEFAULT_WINDOW_CLAMP: u32 = 0x8000_0000; +pub(super) const DEFAULT_MAXSEG: u32 = 536; +pub(super) const DEFAULT_KEEP_IDLE: u32 = 7200; +pub(super) const DEFAULT_SYN_CNT: u8 = 6; +pub(super) const DEFAULT_WINDOW_CLAMP: u32 = 0x8000_0000; impl TcpOptionSet { - pub fn new() -> Self { + pub(super) fn new() -> Self { Self { no_delay: false, maxseg: DEFAULT_MAXSEG, @@ -53,11 +53,11 @@ const TCP_RTO_MAX: Duration = Duration::from_secs(120); /// The number of retransmits. #[derive(Debug, Clone, Copy)] -pub struct Retrans(u8); +pub(super) struct Retrans(u8); impl Retrans { /// Converts seconds to retransmits. - pub const fn from_secs(seconds: u32) -> Self { + pub(super) const fn from_secs(seconds: u32) -> Self { if seconds == 0 { return Self(0); } @@ -80,7 +80,7 @@ impl Retrans { } /// Converts retransmits to seconds. - pub const fn to_secs(self) -> u32 { + pub(super) const fn to_secs(self) -> u32 { let mut retrans = self.0; if retrans == 0 {