Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_packet.h"
#include "nx_igmp.h"
...
_nx_igmp_packet_process(NX_IP *, NX_PACKET *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesnetxduocommon/src/nx_igmp_packet_process.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 */ /** */ /** Internet Group Management Protocol (IGMP) */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_packet.h" #include "nx_igmp.h" #ifndef NX_DISABLE_IPV4... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_igmp_packet_process PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function handles reception of IGMP packets. There are two types*/ /* of IGMP packets. Routers will send IGMP query messages and other */ /* send responses intended for the router but will be seen by all other */ /* hosts belonging to the same multicast group. In the latter case the */ /* other hosts will cancel their own response for the same multicast */ /* group. */ /* */ /* INPUT */ /* */ /* ip_ptr IP instance pointer */ /* packet_ptr IGMP packet pointer */ /* */ /* OUTPUT */ /* */ /* None */ /* */ /* CALLS */ /* */ /* _nx_packet_release Release IGMP packet */ /* tx_time_get Get current time */ /* */ /* CALLED BY */ /* */ /* _nx_igmp_packet_receive IGMP packet receive */ /* _nx_igmp_queue_process IGMP queue processing */ /* */ /* 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 */ /* */... /**************************************************************************/ VOID _nx_igmp_packet_process(NX_IP *ip_ptr, NX_PACKET *packet_ptr) { UINT i; ULONG update_time; NX_IGMP_HEADER *header_ptr; USHORT max_update_time; ULONG checksum; ULONG length; UCHAR *word_ptr; NX_PACKET *current_packet; ULONG long_temp; USHORT short_temp; /* Add debug information. */ NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr); /* Setup a pointer to the IGMP packet header. */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ header_ptr = (NX_IGMP_HEADER *)packet_ptr -> nx_packet_prepend_ptr; #ifdef NX_ENABLE_INTERFACE_CAPABILITY if (!(packet_ptr -> nx_packet_address.nx_packet_interface_ptr -> nx_interface_capability_flag & NX_INTERFACE_CAPABILITY_IGMP_RX_CHECKSUM)) #endif /* FEATURE_LINK_CAPABILITY */ { /* First verify the checksum is correct. */ /* Setup the length of the packet checksum. */ length = packet_ptr -> nx_packet_length; /* Determine if we need to add a padding byte. */ if (((length / sizeof(USHORT)) * sizeof(USHORT)) != length) { /* We have single byte alignment and we need two byte alignment. */ length++; #ifndef NX_DISABLE_PACKET_CHAIN /* Determine if there is a last packet pointer. */ if (packet_ptr -> nx_packet_last) { /* Multi-packet message, add a zero byte at the end. */ *((packet_ptr -> nx_packet_last) -> nx_packet_append_ptr) = 0; }if (packet_ptr -> nx_packet_last) { ... } else { #endif /* Write a zero byte at the end of the first and only packet. */ *(packet_ptr -> nx_packet_append_ptr) = 0; #ifndef NX_DISABLE_PACKET_CHAIN }else { ... } #endif }if (((length / sizeof(USHORT)) * sizeof(USHORT)) != length) { ... } /* Setup the pointer to the start of the packet. */ word_ptr = (UCHAR *)packet_ptr -> nx_packet_prepend_ptr; /* Initialize the current packet to the input packet pointer. */ current_packet = packet_ptr; checksum = 0; /* Loop to calculate the checksum over the entire packet. */ while (length) { /* Determine if there is at least one ULONG left. */ if ((UINT)(current_packet -> nx_packet_append_ptr - word_ptr) >= sizeof(ULONG)) { /* Pickup a whole ULONG. */ long_temp = *((ULONG *)word_ptr); /* Add upper 16-bits into checksum. */ checksum = checksum + (long_temp >> NX_SHIFT_BY_16); /* Check for carry bits. */ if (checksum & NX_CARRY_BIT) { checksum = (checksum & NX_LOWER_16_MASK) + 1; }if (checksum & NX_CARRY_BIT) { ... } /* Add lower 16-bits into checksum. */ checksum = checksum + (long_temp & NX_LOWER_16_MASK); /* Check for carry bits. */ if (checksum & NX_CARRY_BIT) { checksum = (checksum & NX_LOWER_16_MASK) + 1; }if (checksum & NX_CARRY_BIT) { ... } /* Move the word pointer and decrease the length. */ word_ptr = word_ptr + sizeof(ULONG); length = length - (ULONG)sizeof(ULONG); }if ((UINT)(current_packet -> nx_packet_append_ptr - word_ptr) >= sizeof(ULONG)) { ... } else { /* Pickup the 16-bit word. */ short_temp = *((USHORT *)word_ptr); /* Add next 16-bit word into checksum. */ checksum = checksum + short_temp; /* Check for carry bits. */ if (checksum & NX_CARRY_BIT) { checksum = (checksum & NX_LOWER_16_MASK) + 1; }if (checksum & NX_CARRY_BIT) { ... } /* Move the word pointer and decrease the length. */ word_ptr = word_ptr + sizeof(USHORT); length = length - (ULONG)sizeof(USHORT); }else { ... } #ifndef NX_DISABLE_PACKET_CHAIN /* Determine if we are at the end of the current packet. */ if ((word_ptr >= (UCHAR *)current_packet -> nx_packet_append_ptr) && (current_packet -> nx_packet_next)) { /* We have crossed the packet boundary. Move to the next packet structure. *//* ... */ current_packet = current_packet -> nx_packet_next; /* Setup the new word pointer. */ word_ptr = (UCHAR *)current_packet -> nx_packet_prepend_ptr; }if ((word_ptr >= (UCHAR *)current_packet -> nx_packet_append_ptr) && (current_packet -> nx_packet_next)) { ... } /* ... */#endif }while (length) { ... } checksum = ~checksum & NX_LOWER_16_MASK; /* Determine if the checksum is valid. */ if (checksum) { /* It is not. By RFC requirements we should not accept this packet. */ #ifndef NX_DISABLE_IGMP_INFO /* Increment the IGMP invalid packet error. */ ip_ptr -> nx_ip_igmp_invalid_packets++; /* Increment the IGMP checksum error count. */ ip_ptr -> nx_ip_igmp_checksum_errors++;/* ... */ #endif /* NX_DISABLE_IGMP_INFO */ /* Toss this IGMP packet out. */ _nx_packet_release(packet_ptr); return; }if (checksum) { ... } ...} /* Swap the IGMP headers to host byte order. */ NX_CHANGE_ULONG_ENDIAN(header_ptr -> nx_igmp_header_word_0); NX_CHANGE_ULONG_ENDIAN(header_ptr -> nx_igmp_header_word_1); /* If trace is enabled, insert this event into the trace buffer. */ NX_TRACE_IN_LINE_INSERT(NX_TRACE_INTERNAL_IGMP_RECEIVE, ip_ptr, *(((ULONG *)packet_ptr -> nx_packet_prepend_ptr) - 2), packet_ptr, header_ptr -> nx_igmp_header_word_0, NX_TRACE_INTERNAL_EVENTS, 0, 0); /* Determine the type of IGMP message received. Note that an IGMPv1 host will respond to an IGMPv2 general query but not process the maximum response time field. *//* ... */ if ((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_TYPE_MASK) == NX_IGMP_ROUTER_QUERY_TYPE) { #ifndef NX_DISABLE_IGMP_INFO /* Increment the IGMP queries received count. */ ip_ptr -> nx_ip_igmp_queries_received++;/* ... */ #endif /* Set the max response time recommended by RFC 1112 set by host in seconds. In a IGMPv2 network, the router may set a different max time in its IGMP membership queries. *//* ... */ max_update_time = NX_IGMP_MAX_UPDATE_TIME; #ifndef NX_DISABLE_IGMPV2 /* Determine the IGMP version the sender (router) is using. */ /* Is the max response time non zero? */ if ((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_MAX_RESP_TIME_MASK) != NX_NULL) { /* Yes, this must be an IGMPv2 router. */ ip_ptr -> nx_ip_igmp_router_version = NX_IGMP_HOST_VERSION_2; /* Parse the max response time from the IGMP header. */ max_update_time = (USHORT)((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_MAX_RESP_TIME_MASK) >> 16) & 0x0000FF; /* Convert from tenths of a second to seconds. */ max_update_time /= 10; }if ((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_MAX_RESP_TIME_MASK) != NX_NULL) { ... } else { /* No; IGMPv1 requires setting this field to zero. */ ip_ptr -> nx_ip_igmp_router_version = NX_IGMP_HOST_VERSION_1; }else { ... } /* ... */ #endif /* Then generate a random update time initially in timer ticks for the delay. */ update_time = tx_time_get() & 0xF; /* Check we have a valid non zero update time that does not exceed the maximum response time. *//* ... */ if ((update_time > max_update_time) || (update_time == 0)) { /* If not, wrap the update time back to one second. */ update_time = 1; }if ((update_time > max_update_time) || (update_time == 0)) { ... } /* Loop through the multicast join list and assign an arbitrary timeout to respond between 1 and maximum response time for each group. *//* ... */ for (i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) { /* Is there a group address in this slot? */ if (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list == NX_NULL) { /* No, skip doing further processing. */ continue; }if (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list == NX_NULL) { ... } /* Does the group address in the header match our join list? */ if ((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list != header_ptr -> nx_igmp_header_word_1) && /* or is this a general membership query? */ (header_ptr -> nx_igmp_header_word_1 != NX_NULL)) { /* No; so no need to update the timer, skip to the next group in the host list. */ continue; }if ((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list != header_ptr -> nx_igmp_header_word_1) && /* or is this a general membership query? */ (header_ptr -> nx_igmp_header_word_1 != NX_NULL)) { ... } /* Is the current host group running timer less than the max delay? */ if (((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time < max_update_time) && (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time != NX_NULL)) || (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time == NX_WAIT_FOREVER)) { /* Yes; Let the current timer timeout. Skip to the next group. */ continue; }if (((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time < max_update_time) && (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time != NX_NULL)) || (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time == NX_WAIT_FOREVER)) { ... } /* Set the timeout for this multicast group. */ ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time = update_time; /* Then increment the update time for the next host group so the update/expiration times are separated by one second. This avoids bursts of IGMP reports to the server. *//* ... */ update_time++; /* Check after each multicast group that we have not exceeded the maximum response time. */ if (update_time > max_update_time) { /* We have, so wrap the update time back to one. */ update_time = 1; }if (update_time > max_update_time) { ... } }for (i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) { ... } }if ((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_TYPE_MASK) == NX_IGMP_ROUTER_QUERY_TYPE) { ... } #ifndef NX_DISABLE_IGMPV2 /* Is this another IGMPv1 host's join request? */ else if (((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_TYPE_MASK) == NX_IGMP_HOST_RESPONSE_TYPE) || /* ...Or an IGMPv2 host's join request? */ ((header_ptr -> nx_igmp_header_word_0 & NX_IGMPV2_TYPE_MASK) == NX_IGMP_HOST_V2_JOIN_TYPE))/* ... */ #else /* Is this another IGMPv1 host's join request? */ else if ((header_ptr -> nx_igmp_header_word_0 & NX_IGMP_TYPE_MASK) == NX_IGMP_HOST_RESPONSE_TYPE)/* ... */ #endif { /* Yes; Loop through the host multicast join list to find a matching group. */ for (i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) { /* Compare the group address in the header with the host list. Is this a match? */ if ((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list == header_ptr -> nx_igmp_header_word_1) && (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time != NX_WAIT_FOREVER)) { /* Yes; Clear the update time. This will cancel sending a join request for the same multicast group. *//* ... */ ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time = 0; break; }if ((ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_join_list == header_ptr -> nx_igmp_header_word_1) && (ip_ptr -> nx_ipv4_multicast_entry[i].nx_ipv4_multicast_update_time != NX_WAIT_FOREVER)) { ... } }for (i = 0; i < NX_MAX_MULTICAST_GROUPS; i++) { ... } ...} /* Release the IGMP packet. */ _nx_packet_release(packet_ptr); }{ ... } #endif/* ... */ /* !NX_DISABLE_IPV4 */