actor-framework
session.hpp
1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
2 // the main distribution directory for license terms and copyright or visit
3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE.
4 
5 #pragma once
6 
7 #include "caf/config.hpp"
8 
9 #include <memory>
10 
11 CAF_PUSH_WARNINGS
12 #include <openssl/ssl.h>
13 CAF_POP_WARNINGS
14 
15 #include "caf/io/network/default_multiplexer.hpp"
16 #include "caf/io/network/native_socket.hpp"
17 
18 #include "caf/actor_system.hpp"
19 #include "caf/detail/openssl_export.hpp"
20 
21 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
22 # define CAF_SSL_HAS_NON_VERSIONED_TLS_FUN
23 #endif
24 
25 #if defined(SSL_CTX_set_ecdh_auto)
26 # define CAF_SSL_HAS_ECDH_AUTO
27 #endif
28 
29 namespace caf::openssl {
30 
31 using native_socket = io::network::native_socket;
32 
34 
35 class CAF_OPENSSL_EXPORT session {
36 public:
37  session(actor_system& sys);
38  ~session();
39 
40  bool init();
41  rw_state read_some(size_t& result, native_socket fd, void* buf, size_t len);
42  rw_state write_some(size_t& result, native_socket fd, const void* buf,
43  size_t len);
44  bool try_connect(native_socket fd);
45  bool try_accept(native_socket fd);
46 
47  bool must_read_more(native_socket, size_t threshold);
48 
49  const char* openssl_passphrase();
50 
51 private:
52  rw_state do_some(int (*f)(SSL*, void*, int), size_t& result, void* buf,
53  size_t len, const char* debug_name);
54  SSL_CTX* create_ssl_context();
55  std::string get_ssl_error();
56  bool handle_ssl_result(int ret);
57 
58  actor_system& sys_;
59  SSL_CTX* ctx_;
60  SSL* ssl_;
61  std::string openssl_passphrase_;
62  bool connecting_;
63  bool accepting_;
64 };
65 
67 using session_ptr = std::unique_ptr<session>;
68 
70 CAF_OPENSSL_EXPORT session_ptr make_session(actor_system& sys, native_socket fd,
71  bool from_accepted_socket);
72 
73 } // namespace caf::openssl
rw_state
Denotes the returned state of read and write operations on sockets.
Definition: rw_state.hpp:10
std::unique_ptr< session > session_ptr
Definition: session.hpp:67
Wraps the result of a message handler to represent either a value (wrapped into a message)...
Definition: fwd.hpp:64
Definition: session.hpp:35
Actor environment including scheduler, registry, and optional components such as a middleman...
Definition: actor_system.hpp:87
Definition: fwd.hpp:346