OpenVPN 3 Core Library
Loading...
Searching...
No Matches
bufcomplete.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) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12#ifndef OPENVPN_BUFFER_BUFCOMPLETE_H
13#define OPENVPN_BUFFER_BUFCOMPLETE_H
14
15#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
16#include <algorithm> // for std::min
17
19
20namespace openvpn {
21
23{
24 public:
25 virtual ~BufferComplete() = default;
26
27 /* each advance/get method returns false if message is incomplete */
28 bool advance(size_t size)
29 {
30 while (size)
31 {
32 if (!fetch_buffer())
33 return false;
34 const size_t s = std::min(size, buf.size());
35 buf.advance(s);
36 size -= s;
37 }
38 return true;
39 }
40
41 // assumes embedded big-endian uint16_t length in the stream
43 {
44 std::uint8_t h, l;
45 if (!get(h))
46 return false;
47 if (!get(l))
48 return false;
49 return advance(size_t(h) << 8 | size_t(l));
50 }
51
53 {
54 std::uint8_t c;
55 while (get(c))
56 {
57 if (!c)
58 return true;
59 }
60 return false;
61 }
62
63 bool get(std::uint8_t &c)
64 {
65 if (!fetch_buffer())
66 return false;
67 c = buf.pop_front();
68 return true;
69 }
70
71 bool defined() const
72 {
73 return buf.defined();
74 }
75
76 protected:
77 void reset_buf(const Buffer &buf_arg)
78 {
79 buf = buf_arg;
80 }
81
82 void reset_buf()
83 {
85 }
86
87 private:
88 virtual void next_buffer() = 0;
89
91 {
92 if (buf.defined())
93 return true;
95 return buf.defined();
96 }
97
99};
100
101} // namespace openvpn
102
103#endif
virtual void next_buffer()=0
bool advance(size_t size)
bool get(std::uint8_t &c)
virtual ~BufferComplete()=default
void reset_buf(const Buffer &buf_arg)
bool defined() const
Returns true if the buffer is not empty.
Definition buffer.hpp:1224
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242
void advance(const size_t delta)
Advances the buffer by the specified delta.
Definition buffer.hpp:1277
T pop_front()
Removes and returns the first element from the buffer.
Definition buffer.hpp:1256
void reset_content()
Resets the content of the buffer.
Definition buffer.hpp:1176