|
|
@@ -10,7 +10,7 @@ |
|
|
|
#include <fcntl.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <exception>
|
|
|
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include "defs.h"
|
|
|
@@ -39,7 +39,38 @@ mutex WriteReadMutex; |
|
|
|
// thread functions to send and receive
|
|
|
|
void Receive();
|
|
|
|
void Send();
|
|
|
|
int Gmaster=0;
|
|
|
|
|
|
|
|
bool handle_error_code(int& len, SSL* SSLHandler, int code, const char* func)
|
|
|
|
{
|
|
|
|
switch( SSL_get_error( SSLHandler, code ) )
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
len+=code;
|
|
|
|
return false;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
cout << "CONNETION CLOSE ON WRITE" << endl;
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
cout << func << " WANT READ" << endl;
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
cout << func << " WANT WRITE" << endl;
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
cout << func << " ESYSCALL" << endl;
|
|
|
|
// exit(1);
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_SSL:
|
|
|
|
cout << func << " ESSL" << endl;
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cout << func << " SOMETHING ELSE" << endl;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Server::Server()
|
|
|
|
: SSLProcess(true)
|
|
|
@@ -80,6 +111,7 @@ void Server::start() |
|
|
|
startListen();
|
|
|
|
|
|
|
|
Acceptor ac(_master, _ctx);
|
|
|
|
Gmaster=_master;
|
|
|
|
_sender =new thread( Send );
|
|
|
|
_reciver =new thread( Receive );
|
|
|
|
_reactor =new thread( ac );
|
|
|
@@ -114,7 +146,11 @@ void Server::doServerSSLInit() |
|
|
|
|
|
|
|
// set weak protocol, so it is easy to debug with wireshark
|
|
|
|
|
|
|
|
SSL_CTX_set_options(_ctx, SSL_OP_NO_TLSv1_2 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1 | SSL_OP_ALL | SSL_OP_SINGLE_DH_USE );
|
|
|
|
SSL_CTX_set_options(_ctx, SSL_OP_NO_TLSv1_2
|
|
|
|
| SSL_OP_NO_TLSv1_1
|
|
|
|
| SSL_OP_NO_TLSv1
|
|
|
|
| SSL_OP_ALL
|
|
|
|
| SSL_OP_SINGLE_DH_USE );
|
|
|
|
}
|
|
|
|
|
|
|
|
void Acceptor::operator()()
|
|
|
@@ -177,6 +213,8 @@ void Acceptor::operator()() |
|
|
|
int new_fd=openTCPSocket();
|
|
|
|
if( new_fd >= 0 )
|
|
|
|
{
|
|
|
|
int flag =1;
|
|
|
|
// setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag, sizeof(int));
|
|
|
|
cout << "New socket with ID : " << new_fd
|
|
|
|
<< " is going to be added to map" << endl;
|
|
|
|
SSL* ssl = openSSLSession(new_fd);
|
|
|
@@ -230,10 +268,10 @@ void Receive() |
|
|
|
cout << SSL_state_string(handler.second) << endl;
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lock(WriteReadMutex);
|
|
|
|
cout << "SSL_read: start" << endl;
|
|
|
|
len_rcv = SSL_read(handler.second, buf, 1024);
|
|
|
|
switch( SSL_get_error(handler.second, len_rcv) )
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
// cout << "SSL_read: stop" << endl;
|
|
|
|
if( !handle_error_code(len_rcv, handler.second, len_rcv, "rcv") )
|
|
|
|
{
|
|
|
|
// dirty thing - if it has \n on the end - remove it
|
|
|
|
if( buf[len_rcv-1] == '\n' )
|
|
|
@@ -251,21 +289,6 @@ void Receive() |
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
{
|
|
|
|
cout << "WANT_SOMETHING WHEN Receive" << endl;
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
{
|
|
|
|
cout << "Closing connection " << handler.first << endl;
|
|
|
|
::close(handler.first);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -287,33 +310,36 @@ void Send() |
|
|
|
for(int i=0; i<SEND_ITERATIONS; ++i)
|
|
|
|
{
|
|
|
|
int len = 0;
|
|
|
|
// wait timer for select
|
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 10;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
lock_guard<mutex> lock(WriteReadMutex);
|
|
|
|
int write_len=SSL_write(handler.second, buf.c_str()+len, buf.size()-len);
|
|
|
|
switch( SSL_get_error(handler.second, write_len) )
|
|
|
|
{
|
|
|
|
case SSL_ERROR_NONE:
|
|
|
|
{
|
|
|
|
len += write_len;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
{
|
|
|
|
cout << "WANT_SOMETHING WHEN Send" << endl;
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default :
|
|
|
|
fd_set fd_write;
|
|
|
|
FD_ZERO(&fd_write);
|
|
|
|
FD_SET(Gmaster, &fd_write);
|
|
|
|
FD_SET(handler.first, &fd_write);
|
|
|
|
|
|
|
|
int maxv=Gmaster;
|
|
|
|
if(Gmaster < handler.first)
|
|
|
|
maxv=handler.first;
|
|
|
|
|
|
|
|
select(maxv+1, NULL, &fd_write, NULL, (struct timeval *)&tv);
|
|
|
|
|
|
|
|
if( FD_ISSET(handler.first, &fd_write) )
|
|
|
|
{
|
|
|
|
cout << "Closing connection " << handler.first << endl;
|
|
|
|
::close(handler.first);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
lock_guard<mutex> lock(WriteReadMutex);
|
|
|
|
// cout << "SSL_write: start" << endl;
|
|
|
|
int write_len=SSL_write(handler.second, buf.c_str()+len, buf.size()-len);
|
|
|
|
// cout << "SSL_write: stop " << endl;
|
|
|
|
handle_error_code(len, handler.second, write_len, "write");
|
|
|
|
|
|
|
|
// for debugging re-neg
|
|
|
|
// cout << "SSL STATE: " << SSL_state_string(handler.second) << endl;
|
|
|
|
}
|
|
|
|
// for debugging re-neg
|
|
|
|
// cout << "SSL STATE: " << SSL_state_string(handler.second) << endl;
|
|
|
|
|
|
|
|
} while( len != static_cast<int>(buf.size()) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|