Retro Rocket Kernel
BASIC-Powered Operating System
tcp.h File Reference
#include "kernel.h"

Data Structures

struct  tcp_ip_pseudo_header_t
 
union  tcp_segment_flags_t
 
struct  tcp_segment
 
struct  tcp_options_t
 
struct  tcp_ordered_list_t
 
struct  tcp_conn_t
 

Macros

#define TCP_WINDOW_SIZE   65535
 
#define TCP_PACKET_SIZE_OFF   5
 

Enumerations

enum  tcp_state_flags_t {
  TCP_FIN = 1 , TCP_SYN = 2 , TCP_RST = 4 , TCP_PSH = 8 ,
  TCP_ACK = 16 , TCP_URG = 32 , TCP_ECE = 64 , TCP_CWR = 128
}
 
enum  tcp_opt_t { TCP_OPT_END = 0 , TCP_OPT_NOP = 1 , TCP_OPT_MSS = 2 }
 
enum  tcp_state_t {
  TCP_LISTEN = 0 , TCP_SYN_SENT = 1 , TCP_SYN_RECEIVED = 2 , TCP_ESTABLISHED = 3 ,
  TCP_FIN_WAIT_1 = 4 , TCP_FIN_WAIT_2 = 5 , TCP_CLOSE_WAIT = 6 , TCP_CLOSING = 7 ,
  TCP_LAST_ACK = 8 , TCP_TIME_WAIT = 9
}
 
enum  tcp_error_t { TCP_CONN_RESET = 1 , TCP_CONN_REFUSED = 2 , TCP_CONN_CLOSING = 3 }
 
enum  tcp_port_type_t { TCP_PORT_LOCAL , TCP_PORT_REMOTE }
 Port types, either a local or remote port. More...
 
enum  tcp_error_code_t {
  TCP_ERROR_ALREADY_CLOSING = -1 , TCP_ERROR_PORT_IN_USE = -2 , TCP_ERROR_NETWORK_DOWN = -3 , TCP_ERROR_INVALID_CONNECTION = -4 ,
  TCP_ERROR_WRITE_TOO_LARGE = -5 , TCP_ERROR_NOT_CONNECTED = -6 , TCP_ERROR_OUT_OF_DESCRIPTORS = -7 , TCP_ERROR_OUT_OF_MEMORY = -8 ,
  TCP_ERROR_INVALID_SOCKET = -9 , TCP_ERROR_CONNECTION_FAILED = -10 , TCP_LAST_ERROR = -11
}
 Error codes which can be returned by socket functions. More...
 

Functions

struct tcp_ip_pseudo_header_t __attribute__ ((packed)) tcp_ip_pseudo_header_t
 
void tcp_handle_packet ([[maybe_unused]] ip_packet_t *encap_packet, tcp_segment_t *segment, size_t len)
 TCP handler called by the IP layer. More...
 
void tcp_init ()
 Initialise TCP protocol, must happen after IP. More...
 
int connect (uint32_t target_addr, uint16_t target_port, uint16_t source_port, bool blocking)
 Connect to a TCP port at a given IPv4 address. More...
 
int closesocket (int socket)
 Close a TCP connection. More...
 
int send (int socket, const void *buffer, uint32_t length)
 Send data to an open socket. More...
 
int recv (int socket, void *buffer, uint32_t maxlen, bool blocking, uint32_t timeout)
 Receive data from an open socket. The data is buffered, and this function will fill the buffer when there is data in the buffer to read. More...
 
bool is_connected (int socket)
 Returns true if the socket is connected. More...
 
const char * socket_error (int error_code)
 Return the error description associated with an error code. More...
 
void tcp_idle ()
 Idle loop ran from timer ISR. More...
 

Variables

typedef __attribute__
 System Description Table Header. More...
 
uint32_t src
 
uint32_t dst
 
uint8_t reserved
 
uint8_t protocol
 
uint16_t len
 
uint8_t body []
 
uint8_t bits1
 
uint8_t bits2
 
struct {
   uint8_t   reserved:4
 
   uint8_t   off:4
 
   uint8_t   fin:1
 
   uint8_t   syn:1
 
   uint8_t   rst:1
 
   uint8_t   psh:1
 
   uint8_t   ack:1
 
   uint8_t   urg:1
 
   uint8_t   ece:1
 
   uint8_t   cwr:1
 
}; 
 
uint16_t src_port
 
uint16_t dst_port
 
uint32_t seq
 
tcp_segment_flags_t flags
 
uint16_t window_size
 
uint16_t checksum
 
uint16_t urgent
 
uint8_t options [0]
 
uint8_t payload []
 

Detailed Description

Author
Craig Edwards (craig.nosp@m.edwa.nosp@m.rds@b.nosp@m.rain.nosp@m.box.c.nosp@m.c)

Macro Definition Documentation

◆ TCP_PACKET_SIZE_OFF

#define TCP_PACKET_SIZE_OFF   5

◆ TCP_WINDOW_SIZE

#define TCP_WINDOW_SIZE   65535

Enumeration Type Documentation

◆ tcp_error_code_t

Error codes which can be returned by socket functions.

Enumerator
TCP_ERROR_ALREADY_CLOSING 
TCP_ERROR_PORT_IN_USE 
TCP_ERROR_NETWORK_DOWN 
TCP_ERROR_INVALID_CONNECTION 
TCP_ERROR_WRITE_TOO_LARGE 
TCP_ERROR_NOT_CONNECTED 
TCP_ERROR_OUT_OF_DESCRIPTORS 
TCP_ERROR_OUT_OF_MEMORY 
TCP_ERROR_INVALID_SOCKET 
TCP_ERROR_CONNECTION_FAILED 
TCP_LAST_ERROR 

◆ tcp_error_t

Enumerator
TCP_CONN_RESET 
TCP_CONN_REFUSED 
TCP_CONN_CLOSING 

◆ tcp_opt_t

enum tcp_opt_t
Enumerator
TCP_OPT_END 
TCP_OPT_NOP 
TCP_OPT_MSS 

◆ tcp_port_type_t

Port types, either a local or remote port.

Enumerator
TCP_PORT_LOCAL 
TCP_PORT_REMOTE 

◆ tcp_state_flags_t

Enumerator
TCP_FIN 
TCP_SYN 
TCP_RST 
TCP_PSH 
TCP_ACK 
TCP_URG 
TCP_ECE 
TCP_CWR 

◆ tcp_state_t

Enumerator
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

◆ __attribute__()

struct tcp_ip_pseudo_header_t __attribute__ ( (packed)  )

◆ closesocket()

int closesocket ( int  socket)

Close a TCP connection.

Parameters
socketsocket descriptor to close
Returns
zero on success, error code on error

◆ connect()

int connect ( uint32_t  target_addr,
uint16_t  target_port,
uint16_t  source_port,
bool  blocking 
)

Connect to a TCP port at a given IPv4 address.

Parameters
target_addrTarget address to connect to
target_portTarget port to connect to
source_portOur source port to use, or 0 to choose automatically
blockingSet to true if this call is to block until the connection is established or errors
Returns
Zero or positive file descriptor number on success, negative for error

◆ is_connected()

bool is_connected ( int  socket)

Returns true if the socket is connected.

Parameters
socketsocket descriptor from connect()
Returns
true if socket is connected, false if not. This will return false if connect() hasn't succeeded yet.

◆ recv()

int recv ( int  socket,
void *  buffer,
uint32_t  maxlen,
bool  blocking,
uint32_t  timeout 
)

Receive data from an open socket. The data is buffered, and this function will fill the buffer when there is data in the buffer to read.

Parameters
socketsocket descriptor from connect()
bufferbuffer to receive data to
maxlenmaximum size of data to receive to buffer
blockingtrue if this call should block until the buffer has data
timeoutread timeout when blocking (seconds)
Returns
int number of bytes received

◆ send()

int send ( int  socket,
const void *  buffer,
uint32_t  length 
)

Send data to an open socket.

Parameters
socketsocket descriptor from connect()
bufferbuffer to send
lengthnumber of bytes to send
Returns
int number of bytes written

◆ socket_error()

const char* socket_error ( int  error_code)

Return the error description associated with an error code.

Note
Invalid error codes return the constant "No error".
Parameters
error_codeerror code
Returns
const char* description

◆ tcp_handle_packet()

void tcp_handle_packet ( [[maybe_unused] ] ip_packet_t *  encap_packet,
tcp_segment_t *  segment,
size_t  len 
)

TCP handler called by the IP layer.

Parameters
encap_packetencapsulating IP packet
segmentTCP segment
lenlength of TCP segment including header

◆ tcp_idle()

void tcp_idle ( )

Idle loop ran from timer ISR.

◆ tcp_init()

void tcp_init ( )

Initialise TCP protocol, must happen after IP.

Variable Documentation

◆ 

struct { ... }

◆ __attribute__

System Description Table Header.

A disk partition table consisiting of four partition_t.

Structure for an IP packet.

Long filename entry, overlays a directory_entry_t. These entries repeat before a non-lfn entry, each holding up to 13 UCS-2 characters. Note that they are not stored in order, the 'order' attribute indicates which order they go in.

FAT32 BIOS parameter block (BPB), stored in the boot sector of the drive.

A directory entry, e.g. file, volume label, directory, long filename entry.

Root System Description Table.

◆ ack

uint32_t ack

◆ bits1

uint8_t bits1

◆ bits2

uint8_t bits2

◆ body

uint8_t body[]

◆ checksum

uint16_t checksum

◆ cwr

uint8_t cwr

◆ dst

uint32_t dst

◆ dst_port

uint16_t dst_port

◆ ece

uint8_t ece

◆ fin

uint8_t fin

◆ flags

◆ len

uint16_t len

◆ off

uint8_t off

◆ options

uint8_t options[0]

◆ payload

uint8_t payload[]

◆ protocol

uint8_t protocol

◆ psh

uint8_t psh

◆ reserved

uint8_t reserved

◆ rst

uint8_t rst

◆ seq

uint32_t seq

◆ src

uint32_t src

◆ src_port

uint16_t src_port

◆ syn

uint8_t syn

◆ urg

uint8_t urg

◆ urgent

uint16_t urgent

◆ window_size

uint16_t window_size