OpenVPN 3 Core Library
Loading...
Searching...
No Matches
buflimit.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_BUFLIMIT_H
13#define OPENVPN_BUFFER_BUFLIMIT_H
14
16
17namespace openvpn {
18
19template <typename T>
21{
22 public:
24 {
25 set_max(0, 0);
26 reset();
27 }
28
29 BufferLimit(const T max_lines_arg,
30 const T max_bytes_arg)
31 {
32 set_max(max_lines_arg, max_bytes_arg);
33 reset();
34 }
35
36 virtual ~BufferLimit() = default;
37
38 void set_max(const T max_lines_arg,
39 const T max_bytes_arg)
40 {
41 max_lines = max_lines_arg;
42 max_bytes = max_bytes_arg;
43 }
44
45 void reset()
46 {
47 n_bytes = n_lines = 0;
48 }
49
50 void add(const Buffer &buf)
51 {
52 T size = (T)buf.size();
53 n_bytes += size;
56 if (max_lines)
57 {
58 const unsigned char *p = buf.c_data();
59 while (size--)
60 {
61 const unsigned char c = *p++;
62 if (c == '\n')
63 {
64 ++n_lines;
65 if (n_lines > max_lines)
67 }
68 }
69 }
70 }
71
72 virtual void bytes_exceeded() = 0;
73 virtual void lines_exceeded() = 0;
74
75 protected:
80};
81
82} // namespace openvpn
83
84#endif
virtual void bytes_exceeded()=0
void add(const Buffer &buf)
Definition buflimit.hpp:50
virtual ~BufferLimit()=default
void set_max(const T max_lines_arg, const T max_bytes_arg)
Definition buflimit.hpp:38
BufferLimit(const T max_lines_arg, const T max_bytes_arg)
Definition buflimit.hpp:29
virtual void lines_exceeded()=0
const T * c_data() const
Returns a const pointer to the start of the buffer.
Definition buffer.hpp:1194
size_t size() const
Returns the size of the buffer in T objects.
Definition buffer.hpp:1242