Skip to content

Commit 573d33b

Browse files
committed
MODIFIED: ARP_SOURCE_FILE for parsing the packets.
1 parent 2bdfc21 commit 573d33b

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

src/parsers/arp.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "parsers/arp.h"
2+
#include <arpa/inet.h>
3+
#include <cstring>
4+
5+
namespace parsers {
6+
7+
ARPHeader parse_arp_header(const uint8_t* buffer, ssize_t length) {
8+
ARPHeader header;
9+
10+
if (length < 28) {
11+
throw std::runtime_error("ARP packet too short");
12+
}
13+
14+
// Extract fixed fields
15+
header.hardware_type = ntohs(*reinterpret_cast<const uint16_t*>(buffer));
16+
header.protocol_type = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 2));
17+
header.hardware_size = *(buffer + 4);
18+
header.protocol_size = *(buffer + 5);
19+
header.operation = ntohs(*reinterpret_cast<const uint16_t*>(buffer + 6));
20+
21+
// Extract addresses
22+
const uint8_t* ptr = buffer + 8;
23+
header.sender_mac.assign(ptr, ptr + 6);
24+
ptr += 6;
25+
26+
char ip_buf[INET_ADDRSTRLEN];
27+
std::memcpy(ip_buf, ptr, 4);
28+
header.sender_ip = inet_ntoa(*reinterpret_cast<const in_addr*>(ptr));
29+
ptr += 4;
30+
31+
header.target_mac.assign(ptr, ptr + 6);
32+
ptr += 6;
33+
34+
std::memcpy(ip_buf, ptr, 4);
35+
header.target_ip = inet_ntoa(*reinterpret_cast<const in_addr*>(ptr));
36+
37+
return header;
38+
}
39+
40+
} // namespace parsers

0 commit comments

Comments
 (0)