smoltcp-rs/smoltcp

Undertanding TCP Implementation

Closed this issue · 1 comments

Hello! I am rather new to the code base and am interested in using smoltcp. I am, however, having a hard time seeing at how some of it works when I am looking through the code base. Bare in mind that I am not an expert at Rust or networking, but I want to learn more!

More specifically...

pub fn connect<T, U>(

I have been looking through the examples. When a socket seeks to establish a connection it calls socket.connect() which should initiate the handshake with a SYN. In connect it seems that the state of the socket is set to SynSent, but I am failing to see where the actual transmission of a SYN occurs. Please help me understand. In general, any advice on better understanding smoltcp would be a huge help. Thank you!

here

smoltcp/src/socket/tcp.rs

Lines 2301 to 2310 in 23697fa

// We transmit a SYN in the SYN-SENT state.
// We transmit a SYN|ACK in the SYN-RECEIVED state.
State::SynSent | State::SynReceived => {
repr.control = TcpControl::Syn;
// window len must NOT be scaled in SYNs.
repr.window_len = self.rx_buffer.window().min((1 << 16) - 1) as u16;
if self.state == State::SynSent {
repr.ack_number = None;
repr.window_scale = Some(self.remote_win_shift);
repr.sack_permitted = true;

The TcpSocket methods just get/set state in the struct, and it's fn dispatch that sends packets that need to be sent based on the state.