xref: /FreeRTOS-Plus-TCP-v3.1.0/source/FreeRTOS_TCP_Utils.c (revision 37bdfe577f3b728058de714e2e747d3c78803f26)
1 /*
2  * FreeRTOS+TCP V3.1.0
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 /**
29  * @file FreeRTOS_TCP_Utils.c
30  * @brief Module contains utility functions used by FreeRTOS+TCP module.
31  *
32  * Endianness: in this module all ports and IP addresses are stored in
33  * host byte-order, except fields in the IP-packets
34  */
35 /* Standard includes. */
36 #include <stdint.h>
37 #include <stdio.h>
38 
39 /* FreeRTOS includes. */
40 #include "FreeRTOS.h"
41 
42 /* FreeRTOS+TCP includes. */
43 #include "FreeRTOS_IP.h"
44 #include "FreeRTOS_IP_Private.h"
45 
46 #include "FreeRTOS_TCP_Utils.h"
47 
48 /* Just make sure the contents doesn't get compiled if TCP is not enabled. */
49 #if ipconfigUSE_TCP == 1
50 
51 
52 /* For logging and debugging: make a string showing the TCP flags
53  */
54     #if ( ipconfigHAS_DEBUG_PRINTF != 0 )
55 
56 /**
57  * @brief Print out the value of flags in a human readable manner.
58  *
59  * @param[in] xFlags: The TCP flags.
60  *
61  * @return The string containing the flags.
62  */
63 
64         static char retString[ 10 ];
prvTCPFlagMeaning(UBaseType_t xFlags)65         const char * prvTCPFlagMeaning( UBaseType_t xFlags )
66         {
67             size_t uxFlags = ( size_t ) xFlags;
68 
69             ( void ) snprintf( retString,
70                                sizeof( retString ), "%c%c%c%c%c%c%c%c",
71                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_FIN ) != 0 ) ? 'F' : '.',   /* 0x0001: No more data from sender */
72                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_SYN ) != 0 ) ? 'S' : '.',   /* 0x0002: Synchronize sequence numbers */
73                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_RST ) != 0 ) ? 'R' : '.',   /* 0x0004: Reset the connection */
74                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_PSH ) != 0 ) ? 'P' : '.',   /* 0x0008: Push function: please push buffered data to the recv application */
75                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_ACK ) != 0 ) ? 'A' : '.',   /* 0x0010: Acknowledgment field is significant */
76                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_URG ) != 0 ) ? 'U' : '.',   /* 0x0020: Urgent pointer field is significant */
77                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_ECN ) != 0 ) ? 'E' : '.',   /* 0x0040: ECN-Echo */
78                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_CWR ) != 0 ) ? 'C' : '.' ); /* 0x0080: Congestion Window Reduced */
79             return retString;
80         }
81         /*-----------------------------------------------------------*/
82 
83     #endif /* ipconfigHAS_DEBUG_PRINTF */
84 
85 /**
86  * @brief Set the MSS (Maximum segment size) associated with the given socket.
87  *
88  * @param[in] pxSocket: The socket whose MSS is to be set.
89  */
prvSocketSetMSS(FreeRTOS_Socket_t * pxSocket)90     void prvSocketSetMSS( FreeRTOS_Socket_t * pxSocket )
91     {
92         uint32_t ulMSS;
93 
94         /* Do not allow MSS smaller than tcpMINIMUM_SEGMENT_LENGTH. */
95         #if ( ipconfigTCP_MSS >= tcpMINIMUM_SEGMENT_LENGTH )
96             {
97                 ulMSS = ipconfigTCP_MSS;
98             }
99         #else
100             {
101                 ulMSS = tcpMINIMUM_SEGMENT_LENGTH;
102             }
103         #endif
104 
105         if( ( ( FreeRTOS_ntohl( pxSocket->u.xTCP.ulRemoteIP ) ^ *ipLOCAL_IP_ADDRESS_POINTER ) & xNetworkAddressing.ulNetMask ) != 0U )
106         {
107             /* Data for this peer will pass through a router, and maybe through
108              * the internet.  Limit the MSS to 1400 bytes or less. */
109             ulMSS = FreeRTOS_min_uint32( ( uint32_t ) tcpREDUCED_MSS_THROUGH_INTERNET, ulMSS );
110         }
111 
112         FreeRTOS_debug_printf( ( "prvSocketSetMSS: %u bytes for %xip:%u\n", ( unsigned ) ulMSS, ( unsigned ) pxSocket->u.xTCP.ulRemoteIP, pxSocket->u.xTCP.usRemotePort ) );
113 
114         pxSocket->u.xTCP.usMSS = ( uint16_t ) ulMSS;
115     }
116     /*-----------------------------------------------------------*/
117 
118 #endif /* ipconfigUSE_TCP == 1 */
119