Alsmost TLS
This commit is contained in:
parent
43a24b900a
commit
d373a2e4a0
@ -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<int, Socket *>::iterator temp = sockets.find(socket->getDescriptor());
|
||||
if(temp != sockets.end())
|
||||
@ -77,7 +77,6 @@ namespace core {
|
||||
sockets.insert(std::pair<int, Socket *>(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;
|
||||
}
|
||||
|
||||
|
16
Socket.cpp
16
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::onUnregistered() {
|
||||
void Socket::onRegistered() {}
|
||||
|
||||
}
|
||||
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();
|
||||
|
23
Socket.h
23
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); ///<Set the descriptor for the socket.
|
||||
|
||||
int getDescriptor(); ///< Get the descriptor for the socket.
|
||||
@ -76,20 +80,23 @@ namespace core {
|
||||
void output(std::stringstream &out);
|
||||
|
||||
///
|
||||
/// The onRegistered method is called whenever the socket is registered with
|
||||
/// ePoll and socket communcation events can be started.
|
||||
/// The onRegister method is called before the socket is registered with
|
||||
/// 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
|
||||
/// 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.
|
||||
///
|
||||
|
||||
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.
|
||||
|
||||
@ -107,7 +114,7 @@ namespace core {
|
||||
/// 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.
|
||||
|
||||
///
|
||||
///
|
||||
|
@ -14,7 +14,6 @@ namespace core {
|
||||
throw coreutils::Exception("Error on bind to socket: " + std::to_string(errno));
|
||||
if(listen(getDescriptor(), 10) < 0)
|
||||
throw coreutils::Exception("Error on listen to socket");
|
||||
ePoll.registerSocket(this);
|
||||
}
|
||||
|
||||
TCPServer::~TCPServer() {
|
||||
@ -42,7 +41,6 @@ namespace core {
|
||||
// return NULL;
|
||||
// }
|
||||
//
|
||||
ePoll.registerSocket(session);
|
||||
coreutils::Log(coreutils::LOG_DEBUG_2) << "Session started on socket " << session->getDescriptor() << ".";
|
||||
return session;
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ namespace core {
|
||||
}
|
||||
}
|
||||
|
||||
void TCPSession::onConnected() {
|
||||
void TCPSession::onRegister() {
|
||||
protocol();
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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,10 +31,10 @@ 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() << "...";
|
||||
@ -53,6 +53,10 @@ 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;
|
||||
//
|
||||
|
@ -39,6 +39,8 @@ namespace core {
|
||||
|
||||
protected:
|
||||
void receiveData(char *buffer, int bufferLength) override;
|
||||
void onRegister();
|
||||
void onRegistered();
|
||||
|
||||
private:
|
||||
bool initialized = false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user