xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_TCP_Utils.c (revision 574b646147a48c508a8bfc82181fd89ea89c8c17)
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 /**
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 
36 /* Standard includes. */
37 #include <stdint.h>
38 #include <stdio.h>
39 
40 /* FreeRTOS includes. */
41 #include "FreeRTOS.h"
42 #include "task.h"
43 
44 /* FreeRTOS+TCP includes. */
45 #include "FreeRTOS_IP.h"
46 #include "FreeRTOS_IP_Private.h"
47 #include "FreeRTOS_Routing.h"
48 
49 #include "FreeRTOS_TCP_Utils.h"
50 
51 /* Just make sure the contents doesn't get compiled if TCP is not enabled. */
52 #if ipconfigUSE_TCP == 1
53 
54 
55 /* For logging and debugging: make a string showing the TCP flags
56  */
57     #if ( ipconfigHAS_DEBUG_PRINTF != 0 )
58 
59         static char retString[ 10 ];
60 
61 /**
62  * @brief Print out the value of flags in a human readable manner.
63  *
64  * @param[in] xFlags The TCP flags.
65  *
66  * @return The string containing the flags.
67  */
prvTCPFlagMeaning(UBaseType_t xFlags)68         const char * prvTCPFlagMeaning( UBaseType_t xFlags )
69         {
70             size_t uxFlags = ( size_t ) xFlags;
71 
72             ( void ) snprintf( retString,
73                                sizeof( retString ), "%c%c%c%c%c%c%c%c",
74                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_FIN ) != 0 ) ? 'F' : '.',   /* 0x0001: No more data from sender */
75                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_SYN ) != 0 ) ? 'S' : '.',   /* 0x0002: Synchronize sequence numbers */
76                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_RST ) != 0 ) ? 'R' : '.',   /* 0x0004: Reset the connection */
77                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_PSH ) != 0 ) ? 'P' : '.',   /* 0x0008: Push function: please push buffered data to the recv application */
78                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_ACK ) != 0 ) ? 'A' : '.',   /* 0x0010: Acknowledgment field is significant */
79                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_URG ) != 0 ) ? 'U' : '.',   /* 0x0020: Urgent pointer field is significant */
80                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_ECN ) != 0 ) ? 'E' : '.',   /* 0x0040: ECN-Echo */
81                                ( ( uxFlags & ( size_t ) tcpTCP_FLAG_CWR ) != 0 ) ? 'C' : '.' ); /* 0x0080: Congestion Window Reduced */
82             return retString;
83         }
84     #endif /* #if ( ipconfigHAS_DEBUG_PRINTF != 0 ) */
85     /*-----------------------------------------------------------*/
86 
87 /**
88  * @brief Set the MSS (Maximum segment size) associated with the given socket.
89  *
90  * @param[in] pxSocket The socket whose MSS is to be set.
91  */
prvSocketSetMSS(FreeRTOS_Socket_t * pxSocket)92     void prvSocketSetMSS( FreeRTOS_Socket_t * pxSocket )
93     {
94         switch( pxSocket->bits.bIsIPv6 ) /* LCOV_EXCL_BR_LINE */
95         {
96             #if ( ipconfigUSE_IPv4 != 0 )
97                 case pdFALSE_UNSIGNED:
98                     prvSocketSetMSS_IPV4( pxSocket );
99                     break;
100             #endif /* ( ipconfigUSE_IPv4 != 0 ) */
101 
102             #if ( ipconfigUSE_IPv6 != 0 )
103                 case pdTRUE_UNSIGNED:
104                     prvSocketSetMSS_IPV6( pxSocket );
105                     break;
106             #endif /* ( ipconfigUSE_IPv6 != 0 ) */
107 
108             default: /* LCOV_EXCL_LINE */
109                 /* Shouldn't reach here */
110                 /* MISRA 16.4 Compliance */
111                 break; /* LCOV_EXCL_LINE */
112         }
113     }
114     /*-----------------------------------------------------------*/
115 
116 #endif /* ipconfigUSE_TCP == 1 */
117