PcapPlusPlus  Next
EthLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "MacAddress.h"
5 
7 
12 namespace pcpp
13 {
14 
19 #pragma pack(push, 1)
20  struct ether_header
21  {
23  uint8_t dstMac[6];
25  uint8_t srcMac[6];
27  uint16_t etherType;
28  };
29 #pragma pack(pop)
30 
31  /* Ethernet protocol ID's */
32 
34 #define PCPP_ETHERTYPE_IP 0x0800
36 #define PCPP_ETHERTYPE_ARP 0x0806
38 #define PCPP_ETHERTYPE_ETHBRIDGE 0x6558
40 #define PCPP_ETHERTYPE_REVARP 0x8035
42 #define PCPP_ETHERTYPE_AT 0x809B
44 #define PCPP_ETHERTYPE_AARP 0x80F3
46 #define PCPP_ETHERTYPE_VLAN 0x8100
48 #define PCPP_ETHERTYPE_IPX 0x8137
50 #define PCPP_ETHERTYPE_IPV6 0x86dd
52 #define PCPP_ETHERTYPE_LOOPBACK 0x9000
54 #define PCPP_ETHERTYPE_PPPOED 0x8863
56 #define PCPP_ETHERTYPE_PPPOES 0x8864
58 #define PCPP_ETHERTYPE_MPLS 0x8847
60 #define PCPP_ETHERTYPE_PPP 0x880B
62 #define PCPP_ETHERTYPE_ROCEV1 0x8915
64 #define PCPP_ETHERTYPE_IEEE_802_1AD 0x88A8
66 #define PCPP_ETHERTYPE_WAKE_ON_LAN 0x0842
67 
72  class EthLayer : public Layer
73  {
74  public:
81  EthLayer(uint8_t* data, size_t dataLen, Packet* packet) : Layer(data, dataLen, nullptr, packet, Ethernet)
82  {}
83 
91  EthLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
92  : Layer(data, dataLen, prevLayer, packet, Ethernet)
93  {}
94 
102  EthLayer(const MacAddress& sourceMac, const MacAddress& destMac, uint16_t etherType = 0);
103 
104  ~EthLayer() override = default;
105 
111  inline ether_header* getEthHeader() const
112  {
113  return reinterpret_cast<ether_header*>(m_Data);
114  }
115 
120  inline MacAddress getSourceMac() const
121  {
122  return MacAddress(getEthHeader()->srcMac);
123  }
124 
129  inline void setSourceMac(const MacAddress& sourceMac)
130  {
131  sourceMac.copyTo(getEthHeader()->srcMac);
132  }
133 
138  inline MacAddress getDestMac() const
139  {
140  return MacAddress(getEthHeader()->dstMac);
141  }
142 
147  inline void setDestMac(const MacAddress& destMac)
148  {
149  destMac.copyTo(getEthHeader()->dstMac);
150  }
151 
152  // implement abstract methods
153 
158  void parseNextLayer() override;
159 
163  size_t getHeaderLen() const override
164  {
165  return sizeof(ether_header);
166  }
167 
171  void computeCalculateFields() override;
172 
173  std::string toString() const override;
174 
176  {
177  return OsiModelDataLinkLayer;
178  }
179 
186  static bool isDataValid(const uint8_t* data, size_t dataLen);
187  };
188 
189 } // namespace pcpp
Definition: EthLayer.h:73
OsiModelLayer getOsiModelLayer() const override
Definition: EthLayer.h:175
std::string toString() const override
static bool isDataValid(const uint8_t *data, size_t dataLen)
EthLayer(const MacAddress &sourceMac, const MacAddress &destMac, uint16_t etherType=0)
void parseNextLayer() override
ether_header * getEthHeader() const
Definition: EthLayer.h:111
void setSourceMac(const MacAddress &sourceMac)
Definition: EthLayer.h:129
MacAddress getDestMac() const
Definition: EthLayer.h:138
MacAddress getSourceMac() const
Definition: EthLayer.h:120
size_t getHeaderLen() const override
Definition: EthLayer.h:163
void computeCalculateFields() override
EthLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: EthLayer.h:91
void setDestMac(const MacAddress &destMac)
Definition: EthLayer.h:147
EthLayer(uint8_t *data, size_t dataLen, Packet *packet)
Definition: EthLayer.h:81
Definition: Layer.h:69
Definition: MacAddress.h:25
void copyTo(uint8_t **arr) const
Definition: MacAddress.h:152
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
const ProtocolType Ethernet
Definition: ProtocolType.h:33
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelDataLinkLayer
Definition: ProtocolType.h:368
Definition: EthLayer.h:21
uint16_t etherType
Definition: EthLayer.h:27
uint8_t srcMac[6]
Definition: EthLayer.h:25
uint8_t dstMac[6]
Definition: EthLayer.h:23