OpenVPN 3 Core Library
Loading...
Searching...
No Matches
vpnservpool.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_SERVER_VPNSERVPOOL_H
13#define OPENVPN_SERVER_VPNSERVPOOL_H
14
15#include <memory>
16#include <mutex>
17#include <sstream>
18#include <vector>
19
21#include <openvpn/common/rc.hpp>
23#include <openvpn/addr/ip.hpp>
25#include <openvpn/addr/pool.hpp>
26
28
29OPENVPN_EXCEPTION(vpn_serv_pool_error);
30
31struct IP46
32{
33 void add_routes(std::vector<IP::Route> &rtvec)
34 {
35 if (ip4.defined())
36 rtvec.emplace_back(ip4, ip4.size());
37 if (ip6.defined())
38 rtvec.emplace_back(ip6, ip6.size());
39 }
40
41 std::string to_string() const
42 {
43 std::ostringstream os;
44 os << '[' << ip4 << ' ' << ip6 << ']';
45 return os.str();
46 }
47
48 bool defined() const
49 {
50 return ip4.defined() || ip6.defined();
51 }
52
55};
56
57class Pool : public VPNServerNetblock
58{
59 public:
60 enum Flags
61 {
62 IPv4_DEPLETION = (1 << 0),
63 IPv6_DEPLETION = (1 << 1),
64 };
65
66 Pool(const OptionList &opt)
68 {
69 if (configured(opt, "server"))
70 {
71 pool4.add_range(netblock4().clients);
72 pool6.add_range(netblock6().clients);
73 }
74 }
75
76 // returns flags
77 unsigned int acquire(IP46 &addr_pair, const bool request_ipv6)
78 {
79 std::lock_guard<std::mutex> lock(mutex);
80 unsigned int flags = 0;
81 if (!pool4.acquire_addr(addr_pair.ip4))
83 if (request_ipv6 && netblock6().defined())
84 {
85 if (!pool6.acquire_addr(addr_pair.ip6))
87 }
88 return flags;
89 }
90
91 void release(IP46 &addr_pair)
92 {
93 std::lock_guard<std::mutex> lock(mutex);
94 if (addr_pair.ip4.defined())
95 pool4.release_addr(addr_pair.ip4);
96 if (addr_pair.ip6.defined())
97 pool6.release_addr(addr_pair.ip6);
98 }
99
100 private:
102 {
103 if (configured(opt, "server"))
104 return VPNServerNetblock(opt, "server", false, 0);
105 if (configured(opt, "ifconfig"))
106 return VPNServerNetblock(opt, "ifconfig", false, 0);
107 return VPNServerNetblock();
108 }
109
110 static bool configured(const OptionList &opt,
111 const std::string &opt_name)
112 {
113 return opt.exists(opt_name) || opt.exists(opt_name + "-ipv6");
114 }
115
116 std::mutex mutex;
117
120};
121
122class IP46AutoRelease : public IP46, public RC<thread_safe_refcount>
123{
124 public:
126
128 : pool(pool_arg)
129 {
130 }
131
133 {
134 if (pool)
135 pool->release(*this);
136 }
137
138 private:
140};
141
142} // namespace openvpn::VPNServerPool
143
144#endif
unsigned int size() const
Definition ip.hpp:1016
bool defined() const
Definition ip.hpp:872
void add_range(const RangeType< ADDR > &range)
Adds range of addresses to pool (pool will own the addresses).
Definition pool.hpp:38
bool acquire_addr(ADDR &dest)
Definition pool.hpp:76
void release_addr(const ADDR &addr)
Definition pool.hpp:122
bool exists(const std::string &name) const
Definition options.hpp:1325
The smart pointer class.
Definition rc.hpp:119
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912
const ClientNetblock & netblock6() const
const ClientNetblock & netblock4() const
static VPNServerNetblock init_snb_from_opt(const OptionList &opt)
unsigned int acquire(IP46 &addr_pair, const bool request_ipv6)
static bool configured(const OptionList &opt, const std::string &opt_name)
void release(IP46 &addr_pair)
Pool(const OptionList &opt)
#define OPENVPN_EXCEPTION(C)
std::string to_string() const
void add_routes(std::vector< IP::Route > &rtvec)
reroute_gw flags
std::ostringstream os