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_udp.h"
30 #include "nx_ip.h"
31 #include "nx_ipv6.h"
32
33 /* Bring in externs for caller checking code. */
34
35 NX_CALLER_CHECKING_EXTERNS
36
37
38 /**************************************************************************/
39 /* */
40 /* FUNCTION RELEASE */
41 /* */
42 /* _nxde_udp_socket_source_send PORTABLE C */
43 /* 6.1 */
44 /* AUTHOR */
45 /* */
46 /* Yuxin Zhou, Microsoft Corporation */
47 /* */
48 /* DESCRIPTION */
49 /* */
50 /* This function performs error checking on the UDP socket send */
51 /* via source address service. */
52 /* */
53 /* INPUT */
54 /* */
55 /* socket_ptr Pointer to the UDP socket */
56 /* packet_ptr Pointer to packet to send */
57 /* ip_address Pointer to destination address*/
58 /* port Destination port number */
59 /* address_index Index of IPv4 or IPv6 address */
60 /* to use as the source address*/
61 /* */
62 /* OUTPUT */
63 /* */
64 /* status Actual completion status */
65 /* NX_PTR_ERROR Invalid pointer input */
66 /* NX_NOT_ENABLED UDP not enabled on IP instance*/
67 /* NX_IP_ADDRESS_ERROR Invalid input address */
68 /* NX_INVALID_INTERFACE Invalid interface input */
69 /* */
70 /* CALLS */
71 /* */
72 /* _nxde_udp_socket_source_send Actual UDP socket socket */
73 /* send function */
74 /* */
75 /* CALLED BY */
76 /* */
77 /* Application Code */
78 /* */
79 /* RELEASE HISTORY */
80 /* */
81 /* DATE NAME DESCRIPTION */
82 /* */
83 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
84 /* 09-30-2020 Yuxin Zhou Modified comment(s), */
85 /* resulting in version 6.1 */
86 /* */
87 /**************************************************************************/
88
_nxde_udp_socket_source_send(NX_UDP_SOCKET * socket_ptr,NX_PACKET * packet_ptr,NXD_ADDRESS * ip_address,UINT port,UINT address_index)89 UINT _nxde_udp_socket_source_send(NX_UDP_SOCKET *socket_ptr, NX_PACKET *packet_ptr,
90 NXD_ADDRESS *ip_address, UINT port, UINT address_index)
91 {
92
93 UINT status;
94 UINT ip_header_size;
95
96
97 /* Check for invalid input pointers. */
98 if ((socket_ptr == NX_NULL) || (packet_ptr == NX_NULL) || (ip_address == NX_NULL) || (socket_ptr -> nx_udp_socket_id != NX_UDP_ID))
99 {
100 return(NX_PTR_ERROR);
101 }
102
103 /* Verify UDP is enabled. */
104 if (!(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_packet_receive)
105 {
106 return(NX_NOT_ENABLED);
107 }
108
109 /* Check that the destination address version is either IPv4 or IPv6. */
110 #ifndef NX_DISABLE_IPV4
111 if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
112 {
113 if (address_index >= NX_MAX_IP_INTERFACES)
114 {
115 return(NX_INVALID_INTERFACE);
116 }
117
118 if (ip_address -> nxd_ip_address.v4 == 0)
119 {
120 return(NX_IP_ADDRESS_ERROR);
121 }
122
123 ip_header_size = (UINT)sizeof(NX_IPV4_HEADER);
124 }
125 else
126 #endif /* !NX_DISABLE_IPV4 */
127
128 #ifdef FEATURE_NX_IPV6
129 if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
130 {
131 if (address_index >= (NX_MAX_IPV6_ADDRESSES + NX_LOOPBACK_IPV6_ENABLED))
132 {
133 return(NX_INVALID_INTERFACE);
134 }
135
136 if (CHECK_UNSPECIFIED_ADDRESS(&ip_address -> nxd_ip_address.v6[0]))
137 {
138 return(NX_IP_ADDRESS_ERROR);
139 }
140
141 ip_header_size = (UINT)sizeof(NX_IPV6_HEADER);
142 }
143 else
144 #endif /* FEATURE_NX_IPV6 */
145 {
146 return(NX_IP_ADDRESS_ERROR);
147 }
148
149 /* Check for an invalid packet prepend pointer. */
150 /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
151 if ((INT)(packet_ptr -> nx_packet_prepend_ptr - packet_ptr -> nx_packet_data_start) < (INT)(ip_header_size + sizeof(NX_UDP_HEADER)))
152 {
153
154 #ifndef NX_DISABLE_UDP_INFO
155 /* Increment the total UDP invalid packet count. */
156 (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++;
157
158 /* Increment the total UDP invalid packet count for this socket. */
159 socket_ptr -> nx_udp_socket_invalid_packets++;
160 #endif
161
162 /* Return error code. */
163 return(NX_UNDERFLOW);
164 }
165
166 /* Check for an invalid packet append pointer. */
167 /*lint -e{946} suppress pointer subtraction, since it is necessary. */
168 if (packet_ptr -> nx_packet_append_ptr > packet_ptr -> nx_packet_data_end)
169 {
170
171 #ifndef NX_DISABLE_UDP_INFO
172 /* Increment the total UDP invalid packet count. */
173 (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++;
174
175 /* Increment the total UDP invalid packet count for this socket. */
176 socket_ptr -> nx_udp_socket_invalid_packets++;
177 #endif
178
179 /* Return error code. */
180 return(NX_OVERFLOW);
181 }
182
183 /* Check for an invalid port. */
184 if (((ULONG)port) > (ULONG)NX_MAX_PORT)
185 {
186 return(NX_INVALID_PORT);
187 }
188
189 /* Check for appropriate caller. */
190 NX_THREADS_ONLY_CALLER_CHECKING
191
192 /* Call actual UDP socket send function. */
193 status = _nxd_udp_socket_source_send(socket_ptr, packet_ptr, ip_address, port, address_index);
194
195 /* Return completion status. */
196 return(status);
197 }
198
199