99#endif
1010#include < stddef.h>
1111
12+ #include " DeprecationUtils.h"
13+
1214// / @file
1315
1416// / @namespace pcpp
@@ -262,7 +264,7 @@ namespace pcpp
262264 int m_RawDataLen = 0 ;
263265 int m_FrameLength = 0 ;
264266 timespec m_TimeStamp{}; // Zero initialized
265- bool m_DeleteRawDataAtDestructor = true ;
267+ bool m_OwnsRawData = false ;
266268 bool m_RawPacketSet = false ;
267269 LinkLayerType m_LinkLayerType = LinkLayerType::LINKTYPE_ETHERNET ;
268270
@@ -273,7 +275,7 @@ namespace pcpp
273275 // / - data pointer is set to nullptr
274276 // / - data length is set to 0
275277 // / - frame length is set to 0
276- // / - deleteRawDataAtDestructor is set to 'true '
278+ // / - takeOwnership is set to 'false '
277279 // / - timestamp is set to 0
278280 // / - layer type is set to Ethernet
279281 RawPacket () = default ;
@@ -285,10 +287,10 @@ namespace pcpp
285287 // / @param[in] pRawData A pointer to the raw data
286288 // / @param[in] rawDataLen The raw data length in bytes
287289 // / @param[in] timestamp The timestamp packet was received by the NIC (in usec precision)
288- // / @param[in] deleteRawDataAtDestructor An indicator whether raw data pointer should be freed when the instance
289- // / is freed or not. If set to 'true' than pRawData will be freed when instanced is being freed
290+ // / @param[in] takeOwnership If 'true' the RawPacket will take ownership of the pRawData pointer and will
291+ // / free it when the RawPacket instance is destroyed or the buffer is replaced.
290292 // / @param[in] layerType The link layer type of this raw packet. The default is Ethernet
291- RawPacket (const uint8_t * pRawData, int rawDataLen, timeval timestamp, bool deleteRawDataAtDestructor ,
293+ RawPacket (const uint8_t * pRawData, int rawDataLen, timeval timestamp, bool takeOwnership ,
292294 LinkLayerType layerType = LINKTYPE_ETHERNET );
293295
294296 // / A constructor that receives a pointer to the raw data (allocated elsewhere). This constructor is usually
@@ -298,13 +300,13 @@ namespace pcpp
298300 // / @param[in] pRawData A pointer to the raw data
299301 // / @param[in] rawDataLen The raw data length in bytes
300302 // / @param[in] timestamp The timestamp packet was received by the NIC (in nsec precision)
301- // / @param[in] deleteRawDataAtDestructor An indicator whether raw data pointer should be freed when the instance
302- // / is freed or not. If set to 'true' than pRawData will be freed when instanced is being freed
303+ // / @param[in] takeOwnership If 'true' the RawPacket will take ownership of the pRawData pointer and will
304+ // / free it when the RawPacket instance is destroyed or the buffer is replaced.
303305 // / @param[in] layerType The link layer type of this raw packet. The default is Ethernet
304- RawPacket (const uint8_t * pRawData, int rawDataLen, timespec timestamp, bool deleteRawDataAtDestructor ,
306+ RawPacket (const uint8_t * pRawData, int rawDataLen, timespec timestamp, bool takeOwnership ,
305307 LinkLayerType layerType = LINKTYPE_ETHERNET );
306308
307- // / A destructor for this class. Frees the raw data if deleteRawDataAtDestructor was set to 'true'
309+ // / A destructor for this class. Frees the raw data if takeOwnership was set to 'true'
308310 virtual ~RawPacket ();
309311
310312 // / A copy constructor that copies all data from another instance. Notice all raw data is copied (using memcpy),
@@ -313,7 +315,7 @@ namespace pcpp
313315 RawPacket (const RawPacket& other);
314316
315317 // / Assignment operator overload for this class. When using this operator on an already initialized RawPacket
316- // / instance, the original raw data is freed first if deleteRawDataAtDestructor was set to 'true'.
318+ // / instance, the original raw data is freed first if takeOwnership was set to 'true'.
317319 // / Then the other instance is copied to this instance, the same way the copy constructor works
318320 // / @param[in] other The instance to copy from
319321 RawPacket& operator =(const RawPacket& other);
@@ -328,7 +330,7 @@ namespace pcpp
328330 return 0 ;
329331 }
330332
331- // / Set a raw data. If data was already set and deleteRawDataAtDestructor was set to 'true' the old data will be
333+ // / Set a raw data. If data was already set and takeOwnership was set to 'true' the old data will be
332334 // / freed first
333335 // / @param[in] pRawData A pointer to the new raw data
334336 // / @param[in] rawDataLen The new raw data length in bytes
@@ -338,10 +340,36 @@ namespace pcpp
338340 // / actual packet length. This parameter represents the packet length. This parameter is optional, if not set or
339341 // / set to -1 it is assumed both lengths are equal
340342 // / @return True if raw data was set successfully, false otherwise
341- virtual bool setRawData (const uint8_t * pRawData, int rawDataLen, timeval timestamp,
342- LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
343+ // / @deprecated This method does not update ownership of the pRawData pointer, inheriting the previous buffer's
344+ // / ownership. Use the overload that takes bool takeOwnership parameter for explicit control.
345+ // / @remarks Derived classes may not support using the raw data buffer directly.
346+ // / Users should not rely on the RawPacket always writing to the provided pointer location,
347+ // / and instead get the raw data pointer using getRawData().
348+ PCPP_DEPRECATED (" Use the overload that takes bool takeOwnership parameter for explicit control." )
349+ bool setRawData (const uint8_t * pRawData, int rawDataLen, timeval timestamp,
350+ LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
343351
344- // / Set a raw data. If data was already set and deleteRawDataAtDestructor was set to 'true' the old data will be
352+ // / @brief Assign a buffer to use as raw data.
353+ // /
354+ // / If the RawPacket already had raw data, it will be disposed of accordingly.
355+ // /
356+ // / @param pRawData A pointer to the new raw data buffer.
357+ // / @param rawDataLen The length of the new raw data buffer in bytes.
358+ // / @param takeOwnership If true, the RawPacket will take ownership of the buffer and will be responsible for
359+ // / freeing it.
360+ // / @param timestamp The timestamp packet was received by the NIC (in usec precision).
361+ // / @param layerType The link layer type for this raw data.
362+ // / @param frameLength When reading from pcap files, sometimes the captured length is different from the
363+ // / actual packet length. This parameter represents the packet length. This parameter is optional, if not set or
364+ // / set to -1 it is assumed both lengths are equal
365+ // / @return True if raw data was set successfully, false otherwise.
366+ // / @remarks Derived classes may not support using the raw data buffer directly.
367+ // / Users should not rely on the RawPacket always writing to the provided pointer location,
368+ // / and instead get the raw data pointer using getRawData().
369+ bool setRawData (const uint8_t * pRawData, int rawDataLen, bool takeOwnership, timeval timestamp,
370+ LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
371+
372+ // / Set a raw data. If data was already set and takeOwnership was set to 'true' the old data will be
345373 // / freed first
346374 // / @param[in] pRawData A pointer to the new raw data
347375 // / @param[in] rawDataLen The new raw data length in bytes
@@ -351,8 +379,34 @@ namespace pcpp
351379 // / actual packet length. This parameter represents the packet length. This parameter is optional, if not set or
352380 // / set to -1 it is assumed both lengths are equal
353381 // / @return True if raw data was set successfully, false otherwise
354- virtual bool setRawData (const uint8_t * pRawData, int rawDataLen, timespec timestamp,
355- LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
382+ // / @deprecated This method does not update ownership of the pRawData pointer, inheriting the previous buffer's
383+ // / ownership. Use the overload that takes bool takeOwnership parameter for explicit control.
384+ // / @remarks Derived classes may not support using the raw data buffer directly.
385+ // / Users should not rely on the RawPacket always writing to the provided pointer location,
386+ // / and instead get the raw data pointer using getRawData().
387+ PCPP_DEPRECATED (" Use the overload that takes bool takeOwnership parameter for explicit control." )
388+ bool setRawData (const uint8_t * pRawData, int rawDataLen, timespec timestamp,
389+ LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
390+
391+ // / @brief Assign a buffer to use as raw data.
392+ // /
393+ // / If the RawPacket already had raw data, it will be disposed of accordingly.
394+ // /
395+ // / @param pRawData A pointer to the new raw data buffer.
396+ // / @param rawDataLen The length of the new raw data buffer in bytes.
397+ // / @param takeOwnership If true, the RawPacket will take ownership of the buffer and will be responsible for
398+ // / freeing it.
399+ // / @param timestamp The timestamp packet was received by the NIC (in nsec precision).
400+ // / @param layerType The link layer type for this raw data.
401+ // / @param frameLength When reading from pcap files, sometimes the captured length is different from the
402+ // / actual packet length. This parameter represents the packet length. This parameter is optional, if not set or
403+ // / set to -1 it is assumed both lengths are equal
404+ // / @return True if raw data was set successfully, false otherwise.
405+ // / @remarks Derived classes may not support using the raw data buffer directly.
406+ // / Users should not rely on the RawPacket always writing to the provided pointer location,
407+ // / and instead get the raw data pointer using getRawData().
408+ bool setRawData (const uint8_t * pRawData, int rawDataLen, bool takeOwnership, timespec timestamp,
409+ LinkLayerType layerType = LINKTYPE_ETHERNET , int frameLength = -1 );
356410
357411 // / Initialize a raw packet with data. The main difference between this method and setRawData() is that
358412 // / setRawData() is meant for replacing the data in an existing raw packet, whereas this method is meant to be
@@ -362,6 +416,8 @@ namespace pcpp
362416 // / @param timestamp The timestamp packet was received by the NIC (in nsec precision)
363417 // / @param layerType The link layer type for this raw data
364418 // / @return True if raw data was set successfully, false otherwise
419+ // / @deprecated Prefer using setRawData() with takeOwnership `false`.
420+ PCPP_DEPRECATED (" Prefer using setRawData() with takeOwnership `false`." )
365421 bool initWithRawData (const uint8_t * pRawData, int rawDataLen, timespec timestamp,
366422 LinkLayerType layerType = LINKTYPE_ETHERNET );
367423
@@ -424,7 +480,7 @@ namespace pcpp
424480 }
425481
426482 // / Clears all members of this instance, meaning setting raw data to nullptr, raw data length to 0, etc.
427- // / Frees the raw data if deleteRawDataAtDestructor was set to 'true'
483+ // / Frees the raw data if takeOwnership was set to 'true'
428484 // / @todo set timestamp to a default value as well
429485 virtual void clear ();
430486
@@ -461,6 +517,11 @@ namespace pcpp
461517 // / allocate the memory
462518 // / @return True if data was reallocated successfully, false otherwise
463519 virtual bool reallocateData (size_t newBufferLength);
520+
521+ protected:
522+ // Updates the raw data buffer and related members. Used by setRawData() methods.
523+ virtual bool doSetRawData (const uint8_t * pRawData, int rawDataLen, bool takeOwnership, timespec timestamp,
524+ LinkLayerType layerType, int frameLength);
464525 };
465526
466527} // namespace pcpp
0 commit comments