TcpSocket

Struct TcpSocket 

Source
pub struct TcpSocket {
Show 35 fields state: Mutex<TcpState>, local_ip: Mutex<Option<Ipv4Address>>, pub(crate) local_port: AtomicU16, remote_ip: Mutex<Option<Ipv4Address>>, remote_port: AtomicU16, send_seq: AtomicU32, send_unacked: AtomicU32, recv_seq: AtomicU32, recv_ack: AtomicU32, send_window: AtomicU16, recv_window: AtomicU16, send_buffer: Mutex<VecDeque<u8>>, recv_buffer: Mutex<VecDeque<u8>>, tcp_layer: Weak<TcpLayer>, self_weak: Weak<TcpSocket>, pending_accept: Mutex<VecDeque<Arc<TcpSocket>>>, max_backlog: AtomicUsize, bytes_sent: AtomicU64, bytes_received: AtomicU64, srtt: AtomicU32, rttvar: AtomicU32, rto: AtomicU32, retrans_count: AtomicU16, retrans_timer_id: Mutex<Option<u64>>, last_send_time: AtomicU64, timing_rtt: AtomicU16, timed_seq: AtomicU32, unacked_segments: Mutex<VecDeque<UnackedSegment>>, out_of_order: Mutex<BTreeMap<u32, OutOfOrderSegment>>, accept_waker: Mutex<Option<Arc<Waker>>>, recv_waker: Mutex<Option<Arc<Waker>>>, send_waker: Mutex<Option<Arc<Waker>>>, blocking_mode: AtomicBool, dup_ack_count: AtomicU16, last_ack_seq: AtomicU32,
}
Expand description

TCP socket (full implementation)

Fields§

§state: Mutex<TcpState>

TCP connection state

§local_ip: Mutex<Option<Ipv4Address>>

Local IP address

§local_port: AtomicU16

Local port

§remote_ip: Mutex<Option<Ipv4Address>>

Remote IP address

§remote_port: AtomicU16

Remote port

§send_seq: AtomicU32

Sequence numbers

§send_unacked: AtomicU32§recv_seq: AtomicU32§recv_ack: AtomicU32§send_window: AtomicU16

Window size

§recv_window: AtomicU16§send_buffer: Mutex<VecDeque<u8>>

Data buffers

§recv_buffer: Mutex<VecDeque<u8>>§tcp_layer: Weak<TcpLayer>

Reference to TCP layer

§self_weak: Weak<TcpSocket>

Weak self reference for registration

§pending_accept: Mutex<VecDeque<Arc<TcpSocket>>>

Pending accepted connections (listener only)

§max_backlog: AtomicUsize

Maximum backlog size (from listen())

§bytes_sent: AtomicU64

Statistics

§bytes_received: AtomicU64§srtt: AtomicU32

RTO (Retransmission Timeout) calculation - RFC 6298 Smoothed RTT (8 * srtt for fixed-point arithmetic)

§rttvar: AtomicU32

RTT variation (4 * rttvar for fixed-point arithmetic)

§rto: AtomicU32

Current RTO in ticks (initial: 1 second = 100 ticks @ 10ms)

§retrans_count: AtomicU16

Retransmission count for exponential backoff

§retrans_timer_id: Mutex<Option<u64>>

Timer ID for retransmission timer

§last_send_time: AtomicU64

Timestamp of last segment transmission (for RTT measurement)

§timing_rtt: AtomicU16

Whether we’re timing an RTT measurement (Karn’s algorithm)

§timed_seq: AtomicU32

Sequence number being timed

§unacked_segments: Mutex<VecDeque<UnackedSegment>>

List of unacknowledged segments for retransmission

§out_of_order: Mutex<BTreeMap<u32, OutOfOrderSegment>>

Out-of-order segments for reassembly (sorted by sequence number)

§accept_waker: Mutex<Option<Arc<Waker>>>

Waker for blocking accept() operations

§recv_waker: Mutex<Option<Arc<Waker>>>

Waker for blocking recv() operations

§send_waker: Mutex<Option<Arc<Waker>>>

Waker for blocking send() operations

§blocking_mode: AtomicBool

Block mode: true for blocking, false for non-blocking

§dup_ack_count: AtomicU16

Duplicate ACK count for Fast Retransmit

§last_ack_seq: AtomicU32

Last ACK sequence number for detecting duplicates

Implementations§

Source§

impl TcpSocket

Source

pub fn from_socket_object(socket: &Arc<dyn SocketObject>) -> Option<&Self>

Safely downcast a SocketObject to TcpSocket using Any trait

Returns None if socket is not a TcpSocket. This is completely safe and does not use any unsafe code.

Source

pub fn accept_blocking( &self, task_id: usize, trapframe: &mut Trapframe, ) -> Result<Arc<dyn SocketObject>, SocketError>

Blocking accept - waits for a connection

Source

pub fn new(tcp_layer: Weak<TcpLayer>) -> Arc<Self>

Create a new TCP socket

Source

fn matches_peer(&self, src_ip: Ipv4Address, src_port: u16) -> bool

Source

fn ensure_local_ip(&self)

Source

fn register_local_port(&self, port: u16) -> Result<(), SocketError>

Source

fn allocate_ephemeral_port(&self) -> u16

Source

pub fn get_state(&self) -> TcpState

Get current TCP state

Source

pub fn set_state(&self, new_state: TcpState)

Set TCP state

Source

pub fn process_segment( &self, src_ip: Ipv4Address, header: TcpHeader, data: &[u8], )

Process incoming TCP segment

Source

fn handle_syn_received(&self, src_ip: Ipv4Address, header: TcpHeader)

Handle incoming SYN (SYN-RECEIVED state)

Source

fn handle_syn_ack_received(&self, src_ip: Ipv4Address, header: TcpHeader)

Handle received SYN-ACK (move to ESTABLISHED)

Source

fn handle_rst(&self)

Handle RST (Reset) - properly cleanup connection

Source

fn handle_control_segment(&self, src_ip: Ipv4Address, header: TcpHeader)

Handle control segment (ACK, FIN, RST)

Source

fn handle_data_segment( &self, src_ip: Ipv4Address, header: TcpHeader, data: &[u8], )

Handle data segment

Source

fn handle_fin(&self, src_ip: Ipv4Address, header: TcpHeader)

Handle FIN segment

Source

fn update_send_window(&self, ack_number: u32)

Update send window based on acknowledgment

Source

fn fast_retransmit(&self)

Fast Retransmit - immediately retransmit unacknowledged segments

Source

fn send_syn(&self, dest_ip: Ipv4Address, dest_port: u16)

Send SYN packet

Source

fn send_syn_ack( &self, dest_ip: Ipv4Address, dest_port: u16, their_seq: u32, ack_seq: u32, )

Send SYN-ACK packet

Source

fn send_ack(&self, dest_ip: Ipv4Address, dest_port: u16, ack_seq: u32)

Send ACK packet

Source

fn send_fin(&self)

Send FIN packet

Source

fn send_fin_ack(&self)

Send FIN-ACK packet

Source

fn send_segment( &self, dest_ip: Ipv4Address, header: TcpHeader, data: &[u8], update_seq: bool, is_retransmit: bool, )

Send TCP segment through IP layer

Source

pub fn send_data(&self, data: &[u8]) -> Result<usize, SocketError>

Send data through socket

Source

pub fn send_blocking( &self, data: &[u8], task_id: usize, trapframe: &mut Trapframe, ) -> Result<usize, SocketError>

Blocking send - waits for buffer space

Source

pub fn recv_data(&self, buffer: &mut [u8]) -> Result<usize, SocketError>

Receive data from socket

Source

pub fn recv_blocking( &self, buffer: &mut [u8], task_id: usize, trapframe: &mut Trapframe, ) -> Result<usize, SocketError>

Blocking receive - waits for data to be available

Source

fn update_rto(&self, rtt_ticks: u32)

Update RTO based on RTT measurement (Jacobson/Karels algorithm) Uses fixed-point arithmetic for better precision in no_std

Source

fn get_rto_ms(&self) -> u32

Get current RTO in milliseconds

Source

fn get_rto_ticks(&self) -> u32

Get current RTO in ticks

Source

fn start_rtt_measurement(&self, seq: u32)

Start RTT measurement for a sequence number

Source

fn stop_rtt_measurement(&self, ack_seq: u32)

Stop RTT measurement when ACK is received

Source

fn backoff_rto(&self)

Exponential backoff for retransmission

Source

fn max_retransmissions_exceeded(&self) -> bool

Check if maximum retransmissions exceeded

Source

fn handle_retrans_timeout(&self, seq: u32)

Handle retransmission timeout

Source

fn schedule_retrans_timer(&self, seq: u32)

Schedule retransmission timer for a segment

Source

fn cancel_retrans_timer(&self)

Cancel retransmission timer

Source

fn add_unacked_segment(&self, seq: u32, data: Vec<u8>, flags: u8)

Add segment to unacked list and schedule retransmission

Source

fn remove_acked_segments(&self, ack_seq: u32)

Remove acknowledged segments from unacked list

Trait Implementations§

Source§

impl CloneOps for TcpSocket

Source§

fn custom_clone(&self) -> KernelObject

Perform a custom clone operation and return the cloned object Read more
Source§

impl ControlOps for TcpSocket

Source§

fn control(&self, command: u32, arg: usize) -> Result<i32, &'static str>

Perform a control operation Read more
Source§

fn supported_control_commands(&self) -> Vec<(u32, &'static str)>

Get a list of supported control commands Read more
Source§

impl Drop for TcpSocket

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Selectable for TcpSocket

Source§

fn current_ready(&self, interest: ReadyInterest) -> ReadySet

Return current readiness for the given interest set.
Source§

fn wait_until_ready( &self, interest: ReadyInterest, trapframe: &mut Trapframe, timeout_ticks: Option<u64>, ) -> SelectWaitOutcome

Block the current task using the provided trapframe until the interest becomes ready or the optional timeout (in ticks) expires. Read more
Source§

fn set_nonblocking(&self, enabled: bool)

Enable or disable non-blocking I/O semantics on this object. Read more
Source§

fn is_nonblocking(&self) -> bool

Query whether non-blocking I/O semantics are enabled on this object.
Source§

impl SocketControl for TcpSocket

Source§

fn bind(&self, address: &SocketAddress) -> Result<(), SocketError>

Bind socket to an address
Source§

fn listen(&self, backlog: usize) -> Result<(), SocketError>

Listen for incoming connections (for stream sockets)
Source§

fn connect(&self, address: &SocketAddress) -> Result<(), SocketError>

Connect to a remote address
Source§

fn accept(&self) -> Result<Arc<dyn SocketObject>, SocketError>

Accept an incoming connection (for listening sockets) Returns a new socket for the accepted connection
Source§

fn getpeername(&self) -> Result<SocketAddress, SocketError>

Get socket peer address
Source§

fn getsockname(&self) -> Result<SocketAddress, SocketError>

Get socket local address
Source§

fn shutdown(&self, how: ShutdownHow) -> Result<(), SocketError>

Shutdown socket for reading, writing, or both
Source§

fn is_connected(&self) -> bool

Check if socket is connected
Source§

fn state(&self) -> SocketState

Get socket state
Source§

impl SocketObject for TcpSocket

Source§

fn socket_type(&self) -> SocketType

Get socket type (Stream, Datagram, etc.)
Source§

fn socket_domain(&self) -> SocketDomain

Get socket domain (Local, Inet, Inet6, etc.)
Source§

fn socket_protocol(&self) -> SocketProtocol

Get socket protocol
Source§

fn as_any(&self) -> &dyn Any

Cast to Any for safe downcasting
Source§

fn as_selectable(&self) -> Option<&dyn Selectable>

Optional capability: expose select/pselect readiness/wait interface
Source§

fn as_control_ops(&self) -> Option<&dyn ControlOps>

Optional capability: expose control operations interface
Source§

fn sendto( &self, data: &[u8], address: &SocketAddress, flags: u32, ) -> Result<usize, SocketError>

Send data to a specific address (for datagram sockets) For stream sockets, address is ignored and data is sent to connected peer
Source§

fn recvfrom( &self, buffer: &mut [u8], flags: u32, ) -> Result<(usize, SocketAddress), SocketError>

Receive data with source address (for datagram sockets) For stream sockets, returns Unspecified address
Source§

impl StreamIpcOps for TcpSocket

Source§

fn is_connected(&self) -> bool

Check if the stream IPC object is still connected/valid
Source§

fn peer_count(&self) -> usize

Get the number of active peers (readers/writers/endpoints)
Source§

fn description(&self) -> String

Get a human-readable description of this IPC object
Source§

impl StreamOps for TcpSocket

Source§

fn read(&self, buffer: &mut [u8]) -> Result<usize, StreamError>

Read data from the stream
Source§

fn write(&self, data: &[u8]) -> Result<usize, StreamError>

Write data to the stream

Auto Trait Implementations§

§

impl !Freeze for TcpSocket

§

impl !RefUnwindSafe for TcpSocket

§

impl Send for TcpSocket

§

impl Sync for TcpSocket

§

impl Unpin for TcpSocket

§

impl !UnwindSafe for TcpSocket

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.