Skip to content

Commit d0dd76b

Browse files
authored
Add support for a delayed SSL handshake (#66)
1 parent dc45be8 commit d0dd76b

13 files changed

+775
-643
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ find_package(OpenSSL)
8686
if(OpenSSL_FOUND)
8787
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
8888
target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENSSL_LIBRARIES})
89-
set(TRANTOR_SOURCES ${TRANTOR_SOURCES} trantor/net/ssl/SSLConnection.cc)
90-
else()
91-
set(TRANTOR_SOURCES ${TRANTOR_SOURCES}
92-
trantor/net/ssl/SSLConnectionSkipped.cc)
89+
add_definitions(-DUSE_OPENSSL)
9390
endif()
9491

9592
find_path(CARES_INCLUDE_DIR ares.h)

trantor/net/TcpClient.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <trantor/net/TcpClient.h>
1313

1414
#include <trantor/utils/Logger.h>
15-
#include "ssl/SSLConnection.h"
1615
#include "Connector.h"
1716
#include "inner/TcpConnectionImpl.h"
1817
#include <trantor/net/EventLoop.h>
@@ -152,8 +151,13 @@ void TcpClient::newConnection(int sockfd)
152151
std::shared_ptr<TcpConnectionImpl> conn;
153152
if (sslCtxPtr_)
154153
{
155-
conn = std::make_shared<SSLConnection>(
154+
#ifdef USE_OPENSSL
155+
conn = std::make_shared<TcpConnectionImpl>(
156156
loop_, sockfd, localAddr, peerAddr, sslCtxPtr_, false);
157+
#else
158+
LOG_FATAL << "OpenSSL is not found in your system!";
159+
abort();
160+
#endif
157161
}
158162
else
159163
{
@@ -197,8 +201,11 @@ void TcpClient::removeConnection(const TcpConnectionPtr &conn)
197201

198202
void TcpClient::enableSSL()
199203
{
200-
// init OpenSSL
201-
initOpenSSL();
204+
#ifdef USE_OPENSSL
202205
/* Create a new OpenSSL context */
203206
sslCtxPtr_ = newSSLContext();
207+
#else
208+
LOG_FATAL << "OpenSSL is not found in your system!";
209+
abort();
210+
#endif
204211
}

trantor/net/TcpConnection.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
namespace trantor
2626
{
27+
class SSLContext;
28+
std::shared_ptr<SSLContext> newSSLServerContext(const std::string &certPath,
29+
const std::string &keyPath);
2730
class TcpConnection
2831
{
2932
public:
@@ -94,10 +97,11 @@ class TcpConnection
9497
virtual size_t bytesSent() const = 0;
9598
virtual size_t bytesReceived() const = 0;
9699

97-
virtual bool isSSLConnection() const
98-
{
99-
return false;
100-
}
100+
virtual bool isSSLConnection() const = 0;
101+
102+
virtual void startClientEncryption(std::function<void()> callback) = 0;
103+
virtual void startServerEncryption(const std::shared_ptr<SSLContext> &ctx,
104+
std::function<void()> callback) = 0;
101105

102106
private:
103107
std::shared_ptr<void> contextPtr_;

trantor/net/TcpServer.cc

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
#include "Acceptor.h"
1616
#include "inner/TcpConnectionImpl.h"
17-
#include "ssl/SSLConnection.h"
1817
#include <trantor/net/TcpServer.h>
1918
#include <trantor/utils/Logger.h>
2019
#include <functional>
@@ -68,13 +67,17 @@ void TcpServer::newConnection(int sockfd, const InetAddress &peer)
6867
std::shared_ptr<TcpConnectionImpl> newPtr;
6968
if (sslCtxPtr_)
7069
{
71-
newPtr =
72-
std::make_shared<SSLConnection>(ioLoop,
73-
sockfd,
74-
InetAddress(
75-
Socket::getLocalAddr(sockfd)),
76-
peer,
77-
sslCtxPtr_);
70+
#ifdef USE_OPENSSL
71+
newPtr = std::make_shared<TcpConnectionImpl>(
72+
ioLoop,
73+
sockfd,
74+
InetAddress(Socket::getLocalAddr(sockfd)),
75+
peer,
76+
sslCtxPtr_);
77+
#else
78+
LOG_FATAL << "OpenSSL is not found in your system!";
79+
abort();
80+
#endif
7881
}
7982
else
8083
{
@@ -161,9 +164,11 @@ const std::string TcpServer::ipPort() const
161164
void TcpServer::enableSSL(const std::string &certPath,
162165
const std::string &keyPath)
163166
{
164-
// init OpenSSL
165-
initOpenSSL();
167+
#ifdef USE_OPENSSL
166168
/* Create a new OpenSSL context */
167-
sslCtxPtr_ = newSSLContext();
168-
initServerSSLContext(sslCtxPtr_, certPath, keyPath);
169+
sslCtxPtr_ = newSSLServerContext(certPath, keyPath);
170+
#else
171+
LOG_FATAL << "OpenSSL is not found in your system!";
172+
abort();
173+
#endif
169174
}

trantor/net/callbacks.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ using TimerCallback = std::function<void()>;
2828

2929
// the data has been read to (buf, len)
3030
class TcpConnection;
31-
class SSLConnection;
3231
class MsgBuffer;
3332
using TcpConnectionPtr = std::shared_ptr<TcpConnection>;
34-
using SSLConnectionPtr = std::shared_ptr<SSLConnection>;
3533
// tcp server and connection callback
3634
using RecvMessageCallback =
3735
std::function<void(const TcpConnectionPtr &, MsgBuffer *)>;
@@ -41,14 +39,6 @@ using CloseCallback = std::function<void(const TcpConnectionPtr &)>;
4139
using WriteCompleteCallback = std::function<void(const TcpConnectionPtr &)>;
4240
using HighWaterMarkCallback =
4341
std::function<void(const TcpConnectionPtr &, const size_t)>;
44-
// ssl server and connection callback
45-
using SSLRecvMessageCallback =
46-
std::function<void(const SSLConnectionPtr &, MsgBuffer *)>;
47-
using SSLConnectionCallback = std::function<void(const SSLConnectionPtr &)>;
48-
using SSLCloseCallback = std::function<void(const SSLConnectionPtr &)>;
49-
using SSLWriteCompleteCallback = std::function<void(const SSLConnectionPtr &)>;
50-
using SSLHighWaterMarkCallback =
51-
std::function<void(const SSLConnectionPtr &, const size_t)>;
5242

5343
using OperationCompleteCallback = std::function<void(const TrantorError)>;
5444

0 commit comments

Comments
 (0)