diff --git a/ConsoleServer.cpp b/ConsoleServer.cpp index d5e81ea..2a49815 100644 --- a/ConsoleServer.cpp +++ b/ConsoleServer.cpp @@ -5,7 +5,7 @@ namespace core { - ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address) : TCPServer(ePoll, address, " ", 10, "Console Server") { + ConsoleServer::ConsoleServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo) : TCPServer(ePoll, address, tlsInfo, " ", 10, "Console Server") { coreutils::Log(this); } diff --git a/ConsoleServer.h b/ConsoleServer.h index 10c7420..55f8e25 100644 --- a/ConsoleServer.h +++ b/ConsoleServer.h @@ -1,10 +1,11 @@ #ifndef __ConsoleServer_h__ -#define __ConsoleServer_h__ +# define __ConsoleServer_h__ -#include "TCPServer.h" -#include "Command.h" -#include "EPoll.h" -#include "LogListener.h" +# include "TCPServer.h" +# include "Command.h" +# include "EPoll.h" +# include "LogListener.h" +# include "TLSInfo.h" namespace core { @@ -23,7 +24,7 @@ namespace core { // // - ConsoleServer(EPoll &ePoll, IPAddress address); + ConsoleServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo); // // diff --git a/Socket.cpp b/Socket.cpp index 6bd006f..6cb32e5 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -70,7 +70,7 @@ namespace core bool Socket::eventReceived(struct epoll_event event, long long eventId) { coreutils::Log(coreutils::LOG_DEBUG_1) << "Event process beginning for socket " << getDescriptor(); if(inHandler) - coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ coreutils::Log(coreutils::LOG_DEBUG_2) << "inHandler was already true."; inHandler = true; if(event.events & EPOLLRDHUP) { coreutils::Log(coreutils::LOG_DEBUG_2) << "EPOLLRDHUP"; diff --git a/TCPServer.cpp b/TCPServer.cpp index 4a78281..a068712 100644 --- a/TCPServer.cpp +++ b/TCPServer.cpp @@ -5,21 +5,21 @@ #include "TCPSession.h" namespace core { - - TCPServer::TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter, int depth, std::string text) - : TCPSocket(ePoll, text), commands(delimiter, depth) { - - setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); - int yes = 1; - setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - - if (bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) - throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); - - if (listen(getDescriptor(), 20) < 0) - throw coreutils::Exception("Error on listen to socket"); - } - + + TCPServer::TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter, int depth, std::string text) + : TCPSocket(ePoll, tlsInfo, text), commands(delimiter, depth) { + + setDescriptor(socket(AF_INET, SOCK_STREAM, 0)); + int yes = 1; + setsockopt(getDescriptor(), SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); + + if (bind(getDescriptor(), address.getPointer(), address.addressLength) < 0) + throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); + + if (listen(getDescriptor(), 20) < 0) + throw coreutils::Exception("Error on listen to socket"); + } + TCPServer::~TCPServer() { coreutils::Log(coreutils::LOG_DEBUG_2) << "Closing server socket " << getDescriptor() << "."; close(getDescriptor()); @@ -31,6 +31,10 @@ namespace core { TCPSession *session = accept(); if (session) sessions.push_back(session); + if(true) { + registerSocket(session->getDescriptor()); + acceptSocket(); + } lock.unlock(); } diff --git a/TCPServer.h b/TCPServer.h index 389ccfb..5017ef6 100644 --- a/TCPServer.h +++ b/TCPServer.h @@ -8,6 +8,7 @@ # include "SubscriptionManager.h" # include "TCPSession.h" # include "TCPSocket.h" +# include "TLSInfo.h" namespace core { @@ -36,7 +37,7 @@ namespace core { /// @param commandName the name of the command used to invoke the status display for this object. /// - TCPServer(EPoll &ePoll, IPAddress address, std::string delimiter = " ", int depth = 10, std::string text = ""); + TCPServer(EPoll &ePoll, IPAddress address, TLSInfo *tlsInfo, std::string delimiter = " ", int depth = 10, std::string text = ""); /// /// The destructor for this object. diff --git a/TCPSocket.cpp b/TCPSocket.cpp index e2e054a..59df2b8 100644 --- a/TCPSocket.cpp +++ b/TCPSocket.cpp @@ -10,7 +10,7 @@ namespace core { TCPSocket::TCPSocket(EPoll &ePoll, std::string text) : Socket(ePoll, text) {} - TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo) {} + TCPSocket::TCPSocket(EPoll &ePoll, TLSInfo *tlsInfo, std::string text) : Socket(ePoll, text), TLS(tlsInfo), tlsInfo(tlsInfo) {} TCPSocket::TCPSocket(EPoll &ePoll, SSL_CTX *ctx, std::string text) : Socket(ePoll, text), TLS(ctx) {} @@ -28,6 +28,11 @@ namespace core { } void TCPSocket::onDataReceived(coreutils::ZString &data) { + + if(tlsInfo) { + + } + if (data.getLength() > 0) { lineBuffer = (char *)realloc(lineBuffer, lineBufferSize + data.getLength()); memcpy(lineBuffer + lineBufferSize, data.getData(), data.getLength()); diff --git a/TCPSocket.h b/TCPSocket.h index c06d120..8f67cb0 100644 --- a/TCPSocket.h +++ b/TCPSocket.h @@ -106,6 +106,7 @@ namespace core { protected: bool term = false; + TLSInfo *tlsInfo; private: char *lineBuffer = NULL; diff --git a/TLS.cpp b/TLS.cpp index 833fa2e..a82d43d 100644 --- a/TLS.cpp +++ b/TLS.cpp @@ -85,7 +85,7 @@ namespace core { if(!SSL_CTX_load_verify_locations(ctx, tlsInfo->cACertificate.c_str(), NULL)) throw coreutils::Exception("Cannot verify locations."); SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(tlsInfo->cACertificate.c_str())); - coreutils::Log(coreutils::LOG_DEBUG_1) << "Server key authenticated."; + coreutils::Log(coreutils::LOG_INFO) << "Server key authenticated."; } } diff --git a/testing/consoleserver b/testing/consoleserver index 32053fc..7aba32e 100755 Binary files a/testing/consoleserver and b/testing/consoleserver differ diff --git a/testing/main.cpp b/testing/main.cpp index 975bffe..e8dd036 100644 --- a/testing/main.cpp +++ b/testing/main.cpp @@ -4,6 +4,7 @@ #include "File.h" #include "Log.h" #include "IPAddress.h" +#include "TLSInfo.h" #include int main(int argc, char **argv) { @@ -16,8 +17,13 @@ int main(int argc, char **argv) { std::string ipAddress = "0.0.0.0"; core::EPoll ePoll; + + core::TLSInfo tlsInfo; + tlsInfo.cACertificate = "certs/cert.pem"; + tlsInfo.certificate = "certs/cert.pem"; + tlsInfo.key = "certs/key.pem"; - core::TCPServer console(ePoll, core::IPAddress(ipAddress, 1027)); + core::TCPServer console(ePoll, core::IPAddress(ipAddress, 1027), &tlsInfo); console.commands.add(ePoll, "threads"); console.commands.add(console, "consoles");