summaryrefslogtreecommitdiff
path: root/common/src/nx_tcp_socket_receive.c
blob: baecd519da634b93634cc68a57e98e6cec3646ae (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/***************************************************************************
 * 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_ip.h"
#include "nx_packet.h"
#include "nx_tcp.h"
#include "tx_thread.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_tcp_socket_receive                              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function attempts to receive one or more TCP packets from the  */
/*    specified socket.                                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    socket_ptr                            Pointer to socket             */
/*    packet_ptr                            Pointer to packet pointer     */
/*    wait_option                           Suspension option             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_tcp_packet_send_ack               Send ACK message              */
/*    _nx_tcp_socket_thread_suspend         Suspend calling thread        */
/*    tx_mutex_get                          Get protection mutex          */
/*    tx_mutex_put                          Put protection mutex          */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT  _nx_tcp_socket_receive(NX_TCP_SOCKET *socket_ptr, NX_PACKET **packet_ptr, ULONG wait_option)
{

NX_IP                 *ip_ptr;
NX_TCP_HEADER         *header_ptr;
NX_PACKET             *head_packet_ptr;
ULONG                  header_length;

#ifdef TX_ENABLE_EVENT_TRACE
TX_TRACE_BUFFER_ENTRY *trace_event;
ULONG                  trace_timestamp;
#endif

    /* Setup the pointer to the associated IP instance.  */
    ip_ptr =  socket_ptr -> nx_tcp_socket_ip_ptr;

    /* Set the return pointer to NULL initially.  */
    *packet_ptr =   NX_NULL;

    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_SOCKET_RECEIVE, socket_ptr, 0, 0, 0, NX_TRACE_TCP_EVENTS, &trace_event, &trace_timestamp);

    /* Get protection while we look at this socket.  */
    tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);

    /* Determine if the socket is currently bound.  */
    if (!socket_ptr ->  nx_tcp_socket_bound_next)
    {

        /* Release protection.  */
        tx_mutex_put(&(ip_ptr -> nx_ip_protection));

        /* Socket is not bound, return an error message.  */
        return(NX_NOT_BOUND);
    }

    /* Do not return without data if there is data on the queue. */
    if (!socket_ptr -> nx_tcp_socket_receive_queue_head)
    {
        /* There is no data on the queue. */

        /* Determine if the socket is still in an active state, but also allow
           a receive socket operation if there are still more queued receive
           packets for this socket.  */
        if ((socket_ptr -> nx_tcp_socket_state < NX_TCP_SYN_SENT)   ||
            (socket_ptr -> nx_tcp_socket_state == NX_TCP_CLOSE_WAIT) ||
            (socket_ptr -> nx_tcp_socket_state >= NX_TCP_CLOSING))
        {

            /* Release the IP protection.  */
            tx_mutex_put(&(ip_ptr -> nx_ip_protection));

            /* Return an error code.  */
            return(NX_NOT_CONNECTED);
        }
    }

    /* Pickup the important information from the socket.  */

    /* Attempt to build a pointer to the first packet in the socket's
       receive queue.  */
    if (socket_ptr -> nx_tcp_socket_receive_queue_head)
    {

        /* Yes, there is a packet on the receive queue.  Setup a pointer to it and
           its header.  */
        head_packet_ptr =  socket_ptr -> nx_tcp_socket_receive_queue_head;
    }
    else
    {

        /* Just set the pointers to NULL.  */
        head_packet_ptr =  NX_NULL;
    }

    /* Determine if there is a receive packet available.  */
    /*lint -e{923} suppress cast of ULONT to pointer.  */
    if ((head_packet_ptr) && (head_packet_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_PACKET_READY)))
    {


        /* Yes, the first packet in the queue is available and has been ACKed.  Remove it
           from the queue and return it to the caller.  */
        if (head_packet_ptr == socket_ptr -> nx_tcp_socket_receive_queue_tail)
        {

            /* Only item in the queue.  Set the head and tail pointers to NULL.  */
            socket_ptr -> nx_tcp_socket_receive_queue_head =  NX_NULL;
            socket_ptr -> nx_tcp_socket_receive_queue_tail =  NX_NULL;
        }
        else
        {

            /* Simply update the head pointer to the packet after the current. The tail pointer does not
               need update.  */
            socket_ptr -> nx_tcp_socket_receive_queue_head =  head_packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next;
        }

        /* Decrease the number of received packets.  */
        socket_ptr -> nx_tcp_socket_receive_queue_count--;

        /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
        header_ptr =  (NX_TCP_HEADER *)head_packet_ptr -> nx_packet_prepend_ptr;

        /* Calculate the header size for this packet.  */
        header_length =  (header_ptr -> nx_tcp_header_word_3 >> NX_TCP_HEADER_SHIFT) * (ULONG)sizeof(ULONG);

        /* Adjust the packet prepend pointer and length to position past the TCP header.  */
        head_packet_ptr -> nx_packet_prepend_ptr =  head_packet_ptr -> nx_packet_prepend_ptr + header_length;
        head_packet_ptr -> nx_packet_length =       head_packet_ptr -> nx_packet_length - header_length;

        /* Indicate that this TCP packet is no longer enqueued by marking it again as allocated. This is what
           it was prior to being part of the TCP receive queue.  */
        /*lint -e{923} suppress cast of ULONT to pointer.  */
        head_packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next =  (NX_PACKET *)NX_PACKET_ALLOCATED;

        /* Clear the queue next pointer.  */
        head_packet_ptr -> nx_packet_queue_next =  NX_NULL;

        /* Place the packet pointer in the return pointer.  */
        *packet_ptr =  head_packet_ptr;

        /* Check the receive queue count.  */
        if (socket_ptr -> nx_tcp_socket_receive_queue_count == 0)
        {

            /* Make sure the current receive window is the default window!  */
            socket_ptr -> nx_tcp_socket_rx_window_current =  socket_ptr -> nx_tcp_socket_rx_window_default;
        }
        else
        {

            /* Increase the receive window size.  */
            socket_ptr -> nx_tcp_socket_rx_window_current += (*packet_ptr) -> nx_packet_length;
        }

        /* Determine if an ACK should be forced out for window update, SWS avoidance algorithm.
           RFC1122, Section4.2.3.3, Page97-98. */
        if (((socket_ptr -> nx_tcp_socket_rx_window_current - socket_ptr -> nx_tcp_socket_rx_window_last_sent) >= (socket_ptr -> nx_tcp_socket_rx_window_default / 2)) &&
            ((socket_ptr -> nx_tcp_socket_state == NX_TCP_ESTABLISHED) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_FIN_WAIT_1) || (socket_ptr -> nx_tcp_socket_state == NX_TCP_FIN_WAIT_2)))
        {

            /* Send a Window Update.  */
            _nx_tcp_packet_send_ack(socket_ptr, socket_ptr -> nx_tcp_socket_tx_sequence);
        }

#ifdef TX_ENABLE_EVENT_TRACE
        /* Update the trace event with the status.  */
        NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_TCP_SOCKET_RECEIVE, 0, *packet_ptr, (*packet_ptr) -> nx_packet_length, socket_ptr -> nx_tcp_socket_rx_sequence);
#endif /* TX_ENABLE_EVENT_TRACE */

        /* Release protection.  */
        tx_mutex_put(&(ip_ptr -> nx_ip_protection));

        /* Return a successful status.  */
        return(NX_SUCCESS);
    }
    else if ((wait_option) && (_tx_thread_current_ptr != &(ip_ptr -> nx_ip_thread)))
    {

        /* Suspend the thread on this socket's receive queue.  */

        /* Save the return packet pointer address as well.  */
        _tx_thread_current_ptr -> tx_thread_additional_suspend_info =  (void *)packet_ptr;

        /* Increment the suspended thread count.  */
        socket_ptr -> nx_tcp_socket_receive_suspended_count++;

        /* Suspend the thread on the receive queue.  */
        /* Note that the mutex is released inside _nx_tcp_socket_thread_suspend(). */
        _nx_tcp_socket_thread_suspend(&(socket_ptr -> nx_tcp_socket_receive_suspension_list), _nx_tcp_receive_cleanup, socket_ptr, &(ip_ptr -> nx_ip_protection), wait_option);
#ifdef TX_ENABLE_EVENT_TRACE
        if (*packet_ptr)
        {

            /* Update the trace event with the status.  */
            NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_TCP_SOCKET_RECEIVE, 0, *packet_ptr, (*packet_ptr) -> nx_packet_length, socket_ptr -> nx_tcp_socket_rx_sequence);
        }
#endif /* TX_ENABLE_EVENT_TRACE */
        /* If not, just return the error code.  */
        return(_tx_thread_current_ptr -> tx_thread_suspend_status);
    }
    else
    {

        /* Release protection.  */
        tx_mutex_put(&(ip_ptr -> nx_ip_protection));

        /* Return an empty receive queue error message.  */
        return(NX_NO_PACKET);
    }
}