OpenVPN 3 Core Library
Loading...
Searching...
No Matches
range.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_RANGE_H
13#define OPENVPN_ADDR_RANGE_H
14
15#include <cstddef>
16#include <string>
17#include <sstream>
18
19#include <openvpn/addr/ip.hpp>
20
21namespace openvpn::IP {
22
23// Denote a range of IP addresses with a start and extent,
24// where A represents an address class.
25// A should be a network address class such as IP::Addr, IPv4::Addr, or IPv6::Addr.
26
63template <typename ADDR>
65{
66 public:
67 class Iterator;
68
72 RangeType();
73
79 RangeType(const ADDR &start, const std::size_t extent);
80
85 Iterator begin() const;
86
91 Iterator end() const;
92
98 Iterator iterator() const;
99
104 bool defined() const;
105
110 const ADDR &start() const;
111
116 std::size_t extent() const;
117
124 RangeType pull_front(std::size_t extent);
125
130 std::string to_string() const;
131
132 private:
133 ADDR start_;
134 std::size_t extent_;
135};
136
154template <typename ADDR>
155class RangeType<ADDR>::Iterator
156{
157 friend class RangeType;
158
159 public:
164 bool more() const;
165
170 const ADDR &addr() const;
171
176 void next();
177
183 Iterator &operator++();
184
190 const ADDR &operator*() const;
191
198 bool operator!=(const Iterator &rhs) const;
199
200 private:
206 Iterator(const RangeType &range)
207 : addr_(range.start_), remaining_(range.extent_)
208 {
209 }
210
211 ADDR addr_;
212 std::size_t remaining_;
213};
214
216
224template <typename ADDR>
226{
227 public:
234 RangePartitionType(const RangeType<ADDR> &src_range, const std::size_t n_partitions);
235
242 bool next(RangeType<ADDR> &r);
243
244 private:
246 std::size_t remaining;
247};
248
250
251// ================================================================================================
252
253template <typename ADDR>
255{
256 return remaining_ > 0;
257}
258
259template <typename ADDR>
260inline const ADDR &RangeType<ADDR>::Iterator::addr() const
261{
262 return addr_;
263}
264
265template <typename ADDR>
267{
268 if (more())
269 {
270 ++addr_;
271 --remaining_;
272 }
273}
274
275template <typename ADDR>
277{
278 next();
279 return *this;
280}
281
282template <typename ADDR>
283inline const ADDR &RangeType<ADDR>::Iterator::operator*() const
284{
285 return addr_;
286}
287
288template <typename ADDR>
290{
291 return remaining_ != rhs.remaining_ || addr_ != rhs.addr_;
292}
293
294template <typename ADDR>
296 : extent_(0)
297{
298}
299
300template <typename ADDR>
301inline RangeType<ADDR>::RangeType(const ADDR &start, const std::size_t extent)
302 : start_(start), extent_(extent)
303{
304}
305
306template <typename ADDR>
308{
309 return Iterator(*this);
310}
311
312template <typename ADDR>
314{
315 RangeType end_range = *this;
316 end_range.start_ += static_cast<long>(end_range.extent_);
317 end_range.extent_ = 0;
318 return Iterator(end_range);
319}
320
321template <typename ADDR>
323{
324 return Iterator(*this);
325}
326
327template <typename ADDR>
328inline bool RangeType<ADDR>::defined() const
329{
330 return extent_ > 0;
331}
332
333template <typename ADDR>
334inline const ADDR &RangeType<ADDR>::start() const
335{
336 return start_;
337}
338
339template <typename ADDR>
340inline std::size_t RangeType<ADDR>::extent() const
341{
342 return extent_;
343}
344
345template <typename ADDR>
347{
348 if (extent > extent_)
349 extent = extent_;
350 RangeType ret(start_, extent);
351 start_ += extent;
352 extent_ -= extent;
353 return ret;
354}
355
356template <typename ADDR>
357inline std::string RangeType<ADDR>::to_string() const
358{
359 std::ostringstream os;
360 os << start_.to_string() << '[' << extent_ << ']';
361 return os.str();
362}
363
364template <typename ADDR>
365inline RangePartitionType<ADDR>::RangePartitionType(const RangeType<ADDR> &src_range, const std::size_t n_partitions)
366 : range(src_range),
367 remaining(n_partitions)
368{
369}
370
371template <typename ADDR>
373{
374 if (remaining)
375 {
376 if (remaining > 1)
377 r = range.pull_front(range.extent() / remaining);
378 else
379 r = range;
380 --remaining;
381 return r.defined();
382 }
383 else
384 return false;
385}
386
387} // namespace openvpn::IP
388
389#endif
divide a range of IP addresses into smaller, equal-sized partitions
Definition range.hpp:226
bool next(RangeType< ADDR > &r)
Retrieves the next partition in the range.
Definition range.hpp:372
RangeType< ADDR > range
Definition range.hpp:245
RangePartitionType(const RangeType< ADDR > &src_range, const std::size_t n_partitions)
Constructor for RangePartitionType.
Definition range.hpp:365
allow easy navigation through a series of IP addresses
Definition range.hpp:156
Iterator & operator++()
Prefix increment operator.
Definition range.hpp:276
const ADDR & addr() const
Get the current address in the range.
Definition range.hpp:260
bool more() const
Check if there are more elements in the range.
Definition range.hpp:254
const ADDR & operator*() const
Dereference operator.
Definition range.hpp:283
void next()
Move to the next address in the range.
Definition range.hpp:266
Iterator(const RangeType &range)
Constructor for the Iterator class.
Definition range.hpp:206
bool operator!=(const Iterator &rhs) const
Inequality comparison operator.
Definition range.hpp:289
designed to represent and manage a range of IP addresses.
Definition range.hpp:65
Iterator iterator() const
Get an iterator for the range.
Definition range.hpp:322
std::size_t extent_
Definition range.hpp:134
Iterator end() const
Get an iterator pointing to the end of the range.
Definition range.hpp:313
bool defined() const
Check if the range is defined (non-empty).
Definition range.hpp:328
Iterator begin() const
Get an iterator pointing to the beginning of the range.
Definition range.hpp:307
RangeType()
Default constructor for RangeType.
Definition range.hpp:295
const ADDR & start() const
Get the starting address of the range.
Definition range.hpp:334
std::size_t extent() const
Get the extent (size) of the range.
Definition range.hpp:340
std::string to_string() const
Convert the range to a string representation.
Definition range.hpp:357
RangeType pull_front(std::size_t extent)
Remove and return a new range from the front of this range.
Definition range.hpp:346
constexpr BaseT operator*(IntrinsicType< BaseT, T > lhs, IntrinsicType< BaseT, T > rhs) noexcept
Multiplication operator.
std::string ret
std::ostringstream os