xref: /FreeRTOS-Plus-TCP-v4.0.0/source/FreeRTOS_TCP_Utils_IPv4.c (revision 67b9e1c2813792625500f9e8c091822b325f3fe7)
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_IPv4.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 /* *INDENT-OFF* */
50 #if( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 )
51 /* *INDENT-ON* */
52 
53 /**
54  * @brief Set the MSS (Maximum segment size) associated with the given socket.
55  *
56  * @param[in] pxSocket The socket whose MSS is to be set.
57  */
prvSocketSetMSS_IPV4(FreeRTOS_Socket_t * pxSocket)58 void prvSocketSetMSS_IPV4( FreeRTOS_Socket_t * pxSocket )
59 {
60     uint32_t ulMSS = ipconfigTCP_MSS;
61     const NetworkEndPoint_t * pxEndPoint = pxSocket->pxEndPoint;
62 
63     if( pxEndPoint != NULL )
64     {
65         /* Do not allow MSS smaller than tcpMINIMUM_SEGMENT_LENGTH. */
66         #if ( ipconfigTCP_MSS >= tcpMINIMUM_SEGMENT_LENGTH )
67             {
68                 ulMSS = ipconfigTCP_MSS;
69             }
70         #else
71             {
72                 ulMSS = tcpMINIMUM_SEGMENT_LENGTH;
73             }
74         #endif
75 
76         /* Check if the remote IP-address belongs to the same netmask. */
77         if( ( ( FreeRTOS_ntohl( pxSocket->u.xTCP.xRemoteIP.ulIP_IPv4 ) ^ pxEndPoint->ipv4_settings.ulIPAddress ) & pxEndPoint->ipv4_settings.ulNetMask ) != 0U )
78         {
79             /* Data for this peer will pass through a router, and maybe through
80              * the internet.  Limit the MSS to 1400 bytes or less. */
81             ulMSS = FreeRTOS_min_uint32( ( uint32_t ) tcpREDUCED_MSS_THROUGH_INTERNET, ulMSS );
82         }
83     }
84 
85     FreeRTOS_debug_printf( ( "prvSocketSetMSS: %u bytes for %xip port %u\n", ( unsigned ) ulMSS, ( unsigned ) pxSocket->u.xTCP.xRemoteIP.ulIP_IPv4, pxSocket->u.xTCP.usRemotePort ) );
86 
87     pxSocket->u.xTCP.usMSS = ( uint16_t ) ulMSS;
88 }
89 /*-----------------------------------------------------------*/
90 
91 /* *INDENT-OFF* */
92 #endif /* ( ipconfigUSE_IPv4 != 0 ) && ( ipconfigUSE_TCP == 1 ) */
93 /* *INDENT-ON* */
94