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