Alsmost TLS

This commit is contained in:
Brad Arant 2019-09-21 13:07:18 -07:00
parent 43a24b900a
commit d373a2e4a0
8 changed files with 44 additions and 36 deletions

View File

@ -68,7 +68,7 @@ namespace core {
return terminateThreads; return terminateThreads;
} }
bool EPoll::registerSocket(Socket *socket /**< The Socket to register.*/) { bool EPoll::registerSocket(Socket *socket) {
lock.lock(); lock.lock();
std::map<int, Socket *>::iterator temp = sockets.find(socket->getDescriptor()); std::map<int, Socket *>::iterator temp = sockets.find(socket->getDescriptor());
if(temp != sockets.end()) if(temp != sockets.end())
@ -77,7 +77,6 @@ namespace core {
sockets.insert(std::pair<int, Socket *>(socket->getDescriptor(), socket)); sockets.insert(std::pair<int, Socket *>(socket->getDescriptor(), socket));
lock.unlock(); lock.unlock();
socket->enable(true); socket->enable(true);
socket->onRegistered();
return true; return true;
} }
@ -90,7 +89,6 @@ namespace core {
throw coreutils::Exception("Attempt to unregister socket that is not registered."); throw coreutils::Exception("Attempt to unregister socket that is not registered.");
sockets.erase(temp); sockets.erase(temp);
lock.unlock(); lock.unlock();
socket->onUnregistered();
return true; return true;
} }

View File

@ -15,6 +15,7 @@ namespace core {
ePoll.unregisterSocket(this); ePoll.unregisterSocket(this);
close(descriptor); close(descriptor);
free(buffer); free(buffer);
onUnregister();
} }
void Socket::setDescriptor(int descriptor) { void Socket::setDescriptor(int descriptor) {
@ -22,6 +23,9 @@ namespace core {
if(descriptor < 3) if(descriptor < 3)
throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__); throw coreutils::Exception("Descriptor out of range", __FILE__, __LINE__);
this->descriptor = descriptor; this->descriptor = descriptor;
onRegister();
ePoll.registerSocket(this);
onRegistered();
} }
int Socket::getDescriptor() { int Socket::getDescriptor() {
@ -33,13 +37,11 @@ namespace core {
this->length = length; this->length = length;
} }
void Socket::onRegistered() { void Socket::onRegister() {}
onConnected();
} void Socket::onRegistered() {}
void Socket::onUnregistered() { void Socket::onUnregister() {}
}
void Socket::eventReceived(struct epoll_event event) { void Socket::eventReceived(struct epoll_event event) {
@ -112,8 +114,8 @@ namespace core {
} }
} }
void Socket::onConnected() { // void Socket::onConnected() {
} // }
void Socket::writeSocket() { void Socket::writeSocket() {
lock.lock(); lock.lock();

View File

@ -29,8 +29,7 @@ namespace core {
/// receiving the EPOLLOUT event then the buffer is written to the socket output. /// receiving the EPOLLOUT event then the buffer is written to the socket output.
/// ///
class Socket : public std::streambuf, class Socket : public core::Object {
public core::Object {
public: public:
@ -41,6 +40,11 @@ namespace core {
void shutdown(); 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); ///<Set the descriptor for the socket. void setDescriptor(int descriptor); ///<Set the descriptor for the socket.
int getDescriptor(); ///< Get the descriptor for the socket. int getDescriptor(); ///< Get the descriptor for the socket.
@ -76,20 +80,23 @@ namespace core {
void output(std::stringstream &out); void output(std::stringstream &out);
/// ///
/// The onRegistered method is called whenever the socket is registered with /// The onRegister method is called before the socket is registered with
/// ePoll and socket communcation events can be started. /// ePoll so objects extending the Socket definition can initialize the socket
/// before receiving events. Evoked when the
/// descriptor is set using setDescriptor for the socket.
/// ///
virtual void onRegistered(); ///< Called when the socket has finished registering with the epoll processing. virtual void onRegister(); ///< Called when the socket has finished registering with the epoll processing.
virtual void onRegistered();
/// ///
/// The onUnregistered method is called whenever the socket is unregistered with /// The onUnregister method is called whenever the socket is unregistered with
/// ePoll and socket communcation events will be stopped. The default method will /// ePoll and socket communcation events will be stopped. The default method will
/// close the socket and clean up the connection. If this is overridden by an /// close the socket and clean up the connection. If this is overridden by an
/// extended object then the object should call this method to clean the socket up. /// extended object then the object should call this method to clean the socket up.
/// ///
virtual void onUnregistered(); ///< Called when the socket has finished unregistering for the epoll processing. virtual void onUnregister(); ///< Called when the socket has finished unregistering for the epoll processing.
void enable(bool mode); ///< Enable the socket to read or write based upon buffer. void enable(bool mode); ///< Enable the socket to read or write based upon buffer.
@ -107,7 +114,7 @@ namespace core {
/// remote device. /// remote device.
/// ///
virtual void onConnected(); ///< Called when socket is open and ready to communicate. // virtual void onConnected(); ///< Called when socket is open and ready to communicate.
/// ///
/// ///

View File

@ -14,7 +14,6 @@ namespace core {
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno)); throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
if(listen(getDescriptor(), 10) < 0) if(listen(getDescriptor(), 10) < 0)
throw coreutils::Exception("Error on listen to socket"); throw coreutils::Exception("Error on listen to socket");
ePoll.registerSocket(this);
} }
TCPServer::~TCPServer() { TCPServer::~TCPServer() {
@ -42,7 +41,6 @@ namespace core {
// return NULL; // return NULL;
// } // }
// //
ePoll.registerSocket(session);
coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << "."; coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << ".";
return session; return session;
} }

View File

@ -22,7 +22,7 @@ namespace core {
} }
} }
void TCPSession::onConnected() { void TCPSession::onRegister() {
protocol(); protocol();
} }

View File

@ -54,8 +54,8 @@ namespace core {
protected: protected:
void onDataReceived(std::string data) override; virtual void onDataReceived(std::string data) override;
void onConnected() override; virtual void onRegister() override;
/// ///
/// Override the protocol method to manage and control the session communications /// Override the protocol method to manage and control the session communications

View File

@ -17,7 +17,7 @@ namespace core {
} }
void handshake_complete(const SSL *ssl, int where, int ret) { 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) { if(where & SSL_CB_HANDSHAKE_DONE) {
X509 *ssl_client_cert = SSL_get_peer_certificate(ssl); X509 *ssl_client_cert = SSL_get_peer_certificate(ssl);
if(!ssl_client_cert) if(!ssl_client_cert)
@ -31,14 +31,14 @@ namespace core {
coreutils::Log(coreutils::LOG_DEBUG_3) << "No client certificate."; 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; initialized = true;
int ret; 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); fcntl(getDescriptor(), F_SETFL, fcntl(getDescriptor(), F_GETFL, 0) | O_NONBLOCK);
ssl = SSL_new(static_cast<TLSServer &>(server).ctx); ssl = SSL_new(static_cast<TLSServer &>(server).ctx);
@ -52,7 +52,11 @@ namespace core {
if(!SSL_set_generate_session_id(ssl, generate_session_id)) if(!SSL_set_generate_session_id(ssl, generate_session_id))
throw std::string("Error setting session identifier callback."); throw std::string("Error setting session identifier callback.");
}
void TLSSession::onRegistered() {
switch (SSL_get_error(ssl, SSL_accept(ssl))) { switch (SSL_get_error(ssl, SSL_accept(ssl))) {
case SSL_ERROR_SSL: case SSL_ERROR_SSL:
coreutils::Log(coreutils::LOG_DEBUG_3) << "ERROR_SSL on ssl_accept. errno=" << errno; 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) { void TLSSession::receiveData(char *buffer, int bufferLength) {
// if(!initialized)
// init();
int len; int len;
// int error = -1; // int error = -1;
// //

View File

@ -39,6 +39,8 @@ namespace core {
protected: protected:
void receiveData(char *buffer, int bufferLength) override; void receiveData(char *buffer, int bufferLength) override;
void onRegister();
void onRegistered();
private: private:
bool initialized = false; bool initialized = false;