1 /*
2  * FreeRTOS+TCP <DEVELOPMENT BRANCH>
3  * Copyright (C) 2022 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * http://aws.amazon.com/freertos
25  * http://www.FreeRTOS.org
26  */
27 
28 #ifndef FREERTOS_TCP_TRANSMISSION_H
29 #define FREERTOS_TCP_TRANSMISSION_H
30 
31 /* *INDENT-OFF* */
32 #ifdef __cplusplus
33     extern "C" {
34 #endif
35 /* *INDENT-ON* */
36 
37 /*
38  * Either sends a SYN or calls prvTCPSendRepeated (for regular messages).
39  */
40 int32_t prvTCPSendPacket( FreeRTOS_Socket_t * pxSocket );
41 
42 /*
43  * Try to send a series of messages.
44  */
45 int32_t prvTCPSendRepeated( FreeRTOS_Socket_t * pxSocket,
46                             NetworkBufferDescriptor_t ** ppxNetworkBuffer );
47 
48 /*
49  * Return or send a packet to the other party.
50  */
51 void prvTCPReturnPacket( FreeRTOS_Socket_t * pxSocket,
52                          NetworkBufferDescriptor_t * pxDescriptor,
53                          uint32_t ulLen,
54                          BaseType_t xReleaseAfterSend );
55 
56 /**
57  * Called by prvTCPReturnPacket(), this function will set the the window
58  * size on this side: 'xTCPHeader.usWindow'.
59  */
60 void prvTCPReturn_CheckTCPWindow( FreeRTOS_Socket_t * pxSocket,
61                                   const NetworkBufferDescriptor_t * pxNetworkBuffer,
62                                   size_t uxIPHeaderSize );
63 
64 /*
65  * Called by prvTCPReturnPacket(), this function sets the sequence and ack numbers
66  * in the TCP-header.
67  */
68 void prvTCPReturn_SetSequenceNumber( FreeRTOS_Socket_t * pxSocket,
69                                      const NetworkBufferDescriptor_t * pxNetworkBuffer,
70                                      size_t uxIPHeaderSize,
71                                      uint32_t ulLen );
72 
73 /*
74  * Return or send a packet to the other party.
75  */
76 void prvTCPReturnPacket_IPV4( FreeRTOS_Socket_t * pxSocket,
77                               NetworkBufferDescriptor_t * pxDescriptor,
78                               uint32_t ulLen,
79                               BaseType_t xReleaseAfterSend );
80 
81 /*
82  * Return or send a packet to the other party.
83  */
84 void prvTCPReturnPacket_IPV6( FreeRTOS_Socket_t * pxSocket,
85                               NetworkBufferDescriptor_t * pxDescriptor,
86                               uint32_t ulLen,
87                               BaseType_t xReleaseAfterSend );
88 
89 void prvTCPReturn_SetEndPoint( const FreeRTOS_Socket_t * pxSocket,
90                                NetworkBufferDescriptor_t * pxNetworkBuffer,
91                                size_t uxIPHeaderSize );
92 
93 /*
94  * Let ARP look-up the MAC-address of the peer and initialise the first SYN
95  * packet.
96  */
97 BaseType_t prvTCPPrepareConnect_IPV4( FreeRTOS_Socket_t * pxSocket );
98 
99 /*
100  * Let ARP look-up the MAC-address of the peer and initialise the first SYN
101  * packet.
102  */
103 BaseType_t prvTCPPrepareConnect_IPV6( FreeRTOS_Socket_t * pxSocket );
104 
105 /*
106  * Initialise the data structures which keep track of the TCP windowing system.
107  */
108 void prvTCPCreateWindow( FreeRTOS_Socket_t * pxSocket );
109 
110 /*
111  * Set the initial properties in the options fields, like the preferred
112  * value of MSS and whether SACK allowed.  Will be transmitted in the state
113  * 'eCONNECT_SYN'.
114  */
115 UBaseType_t prvSetSynAckOptions( FreeRTOS_Socket_t * pxSocket,
116                                  TCPHeader_t * pxTCPHeader );
117 
118 /*
119  * Prepare an outgoing message, if anything has to be sent.
120  */
121 int32_t prvTCPPrepareSend( FreeRTOS_Socket_t * pxSocket,
122                            NetworkBufferDescriptor_t ** ppxNetworkBuffer,
123                            UBaseType_t uxOptionsLength );
124 
125 /*
126  * The API FreeRTOS_send() adds data to the TX stream.  Add
127  * this data to the windowing system to it can be transmitted.
128  */
129 void prvTCPAddTxData( FreeRTOS_Socket_t * pxSocket );
130 
131 /*
132  * Set the TCP options (if any) for the outgoing packet.
133  */
134 UBaseType_t prvSetOptions( FreeRTOS_Socket_t * pxSocket,
135                            const NetworkBufferDescriptor_t * pxNetworkBuffer );
136 
137 /*
138  * Called from prvTCPHandleState().  There is data to be sent.
139  * If ipconfigUSE_TCP_WIN is defined, and if only an ACK must be sent, it will
140  * be checked if it would better be postponed for efficiency.
141  */
142 BaseType_t prvSendData( FreeRTOS_Socket_t * pxSocket,
143                         NetworkBufferDescriptor_t ** ppxNetworkBuffer,
144                         uint32_t ulReceiveLength,
145                         BaseType_t xByteCount );
146 
147 /*
148  * A "challenge ACK" is as per https://tools.ietf.org/html/rfc5961#section-3.2,
149  * case #3. In summary, an RST was received with a sequence number that is
150  * unexpected but still within the window.
151  */
152 BaseType_t prvTCPSendChallengeAck( NetworkBufferDescriptor_t * pxNetworkBuffer );
153 
154 /*
155  * Reply to a peer with the RST flag on, in case a packet can not be handled.
156  */
157 BaseType_t prvTCPSendReset( NetworkBufferDescriptor_t * pxNetworkBuffer );
158 
159 /*
160  *  Check if the size of a network buffer is big enough to hold the outgoing message.
161  *  Allocate a new bigger network buffer when necessary.
162  */
163 NetworkBufferDescriptor_t * prvTCPBufferResize( const FreeRTOS_Socket_t * pxSocket,
164                                                 NetworkBufferDescriptor_t * pxNetworkBuffer,
165                                                 int32_t lDataLen,
166                                                 UBaseType_t uxOptionsLength );
167 /* *INDENT-OFF* */
168 #ifdef __cplusplus
169     } /* extern "C" */
170 #endif
171 /* *INDENT-ON* */
172 
173 #endif /* FREERTOS_TCP_TRANSMISSION_H */
174