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