Restrict visibility for TCP internals

This commit is contained in:
Ruihan Li
2025-06-12 23:54:43 +08:00
committed by Jianfeng Jiang
parent 639bc91e31
commit 0e8106abfa
5 changed files with 42 additions and 42 deletions

View File

@ -18,7 +18,7 @@ use crate::{
util::{MultiRead, MultiWrite}, util::{MultiRead, MultiWrite},
}; };
pub struct ConnectedStream { pub(super) struct ConnectedStream {
tcp_conn: TcpConnection, tcp_conn: TcpConnection,
remote_endpoint: IpEndpoint, remote_endpoint: IpEndpoint,
/// Indicates whether this connection is "new" in a `connect()` system call. /// Indicates whether this connection is "new" in a `connect()` system call.
@ -35,7 +35,7 @@ pub struct ConnectedStream {
} }
impl ConnectedStream { impl ConnectedStream {
pub fn new( pub(super) fn new(
tcp_conn: TcpConnection, tcp_conn: TcpConnection,
remote_endpoint: IpEndpoint, remote_endpoint: IpEndpoint,
is_new_connection: bool, 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(); let mut events = IoEvents::empty();
if cmd.shut_read() { if cmd.shut_read() {
@ -69,7 +69,7 @@ impl ConnectedStream {
Ok(()) Ok(())
} }
pub fn try_recv( pub(super) fn try_recv(
&self, &self,
writer: &mut dyn MultiWrite, writer: &mut dyn MultiWrite,
_flags: SendRecvFlags, _flags: SendRecvFlags,
@ -102,7 +102,7 @@ impl ConnectedStream {
} }
} }
pub fn try_send( pub(super) fn try_send(
&self, &self,
reader: &mut dyn MultiRead, reader: &mut dyn MultiRead,
_flags: SendRecvFlags, _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() self.tcp_conn.local_endpoint().unwrap()
} }
pub fn remote_endpoint(&self) -> IpEndpoint { pub(super) fn remote_endpoint(&self) -> IpEndpoint {
self.remote_endpoint self.remote_endpoint
} }
pub fn iface(&self) -> &Arc<Iface> { pub(super) fn iface(&self) -> &Arc<Iface> {
self.tcp_conn.iface() 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 { if !self.is_new_connection {
return_errno_with_message!(Errno::EISCONN, "the socket is already connected"); return_errno_with_message!(Errno::EISCONN, "the socket is already connected");
} }

View File

@ -13,19 +13,19 @@ use crate::{
prelude::*, prelude::*,
}; };
pub struct ConnectingStream { pub(super) struct ConnectingStream {
tcp_conn: TcpConnection, tcp_conn: TcpConnection,
remote_endpoint: IpEndpoint, remote_endpoint: IpEndpoint,
} }
pub enum ConnResult { pub(super) enum ConnResult {
Connecting(ConnectingStream), Connecting(ConnectingStream),
Connected(ConnectedStream), Connected(ConnectedStream),
Refused(InitStream), Refused(InitStream),
} }
impl ConnectingStream { impl ConnectingStream {
pub fn new( pub(super) fn new(
bound_port: BoundPort, bound_port: BoundPort,
remote_endpoint: IpEndpoint, remote_endpoint: IpEndpoint,
option: &RawTcpOption, 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() { match self.tcp_conn.connect_state() {
ConnectState::Connecting => false, ConnectState::Connecting => false,
ConnectState::Connected => true, 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(); let next_state = self.tcp_conn.connect_state();
match next_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() self.tcp_conn.local_endpoint().unwrap()
} }
pub fn remote_endpoint(&self) -> IpEndpoint { pub(super) fn remote_endpoint(&self) -> IpEndpoint {
self.remote_endpoint self.remote_endpoint
} }
pub fn iface(&self) -> &Arc<Iface> { pub(super) fn iface(&self) -> &Arc<Iface> {
self.tcp_conn.iface() self.tcp_conn.iface()
} }

View File

@ -20,7 +20,7 @@ use crate::{
prelude::*, prelude::*,
}; };
pub struct InitStream { pub(super) struct InitStream {
bound_port: Option<BoundPort>, bound_port: Option<BoundPort>,
/// Indicates if the last `connect()` is considered to be done. /// Indicates if the last `connect()` is considered to be done.
/// ///
@ -45,7 +45,7 @@ pub struct InitStream {
} }
impl InitStream { impl InitStream {
pub fn new() -> Self { pub(super) fn new() -> Self {
Self { Self {
bound_port: None, bound_port: None,
is_connect_done: true, 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 { Self {
bound_port: Some(bound_port), bound_port: Some(bound_port),
is_connect_done: true, 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 { Self {
bound_port: Some(bound_port), bound_port: Some(bound_port),
is_connect_done: false, 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() { if self.bound_port.is_some() {
return_errno_with_message!(Errno::EINVAL, "the socket is already bound to an address"); return_errno_with_message!(Errno::EINVAL, "the socket is already bound to an address");
} }
@ -79,7 +79,7 @@ impl InitStream {
Ok(()) Ok(())
} }
pub fn connect( pub(super) fn connect(
self, self,
remote_endpoint: &IpEndpoint, remote_endpoint: &IpEndpoint,
option: &RawTcpOption, 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 { if self.is_connect_done {
return Ok(()); return Ok(());
} }
@ -130,7 +130,7 @@ impl InitStream {
} }
} }
pub fn listen( pub(super) fn listen(
self, self,
backlog: usize, backlog: usize,
option: &RawTcpOption, 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. // 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 // This is a placeholder that has no Linux equivalent. (Note also that in this case
// `getpeeraddr` will simply fail with `ENOTCONN`). // `getpeeraddr` will simply fail with `ENOTCONN`).
@ -183,7 +183,7 @@ impl InitStream {
Ok((0, UNSPECIFIED_SOCKET_ADDR)) Ok((0, UNSPECIFIED_SOCKET_ADDR))
} }
pub fn try_send(&self) -> Result<usize> { pub(super) fn try_send(&self) -> Result<usize> {
if let Some(err) = self.test_and_clear_error() { if let Some(err) = self.test_and_clear_error() {
return Err(err); return Err(err);
} }
@ -191,7 +191,7 @@ impl InitStream {
return_errno_with_message!(Errno::EPIPE, "the socket is not connected"); return_errno_with_message!(Errno::EPIPE, "the socket is not connected");
} }
pub fn local_endpoint(&self) -> Option<IpEndpoint> { pub(super) fn local_endpoint(&self) -> Option<IpEndpoint> {
self.bound_port self.bound_port
.as_ref() .as_ref()
.map(|bound_port| bound_port.endpoint().unwrap()) .map(|bound_port| bound_port.endpoint().unwrap())

View File

@ -13,12 +13,12 @@ use crate::{
prelude::*, prelude::*,
}; };
pub struct ListenStream { pub(super) struct ListenStream {
tcp_listener: TcpListener, tcp_listener: TcpListener,
} }
impl ListenStream { impl ListenStream {
pub fn new( pub(super) fn new(
bound_port: BoundPort, bound_port: BoundPort,
backlog: usize, backlog: usize,
option: &RawTcpOption, option: &RawTcpOption,
@ -39,7 +39,7 @@ impl ListenStream {
} }
} }
pub fn try_accept(&self) -> Result<ConnectedStream> { pub(super) fn try_accept(&self) -> Result<ConnectedStream> {
let (new_conn, remote_endpoint) = self.tcp_listener.accept().ok_or_else(|| { let (new_conn, remote_endpoint) = self.tcp_listener.accept().ok_or_else(|| {
Error::with_message(Errno::EAGAIN, "no pending connection is available") Error::with_message(Errno::EAGAIN, "no pending connection is available")
})?; })?;
@ -47,11 +47,11 @@ impl ListenStream {
Ok(ConnectedStream::new(new_conn, remote_endpoint, false)) 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() self.tcp_listener.local_endpoint().unwrap()
} }
pub fn iface(&self) -> &Arc<Iface> { pub(super) fn iface(&self) -> &Arc<Iface> {
self.tcp_listener.iface() self.tcp_listener.iface()
} }

View File

@ -8,7 +8,7 @@ use crate::prelude::*;
#[derive(Debug, Clone, Copy, CopyGetters, Setters)] #[derive(Debug, Clone, Copy, CopyGetters, Setters)]
#[get_copy = "pub"] #[get_copy = "pub"]
#[set = "pub"] #[set = "pub"]
pub struct TcpOptionSet { pub(super) struct TcpOptionSet {
no_delay: bool, no_delay: bool,
maxseg: u32, maxseg: u32,
keep_idle: u32, keep_idle: u32,
@ -20,13 +20,13 @@ pub struct TcpOptionSet {
receive_inq: bool, receive_inq: bool,
} }
pub const DEFAULT_MAXSEG: u32 = 536; pub(super) const DEFAULT_MAXSEG: u32 = 536;
pub const DEFAULT_KEEP_IDLE: u32 = 7200; pub(super) const DEFAULT_KEEP_IDLE: u32 = 7200;
pub const DEFAULT_SYN_CNT: u8 = 6; pub(super) const DEFAULT_SYN_CNT: u8 = 6;
pub const DEFAULT_WINDOW_CLAMP: u32 = 0x8000_0000; pub(super) const DEFAULT_WINDOW_CLAMP: u32 = 0x8000_0000;
impl TcpOptionSet { impl TcpOptionSet {
pub fn new() -> Self { pub(super) fn new() -> Self {
Self { Self {
no_delay: false, no_delay: false,
maxseg: DEFAULT_MAXSEG, maxseg: DEFAULT_MAXSEG,
@ -53,11 +53,11 @@ const TCP_RTO_MAX: Duration = Duration::from_secs(120);
/// The number of retransmits. /// The number of retransmits.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Retrans(u8); pub(super) struct Retrans(u8);
impl Retrans { impl Retrans {
/// Converts seconds to retransmits. /// Converts seconds to retransmits.
pub const fn from_secs(seconds: u32) -> Self { pub(super) const fn from_secs(seconds: u32) -> Self {
if seconds == 0 { if seconds == 0 {
return Self(0); return Self(0);
} }
@ -80,7 +80,7 @@ impl Retrans {
} }
/// Converts retransmits to seconds. /// Converts retransmits to seconds.
pub const fn to_secs(self) -> u32 { pub(super) const fn to_secs(self) -> u32 {
let mut retrans = self.0; let mut retrans = self.0;
if retrans == 0 { if retrans == 0 {