/src/PcapPlusPlus/Packet++/header/EthDot3Layer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include "Layer.h" |
4 | | #include "MacAddress.h" |
5 | | |
6 | | /// @file |
7 | | |
8 | | /// @namespace pcpp |
9 | | /// @brief The main namespace for the PcapPlusPlus lib |
10 | | namespace pcpp |
11 | | { |
12 | | |
13 | | /// @struct ether_dot3_header |
14 | | /// Represents an IEEE 802.3 Ethernet header |
15 | | #pragma pack(push, 1) |
16 | | struct ether_dot3_header |
17 | | { |
18 | | /// Destination MAC |
19 | | uint8_t dstMac[6]; |
20 | | /// Source MAC |
21 | | uint8_t srcMac[6]; |
22 | | /// EtherType |
23 | | uint16_t length; |
24 | | }; |
25 | | #pragma pack(pop) |
26 | | static_assert(sizeof(ether_dot3_header) == 14, "ether_dot3_header size is not 14 bytes"); |
27 | | |
28 | | /// @class EthDot3Layer |
29 | | /// Represents an IEEE 802.3 Ethernet protocol layer |
30 | | class EthDot3Layer : public Layer |
31 | | { |
32 | | public: |
33 | | /// A constructor that creates the layer from an existing packet raw data |
34 | | /// @param[in] data A pointer to the raw data (will be casted to ether_dot3_header) |
35 | | /// @param[in] dataLen Size of the data in bytes |
36 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
37 | | EthDot3Layer(uint8_t* data, size_t dataLen, Packet* packet) |
38 | 445 | : Layer(data, dataLen, nullptr, packet, EthernetDot3) |
39 | 445 | {} |
40 | | |
41 | | /// A constructor that creates the layer from an existing packet raw data |
42 | | /// @param[in] data A pointer to the raw data (will be casted to ether_header) |
43 | | /// @param[in] dataLen Size of the data in bytes |
44 | | /// @param[in] prevLayer A pointer to the previous layer |
45 | | /// @param[in] packet A pointer to the Packet instance where layer will be stored in |
46 | | EthDot3Layer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) |
47 | 0 | : Layer(data, dataLen, prevLayer, packet, EthernetDot3) |
48 | 0 | {} |
49 | | |
50 | | /// A constructor that creates a new IEEE 802.3 Ethernet header and allocates the data |
51 | | /// @param[in] sourceMac The source MAC address |
52 | | /// @param[in] destMac The destination MAC address |
53 | | /// @param[in] length The frame length |
54 | | EthDot3Layer(const MacAddress& sourceMac, const MacAddress& destMac, uint16_t length); |
55 | | |
56 | | ~EthDot3Layer() override = default; |
57 | | |
58 | | /// Get a pointer to the Ethernet header. Notice this points directly to the data, so every change will change |
59 | | /// the actual packet data |
60 | | /// @return A pointer to the ether_header |
61 | | ether_dot3_header* getEthHeader() const |
62 | 488 | { |
63 | 488 | return reinterpret_cast<ether_dot3_header*>(m_Data); |
64 | 488 | } |
65 | | |
66 | | /// Get the source MAC address |
67 | | /// @return The source MAC address |
68 | | MacAddress getSourceMac() const |
69 | 244 | { |
70 | 244 | return MacAddress(getEthHeader()->srcMac); |
71 | 244 | } |
72 | | |
73 | | /// Set source MAC address |
74 | | /// @param sourceMac Source MAC to set |
75 | | void setSourceMac(const MacAddress& sourceMac) |
76 | 0 | { |
77 | 0 | sourceMac.copyTo(getEthHeader()->srcMac); |
78 | 0 | } |
79 | | |
80 | | /// Get the destination MAC address |
81 | | /// @return The destination MAC address |
82 | | MacAddress getDestMac() const |
83 | 244 | { |
84 | 244 | return MacAddress(getEthHeader()->dstMac); |
85 | 244 | } |
86 | | |
87 | | /// Set destination MAC address |
88 | | /// @param destMac Destination MAC to set |
89 | | void setDestMac(const MacAddress& destMac) |
90 | 0 | { |
91 | 0 | destMac.copyTo(getEthHeader()->dstMac); |
92 | 0 | } |
93 | | |
94 | | // implement abstract methods |
95 | | |
96 | | /// Parses next layer |
97 | | void parseNextLayer() override; |
98 | | |
99 | | /// @return Size of ether_dot3_header |
100 | | size_t getHeaderLen() const override |
101 | 122 | { |
102 | 122 | return sizeof(ether_dot3_header); |
103 | 122 | } |
104 | | |
105 | | /// Does nothing for this layer |
106 | | void computeCalculateFields() override |
107 | 122 | {} |
108 | | |
109 | | std::string toString() const override; |
110 | | |
111 | | OsiModelLayer getOsiModelLayer() const override |
112 | 122 | { |
113 | 122 | return OsiModelDataLinkLayer; |
114 | 122 | } |
115 | | |
116 | | /// A static method that validates the input data |
117 | | /// @param[in] data The pointer to the beginning of a byte stream of an IEEE 802.3 Eth packet |
118 | | /// @param[in] dataLen The length of the byte stream |
119 | | /// @return True if the data is valid and can represent an IEEE 802.3 Eth packet |
120 | | static bool isDataValid(const uint8_t* data, size_t dataLen); |
121 | | }; |
122 | | |
123 | | } // namespace pcpp |