The friendly Operating System for the Internet of Things

RIOT's tcp implementation for the gnrc stack. More...

Detailed Description

Files

file  config.h
 GNRC TCP configuration.
 
file  tcb.h
 GNRC TCP transmission control block.
 
file  gnrc/tcp.h
 GNRC TCP API.
 
file  common.h
 Internally used common defines, macros and variable declaration.
 
file  eventloop.h
 TCP event loop delarations.
 
file  fsm.h
 TCP finite state maschine declarations.
 
file  option.h
 TCP option handling declarations.
 
file  net/gnrc/transport_layer/tcp/internal/pkt.h
 TCP paket handling declarations.
 
file  rcvbuf.h
 Functions for allocating and freeing the receive buffer.
 

Data Structures

struct  _transmission_control_block
 transmission control block of gnrc_tcp More...
 
struct  rcvbuf_entry
 Struct for a single connections receive buffer. More...
 
struct  rcvbuf
 Stuct holding receive buffers. More...
 

Macros

#define GNRC_TCP_CONNECTION_TIMEOUT_DURATION   (120U * US_PER_SEC)
 Timeout Duration for user calls. More...
 
#define GNRC_TCP_MSL   (30U * US_PER_SEC)
 Maximum Segment Lifetime. More...
 
#define GNRC_TCP_MSS   (1220U)
 Maximum Segement Size. More...
 
#define GNRC_TCP_MSS_MULTIPLICATOR   (1U)
 MSS Multiplicator = Number of MSS sized packets stored in receive buffer.
 
#define GNRC_TCP_DEFAULT_WINDOW   (GNRC_TCP_MSS * GNRC_TCP_MSS_MULTIPLICATOR)
 Default Window Size.
 
#define GNRC_TCP_RCV_BUFFERS   (1U)
 Number of preallocated receive buffers.
 
#define GNRC_TCP_RCV_BUF_SIZE   (GNRC_TCP_DEFAULT_WINDOW)
 Default Receive Buffer Size.
 
#define GNRC_TCP_RTO_LOWER_BOUND   (1U * US_PER_SEC)
 Lower Bound for RTO = 1 sec (see RFC 6298)
 
#define GNRC_TCP_RTO_UPPER_BOUND   (60U * US_PER_SEC)
 Upper Bound for RTO = 60 sec (see RFC 6298)
 
#define GNRC_TCP_RTO_GRANULARITY   (10U * MS_PER_SEC)
 Assumes clock granularity for TCP of 10 ms (see RFC 6298)
 
#define GNRC_TCP_RTO_A_DIV   (8U)
 Alpha value for RTO calculation, default is 1/8.
 
#define GNRC_TCP_RTO_B_DIV   (4U)
 Beta value for RTO calculation, default is 1/4.
 
#define GNRC_TCP_RTO_K   (4U)
 K value for RTO calculation, default is 4.
 
#define GNRC_TCP_PROBE_LOWER_BOUND   (1U * US_PER_SEC)
 Lower Bound for the duration between probes.
 
#define GNRC_TCP_PROBE_UPPER_BOUND   (60U * US_PER_SEC)
 Upper Bound for the duration between probes.
 
#define GNRC_TCP_TCB_MSG_QUEUE_SIZE   (8U)
 Size of the tcbs internal message queue.
 
#define PORT_UNSPEC   (0)
 Port unspecified. More...
 
#define RTO_UNINITIALIZED   (-1)
 Macro to mark is the time measurement is uninitialized.
 
#define INSIDE_WND(l_ed, seq_num, r_ed)   (LEQ_32_BIT(l_ed, seq_num) && LSS_32_BIT(seq_num, r_ed))
 Check if a given sequence number, falls into the a receive window.
 
#define GET_OFFSET(x)   (((x) & MSK_OFFSET) >> 12)
 Extract offset value from "offctl"-header field.
 

Typedefs

typedef struct _transmission_control_block gnrc_tcp_tcb_t
 transmission control block of gnrc_tcp
 
typedef struct rcvbuf_entry rcvbuf_entry_t
 Struct for a single connections receive buffer.
 
typedef struct rcvbuf rcvbuf_t
 Stuct holding receive buffers.
 

Enumerations

enum  fsm_state_t {
  FSM_STATE_CLOSED, FSM_STATE_LISTEN, FSM_STATE_SYN_SENT, FSM_STATE_SYN_RCVD,
  FSM_STATE_ESTABLISHED, FSM_STATE_CLOSE_WAIT, FSM_STATE_LAST_ACK, FSM_STATE_FIN_WAIT_1,
  FSM_STATE_FIN_WAIT_2, FSM_STATE_CLOSING, FSM_STATE_TIME_WAIT
}
 The TCP FSM States.
 
enum  fsm_event_t {
  FSM_EVENT_CALL_OPEN, FSM_EVENT_CALL_SEND, FSM_EVENT_CALL_RECV, FSM_EVENT_CALL_CLOSE,
  FSM_EVENT_CALL_ABORT, FSM_EVENT_RCVD_PKT, FSM_EVENT_TIMEOUT_TIMEWAIT, FSM_EVENT_TIMEOUT_RETRANSMIT,
  FSM_EVENT_TIMEOUT_CONNECTION, FSM_EVENT_SEND_PROBE, FSM_EVENT_CLEAR_RETRANSMIT
}
 Events that trigger translations in TCP FSM.
 

Functions

int gnrc_tcp_init (void)
 Initialize and start TCP. More...
 
void gnrc_tcp_tcb_init (gnrc_tcp_tcb_t *tcb)
 Initialize Transmission Control Block (tcb) More...
 
int gnrc_tcp_open_active (gnrc_tcp_tcb_t *tcb, const uint8_t address_family, const uint8_t *target_addr, const uint16_t target_port, const uint16_t local_port)
 Opens a connection actively. More...
 
int gnrc_tcp_open_passive (gnrc_tcp_tcb_t *tcb, const uint8_t address_family, const uint8_t *local_addr, const uint16_t local_port)
 Opens a connection passively, by waiting for an incomming request. More...
 
ssize_t gnrc_tcp_send (gnrc_tcp_tcb_t *tcb, const void *data, const size_t len, const uint32_t user_timeout_duration_us)
 Transmit Data to Peer. More...
 
ssize_t gnrc_tcp_recv (gnrc_tcp_tcb_t *tcb, void *data, const size_t max_len, const uint32_t user_timeout_duration_us)
 Receive Data from the Peer. More...
 
int gnrc_tcp_close (gnrc_tcp_tcb_t *tcb)
 Close a tcp connection. More...
 
int gnrc_tcp_calc_csum (const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_hdr)
 Set checksum calculated from tcp and network-layer header in tcp-header. More...
 
gnrc_pktsnip_tgnrc_tcp_hdr_build (gnrc_pktsnip_t *payload, uint16_t src, uint16_t dst)
 Adds a tcp header to a given payload. More...
 
void * _event_loop (void *arg)
 TCP's mein processing thread. More...
 
int _fsm (gnrc_tcp_tcb_t *tcb, fsm_event_t event, gnrc_pktsnip_t *in_pkt, void *buf, size_t len)
 TCP finite state maschine. More...
 
static uint32_t _option_build_mss (uint16_t mss)
 Helper Function to build the MSS Option. More...
 
static uint16_t _option_build_offset_control (uint16_t nopts, uint16_t ctl)
 Helper Function to build the combined option and control flag field. More...
 
int _option_parse (gnrc_tcp_tcb_t *tcb, tcp_hdr_t *hdr)
 Parses options of a given tcp-header pktsnip. More...
 
int _pkt_build_reset_from_pkt (gnrc_pktsnip_t **out_pkt, gnrc_pktsnip_t *in_pkt)
 Build a reset packet from an incomming packet. More...
 
int _pkt_build (gnrc_tcp_tcb_t *tcb, gnrc_pktsnip_t **out_pkt, uint16_t *seq_con, const uint16_t ctl, const uint32_t seq_num, const uint32_t ack_num, void *payload, const size_t payload_len)
 Build and allocate a tcp paket, tcb stores pointer to new paket. More...
 
int _pkt_send (gnrc_tcp_tcb_t *tcb, gnrc_pktsnip_t *out_pkt, const uint16_t seq_con, const bool retransmit)
 Sends a packet to the peer. More...
 
int _pkt_chk_seq_num (const gnrc_tcp_tcb_t *tcb, const uint32_t seq_num, const uint32_t seg_len)
 Checks sequence number. More...
 
uint32_t _pkt_get_seg_len (gnrc_pktsnip_t *pkt)
 Extracts the length of a segment. More...
 
uint32_t _pkt_get_pay_len (gnrc_pktsnip_t *pkt)
 Calculates a segments payload length. More...
 
int _pkt_setup_retransmit (gnrc_tcp_tcb_t *tcb, gnrc_pktsnip_t *pkt, const bool retransmit)
 Adds a paket to the retransmission mechanism. More...
 
int _pkt_acknowledge (gnrc_tcp_tcb_t *tcb, const uint32_t ack)
 Acknowledges and removes packet from the retransmission mechanism. More...
 
uint16_t _pkt_calc_csum (const gnrc_pktsnip_t *hdr, const gnrc_pktsnip_t *pseudo_hdr, const gnrc_pktsnip_t *payload)
 Calculates checksum over payload, tcp-header and network layer header. More...
 
void _rcvbuf_init (void)
 Initializes global receive Buffer.
 
int _rcvbuf_get_buffer (gnrc_tcp_tcb_t *tcb)
 Initializes and assigns receive Buffer to tcb. More...
 
void _rcvbuf_release_buffer (gnrc_tcp_tcb_t *tcb)
 Free allocated receive buffer. More...
 

Variables

kernel_pid_t gnrc_tcp_pid
 PID of tcp event handling thread.
 
gnrc_tcp_tcb_t_list_tcb_head
 Head of linked tcb list.
 
mutex_t _list_tcb_lock
 Mutex to protect linked list.
 
#define STATUS_PASSIVE   (1 << 0)
 Status Flags for TCP.
 
#define STATUS_ALLOW_ANY_ADDR   (1 << 1)
 
#define TCP_EVENTLOOP_MSG_QUEUE_SIZE   (8U)
 Defines for gnrc tcps "eventloop" thread.
 
#define TCP_EVENTLOOP_PRIO   (THREAD_PRIORITY_MAIN - 2U)
 
#define TCP_EVENTLOOP_STACK_SIZE   (THREAD_STACKSIZE_DEFAULT)
 
#define MSK_FIN   (0x0001)
 Bitmasks for control bit handling.
 
#define MSK_SYN   (0x0002)
 
#define MSK_RST   (0x0004)
 
#define MSK_PSH   (0x0008)
 
#define MSK_ACK   (0x0010)
 
#define MSK_URG   (0x0020)
 
#define MSK_FIN_ACK   (0x0011)
 
#define MSK_SYN_ACK   (0x0012)
 
#define MSK_RST_ACK   (0x0014)
 
#define MSK_SYN_FIN_ACK   (0x0013)
 
#define MSK_FIN_ACK_PSH   (0x0019)
 
#define MSK_CTL   (0x003F)
 
#define MSK_OFFSET   (0xF000)
 
#define MSG_TYPE_CONNECTION_TIMEOUT   (GNRC_NETAPI_MSG_TYPE_ACK + 101)
 Type field values for TCP internal Message Passing.
 
#define MSG_TYPE_PROBE_TIMEOUT   (GNRC_NETAPI_MSG_TYPE_ACK + 102)
 
#define MSG_TYPE_USER_SPEC_TIMEOUT   (GNRC_NETAPI_MSG_TYPE_ACK + 103)
 
#define MSG_TYPE_RETRANSMISSION   (GNRC_NETAPI_MSG_TYPE_ACK + 104)
 
#define MSG_TYPE_TIMEWAIT   (GNRC_NETAPI_MSG_TYPE_ACK + 105)
 
#define MSG_TYPE_NOTIFY_USER   (GNRC_NETAPI_MSG_TYPE_ACK + 106)
 
#define LSS_32_BIT(x, y)   (((int32_t) (x)) - ((int32_t) (y)) < 0)
 Overflow tolerant comparision operators for sequence and acknowledgement number comparision.
 
#define LEQ_32_BIT(x, y)   (((int32_t) (x)) - ((int32_t) (y)) <= 0)
 
#define GRT_32_BIT(x, y)   (!LEQ_32_BIT(x, y))
 
#define GEQ_32_BIT(x, y)   (!LSS_32_BOT(x, y))
 

Macro Definition Documentation

#define GNRC_TCP_CONNECTION_TIMEOUT_DURATION   (120U * US_PER_SEC)

Default 2 minutes

Definition at line 35 of file config.h.

#define GNRC_TCP_MSL   (30U * US_PER_SEC)

Default 30 secounds

Definition at line 42 of file config.h.

#define GNRC_TCP_MSS   (1220U)

If IPv6 is used. Get MSS = 1280 - IPv6-Hdr - TCP-Hdr = 1220

Definition at line 50 of file config.h.

#define PORT_UNSPEC   (0)
Note
PORT 0 is unspecified (
See also
https://www.ietf.org/rfc/rfc1700.txt)

Definition at line 41 of file common.h.

Function Documentation

void* _event_loop ( void *  arg)
Parameters
[in]argarguments, unused
Returns
Never returns, its an endless loop
int _fsm ( gnrc_tcp_tcb_t tcb,
fsm_event_t  event,
gnrc_pktsnip_t in_pkt,
void *  buf,
size_t  len 
)
Parameters
[in,out]tcbspecifies connection to use fsm on.
[in]eventcurrent event that triggers fsm translation
[in]in_pktpacket that triggered fsm event. Only in case of RCVD_PKT
[in,out]bufbuffer for send and receive functions
[in]lennumber of bytes to send or receive atmost
Returns
Zero on success
Positive Number, number of bytes sent from or copied into buf.
-ENOSYS if event is not implemented
static uint32_t _option_build_mss ( uint16_t  mss)
inlinestatic
Parameters
[in]msstcp header to be checked
Returns
Valid MSS Option.

Definition at line 41 of file option.h.

static uint16_t _option_build_offset_control ( uint16_t  nopts,
uint16_t  ctl 
)
inlinestatic
Parameters
[in]noptsNumber of Options
[in]ctlControl Flags
Returns
Valid option size and control field.

Definition at line 55 of file option.h.

int _option_parse ( gnrc_tcp_tcb_t tcb,
tcp_hdr_t hdr 
)
Parameters
[out]tcbtransmission control block to memorize options.
[in]hdrtcp header to be checked
Returns
Zero on success
A negative value on error
int _pkt_acknowledge ( gnrc_tcp_tcb_t tcb,
const uint32_t  ack 
)
Parameters
[in,out]tcbThis connections Transmission control block.
[in]ackAcknowldegment number used to acknowledge packets
Returns
Zero on success
-ENODATA if there is nothing to acknowledge
int _pkt_build ( gnrc_tcp_tcb_t tcb,
gnrc_pktsnip_t **  out_pkt,
uint16_t *  seq_con,
const uint16_t  ctl,
const uint32_t  seq_num,
const uint32_t  ack_num,
void *  payload,
const size_t  payload_len 
)
Parameters
[in,out]tcbThis connections transmission control block.
[out]out_pktPointer to paket to build
[out]seq_conNumber of Bytes, the packet will consume in sequence number space
[in]ctlcontrol bits to set in out_pkt
[in]seq_numsequence number of the new paket
[in]ack_numacknowledgment number of the new paket
[in]payloadpointer to payload buffer
[in]payload_lenpayload size
Returns
Zero on success.
-ENOMEM if pktbuf is full.
int _pkt_build_reset_from_pkt ( gnrc_pktsnip_t **  out_pkt,
gnrc_pktsnip_t in_pkt 
)

This function builds a reset from an incomming packet for cases where the connection has not been established

Parameters
[out]out_pktoutgoing reset packet
[in]in_pktincomming packet
Returns
Zero on success
-ENOMEM if pktbuf is full.
uint16_t _pkt_calc_csum ( const gnrc_pktsnip_t hdr,
const gnrc_pktsnip_t pseudo_hdr,
const gnrc_pktsnip_t payload 
)
Parameters
[in]hdrgnrc_pktsnip_t to tcp-header
[in]pseudo_hdrgnrc_pktsnip_t to network layer header
[in]payloadgnrc_pktsnip_t to payload
Returns
non zero checksum if given network layer is supported
zero if given network layer is not supported
int _pkt_chk_seq_num ( const gnrc_tcp_tcb_t tcb,
const uint32_t  seq_num,
const uint32_t  seg_len 
)
Parameters
[in,out]tcbThis connections Transmission control block.
[in]seq_numsequence number from the segment
[in]seg_lenlength of a segments payload
Returns
Zero if the sequence number is invalid
Non-zero if the sequence number is acceptable
uint32_t _pkt_get_pay_len ( gnrc_pktsnip_t pkt)
Parameters
[in]pktPacket to calculate payload length
Returns
the segments payload length in bytes
uint32_t _pkt_get_seg_len ( gnrc_pktsnip_t pkt)
Parameters
[in]pktPacket to calculate payload length
Returns
number consumption in sequence number space
int _pkt_send ( gnrc_tcp_tcb_t tcb,
gnrc_pktsnip_t out_pkt,
const uint16_t  seq_con,
const bool  retransmit 
)
Parameters
[in,out]tcbThis connections Transmission control block.
[in]out_pktpointer to paket to send
[in]seq_consequence number consumption of the paket to send
[in]retransmitis this a retransmission ?
Returns
Zero on success.
-EINVAL if out_pkt was NULL
int _pkt_setup_retransmit ( gnrc_tcp_tcb_t tcb,
gnrc_pktsnip_t pkt,
const bool  retransmit 
)
Parameters
[in,out]tcbThis connections Transmission control block.
[in]pktpaket to add to the retransmission mechanism
[in]retransmitFlag used to indicate that pkt is a retransmit.
Returns
Zero on success
-ENOMEM if the retransmission queue is full
-EINVAL if pkt is null
int _rcvbuf_get_buffer ( gnrc_tcp_tcb_t tcb)
Parameters
[in]tcbTransmission control block that should hold the buffer.
Returns
zero on success
-ENOMEM If receive buffer is out of memory.
void _rcvbuf_release_buffer ( gnrc_tcp_tcb_t tcb)
Parameters
[in]tcbTransmission control block that buffer should be freed.
int gnrc_tcp_calc_csum ( const gnrc_pktsnip_t hdr,
const gnrc_pktsnip_t pseudo_hdr 
)
Parameters
[in]hdrgnrc_pktsnip that contains tcp header.
[in]pseudo_hdrgnrc_pktsnip that contains networklayer header.
Returns
zero on succeed.
-EFAULT if hdr or pseudo_hdr were NULL
-EBADMSG if hdr is not of type GNRC_NETTYPE_TCP
-ENOENT if pseudo_hdr protocol is unsupported.
int gnrc_tcp_close ( gnrc_tcp_tcb_t tcb)
Precondition
gnrc_tcp_tcb_init() must have been successfully called.
tcb must not be NULL.
Parameters
[in,out]tcbThis connections Transmission control block.
Returns
Zero on success.
gnrc_pktsnip_t* gnrc_tcp_hdr_build ( gnrc_pktsnip_t payload,
uint16_t  src,
uint16_t  dst 
)

Be carefull, leads to huge headers. Allocates all option bytes

Parameters
[in]payloadpayload that follows the tcp header
[in]srcSource port in host byte order
[in]dstDestination port in host byte order
Returns
NULL, if paket buffer is full
Not NULL on success
int gnrc_tcp_init ( void  )
Returns
PID of TCP thread on success
-1 if thread is already running.
-EINVAL, if priority is greater than or equal SCHED_PRIO_LEVELS
-EOVERFLOW, if there are too many threads running.
int gnrc_tcp_open_active ( gnrc_tcp_tcb_t tcb,
const uint8_t  address_family,
const uint8_t *  target_addr,
const uint16_t  target_port,
const uint16_t  local_port 
)
Precondition
gnrc_tcp_tcb_init() must have been successfully called.
tcb must not be NULL
target_addr must not be NULL.
target_port must not be 0.
Note
Blocks until a connection has been established or an error occured.
Parameters
[in,out]tcbThis connections Transmission control block.
[in]address_familyAddress Family of target_addr.
[in]target_addrPointer to target address.
[in]target_portTargets port number.
[in]local_portIf zero or GNRC_TCP_PORT_UNSPEC, the connections source port is randomly chosen. If local_port is non-zero the local_port is used as source port.
Returns
Zero on success.
-EAFNOSUPPORT if address_family is not supported.
-EINVAL if address_family is not the same the address_family use by the tcb.
-EISCONN if transmission control block is already in use.
-ENOMEM if the receive buffer for the tcb could not be allocated. Increase "GNRC_TCP_RCV_BUFFERS".
-EADDRINUSE if local_port is already used by another connection.
-ETIMEDOUT if the connection could not be opened.
-ECONNREFUSED if the connection was resetted by the peer.
int gnrc_tcp_open_passive ( gnrc_tcp_tcb_t tcb,
const uint8_t  address_family,
const uint8_t *  local_addr,
const uint16_t  local_port 
)
Precondition
gnrc_tcp_tcb_init() must have been successfully called.
tcb must not be NULL.
if local_addr is not NULL, local_addr must be assigned to a network interface.
if local_port is not zero.
Note
Blocks until a connection has been established (incomming connection request to local_port) or an error occured.
Parameters
[in,out]tcbThis connections Transmission control block.
[in]address_familyAddress Family of local_addr. If local_addr == NULL, address_family is ignored.
[in]local_addrIf not NULL the connection is bound to the address local_addr. If NULL a connection request to every local ip address is valid.
[in]local_portPortnumber that should used for incomming connection requests.
Returns
Zero on success
-EAFNOSUPPORT if local_addr != NULL and address_family is not supported.
-EINVAL if address_family is not the same the address_family use by the tcb.
-EISCONN if transmission control block is already in use.
-ENOMEM if the receive buffer for the tcb could not be allocated. Increase "GNRC_TCP_RCV_BUFFERS".
ssize_t gnrc_tcp_recv ( gnrc_tcp_tcb_t tcb,
void *  data,
const size_t  max_len,
const uint32_t  user_timeout_duration_us 
)
Precondition
gnrc_tcp_tcb_init() must have been successfully called.
tcb must not be NULL.
data must not be NULL.
Note
Function blocks if user_timeout_duration_us is not zero.
Parameters
[in,out]tcbThis connections Transmission control block.
[out]dataPointer to the buffer where the received data should be copied into.
[in]max_lenMaximum amount to bytes that should be reeived. Should not exceed size of data.
[in]user_timeout_duration_usTimeout for receive in microseconds. If zero and no data is available, the function returns immediately. If not zero the function block until data is available or user_timeout_duration_us microseconds have passed.
Returns
On success, the number of bytes read into data.
-ENOTCONN if connection is not established.
-EAGAIN if user_timeout_duration_us is zero and no data is available.
-ECONNRESET if connection was resetted by the peer.
-ECONNABORTED if the connection was aborted.
-ETIMEDOUT if user_timeout_duration_us expired.
ssize_t gnrc_tcp_send ( gnrc_tcp_tcb_t tcb,
const void *  data,
const size_t  len,
const uint32_t  user_timeout_duration_us 
)
Precondition
gnrc_tcp_tcb_init() must have been successfully called.
tcb must not be NULL.
data must not be NULL.
Note
Blocks until up to len bytes were transmitted or an error occured.
Parameters
[in,out]tcbThis connections Transmission control block.
[in]dataPointer to the data that should be transmitted.
[in]lenNumber of bytes that should be transmitted.
[in]user_timeout_duration_usIf not zero and there were not data transmitted successfully, the function call returns after user_timeout_duration_us. If zero not timeout will be triggered.
Returns
On success, the number of successfully transmitted bytes.
-ENOTCONN if connection is not established.
-ECONNRESET if connection was resetted by the peer.
-ECONNABORTED if the connection was aborted.
-ETIMEDOUT if user_timeout_duration_us expired.
void gnrc_tcp_tcb_init ( gnrc_tcp_tcb_t tcb)
Precondition
tcb must not be NULL.
Parameters
[in,out]tcbTransmission that should be initialized.