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