Coverage Report

Created: 2026-02-14 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/header/Layer.h
Line
Count
Source
1
#pragma once
2
3
#include <stdint.h>
4
#include <stdio.h>
5
#include "ProtocolType.h"
6
#include <string>
7
#include <stdexcept>
8
#include <utility>
9
10
/// @file
11
12
/// @namespace pcpp
13
/// @brief The main namespace for the PcapPlusPlus lib
14
namespace pcpp
15
{
16
17
  /// @class IDataContainer
18
  /// An interface (virtual abstract class) that indicates an object that holds a pointer to a buffer data. The Layer
19
  /// class is an example of such object, hence it inherits this interface
20
  class IDataContainer
21
  {
22
  public:
23
    /// Get a pointer to the data
24
    /// @param[in] offset Get a pointer in a certain offset. Default is 0 - get a pointer to start of data
25
    /// @return A pointer to the data
26
    virtual uint8_t* getDataPtr(size_t offset = 0) const = 0;
27
28
    virtual ~IDataContainer() = default;
29
  };
30
31
  class Packet;
32
33
  namespace internal
34
  {
35
    /// @brief Holds information about a Layer's data and object ownership.
36
    struct LayerAllocationInfo
37
    {
38
      /// @brief Pointer to the Packet this layer is attached to (if any).
39
      ///
40
      /// If the layer is attached to a Packet, the layer's memory span (data) is considered managed by the
41
      /// Packet. The Packet is responsible for keeping the layer's memory span valid and updating it should it
42
      /// become necessary as long as the layer is attached to it.
43
      ///
44
      /// In an event the Packet is destroyed, all of its attached layers's memory views are considered invalid.
45
      /// Accessing layer data after the Packet is destroyed results in undefined behavior.
46
      ///
47
      /// If nullptr, the layer is not attached to any Packet and is considered unmanaged.
48
      /// It also means the layer's memory span is considered owned by the layer itself and will be freed when
49
      /// the layer is destroyed.
50
      Packet* attachedPacket = nullptr;
51
52
      /// @brief Controls if the layer object is considered owned by the attached Packet
53
      ///
54
      /// If 'true', the Layer object is considered owned by the attached Packet and will be freed by it on Packet
55
      /// destruction.
56
      ///
57
      /// If 'false', the Layer object is considered unmanaged and the user is responsible for freeing it.
58
      /// This is commonly the case for layers created on the stack and attached to a Packet.
59
      bool ownedByPacket = false;
60
61
      /// @brief Sets the state of attachment to a specified Packet
62
      /// @param packet Pointer to the Packet this layer is attached to (or nullptr if not attached to any Packet)
63
      /// @param managed True if the layer object's lifetime is to be managed by the Packet, false otherwise
64
      /// @param force If true, bypasses the check for existing attachment. Default is false.
65
      /// @throws std::runtime_error if the layer is already attached to a Packet and 'force' is false
66
      void attachPacket(Packet* packet, bool managed, bool force = false)
67
0
      {
68
0
        if (!force && attachedPacket != nullptr)
69
0
        {
70
0
          throw std::runtime_error("Layer is already attached to a Packet");
71
0
        }
72
0
73
0
        attachedPacket = packet;
74
0
        ownedByPacket = managed;
75
0
      }
76
77
      /// @brief Clears the attachment to any Packet, resetting to unmanaged state.
78
      void detach()
79
0
      {
80
0
        attachedPacket = nullptr;
81
0
        ownedByPacket = false;
82
0
      }
83
    };
84
  }  // namespace internal
85
86
  /// @class Layer
87
  /// Layer is the base class for all protocol layers. Each protocol supported in PcapPlusPlus has a class that
88
  /// inherits Layer.
89
  /// The protocol layer class expose all properties and methods relevant for viewing and editing protocol fields.
90
  /// For example: a pointer to a structured header (e.g tcphdr, iphdr, etc.), protocol header size, payload size,
91
  /// compute fields that can be automatically computed, print protocol data to string, etc.
92
  /// Each protocol instance is obviously part of a protocol stack (which construct a packet). This protocol stack is
93
  /// represented in PcapPlusPlus in a linked list, and each layer is an element in this list. That's why each layer
94
  /// has properties to the next and previous layer in the protocol stack. The Layer class, as a base class, is
95
  /// abstract and the user can't create an instance of it (it has a private constructor). Each layer holds a pointer
96
  /// to the relevant place in the packet. The layer sees all the data from this pointer forward until the end of the
97
  /// packet. Here is an example packet showing this concept:
98
  ///
99
  /// @code{.unparsed}
100
  /// ====================================================
101
  /// |Eth       |IPv4       |TCP       |Packet          |
102
  /// |Header    |Header     |Header    |Payload         |
103
  /// ====================================================
104
  ///
105
  /// |--------------------------------------------------|
106
  /// EthLayer data
107
  ///            |---------------------------------------|
108
  ///            IPv4Layer data
109
  ///                        |---------------------------|
110
  ///                        TcpLayer data
111
  ///                                   |----------------|
112
  ///                                   PayloadLayer data
113
  /// @endcode
114
  class Layer : public IDataContainer
115
  {
116
    friend class Packet;
117
118
  public:
119
    /// A destructor for this class. Frees the data if it was allocated by the layer constructor (see
120
    /// isAllocatedToPacket() for more info)
121
    ~Layer() override;
122
123
    /// @return A pointer to the next layer in the protocol stack or nullptr if the layer is the last one
124
    Layer* getNextLayer() const
125
0
    {
126
0
      return m_NextLayer;
127
0
    }
128
129
    /// @return A pointer to the previous layer in the protocol stack or nullptr if the layer is the first one
130
    Layer* getPrevLayer() const
131
0
    {
132
0
      return m_PrevLayer;
133
0
    }
134
135
    /// @return The protocol enum
136
    ProtocolType getProtocol() const
137
0
    {
138
0
      return m_Protocol;
139
0
    }
140
141
    /// Check if the layer's protocol matches a protocol family
142
    /// @param protocolTypeFamily The protocol family to check
143
    /// @return True if the layer's protocol matches the protocol family, false otherwise
144
    bool isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const;
145
146
    /// @return A pointer to the layer raw data. In most cases it'll be a pointer to the first byte of the header
147
    uint8_t* getData() const
148
0
    {
149
0
      return m_Data;
150
0
    }
151
152
    /// @return The length in bytes of the data from the first byte of the header until the end of the packet
153
    size_t getDataLen() const
154
0
    {
155
0
      return m_DataLen;
156
0
    }
157
158
    /// @return A pointer for the layer payload, meaning the first byte after the header
159
    uint8_t* getLayerPayload() const
160
0
    {
161
0
      return m_Data + getHeaderLen();
162
0
    }
163
164
    /// @return The size in bytes of the payload
165
    size_t getLayerPayloadSize() const
166
0
    {
167
0
      return m_DataLen - getHeaderLen();
168
0
    }
169
170
    /// Raw data in layers can come from one of sources:
171
    /// 1. from an existing packet - this is the case when parsing packets received from files or the network. In
172
    /// this case the data was already allocated by someone else, and layer only holds the pointer to the relevant
173
    /// place inside this data
174
    /// 2. when creating packets, data is allocated when layer is created. In this case the layer is responsible for
175
    /// freeing it as well
176
    ///
177
    /// @return Returns true if the data was allocated by an external source (a packet) or false if it was allocated
178
    /// by the layer itself
179
    bool isAllocatedToPacket() const
180
0
    {
181
0
      return m_AllocationInfo.attachedPacket != nullptr;
182
0
    }
183
184
    /// Copy the raw data of this layer to another array
185
    /// @param[out] toArr The destination byte array
186
    void copyData(uint8_t* toArr) const;
187
188
    // implement abstract methods
189
190
    uint8_t* getDataPtr(size_t offset = 0) const override
191
0
    {
192
0
      return static_cast<uint8_t*>(m_Data + offset);
193
0
    }
194
195
    // abstract methods
196
197
    /// Each layer is responsible for parsing the next layer
198
    virtual void parseNextLayer() = 0;
199
200
    /// @return The header length in bytes
201
    virtual size_t getHeaderLen() const = 0;
202
203
    /// Each layer can compute field values automatically using this method. This is an abstract method
204
    virtual void computeCalculateFields() = 0;
205
206
    /// @return A string representation of the layer most important data (should look like the layer description in
207
    /// Wireshark)
208
    virtual std::string toString() const = 0;
209
210
    /// @return The OSI Model layer this protocol belongs to
211
    virtual OsiModelLayer getOsiModelLayer() const = 0;
212
213
  protected:
214
    uint8_t* m_Data;
215
    size_t m_DataLen;
216
    ProtocolType m_Protocol;
217
    Layer* m_NextLayer;
218
    Layer* m_PrevLayer;
219
220
  private:
221
    internal::LayerAllocationInfo m_AllocationInfo;
222
223
  protected:
224
    Layer() : m_Data(nullptr), m_DataLen(0), m_Protocol(UnknownProtocol), m_NextLayer(nullptr), m_PrevLayer(nullptr)
225
0
    {}
226
227
    Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ProtocolType protocol = UnknownProtocol)
228
        : m_Data(data), m_DataLen(dataLen), m_Protocol(protocol), m_NextLayer(nullptr), m_PrevLayer(prevLayer),
229
          m_AllocationInfo{ packet, false }
230
0
    {}
231
232
    // Copy c'tor
233
    Layer(const Layer& other);
234
    Layer& operator=(const Layer& other);
235
236
    /// @brief Get a pointer to the Packet this layer is attached to (if any).
237
    /// @return A pointer to the Packet this layer is attached to, or nullptr if the layer is not attached.
238
    Packet* getAttachedPacket()
239
0
    {
240
0
      return m_AllocationInfo.attachedPacket;
241
0
    }
242
243
    /// @brief Get a pointer to the Packet this layer is attached to (if any).
244
    /// @return A const pointer to the Packet this layer is attached to, or nullptr if the layer is not attached.
245
    Packet const* getAttachedPacket() const
246
0
    {
247
0
      return m_AllocationInfo.attachedPacket;
248
0
    }
249
250
    void setNextLayer(Layer* nextLayer)
251
0
    {
252
0
      m_NextLayer = nextLayer;
253
0
    }
254
    void setPrevLayer(Layer* prevLayer)
255
0
    {
256
0
      m_PrevLayer = prevLayer;
257
0
    }
258
259
    virtual bool extendLayer(int offsetInLayer, size_t numOfBytesToExtend);
260
    virtual bool shortenLayer(int offsetInLayer, size_t numOfBytesToShorten);
261
262
    bool hasNextLayer() const
263
0
    {
264
0
      return m_NextLayer != nullptr;
265
0
    }
266
267
    /// @brief Construct the next layer in the protocol stack. No validation is performed on the data.
268
    ///
269
    /// This overload infers the Packet from the current layer.
270
    ///
271
    /// @tparam T The type of the layer to construct
272
    /// @tparam Args The types of the arguments to pass to the layer constructor
273
    /// @param data The data to construct the layer from
274
    /// @param dataLen The length of the data
275
    /// @param extraArgs Extra arguments to be forwarded to the layer constructor
276
    /// @return The constructed layer
277
    template <typename T, typename... Args>
278
    Layer* constructNextLayer(uint8_t* data, size_t dataLen, Args&&... extraArgs)
279
    {
280
      return constructNextLayer<T>(data, dataLen, getAttachedPacket(), std::forward<Args>(extraArgs)...);
281
    }
282
283
    /// Construct the next layer in the protocol stack. No validation is performed on the data.
284
    /// @tparam T The type of the layer to construct
285
    /// @tparam Args The types of the arguments to pass to the layer constructor
286
    /// @param[in] data The data to construct the layer from
287
    /// @param[in] dataLen The length of the data
288
    /// @param[in] packet The packet the layer belongs to
289
    /// @param[in] extraArgs Extra arguments to be forwarded to the layer constructor
290
    /// @return The constructed layer
291
    template <typename T, typename... Args>
292
    Layer* constructNextLayer(uint8_t* data, size_t dataLen, Packet* packet, Args&&... extraArgs)
293
    {
294
      if (hasNextLayer())
295
      {
296
        throw std::runtime_error("Next layer already exists");
297
      }
298
299
      Layer* newLayer = new T(data, dataLen, this, packet, std::forward<Args>(extraArgs)...);
300
      setNextLayer(newLayer);
301
      return newLayer;
302
    }
303
304
    /// @brief Construct the next layer in the protocol stack using a factory functor.
305
    ///
306
    /// No validation is performed on the data, outside of what the factory functor may perform.
307
    /// If the factory returns a nullptr, no next layer is set.
308
    ///
309
    /// The factory functor is expected to have the following signature:
310
    /// Layer* factoryFn(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ...);
311
    ///
312
    /// This overload infers the Packet from the current layer.
313
    ///
314
    /// @tparam TFactory The factory functor type.
315
    /// @tparam ...Args Parameter pack for extra arguments to pass to the factory functor.
316
    /// @param[in] factoryFn The factory functor to create the layer.
317
    /// @param[in] data The data to construct the layer from
318
    /// @param[in] dataLen The length of the data
319
    /// @param[in] extraArgs Extra arguments to be forwarded to the factory.
320
    /// @return The return value of the factory functor.
321
    template <typename TFactory, typename... Args>
322
    Layer* constructNextLayerFromFactory(TFactory factoryFn, uint8_t* data, size_t dataLen, Args&&... extraArgs)
323
    {
324
      return constructNextLayerFromFactory<TFactory>(factoryFn, data, dataLen, getAttachedPacket(),
325
                                                     std::forward<Args>(extraArgs)...);
326
    }
327
328
    /// @brief Construct the next layer in the protocol stack using a factory functor.
329
    ///
330
    /// No validation is performed on the data, outside of what the factory functor may perform.
331
    /// If the factory returns a nullptr, no next layer is set.
332
    ///
333
    /// The factory functor is expected to have the following signature:
334
    /// Layer* factoryFn(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ...);
335
    ///
336
    /// @tparam TFactory The factory functor type.
337
    /// @tparam ...Args Parameter pack for extra arguments to pass to the factory functor.
338
    /// @param[in] factoryFn The factory functor to create the layer.
339
    /// @param[in] data The data to construct the layer from
340
    /// @param[in] dataLen The length of the data
341
    /// @param[in] packet The packet the layer belongs to
342
    /// @param[in] extraArgs Extra arguments to be forwarded to the factory.
343
    /// @return The return value of the factory functor.
344
    template <typename TFactory, typename... Args>
345
    Layer* constructNextLayerFromFactory(TFactory factoryFn, uint8_t* data, size_t dataLen, Packet* packet,
346
                                         Args&&... extraArgs)
347
    {
348
      if (hasNextLayer())
349
      {
350
        throw std::runtime_error("Next layer already exists");
351
      }
352
353
      // cppcheck-suppress redundantInitialization
354
      Layer* newLayer = factoryFn(data, dataLen, this, packet, std::forward<Args>(extraArgs)...);
355
      setNextLayer(newLayer);
356
      return newLayer;
357
    }
358
359
    /// Try to construct the next layer in the protocol stack.
360
    ///
361
    /// This overload infers the Packet from the current layer.
362
    ///
363
    /// The method checks if the data is valid for the layer type T before constructing it by calling
364
    /// T::isDataValid(data, dataLen). If the data is invalid, no layer is constructed and a nullptr is returned.
365
    ///
366
    /// @tparam T The type of the layer to construct
367
    /// @tparam Args The types of the extra arguments to pass to the layer constructor
368
    /// @param[in] data The data to construct the layer from
369
    /// @param[in] dataLen The length of the data
370
    /// @param[in] extraArgs Extra arguments to be forwarded to the layer constructor
371
    /// @return The constructed layer or nullptr if the data is invalid
372
    template <typename T, typename... Args>
373
    Layer* tryConstructNextLayer(uint8_t* data, size_t dataLen, Args&&... extraArgs)
374
    {
375
      return tryConstructNextLayer<T>(data, dataLen, getAttachedPacket(), std::forward<Args>(extraArgs)...);
376
    }
377
378
    /// Try to construct the next layer in the protocol stack.
379
    ///
380
    /// The method checks if the data is valid for the layer type T before constructing it by calling
381
    /// T::isDataValid(data, dataLen). If the data is invalid, no layer is constructed and a nullptr is returned.
382
    ///
383
    /// @tparam T The type of the layer to construct
384
    /// @tparam Args The types of the extra arguments to pass to the layer constructor
385
    /// @param[in] data The data to construct the layer from
386
    /// @param[in] dataLen The length of the data
387
    /// @param[in] packet The packet the layer belongs to
388
    /// @param[in] extraArgs Extra arguments to be forwarded to the layer constructor
389
    /// @return The constructed layer or nullptr if the data is invalid
390
    template <typename T, typename... Args>
391
    Layer* tryConstructNextLayer(uint8_t* data, size_t dataLen, Packet* packet, Args&&... extraArgs)
392
    {
393
      if (T::isDataValid(data, dataLen))
394
      {
395
        return constructNextLayer<T>(data, dataLen, packet, std::forward<Args>(extraArgs)...);
396
      }
397
      return nullptr;
398
    }
399
400
    /// @brief Try to construct the next layer in the protocol stack with a fallback option.
401
    ///
402
    /// This overload infers the Packet from the current layer.
403
    ///
404
    /// The method checks if the data is valid for the layer type T before constructing it by calling
405
    /// T::isDataValid(data, dataLen). If the data is invalid, it constructs the layer of type TFallback.
406
    ///
407
    /// @tparam T The type of the layer to construct
408
    /// @tparam TFallback The fallback layer type to construct if T fails
409
    /// @tparam Args The types of the extra arguments to pass to the layer constructor of T
410
    /// @param[in] data The data to construct the layer from
411
    /// @param[in] dataLen The length of the data
412
    /// @param[in] extraArgs Extra arguments to be forwarded to the layer constructor of T
413
    /// @return The constructed layer of type T or TFallback
414
    /// @remarks The parameters extraArgs are forwarded to the factory function, but not to the TFallback
415
    /// constructor.
416
    template <typename T, typename TFallback, typename... Args>
417
    Layer* tryConstructNextLayerWithFallback(uint8_t* data, size_t dataLen, Args&&... extraArgs)
418
    {
419
      return tryConstructNextLayerWithFallback<T, TFallback>(data, dataLen, getAttachedPacket(),
420
                                                             std::forward<Args>(extraArgs)...);
421
    }
422
423
    /// Try to construct the next layer in the protocol stack with a fallback option.
424
    ///
425
    /// The method checks if the data is valid for the layer type T before constructing it by calling
426
    /// T::isDataValid(data, dataLen). If the data is invalid, it constructs the layer of type TFallback.
427
    ///
428
    /// @tparam T The type of the layer to construct
429
    /// @tparam TFallback The fallback layer type to construct if T fails
430
    /// @tparam Args The types of the extra arguments to pass to the layer constructor of T
431
    /// @param[in] data The data to construct the layer from
432
    /// @param[in] dataLen The length of the data
433
    /// @param[in] packet The packet the layer belongs to
434
    /// @param[in] extraArgs Extra arguments to be forwarded to the layer constructor of T
435
    /// @return The constructed layer of type T or TFallback
436
    /// @remarks The parameters extraArgs are forwarded to the factory function, but not to the TFallback
437
    /// constructor.
438
    template <typename T, typename TFallback, typename... Args>
439
    Layer* tryConstructNextLayerWithFallback(uint8_t* data, size_t dataLen, Packet* packet, Args&&... extraArgs)
440
    {
441
      if (tryConstructNextLayer<T>(data, dataLen, packet, std::forward<Args>(extraArgs)...))
442
      {
443
        return m_NextLayer;
444
      }
445
446
      return constructNextLayer<TFallback>(data, dataLen, packet);
447
    }
448
449
    /// @brief Try to construct the next layer in the protocol stack using a factory functor with a fallback option.
450
    ///
451
    /// The method will attempt to construct the next layer using the provided factory function.
452
    /// If the factory function returns nullptr, indicating failure to create the layer, the method will then
453
    /// construct a layer of type TFallback.
454
    ///
455
    /// The factory functor is expected to have the following signature:
456
    /// Layer* factoryFn(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ...);
457
    ///
458
    /// This overload infers the Packet from the current layer.
459
    ///
460
    /// @tparam TFallback The fallback layer type to construct if the factory fails.
461
    /// @tparam TFactory The factory functor type.
462
    /// @tparam ...Args Parameter pack for extra arguments to pass to the factory functor.
463
    /// @param[in] factoryFn The factory functor to create the layer.
464
    /// @param[in] data The data to construct the layer from
465
    /// @param[in] dataLen The length of the data
466
    /// @param[in] extraArgs Extra arguments to be forwarded to the factory.
467
    /// @return The return value of the factory functor.
468
    /// @remarks The parameters extraArgs are forwarded to the factory function, but not to the TFallback
469
    /// constructor.
470
    template <typename TFallback, typename TFactory, typename... Args>
471
    Layer* tryConstructNextLayerFromFactoryWithFallback(TFactory factoryFn, uint8_t* data, size_t dataLen,
472
                                                        Args&&... extraArgs)
473
    {
474
      // Note that the fallback is first to allow template argument deduction of the factory type.
475
      return tryConstructNextLayerFromFactoryWithFallback<TFallback, TFactory>(
476
          factoryFn, data, dataLen, getAttachedPacket(), std::forward<Args>(extraArgs)...);
477
    }
478
479
    /// @brief Try to construct the next layer in the protocol stack using a factory functor with a fallback option.
480
    ///
481
    /// The method will attempt to construct the next layer using the provided factory function.
482
    /// If the factory function returns nullptr, indicating failure to create the layer, the method will then
483
    /// construct a layer of type TFallback.
484
    ///
485
    /// The factory functor is expected to have the following signature:
486
    /// Layer* factoryFn(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet, ...);
487
    ///
488
    /// @tparam TFallback The fallback layer type to construct if the factory fails.
489
    /// @tparam TFactory The factory functor type.
490
    /// @tparam ...Args Parameter pack for extra arguments to pass to the factory functor.
491
    /// @param[in] factoryFn The factory functor to create the layer.
492
    /// @param[in] data The data to construct the layer from
493
    /// @param[in] dataLen The length of the data
494
    /// @param[in] packet The packet the layer belongs to
495
    /// @param[in] extraArgs Extra arguments to be forwarded to the factory.
496
    /// @return The return value of the factory functor.
497
    /// @remarks The parameters extraArgs are forwarded to the factory function, but not to the TFallback
498
    /// constructor.
499
    template <typename TFallback, typename TFactory, typename... Args>
500
    Layer* tryConstructNextLayerFromFactoryWithFallback(TFactory factoryFn, uint8_t* data, size_t dataLen,
501
                                                        Packet* packet, Args&&... extraArgs)
502
    {
503
      auto nextLayer = constructNextLayerFromFactory<TFactory>(factoryFn, data, dataLen, packet,
504
                                                               std::forward<Args>(extraArgs)...);
505
      if (nextLayer != nullptr)
506
      {
507
        return nextLayer;
508
      }
509
510
      // factory failed, construct fallback layer
511
      return constructNextLayer<TFallback>(data, dataLen, packet);
512
    }
513
514
    /// @brief Check if the data is large enough to reinterpret as a type
515
    ///
516
    /// The data must be non-null and at least as large as the type
517
    ///
518
    /// @tparam T The type to reinterpret as
519
    /// @param data The data to check
520
    /// @param dataLen The length of the data
521
    /// @return True if the data is large enough to reinterpret as T, false otherwise
522
    template <typename T> static bool canReinterpretAs(const uint8_t* data, size_t dataLen)
523
0
    {
524
0
      return data != nullptr && dataLen >= sizeof(T);
525
0
    }
Unexecuted instantiation: bool pcpp::Layer::canReinterpretAs<pcpp::arphdr>(unsigned char const*, unsigned long)
Unexecuted instantiation: bool pcpp::Layer::canReinterpretAs<pcpp::iphdr>(unsigned char const*, unsigned long)
526
  };
527
528
  inline std::ostream& operator<<(std::ostream& os, const pcpp::Layer& layer)
529
0
  {
530
0
    os << layer.toString();
531
0
    return os;
532
0
  }
533
}  // namespace pcpp