1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Transmission Control Protocol (TCP)                                 */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "nx_api.h"
28 #include "nx_tcp.h"
29 #include "nx_packet.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nx_tcp_socket_bytes_available                      PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function determines the number of bytes available on a TCP     */
45 /*    socket for reception.                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to the TCP sockete    */
50 /*    bytes_available                       Number of bytes returned to   */
51 /*                                             the caller.                */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    tx_mutex_get                          Obtain protection             */
60 /*    tx_mutex_put                          Release protection            */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_tcp_socket_bytes_available(NX_TCP_SOCKET * socket_ptr,ULONG * bytes_available)75 UINT  _nx_tcp_socket_bytes_available(NX_TCP_SOCKET *socket_ptr, ULONG *bytes_available)
76 {
77 NX_IP         *ip_ptr;
78 NX_PACKET     *packet_ptr;
79 UINT           data_size;
80 NX_TCP_HEADER *header_ptr;
81 UINT           header_length;
82 INT            done = 0;
83 
84     /* Setup IP pointer. */
85     ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
86 
87     /* Obtain the IP mutex so we can examine the bound port.  */
88     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
89 
90     *bytes_available = 0;
91 
92 
93     /* Make sure the TCP connection has been established. */
94     if ((socket_ptr -> nx_tcp_socket_state <= NX_TCP_LISTEN_STATE) ||
95         (socket_ptr -> nx_tcp_socket_state > NX_TCP_ESTABLISHED))
96     {
97         /* Release protection.  */
98         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
99 
100         return(NX_NOT_CONNECTED);
101     }
102 
103     packet_ptr = socket_ptr -> nx_tcp_socket_receive_queue_head;
104 
105     if (packet_ptr == NX_NULL)
106     {
107 
108         /* The receive queue is empty. */
109         /* Release protection.  */
110         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
111 
112         return(NX_SUCCESS);
113     }
114 
115     /* Loop through all the packets on the queue and find out the total
116        number of bytes in the rx queue that are available to the applciation. */
117     do
118     {
119         /*lint -e{923} suppress cast of ULONG to pointer.  */
120         if (packet_ptr -> nx_packet_queue_next == ((NX_PACKET *)NX_PACKET_READY))
121         {
122 
123             /* Compute the size of TCP payload in this packet */
124             /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary  */
125             header_ptr =  (NX_TCP_HEADER *)packet_ptr -> nx_packet_prepend_ptr;
126 
127             /* Calculate the header size for this packet.  */
128             header_length = (UINT)((header_ptr -> nx_tcp_header_word_3 >> NX_TCP_HEADER_SHIFT) * sizeof(ULONG));
129 
130             data_size = (UINT)(packet_ptr -> nx_packet_length - header_length);
131             *bytes_available += data_size;
132 
133             if (packet_ptr == socket_ptr -> nx_tcp_socket_receive_queue_tail)
134             {
135                 /* Already reached the last packet.  */
136                 done = 1;
137             }
138             else
139             {
140                 /* Move on to the next packet. */
141                 packet_ptr = packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next;
142             }
143         }
144         else
145         {
146             /* If the packet has not been acked yet, then just return the
147                amount of bytes available so far. */
148             done = 1;
149         }
150     } while (!done);
151 
152     /* Release protection.  */
153     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
154 
155     return(NX_SUCCESS);
156 }
157 
158