OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hostlist.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_COMMON_HOSTLIST_H
13#define OPENVPN_COMMON_HOSTLIST_H
14
15#include <string>
16#include <sstream>
17#include <vector>
18#include <algorithm>
19
24
26
27struct Host
28{
29 Host() = default;
30
31 Host(const std::string &host_arg, const std::string &port_arg)
32 : host(host_arg),
33 port(port_arg)
34 {
35 }
36
37 bool defined() const
38 {
39 return !host.empty();
40 }
41
42 void swap(Host &rhs) noexcept
43 {
44 host.swap(rhs.host);
45 port.swap(rhs.port);
46 }
47
48 void reset()
49 {
50 host.clear();
51 port.clear();
52 }
53
54 std::string to_string() const
55 {
56 std::ostringstream os;
57 if (defined())
58 os << '[' << host << "]:" << port;
59 else
60 os << "UNDEF_HOST";
61 return os.str();
62 }
63
64 std::string host;
65 std::string port;
66};
67
68class List : public std::vector<Host>
69{
70 public:
71 List() = default;
72
73 List(const OptionList &opt,
74 const std::string &directive,
75 const std::string &default_port)
76 {
77 auto hl = opt.get_index_ptr(directive);
78 if (hl)
79 {
80 for (auto &i : *hl)
81 {
82 const Option &o = opt[i];
83 o.touch();
84 add(o.get(1, 256), o.get_default(2, 16, default_port));
85 }
86 }
87 }
88
90 {
91 std::shuffle(begin(), end(), rng());
92 }
93
94 std::string to_string() const
95 {
96 std::ostringstream os;
97 for (auto &h : *this)
98 os << h.to_string() << '\n';
99 return os.str();
100 }
101
102 private:
103 void add(const std::string &host,
104 const std::string &port)
105 {
106 const std::string title = "host list";
109 emplace_back(host, port);
110 }
111};
112
114{
115 public:
117 {
118 reset();
119 }
120
121 void reset()
122 {
123 index = -1;
124 }
125
126 template <typename HOST>
127 bool next(const List &list, HOST &host)
128 {
129 if (list.size() > 0)
130 {
131 if (++index >= list.size())
132 index = 0;
133 const Host &h = list[index];
134 host.host = h.host;
135 host.port = h.port;
136 return true;
137 }
138 else
139 return false;
140 }
141
142 private:
143 int index;
144};
145} // namespace openvpn::HostList
146
147#endif
bool next(const List &list, HOST &host)
Definition hostlist.hpp:127
void add(const std::string &host, const std::string &port)
Definition hostlist.hpp:103
std::string to_string() const
Definition hostlist.hpp:94
List(const OptionList &opt, const std::string &directive, const std::string &default_port)
Definition hostlist.hpp:73
void randomize(RandomAPI &rng)
Definition hostlist.hpp:89
const IndexList * get_index_ptr(const std::string &name) const
Definition options.hpp:1260
std::string get_default(const size_t index, const size_t max_len, const std::string &default_value) const
Definition options.hpp:199
void touch(bool lightly=false) const
Definition options.hpp:378
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:184
Abstract base class for random number generators.
Definition randapi.hpp:39
void validate_host(const std::string &host, const std::string &title)
Definition hostport.hpp:93
void validate_port(const std::string &port, const std::string &title, unsigned int *value=nullptr)
Definition hostport.hpp:34
Implementation of the base classes for random number generators.
Host(const std::string &host_arg, const std::string &port_arg)
Definition hostlist.hpp:31
std::string to_string() const
Definition hostlist.hpp:54
void swap(Host &rhs) noexcept
Definition hostlist.hpp:42
proxy_host_port port
proxy_host_port host
std::ostringstream os