OpenVPN 3 Core Library
Loading...
Searching...
No Matches
clihalt.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_CLIENT_CLIHALT_H
13#define OPENVPN_CLIENT_CLIHALT_H
14
15#include <string>
16#include <sstream>
17#include <vector>
18
23
24// Process halt/restart messages from server:
25// HALT,<client_reason> -> disconnect
26// RESTART,<client_reason> -> restart with reason, don't preserve session ID
27// RESTART,[P]:<client_reason> -> restart with reason, do preserve session ID
28
29namespace openvpn {
31{
32 typedef std::vector<std::string> StringList;
33
34 public:
35 OPENVPN_SIMPLE_EXCEPTION(client_halt_error);
36
37 ClientHalt(const std::string &msg, const bool unicode_filter)
38 {
39 // get operator (halt or restart)
40 StringList sl;
41 parse_msg(sl, msg);
42 if (is_halt(sl))
43 ;
44 else if (is_restart(sl))
45 restart_ = true;
46 else
47 throw client_halt_error();
48
49 // get flags and reason
50 if (sl.size() >= 2)
51 {
52 size_t reason_pos = 0;
53 if (restart_ && string::starts_with(sl[1], "[P]:"))
54 {
55 psid_ = true;
56 reason_pos = 4;
57 }
58 reason_ = sl[1].substr(reason_pos);
59 if (unicode_filter)
61 }
62 }
63
64 static bool match(const std::string &msg)
65 {
66 StringList sl;
67 parse_msg(sl, msg);
68 return is_halt(sl) || is_restart(sl);
69 }
70
71 // returns true for restart, false for halt
72 bool restart() const
73 {
74 return restart_;
75 }
76
77 // returns true if session ID should be preserved
78 bool psid() const
79 {
80 return psid_;
81 }
82
83 // returns user-visible reason string
84 const std::string &reason() const
85 {
86 return reason_;
87 }
88
89 std::string render() const
90 {
91 std::ostringstream os;
92 os << (restart_ ? "RESTART" : "HALT") << " psid=" << psid_ << " reason='" << reason_ << '\'';
93 return os.str();
94 }
95
96 private:
97 static void parse_msg(StringList &sl, const std::string &msg)
98 {
99 sl.reserve(2);
100 Split::by_char_void<StringList, NullLex, Split::NullLimit>(sl, msg, ',', 0, 1);
101 }
102
103 static bool is_halt(const StringList &sl)
104 {
105 return sl.size() >= 1 && sl[0] == "HALT";
106 }
107
108 static bool is_restart(const StringList &sl)
109 {
110 return sl.size() >= 1 && sl[0] == "RESTART";
111 }
112
113 bool restart_ = false;
114 bool psid_ = false;
115 std::string reason_;
116};
117} // namespace openvpn
118
119#endif
bool restart() const
Definition clihalt.hpp:72
std::string reason_
Definition clihalt.hpp:115
std::string render() const
Definition clihalt.hpp:89
static bool match(const std::string &msg)
Definition clihalt.hpp:64
const std::string & reason() const
Definition clihalt.hpp:84
static bool is_halt(const StringList &sl)
Definition clihalt.hpp:103
static bool is_restart(const StringList &sl)
Definition clihalt.hpp:108
ClientHalt(const std::string &msg, const bool unicode_filter)
Definition clihalt.hpp:37
OPENVPN_SIMPLE_EXCEPTION(client_halt_error)
static void parse_msg(StringList &sl, const std::string &msg)
Definition clihalt.hpp:97
bool psid() const
Definition clihalt.hpp:78
std::vector< std::string > StringList
Definition clihalt.hpp:32
STRING utf8_printable(const STRING &str, size_t max_len_flags)
Definition unicode.hpp:129
bool starts_with(const STRING &str, const std::string &prefix)
Definition string.hpp:79
std::ostringstream os
#define msg(flags,...)