OpenVPN 3 Core Library
Loading...
Searching...
No Matches
environ.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// Environmental variables
13
14#ifndef OPENVPN_COMMON_ENVIRON_H
15#define OPENVPN_COMMON_ENVIRON_H
16
17#include <string>
18#include <cstring>
19#include <vector>
20#include <utility>
21
23
24extern char **environ;
25
26namespace openvpn {
27
28class Environ : public std::vector<std::string>
29{
30 public:
31 static std::string find_static(const std::string &name)
32 {
33 for (char **e = ::environ; *e != NULL; ++e)
34 {
35 const char *eq = ::strchr(*e, '=');
36 if (eq && eq > *e)
37 {
38 const size_t namelen = eq - *e;
39 if (name.length() == namelen && ::strncmp(name.c_str(), *e, namelen) == 0)
40 return std::string(eq + 1);
41 }
42 }
43 return "";
44 }
45
47 {
48 reserve(64);
49 for (char **e = ::environ; *e != NULL; ++e)
50 emplace_back(*e);
51 }
52
53 std::string to_string() const
54 {
55 std::string ret;
56 ret.reserve(512);
57 for (const auto &s : *this)
58 {
59 ret += s;
60 ret += '\n';
61 }
62 return ret;
63 }
64
65 int find_index(const std::string &name) const
66 {
67 for (size_type i = 0; i < size(); ++i)
68 {
69 const std::string &s = (*this)[i];
70 const size_t pos = s.find_first_of('=');
71 if (pos != std::string::npos)
72 {
73 if (name == s.substr(0, pos))
74 return static_cast<int>(i); // TODO: [OVPN3-928] Evaluate the safety of casting this down
75 }
76 else
77 {
78 if (name == s)
79 return static_cast<int>(i); // Same as above
80 }
81 }
82 return -1;
83 }
84
85 std::string find(const std::string &name) const
86 {
87 const int i = find_index(name);
88 if (i >= 0)
89 return value(i);
90 else
91 return "";
92 }
93
94 std::string value(const size_t idx) const
95 {
96 const std::string &s = (*this)[idx];
97 const size_t pos = s.find_first_of('=');
98 if (pos != std::string::npos)
99 return s.substr(pos + 1);
100 else
101 return "";
102 }
103
104 void assign(const std::string &name, const std::string &value)
105 {
106 std::string nv = name + '=' + value;
107 const int i = find_index(name);
108 if (i >= 0)
109 (*this)[i] = std::move(nv);
110 else
111 push_back(std::move(nv));
112 }
113};
114
115} // namespace openvpn
116#endif
std::string value(const size_t idx) const
Definition environ.hpp:94
int find_index(const std::string &name) const
Definition environ.hpp:65
static std::string find_static(const std::string &name)
Definition environ.hpp:31
std::string to_string() const
Definition environ.hpp:53
std::string find(const std::string &name) const
Definition environ.hpp:85
void load_from_environ()
Definition environ.hpp:46
void assign(const std::string &name, const std::string &value)
Definition environ.hpp:104
char ** environ
server addresses push_back({address, port})
std::string ret