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{
30 {
31 }
32
33 Host(const std::string &host_arg, const std::string &port_arg)
34 : host(host_arg),
35 port(port_arg)
36 {
37 }
38
39 bool defined() const
40 {
41 return !host.empty();
42 }
43
44 void swap(Host &rhs) noexcept
45 {
46 host.swap(rhs.host);
47 port.swap(rhs.port);
48 }
49
50 void reset()
51 {
52 host.clear();
53 port.clear();
54 }
55
56 std::string to_string() const
57 {
58 std::ostringstream os;
59 if (defined())
60 os << '[' << host << "]:" << port;
61 else
62 os << "UNDEF_HOST";
63 return os.str();
64 }
65
66 std::string host;
67 std::string port;
68};
69
70class List : public std::vector<Host>
71{
72 public:
74 {
75 }
76
77 List(const OptionList &opt,
78 const std::string &directive,
79 const std::string &default_port)
80 {
81 auto hl = opt.get_index_ptr(directive);
82 if (hl)
83 {
84 for (auto &i : *hl)
85 {
86 const Option &o = opt[i];
87 o.touch();
88 add(o.get(1, 256), o.get_default(2, 16, default_port));
89 }
90 }
91 }
92
94 {
95 std::shuffle(begin(), end(), rng());
96 }
97
98 std::string to_string() const
99 {
100 std::ostringstream os;
101 for (auto &h : *this)
102 os << h.to_string() << '\n';
103 return os.str();
104 }
105
106 private:
107 void add(const std::string &host,
108 const std::string &port)
109 {
110 const std::string title = "host list";
113 emplace_back(host, port);
114 }
115};
116
118{
119 public:
121 {
122 reset();
123 }
124
125 void reset()
126 {
127 index = -1;
128 }
129
130 template <typename HOST>
131 bool next(const List &list, HOST &host)
132 {
133 if (list.size() > 0)
134 {
135 if (++index >= list.size())
136 index = 0;
137 const Host &h = list[index];
138 host.host = h.host;
139 host.port = h.port;
140 return true;
141 }
142 else
143 return false;
144 }
145
146 private:
147 int index;
148};
149} // namespace openvpn::HostList
150
151#endif
bool next(const List &list, HOST &host)
Definition hostlist.hpp:131
void add(const std::string &host, const std::string &port)
Definition hostlist.hpp:107
std::string to_string() const
Definition hostlist.hpp:98
List(const OptionList &opt, const std::string &directive, const std::string &default_port)
Definition hostlist.hpp:77
void randomize(RandomAPI &rng)
Definition hostlist.hpp:93
const IndexList * get_index_ptr(const std::string &name) const
Definition options.hpp:1276
std::string get_default(const size_t index, const size_t max_len, const std::string &default_value) const
Definition options.hpp:203
void touch(bool lightly=false) const
Definition options.hpp:385
const std::string & get(const size_t index, const size_t max_len) const
Definition options.hpp:187
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:33
std::string to_string() const
Definition hostlist.hpp:56
void swap(Host &rhs) noexcept
Definition hostlist.hpp:44
proxy_host_port port
proxy_host_port host
std::ostringstream os