/src/PcapPlusPlus/Packet++/header/EthLayer.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 | | /** |
9 | | * \namespace pcpp |
10 | | * \brief The main namespace for the PcapPlusPlus lib |
11 | | */ |
12 | | namespace pcpp |
13 | | { |
14 | | |
15 | | /** |
16 | | * @struct ether_header |
17 | | * Represents an Ethernet II header |
18 | | */ |
19 | | #pragma pack(push, 1) |
20 | | struct ether_header |
21 | | { |
22 | | /** Destination MAC */ |
23 | | uint8_t dstMac[6]; |
24 | | /** Source MAC */ |
25 | | uint8_t srcMac[6]; |
26 | | /** EtherType */ |
27 | | uint16_t etherType; |
28 | | }; |
29 | | #pragma pack(pop) |
30 | | |
31 | | /* Ethernet protocol ID's */ |
32 | | |
33 | | /** IP */ |
34 | 542k | #define PCPP_ETHERTYPE_IP 0x0800 |
35 | | /** Address resolution */ |
36 | 24.5k | #define PCPP_ETHERTYPE_ARP 0x0806 |
37 | | /** Transparent Ethernet Bridging */ |
38 | 4.60k | #define PCPP_ETHERTYPE_ETHBRIDGE 0x6558 |
39 | | /** Reverse ARP */ |
40 | | #define PCPP_ETHERTYPE_REVARP 0x8035 |
41 | | /** AppleTalk protocol */ |
42 | | #define PCPP_ETHERTYPE_AT 0x809B |
43 | | /** AppleTalk ARP */ |
44 | | #define PCPP_ETHERTYPE_AARP 0x80F3 |
45 | | /** IEEE 802.1Q VLAN tagging */ |
46 | 67.3k | #define PCPP_ETHERTYPE_VLAN 0x8100 |
47 | | /** IPX */ |
48 | | #define PCPP_ETHERTYPE_IPX 0x8137 |
49 | | /** IP protocol version 6 */ |
50 | 201k | #define PCPP_ETHERTYPE_IPV6 0x86dd |
51 | | /** used to test interfaces */ |
52 | | #define PCPP_ETHERTYPE_LOOPBACK 0x9000 |
53 | | /** PPPoE discovery */ |
54 | 6.01k | #define PCPP_ETHERTYPE_PPPOED 0x8863 |
55 | | /** PPPoE session */ |
56 | 5.29k | #define PCPP_ETHERTYPE_PPPOES 0x8864 |
57 | | /** MPLS */ |
58 | 17.6k | #define PCPP_ETHERTYPE_MPLS 0x8847 |
59 | | /** Point-to-point protocol (PPP) */ |
60 | 2.05k | #define PCPP_ETHERTYPE_PPP 0x880B |
61 | | /** RDMA over Converged Ethernet (RoCEv1) */ |
62 | | #define PCPP_ETHERTYPE_ROCEV1 0x8915 |
63 | | /** IEEE 802.1ad Provider Bridge, Q-in-Q */ |
64 | 70.1k | #define PCPP_ETHERTYPE_IEEE_802_1AD 0x88A8 |
65 | | /** Wake on LAN */ |
66 | 213 | #define PCPP_ETHERTYPE_WAKE_ON_LAN 0x0842 |
67 | | |
68 | | |
69 | | /** |
70 | | * @class EthLayer |
71 | | * Represents an Ethernet II protocol layer |
72 | | */ |
73 | | class EthLayer : public Layer |
74 | | { |
75 | | public: |
76 | | /** |
77 | | * A constructor that creates the layer from an existing packet raw data |
78 | | * @param[in] data A pointer to the raw data (will be casted to ether_header) |
79 | | * @param[in] dataLen Size of the data in bytes |
80 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
81 | | */ |
82 | 734k | EthLayer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, NULL, packet) { m_Protocol = Ethernet; } |
83 | | |
84 | | /** |
85 | | * A constructor that creates the layer from an existing packet raw data |
86 | | * @param[in] data A pointer to the raw data (will be casted to ether_header) |
87 | | * @param[in] dataLen Size of the data in bytes |
88 | | * @param[in] prevLayer A pointer to the previous layer |
89 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
90 | | */ |
91 | 2.00k | EthLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = Ethernet; } |
92 | | |
93 | | /** |
94 | | * A constructor that creates a new Ethernet header and allocates the data |
95 | | * @param[in] sourceMac The source MAC address |
96 | | * @param[in] destMac The destination MAC address |
97 | | * @param[in] etherType The EtherType to be used. It's an optional parameter, a value of 0 will be set if not provided |
98 | | */ |
99 | | EthLayer(const MacAddress& sourceMac, const MacAddress& destMac, uint16_t etherType = 0); |
100 | | |
101 | 0 | ~EthLayer() {} |
102 | | |
103 | | /** |
104 | | * Get a pointer to the Ethernet header. Notice this points directly to the data, so every change will change the actual packet data |
105 | | * @return A pointer to the ether_header |
106 | | */ |
107 | 1.49M | inline ether_header* getEthHeader() const { return (ether_header*)m_Data; } |
108 | | |
109 | | /** |
110 | | * Get the source MAC address |
111 | | * @return The source MAC address |
112 | | */ |
113 | 304k | inline MacAddress getSourceMac() const { return MacAddress(getEthHeader()->srcMac); } |
114 | | |
115 | | /** |
116 | | * Set source MAC address |
117 | | * @param sourceMac Source MAC to set |
118 | | */ |
119 | 0 | inline void setSourceMac(const MacAddress& sourceMac) { sourceMac.copyTo(getEthHeader()->srcMac); } |
120 | | |
121 | | /** |
122 | | * Get the destination MAC address |
123 | | * @return The destination MAC address |
124 | | */ |
125 | 304k | inline MacAddress getDestMac() const { return MacAddress(getEthHeader()->dstMac); } |
126 | | |
127 | | /** |
128 | | * Set destination MAC address |
129 | | * @param destMac Destination MAC to set |
130 | | */ |
131 | 0 | inline void setDestMac(const MacAddress& destMac) { destMac.copyTo(getEthHeader()->dstMac); } |
132 | | |
133 | | // implement abstract methods |
134 | | |
135 | | /** |
136 | | * Currently identifies the following next layers: IPv4Layer, IPv6Layer, ArpLayer, VlanLayer, PPPoESessionLayer, PPPoEDiscoveryLayer, |
137 | | * MplsLayer. |
138 | | * Otherwise sets PayloadLayer |
139 | | */ |
140 | | void parseNextLayer(); |
141 | | |
142 | | /** |
143 | | * @return Size of ether_header |
144 | | */ |
145 | 371k | size_t getHeaderLen() const { return sizeof(ether_header); } |
146 | | |
147 | | /** |
148 | | * Calculate ether_header#etherType for known protocols: IPv4, IPv6, ARP, VLAN |
149 | | */ |
150 | | void computeCalculateFields(); |
151 | | |
152 | | std::string toString() const; |
153 | | |
154 | 152k | OsiModelLayer getOsiModelLayer() const { return OsiModelDataLinkLayer; } |
155 | | |
156 | | /** |
157 | | * A static method that validates the input data |
158 | | * @param[in] data The pointer to the beginning of a byte stream of an Ethernet II packet |
159 | | * @param[in] dataLen The length of the byte stream |
160 | | * @return True if the data is valid and can represent an Ethernet II packet |
161 | | */ |
162 | | static bool isDataValid(const uint8_t* data, size_t dataLen); |
163 | | }; |
164 | | |
165 | | } // namespace pcpp |