Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define NX_SOURCE_CODE
#include "nx_api.h"
#include "nx_icmp.h"
#include "nx_icmpv6.h"
#include "nx_ip.h"
...
...
_nx_ip_checksum_compute(NX_PACKET *, ULONG, UINT, ULONG *, ULONG *)
Files
loading...
SourceVuSTM32 Libraries and Samplesnetxduocommon/src/nx_ip_checksum_compute.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* 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 Protocol Checksum Computation */ /** */... /**************************************************************************/ /**************************************************************************/ #define NX_SOURCE_CODE /* Include necessary system files. */ #include "nx_api.h" #include "nx_icmp.h" #include "nx_icmpv6.h" #include "nx_ip.h" ... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _nx_ip_checksum_compute PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Yuxin Zhou, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function computes the checksum from the supplied packet */ /* pointer and IP address fields required for the pseudo header. */ /* */ /* INPUT */ /* */ /* packet_ptr Pointer to packet */ /* protocol Protocol type */ /* data_length Size of the protocol payload */ /* src_ip_addr IPv4 or IPv6 address, used in */ /* constructing pseudo header.*/ /* dest_ip_addr IPv4 or IPv6 address, used in */ /* constructing pseudo header.*/ /* */ /* OUTPUT */ /* */ /* computed checksum */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* NetX Duo internal routines */ /* */ /* 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 */ /* */... /**************************************************************************/ USHORT _nx_ip_checksum_compute(NX_PACKET *packet_ptr, ULONG protocol, UINT data_length, ULONG *src_ip_addr, ULONG *dest_ip_addr) { ULONG checksum = 0; USHORT tmp; USHORT *short_ptr; ULONG *long_ptr; #ifndef NX_DISABLE_PACKET_CHAIN ULONG packet_size; #endif /* NX_DISABLE_PACKET_CHAIN */ NX_PACKET *current_packet; ALIGN_TYPE end_ptr; #ifdef FEATURE_NX_IPV6 UINT i; #endif /* For computing TCP/UDP/ICMPv6, we need to include the pseudo header. The ICMPv4 checksum does not cover the pseudo header. *//* ... */ if ((protocol == NX_PROTOCOL_UDP) || #ifdef FEATURE_NX_IPV6 (protocol == NX_PROTOCOL_ICMPV6) || #endif /* FEATURE_NX_IPV6 */ (protocol == NX_PROTOCOL_TCP)) { USHORT *src_ip_short, *dest_ip_short; checksum = protocol; /* The addresses must not be null. */ NX_ASSERT((src_ip_addr != NX_NULL) && (dest_ip_addr != NX_NULL)); /*lint -e{929} -e{740} suppress cast of pointer to pointer, since it is necessary */ src_ip_short = (USHORT *)src_ip_addr; /*lint -e{929} -e{740} suppress cast of pointer to pointer, since it is necessary */ dest_ip_short = (USHORT *)dest_ip_addr; checksum += src_ip_short[0]; checksum += src_ip_short[1]; checksum += dest_ip_short[0]; checksum += dest_ip_short[1]; #ifdef FEATURE_NX_IPV6 /* Note that the IPv6 address is 128 bits/4 words compared with the 32 IPv4 address.*//* ... */ if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { for (i = 2; i < 8; i++) { checksum += dest_ip_short[i]; checksum += src_ip_short[i]; }for (i = 2; i < 8; i++) { ... } }if (packet_ptr -> nx_packet_ip_version == NX_IP_VERSION_V6) { ... } /* ... */#endif /* FEATURE_NX_IPV6 */ /* Take care of data length */ checksum += data_length; /* Fold a 4-byte value into a two byte value */ checksum = (checksum >> 16) + (checksum & 0xFFFF); /* Do it again in case previous operation generates an overflow */ checksum = (checksum >> 16) + (checksum & 0xFFFF); /* Convert to network byte order. */ tmp = (USHORT)checksum; NX_CHANGE_USHORT_ENDIAN(tmp); checksum = tmp; }if ((protocol == NX_PROTOCOL_UDP) || #ifdef FEATURE_NX_IPV6 (protocol == NX_PROTOCOL_ICMPV6) || #endif /* FEATURE_NX_IPV6 */ (protocol == NX_PROTOCOL_TCP)) { ... } /* Now we need to go through the payloads */ /* Setup the pointer to the start of the packet. */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ long_ptr = (ULONG *)packet_ptr -> nx_packet_prepend_ptr; /* Initialize the current packet to the input packet pointer. */ current_packet = packet_ptr; #ifndef NX_DISABLE_PACKET_CHAIN /* Loop the packet. */ while (current_packet) { /* Calculate current packet size. */ /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */ packet_size = (ULONG)(current_packet -> nx_packet_append_ptr - current_packet -> nx_packet_prepend_ptr); /* Calculate the end address in this packet. */ if (data_length > (UINT)packet_size) { /*lint -e{927} -e{923} -e{826} suppress cast of pointer to pointer, since it is necessary */ end_ptr = ((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & (ALIGN_TYPE)(~3); }if (data_length > (UINT)packet_size) { ... } else { #endif /* NX_DISABLE_PACKET_CHAIN */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ end_ptr = (ALIGN_TYPE)current_packet -> nx_packet_prepend_ptr + data_length - 3; #ifndef NX_DISABLE_PACKET_CHAIN }else { ... } #endif /* NX_DISABLE_PACKET_CHAIN */ /* Set the start address in this packet. */ /*lint -e{927} -e{826} suppress cast of pointer to pointer, since it is necessary */ long_ptr = (ULONG *)current_packet -> nx_packet_prepend_ptr; /*lint -e{946} suppress pointer subtraction, since it is necessary. */ if ((ALIGN_TYPE)long_ptr < end_ptr) { /* Calculate the data_length. */ /*lint -e{923} suppress cast of pointer to ULONG. */ data_length -= (UINT)(((end_ptr + 3) & (ALIGN_TYPE)(~3llu)) - (ALIGN_TYPE)long_ptr); /* Loop to calculate the packet's checksum. */ /*lint -e{946} suppress pointer subtraction, since it is necessary. */ while ((ALIGN_TYPE)long_ptr < end_ptr) { checksum += (*long_ptr & NX_LOWER_16_MASK); checksum += (*long_ptr >> NX_SHIFT_BY_16); long_ptr++; }while ((ALIGN_TYPE)long_ptr < end_ptr) { ... } }if ((ALIGN_TYPE)long_ptr < end_ptr) { ... } #ifndef NX_DISABLE_PACKET_CHAIN /* Determine if we are at the end of the current packet. */ if ((data_length > 0) && (current_packet -> nx_packet_next)) { /* Is append_ptr two bytes aligned but not four bytes aligned? */ /*lint -e{923} suppress cast of pointer to ULONG. */ if ((((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & 3) == 2) { /* Yes it is. Process the last two bytes in chaining packets. */ /*lint -e{929} -e{740} suppress cast of pointer to pointer, since it is necessary */ short_ptr = (USHORT *)long_ptr; /*lint -e{929} -e{740} suppress cast of pointer to pointer, since it is necessary */ checksum += *short_ptr; data_length -= 2; }if ((((ALIGN_TYPE)current_packet -> nx_packet_append_ptr) & 3) == 2) { ... } /* We have crossed the packet boundary. Move to the next packet structure. *//* ... */ current_packet = current_packet -> nx_packet_next; }if ((data_length > 0) && (current_packet -> nx_packet_next)) { ... } else { /* End the loop. */ current_packet = NX_NULL; }else { ... } }/* ... */ while (current_packet) { ... }#endif /* NX_DISABLE_PACKET_CHAIN */ /* Determine if there is only one byte left. */ if (data_length) { /* Set the short_ptr. */ short_ptr = (USHORT *)(long_ptr); /* Check the data length. */ if (data_length == 1) { *((UCHAR *)short_ptr + 1) = 0; }if (data_length == 1) { ... } else if (data_length == 3) { checksum += *short_ptr; short_ptr++; *((UCHAR *)short_ptr + 1) = 0; }else if (data_length == 3) { ... } checksum += *short_ptr; }if (data_length) { ... } /* Fold a 4-byte value into a two byte value */ checksum = (checksum >> 16) + (checksum & 0xFFFF); /* Do it again in case previous operation generates an overflow */ checksum = (checksum >> 16) + (checksum & 0xFFFF); /* Convert to host byte order. */ tmp = (USHORT)checksum; NX_CHANGE_USHORT_ENDIAN(tmp); /* Return the computed checksum. */ return(tmp); }{ ... }
Details