The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
tcp.h File Reference

TCP (RFC 9293): reliable byte streams between ports. More...

#include "ipv4.h"
#include "kernel/sync/wait_queue.h"

Go to the source code of this file.

Data Structures

struct  tcp_conn

Macros

#define TCP_BUFFER_SIZE   65535
#define TCP_MAX_RETRIES   8
#define TCP_SYN_RETRIES   5
#define TCP_TIME_WAIT_MS   30000
#define TCP_BACKLOG_MAX   32

Typedefs

typedef struct tcp_conn tcp_conn_t

Enumerations

enum  tcp_state_t {
  TCP_CLOSED , TCP_LISTEN , TCP_SYN_SENT , TCP_SYN_RECEIVED ,
  TCP_ESTABLISHED , TCP_FIN_WAIT_1 , TCP_FIN_WAIT_2 , TCP_CLOSE_WAIT ,
  TCP_CLOSING , TCP_LAST_ACK , TCP_TIME_WAIT
}

Functions

void tcp_init (void)
 set up TCP (from net_init): the timer.
void tcp_receive (netif_t *netif, const ipv4_header_t *ip, const uint8_t *segment, uint32_t length)
 a received segment (net_lock held).
tcp_conn_t * tcp_connect (uint32_t local, uint16_t local_port, uint32_t remote, uint16_t remote_port)
 start a connection: send a SYN (net_lock held).
tcp_conn_t * tcp_listen (uint32_t local, uint16_t local_port, uint32_t backlog)
 a listener on a port (net_lock held).
tcp_conn_t * tcp_accept (tcp_conn_t *listener)
 take the next connection from a listener's queue (net_lock held).
uint32_t tcp_write (tcp_conn_t *conn, const uint8_t *data, uint32_t length)
 put data into the send buffer and send what the windows allow (net_lock held).
uint32_t tcp_read (tcp_conn_t *conn, uint8_t *out, uint32_t length, bool peek)
 take data from the receive buffer (net_lock held).
void tcp_shutdown_write (tcp_conn_t *conn)
 no more data from the user: send FIN after the data (net_lock held).
void tcp_release (tcp_conn_t *conn)
 the socket lets the connection go (net_lock held): a listener resets the connections in its queue; a connection with data the user did not read is reset; any other is closed with FIN.
uint32_t tcp_send_room (const tcp_conn_t *conn)
 the room in the send buffer.
bool tcp_can_send (const tcp_conn_t *conn)
 true in the states where the user can send data.
void tcp_timer (uint64_t now)
 the timers, each tick (net_lock is taken here).

Detailed Description

TCP (RFC 9293): reliable byte streams between ports.

a connection (tcp_conn_t) keeps the state of one end: the sequence numbers, a send buffer and a receive buffer, and the timers. the socket code (tcp_socket.c) puts data in and takes data out; this code sends and receives the segments.

what it does:

  • the three-way handshake, both ways; a listener with a queue of connections to accept.
  • retransmission with a timeout from the measured round-trip time (RFC 6298), backing off to TCP_MAX_RETRIES; fast retransmit after three duplicate acks.
  • slow start and congestion avoidance (RFC 5681).
  • the receive window (no window scaling, so at most 64 KiB); a probe when the peer's window is zero.
  • the MSS option.
  • close with FIN (TIME_WAIT for TCP_TIME_WAIT_MS), reset with RST. what it does not do (yet): segments that come out of order are dropped (the sender sends them again), no selective acks, no timestamps, no keepalive, no urgent data. acks are sent at once (no delayed acks), and small writes go at once (no Nagle).

locking: all of it runs with net_lock held. a connection is freed only by tcp_timer (closed and with no socket), never while the stack works on it, so a send that loops back through lo cannot free the connection under its caller.

Macro Definition Documentation

◆ TCP_BUFFER_SIZE

#define TCP_BUFFER_SIZE   65535

◆ TCP_MAX_RETRIES

#define TCP_MAX_RETRIES   8

◆ TCP_SYN_RETRIES

#define TCP_SYN_RETRIES   5

◆ TCP_TIME_WAIT_MS

#define TCP_TIME_WAIT_MS   30000

◆ TCP_BACKLOG_MAX

#define TCP_BACKLOG_MAX   32

Typedef Documentation

◆ tcp_conn_t

typedef struct tcp_conn tcp_conn_t

Enumeration Type Documentation

◆ tcp_state_t

Enumerator
TCP_CLOSED 
TCP_LISTEN 
TCP_SYN_SENT 
TCP_SYN_RECEIVED 
TCP_ESTABLISHED 
TCP_FIN_WAIT_1 
TCP_FIN_WAIT_2 
TCP_CLOSE_WAIT 
TCP_CLOSING 
TCP_LAST_ACK 
TCP_TIME_WAIT 

Function Documentation

◆ tcp_init()

void tcp_init ( void )

set up TCP (from net_init): the timer.

◆ tcp_receive()

void tcp_receive ( netif_t * netif,
const ipv4_header_t * ip,
const uint8_t * segment,
uint32_t length )

a received segment (net_lock held).

◆ tcp_connect()

tcp_conn_t * tcp_connect ( uint32_t local,
uint16_t local_port,
uint32_t remote,
uint16_t remote_port )

start a connection: send a SYN (net_lock held).

Returns
the connection in SYN_SENT (or already ESTABLISHED over lo), or NULL if there is no memory.

◆ tcp_listen()

tcp_conn_t * tcp_listen ( uint32_t local,
uint16_t local_port,
uint32_t backlog )

a listener on a port (net_lock held).

Parameters
localthe address, or 0 for any.
Returns
the connection in LISTEN, or NULL if there is no memory.

◆ tcp_accept()

tcp_conn_t * tcp_accept ( tcp_conn_t * listener)

take the next connection from a listener's queue (net_lock held).

Returns
it (not orphaned: the caller gives it a socket), or NULL.

◆ tcp_write()

uint32_t tcp_write ( tcp_conn_t * conn,
const uint8_t * data,
uint32_t length )

put data into the send buffer and send what the windows allow (net_lock held).

Returns
the bytes taken (0 when the buffer is full).

◆ tcp_read()

uint32_t tcp_read ( tcp_conn_t * conn,
uint8_t * out,
uint32_t length,
bool peek )

take data from the receive buffer (net_lock held).

this opens the window again, and the peer is told when that matters.

Parameters
peekleave the data in the buffer.
Returns
the bytes taken.

◆ tcp_shutdown_write()

void tcp_shutdown_write ( tcp_conn_t * conn)

no more data from the user: send FIN after the data (net_lock held).

◆ tcp_release()

void tcp_release ( tcp_conn_t * conn)

the socket lets the connection go (net_lock held): a listener resets the connections in its queue; a connection with data the user did not read is reset; any other is closed with FIN.

the connection is freed when it is closed.

◆ tcp_send_room()

uint32_t tcp_send_room ( const tcp_conn_t * conn)

the room in the send buffer.

◆ tcp_can_send()

bool tcp_can_send ( const tcp_conn_t * conn)

true in the states where the user can send data.

◆ tcp_timer()

void tcp_timer ( uint64_t now)

the timers, each tick (net_lock is taken here).