VBVX 0.1.0
Header-only C++23 library for safe, zero-copy parsing of packet buffers.
Loading...
Searching...
No Matches
ether.hxx
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <cstdint>
5#include <span>
6
7#include "utils.hxx"
8
9namespace vbvx {
10
19enum class EtherType : uint16_t {
20 IPv4 = 0x0800,
21 ARP = 0x0806,
22 VLAN = 0x8100,
23 IPv6 = 0x86DD
24};
25
34struct [[gnu::packed]] EtherHeader {
35 uint8_t dst[6];
36 uint8_t src[6];
37 uint16_t type_be;
38
39 constexpr auto dst_mac() const noexcept -> std::span<const uint8_t, 6> {
40 return std::span<const uint8_t, 6>(dst, 6);
41 }
42
43 constexpr auto src_mac() const noexcept -> std::span<const uint8_t, 6> {
44 return std::span<const uint8_t, 6>(src, 6);
45 }
46
47 constexpr auto type() const noexcept -> uint16_t { return autoswap(type_be); }
48};
49
50static_assert(sizeof(EtherHeader) == 14, "Wrong Ethernet header size");
51static_assert(alignof(EtherHeader) == 1, "Wrong Ethernet header alignment");
52
61enum class VlanPcp : uint8_t {
62 p0 = 0,
63 p1 = 1,
64 p2 = 2,
65 p3 = 3,
66 p4 = 4,
67 p5 = 5,
68 p6 = 6,
69 p7 = 7
70};
71
80struct [[gnu::packed]] VlanTci {
81 uint16_t raw{};
82
83 constexpr auto pcp() const noexcept -> VlanPcp {
84 return static_cast<VlanPcp>((raw >> 13) & 0x7u);
85 }
86
87 constexpr bool dei() const noexcept { return ((raw >> 12) & 0x1u) != 0; }
88
89 constexpr auto vid() const noexcept -> uint16_t {
90 return static_cast<uint16_t>(raw & 0x0FFFu);
91 }
92
93 constexpr void set_pcp(VlanPcp v) noexcept {
94 raw = static_cast<uint16_t>((raw & 0x1FFFu) | ((uint16_t(v) & 0x7u) << 13));
95 }
96
97 constexpr void set_dei(bool v) noexcept {
98 raw = static_cast<uint16_t>((raw & 0xEFFFu) | (uint16_t(v ? 1 : 0) << 12));
99 }
100
101 constexpr void set_vid(uint16_t v) noexcept {
102 raw = static_cast<uint16_t>((raw & 0xF000u) | (v & 0x0FFFu));
103 }
104};
105
114struct [[gnu::packed]] VlanHeader {
115 uint16_t tci_be;
116 uint16_t type_be;
117
118 constexpr auto tci() const noexcept -> uint16_t { return autoswap(tci_be); }
119 constexpr auto type() const noexcept -> uint16_t { return autoswap(type_be); }
120 constexpr void set_tci(uint16_t tci) noexcept { tci_be = autoswap(tci); }
121 constexpr void set_type(uint16_t type) noexcept { type_be = autoswap(type); }
122};
123
124static_assert(sizeof(VlanHeader) == 4, "Wrong VLAN header size");
125static_assert(alignof(VlanHeader) == 1, "Wrong VLAN header alignment");
126
127} // namespace vbvx
Definition arp.hxx:11
constexpr _Tp autoswap(_Tp tp)
Byte-swap a value if the host is little-endian.
Definition utils.hxx:13
EtherType
Ethernet frame EtherType values (network byte order).
Definition ether.hxx:19
@ IPv4
Definition ether.hxx:20
@ VLAN
Definition ether.hxx:22
@ ARP
Definition ether.hxx:21
@ IPv6
Definition ether.hxx:23
VlanPcp
VLAN Priority Code Point (PCP) values (3 bits).
Definition ether.hxx:61
@ p4
Definition ether.hxx:66
@ p2
Definition ether.hxx:64
@ p0
Definition ether.hxx:62
@ p7
Definition ether.hxx:69
@ p3
Definition ether.hxx:65
@ p6
Definition ether.hxx:68
@ p1
Definition ether.hxx:63
@ p5
Definition ether.hxx:67
Ethernet frame header (14 bytes).
Definition ether.hxx:34
constexpr auto dst_mac() const noexcept -> std::span< const uint8_t, 6 >
Definition ether.hxx:39
uint16_t type_be
Definition ether.hxx:37
constexpr auto src_mac() const noexcept -> std::span< const uint8_t, 6 >
Definition ether.hxx:43
constexpr auto type() const noexcept -> uint16_t
Definition ether.hxx:47
uint8_t src[6]
Definition ether.hxx:36
uint8_t dst[6]
Definition ether.hxx:35
VLAN (802.1Q) header (4 bytes after Ethernet header).
Definition ether.hxx:114
uint16_t type_be
Definition ether.hxx:116
constexpr auto tci() const noexcept -> uint16_t
Definition ether.hxx:118
constexpr void set_tci(uint16_t tci) noexcept
Definition ether.hxx:120
constexpr auto type() const noexcept -> uint16_t
Definition ether.hxx:119
constexpr void set_type(uint16_t type) noexcept
Definition ether.hxx:121
uint16_t tci_be
Definition ether.hxx:115
VLAN Tag Control Information (TCI) helpers and layout.
Definition ether.hxx:80
constexpr void set_dei(bool v) noexcept
Definition ether.hxx:97
constexpr auto vid() const noexcept -> uint16_t
Definition ether.hxx:89
constexpr bool dei() const noexcept
Definition ether.hxx:87
constexpr auto pcp() const noexcept -> VlanPcp
Definition ether.hxx:83
uint16_t raw
Definition ether.hxx:81
constexpr void set_pcp(VlanPcp v) noexcept
Definition ether.hxx:93
constexpr void set_vid(uint16_t v) noexcept
Definition ether.hxx:101