File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments