Coverage Report

Created: 2025-07-11 07:47

/src/PcapPlusPlus/Packet++/header/PacketTrailerLayer.h
Line
Count
Source (jump to first uncovered line)
1
#pragma once
2
3
#include "Layer.h"
4
5
/// @file
6
7
/// @namespace pcpp
8
/// @brief The main namespace for the PcapPlusPlus lib
9
namespace pcpp
10
{
11
  /// @class PacketTrailerLayer
12
  /// A class for representing packet trailer (a.k.a footer or padding) which refers to supplemental data placed at
13
  /// the end of a block of data being stored or transmitted, which may contain information for the handling of the
14
  /// data block, or just mark its end (taken from Wikipedia: https://en.wikipedia.org/wiki/Trailer_(computing) )
15
  ///
16
  /// There are various reasons for adding a packet trailer, one of the most famous is FCS (Frame check sequence)
17
  /// which refers to the extra error-detecting code added to a frame. Another usage is padding which means adding
18
  /// data to reach a minimum required packet length.
19
  ///
20
  /// Although this layer inherits from the Layer class, it is not a standard layer in the sense that it can't be
21
  /// constructed by the user. This layer may be only be constructed in the Packet class, in the process of parsing
22
  /// the packet and creating the layers; if at the end of the parsing process there is data left that is not
23
  /// allocated to any layer, it's assumed to be the packet trailer and an instance of this class is created. This
24
  /// means this layer can only exist as the last layer in a packet, if a packet trailer indeed exists.
25
  ///
26
  /// No layer can be added by the user after this layer (trying to do that will result with an error).
27
  ///
28
  /// This layer can be removed by the user or extended/shortened, as any layer.
29
  ///
30
  /// It also contains method to extract the trailer data
31
  class PacketTrailerLayer : public Layer
32
  {
33
  public:
34
    /// A constructor that creates the layer from an existing packet raw data
35
    /// @param[in] data A pointer to the raw data
36
    /// @param[in] dataLen Size of the data in bytes
37
    /// @param[in] prevLayer A pointer to the previous layer
38
    /// @param[in] packet A pointer to the Packet instance where layer will be stored in
39
    PacketTrailerLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
40
12.5k
        : Layer(data, dataLen, prevLayer, packet, PacketTrailer)
41
12.5k
    {}
42
43
    ~PacketTrailerLayer() override = default;
44
45
    /// Get a pointer to the trailer data
46
    /// @return A pointer to the trailer data
47
    uint8_t* getTrailerData() const
48
0
    {
49
0
      return m_Data;
50
0
    }
51
52
    /// @return Trailer data as hex string
53
    std::string getTrailerDataAsHexString() const;
54
55
    /// Get the trailer data length
56
    /// @return The trailer data length in bytes
57
    size_t getTrailerLen() const
58
0
    {
59
0
      return m_DataLen;
60
0
    }
61
62
    // implement abstract methods
63
64
    /// Does nothing for this layer (PacketTrailerLayer is always last)
65
    void parseNextLayer() override
66
0
    {}
67
68
    /// @return trailer data length in bytes
69
    size_t getHeaderLen() const override
70
36.4k
    {
71
36.4k
      return m_DataLen;
72
36.4k
    }
73
74
    /// Does nothing for this layer
75
    void computeCalculateFields() override
76
12.5k
    {}
77
78
    std::string toString() const override;
79
80
    OsiModelLayer getOsiModelLayer() const override
81
0
    {
82
0
      return OsiModelDataLinkLayer;
83
0
    }
84
  };
85
86
}  // namespace pcpp