2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2022-09-29 11:41:13 +02:00
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Process-wide static initialization
|
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
#pragma once
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2015-05-11 04:04:22 +02:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2012-03-03 03:56:58 +01:00
|
|
|
#include <openvpn/common/base64.hpp>
|
2012-02-06 21:39:10 +01:00
|
|
|
#include <openvpn/time/time.hpp>
|
|
|
|
#include <openvpn/compress/compress.hpp>
|
2012-03-12 13:24:40 +01:00
|
|
|
#include <openvpn/init/cryptoinit.hpp>
|
|
|
|
#include <openvpn/init/engineinit.hpp>
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2024-06-25 18:45:07 +02:00
|
|
|
namespace openvpn::InitProcess {
|
2012-02-06 21:39:10 +01:00
|
|
|
class Init
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
|
|
|
private:
|
2020-08-28 16:24:38 +02:00
|
|
|
class InitImpl
|
2012-02-06 21:39:10 +01:00
|
|
|
{
|
2020-08-28 16:24:38 +02:00
|
|
|
public:
|
|
|
|
InitImpl()
|
|
|
|
{
|
|
|
|
// initialize time base
|
|
|
|
Time::reset_base();
|
|
|
|
|
|
|
|
// initialize compression
|
|
|
|
CompressContext::init_static();
|
|
|
|
|
|
|
|
// init OpenSSL if included
|
|
|
|
init_openssl("auto");
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
base64_init_static();
|
|
|
|
}
|
|
|
|
|
|
|
|
~InitImpl()
|
|
|
|
{
|
|
|
|
base64_uninit_static();
|
|
|
|
}
|
2012-02-10 15:25:52 +01:00
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
private:
|
|
|
|
// SSL library init happens when instantiated
|
|
|
|
crypto_init crypto_init_;
|
2012-02-06 21:39:10 +01:00
|
|
|
};
|
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
// process-wide singular instance
|
2023-04-24 17:51:09 +02:00
|
|
|
inline static std::weak_ptr<InitImpl> init_instance; // GLOBAL
|
|
|
|
inline static std::mutex the_instance_mutex; // GLOBAL
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
// istance of this class to refcount
|
|
|
|
std::shared_ptr<InitImpl> initptr;
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2023-01-11 20:43:22 +01:00
|
|
|
public:
|
|
|
|
Init()
|
|
|
|
{
|
2020-08-28 16:24:38 +02:00
|
|
|
std::lock_guard<std::mutex> lock(the_instance_mutex);
|
2012-03-03 03:56:58 +01:00
|
|
|
|
2020-08-28 16:24:38 +02:00
|
|
|
initptr = init_instance.lock();
|
|
|
|
if (!initptr)
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
2020-08-28 16:24:38 +02:00
|
|
|
initptr = std::make_shared<InitImpl>();
|
|
|
|
init_instance = initptr;
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
}
|
2012-02-06 21:39:10 +01:00
|
|
|
|
2023-01-11 20:43:22 +01:00
|
|
|
~Init()
|
|
|
|
{
|
2020-08-28 16:24:38 +02:00
|
|
|
// explicitly reset smart pointer to make the destructor run under the lock_guard
|
|
|
|
std::lock_guard<std::mutex> lock(the_instance_mutex);
|
|
|
|
initptr.reset();
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-25 18:45:07 +02:00
|
|
|
} // namespace openvpn::InitProcess
|