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