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