VBVX 0.1.0
Header-only C++23 library for safe, zero-copy parsing of packet buffers.
Loading...
Searching...
No Matches
arp.hxx
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <cstdint>
5#include <span>
6#include <cstring>
7
8#include "utils.hxx"
9#include "ether.hxx"
10
11namespace vbvx {
12
14enum class ArpHType : uint16_t { Ethernet = 1 };
15
17enum class ArpOpCode : uint16_t { Request = 1, Reply = 2 };
18
27struct [[gnu::packed]] ArpHeader {
28 uint16_t htype_be;
29 uint16_t ptype_be;
30 uint8_t hlen;
31 uint8_t plen;
32 uint16_t oper_be;
33 uint8_t sha[6];
34 uint8_t spa[4];
35 uint8_t tha[6];
36 uint8_t tpa[4];
37
38 constexpr auto htype() const noexcept -> ArpHType {
39 return static_cast<ArpHType>(autoswap(htype_be));
40 }
41
42 constexpr auto ptype() const noexcept -> EtherType {
43 return static_cast<EtherType>(autoswap(ptype_be));
44 }
45
46 constexpr auto opcode() const noexcept -> ArpOpCode {
47 return static_cast<ArpOpCode>(autoswap(oper_be));
48 }
49
50 constexpr auto sender_mac() const noexcept -> std::span<const uint8_t, 6> {
51 return std::span<const uint8_t, 6>{sha, sha + 6};
52 }
53
54 constexpr auto target_mac() const noexcept -> std::span<const uint8_t, 6> {
55 return std::span<const uint8_t, 6>{tha, tha + 6};
56 }
57
58 constexpr auto sender_ipv4() const noexcept -> std::span<const uint8_t, 4> {
59 return std::span<const uint8_t, 4>{spa, spa + 4};
60 }
61
62 constexpr auto target_ipv4() const noexcept -> std::span<const uint8_t, 4> {
63 return std::span<const uint8_t, 4>{tpa, tpa + 4};
64 }
65
66 constexpr auto sender_ipv4_host() const noexcept -> uint32_t {
68 }
69
70 constexpr auto target_ipv4_host() const noexcept -> uint32_t {
72 }
73
74 constexpr void set_opcode(ArpOpCode code) noexcept {
75 oper_be = autoswap(static_cast<uint16_t>(code));
76 }
77};
78
79static_assert(sizeof(ArpHeader) == 28, "Wrong ARP header size");
80static_assert(alignof(ArpHeader) == 1, "Wrong ARP header alignment");
81
82} // namespace vbvx
Definition arp.hxx:11
ArpOpCode
ARP opcode values.
Definition arp.hxx:17
@ Request
Definition arp.hxx:17
@ Reply
Definition arp.hxx:17
constexpr _Tp read_from_bytes(const uint8_t *src)
Read a trivially copyable type from a byte array.
Definition utils.hxx:24
constexpr _Tp autoswap(_Tp tp)
Byte-swap a value if the host is little-endian.
Definition utils.hxx:13
ArpHType
ARP hardware type values.
Definition arp.hxx:14
@ Ethernet
Definition arp.hxx:14
EtherType
Ethernet frame EtherType values (network byte order).
Definition ether.hxx:19
Address Resolution Protocol (ARP) header.
Definition arp.hxx:27
constexpr auto ptype() const noexcept -> EtherType
Definition arp.hxx:42
constexpr auto target_ipv4_host() const noexcept -> uint32_t
Definition arp.hxx:70
constexpr auto sender_ipv4_host() const noexcept -> uint32_t
Definition arp.hxx:66
constexpr auto sender_mac() const noexcept -> std::span< const uint8_t, 6 >
Definition arp.hxx:50
constexpr auto htype() const noexcept -> ArpHType
Definition arp.hxx:38
constexpr auto target_mac() const noexcept -> std::span< const uint8_t, 6 >
Definition arp.hxx:54
constexpr auto sender_ipv4() const noexcept -> std::span< const uint8_t, 4 >
Definition arp.hxx:58
uint16_t ptype_be
Definition arp.hxx:29
uint8_t sha[6]
Definition arp.hxx:33
constexpr auto target_ipv4() const noexcept -> std::span< const uint8_t, 4 >
Definition arp.hxx:62
uint8_t tpa[4]
Definition arp.hxx:36
constexpr void set_opcode(ArpOpCode code) noexcept
Definition arp.hxx:74
constexpr auto opcode() const noexcept -> ArpOpCode
Definition arp.hxx:46
uint8_t tha[6]
Definition arp.hxx:35
uint16_t oper_be
Definition arp.hxx:32
uint16_t htype_be
Definition arp.hxx:28
uint8_t hlen
Definition arp.hxx:30
uint8_t spa[4]
Definition arp.hxx:34
uint8_t plen
Definition arp.hxx:31