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
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** NetX Component */
/** */
/** Internet Protocol (IP) */
/** */
/**************************************************************************/
/**************************************************************************/
#define NX_SOURCE_CODE
/* Include necessary system files. */
#include "nx_api.h"
#include "nx_packet.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _nx_packet_data_extract_offset PORTABLE C */
/* 6.4.3 */
/* AUTHOR */
/* */
/* Yuxin Zhou, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function copies data from a NetX packet (or packet chain) into */
/* the supplied user buffer. If an empty packet (no data) is */
/* received, zero bytes are copied, and the function returns without */
/* errors. */
/* */
/* Note that this function extracts data from a packet into user */
/* supplied buffer. It does not modify packet internal state */
/* information. The data being extracted is still available in the */
/* original packet for consumption again. */
/* */
/* INPUT */
/* */
/* packet_ptr Pointer to the source packet */
/* offset Offset from start of data */
/* buffer_start Pointer to destination data area */
/* buffer_length Size in bytes */
/* bytes_copied Number of bytes copied */
/* */
/* OUTPUT */
/* */
/* status Completion status */
/* */
/* CALLS */
/* */
/* None */
/* */
/* CALLED BY */
/* */
/* Application Code */
/* */
/**************************************************************************/
UINT _nx_packet_data_extract_offset(NX_PACKET *packet_ptr, ULONG offset, VOID *buffer_start, ULONG buffer_length, ULONG *bytes_copied)
{
ULONG remaining_bytes;
UCHAR *source_ptr;
UCHAR *destination_ptr;
ULONG offset_bytes;
#ifndef NX_DISABLE_PACKET_CHAIN
ULONG packet_fragment_length;
#endif /* NX_DISABLE_PACKET_CHAIN */
ULONG bytes_to_copy;
NX_PACKET *working_packet_ptr;
working_packet_ptr = packet_ptr;
/* Check for an invalid offset or packet length. */
if (offset >= working_packet_ptr -> nx_packet_length)
{
/* Note: A zero offset with a packet of zero length is ok. */
if (offset == 0)
{
*bytes_copied = 0;
return(NX_SUCCESS);
}
/* Otherwise, this is an invalid offset or packet length. */
return(NX_PACKET_OFFSET_ERROR);
}
/* Initialize the source pointer to NULL. */
source_ptr = NX_NULL;
/* Traverse packet chain to offset. */
offset_bytes = offset;
#ifndef NX_DISABLE_PACKET_CHAIN
while (working_packet_ptr)
{
/*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
packet_fragment_length = (ULONG)((working_packet_ptr -> nx_packet_append_ptr - working_packet_ptr -> nx_packet_prepend_ptr));
/* Determine if we are at the offset location fragment in the packet chain */
if (packet_fragment_length > offset_bytes)
{
/* Setup loop to copy from this packet. */
source_ptr = working_packet_ptr -> nx_packet_prepend_ptr + offset_bytes;
/* Yes, get out of this loop. */
break;
}
/* Decrement the remaining offset bytes*/
offset_bytes = offset_bytes - packet_fragment_length;
/* Move to next packet. */
working_packet_ptr = working_packet_ptr -> nx_packet_next;
}
#else /* NX_DISABLE_PACKET_CHAIN */
/* Setup loop to copy from this packet. */
source_ptr = working_packet_ptr -> nx_packet_prepend_ptr + offset_bytes;
#endif /* NX_DISABLE_PACKET_CHAIN */
/* Check for a valid source pointer. */
if (source_ptr == NX_NULL)
{
return(NX_PACKET_OFFSET_ERROR);
}
/* Setup the destination pointer. */
destination_ptr = buffer_start;
bytes_to_copy = (packet_ptr -> nx_packet_length - offset);
/* Pickup the amount of bytes to copy. */
if (bytes_to_copy < buffer_length)
{
*bytes_copied = bytes_to_copy; /* the amount of bytes returned to the caller */
remaining_bytes = bytes_to_copy; /* for use in the copy loop */
}
else
{
*bytes_copied = buffer_length;
remaining_bytes = buffer_length;
}
#ifndef NX_DISABLE_PACKET_CHAIN
/* Loop to copy bytes from packet(s). */
while (working_packet_ptr && remaining_bytes)
{
#endif /* NX_DISABLE_PACKET_CHAIN */
/* Calculate bytes to copy. */
/*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
bytes_to_copy = (ULONG)(working_packet_ptr -> nx_packet_append_ptr - source_ptr);
if (remaining_bytes < bytes_to_copy)
{
bytes_to_copy = remaining_bytes;
}
/* Copy data from this packet. */
memcpy(destination_ptr, source_ptr, bytes_to_copy); /* Use case of memcpy is verified. lgtm[cpp/banned-api-usage-required-any] */
/* Update the pointers. */
destination_ptr += bytes_to_copy;
remaining_bytes -= bytes_to_copy;
#ifndef NX_DISABLE_PACKET_CHAIN
/* Move to next packet. */
working_packet_ptr = working_packet_ptr -> nx_packet_next;
/* Check for a next packet. */
if (working_packet_ptr)
{
/* Setup new source pointer. */
source_ptr = working_packet_ptr -> nx_packet_prepend_ptr;
}
}
#endif /* NX_DISABLE_PACKET_CHAIN */
/* If trace is enabled, insert this event into the trace buffer. */
NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_DATA_EXTRACT_OFFSET, packet_ptr, buffer_length, *bytes_copied, 0, NX_TRACE_PACKET_EVENTS, 0, 0);
/* Return successful completion. */
return(NX_SUCCESS);
}
|