Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#include "nx_ipv6.h"
#include "tx_thread.h"
#include "nx_packet.h"
#include "nx_udp.h"
#include "nx_ipsec.h"
...
...
_nx_udp_socket_receive(NX_UDP_SOCKET *, NX_PACKET **, ULONG)
Files
loading...
SourceVuSTM32 Libraries and Samplesnetxduocommon/src/nx_udp_socket_receive.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** NetX Component */ /** */ /** User Datagram Protocol (UDP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_ip.h" #ifdef FEATURE_NX_IPV6 #include "nx_ipv6.h" #endif /* FEATURE_NX_IPV6 */ #include "tx_thread.h" #include "nx_packet.h" #include "nx_udp.h" #ifdef NX_IPSEC_ENABLE #include "nx_ipsec.h" #endif /* NX_IPSEC_ENABLE */ ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_udp_socket_receive PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function checks for a received UDP packet on the specified */ /* socket and if no packets are on the receive queue, suspends for the */ /* wait option duration. */ /* */ /* INPUT */ /* */ /* socket_ptr Pointer to UDP socket */ /* packet_ptr Pointer to UDP packet pointer */ /* wait_option Suspension option */ /* */ /* OUTPUT */ /* */ /* status Completion status */ /* */ /* CALLS */ /* */ /* _nx_packet_release Release data packet */ /* _tx_thread_system_suspend Suspend thread */ /* */ /* CALLED BY */ /* */ /* Application Code */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */ /* 09-30-2020 Yuxin Zhou Modified comment(s), */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _nx_udp_socket_receive(NX_UDP_SOCKET *socket_ptr, NX_PACKET **packet_ptr, ULONG wait_option) { TX_INTERRUPT_SAVE_AREA ULONG *temp_ptr; #ifdef NX_ENABLE_INTERFACE_CAPABILITY NX_INTERFACE *interface_ptr = NX_NULL; #endif /* NX_ENABLE_INTERFACE_CAPABILITY */ #if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) UINT compute_checksum = 1; #endif /* defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */ TX_THREAD *thread_ptr; #ifdef TX_ENABLE_EVENT_TRACE TX_TRACE_BUFFER_ENTRY *trace_event; ULONG trace_timestamp;/* ... */ #endif #ifdef NX_DISABLE_UDP_RX_CHECKSUM compute_checksum = 0; #endif /* NX_DISABLE_UDP_RX_CHECKSUM */ /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_UDP_SOCKET_RECEIVE, socket_ptr -> nx_udp_socket_ip_ptr, socket_ptr, 0, 0, NX_TRACE_UDP_EVENTS, &trace_event, &trace_timestamp); /* Set the return pointer to NULL initially. */ *packet_ptr = NX_NULL; /* Loop to retrieve a packet from the interface. */ for (;;) { /* Lockout interrupts. */ TX_DISABLE /* Determine if the socket is currently bound. */ if (!socket_ptr -> nx_udp_socket_bound_next) { /* Restore interrupts. */ TX_RESTORE /* Socket is not bound, return an error message. */ return(NX_NOT_BOUND); }if (!socket_ptr -> nx_udp_socket_bound_next) { ... } /* Determine if there is a packet already queued up for this socket. */ if (socket_ptr -> nx_udp_socket_receive_head) { /* Yes, there is a packet waiting. */ /* Remove it and place it in the thread's destination. */ *packet_ptr = socket_ptr -> nx_udp_socket_receive_head; socket_ptr -> nx_udp_socket_receive_head = (*packet_ptr) -> nx_packet_queue_next; /* If this was the last packet, set the tail pointer to NULL. */ if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL) { socket_ptr -> nx_udp_socket_receive_tail = NX_NULL; }if (socket_ptr -> nx_udp_socket_receive_head == NX_NULL) { ... } /* Decrease the queued packet count. */ socket_ptr -> nx_udp_socket_receive_count--; /* Restore interrupts. */ TX_RESTORE }if (socket_ptr -> nx_udp_socket_receive_head) { ... } else { /* Determine if the request specifies suspension. */ if (wait_option) { /* Prepare for suspension of this thread. */ /* Pickup thread pointer. */ thread_ptr = _tx_thread_current_ptr; /* Setup cleanup routine pointer. */ thread_ptr -> tx_thread_suspend_cleanup = _nx_udp_receive_cleanup; /* Setup cleanup information, i.e. this pool control block. *//* ... */ thread_ptr -> tx_thread_suspend_control_block = (void *)socket_ptr; /* Save the return packet pointer address as well. */ thread_ptr -> tx_thread_additional_suspend_info = (void *)packet_ptr; /* Setup suspension list. */ if (socket_ptr -> nx_udp_socket_receive_suspension_list) { /* This list is not NULL, add current thread to the end. */ thread_ptr -> tx_thread_suspended_next = socket_ptr -> nx_udp_socket_receive_suspension_list; thread_ptr -> tx_thread_suspended_previous = (socket_ptr -> nx_udp_socket_receive_suspension_list) -> tx_thread_suspended_previous; ((socket_ptr -> nx_udp_socket_receive_suspension_list) -> tx_thread_suspended_previous) -> tx_thread_suspended_next = thread_ptr; (socket_ptr -> nx_udp_socket_receive_suspension_list) -> tx_thread_suspended_previous = thread_ptr; }if (socket_ptr -> nx_udp_socket_receive_suspension_list) { ... } else { /* No other threads are suspended. Setup the head pointer and just setup this threads pointers to itself. *//* ... */ socket_ptr -> nx_udp_socket_receive_suspension_list = thread_ptr; thread_ptr -> tx_thread_suspended_next = thread_ptr; thread_ptr -> tx_thread_suspended_previous = thread_ptr; }else { ... } /* Increment the suspended thread count. */ socket_ptr -> nx_udp_socket_receive_suspended_count++; /* Set the state to suspended. */ thread_ptr -> tx_thread_state = TX_TCP_IP; /* Set the suspending flag. */ thread_ptr -> tx_thread_suspending = TX_TRUE; /* Temporarily disable preemption. */ _tx_thread_preempt_disable++; /* Save the timeout value. */ thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = wait_option; /* Restore interrupts. */ TX_RESTORE /* Call actual thread suspension routine. */ _tx_thread_system_suspend(thread_ptr); /* Determine if a packet was received successfully. */ if (thread_ptr -> tx_thread_suspend_status != NX_SUCCESS) { /* If not, just return the error code. */ return(thread_ptr -> tx_thread_suspend_status); }if (thread_ptr -> tx_thread_suspend_status != NX_SUCCESS) { ... } /* Otherwise, just fall through to the checksum logic for the UDP packet. *//* ... */ }if (wait_option) { ... } else { /* Restore interrupts. */ TX_RESTORE /* Set the return pointer to NULL in case it was set but released due to checksum error. */ *packet_ptr = NX_NULL; /* Immediate return, return error completion. */ return(NX_NO_PACKET); }else { ... } }else { ... } #ifdef NX_ENABLE_INTERFACE_CAPABILITY /* Get the packet interface. */ #ifndef NX_DISABLE_IPV4 if ((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V4) { interface_ptr = (*packet_ptr) -> nx_packet_address.nx_packet_interface_ptr; }if ((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* !NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if ((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V6) { interface_ptr = (*packet_ptr) -> nx_packet_address.nx_packet_ipv6_address_ptr -> nxd_ipv6_address_attached; }if ((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ if (interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_RX_CHECKSUM) { compute_checksum = 0; }if (interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_UDP_RX_CHECKSUM) { ... } /* ... */#endif /* NX_ENABLE_INTERFACE_CAPABILITY */ #ifdef NX_IPSEC_ENABLE if (((*packet_ptr) -> nx_packet_ipsec_sa_ptr != NX_NULL) && (((NX_IPSEC_SA *)((*packet_ptr) -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_encryption_method != NX_CRYPTO_NONE)) { compute_checksum = 1; }if (((*packet_ptr) -> nx_packet_ipsec_sa_ptr != NX_NULL) && (((NX_IPSEC_SA *)((*packet_ptr) -> nx_packet_ipsec_sa_ptr)) -> nx_ipsec_sa_encryption_method != NX_CRYPTO_NONE)) { ... } /* ... */#endif /* NX_IPSEC_ENABLE */ #if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) if (compute_checksum) #endif /* defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */ { /* Determine if we need to compute the UDP checksum. If it is disabled for this socket or if the UDP packet has a zero in the checksum field (indicating it was not computed by the sender, skip the checksum processing. *//* ... */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ temp_ptr = (ULONG *)(*packet_ptr) -> nx_packet_prepend_ptr; if ((!socket_ptr -> nx_udp_socket_disable_checksum && (*(temp_ptr + 1) & NX_LOWER_16_MASK)) || /* per-socket checksum is not disabled, and the checksum field is not zero*/ ((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V6)) /* It is IPv6 packet */ { ULONG *ip_src_addr = NX_NULL, *ip_dest_addr = NX_NULL; ULONG checksum; NX_PACKET *current_ptr = *packet_ptr; #ifdef NX_LITTLE_ENDIAN NX_UDP_HEADER *udp_header_ptr; /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ udp_header_ptr = (NX_UDP_HEADER *)(current_ptr -> nx_packet_prepend_ptr);/* ... */ #endif /* NX_LITTLE_ENDIAN */ #ifndef NX_DISABLE_IPV4 if (current_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { NX_IPV4_HEADER *ipv4_header; /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ ipv4_header = (NX_IPV4_HEADER *)(current_ptr -> nx_packet_ip_header); ip_src_addr = &(ipv4_header -> nx_ip_header_source_ip); ip_dest_addr = &(ipv4_header -> nx_ip_header_destination_ip); }if (current_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4) { ... } /* ... */#endif /* !NX_DISABLE_IPV4 */ #ifdef FEATURE_NX_IPV6 if (current_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) /* IPv6 */ { NX_IPV6_HEADER *ipv6_header; /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ ipv6_header = (NX_IPV6_HEADER *)(current_ptr -> nx_packet_ip_header); ip_src_addr = (&ipv6_header -> nx_ip_header_source_ip[0]); ip_dest_addr = (&ipv6_header -> nx_ip_header_destination_ip[0]); ...} /* ... */ #endif /* FEATURE_NX_IPV6 */ #ifdef NX_LITTLE_ENDIAN /* Restore UDP header to network byte order */ NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_0); NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);/* ... */ #endif /* NX_LITTLE_ENDIAN */ /* nx_ip_checksum_compute takes care of both even number length and odd number length */ /* Compute the checksum of the first packet */ checksum = _nx_ip_checksum_compute(current_ptr, NX_PROTOCOL_UDP, (UINT)current_ptr -> nx_packet_length, /* IPv6 src/dest address */ ip_src_addr, ip_dest_addr); #ifdef NX_LITTLE_ENDIAN /* Convert UDP header to host byte order */ NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_0); NX_CHANGE_ULONG_ENDIAN(udp_header_ptr -> nx_udp_header_word_1);/* ... */ #endif /* NX_LITTLE_ENDIAN */ /* Perform the one's complement processing on the checksum. */ checksum = NX_LOWER_16_MASK & ~checksum; /* Determine if it is valid. */ if (checksum == 0) { /* The checksum is okay, so get out of the loop. */ break; }if (checksum == 0) { ... } else { #ifndef NX_DISABLE_UDP_INFO /* Disable interrupts. */ TX_DISABLE /* Increment the UDP checksum error count. */ (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_checksum_errors++; /* Increment the UDP invalid packets error count. */ (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++; /* Increment the UDP checksum error count for this socket. */ socket_ptr -> nx_udp_socket_checksum_errors++; /* Decrement the total UDP receive packets count. */ (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_packets_received--; /* Decrement the total UDP receive bytes. */ (socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_bytes_received -= (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER); /* Decrement the total UDP receive packets count. */ socket_ptr -> nx_udp_socket_packets_received--; /* Decrement the total UDP receive bytes. */ socket_ptr -> nx_udp_socket_bytes_received -= (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER); /* Restore interrupts. */ TX_RESTORE/* ... */ #endif /* Bad UDP checksum. Release the packet. */ _nx_packet_release(*packet_ptr); }else { ... } ...} else { /* Checksum logic is either disabled for this socket or the received UDP packet checksum was not calculated - get out of the loop. *//* ... */ break; }else { ... } ...} #if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) else { /* Simply break - checksum logic is conditionally disabled. */ break; }else { ... } /* ... */#endif /* defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE) */ }for (;;) { ... } /* At this point, we have a valid UDP packet for the caller. */ /* Remove the UDP header. */ /* Decrease the packet length. */ (*packet_ptr) -> nx_packet_length = (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER); /* Position past the UDP header pointer. */ (*packet_ptr) -> nx_packet_prepend_ptr = (*packet_ptr) -> nx_packet_prepend_ptr + sizeof(NX_UDP_HEADER); /* Update the trace event with the status. */ NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_UDP_SOCKET_RECEIVE, 0, 0, *packet_ptr, (*packet_ptr) -> nx_packet_length); /* Return a successful status to the caller. */ return(NX_SUCCESS); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.