OpenVPN 3 Core Library
Loading...
Searching...
No Matches
rgopt.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// This class handles parsing and representation of redirect-gateway
13// and redirect-private directives.
14
15#ifndef OPENVPN_CLIENT_RGOPT_H
16#define OPENVPN_CLIENT_RGOPT_H
17
19
20namespace openvpn {
22{
23 public:
24 enum Flags
25 {
26 RG_ENABLE = (1 << 0),
27 RG_REROUTE_GW = (1 << 1),
28 RG_LOCAL = (1 << 2),
29 RG_AUTO_LOCAL = (1 << 3),
30 RG_DEF1 = (1 << 4),
31 RG_BYPASS_DHCP = (1 << 5),
32 RG_BYPASS_DNS = (1 << 6),
33 RG_BLOCK_LOCAL = (1 << 7),
34 RG_IPv4 = (1 << 8),
35 RG_IPv6 = (1 << 9),
36
38 };
39
44
46 : flags_(flags)
47 {
48 }
49
50 explicit RedirectGatewayFlags(const OptionList &opt)
51 {
52 init(opt);
53 }
54
55 void init(const OptionList &opt)
56 {
58 doinit(opt, "redirect-gateway", true); // DIRECTIVE
59 doinit(opt, "redirect-private", false); // DIRECTIVE
60 }
61
62 unsigned int operator()() const
63 {
64 return flags_;
65 }
66
68 {
69 return rg_enabled() && (flags_ & RG_IPv4);
70 }
71
73 {
74 return rg_enabled() && (flags_ & RG_IPv6);
75 }
76
78 {
79 return flags_ & RG_LOCAL;
80 }
81
82 std::string to_string() const
83 {
84 std::string ret;
85 ret += "[ ";
86 if (flags_ & RG_ENABLE)
87 ret += "ENABLE ";
89 ret += "REROUTE_GW ";
90 if (flags_ & RG_LOCAL)
91 ret += "LOCAL ";
93 ret += "AUTO_LOCAL ";
94 if (flags_ & RG_DEF1)
95 ret += "DEF1 ";
97 ret += "BYPASS_DHCP ";
99 ret += "BYPASS_DNS ";
101 ret += "BLOCK_LOCAL ";
102 if (flags_ & RG_IPv4)
103 ret += "IPv4 ";
104 if (flags_ & RG_IPv6)
105 ret += "IPv6 ";
106 ret += "]";
107 return ret;
108 }
109
110 private:
111 bool rg_enabled() const
112 {
114 }
115
116 void doinit(const OptionList &opt, const std::string &directive, const bool redirect_gateway)
117 {
118 OptionList::IndexMap::const_iterator rg = opt.map().find(directive);
119 if (rg != opt.map().end())
120 add_flags(opt, rg->second, redirect_gateway);
121 }
122
123 void add_flags(const OptionList &opt, const OptionList::IndexList &idx, const bool redirect_gateway)
124 {
125 flags_ |= RG_ENABLE;
126 if (redirect_gateway)
128 else
129 flags_ &= ~RG_REROUTE_GW;
130 for (OptionList::IndexList::const_iterator i = idx.begin(); i != idx.end(); ++i)
131 {
132 const Option &o = opt[*i];
133 for (size_t j = 1; j < o.size(); ++j)
134 {
135 const std::string &f = o.get(j, 64);
136 if (f == "local")
137 flags_ |= RG_LOCAL;
138 else if (f == "autolocal")
140 else if (f == "def1")
141 flags_ |= RG_DEF1;
142 else if (f == "bypass-dhcp")
144 else if (f == "bypass-dns")
146 else if (f == "block-local")
148 else if (f == "ipv4")
149 flags_ |= RG_IPv4;
150 else if (f == "!ipv4")
151 flags_ &= ~RG_IPv4;
152 else if (f == "ipv6")
153 flags_ |= RG_IPv6;
154 else if (f == "!ipv6")
155 flags_ &= ~RG_IPv6;
156 }
157 }
158 }
159
160 unsigned int flags_;
161};
162} // namespace openvpn
163
164#endif
const IndexMap & map() const
Definition options.hpp:1562
std::vector< unsigned int > IndexList
Definition options.hpp:520
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
size_t size() const
Definition options.hpp:327
bool redirect_gateway_ipv6_enabled() const
Definition rgopt.hpp:72
bool redirect_gateway_ipv4_enabled() const
Definition rgopt.hpp:67
unsigned int operator()() const
Definition rgopt.hpp:62
RedirectGatewayFlags(const OptionList &opt)
Definition rgopt.hpp:50
void init(const OptionList &opt)
Definition rgopt.hpp:55
RedirectGatewayFlags(unsigned int flags)
Definition rgopt.hpp:45
void doinit(const OptionList &opt, const std::string &directive, const bool redirect_gateway)
Definition rgopt.hpp:116
bool redirect_gateway_local() const
Definition rgopt.hpp:77
void add_flags(const OptionList &opt, const OptionList::IndexList &idx, const bool redirect_gateway)
Definition rgopt.hpp:123
std::string to_string() const
Definition rgopt.hpp:82
reroute_gw flags
std::string ret
auto f(const Thing1 t)