OpenVPN 3 Core Library
Loading...
Searching...
No Matches
buflist.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_BUFLIST_H
13#define OPENVPN_BUFFER_BUFLIST_H
14
15#include <list>
16#include <utility>
17
20
21namespace openvpn {
22
23template <template <typename...> class COLLECTION>
24struct BufferCollection : public COLLECTION<BufferPtr>
25{
26 using COLLECTION<BufferPtr>::size;
27 using COLLECTION<BufferPtr>::front;
28 using COLLECTION<BufferPtr>::empty;
29 using COLLECTION<BufferPtr>::back;
30 using COLLECTION<BufferPtr>::emplace_back;
31
32 BufferPtr join(const size_t headroom,
33 const size_t tailroom,
34 const bool size_1_optim) const
35 {
36 // special optimization if list contains
37 // a single element that satisfies our
38 // headroom/tailroom constraints.
39 if (size_1_optim
40 && size() == 1
41 && front()->offset() >= headroom
42 && front()->remaining() >= tailroom)
43 return front();
44
45 // first pass -- measure total size
46 const size_t size = join_size();
47
48 // allocate buffer
49 auto big = BufferAllocatedRc::Create(size + headroom + tailroom, 0);
50 big->init_headroom(headroom);
51
52 // second pass -- copy data
53 for (auto &b : *this)
54 big->write(b->c_data(), b->size());
55
56 return big;
57 }
58
60 {
61 return join(0, 0, true);
62 }
63
64 size_t join_size() const
65 {
66 size_t size = 0;
67 for (auto &b : *this)
68 size += b->size();
69 return size;
70 }
71
72 std::string to_string() const
73 {
74 BufferPtr bp = join();
75 return buf_to_string(*bp);
76 }
77
79 {
81 for (auto &b : *this)
82 ret.emplace_back(BufferAllocatedRc::Create(*b));
83 return ret;
84 }
85
86 void put_consume(BufferAllocated &buf, const size_t tailroom = 0)
87 {
88 const size_t s = buf.size();
89 if (!s)
90 return;
91 if (!empty())
92 {
93 // special optimization if buf data fits in
94 // back() unused tail capacity -- if so, append
95 // buf to existing back().
96 BufferPtr &b = back();
97 const size_t r = b->remaining(tailroom);
98 if (s < r)
99 {
100 b->write(buf.read_alloc(s), s);
101 return;
102 }
103 }
104 emplace_back(BufferAllocatedRc::Create(std::move(buf)));
105 }
106};
107
110} // namespace openvpn
111
112#endif
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1225
auto * read_alloc(const size_t size)
Allocate memory and read data from the buffer into the allocated memory.
Definition buffer.hpp:1326
static Ptr Create(ArgsT &&...args)
Creates a new instance of RcEnable with the given arguments.
Definition make_rc.hpp:43
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
BufferCollection< std::list > BufferList
Definition buflist.hpp:108
BufferCollection< std::vector > BufferVector
Definition buflist.hpp:109
std::string buf_to_string(const Buffer &buf)
Definition bufstr.hpp:22
size_t join_size() const
Definition buflist.hpp:64
BufferPtr join() const
Definition buflist.hpp:59
BufferPtr join(const size_t headroom, const size_t tailroom, const bool size_1_optim) const
Definition buflist.hpp:32
std::string to_string() const
Definition buflist.hpp:72
BufferCollection copy() const
Definition buflist.hpp:78
void put_consume(BufferAllocated &buf, const size_t tailroom=0)
Definition buflist.hpp:86
std::string ret