OpenVPN 3 Core Library
Loading...
Searching...
No Matches
bufbe.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2026- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Bounds-checked big-endian (network byte order) integer read/write on a Buffer,
13// built on the platform-independent htonl/htons from socktypes.hpp. 64-bit values
14// are handled as two 32-bit halves, mirroring htonll().
15
16#pragma once
17
18#include <cstdint>
19
20#include <openvpn/common/socktypes.hpp> // for ntohl/htonl/ntohs/htons
22
24
26inline bool write_u16(Buffer &buf, std::uint16_t v)
27{
28 if (buf.remaining() < sizeof(v))
29 return false;
30 const std::uint16_t net = htons(v);
31 buf.write(&net, sizeof(net));
32 return true;
33}
34
36inline bool write_u32(Buffer &buf, std::uint32_t v)
37{
38 if (buf.remaining() < sizeof(v))
39 return false;
40 const std::uint32_t net = htonl(v);
41 buf.write(&net, sizeof(net));
42 return true;
43}
44
46inline bool write_u64(Buffer &buf, std::uint64_t v)
47{
48 return write_u32(buf, static_cast<std::uint32_t>(v >> 32))
49 && write_u32(buf, static_cast<std::uint32_t>(v & 0xffffffff));
50}
51
53inline bool read_u16(Buffer &buf, std::uint16_t &out)
54{
55 if (buf.size() < sizeof(out))
56 return false;
57 std::uint16_t net;
58 buf.read(&net, sizeof(net));
59 out = ntohs(net);
60 return true;
61}
62
64inline bool read_u32(Buffer &buf, std::uint32_t &out)
65{
66 if (buf.size() < sizeof(out))
67 return false;
68 std::uint32_t net;
69 buf.read(&net, sizeof(net));
70 out = ntohl(net);
71 return true;
72}
73
75inline bool read_u64(Buffer &buf, std::uint64_t &out)
76{
77 std::uint32_t hi, lo;
78 if (!read_u32(buf, hi) || !read_u32(buf, lo))
79 return false;
80 out = (static_cast<std::uint64_t>(hi) << 32) | lo;
81 return true;
82}
83
84} // namespace openvpn::BufferBE
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1241
void write(const T *data, const size_t size)
Write data to the buffer.
Definition buffer.hpp:1561
size_t remaining(const size_t tailroom=0) const
Return the number of additional T objects that can be added before capacity is reached (without consi...
Definition buffer.hpp:1466
void read(NCT *data, const size_t size)
Read data from the buffer into the specified memory location.
Definition buffer.hpp:1330
bool read_u32(Buffer &buf, std::uint32_t &out)
Read a big-endian uint32 from buf into out; false if fewer than 4 bytes.
Definition bufbe.hpp:64
bool read_u16(Buffer &buf, std::uint16_t &out)
Read a big-endian uint16 from buf into out; false if fewer than 2 bytes.
Definition bufbe.hpp:53
bool write_u16(Buffer &buf, std::uint16_t v)
Append a big-endian uint16 to buf; false if there is no room.
Definition bufbe.hpp:26
bool write_u64(Buffer &buf, std::uint64_t v)
Append a big-endian uint64 to buf; false if there is no room.
Definition bufbe.hpp:46
bool read_u64(Buffer &buf, std::uint64_t &out)
Read a big-endian uint64 from buf into out; false if fewer than 8 bytes.
Definition bufbe.hpp:75
bool write_u32(Buffer &buf, std::uint32_t v)
Append a big-endian uint32 to buf; false if there is no room.
Definition bufbe.hpp:36
static std::stringstream out
Definition test_path.cpp:10