1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2022 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_COMMON_STOP_H
#define OPENVPN_COMMON_STOP_H
#include <vector>
#include <functional>
#include <utility>
#include <mutex>
#include <openvpn/common/exception.hpp>
OPENVPN_EXCEPTION_INHERIT(std::range_error, openvpn_stop_limit);
namespace openvpn {
class Stop
{
public:
class Scope
{
friend Stop;
public:
Scope(Stop *stop_arg, std::function<void()> &&method_arg)
: stop(stop_arg),
method(std::move(method_arg)),
index(-1)
{
constexpr int stop_index_limit = 1000;
if (stop)
{
std::lock_guard<std::recursive_mutex> lock(stop->mutex);
if (stop->stop_called)
{
// stop already called, call method immediately
method();
}
else
{
if (index > stop_index_limit)
throw openvpn_stop_limit("Stop count limit exceeded");
index = static_cast<decltype(index)>(stop->scopes.size());
stop->scopes.push_back(this);
}
}
}
~Scope()
{
if (stop)
{
std::lock_guard<std::recursive_mutex> lock(stop->mutex);
if (index >= 0 && index < static_cast<int>(stop->scopes.size()) && stop->scopes[index] == this)
{
stop->scopes[index] = nullptr;
stop->prune();
}
}
}
private:
Scope(const Scope &) = delete;
Scope &operator=(const Scope &) = delete;
Stop *const stop;
const std::function<void()> method;
int index;
};
Stop()
{
}
void stop()
{
std::lock_guard<std::recursive_mutex> lock(mutex);
stop_called = true;
while (scopes.size())
{
Scope *scope = scopes.back();
scopes.pop_back();
if (scope)
{
scope->index = -1;
scope->method();
}
}
}
static bool is_triggered(const Stop *stop)
{
return stop && stop->stop_called;
}
Stop(const Stop &) = delete;
Stop &operator=(const Stop &) = delete;
private:
void prune()
{
while (scopes.size() && !scopes.back())
scopes.pop_back();
}
std::recursive_mutex mutex;
std::vector<Scope *> scopes;
volatile bool stop_called = false;
};
} // namespace openvpn
#endif
|