summaryrefslogtreecommitdiff
path: root/common/src/nx_tcp_mss_option_get.c
blob: 219988b36eca8e38290819f651d200f6abe242a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Transmission Control Protocol (TCP)                                 */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_tcp.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_tcp_mss_option_get                              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function searches for the Maximum Segment Size (MSS) option.   */
/*    If found, first check the option length, if option length is not    */
/*    valid, it returns NX_FALSE to the caller, else it set the mss value */
/*    and returns NX_TRUE to the caller. Otherwise, NX_TRUE is returned.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    option_ptr                            Pointer to option area        */
/*    option_area_size                      Size of option area           */
/*    mss                                   Max segment size              */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    NX_FALSE                              TCP option is invalid         */
/*    NX_TRUE                               TCP option is valid           */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _nx_tcp_packet_process                TCP packet processing         */
/*    _nx_tcp_server_socket_relisten        Socket relisten processing    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_tcp_mss_option_get(UCHAR *option_ptr, ULONG option_area_size, ULONG *mss)
{

ULONG option_length;

    /* Initialize the value.  */
    *mss = 0;

    /* Loop through the option area looking for the MSS.  */
    while (option_area_size >= 4)
    {

        /* Is the current character the MSS type?  */
        if (*option_ptr == NX_TCP_MSS_KIND)
        {

            /* Yes, we found it!  */

            /* Move the pointer forward by one.  */
            option_ptr++;

            /* Check the option length, if option length is not equal to 4, return NX_FALSE.  */
            if (*option_ptr++ != 4)
            {
                return(NX_FALSE);
            }

            /* Build the mss size.  */
            *mss = (ULONG)*option_ptr++;

            /* Get the LSB of the MSS.  */
            *mss = (*mss << 8) | (ULONG)*option_ptr;

            /* Finished, get out of the loop!  */
            break;
        }

        /* Otherwise, process relative to the option type.  */

        /* Check for end of list.  */
        if (*option_ptr == NX_TCP_EOL_KIND)
        {

            /* Yes, end of list, get out!  */
            break;
        }

        /* Check for NOP.  */
        if (*option_ptr++ == NX_TCP_NOP_KIND)
        {

            /* One character option!  */
            option_area_size--;
        }
        else
        {

            /* Derive the option length.  */
            option_length =  ((ULONG)*option_ptr);

            /* Return when option length is invalid. */
            if (option_length == 0)
            {
                return(NX_FALSE);
            }

            /* Move the option pointer forward.  */
            option_ptr =  option_ptr + (option_length - 1);

            /* Determine if this is greater than the option area size.  */
            if (option_length > option_area_size)
            {
                return(NX_FALSE);
            }
            else
            {
                option_area_size =  option_area_size - option_length;
            }
        }
    }

    /* Return.  */
    return(NX_TRUE);
}