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

TCP (see tcp.h). More...

#include "tcp.h"
#include "byteorder.h"
#include "checksum.h"
#include "common/memory.h"
#include "kernel/errno.h"
#include "kernel/memory/heap.h"
#include "kernel/random/random.h"
#include "kernel/time/timer.h"
#include "kernel/vfs/poll.h"
#include "netif.h"

Macros

#define HEADER_SIZE   20
#define OPTION_MSS   2
#define FLAG_FIN   0x01
#define FLAG_SYN   0x02
#define FLAG_RST   0x04
#define FLAG_PSH   0x08
#define FLAG_ACK   0x10
#define DEFAULT_MSS   536
#define INITIAL_RTO_MS   1000
#define MIN_RTO_MS   200
#define MAX_RTO_MS   60000
#define INITIAL_CWND   10
#define ORPHAN_FIN_WAIT_2_MS   60000

Functions

uint32_t tcp_send_room (const tcp_conn_t *c)
 the room in the send buffer.
bool tcp_can_send (const tcp_conn_t *c)
 true in the states where the user can send data.
void tcp_receive (netif_t *netif, const ipv4_header_t *ip, const uint8_t *seg, 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 *c, 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 *c, uint8_t *out, uint32_t length, bool peek)
 take data from the receive buffer (net_lock held).
void tcp_shutdown_write (tcp_conn_t *c)
 no more data from the user: send FIN after the data (net_lock held).
void tcp_release (tcp_conn_t *c)
 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.
void tcp_timer (uint64_t now)
 the timers, each tick (net_lock is taken here).
void tcp_init (void)
 set up TCP (from net_init): the timer.

Detailed Description

TCP (see tcp.h).

sequence numbers wrap, so they are compared with seq_lt and the others, which look at the signed difference.

Macro Definition Documentation

◆ HEADER_SIZE

#define HEADER_SIZE   20

◆ OPTION_MSS

#define OPTION_MSS   2

◆ FLAG_FIN

#define FLAG_FIN   0x01

◆ FLAG_SYN

#define FLAG_SYN   0x02

◆ FLAG_RST

#define FLAG_RST   0x04

◆ FLAG_PSH

#define FLAG_PSH   0x08

◆ FLAG_ACK

#define FLAG_ACK   0x10

◆ DEFAULT_MSS

#define DEFAULT_MSS   536

◆ INITIAL_RTO_MS

#define INITIAL_RTO_MS   1000

◆ MIN_RTO_MS

#define MIN_RTO_MS   200

◆ MAX_RTO_MS

#define MAX_RTO_MS   60000

◆ INITIAL_CWND

#define INITIAL_CWND   10

◆ ORPHAN_FIN_WAIT_2_MS

#define ORPHAN_FIN_WAIT_2_MS   60000

Function Documentation

◆ tcp_send_room()

uint32_t tcp_send_room ( const tcp_conn_t * c)

the room in the send buffer.

◆ tcp_can_send()

bool tcp_can_send ( const tcp_conn_t * c)

true in the states where the user can send data.

◆ tcp_receive()

void tcp_receive ( netif_t * netif,
const ipv4_header_t * ip,
const uint8_t * seg,
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 * c)

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_timer()

void tcp_timer ( uint64_t now)

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

◆ tcp_init()

void tcp_init ( void )

set up TCP (from net_init): the timer.