1 /***************************************************************************
2 * Copyright (c) 2024 Microsoft Corporation
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the MIT License which is available at
6 * https://opensource.org/licenses/MIT.
7 *
8 * SPDX-License-Identifier: MIT
9 **************************************************************************/
10
11
12 /**************************************************************************/
13 /**************************************************************************/
14 /** */
15 /** NetX Component */
16 /** */
17 /** Packet Pool Management (Packet) */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22 #define NX_SOURCE_CODE
23
24
25 /* Include necessary system files. */
26
27 #include "nx_api.h"
28 #include "nx_packet.h"
29
30
31 /**************************************************************************/
32 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _nx_packet_data_retrieve PORTABLE C */
36 /* 6.1 */
37 /* AUTHOR */
38 /* */
39 /* Yuxin Zhou, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function copies data from a NetX packet (or packet chain) into */
44 /* the supplied user buffer. */
45 /* */
46 /* INPUT */
47 /* */
48 /* packet_ptr Pointer to the source packet */
49 /* buffer_start Pointer to destination area */
50 /* bytes_copied Number of bytes copied */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* status Completion status */
55 /* */
56 /* CALLS */
57 /* */
58 /* None */
59 /* */
60 /* CALLED BY */
61 /* */
62 /* Application Code */
63 /* */
64 /* RELEASE HISTORY */
65 /* */
66 /* DATE NAME DESCRIPTION */
67 /* */
68 /* 05-19-2020 Yuxin Zhou Initial Version 6.0 */
69 /* 09-30-2020 Yuxin Zhou Modified comment(s), and */
70 /* verified memcpy use cases, */
71 /* resulting in version 6.1 */
72 /* */
73 /**************************************************************************/
_nx_packet_data_retrieve(NX_PACKET * packet_ptr,VOID * buffer_start,ULONG * bytes_copied)74 UINT _nx_packet_data_retrieve(NX_PACKET *packet_ptr, VOID *buffer_start, ULONG *bytes_copied)
75 {
76
77 ULONG remaining_bytes;
78 UCHAR *destination_ptr;
79 ULONG bytes_to_copy;
80
81 /* If trace is enabled, insert this event into the trace buffer. */
82 NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_DATA_RETRIEVE, packet_ptr, buffer_start, bytes_copied, 0, NX_TRACE_PACKET_EVENTS, 0, 0);
83
84 /* Setup the destination pointer. */
85 destination_ptr = buffer_start;
86
87 /* Pickup the amount of bytes to copy. */
88 *bytes_copied = packet_ptr -> nx_packet_length;
89
90 /* Setup the remaining bytes. */
91 remaining_bytes = packet_ptr -> nx_packet_length;
92
93 #ifndef NX_DISABLE_PACKET_CHAIN
94 /* Loop to copy bytes from packet(s). */
95 while (packet_ptr)
96 {
97 #endif /* NX_DISABLE_PACKET_CHAIN */
98
99 /* Calculate the bytes to copy in this packet. */
100 /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
101 bytes_to_copy = (ULONG)(packet_ptr -> nx_packet_append_ptr - packet_ptr -> nx_packet_prepend_ptr);
102
103 /* Copy data to destination. */
104 /* Note: The buffer size must be not less than packet_ptr -> nx_packet_length. */
105 memcpy(destination_ptr, packet_ptr -> nx_packet_prepend_ptr, bytes_to_copy); /* Use case of memcpy is verified. The buffer is provided by user. lgtm[cpp/banned-api-usage-required-any] */
106
107 remaining_bytes -= bytes_to_copy;
108 destination_ptr += bytes_to_copy;
109
110 #ifndef NX_DISABLE_PACKET_CHAIN
111 /* Move to next packet. */
112 packet_ptr = packet_ptr -> nx_packet_next;
113 }
114 #endif /* NX_DISABLE_PACKET_CHAIN */
115
116 /* Determine if the packet chain was valid. */
117 if (remaining_bytes)
118 {
119
120 /* Invalid packet chain. Calculate the actual number of bytes
121 copied. */
122 *bytes_copied = *bytes_copied - remaining_bytes;
123
124 /* Return an error. */
125 return(NX_INVALID_PACKET);
126 }
127
128 /* Return successful completion. */
129 return(NX_SUCCESS);
130 }
131
132