2014-07-21 05:22:06 +02:00
|
|
|
// OpenVPN -- An application to securely tunnel IP networks
|
|
|
|
// over a single port, with support for SSL/TLS-based
|
|
|
|
// session authentication and key exchange,
|
|
|
|
// packet encryption, packet authentication, and
|
|
|
|
// packet compression.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2022-09-29 11:41:13 +02:00
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-12-21 21:42:20 +01:00
|
|
|
// it under the terms of the GNU Affero General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-12-21 21:42:20 +01:00
|
|
|
// GNU Affero General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-12-21 21:42:20 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// Basic exception handling. Allow exception classes for specific errors
|
|
|
|
// to be easily defined, and allow exceptions to be thrown with a consise
|
|
|
|
// syntax that allows stringstream concatenation using <<
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
#ifndef OPENVPN_COMMON_EXCEPTION_H
|
|
|
|
#define OPENVPN_COMMON_EXCEPTION_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <exception>
|
2015-09-24 08:26:30 +02:00
|
|
|
#include <utility>
|
2011-09-25 00:51:28 +02:00
|
|
|
|
2014-02-16 08:37:50 +01:00
|
|
|
#include <openvpn/common/stringize.hpp> // for OPENVPN_STRINGIZE
|
2015-06-05 03:22:59 +02:00
|
|
|
#include <openvpn/common/string.hpp>
|
2014-02-16 08:37:50 +01:00
|
|
|
|
2011-12-18 11:50:08 +01:00
|
|
|
#ifdef OPENVPN_DEBUG_EXCEPTION
|
2012-11-23 07:18:43 +01:00
|
|
|
// well-known preprocessor hack to get __FILE__:__LINE__ rendered as a string
|
2011-12-18 11:50:08 +01:00
|
|
|
#define OPENVPN_FILE_LINE "/" __FILE__ ":" OPENVPN_STRINGIZE(__LINE__)
|
|
|
|
#else
|
|
|
|
#define OPENVPN_FILE_LINE
|
|
|
|
#endif
|
2011-10-25 19:32:26 +02:00
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
namespace openvpn {
|
|
|
|
|
2012-11-23 07:18:43 +01:00
|
|
|
// string exception class, where the exception is described by a std::string
|
2012-11-14 17:41:33 +01:00
|
|
|
class Exception : public std::exception
|
2011-09-25 00:51:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2022-11-15 12:56:26 +01:00
|
|
|
explicit Exception(const std::string &err) noexcept
|
|
|
|
: err_(err)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
explicit Exception(std::string &&err) noexcept
|
|
|
|
: err_(std::move(err))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual const char *what() const noexcept
|
2023-01-11 20:43:22 +01:00
|
|
|
{
|
2022-11-15 12:56:26 +01:00
|
|
|
return err_.c_str();
|
2015-09-24 08:26:30 +02:00
|
|
|
}
|
|
|
|
const std::string &err() const noexcept
|
|
|
|
{
|
|
|
|
return err_;
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
2022-11-15 12:56:26 +01:00
|
|
|
virtual ~Exception() noexcept = default;
|
2012-11-14 06:12:48 +01:00
|
|
|
|
|
|
|
void add_label(const std::string &label)
|
|
|
|
{
|
|
|
|
err_ = label + ": " + err_;
|
|
|
|
}
|
|
|
|
|
2012-11-14 17:41:33 +01:00
|
|
|
void remove_label(const std::string &label)
|
2012-11-14 06:12:48 +01:00
|
|
|
{
|
2012-11-14 17:41:33 +01:00
|
|
|
const std::string head = label + ": ";
|
2015-06-05 03:22:59 +02:00
|
|
|
if (string::starts_with(err_, head))
|
2012-11-14 17:41:33 +01:00
|
|
|
err_ = err_.substr(head.length());
|
2012-11-14 06:12:48 +01:00
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
private:
|
|
|
|
std::string err_;
|
|
|
|
};
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// define a simple custom exception class with no extra info
|
|
|
|
#define OPENVPN_SIMPLE_EXCEPTION(C) \
|
2012-11-14 17:41:33 +01:00
|
|
|
class C : public std::exception \
|
|
|
|
{ \
|
2011-09-25 00:51:28 +02:00
|
|
|
public: \
|
2022-11-15 12:56:26 +01:00
|
|
|
virtual const char *what() const noexcept \
|
|
|
|
{ \
|
|
|
|
return #C OPENVPN_FILE_LINE; \
|
2011-09-25 00:51:28 +02:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// define a simple custom exception class with no extra info that inherits from a custom base
|
2011-12-11 09:28:55 +01:00
|
|
|
#define OPENVPN_SIMPLE_EXCEPTION_INHERIT(B, C) \
|
2011-09-25 00:51:28 +02:00
|
|
|
class C : public B \
|
|
|
|
{ \
|
|
|
|
public: \
|
2014-02-28 05:21:54 +01:00
|
|
|
C() : B(#C OPENVPN_FILE_LINE) \
|
2022-11-15 12:56:26 +01:00
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
virtual const char *what() const noexcept \
|
2023-01-11 20:43:22 +01:00
|
|
|
{ \
|
2022-11-15 12:56:26 +01:00
|
|
|
return #C OPENVPN_FILE_LINE; \
|
2011-09-25 00:51:28 +02:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// define a custom exception class that allows extra info
|
|
|
|
#define OPENVPN_EXCEPTION(C) \
|
|
|
|
class C : public openvpn::Exception \
|
|
|
|
{ \
|
|
|
|
public: \
|
2011-12-18 11:50:08 +01:00
|
|
|
C() : openvpn::Exception(#C OPENVPN_FILE_LINE) \
|
2018-07-04 19:28:43 +02:00
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
C(const std::string err) : openvpn::Exception(#C OPENVPN_FILE_LINE ": " + err) \
|
2023-01-11 20:43:22 +01:00
|
|
|
{ \
|
2011-09-25 00:51:28 +02:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 02:52:03 +01:00
|
|
|
// define a custom exception class that allows extra info, but does not emit a tag
|
|
|
|
#define OPENVPN_UNTAGGED_EXCEPTION(C) \
|
|
|
|
class C : public openvpn::Exception \
|
|
|
|
{ \
|
|
|
|
public: \
|
2018-07-04 19:28:43 +02:00
|
|
|
C(const std::string err) : openvpn::Exception(err) \
|
|
|
|
{ \
|
2012-11-12 02:52:03 +01:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// define a custom exception class that allows extra info, and inherits from a custom base
|
2012-10-31 15:46:40 +01:00
|
|
|
#define OPENVPN_EXCEPTION_INHERIT(B, C) \
|
2011-09-25 00:51:28 +02:00
|
|
|
class C : public B \
|
|
|
|
{ \
|
|
|
|
public: \
|
2011-12-18 11:50:08 +01:00
|
|
|
C() : B(#C OPENVPN_FILE_LINE) \
|
2018-07-04 19:28:43 +02:00
|
|
|
{ \
|
|
|
|
} \
|
|
|
|
C(const std::string err) : B(#C OPENVPN_FILE_LINE ": " + err) \
|
2023-01-11 20:43:22 +01:00
|
|
|
{ \
|
2011-09-25 00:51:28 +02:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 02:52:03 +01:00
|
|
|
// define a custom exception class that allows extra info, and inherits from a custom base,
|
|
|
|
// but does not emit a tag
|
|
|
|
#define OPENVPN_UNTAGGED_EXCEPTION_INHERIT(B, C) \
|
|
|
|
class C : public B \
|
|
|
|
{ \
|
|
|
|
public: \
|
2018-07-04 19:28:43 +02:00
|
|
|
C(const std::string err) : B(err) \
|
|
|
|
{ \
|
2012-11-12 02:52:03 +01:00
|
|
|
} \
|
2023-01-11 20:43:22 +01:00
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// throw an Exception with stringstream concatenation allowed
|
|
|
|
#define OPENVPN_THROW_EXCEPTION(stuff) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
std::ostringstream _ovpn_exc; \
|
|
|
|
_ovpn_exc << stuff; \
|
|
|
|
throw openvpn::Exception(_ovpn_exc.str()); \
|
|
|
|
} while (0)
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
// throw an OPENVPN_EXCEPTION class with stringstream concatenation allowed
|
|
|
|
#define OPENVPN_THROW(exc, stuff) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
std::ostringstream _ovpn_exc; \
|
|
|
|
_ovpn_exc << stuff; \
|
|
|
|
throw exc(_ovpn_exc.str()); \
|
|
|
|
} while (0)
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2012-02-17 21:30:08 +01:00
|
|
|
// properly rethrow an exception that might be derived from Exception
|
|
|
|
inline void throw_ref(const std::exception &e)
|
|
|
|
{
|
|
|
|
const Exception *ex = dynamic_cast<const Exception *>(&e);
|
|
|
|
if (ex)
|
|
|
|
throw *ex;
|
|
|
|
else
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
2011-09-25 00:51:28 +02:00
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_COMMON_EXCEPTION_H
|