Coverage Report

Created: 2023-01-17 06:15

/src/PcapPlusPlus/Packet++/header/VlanLayer.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef PACKETPP_VLAN_LAYER
2
#define PACKETPP_VLAN_LAYER
3
4
#include "Layer.h"
5
#include "EthLayer.h"
6
7
/// @file
8
9
/**
10
 * \namespace pcpp
11
 * \brief The main namespace for the PcapPlusPlus lib
12
 */
13
namespace pcpp
14
{
15
16
  /**
17
   * @struct vlan_header
18
   * Represents a VLAN header
19
   */
20
#pragma pack(push, 1)
21
  struct vlan_header
22
  {
23
    /**
24
       @verbatim
25
       0                 1
26
       0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
27
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28
       |Prio |C|         VLAN ID     |
29
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30
       @endverbatim
31
     */
32
    uint16_t vlan;
33
    /** Ethernet type for next layer */
34
    uint16_t etherType;
35
  };
36
#pragma pack(pop)
37
38
39
  /**
40
   * @class VlanLayer
41
   * Represents a VLAN tunnel layer
42
   */
43
  class VlanLayer : public Layer
44
  {
45
  public:
46
     /** A constructor that creates the layer from an existing packet raw data
47
     * @param[in] data A pointer to the raw data
48
     * @param[in] dataLen Size of the data in bytes
49
     * @param[in] prevLayer A pointer to the previous layer
50
     * @param[in] packet A pointer to the Packet instance where layer will be stored in
51
     */
52
8.12k
    VlanLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = VLAN; }
53
54
    /**
55
     * A constructor that allocates a new VLAN header
56
     * @param[in] vlanID VLAN ID
57
     * @param[in] cfi CFI value
58
     * @param[in] priority Priority value
59
     * @param[in] etherType Protocol EtherType of the next layer. It's an optional parameter, a value of 0 will be set if not provided
60
     */
61
    VlanLayer(const uint16_t vlanID, bool cfi, uint8_t priority, uint16_t etherType = 0);
62
63
0
    ~VlanLayer() {}
64
65
    /**
66
     * Get a pointer to the VLAN header. Notice this points directly to the data, so every change will change the actual packet data
67
     * @return A pointer to the vlan_header
68
     */
69
7.76k
    vlan_header* getVlanHeader() const { return (vlan_header*)m_Data; }
70
71
    /**
72
     * Get the VLAN ID value. This method differs from vlan_header#vlanID because vlan_header#vlanID is 12 bits long in a 16 bit field.
73
     * This methods extracts only the 12 bit relevant for the VLAN ID
74
     * @return VLAN ID value
75
     * @todo Verify it works in big endian machines as well
76
     */
77
    uint16_t getVlanID() const;
78
79
    /**
80
     * @return The CFI bit value
81
     * @todo Verify it works in big endian machines as well
82
     */
83
    uint8_t getCFI() const;
84
85
    /**
86
     * @return The priority value
87
     * @todo Verify it works in big endian machines as well
88
     */
89
    uint8_t getPriority() const;
90
91
    /**
92
     * Set VLAN ID. This method differs from setting vlan_header#vlanID because vlan_header#vlanID is 12 bits long in a 16 bit field.
93
     * This methods sets only the 12 bit relevant for the VLAN ID
94
     * @param[in] id The VLAN ID to set
95
     * @todo Verify it works in big endian machines as well
96
     */
97
    void setVlanID(uint16_t id);
98
99
    /**
100
     * Set CFI bit
101
     * @param[in] cfi The CFI bit to set
102
     * @todo Verify it works in big endian machines as well
103
     */
104
    void setCFI(bool cfi);
105
106
    /**
107
     * Set priority value
108
     * @param[in] priority The priority value to set
109
     * @todo Verify it works in big endian machines as well
110
     */
111
    void setPriority(uint8_t priority);
112
113
    // implement abstract methods
114
115
    /**
116
     * Currently identifies the following next layers: IPv4Layer, IPv6Layer, ArpLayer, VlanLayer, MplsLayer. Otherwise sets PayloadLayer
117
     */
118
    void parseNextLayer();
119
120
    /**
121
     * @return Size of vlan_header
122
     */
123
0
    size_t getHeaderLen() const { return sizeof(vlan_header); }
124
125
    /**
126
     * Calculate the EtherType for known protocols: IPv4, IPv6, ARP, VLAN
127
     */
128
    void computeCalculateFields();
129
130
    std::string toString() const;
131
132
8.12k
    OsiModelLayer getOsiModelLayer() const { return OsiModelDataLinkLayer; }
133
  };
134
135
} // namespace pcpp
136
137
#endif /* PACKETPP_VLAN_LAYER */