Coverage Report

Created: 2023-01-17 06:15

/src/PcapPlusPlus/Packet++/header/MplsLayer.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef PACKETPP_MPLS_LAYER
2
#define PACKETPP_MPLS_LAYER
3
4
#include "Layer.h"
5
6
/// @file
7
8
/**
9
 * \namespace pcpp
10
 * \brief The main namespace for the PcapPlusPlus lib
11
 */
12
namespace pcpp
13
{
14
15
  /**
16
   * @class MplsLayer
17
   * Represents a MPLS (Multi-Protocol Label Switching) layer
18
   */
19
  class MplsLayer : public Layer
20
  {
21
  private:
22
23
    #pragma pack(push, 1)
24
    struct mpls_header
25
    {
26
      uint16_t hiLabel;
27
      uint8_t  misc;
28
      uint8_t  ttl;
29
    };
30
    #pragma pack(pop)
31
32
5.42k
    mpls_header* getMplsHeader() const { return (mpls_header*)m_Data; }
33
34
  public:
35
     /** A constructor that creates the layer from an existing packet raw data
36
     * @param[in] data A pointer to the raw data
37
     * @param[in] dataLen Size of the data in bytes
38
     * @param[in] prevLayer A pointer to the previous layer
39
     * @param[in] packet A pointer to the Packet instance where layer will be stored in
40
     */
41
5.57k
    MplsLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = MPLS; }
42
43
    /**
44
     * A constructor that allocates a new MPLS header
45
     * @param[in] mplsLabel MPLS label
46
     * @param[in] ttl Time-to-leave value
47
     * @param[in] experimentalUseValue Experimental use value
48
     * @param[in] bottomOfStack Bottom-of-stack value which indicate whether the next layer will also be a MPLS label or not
49
     */
50
    MplsLayer(uint32_t mplsLabel, uint8_t ttl, uint8_t experimentalUseValue, bool bottomOfStack);
51
52
0
    virtual ~MplsLayer() {}
53
54
    /**
55
     * @return TTL value of the MPLS header
56
     */
57
0
    uint8_t getTTL() const { return getMplsHeader()->ttl; }
58
59
    /**
60
     * Set the TTL value
61
     * @param[in] ttl The TTL value to set
62
     */
63
0
    void setTTL(uint8_t ttl) { getMplsHeader()->ttl = ttl; }
64
65
    /**
66
     * Get an indication whether the next layer is also be a MPLS label or not
67
     * @return True if it's the last MPLS layer, false otherwise
68
     */
69
    bool isBottomOfStack() const;
70
71
    /**
72
     * Set the bottom-of-stack bit in the MPLS label
73
     * @param[in] val Set or unset the bit
74
     */
75
    void setBottomOfStack(bool val);
76
77
    /**
78
     * @return The exp value (3 bits) of the MPLS label
79
     */
80
    uint8_t getExperimentalUseValue() const;
81
82
    /**
83
     * Set the exp value (3 bits) of the MPLS label
84
     * @param[in] val The exp value to set. val must be a valid number meaning between 0 and 7 (inclusive)
85
     * @return True if exp value was set successfully or false if val has invalid value
86
     */
87
    bool setExperimentalUseValue(uint8_t val);
88
89
    /**
90
     * @return The MPLS label value (20 bits)
91
     */
92
    uint32_t getMplsLabel() const;
93
94
    /**
95
     * Set the MPLS label (20 bits)
96
     * @param[in] label The label to set. label must be a valid number meaning between 0 and 0xFFFFF (inclusive)
97
     * @return True if label was set successfully or false if label has invalid value
98
     */
99
    bool setMplsLabel(uint32_t label);
100
101
    // implement abstract methods
102
103
    /**
104
     * Currently identifies the following next layers: IPv4Layer, IPv6Layer, MplsLayer. Otherwise sets PayloadLayer
105
     */
106
    void parseNextLayer();
107
108
    /**
109
     * @return Size of MPLS header (4 bytes)
110
     */
111
5.57k
    size_t getHeaderLen() const { return sizeof(mpls_header); }
112
113
    /**
114
     * Set/unset the bottom-of-stack bit according to next layer: if it's a MPLS layer then bottom-of-stack will be unset. If it's not a
115
     * MPLS layer this bit will be set
116
     */
117
    void computeCalculateFields();
118
119
    std::string toString() const;
120
121
5.57k
    OsiModelLayer getOsiModelLayer() const { return OsiModelNetworkLayer; }
122
  };
123
124
} // namespace pcpp
125
126
#endif /* PACKETPP_MPLS_LAYER */