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