PcapPlusPlus  Next
ArpLayer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Layer.h"
4 #include "IpAddress.h"
5 #include "MacAddress.h"
6 
8 
13 namespace pcpp
14 {
15 
20 #pragma pack(push, 1)
21  struct arphdr
22  {
24  uint16_t hardwareType;
26  uint16_t protocolType;
28  uint8_t hardwareSize;
31  uint8_t protocolSize;
34  uint16_t opcode;
36  uint8_t senderMacAddr[6];
38  uint32_t senderIpAddr;
40  uint8_t targetMacAddr[6];
42  uint32_t targetIpAddr;
43  };
44 #pragma pack(pop)
45 
49  enum ArpOpcode
50  {
51  ARP_REQUEST = 0x0001,
52  ARP_REPLY = 0x0002
53  };
54 
59  class ArpLayer : public Layer
60  {
61  public:
69  ArpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet)
70  : Layer(data, dataLen, prevLayer, packet, ARP)
71  {
72  m_DataLen = sizeof(arphdr);
73  }
74 
83  ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr,
84  const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr);
85 
86  ~ArpLayer() override = default;
87 
93  inline arphdr* getArpHeader() const
94  {
95  return reinterpret_cast<arphdr*>(m_Data);
96  }
97 
103  {
104  return MacAddress(getArpHeader()->senderMacAddr);
105  }
106 
112  {
113  return MacAddress(getArpHeader()->targetMacAddr);
114  }
115 
121  {
122  return getArpHeader()->senderIpAddr;
123  }
124 
130  {
131  return getArpHeader()->targetIpAddr;
132  }
133 
134  // implement abstract methods
135 
139  void parseNextLayer() override
140  {}
141 
145  size_t getHeaderLen() const override
146  {
147  return sizeof(arphdr);
148  }
149 
158  void computeCalculateFields() override;
159 
163  bool isRequest() const;
164 
168  bool isReply() const;
169 
170  std::string toString() const override;
171 
173  {
174  return OsiModelNetworkLayer;
175  }
176  };
177 
178 } // namespace pcpp
Definition: ArpLayer.h:60
ArpLayer(ArpOpcode opCode, const MacAddress &senderMacAddr, const MacAddress &targetMacAddr, const IPv4Address &senderIpAddr, const IPv4Address &targetIpAddr)
arphdr * getArpHeader() const
Definition: ArpLayer.h:93
IPv4Address getTargetIpAddr() const
Definition: ArpLayer.h:129
void parseNextLayer() override
Definition: ArpLayer.h:139
bool isReply() const
size_t getHeaderLen() const override
Definition: ArpLayer.h:145
MacAddress getTargetMacAddress() const
Definition: ArpLayer.h:111
void computeCalculateFields() override
bool isRequest() const
IPv4Address getSenderIpAddr() const
Definition: ArpLayer.h:120
std::string toString() const override
ArpLayer(uint8_t *data, size_t dataLen, Layer *prevLayer, Packet *packet)
Definition: ArpLayer.h:69
OsiModelLayer getOsiModelLayer() const override
Definition: ArpLayer.h:172
MacAddress getSenderMacAddress() const
Definition: ArpLayer.h:102
Definition: IpAddress.h:32
Definition: Layer.h:69
Definition: MacAddress.h:25
Definition: Packet.h:27
The main namespace for the PcapPlusPlus lib.
ArpOpcode
Definition: ArpLayer.h:50
@ ARP_REQUEST
ARP request.
Definition: ArpLayer.h:51
@ ARP_REPLY
ARP reply (response)
Definition: ArpLayer.h:52
OsiModelLayer
Definition: ProtocolType.h:364
@ OsiModelNetworkLayer
Definition: ProtocolType.h:370
const ProtocolType ARP
Definition: ProtocolType.h:78
Definition: ArpLayer.h:22
uint32_t targetIpAddr
Definition: ArpLayer.h:42
uint8_t protocolSize
Definition: ArpLayer.h:31
uint16_t opcode
Definition: ArpLayer.h:34
uint16_t hardwareType
Definition: ArpLayer.h:24
uint8_t targetMacAddr[6]
Definition: ArpLayer.h:40
uint32_t senderIpAddr
Definition: ArpLayer.h:38
uint8_t hardwareSize
Definition: ArpLayer.h:28
uint8_t senderMacAddr[6]
Definition: ArpLayer.h:36
uint16_t protocolType
Definition: ArpLayer.h:26