1
10
13
14
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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
226
227
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
251
252
253
258
259
263
264
265
266
271
272
273
274
275
276
277
280
281
282
283
284
285
286
287
288
289
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
324
325
326
327
328
329
330
331
332
333
334
335
338
339
340
341
342
343
344
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
394
395
396
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_ip.h"
#ifdef FEATURE_NX_IPV6
#include "nx_ipv6.h"
#endif
#include "tx_thread.h"
#include "nx_packet.h"
#include "nx_udp.h"
#ifdef NX_IPSEC_ENABLE
#include "nx_ipsec.h"
#endif
...
...
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
#if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
UINT compute_checksum = 1;
#endif
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_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);
*packet_ptr = NX_NULL;
for (;;)
{
TX_DISABLE
if (!socket_ptr -> nx_udp_socket_bound_next)
{
TX_RESTORE
return(NX_NOT_BOUND);
}if (!socket_ptr -> nx_udp_socket_bound_next) { ... }
if (socket_ptr -> nx_udp_socket_receive_head)
{
*packet_ptr = socket_ptr -> nx_udp_socket_receive_head;
socket_ptr -> nx_udp_socket_receive_head = (*packet_ptr) -> nx_packet_queue_next;
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) { ... }
socket_ptr -> nx_udp_socket_receive_count--;
TX_RESTORE
}if (socket_ptr -> nx_udp_socket_receive_head) { ... }
else
{
if (wait_option)
{
thread_ptr = _tx_thread_current_ptr;
thread_ptr -> tx_thread_suspend_cleanup = _nx_udp_receive_cleanup;
/* ... */
thread_ptr -> tx_thread_suspend_control_block = (void *)socket_ptr;
thread_ptr -> tx_thread_additional_suspend_info = (void *)packet_ptr;
if (socket_ptr -> nx_udp_socket_receive_suspension_list)
{
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
{
/* ... */
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 { ... }
socket_ptr -> nx_udp_socket_receive_suspended_count++;
thread_ptr -> tx_thread_state = TX_TCP_IP;
thread_ptr -> tx_thread_suspending = TX_TRUE;
_tx_thread_preempt_disable++;
thread_ptr -> tx_thread_timer.tx_timer_internal_remaining_ticks = wait_option;
TX_RESTORE
_tx_thread_system_suspend(thread_ptr);
if (thread_ptr -> tx_thread_suspend_status != NX_SUCCESS)
{
return(thread_ptr -> tx_thread_suspend_status);
}if (thread_ptr -> tx_thread_suspend_status != NX_SUCCESS) { ... }
/* ... */
}if (wait_option) { ... }
else
{
TX_RESTORE
*packet_ptr = NX_NULL;
return(NX_NO_PACKET);
}else { ... }
}else { ... }
#ifdef NX_ENABLE_INTERFACE_CAPABILITY
#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
#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
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
#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
#if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
if (compute_checksum)
#endif
{
/* ... */
temp_ptr = (ULONG *)(*packet_ptr) -> nx_packet_prepend_ptr;
if ((!socket_ptr -> nx_udp_socket_disable_checksum && (*(temp_ptr + 1) & NX_LOWER_16_MASK)) ||
((*packet_ptr) -> nx_packet_ip_version == NX_IP_VERSION_V6))
{
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;
udp_header_ptr = (NX_UDP_HEADER *)(current_ptr -> nx_packet_prepend_ptr);/* ... */
#endif
#ifndef NX_DISABLE_IPV4
if (current_ptr -> nx_packet_ip_version == NX_IP_VERSION_V4)
{
NX_IPV4_HEADER *ipv4_header;
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
#ifdef FEATURE_NX_IPV6
if (current_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6)
{
NX_IPV6_HEADER *ipv6_header;
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
#ifdef NX_LITTLE_ENDIAN
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
checksum = _nx_ip_checksum_compute(current_ptr, NX_PROTOCOL_UDP,
(UINT)current_ptr -> nx_packet_length,
ip_src_addr,
ip_dest_addr);
#ifdef NX_LITTLE_ENDIAN
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
checksum = NX_LOWER_16_MASK & ~checksum;
if (checksum == 0)
{
break;
}if (checksum == 0) { ... }
else
{
#ifndef NX_DISABLE_UDP_INFO
TX_DISABLE
(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_checksum_errors++;
(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_invalid_packets++;
socket_ptr -> nx_udp_socket_checksum_errors++;
(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_packets_received--;
(socket_ptr -> nx_udp_socket_ip_ptr) -> nx_ip_udp_bytes_received -= (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER);
socket_ptr -> nx_udp_socket_packets_received--;
socket_ptr -> nx_udp_socket_bytes_received -= (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER);
TX_RESTORE/* ... */
#endif
_nx_packet_release(*packet_ptr);
}else { ... }
...}
else
{
/* ... */
break;
}else { ... }
...}
#if defined(NX_DISABLE_UDP_RX_CHECKSUM) || defined(NX_ENABLE_INTERFACE_CAPABILITY) || defined(NX_IPSEC_ENABLE)
else
{
break;
}else { ... }
/* ... */#endif
}for (;;) { ... }
(*packet_ptr) -> nx_packet_length = (*packet_ptr) -> nx_packet_length - (ULONG)sizeof(NX_UDP_HEADER);
(*packet_ptr) -> nx_packet_prepend_ptr = (*packet_ptr) -> nx_packet_prepend_ptr + sizeof(NX_UDP_HEADER);
NX_TRACE_EVENT_UPDATE(trace_event, trace_timestamp, NX_TRACE_UDP_SOCKET_RECEIVE, 0, 0, *packet_ptr, (*packet_ptr) -> nx_packet_length);
return(NX_SUCCESS);
}{ ... }