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