/src/PcapPlusPlus/Packet++/header/ArpLayer.h
Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef PACKETPP_ARP_LAYER |
2 | | #define PACKETPP_ARP_LAYER |
3 | | |
4 | | #include "Layer.h" |
5 | | #include "IpAddress.h" |
6 | | #include "MacAddress.h" |
7 | | |
8 | | /// @file |
9 | | |
10 | | /** |
11 | | * \namespace pcpp |
12 | | * \brief The main namespace for the PcapPlusPlus lib |
13 | | */ |
14 | | namespace pcpp |
15 | | { |
16 | | |
17 | | /** |
18 | | * @struct arphdr |
19 | | * Represents an ARP protocol header |
20 | | */ |
21 | | #pragma pack(push, 1) |
22 | | struct arphdr |
23 | | { |
24 | | /** Hardware type (HTYPE) */ |
25 | | uint16_t hardwareType; |
26 | | /** Protocol type (PTYPE). The permitted PTYPE values share a numbering space with those for EtherType */ |
27 | | uint16_t protocolType; |
28 | | /** Hardware address length (HLEN). For IPv4, this has the value 0x0800 */ |
29 | | uint8_t hardwareSize; |
30 | | /** Protocol length (PLEN). Length (in octets) of addresses used in the upper layer protocol. (The upper layer protocol specified in PTYPE.) IPv4 address size is 4 */ |
31 | | uint8_t protocolSize; |
32 | | /** Specifies the operation that the sender is performing: 1 (::ARP_REQUEST) for request, 2 (::ARP_REPLY) for reply */ |
33 | | uint16_t opcode; |
34 | | /** Sender hardware address (SHA) */ |
35 | | uint8_t senderMacAddr[6]; |
36 | | /** Sender protocol address (SPA) */ |
37 | | uint32_t senderIpAddr; |
38 | | /** Target hardware address (THA) */ |
39 | | uint8_t targetMacAddr[6]; |
40 | | /** Target protocol address (TPA) */ |
41 | | uint32_t targetIpAddr; |
42 | | }; |
43 | | #pragma pack(pop) |
44 | | |
45 | | /** |
46 | | * An enum for ARP message type |
47 | | */ |
48 | | enum ArpOpcode |
49 | | { |
50 | | ARP_REQUEST = 0x0001, ///< ARP request |
51 | | ARP_REPLY = 0x0002 ///< ARP reply (response) |
52 | | }; |
53 | | |
54 | | /** |
55 | | * @class ArpLayer |
56 | | * Represents an ARP protocol layer. Currently only IPv4 ARP messages are supported |
57 | | */ |
58 | | class ArpLayer : public Layer |
59 | | { |
60 | | public: |
61 | | /** |
62 | | * A constructor that creates the layer from an existing packet raw data |
63 | | * @param[in] data A pointer to the raw data (will be casted to @ref arphdr) |
64 | | * @param[in] dataLen Size of the data in bytes |
65 | | * @param[in] prevLayer A pointer to the previous layer |
66 | | * @param[in] packet A pointer to the Packet instance where layer will be stored in |
67 | | */ |
68 | 1.19k | ArpLayer(uint8_t* data, size_t dataLen, Layer* prevLayer, Packet* packet) : Layer(data, dataLen, prevLayer, packet) { m_Protocol = ARP; m_DataLen = sizeof(arphdr); } |
69 | | |
70 | | /** |
71 | | * A constructor that allocates a new ARP header |
72 | | * @param[in] opCode ARP message type (ARP request or ARP reply) |
73 | | * @param[in] senderMacAddr The sender MAC address (will be put in arphdr#senderMacAddr) |
74 | | * @param[in] targetMacAddr The target MAC address (will be put in arphdr#targetMacAddr) |
75 | | * @param[in] senderIpAddr The sender IP address (will be put in arphdr#senderIpAddr) |
76 | | * @param[in] targetIpAddr The target IP address (will be put in arphdr#targetIpAddr) |
77 | | */ |
78 | | ArpLayer(ArpOpcode opCode, const MacAddress& senderMacAddr, const MacAddress& targetMacAddr, const IPv4Address& senderIpAddr, const IPv4Address& targetIpAddr); |
79 | | |
80 | | ~ArpLayer() {} |
81 | | |
82 | | /** |
83 | | * Get a pointer to the ARP header. Notice this points directly to the data, so every change will change the actual packet data |
84 | | * @return A pointer to the @ref arphdr |
85 | | */ |
86 | 0 | inline arphdr* getArpHeader() const { return (arphdr*)m_Data; } |
87 | | |
88 | | /** |
89 | | * Get the sender hardware address (SHA) in the form of MacAddress |
90 | | * @return A MacAddress containing the sender hardware address (SHA) |
91 | | */ |
92 | 0 | inline MacAddress getSenderMacAddress() const { return MacAddress(getArpHeader()->senderMacAddr); } |
93 | | |
94 | | /** |
95 | | * Get the target hardware address (THA) in the form of MacAddress |
96 | | * @return A MacAddress containing the target hardware address (THA) |
97 | | */ |
98 | | inline MacAddress getTargetMacAddress() const { return MacAddress(getArpHeader()->targetMacAddr); } |
99 | | |
100 | | /** |
101 | | * Get the sender protocol address (SPA) in the form of IPv4Address |
102 | | * @return An IPv4Address containing the sender protocol address (SPA) |
103 | | */ |
104 | 0 | inline IPv4Address getSenderIpAddr() const { return getArpHeader()->senderIpAddr; } |
105 | | |
106 | | /** |
107 | | * Get the target protocol address (TPA) in the form of IPv4Address |
108 | | * @return An IPv4Address containing the target protocol address (TPA) |
109 | | */ |
110 | 0 | inline IPv4Address getTargetIpAddr() const { return getArpHeader()->targetIpAddr; } |
111 | | |
112 | | // implement abstract methods |
113 | | |
114 | | /** |
115 | | * Does nothing for this layer (ArpLayer is always last) |
116 | | */ |
117 | 1.19k | void parseNextLayer() {} |
118 | | |
119 | | /** |
120 | | * @return The size of @ref arphdr |
121 | | */ |
122 | 0 | size_t getHeaderLen() const { return sizeof(arphdr); } |
123 | | |
124 | | /** |
125 | | * Calculate the following fields: |
126 | | * - @ref arphdr#hardwareType = Ethernet (1) |
127 | | * - @ref arphdr#hardwareSize = 6 |
128 | | * - @ref arphdr#protocolType = ETHERTYPE_IP (assume IPv4 over ARP) |
129 | | * - @ref arphdr#protocolSize = 4 (assume IPv4 over ARP) |
130 | | * - if it's an ARP request: @ref arphdr#targetMacAddr = MacAddress("00:00:00:00:00:00") |
131 | | */ |
132 | | void computeCalculateFields(); |
133 | | |
134 | | std::string toString() const; |
135 | | |
136 | 1.19k | OsiModelLayer getOsiModelLayer() const { return OsiModelNetworkLayer; } |
137 | | }; |
138 | | |
139 | | } // namespace pcpp |
140 | | #endif /* PACKETPP_ARP_LAYER */ |