OpenVPN 3 Core Library
Loading...
Searching...
No Matches
addrpair.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_ADDR_ADDRPAIR_H
13#define OPENVPN_ADDR_ADDRPAIR_H
14
15#include <sstream>
16
20#include <openvpn/addr/ip.hpp>
21
22namespace openvpn::IP {
23
24// AddrMaskPair is basically an object that combines an IP address (v4 or v6)
25// with a netmask or prefix length.
27{
28 public:
29 OPENVPN_EXCEPTION(addr_pair_mask_parse_error);
30
32 {
33 public:
34 OPENVPN_SIMPLE_EXCEPTION(addr_pair_string_error);
35
37 : size_(0)
38 {
39 }
40
41 explicit StringPair(const std::string &s1)
42 : size_(1)
43 {
44 data[0] = s1;
45 }
46
47 explicit StringPair(const std::string &s1, const std::string &s2)
48 : size_(2)
49 {
50 data[0] = s1;
51 data[1] = s2;
52 }
53
54 void push_back(const std::string &s)
55 {
56 if (size_ < 2)
57 data[size_++] = s;
58 else
59 throw addr_pair_string_error();
60 }
61
62 const std::string &operator[](const size_t i) const
63 {
64 if (i >= 2)
65 throw addr_pair_string_error();
66 return data[i];
67 }
68
69 std::string &operator[](const size_t i)
70 {
71 if (i >= 2)
72 throw addr_pair_string_error();
73 return data[i];
74 }
75
76 size_t size() const
77 {
78 return size_;
79 }
80
81 std::string render() const
82 {
83 switch (size_)
84 {
85 case 1:
86 return data[0];
87 case 2:
88 return data[0] + "/" + data[1];
89 default:
90 return "";
91 }
92 }
93
94 private:
95 std::string data[2];
96 unsigned int size_;
97 };
98
99 static AddrMaskPair from_string(const std::string &s1, const std::string &s2, const char *title = nullptr)
100 {
101 try
102 {
103 if (s2.empty())
104 {
105 const auto pair = Split::by_char<StringPair, NullLex, Split::NullLimit>(s1, '/');
106 return from_string_impl(pair, title);
107 }
108
109 const StringPair pair(s1, s2);
110 return from_string_impl(pair, title);
111 }
112 catch (const std::exception &e)
113 {
114 const StringPair pair(s1, s2);
115 error(e, pair.render(), title);
116 }
117 return {}; // NOTREACHED
118 }
119
120 static AddrMaskPair from_string(const std::string &s, const char *title = nullptr)
121 {
122 try
123 {
124 const auto pair = Split::by_char<StringPair, NullLex, Split::NullLimit>(s, '/');
125 return from_string_impl(pair, title);
126 }
127 catch (const std::exception &e)
128 {
129 error(e, s, title);
130 }
131 return {}; // NOTREACHED
132 }
133
134 static AddrMaskPair from_string(const StringPair &pair, const char *title = nullptr)
135 {
136 try
137 {
138 return from_string_impl(pair, title);
139 }
140 catch (const std::exception &e)
141 {
142 error(e, pair.render(), title);
143 }
144 return {}; // NOTREACHED
145 }
146
147 std::string to_string(const bool netmask_form = false) const
148 {
149 return netmask_form ? addr.to_string() + '/' + netmask.to_string()
150 : addr.to_string() + '/' + std::to_string(netmask.prefix_len());
151 }
152
153 bool is_canonical() const
154 {
155 return (addr & netmask) == addr;
156 }
157
159 {
160 return addr.version() == netmask.version() ? addr.version() : Addr::UNSPEC;
161 }
162
165
166 private:
167 static void error(const std::exception &e, const std::string &s, const char *title)
168 {
169 if (!title)
170 title = "";
171 OPENVPN_THROW(addr_pair_mask_parse_error, "AddrMaskPair parse error '" << title << "': " << s << " : " << e.what());
172 }
173
174 static AddrMaskPair from_string_impl(const StringPair &pair, const char *title = nullptr)
175 {
176 AddrMaskPair ret;
177 if (pair.size() == 1 || pair.size() == 2)
178 {
179 ret.addr = Addr::from_string(pair[0], title);
180 if (pair.size() == 2 && !pair[1].empty())
181 {
182 if (is_number(pair[1].c_str()))
184 parse_number_throw<unsigned int>(pair[1], "prefix length"));
185 else
186 ret.netmask = Addr::from_string(pair[1]);
187 ret.netmask.prefix_len(); // verify that netmask is ok
188 }
189 else
192 }
193 else
194 throw addr_pair_mask_parse_error("only one or two address terms allowed");
195 return ret;
196 }
197};
198OPENVPN_OSTREAM(AddrMaskPair, to_string)
199} // namespace openvpn::IP
200
201#endif
std::string & operator[](const size_t i)
Definition addrpair.hpp:69
void push_back(const std::string &s)
Definition addrpair.hpp:54
OPENVPN_SIMPLE_EXCEPTION(addr_pair_string_error)
StringPair(const std::string &s1)
Definition addrpair.hpp:41
const std::string & operator[](const size_t i) const
Definition addrpair.hpp:62
StringPair(const std::string &s1, const std::string &s2)
Definition addrpair.hpp:47
static Addr from_string(const std::string &ipstr, const TITLE &title, const Version required_version)
Definition ip.hpp:105
Version version() const
Definition ip.hpp:895
void verify_version_consistency(const Addr &other) const
Definition ip.hpp:954
std::string to_string() const
Definition ip.hpp:528
static Addr netmask_from_prefix_len(Version v, const unsigned int prefix_len)
Definition ip.hpp:502
unsigned int prefix_len() const
Definition ip.hpp:968
static Addr from_zero_complement(const Version v)
Definition ip.hpp:478
#define OPENVPN_THROW(exc, stuff)
bool is_number(std::string_view str)
Definition number.hpp:124
#define OPENVPN_OSTREAM(TYPE, METH)
Definition ostream.hpp:21
static AddrMaskPair from_string(const StringPair &pair, const char *title=nullptr)
Definition addrpair.hpp:134
static void error(const std::exception &e, const std::string &s, const char *title)
Definition addrpair.hpp:167
std::string to_string(const bool netmask_form=false) const
Definition addrpair.hpp:147
OPENVPN_EXCEPTION(addr_pair_mask_parse_error)
static AddrMaskPair from_string(const std::string &s1, const std::string &s2, const char *title=nullptr)
Definition addrpair.hpp:99
static AddrMaskPair from_string_impl(const StringPair &pair, const char *title=nullptr)
Definition addrpair.hpp:174
static AddrMaskPair from_string(const std::string &s, const char *title=nullptr)
Definition addrpair.hpp:120
Addr::Version version() const
Definition addrpair.hpp:158