OpenVPN 3 Core Library
Loading...
Searching...
No Matches
servpush.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_OPTIONS_SERVPUSH_H
13#define OPENVPN_OPTIONS_SERVPUSH_H
14
15#include <string>
16#include <sstream>
17#include <ostream>
18#include <vector>
19#include <utility> // for std::move
20
24
25#ifdef HAVE_JSON
27#endif
28
29namespace openvpn {
30class ServerPushList : public std::vector<std::string>
31{
32 public:
33 void parse(const std::string &opt_name, const OptionList &opt)
34 {
35 const auto *push = opt.get_index_ptr(opt_name);
36 if (push)
37 {
38 reserve(size() + push->size());
39 for (auto &i : *push)
40 {
41 const Option &o = opt[i];
42 o.touch();
43 push_back(o.get(1, 512));
44 }
45 }
46 }
47
48#ifdef HAVE_JSON
49 // Parse JSON representation of a push list.
50 // Each push list array element can be one of:
51 // 1. simple JSON string,
52 // 2. dictionary containing an "item" string, or
53 // 3. dictionary containing an "item" array of strings.
54 void parse(const std::string &title, const Json::Value &push_list) // push_list is JSON array
55 {
56 reserve(16); // arbitrary, just a guess
57 const auto &ja = json::cast_array(push_list, false, title).asArray();
58 for (size_t i = 0; i < ja.size(); ++i)
59 {
60 const Json::Value &jv = ja[i];
61 if (jv.isString())
62 push_back(jv.asStringRef());
63 else if (jv.isObject())
64 {
65 const Json::Value &ji = jv["item"];
66 if (ji.isString())
67 push_back(ji.asStringRef());
68 else if (ji.isArray())
69 {
70 const auto &ia = ji.asArray();
71 for (size_t j = 0; j < ia.size(); ++j)
72 {
73 const Json::Value &iv = ia[j];
74 if (iv.isString())
75 push_back(iv.asStringRef());
76 else
77 throw json::json_parse(json::fmt_name(i, title) + " object contains 'item' array that includes non-string element at index=" + std::to_string(j));
78 }
79 }
80 else
81 throw json::json_parse(json::fmt_name(i, title) + " object must contain 'item' string or array");
82 }
83 else
84 throw json::json_parse(json::fmt_name(i, title) + " must be of type string or object");
85 }
86 }
87#endif
88
89 void extend(const std::vector<std::string> &other)
90 {
91 reserve(size() + other.size());
92 for (auto &e : other)
93 push_back(e);
94 }
95
96 // do a roundtrip to csv and back to OptionList
98 {
99 std::ostringstream os;
100 output_csv(os);
101 return OptionList::parse_from_csv_static(os.str(), nullptr);
102 }
103
104 void output_csv(std::ostream &os) const
105 {
106 for (auto &e : *this)
107 {
108 os << ',';
109 output_arg(e, os);
110 }
111 }
112
113 static void output_arg(const std::string &e, std::ostream &os)
114 {
115 const bool must_quote = (e.find_first_of(',') != std::string::npos);
116 Option::escape_string(os, e, must_quote);
117 }
118};
119} // namespace openvpn
120
121#endif
const IndexList * get_index_ptr(const std::string &name) const
Definition options.hpp:1276
static OptionList parse_from_csv_static(const std::string &str, Limits *lim)
Definition options.hpp:844
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
static void escape_string(std::ostream &out, const std::string &term, const bool must_quote)
Definition options.hpp:285
void extend(const std::vector< std::string > &other)
Definition servpush.hpp:89
void parse(const std::string &opt_name, const OptionList &opt)
Definition servpush.hpp:33
static void output_arg(const std::string &e, std::ostream &os)
Definition servpush.hpp:113
OptionList to_option_list() const
Definition servpush.hpp:97
void output_csv(std::ostream &os) const
Definition servpush.hpp:104
void parse(const std::string &title, const Json::Value &push_list)
Definition servpush.hpp:54
std::string fmt_name(const NAME &name, const TITLE &title)
const Json::Value & cast_array(const Json::Value &value, const bool optional, const TITLE &title)
server addresses push_back({address, port})
std::ostringstream os