From d373a2e4a02b7b96705bb83f4f099a30042bcda0 Mon Sep 17 00:00:00 2001 From: Brad Arant Date: Sat, 21 Sep 2019 13:07:18 -0700 Subject: [PATCH] Alsmost TLS --- EPoll.cpp | 4 +--- Socket.cpp | 18 ++++++++++-------- Socket.h | 25 ++++++++++++++++--------- TCPServer.cpp | 2 -- TCPSession.cpp | 2 +- TCPSession.h | 4 ++-- TLSSession.cpp | 23 ++++++++++++----------- TLSSession.h | 2 ++ 8 files changed, 44 insertions(+), 36 deletions(-) diff --git a/EPoll.cpp b/EPoll.cpp index 1e3a9f2..49c90b9 100644 --- a/EPoll.cpp +++ b/EPoll.cpp @@ -68,7 +68,7 @@ namespace core { return terminateThreads; } - bool EPoll::registerSocket(Socket *socket /**< The Socket to register.*/) { + bool EPoll::registerSocket(Socket *socket) { lock.lock(); std::map::iterator temp = sockets.find(socket->getDescriptor()); if(temp != sockets.end()) @@ -77,7 +77,6 @@ namespace core { sockets.insert(std::pair(socket->getDescriptor(), socket)); lock.unlock(); socket->enable(true); - socket->onRegistered(); return true; } @@ -90,7 +89,6 @@ namespace core { throw coreutils::Exception("Attempt to unregister socket that is not registered."); sockets.erase(temp); lock.unlock(); - socket->onUnregistered(); return true; } diff --git a/Socket.cpp b/Socket.cpp index 51012f0..93e2e6f 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -15,6 +15,7 @@ namespace core { ePoll.unregisterSocket(this); close(descriptor); free(buffer); + onUnregister(); } void Socket::setDescriptor(int descriptor) { @@ -22,6 +23,9 @@ namespace core { if(descriptor < 3) throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__); this->descriptor = descriptor; + onRegister(); + ePoll.registerSocket(this); + onRegistered(); } int Socket::getDescriptor() { @@ -33,13 +37,11 @@ namespace core { this->length = length; } - void Socket::onRegistered() { - onConnected(); - } + void Socket::onRegister() {} + + void Socket::onRegistered() {} - void Socket::onUnregistered() { - - } + void Socket::onUnregister() {} void Socket::eventReceived(struct epoll_event event) { @@ -112,8 +114,8 @@ namespace core { } } - void Socket::onConnected() { - } +// void Socket::onConnected() { +// } void Socket::writeSocket() { lock.lock(); diff --git a/Socket.h b/Socket.h index ad4fa93..e0f54de 100644 --- a/Socket.h +++ b/Socket.h @@ -29,8 +29,7 @@ namespace core { /// receiving the EPOLLOUT event then the buffer is written to the socket output. /// - class Socket : public std::streambuf, - public core::Object { + class Socket : public core::Object { public: @@ -41,6 +40,11 @@ namespace core { void shutdown(); + /// + /// setDescriptor establishes the file descriptor for the socket and registers the socket + /// on the EPoll controller. setDescriptor will invoke the onRegister() event. + /// + void setDescriptor(int descriptor); ///getDescriptor() << "."; return session; } diff --git a/TCPSession.cpp b/TCPSession.cpp index d238c46..7be979f 100644 --- a/TCPSession.cpp +++ b/TCPSession.cpp @@ -22,7 +22,7 @@ namespace core { } } - void TCPSession::onConnected() { + void TCPSession::onRegister() { protocol(); } diff --git a/TCPSession.h b/TCPSession.h index 096bd12..7f31099 100644 --- a/TCPSession.h +++ b/TCPSession.h @@ -54,8 +54,8 @@ namespace core { protected: - void onDataReceived(std::string data) override; - void onConnected() override; + virtual void onDataReceived(std::string data) override; + virtual void onRegister() override; /// /// Override the protocol method to manage and control the session communications diff --git a/TLSSession.cpp b/TLSSession.cpp index aea480f..a3cb654 100644 --- a/TLSSession.cpp +++ b/TLSSession.cpp @@ -17,7 +17,7 @@ namespace core { } void handshake_complete(const SSL *ssl, int where, int ret) { - coreutils::Log(coreutils::LOG_DEBUG_3) << "==>" << SSL_state_string_long(ssl) << "<=="; + coreutils::Log(coreutils::LOG_DEBUG_3) << "==>" << SSL_state_string_long(ssl) << "<==" << ret; if(where & SSL_CB_HANDSHAKE_DONE) { X509 *ssl_client_cert = SSL_get_peer_certificate(ssl); if(!ssl_client_cert) @@ -31,14 +31,14 @@ namespace core { coreutils::Log(coreutils::LOG_DEBUG_3) << "No client certificate."; } - TLSSession::TLSSession(EPoll &ePoll, TCPServer &server) : TCPSession(ePoll, server) { - + TLSSession::TLSSession(EPoll &ePoll, TCPServer &server) : TCPSession(ePoll, server) {} + + void TLSSession::onRegister() { initialized = true; - int ret; - - coreutils::Log(coreutils::LOG_DEBUG_3) << "TLS socket initializing on socket " << getDescriptor() << "..."; - + + coreutils::Log(coreutils::LOG_DEBUG_3) << "TLS socket initializing on socket " << getDescriptor() << "..."; + fcntl(getDescriptor(), F_SETFL, fcntl(getDescriptor(), F_GETFL, 0) | O_NONBLOCK); ssl = SSL_new(static_cast(server).ctx); @@ -52,7 +52,11 @@ namespace core { if(!SSL_set_generate_session_id(ssl, generate_session_id)) throw std::string("Error setting session identifier callback."); - + + } + + void TLSSession::onRegistered() { + switch (SSL_get_error(ssl, SSL_accept(ssl))) { case SSL_ERROR_SSL: coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SSL on ssl_accept. errno=" << errno; @@ -84,9 +88,6 @@ namespace core { void TLSSession::receiveData(char *buffer, int bufferLength) { -// if(!initialized) -// init(); - int len; // int error = -1; // diff --git a/TLSSession.h b/TLSSession.h index 4156242..12da72d 100644 --- a/TLSSession.h +++ b/TLSSession.h @@ -39,6 +39,8 @@ namespace core { protected: void receiveData(char *buffer, int bufferLength) override; + void onRegister(); + void onRegistered(); private: bool initialized = false;