tcp.h
Go to the documentation of this file.
1 /**
2  * @file tcp.h
3  * @brief TCP (Transmission Control Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _TCP_H
32 #define _TCP_H
33 
34 //Dependencies
35 #include "net_config.h"
36 #include "core/ip.h"
37 
38 //TCP support
39 #ifndef TCP_SUPPORT
40  #define TCP_SUPPORT ENABLED
41 #elif (TCP_SUPPORT != ENABLED && TCP_SUPPORT != DISABLED)
42  #error TCP_SUPPORT parameter is not valid
43 #endif
44 
45 //TCP tick interval
46 #ifndef TCP_TICK_INTERVAL
47  #define TCP_TICK_INTERVAL 100
48 #elif (TCP_TICK_INTERVAL < 10)
49  #error TCP_TICK_INTERVAL parameter is not valid
50 #endif
51 
52 //Maximum segment size
53 #ifndef TCP_MAX_MSS
54  #define TCP_MAX_MSS 1430
55 #elif (TCP_MAX_MSS < 536)
56  #error TCP_MAX_MSS parameter is not valid
57 #endif
58 
59 //Mimimum acceptable segment size
60 #ifndef TCP_MIN_MSS
61  #define TCP_MIN_MSS 64
62 #elif (TCP_MIN_MSS < 1)
63  #error TCP_MIN_MSS parameter is not valid
64 #endif
65 
66 //Default buffer size for transmission
67 #ifndef TCP_DEFAULT_TX_BUFFER_SIZE
68  #define TCP_DEFAULT_TX_BUFFER_SIZE 2860
69 #elif (TCP_DEFAULT_TX_BUFFER_SIZE < 536)
70  #error TCP_DEFAULT_TX_BUFFER_SIZE parameter is not valid
71 #endif
72 
73 //Maximum acceptable size for the send buffer
74 #ifndef TCP_MAX_TX_BUFFER_SIZE
75  #define TCP_MAX_TX_BUFFER_SIZE 22880
76 #elif (TCP_MAX_TX_BUFFER_SIZE < 536)
77  #error TCP_MAX_TX_BUFFER_SIZE parameter is not valid
78 #endif
79 
80 //Default buffer size for reception
81 #ifndef TCP_DEFAULT_RX_BUFFER_SIZE
82  #define TCP_DEFAULT_RX_BUFFER_SIZE 2860
83 #elif (TCP_DEFAULT_RX_BUFFER_SIZE < 536)
84  #error TCP_DEFAULT_RX_BUFFER_SIZE parameter is not valid
85 #endif
86 
87 //Maximum acceptable size for the receive buffer
88 #ifndef TCP_MAX_RX_BUFFER_SIZE
89  #define TCP_MAX_RX_BUFFER_SIZE 22880
90 #elif (TCP_MAX_RX_BUFFER_SIZE < 536)
91  #error TCP_MAX_RX_BUFFER_SIZE parameter is not valid
92 #endif
93 
94 //Default SYN queue size for listening sockets
95 #ifndef TCP_DEFAULT_SYN_QUEUE_SIZE
96  #define TCP_DEFAULT_SYN_QUEUE_SIZE 4
97 #elif (TCP_DEFAULT_SYN_QUEUE_SIZE < 1)
98  #error TCP_DEFAULT_SYN_QUEUE_SIZE parameter is not valid
99 #endif
100 
101 //Maximum SYN queue size for listening sockets
102 #ifndef TCP_MAX_SYN_QUEUE_SIZE
103  #define TCP_MAX_SYN_QUEUE_SIZE 16
104 #elif (TCP_MAX_SYN_QUEUE_SIZE < 1)
105  #error TCP_MAX_SYN_QUEUE_SIZE parameter is not valid
106 #endif
107 
108 //Maximum number of retransmissions
109 #ifndef TCP_MAX_RETRIES
110  #define TCP_MAX_RETRIES 5
111 #elif (TCP_MAX_RETRIES < 1)
112  #error TCP_MAX_RETRIES parameter is not valid
113 #endif
114 
115 //Initial retransmission timeout
116 #ifndef TCP_INITIAL_RTO
117  #define TCP_INITIAL_RTO 1000
118 #elif (TCP_INITIAL_RTO < 100)
119  #error TCP_INITIAL_RTO parameter is not valid
120 #endif
121 
122 //Minimum retransmission timeout
123 #ifndef TCP_MIN_RTO
124  #define TCP_MIN_RTO 1000
125 #elif (TCP_MIN_RTO < 100)
126  #error TCP_MIN_RTO parameter is not valid
127 #endif
128 
129 //Maximum retransmission timeout
130 #ifndef TCP_MAX_RTO
131  #define TCP_MAX_RTO 60000
132 #elif (TCP_MAX_RTO < 1000)
133  #error TCP_MAX_RTO parameter is not valid
134 #endif
135 
136 //TCP congestion control
137 #ifndef TCP_CONGEST_CONTROL_SUPPORT
138  #define TCP_CONGEST_CONTROL_SUPPORT ENABLED
139 #elif (TCP_CONGEST_CONTROL_SUPPORT != ENABLED && TCP_CONGEST_CONTROL_SUPPORT != DISABLED)
140  #error TCP_CONGEST_CONTROL_SUPPORT parameter is not valid
141 #endif
142 
143 //Number of duplicate ACKs that triggers fast retransmit algorithm
144 #ifndef TCP_FAST_RETRANSMIT_THRES
145  #define TCP_FAST_RETRANSMIT_THRES 3
146 #elif (TCP_FAST_RETRANSMIT_THRES < 1)
147  #error TCP_FAST_RETRANSMIT_THRES parameter is not valid
148 #endif
149 
150 //Size of the congestion window after the three-way handshake is completed
151 #ifndef TCP_INITIAL_WINDOW
152  #define TCP_INITIAL_WINDOW 3
153 #elif (TCP_INITIAL_WINDOW < 1)
154  #error TCP_INITIAL_WINDOW parameter is not valid
155 #endif
156 
157 //Size of the congestion window after TCP detects loss using its retransmission timer
158 #ifndef TCP_LOSS_WINDOW
159  #define TCP_LOSS_WINDOW 1
160 #elif (TCP_LOSS_WINDOW < 1)
161  #error TCP_LOSS_WINDOW parameter is not valid
162 #endif
163 
164 //Default interval between successive window probes
165 #ifndef TCP_DEFAULT_PROBE_INTERVAL
166  #define TCP_DEFAULT_PROBE_INTERVAL 1000
167 #elif (TCP_DEFAULT_PROBE_INTERVAL < 100)
168  #error TCP_DEFAULT_PROBE_INTERVAL parameter is not valid
169 #endif
170 
171 //Maximum interval between successive window probes
172 #ifndef TCP_MAX_PROBE_INTERVAL
173  #define TCP_MAX_PROBE_INTERVAL 60000
174 #elif (TCP_MAX_PROBE_INTERVAL < 1000)
175  #error TCP_MAX_PROBE_INTERVAL parameter is not valid
176 #endif
177 
178 //Override timeout (should be in the range 0.1 to 1 seconds)
179 #ifndef TCP_OVERRIDE_TIMEOUT
180  #define TCP_OVERRIDE_TIMEOUT 500
181 #elif (TCP_OVERRIDE_TIMEOUT < 100)
182  #error TCP_OVERRIDE_TIMEOUT parameter is not valid
183 #endif
184 
185 //FIN-WAIT-2 timer
186 #ifndef TCP_FIN_WAIT_2_TIMER
187  #define TCP_FIN_WAIT_2_TIMER 4000
188 #elif (TCP_FIN_WAIT_2_TIMER < 1000)
189  #error TCP_FIN_WAIT_2_TIMER parameter is not valid
190 #endif
191 
192 //TIME-WAIT timer
193 #ifndef TCP_2MSL_TIMER
194  #define TCP_2MSL_TIMER 4000
195 #elif (TCP_2MSL_TIMER < 0)
196  #error TCP_2MSL_TIMER parameter is not valid
197 #endif
198 
199 //Selective acknowledgment support
200 #ifndef TCP_SACK_SUPPORT
201  #define TCP_SACK_SUPPORT DISABLED
202 #elif (TCP_SACK_SUPPORT != ENABLED && TCP_SACK_SUPPORT != DISABLED)
203  #error TCP_SACK_SUPPORT parameter is not valid
204 #endif
205 
206 //Number of SACK blocks
207 #ifndef TCP_MAX_SACK_BLOCKS
208  #define TCP_MAX_SACK_BLOCKS 4
209 #elif (TCP_MAX_SACK_BLOCKS < 1)
210  #error TCP_MAX_SACK_BLOCKS parameter is not valid
211 #endif
212 
213 //Maximum TCP header length
214 #define TCP_MAX_HEADER_LENGTH 60
215 //Default maximum segment size
216 #define TCP_DEFAULT_MSS 536
217 
218 //Sequence number comparison macro
219 #define TCP_CMP_SEQ(a, b) ((int32_t) ((a) - (b)))
220 
221 //C++ guard
222 #ifdef __cplusplus
223 extern "C" {
224 #endif
225 
226 
227 /**
228  * @brief TCP FSM states
229  **/
230 
231 typedef enum
232 {
244 } TcpState;
245 
246 
247 /**
248  * @brief TCP congestion states
249  **/
250 
251 typedef enum
252 {
257 
258 
259 /**
260  * @brief TCP control flags
261  **/
262 
263 typedef enum
264 {
265  TCP_FLAG_FIN = 0x01,
266  TCP_FLAG_SYN = 0x02,
267  TCP_FLAG_RST = 0x04,
268  TCP_FLAG_PSH = 0x08,
269  TCP_FLAG_ACK = 0x10,
271 } TcpFlags;
272 
273 
274 /**
275  * @brief TCP option types
276  **/
277 
278 typedef enum
279 {
287 } TcpOptionKind;
288 
289 
290 //CodeWarrior or Win32 compiler?
291 #if defined(__CWCC__) || defined(_WIN32)
292  #pragma pack(push, 1)
293 #endif
294 
295 
296 /**
297  * @brief TCP header
298  **/
299 
300 typedef __start_packed struct
301 {
302  uint16_t srcPort; //0-1
303  uint16_t destPort; //2-3
304  uint32_t seqNum; //4-7
305  uint32_t ackNum; //8-11
306 #ifdef _CPU_BIG_ENDIAN
307  uint8_t dataOffset : 4; //12
308  uint8_t reserved1 : 4;
309  uint8_t reserved2 : 2; //13
310  uint8_t flags : 6;
311 #else
312  uint8_t reserved1 : 4; //12
313  uint8_t dataOffset : 4;
314  uint8_t flags : 6; //13
315  uint8_t reserved2 : 2;
316 #endif
317  uint16_t window; //14-15
318  uint16_t checksum; //16-17
319  uint16_t urgentPointer; //18-19
320  uint8_t options[]; //20
322 
323 
324 /**
325  * @brief TCP option
326  **/
327 
328 typedef __start_packed struct
329 {
330  uint8_t kind;
331  uint8_t length;
332  uint8_t value[];
334 
335 
336 //CodeWarrior or Win32 compiler?
337 #if defined(__CWCC__) || defined(_WIN32)
338  #pragma pack(pop)
339 #endif
340 
341 
342 /**
343  * @brief TCP timer
344  **/
345 
346 typedef struct
347 {
351 } TcpTimer;
352 
353 
354 /**
355  * @brief Retransmission queue item
356  **/
357 
358 typedef struct _TcpQueueItem
359 {
365 } TcpQueueItem;
366 
367 
368 /**
369  * @brief SYN queue item
370  **/
371 
372 typedef struct _TcpSynQueueItem
373 {
377  uint16_t srcPort;
379  uint32_t isn;
380  uint16_t mss;
382 
383 
384 /**
385  * @brief SACK block
386  **/
387 
388 typedef struct
389 {
390  uint32_t leftEdge;
391  uint32_t rightEdge;
392 } TcpSackBlock;
393 
394 
395 /**
396  * @brief Transmit buffer
397  **/
398 
399 typedef struct
400 {
404 } TcpTxBuffer;
405 
406 
407 /**
408  * @brief Receive buffer
409  **/
410 
411 typedef struct
412 {
416 } TcpRxBuffer;
417 
418 
419 //Tick counter to handle periodic operations
421 
422 //TCP related functions
423 error_t tcpInit(void);
424 uint16_t tcpGetDynamicPort(void);
425 
426 error_t tcpConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort);
428 Socket *tcpAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort);
429 
430 error_t tcpSend(Socket *socket, const uint8_t *data,
431  size_t length, size_t *written, uint_t flags);
432 
433 error_t tcpReceive(Socket *socket, uint8_t *data,
434  size_t size, size_t *received, uint_t flags);
435 
438 
440 
442 
443 //C++ guard
444 #ifdef __cplusplus
445 }
446 #endif
447 
448 #endif
__start_packed struct @121 TcpOption
TCP option.
Retransmission queue item.
Definition: tcp.h:358
int bool_t
Definition: compiler_port.h:49
uint_t chunkCount
Definition: tcp.h:401
#define TCP_MAX_HEADER_LENGTH
Definition: tcp.h:214
uint_t sacked
Definition: tcp.h:362
uint8_t header[TCP_MAX_HEADER_LENGTH]
Definition: tcp.h:364
error_t tcpReceive(Socket *socket, uint8_t *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: tcp.c:574
IP network address.
Definition: ip.h:71
TcpCongestState
TCP congestion states.
Definition: tcp.h:251
__start_packed struct @120 TcpHeader
TCP header.
uint16_t checksum
Definition: tcp.h:318
uint16_t mss
Definition: tcp.h:380
IpAddr srcAddr
Definition: tcp.h:376
TcpFlags
TCP control flags.
Definition: tcp.h:263
uint32_t rightEdge
Definition: tcp.h:391
IpAddr destAddr
Definition: tcp.h:378
uint16_t destPort
Definition: tcp.h:303
uint16_t srcPort
Definition: tcp.h:377
struct _TcpSynQueueItem * next
Definition: tcp.h:374
systime_t interval
Definition: tcp.h:350
uint16_t window
Definition: tcp.h:317
#define N(size)
Definition: net_mem.h:64
uint_t length
Definition: tcp.h:361
IP pseudo header.
Definition: ip.h:90
TcpOptionKind
TCP option types.
Definition: tcp.h:278
TcpState
TCP FSM states.
Definition: tcp.h:231
uint_t chunkCount
Definition: tcp.h:413
error_t
Error codes.
Definition: error.h:42
uint32_t seqNum
Definition: tcp.h:304
Structure describing a chunk of data.
Definition: net_mem.h:76
error_t tcpListen(Socket *socket, uint_t backlog)
Place a socket in the listening state.
Definition: tcp.c:236
#define TCP_MAX_RX_BUFFER_SIZE
Definition: tcp.h:89
struct _TcpQueueItem * next
Definition: tcp.h:360
TcpState tcpGetState(Socket *socket)
Get the current state of the TCP FSM.
Definition: tcp.c:937
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:108
uint8_t value[]
Definition: tcp.h:332
uint8_t reserved1
Definition: tcp.h:312
#define NetInterface
Definition: net.h:36
NetInterface * interface
Definition: tcp.h:375
systime_t tcpTickCounter
Definition: tcp.c:49
uint8_t options[]
Definition: tcp.h:320
__start_packed struct _Ipv4Header __end_packed
error_t tcpAbort(Socket *socket)
Abort an existing TCP connection.
Definition: tcp.c:873
uint8_t kind
Definition: tcp.h:330
uint16_t tcpGetDynamicPort(void)
Get an ephemeral port number.
Definition: tcp.c:75
uint_t maxChunkCount
Definition: tcp.h:414
uint32_t isn
Definition: tcp.h:379
#define TCP_MAX_TX_BUFFER_SIZE
Definition: tcp.h:75
uint8_t flags
Definition: tcp.h:314
IpPseudoHeader pseudoHeader
Definition: tcp.h:363
struct _TcpSynQueueItem TcpSynQueueItem
SYN queue item.
systime_t startTime
Definition: tcp.h:349
SACK block.
Definition: tcp.h:388
uint32_t ackNum
Definition: tcp.h:305
IPv4 and IPv6 common routines.
#define Socket
Definition: socket.h:36
TCP timer.
Definition: tcp.h:346
uint8_t dataOffset
Definition: tcp.h:313
bool_t running
Definition: tcp.h:348
uint32_t leftEdge
Definition: tcp.h:390
uint8_t reserved2
Definition: tcp.h:315
SYN queue item.
Definition: tcp.h:372
error_t tcpInit(void)
TCP related initialization.
Definition: tcp.c:60
uint_t maxChunkCount
Definition: tcp.h:402
error_t tcpConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a TCP connection.
Definition: tcp.c:115
struct _TcpQueueItem TcpQueueItem
Retransmission queue item.
uint16_t srcPort
Definition: tcp.h:302
Receive buffer.
Definition: tcp.h:411
Transmit buffer.
Definition: tcp.h:399
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
Socket * tcpAccept(Socket *socket, IpAddr *clientIpAddr, uint16_t *clientPort)
Permit an incoming connection attempt on a TCP socket.
Definition: tcp.c:263
uint32_t systime_t
Definition: compiler_port.h:46
uint8_t length
Definition: tcp.h:331
error_t tcpShutdown(Socket *socket, uint_t how)
Shutdown gracefully reception, transmission, or both.
Definition: tcp.c:722
Socket * tcpKillOldestConnection(void)
Kill the oldest socket in the TIME-WAIT state.
Definition: tcp.c:961
uint16_t urgentPointer
Definition: tcp.h:319
error_t tcpSend(Socket *socket, const uint8_t *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: tcp.c:449