Coverage Report

Created: 2024-02-25 06:29

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