TCP (Transmission Control Protocol) More...

#include "net_config.h"
#include "core/ip.h"

Go to the source code of this file.

Data Structures

struct  _TcpQueueItem
 Retransmission queue item. More...
 
struct  _TcpSynQueueItem
 SYN queue item. More...
 
struct  TcpSackBlock
 SACK block. More...
 
struct  TcpTxBuffer
 Transmit buffer. More...
 
struct  TcpRxBuffer
 Receive buffer. More...
 

Macros

#define TCP_SUPPORT   ENABLED
 
#define TCP_TICK_INTERVAL   100
 
#define TCP_MAX_MSS   1430
 
#define TCP_MIN_MSS   64
 
#define TCP_DEFAULT_TX_BUFFER_SIZE   2860
 
#define TCP_MAX_TX_BUFFER_SIZE   22880
 
#define TCP_DEFAULT_RX_BUFFER_SIZE   2860
 
#define TCP_MAX_RX_BUFFER_SIZE   22880
 
#define TCP_DEFAULT_SYN_QUEUE_SIZE   4
 
#define TCP_MAX_SYN_QUEUE_SIZE   16
 
#define TCP_MAX_RETRIES   5
 
#define TCP_INITIAL_RTO   1000
 
#define TCP_MIN_RTO   1000
 
#define TCP_MAX_RTO   60000
 
#define TCP_SECURE_ISN_SUPPORT   DISABLED
 
#define TCP_CONGEST_CONTROL_SUPPORT   ENABLED
 
#define TCP_FAST_RETRANSMIT_THRES   3
 
#define TCP_INITIAL_WINDOW   3
 
#define TCP_LOSS_WINDOW   1
 
#define TCP_DEFAULT_PROBE_INTERVAL   1000
 
#define TCP_MAX_PROBE_INTERVAL   60000
 
#define TCP_OVERRIDE_TIMEOUT   500
 
#define TCP_FIN_WAIT_2_TIMER   4000
 
#define TCP_2MSL_TIMER   4000
 
#define TCP_KEEP_ALIVE_SUPPORT   DISABLED
 
#define TCP_DEFAULT_KEEP_ALIVE_IDLE   60000
 
#define TCP_DEFAULT_KEEP_ALIVE_INTERVAL   15000
 
#define TCP_DEFAULT_KEEP_ALIVE_PROBES   5
 
#define TCP_SACK_SUPPORT   DISABLED
 
#define TCP_MAX_SACK_BLOCKS   4
 
#define TCP_MAX_HEADER_LENGTH   60
 
#define TCP_DEFAULT_MSS   536
 
#define TCP_CMP_SEQ(a, b)   ((int32_t) ((a) - (b)))
 

Typedefs

typedef struct _TcpQueueItem TcpQueueItem
 Retransmission queue item. More...
 
typedef struct _TcpSynQueueItem TcpSynQueueItem
 SYN queue item. More...
 

Enumerations

enum  TcpState {
  TCP_STATE_CLOSED = 0 , TCP_STATE_LISTEN = 1 , TCP_STATE_SYN_SENT = 2 , TCP_STATE_SYN_RECEIVED = 3 ,
  TCP_STATE_ESTABLISHED = 4 , TCP_STATE_CLOSE_WAIT = 5 , TCP_STATE_LAST_ACK = 6 , TCP_STATE_FIN_WAIT_1 = 7 ,
  TCP_STATE_FIN_WAIT_2 = 8 , TCP_STATE_CLOSING = 9 , TCP_STATE_TIME_WAIT = 10
}
 TCP FSM states. More...
 
enum  TcpCongestState { TCP_CONGEST_STATE_IDLE = 0 , TCP_CONGEST_STATE_RECOVERY = 1 , TCP_CONGEST_STATE_LOSS_RECOVERY = 2 }
 TCP congestion states. More...
 
enum  TcpFlags {
  TCP_FLAG_FIN = 0x01 , TCP_FLAG_SYN = 0x02 , TCP_FLAG_RST = 0x04 , TCP_FLAG_PSH = 0x08 ,
  TCP_FLAG_ACK = 0x10 , TCP_FLAG_URG = 0x20
}
 TCP control flags. More...
 
enum  TcpOptionKind {
  TCP_OPTION_END = 0 , TCP_OPTION_NOP = 1 , TCP_OPTION_MAX_SEGMENT_SIZE = 2 , TCP_OPTION_WINDOW_SCALE_FACTOR = 3 ,
  TCP_OPTION_SACK_PERMITTED = 4 , TCP_OPTION_SACK = 5 , TCP_OPTION_TIMESTAMP = 8
}
 TCP option types. More...
 

Functions

error_t tcpInit (void)
 TCP related initialization. More...
 
error_t tcpSetInitialRto (NetInterface *interface, systime_t initialRto)
 Set TCP initial retransmission timeout. More...
 
uint16_t tcpGetDynamicPort (void)
 Get an ephemeral port number. More...
 
error_t tcpConnect (Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
 Establish a TCP connection. More...
 
error_t tcpListen (Socket *socket, uint_t backlog)
 Place a socket in the listening state. More...
 
SockettcpAccept (Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
 Permit an incoming connection attempt on a TCP socket. More...
 
error_t tcpSend (Socket *socket, const uint8_t *data, size_t length, size_t *written, uint_t flags)
 Send data to a connected socket. More...
 
error_t tcpReceive (Socket *socket, uint8_t *data, size_t size, size_t *received, uint_t flags)
 Receive data from a connected socket. More...
 
error_t tcpShutdown (Socket *socket, uint_t how)
 Shutdown gracefully reception, transmission, or both. More...
 
error_t tcpAbort (Socket *socket)
 Abort an existing TCP connection. More...
 
TcpState tcpGetState (Socket *socket)
 Get the current state of the TCP FSM. More...
 
SockettcpKillOldestConnection (void)
 Kill the oldest socket in the TIME-WAIT state. More...
 

Variables

typedef __packed_struct
 TCP header. More...
 
uint16_t destPort
 
uint32_t seqNum
 
uint32_t ackNum
 
uint8_t reserved1
 
uint8_t dataOffset
 
uint8_t flags
 
uint8_t reserved2
 
uint16_t window
 
uint16_t checksum
 
uint16_t urgentPointer
 
uint8_t options []
 
 TcpHeader
 
uint8_t length
 
uint8_t value []
 
 TcpOption
 
systime_t tcpTickCounter
 

Detailed Description

TCP (Transmission Control Protocol)

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.0

Definition in file tcp.h.

Macro Definition Documentation

◆ TCP_2MSL_TIMER

#define TCP_2MSL_TIMER   4000

Definition at line 201 of file tcp.h.

◆ TCP_CMP_SEQ

#define TCP_CMP_SEQ (   a,
  b 
)    ((int32_t) ((a) - (b)))

Definition at line 254 of file tcp.h.

◆ TCP_CONGEST_CONTROL_SUPPORT

#define TCP_CONGEST_CONTROL_SUPPORT   ENABLED

Definition at line 145 of file tcp.h.

◆ TCP_DEFAULT_KEEP_ALIVE_IDLE

#define TCP_DEFAULT_KEEP_ALIVE_IDLE   60000

Definition at line 215 of file tcp.h.

◆ TCP_DEFAULT_KEEP_ALIVE_INTERVAL

#define TCP_DEFAULT_KEEP_ALIVE_INTERVAL   15000

Definition at line 222 of file tcp.h.

◆ TCP_DEFAULT_KEEP_ALIVE_PROBES

#define TCP_DEFAULT_KEEP_ALIVE_PROBES   5

Definition at line 229 of file tcp.h.

◆ TCP_DEFAULT_MSS

#define TCP_DEFAULT_MSS   536

Definition at line 251 of file tcp.h.

◆ TCP_DEFAULT_PROBE_INTERVAL

#define TCP_DEFAULT_PROBE_INTERVAL   1000

Definition at line 173 of file tcp.h.

◆ TCP_DEFAULT_RX_BUFFER_SIZE

#define TCP_DEFAULT_RX_BUFFER_SIZE   2860

Definition at line 82 of file tcp.h.

◆ TCP_DEFAULT_SYN_QUEUE_SIZE

#define TCP_DEFAULT_SYN_QUEUE_SIZE   4

Definition at line 96 of file tcp.h.

◆ TCP_DEFAULT_TX_BUFFER_SIZE

#define TCP_DEFAULT_TX_BUFFER_SIZE   2860

Definition at line 68 of file tcp.h.

◆ TCP_FAST_RETRANSMIT_THRES

#define TCP_FAST_RETRANSMIT_THRES   3

Definition at line 152 of file tcp.h.

◆ TCP_FIN_WAIT_2_TIMER

#define TCP_FIN_WAIT_2_TIMER   4000

Definition at line 194 of file tcp.h.

◆ TCP_INITIAL_RTO

#define TCP_INITIAL_RTO   1000

Definition at line 117 of file tcp.h.

◆ TCP_INITIAL_WINDOW

#define TCP_INITIAL_WINDOW   3

Definition at line 159 of file tcp.h.

◆ TCP_KEEP_ALIVE_SUPPORT

#define TCP_KEEP_ALIVE_SUPPORT   DISABLED

Definition at line 208 of file tcp.h.

◆ TCP_LOSS_WINDOW

#define TCP_LOSS_WINDOW   1

Definition at line 166 of file tcp.h.

◆ TCP_MAX_HEADER_LENGTH

#define TCP_MAX_HEADER_LENGTH   60

Definition at line 249 of file tcp.h.

◆ TCP_MAX_MSS

#define TCP_MAX_MSS   1430

Definition at line 54 of file tcp.h.

◆ TCP_MAX_PROBE_INTERVAL

#define TCP_MAX_PROBE_INTERVAL   60000

Definition at line 180 of file tcp.h.

◆ TCP_MAX_RETRIES

#define TCP_MAX_RETRIES   5

Definition at line 110 of file tcp.h.

◆ TCP_MAX_RTO

#define TCP_MAX_RTO   60000

Definition at line 131 of file tcp.h.

◆ TCP_MAX_RX_BUFFER_SIZE

#define TCP_MAX_RX_BUFFER_SIZE   22880

Definition at line 89 of file tcp.h.

◆ TCP_MAX_SACK_BLOCKS

#define TCP_MAX_SACK_BLOCKS   4

Definition at line 243 of file tcp.h.

◆ TCP_MAX_SYN_QUEUE_SIZE

#define TCP_MAX_SYN_QUEUE_SIZE   16

Definition at line 103 of file tcp.h.

◆ TCP_MAX_TX_BUFFER_SIZE

#define TCP_MAX_TX_BUFFER_SIZE   22880

Definition at line 75 of file tcp.h.

◆ TCP_MIN_MSS

#define TCP_MIN_MSS   64

Definition at line 61 of file tcp.h.

◆ TCP_MIN_RTO

#define TCP_MIN_RTO   1000

Definition at line 124 of file tcp.h.

◆ TCP_OVERRIDE_TIMEOUT

#define TCP_OVERRIDE_TIMEOUT   500

Definition at line 187 of file tcp.h.

◆ TCP_SACK_SUPPORT

#define TCP_SACK_SUPPORT   DISABLED

Definition at line 236 of file tcp.h.

◆ TCP_SECURE_ISN_SUPPORT

#define TCP_SECURE_ISN_SUPPORT   DISABLED

Definition at line 138 of file tcp.h.

◆ TCP_SUPPORT

#define TCP_SUPPORT   ENABLED

Definition at line 40 of file tcp.h.

◆ TCP_TICK_INTERVAL

#define TCP_TICK_INTERVAL   100

Definition at line 47 of file tcp.h.

Typedef Documentation

◆ TcpQueueItem

typedef struct _TcpQueueItem TcpQueueItem

Retransmission queue item.

◆ TcpSynQueueItem

SYN queue item.

Enumeration Type Documentation

◆ TcpCongestState

TCP congestion states.

Enumerator
TCP_CONGEST_STATE_IDLE 
TCP_CONGEST_STATE_RECOVERY 
TCP_CONGEST_STATE_LOSS_RECOVERY 

Definition at line 286 of file tcp.h.

◆ TcpFlags

enum TcpFlags

TCP control flags.

Enumerator
TCP_FLAG_FIN 
TCP_FLAG_SYN 
TCP_FLAG_RST 
TCP_FLAG_PSH 
TCP_FLAG_ACK 
TCP_FLAG_URG 

Definition at line 298 of file tcp.h.

◆ TcpOptionKind

TCP option types.

Enumerator
TCP_OPTION_END 
TCP_OPTION_NOP 
TCP_OPTION_MAX_SEGMENT_SIZE 
TCP_OPTION_WINDOW_SCALE_FACTOR 
TCP_OPTION_SACK_PERMITTED 
TCP_OPTION_SACK 
TCP_OPTION_TIMESTAMP 

Definition at line 313 of file tcp.h.

◆ TcpState

enum TcpState

TCP FSM states.

Enumerator
TCP_STATE_CLOSED 
TCP_STATE_LISTEN 
TCP_STATE_SYN_SENT 
TCP_STATE_SYN_RECEIVED 
TCP_STATE_ESTABLISHED 
TCP_STATE_CLOSE_WAIT 
TCP_STATE_LAST_ACK 
TCP_STATE_FIN_WAIT_1 
TCP_STATE_FIN_WAIT_2 
TCP_STATE_CLOSING 
TCP_STATE_TIME_WAIT 

Definition at line 266 of file tcp.h.

Function Documentation

◆ tcpAbort()

error_t tcpAbort ( Socket socket)

Abort an existing TCP connection.

Parameters
[in]socketHandle identifying the socket to close
Returns
Error code

Definition at line 958 of file tcp.c.

◆ tcpAccept()

Socket* tcpAccept ( Socket socket,
IpAddr clientIpAddr,
uint16_t *  clientPort 
)

Permit an incoming connection attempt on a TCP socket.

Parameters
[in]socketHandle to a socket previously placed in a listening state
[out]clientIpAddrIP address of the client
[out]clientPortPort number used by the client
Returns
Handle to the socket in which the actual connection is made

Definition at line 321 of file tcp.c.

◆ tcpConnect()

error_t tcpConnect ( Socket socket,
const IpAddr remoteIpAddr,
uint16_t  remotePort 
)

Establish a TCP connection.

Parameters
[in]socketHandle to an unconnected socket
[in]remoteIpAddrIP address of the remote host
[in]remotePortRemote port number that will be used to establish the connection
Returns
Error code

Definition at line 146 of file tcp.c.

◆ tcpGetDynamicPort()

uint16_t tcpGetDynamicPort ( void  )

Get an ephemeral port number.

Returns
Ephemeral port

Definition at line 106 of file tcp.c.

◆ tcpGetState()

TcpState tcpGetState ( Socket socket)

Get the current state of the TCP FSM.

Parameters
[in]socketHandle identifying the socket
Returns
TCP FSM state

Definition at line 1021 of file tcp.c.

◆ tcpInit()

error_t tcpInit ( void  )

TCP related initialization.

Returns
Error code

Definition at line 60 of file tcp.c.

◆ tcpKillOldestConnection()

Socket* tcpKillOldestConnection ( void  )

Kill the oldest socket in the TIME-WAIT state.

Returns
Handle identifying the oldest TCP connection in the TIME-WAIT state. NULL is returned if no socket is currently in the TIME-WAIT state

Definition at line 1045 of file tcp.c.

◆ tcpListen()

error_t tcpListen ( Socket socket,
uint_t  backlog 
)

Place a socket in the listening state.

Place a socket in a state in which it is listening for an incoming connection

Parameters
[in]socketSocket to place in the listening state
[in]backlogbacklog The maximum length of the pending connection queue. If this parameter is zero, then the default backlog value is used instead
Returns
Error code

Definition at line 294 of file tcp.c.

◆ tcpReceive()

error_t tcpReceive ( Socket socket,
uint8_t *  data,
size_t  size,
size_t *  received,
uint_t  flags 
)

Receive data from a connected socket.

Parameters
[in]socketHandle that identifies a connected socket
[out]dataBuffer where to store the incoming data
[in]sizeMaximum number of bytes that can be received
[out]receivedNumber of bytes that have been received
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 659 of file tcp.c.

◆ tcpSend()

error_t tcpSend ( Socket socket,
const uint8_t *  data,
size_t  length,
size_t *  written,
uint_t  flags 
)

Send data to a connected socket.

Parameters
[in]socketHandle that identifies a connected socket
[in]dataPointer to a buffer containing the data to be transmitted
[in]lengthNumber of bytes to be transmitted
[out]writtenActual number of bytes written (optional parameter)
[in]flagsSet of flags that influences the behavior of this function
Returns
Error code

Definition at line 532 of file tcp.c.

◆ tcpSetInitialRto()

error_t tcpSetInitialRto ( NetInterface interface,
systime_t  initialRto 
)

Set TCP initial retransmission timeout.

Parameters
[in]interfaceUnderlying network interface
[in]initialRtoTCP initial RTO value, in milliseconds
Returns
Error code

Definition at line 77 of file tcp.c.

◆ tcpShutdown()

error_t tcpShutdown ( Socket socket,
uint_t  how 
)

Shutdown gracefully reception, transmission, or both.

Note that socketShutdown() does not close the socket, and resources attached to the socket will not be freed until socketClose() is invoked

Parameters
[in]socketHandle to a socket
[in]howFlag that describes what types of operation will no longer be allowed
Returns
Error code

Definition at line 820 of file tcp.c.

Variable Documentation

◆ __packed_struct

typedef __packed_struct
Initial value:
{
uint16_t srcPort

TCP header.

TCP option.

Definition at line 337 of file tcp.h.

◆ ackNum

uint32_t ackNum

Definition at line 342 of file tcp.h.

◆ checksum

uint16_t checksum

Definition at line 355 of file tcp.h.

◆ dataOffset

uint8_t dataOffset

Definition at line 350 of file tcp.h.

◆ destPort

uint16_t destPort

Definition at line 340 of file tcp.h.

◆ flags

uint8_t flags

Definition at line 351 of file tcp.h.

◆ length

uint16_t length

Definition at line 368 of file tcp.h.

◆ options

uint8_t options

Definition at line 357 of file tcp.h.

◆ reserved1

uint8_t reserved1

Definition at line 349 of file tcp.h.

◆ reserved2

uint16_t reserved2

Definition at line 352 of file tcp.h.

◆ seqNum

uint32_t seqNum

Definition at line 341 of file tcp.h.

◆ TcpHeader

TcpHeader

Definition at line 358 of file tcp.h.

◆ TcpOption

TcpOption

Definition at line 370 of file tcp.h.

◆ tcpTickCounter

systime_t tcpTickCounter
extern

Definition at line 49 of file tcp.c.

◆ urgentPointer

uint16_t urgentPointer

Definition at line 356 of file tcp.h.

◆ value

uint8_t value

Definition at line 369 of file tcp.h.

◆ window

uint16_t window

Definition at line 354 of file tcp.h.