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 /**   User Datagram Protocol (UDP)                                        */
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_ip.h"
30 #include "nx_udp.h"
31 #include "nx_packet.h"
32 #ifdef FEATURE_NX_IPV6
33 #include "nx_ipv6.h"
34 #endif /* FEATURE_NX_IPV6 */
35 
36 /* Bring in externs for caller checking code.  */
37 
38 NX_CALLER_CHECKING_EXTERNS
39 
40 
41 /**************************************************************************/
42 /*                                                                        */
43 /*  FUNCTION                                               RELEASE        */
44 /*                                                                        */
45 /*    _nxe_udp_socket_send                                PORTABLE C      */
46 /*                                                           6.1          */
47 /*  AUTHOR                                                                */
48 /*                                                                        */
49 /*    Yuxin Zhou, Microsoft Corporation                                   */
50 /*                                                                        */
51 /*  DESCRIPTION                                                           */
52 /*                                                                        */
53 /*    This function checks for errors in the UDP socket send              */
54 /*    function call.                                                      */
55 /*                                                                        */
56 /*  INPUT                                                                 */
57 /*                                                                        */
58 /*    socket_ptr                            Pointer to UDP socket         */
59 /*    packet_ptr                            Pointer to UDP packet         */
60 /*    ip_address                            IP address                    */
61 /*    port                                  16-bit UDP port number        */
62 /*                                                                        */
63 /*  OUTPUT                                                                */
64 /*                                                                        */
65 /*    status                                Actual completion status      */
66 /*    NX_PTR_ERROR                          Invalid pointer input         */
67 /*    NX_NOT_ENABLED                        UDP not enabled               */
68 /*    NX_IP_ADDRESS_ERROR                   NULL address input            */
69 /*    NX_INVALID_PORT                       Invalid UDP socket port       */
70 /*    NX_UNDERFLOW                          Invalid pointer to packet data*/
71 /*    NX_OVERFLOW                           Invalid pointer to packet data*/
72 /*    NX_CALLER_ERROR                       Invalid (non thread) caller   */
73 /*                                                                        */
74 /*  CALLS                                                                 */
75 /*                                                                        */
76 /*    _nx_udp_socket_send                   Actual UDP socket send        */
77 /*                                            function                    */
78 /*                                                                        */
79 /*  CALLED BY                                                             */
80 /*                                                                        */
81 /*    Application Code                                                    */
82 /*                                                                        */
83 /*  RELEASE HISTORY                                                       */
84 /*                                                                        */
85 /*    DATE              NAME                      DESCRIPTION             */
86 /*                                                                        */
87 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
88 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
89 /*                                            resulting in version 6.1    */
90 /*                                                                        */
91 /**************************************************************************/
_nxe_udp_socket_send(NX_UDP_SOCKET * socket_ptr,NX_PACKET ** packet_ptr_ptr,ULONG ip_address,UINT port)92 UINT  _nxe_udp_socket_send(NX_UDP_SOCKET *socket_ptr, NX_PACKET **packet_ptr_ptr,
93                            ULONG ip_address, UINT port)
94 {
95 
96 #ifndef NX_DISABLE_IPV4
97 NX_PACKET *packet_ptr;
98 UINT       status;
99 
100 
101     /* Setup packet pointer.  */
102     packet_ptr =  *packet_ptr_ptr;
103 
104     /* Check for invalid input pointers.  */
105     /*lint -e{923} suppress cast of ULONG to pointer.  */
106     if ((socket_ptr == NX_NULL) || (socket_ptr -> nx_udp_socket_id != NX_UDP_ID) ||
107         (packet_ptr == NX_NULL) || (packet_ptr -> nx_packet_union_next.nx_packet_tcp_queue_next != ((NX_PACKET *)NX_PACKET_ALLOCATED)))
108     {
109 
110         return(NX_PTR_ERROR);
111     }
112 
113     /* Check to see if UDP is enabled.  */
114     if (!(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_packet_receive)
115     {
116         return(NX_NOT_ENABLED);
117     }
118 
119     /* Check for invalid IP address.  */
120     if (ip_address == NX_NULL)
121     {
122         return(NX_IP_ADDRESS_ERROR);
123     }
124 
125     /* Check for an invalid port.  */
126     if (((ULONG)port) > (ULONG)NX_MAX_PORT)
127     {
128         return(NX_INVALID_PORT);
129     }
130 
131     /* Check for an invalid packet prepend pointer.  */
132     /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
133     if ((INT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) < (INT)(sizeof(NX_IPV4_HEADER) + sizeof(NX_UDP_HEADER)))
134     {
135 
136 #ifndef NX_DISABLE_UDP_INFO
137         /* Increment the total UDP invalid packet count.  */
138         (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++;
139 
140         /* Increment the total UDP invalid packet count for this socket.  */
141         socket_ptr -> nx_udp_socket_invalid_packets++;
142 #endif
143 
144         /* Return error code.  */
145         return(NX_UNDERFLOW);
146     }
147 
148     /* Check for an invalid packet append pointer.  */
149     /*lint -e{946} suppress pointer subtraction, since it is necessary. */
150     if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
151     {
152 
153 #ifndef NX_DISABLE_UDP_INFO
154         /* Increment the total UDP invalid packet count.  */
155         (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++;
156 
157         /* Increment the total UDP invalid packet count for this socket.  */
158         socket_ptr -> nx_udp_socket_invalid_packets++;
159 #endif
160 
161         /* Return error code.  */
162         return(NX_OVERFLOW);
163     }
164 
165     /* Check for appropriate caller.  */
166     NX_THREADS_ONLY_CALLER_CHECKING
167 
168     /* Call actual UDP socket send function.  */
169     status =  _nx_udp_socket_send(socket_ptr, packet_ptr, ip_address, port);
170 
171     /* Determine if the packet send was successful.  */
172     if (status == NX_SUCCESS)
173     {
174 
175         /* Yes, now clear the application's packet pointer so it can't be accidentally
176            used again by the application.  This is only done when error checking is
177            enabled.  */
178         *packet_ptr_ptr =  NX_NULL;
179     }
180 
181     /* Return completion status.  */
182     return(status);
183 #else
184     NX_PARAMETER_NOT_USED(socket_ptr);
185     NX_PARAMETER_NOT_USED(packet_ptr_ptr);
186     NX_PARAMETER_NOT_USED(ip_address);
187     NX_PARAMETER_NOT_USED(port);
188 
189     return(NX_NOT_SUPPORTED);
190 #endif /* !NX_DISABLE_IPV4  */
191 }
192 
193