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 /**   User Datagram Protocol (UDP)                                        */
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_udp.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_udp_socket_bytes_available                      PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function determines the number of bytes available on a UDP     */
44 /*    socket for reception.                                               */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    socket_ptr                            Pointer to the UDP sockete    */
49 /*    bytes_available                       Number of bytes returned to   */
50 /*                                             the caller.                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    status                                Completion status             */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    tx_mutex_get                          Obtain protection             */
59 /*    tx_mutex_put                          Release protection            */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
70 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_nx_udp_socket_bytes_available(NX_UDP_SOCKET * socket_ptr,ULONG * bytes_available)74 UINT  _nx_udp_socket_bytes_available(NX_UDP_SOCKET *socket_ptr, ULONG *bytes_available)
75 {
76 NX_IP     *ip_ptr;
77 NX_PACKET *packet_ptr;
78 INT        count;
79 
80     /* Setup IP pointer. */
81     ip_ptr = socket_ptr -> nx_udp_socket_ip_ptr;
82 
83     /* Obtain the IP mutex so we can examine the bound port.  */
84     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
85 
86     *bytes_available = 0;
87 
88     /* Determine if the socket is currently bound. */
89     if (!socket_ptr -> nx_udp_socket_bound_next)
90     {
91         /* Release mutex */
92         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
93 
94         return(NX_NOT_SUCCESSFUL);
95     }
96 
97     packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
98 
99     /* Loop through all the packets on the queue and find out the total
100        number of bytes in the rx queue that are available to the applciation. */
101     for (count = 0; count < (INT)(socket_ptr -> nx_udp_socket_receive_count); count++)
102     {
103 
104         *bytes_available += (packet_ptr -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER));
105 
106         /* Move on to the next packet. */
107         packet_ptr = packet_ptr -> nx_packet_queue_next;
108     }
109 
110     /* Release protection.  */
111     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
112 
113     return(NX_SUCCESS);
114 }
115 
116