Go to the documentation of this file.
1 /**
2  * @file tcp_misc.h
3  * @brief Helper functions for TCP
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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
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 (
28  * @version 2.4.0
29  **/
31 #ifndef _TCP_MISC_H
32 #define _TCP_MISC_H
34 //Dependencies
35 #include "core/tcp.h"
37 //C++ guard
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 //TCP related functions
43 error_t tcpSendSegment(Socket *socket, uint8_t flags, uint32_t seqNum,
44  uint32_t ackNum, size_t length, bool_t addToQueue);
49  const IpPseudoHeader *pseudoHeader, const TcpHeader *segment, size_t length);
51 error_t tcpAddOption(TcpHeader *segment, uint8_t kind, const void *value,
52  uint8_t length);
54 const TcpOption *tcpGetOption(const TcpHeader *segment, uint8_t kind);
56 uint32_t tcpGenerateInitialSeqNum(const IpAddr *localIpAddr,
57  uint16_t localPort, const IpAddr *remoteIpAddr, uint16_t remotePort);
59 error_t tcpCheckSeqNum(Socket *socket, const TcpHeader *segment, size_t length);
60 error_t tcpCheckSyn(Socket *socket, const TcpHeader *segment, size_t length);
61 error_t tcpCheckAck(Socket *socket, const TcpHeader *segment, size_t length);
64  const TcpHeader *segment);
67  size_t length);
70 void tcpFastRecovery(Socket *socket, const TcpHeader *segment, uint_t n);
71 void tcpFastLossRecovery(Socket *socket, const TcpHeader *segment);
73 void tcpProcessSegmentData(Socket *socket, const TcpHeader *segment,
74  const NetBuffer *buffer, size_t offset, size_t length);
83 void tcpUpdateSackBlocks(Socket *socket, uint32_t *leftEdge, uint32_t *rightEdge);
84 void tcpUpdateSendWindow(Socket *socket, const TcpHeader *segment);
91 void tcpChangeState(Socket *socket, TcpState newState);
94 uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout);
96 void tcpWriteTxBuffer(Socket *socket, uint32_t seqNum,
97  const uint8_t *data, size_t length);
100  NetBuffer *buffer, size_t length);
102 void tcpWriteRxBuffer(Socket *socket, uint32_t seqNum,
103  const NetBuffer *data, size_t dataOffset, size_t length);
105 void tcpReadRxBuffer(Socket *socket, uint32_t seqNum, uint8_t *data,
106  size_t length);
108 void tcpDumpHeader(const TcpHeader *segment, size_t length, uint32_t iss,
109  uint32_t irs);
111 //C++ guard
112 #ifdef __cplusplus
113 }
114 #endif
116 #endif
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:65
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
uint8_t n
Error codes.
Definition: error.h:43
uint8_t data[]
Definition: ethernet.h:222
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
#define Socket
Definition: socket.h:36
IP network address.
Definition: ip.h:79
IP pseudo header.
Definition: ip.h:99
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
TCP (Transmission Control Protocol)
uint32_t ackNum
Definition: tcp.h:342
uint8_t length
Definition: tcp.h:368
uint8_t value[]
Definition: tcp.h:369
Definition: tcp.h:358
Definition: tcp.h:370
TCP FSM states.
Definition: tcp.h:267
uint32_t seqNum
Definition: tcp.h:341
uint8_t flags
Definition: tcp.h:351
uint8_t dataOffset
Definition: tcp.h:350
uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout)
Wait for a particular TCP event.
Definition: tcp_misc.c:2200
void tcpFastRetransmit(Socket *socket)
Fast retransmit procedure.
Definition: tcp_misc.c:1177
void tcpUpdateRetransmitQueue(Socket *socket)
Remove acknowledged segments from retransmission queue.
Definition: tcp_misc.c:1392
void tcpUpdateEvents(Socket *socket)
Update TCP related events.
Definition: tcp_misc.c:2052
bool_t tcpIsDuplicateSyn(Socket *socket, const IpPseudoHeader *pseudoHeader, const TcpHeader *segment)
Test whether the incoming SYN segment is a duplicate.
Definition: tcp_misc.c:1058
error_t tcpCheckAck(Socket *socket, const TcpHeader *segment, size_t length)
Test the ACK field of an incoming segment.
Definition: tcp_misc.c:844
void tcpFlushRetransmitQueue(Socket *socket)
Flush retransmission queue.
Definition: tcp_misc.c:1474
bool_t tcpComputeRto(Socket *socket)
Compute retransmission timeout.
Definition: tcp_misc.c:1675
void tcpFastLossRecovery(Socket *socket, const TcpHeader *segment)
Fast loss recovery procedure.
Definition: tcp_misc.c:1265
error_t tcpRejectSegment(NetInterface *interface, const IpPseudoHeader *pseudoHeader, const TcpHeader *segment, size_t length)
Send a TCP reset in response to an invalid segment.
Definition: tcp_misc.c:404
error_t tcpCheckSyn(Socket *socket, const TcpHeader *segment, size_t length)
Check the SYN bit of an incoming segment.
Definition: tcp_misc.c:810
void tcpDumpHeader(const TcpHeader *segment, size_t length, uint32_t iss, uint32_t irs)
Dump TCP header for debugging purpose.
Definition: tcp_misc.c:2388
void tcpUpdateSendWindow(Socket *socket, const TcpHeader *segment)
Update send window.
Definition: tcp_misc.c:1586
error_t tcpSendResetSegment(Socket *socket, uint32_t seqNum)
Send a TCP reset segment.
Definition: tcp_misc.c:371
bool_t tcpIsDuplicateAck(Socket *socket, const TcpHeader *segment, size_t length)
Test whether the incoming acknowledgment is a duplicate.
Definition: tcp_misc.c:1134
void tcpWriteRxBuffer(Socket *socket, uint32_t seqNum, const NetBuffer *data, size_t dataOffset, size_t length)
Copy incoming data to the receive buffer.
Definition: tcp_misc.c:2318
void tcpWriteTxBuffer(Socket *socket, uint32_t seqNum, const uint8_t *data, size_t length)
Copy incoming data to the send buffer.
Definition: tcp_misc.c:2238
void tcpFastRecovery(Socket *socket, const TcpHeader *segment, uint_t n)
Fast recovery procedure.
Definition: tcp_misc.c:1216
void tcpUpdateSackBlocks(Socket *socket, uint32_t *leftEdge, uint32_t *rightEdge)
Update the list of non-contiguous blocks that have been received.
Definition: tcp_misc.c:1531
void tcpUpdateReceiveWindow(Socket *socket)
Update receive window so as to avoid Silly Window Syndrome.
Definition: tcp_misc.c:1636
error_t tcpCheckSeqNum(Socket *socket, const TcpHeader *segment, size_t length)
Test the sequence number of an incoming segment.
Definition: tcp_misc.c:719
void tcpReadRxBuffer(Socket *socket, uint32_t seqNum, uint8_t *data, size_t length)
Copy data from the receive buffer.
Definition: tcp_misc.c:2353
error_t tcpAddOption(TcpHeader *segment, uint8_t kind, const void *value, uint8_t length)
Append an option to the TCP header.
Definition: tcp_misc.c:554
error_t tcpNagleAlgo(Socket *socket, uint_t flags)
Nagle algorithm implementation.
Definition: tcp_misc.c:1900
error_t tcpSendSegment(Socket *socket, uint8_t flags, uint32_t seqNum, uint32_t ackNum, size_t length, bool_t addToQueue)
Send a TCP segment.
Definition: tcp_misc.c:68
uint32_t tcpGenerateInitialSeqNum(const IpAddr *localIpAddr, uint16_t localPort, const IpAddr *remoteIpAddr, uint16_t remotePort)
Initial sequence number generation.
Definition: tcp_misc.c:683
error_t tcpReadTxBuffer(Socket *socket, uint32_t seqNum, NetBuffer *buffer, size_t length)
Copy data from the send buffer.
Definition: tcp_misc.c:2274
void tcpFlushSynQueue(Socket *socket)
Flush SYN queue.
Definition: tcp_misc.c:1503
error_t tcpRetransmitSegment(Socket *socket)
TCP segment retransmission.
Definition: tcp_misc.c:1744
void tcpProcessSegmentData(Socket *socket, const TcpHeader *segment, const NetBuffer *buffer, size_t offset, size_t length)
Process the segment text.
Definition: tcp_misc.c:1303
void tcpChangeState(Socket *socket, TcpState newState)
Update TCP FSM current state.
Definition: tcp_misc.c:2021
void tcpDeleteControlBlock(Socket *socket)
Delete TCB structure.
Definition: tcp_misc.c:1371
const TcpOption * tcpGetOption(const TcpHeader *segment, uint8_t kind)
Search the TCP header for a given option.
Definition: tcp_misc.c:617