join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
tls_stream.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CRYPTO_TLS_STREAM_HPP
26#define JOIN_CRYPTO_TLS_STREAM_HPP
27
28// libjoin.
30#include <join/tls_wrapper.hpp>
31
32// C++.
33#include <chrono>
34#include <utility>
35
36namespace join
37{
41 template <class Protocol>
42 class BasicTlsStream : public BasicSocketStream<Protocol>
43 {
44 public:
45 using Endpoint = typename Protocol::Endpoint;
46 using Socket = typename Protocol::Socket;
47 using Transport = typename Protocol::Transport::Socket;
48 using Mode = typename Socket::Mode;
49
57
63 explicit BasicTlsStream (TlsContext ctx, Mode mode = Mode::NonBlocking)
64 : BasicSocketStream<Protocol> (Socket{std::move (ctx), mode})
65 {
66 }
67
73 : BasicSocketStream<Protocol> (std::move (socket))
74 {
75 }
76
83 : BasicSocketStream<Protocol> (Socket{std::move (socket), std::move (ctx)})
84 {
85 }
86
91 BasicTlsStream (const BasicTlsStream& other) = delete;
92
98 BasicTlsStream& operator= (const BasicTlsStream& other) = delete;
99
105 : BasicSocketStream<Protocol> (std::move (other))
106 {
107 }
108
115 {
117
118 return *this;
119 }
120
124 virtual ~BasicTlsStream () = default;
125
129 void handshake ()
130 {
131 if (this->_sockbuf.socket ().handshake () == -1)
132 {
133 if (lastError == Errc::TemporaryError)
134 {
135 if (this->_sockbuf.socket ().waitHandshake (this->_sockbuf.deadline ()))
136 {
137 return;
138 }
139 }
140
141 this->setstate (std::ios_base::failbit);
142 }
143 }
144
148 void shutdown ()
149 {
150 if (this->_sockbuf.socket ().shutdown () == -1)
151 {
152 if (lastError == Errc::TemporaryError)
153 {
154 if (this->_sockbuf.socket ().waitShutdown (this->_sockbuf.deadline ()))
155 {
156 return;
157 }
158 }
159
160 this->setstate (std::ios_base::failbit);
161 }
162 }
163
168 bool encrypted ()
169 {
170 return this->_sockbuf.socket ().encrypted ();
171 }
172 };
173}
174
175#endif
socket stream class.
Definition socket_stream.hpp:350
Socket & socket()
get the nested socket.
Definition socket_stream.hpp:520
SocketStreambuf _sockbuf
associated stream buffer.
Definition socket_stream.hpp:527
BasicSocketStream & operator=(const BasicSocketStream &other)=delete
copy assignment operator.
Socket & socket()
get the nested socket.
Definition socket_stream.hpp:229
TLS stream class.
Definition tls_stream.hpp:43
BasicTlsStream(Socket &&socket)
construct the TLS stream by moving an already wrapped TLS socket in.
Definition tls_stream.hpp:72
BasicTlsStream(Transport &&socket, TlsContext ctx=TlsContext{})
construct the TLS stream by wrapping an already connected transport socket.
Definition tls_stream.hpp:82
void shutdown()
perform the TLS shutdown (send a close_notify alert).
Definition tls_stream.hpp:148
BasicTlsStream(TlsContext ctx, Mode mode=Mode::NonBlocking)
construct the TLS stream instance using the given context.
Definition tls_stream.hpp:63
BasicTlsStream()
default constructor.
Definition tls_stream.hpp:53
bool encrypted()
check if the stream is encrypted.
Definition tls_stream.hpp:168
BasicTlsStream(BasicTlsStream &&other)
move constructor.
Definition tls_stream.hpp:104
typename Protocol::Endpoint Endpoint
Definition tls_stream.hpp:45
BasicTlsStream(const BasicTlsStream &other)=delete
copy constructor.
typename Protocol::Transport::Socket Transport
Definition tls_stream.hpp:47
BasicTlsStream & operator=(const BasicTlsStream &other)=delete
copy assignment operator.
void handshake()
perform the TLS handshake.
Definition tls_stream.hpp:129
typename Socket::Mode Mode
Definition tls_stream.hpp:48
typename Protocol::Socket Socket
Definition tls_stream.hpp:46
virtual ~BasicTlsStream()=default
destroy the TLS stream instance.
TLS/DTLS context.
Definition tls_context.hpp:42
Definition acceptor.hpp:32
Definition error.hpp:144