VBVX 0.1.0
Header-only C++23 library for safe, zero-copy parsing of packet buffers.
Loading...
Searching...
No Matches
tcp_header.hxx
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5#include "utils.hxx"
6#include "flags_view.hxx"
7
8namespace vbvx {
9
10enum class TCPFlags : uint8_t {
11 None = 0,
12 FIN = 0x01,
13 SYN = 0x02,
14 RST = 0x04,
15 PSH = 0x08,
16 ACK = 0x10,
17 URG = 0x20,
18 ECE = 0x40,
19 CWR = 0x80
20};
21
22template <> struct enable_bitmask_operators<TCPFlags> : std::true_type {};
23
32struct [[gnu::packed]] TCPHeader {
33 uint16_t src_port_be;
34 uint16_t dst_port_be;
35 uint32_t seq_num_be;
36 uint32_t ack_num_be;
37 uint8_t data_offset;
38 uint8_t tcp_flags;
39 uint16_t window_be;
40 uint16_t checksum_be;
41 uint16_t urgent_ptr_be;
42
43 constexpr auto src_port() const noexcept -> uint16_t {
44 return autoswap(src_port_be);
45 }
46
47 constexpr auto dst_port() const noexcept -> uint16_t {
48 return autoswap(dst_port_be);
49 }
50
51 constexpr auto seq_num() const noexcept -> uint32_t {
52 return autoswap(seq_num_be);
53 }
54
55 constexpr auto ack_num() const noexcept -> uint32_t {
56 return autoswap(ack_num_be);
57 }
58
59 constexpr auto header_words() const noexcept -> uint8_t {
60 return static_cast<uint8_t>((data_offset >> 4) & 0x0Fu);
61 }
62
63 constexpr auto header_bytes() const noexcept -> uint16_t {
64 return static_cast<uint16_t>(header_words() * 4u);
65 }
66
67 constexpr auto flags() const noexcept -> TCPFlags {
68 return static_cast<TCPFlags>(tcp_flags);
69 }
70
71 constexpr auto window() const noexcept -> uint16_t {
72 return autoswap(window_be);
73 }
74 constexpr auto checksum() const noexcept -> uint16_t {
75 return autoswap(checksum_be);
76 }
77
78 constexpr auto urgent_ptr() const noexcept -> uint16_t {
79 return autoswap(urgent_ptr_be);
80 }
81
82 constexpr void set_src_port(uint16_t v) noexcept {
84 }
85
86 constexpr void set_dst_port(uint16_t v) noexcept {
88 }
89
90 constexpr void set_seq_num(uint32_t v) noexcept { seq_num_be = autoswap(v); }
91 constexpr void set_ack_num(uint32_t v) noexcept { ack_num_be = autoswap(v); }
92 constexpr void set_flags(uint8_t f) noexcept { tcp_flags = f; }
93 constexpr void set_window(uint16_t v) noexcept { window_be = autoswap(v); }
94
95 constexpr void set_checksum(uint16_t v) noexcept {
97 }
98
99 constexpr void set_urgent_ptr(uint16_t v) noexcept {
101 }
102
103 constexpr bool valid_min_size() const noexcept {
104 return header_bytes() >= 20;
105 }
106};
107
108static_assert(sizeof(TCPHeader) == 20, "Wrong TCP header size");
109static_assert(alignof(TCPHeader) == 1, "Wrong TCP header alignment");
110
111} // namespace vbvx
Definition arp.hxx:11
@ None
Definition ip4_header.hxx:19
constexpr _Tp autoswap(_Tp tp)
Byte-swap a value if the host is little-endian.
Definition utils.hxx:13
TCPFlags
Definition tcp_header.hxx:10
@ ACK
Definition tcp_header.hxx:16
@ PSH
Definition tcp_header.hxx:15
@ ECE
Definition tcp_header.hxx:18
@ URG
Definition tcp_header.hxx:17
@ SYN
Definition tcp_header.hxx:13
@ CWR
Definition tcp_header.hxx:19
@ FIN
Definition tcp_header.hxx:12
@ RST
Definition tcp_header.hxx:14
TCP header (minimum 20 bytes).
Definition tcp_header.hxx:32
constexpr void set_checksum(uint16_t v) noexcept
Definition tcp_header.hxx:95
constexpr void set_urgent_ptr(uint16_t v) noexcept
Definition tcp_header.hxx:99
uint16_t src_port_be
Definition tcp_header.hxx:33
constexpr bool valid_min_size() const noexcept
Definition tcp_header.hxx:103
uint16_t checksum_be
Definition tcp_header.hxx:40
constexpr auto flags() const noexcept -> TCPFlags
Definition tcp_header.hxx:67
constexpr auto seq_num() const noexcept -> uint32_t
Definition tcp_header.hxx:51
constexpr void set_dst_port(uint16_t v) noexcept
Definition tcp_header.hxx:86
constexpr auto dst_port() const noexcept -> uint16_t
Definition tcp_header.hxx:47
constexpr void set_seq_num(uint32_t v) noexcept
Definition tcp_header.hxx:90
constexpr auto header_words() const noexcept -> uint8_t
Definition tcp_header.hxx:59
constexpr auto checksum() const noexcept -> uint16_t
Definition tcp_header.hxx:74
uint16_t window_be
Definition tcp_header.hxx:39
constexpr auto ack_num() const noexcept -> uint32_t
Definition tcp_header.hxx:55
constexpr void set_flags(uint8_t f) noexcept
Definition tcp_header.hxx:92
constexpr auto urgent_ptr() const noexcept -> uint16_t
Definition tcp_header.hxx:78
constexpr auto src_port() const noexcept -> uint16_t
Definition tcp_header.hxx:43
uint8_t data_offset
Definition tcp_header.hxx:37
constexpr void set_window(uint16_t v) noexcept
Definition tcp_header.hxx:93
uint16_t dst_port_be
Definition tcp_header.hxx:34
constexpr auto header_bytes() const noexcept -> uint16_t
Definition tcp_header.hxx:63
uint32_t ack_num_be
Definition tcp_header.hxx:36
uint16_t urgent_ptr_be
Definition tcp_header.hxx:41
constexpr auto window() const noexcept -> uint16_t
Definition tcp_header.hxx:71
constexpr void set_src_port(uint16_t v) noexcept
Definition tcp_header.hxx:82
uint32_t seq_num_be
Definition tcp_header.hxx:35
uint8_t tcp_flags
Definition tcp_header.hxx:38
constexpr void set_ack_num(uint32_t v) noexcept
Definition tcp_header.hxx:91
Enable bitwise operators for an enum class.
Definition flags_view.hxx:25