aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/tls/tls_callbacks.h
diff options
context:
space:
mode:
authorMatthias Gierlings <[email protected]>2016-05-16 20:46:50 +0200
committerMatthias Gierlings <[email protected]>2016-06-19 18:28:36 +0200
commit93df95db45fa126725808fbd53aa978b00cf08ad (patch)
tree9c0b81ef8488a7c3142d1f95ac5904470ac3c3a9 /src/lib/tls/tls_callbacks.h
parent89b75a5a36c18a7593aa6bdbb472e301904a66b3 (diff)
Added virtual Callback Interface
- extracted inner class TLS::Channel::Callbacks to stand-alone class TLS::Callbacks. - provided default implementations for TLS::Callbacks members executing calls to std::function members for backward compatibility. - applied changes to cli, tests and TLS::Channel related classes to be compatible with new interface.
Diffstat (limited to 'src/lib/tls/tls_callbacks.h')
-rw-r--r--src/lib/tls/tls_callbacks.h97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/lib/tls/tls_callbacks.h b/src/lib/tls/tls_callbacks.h
new file mode 100644
index 000000000..854054c2b
--- /dev/null
+++ b/src/lib/tls/tls_callbacks.h
@@ -0,0 +1,97 @@
+/*
+* TLS Callbacks
+* (C) 2016 Matthias Gierlings
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#ifndef BOTAN_TLS_CALLBACKS_H__
+#define BOTAN_TLS_CALLBACKS_H__
+
+#include <botan/tls_session.h>
+#include <botan/tls_alert.h>
+namespace Botan {
+
+namespace TLS {
+
+class Handshake_State;
+class Handshake_Message;
+
+/**
+* Virtual Interface for TLS-Channel related callback handling. The default
+* implementations involving std::function are only provided for compatibility
+* purposes. New implementations should override the virtual member methods
+* out_fn(), app_data(), alert(), handshake() and handshake_msg() instead.
+*
+*/
+class BOTAN_DLL Callbacks
+ {
+ public:
+ typedef std::function<void (const byte[], size_t)> output_fn;
+ typedef std::function<void (const byte[], size_t)> data_cb;
+ typedef std::function<void (Alert)> alert_cb;
+ typedef std::function<bool (const Session&)> handshake_cb;
+ typedef std::function<void (const Handshake_Message&)> handshake_msg_cb;
+
+ /**
+ * DEPRECATED: This constructor is only provided for backward
+ * compatibility. New implementations should override the
+ * virtual member methods out_fn(), app_data(), alert(),
+ * handshake() and handshake_msg() and use the default constructor
+ * Callbacks().
+ *
+ * Encapsulates a set of callback functions required by a TLS Channel.
+ * @param output_fn is called with data for the outbound socket
+ *
+ * @param app_data_cb is called when new application data is received
+ *
+ * @param alert_cb is called when a TLS alert is received
+ *
+ * @param handshake_cb is called when a handshake is completed
+ */
+
+ Callbacks(output_fn out, data_cb app_data_cb, alert_cb alert_cb,
+ handshake_cb hs_cb, handshake_msg_cb hs_msg_cb = nullptr)
+ : m_output_function(out), m_app_data_cb(app_data_cb),
+ m_alert_cb(alert_cb), m_hs_cb(hs_cb), m_hs_msg_cb(hs_msg_cb) {}
+
+ virtual ~Callbacks() {}
+
+ virtual void out_fn(const byte data[], size_t size) const
+ {
+ if (m_output_function != nullptr) { m_output_function(data, size); }
+ }
+
+ virtual void app_data(const byte data[], size_t size) const
+ {
+ if (m_app_data_cb != nullptr) { m_app_data_cb(data, size); }
+ }
+
+ virtual void alert(Alert alert) const
+ {
+ if (m_alert_cb != nullptr) { m_alert_cb(alert); }
+ }
+
+ virtual bool handshake(const Session& session) const
+ {
+ if (m_hs_cb != nullptr) { return m_hs_cb(session); }
+ }
+
+ virtual void handshake_msg(const Handshake_Message& hmsg)
+ {
+ if (m_hs_msg_cb != nullptr) { m_hs_msg_cb(hmsg); }
+ }
+
+ private:
+ const output_fn m_output_function;
+ const data_cb m_app_data_cb;
+ const alert_cb m_alert_cb;
+ const handshake_cb m_hs_cb;
+ const handshake_msg_cb m_hs_msg_cb;
+ };
+
+}
+
+}
+
+#endif