PcapPlusPlus  22.11
EthLayer.h
Go to the documentation of this file.
1 #ifndef PACKETPP_ETH_LAYER
2 #define PACKETPP_ETH_LAYER
3 
4 #include "Layer.h"
5 #include "MacAddress.h"
6 
8 
13 namespace pcpp
14 {
15 
20 #pragma pack(push, 1)
21  struct ether_header
22  {
24  uint8_t dstMac[6];
26  uint8_t srcMac[6];
28  uint16_t etherType;
29  };
30 #pragma pack(pop)
31 
32  /* Ethernet protocol ID's */
33 
35 #define PCPP_ETHERTYPE_IP 0x0800
36 
37 #define PCPP_ETHERTYPE_ARP 0x0806
38 
39 #define PCPP_ETHERTYPE_ETHBRIDGE 0x6558
40 
41 #define PCPP_ETHERTYPE_REVARP 0x8035
42 
43 #define PCPP_ETHERTYPE_AT 0x809B
44 
45 #define PCPP_ETHERTYPE_AARP 0x80F3
46 
47 #define PCPP_ETHERTYPE_VLAN 0x8100
48 
49 #define PCPP_ETHERTYPE_IPX 0x8137
50 
51 #define PCPP_ETHERTYPE_IPV6 0x86dd
52 
53 #define PCPP_ETHERTYPE_LOOPBACK 0x9000
54 
55 #define PCPP_ETHERTYPE_PPPOED 0x8863
56 
57 #define PCPP_ETHERTYPE_PPPOES 0x8864
58 
59 #define PCPP_ETHERTYPE_MPLS 0x8847
60 
61 #define PCPP_ETHERTYPE_PPP 0x880B
62 
63 #define PCPP_ETHERTYPE_ROCEV1 0x8915
64 
65 #define PCPP_ETHERTYPE_IEEE_802_1AD 0x88A8
66 
67 #define PCPP_ETHERTYPE_WAKE_ON_LAN 0x0842
68 
69 
74  class EthLayer : public Layer
75  {
76  public:
83  EthLayer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, NULL, packet) { m_Protocol = Ethernet; }
84 
92  EthLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = Ethernet; }
93 
100  EthLayer(const MacAddress& sourceMac, const MacAddress& destMac, uint16_t etherType = 0);
101 
102  ~EthLayer() {}
103 
108  inline ether_header* getEthHeader() const { return (ether_header*)m_Data; }
109 
114  inline MacAddress getSourceMac() const { return MacAddress(getEthHeader()->srcMac); }
115 
120  inline void setSourceMac(const MacAddress& sourceMac) { sourceMac.copyTo(getEthHeader()->srcMac); }
121 
126  inline MacAddress getDestMac() const { return MacAddress(getEthHeader()->dstMac); }
127 
132  inline void setDestMac(const MacAddress& destMac) { destMac.copyTo(getEthHeader()->dstMac); }
133 
134  // implement abstract methods
135 
141  void parseNextLayer();
142 
146  size_t getHeaderLen() const { return sizeof(ether_header); }
147 
151  void computeCalculateFields();
152 
153  std::string toString() const;
154 
156 
163  static bool isDataValid(const uint8_t* data, size_t dataLen);
164  };
165 
166 } // namespace pcpp
167 
168 #endif /* PACKETPP_ETH_LAYER */
The main namespace for the PcapPlusPlus lib.
OsiModelLayer
Definition: ProtocolType.h:298
size_t getHeaderLen() const
Definition: EthLayer.h:146
uint16_t etherType
Definition: EthLayer.h:28
EthLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: EthLayer.h:92
uint8_t dstMac[6]
Definition: EthLayer.h:24
Definition: ProtocolType.h:303
Definition: Layer.h:70
EthLayer(uint8_t *data, size_t dataLen, Packet *packet)
Definition: EthLayer.h:83
Definition: Packet.h:26
OsiModelLayer getOsiModelLayer() const
Definition: EthLayer.h:155
ether_header * getEthHeader() const
Definition: EthLayer.h:108
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:146
Definition: EthLayer.h:74
const ProtocolType Ethernet
Definition: ProtocolType.h:28
uint8_t srcMac[6]
Definition: EthLayer.h:26
MacAddress getSourceMac() const
Definition: EthLayer.h:114
MacAddress getDestMac() const
Definition: EthLayer.h:126
Definition: EthLayer.h:21
Definition: MacAddress.h:28
void setSourceMac(const MacAddress &sourceMac)
Definition: EthLayer.h:120
void setDestMac(const MacAddress &destMac)
Definition: EthLayer.h:132