|
|
|
@ -4,12 +4,19 @@ |
|
|
|
|
* Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
server::server (const ushort port, const uint limit) { |
|
|
|
|
server::server (const ushort port, const uint limit, SSL_CTX* _securefds) { |
|
|
|
|
securefds = _securefds; |
|
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET; |
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY; |
|
|
|
|
addr.sin_port = htons(port); |
|
|
|
|
|
|
|
|
|
#if _WIN32 |
|
|
|
|
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { |
|
|
|
|
throw string("[ERROR] WSA Startup. Detail: " + to_string(WSAGetLastError())); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
|
if (sock <= 0) {
|
|
|
|
|
throw string("[ERROR] Unable to open TCP socket "); |
|
|
|
@ -17,9 +24,16 @@ server::server (const ushort port, const uint limit) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int opt=1; |
|
|
|
|
#if __linux__ |
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { |
|
|
|
|
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket "); |
|
|
|
|
} |
|
|
|
|
#elif _WIN32 |
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt))) { |
|
|
|
|
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket "); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { |
|
|
|
|
throw string("[ERROR] Unable to bind socket "); |
|
|
|
@ -31,6 +45,10 @@ server::server (const ushort port, const uint limit) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void server::accept(const uint timeout) { |
|
|
|
|
cli = new client(this, timeout, securefds); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destruktor varijable tipa server |
|
|
|
|
*/ |
|
|
|
@ -38,14 +56,28 @@ server::server (const ushort port, const uint limit) { |
|
|
|
|
|
|
|
|
|
server::~server () { |
|
|
|
|
|
|
|
|
|
cli->~client(); |
|
|
|
|
cli = NULL; |
|
|
|
|
|
|
|
|
|
if (sock<=0) { |
|
|
|
|
throw string("[ERROR] The socket is already closed ");
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else if (close(sock) != 0) { |
|
|
|
|
else { |
|
|
|
|
|
|
|
|
|
#if __linux__ |
|
|
|
|
if (close(sock) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to close socket "); |
|
|
|
|
} |
|
|
|
|
#elif _WIN32 |
|
|
|
|
if (closesocket(sock) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to close socket "); |
|
|
|
|
} |
|
|
|
|
WSACleanup(); |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -110,8 +142,14 @@ secure::~secure () { |
|
|
|
|
|
|
|
|
|
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
|
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
|
if (sock < 0) { |
|
|
|
|
#if _WIN32 |
|
|
|
|
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError())); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
conn = socket(AF_INET, SOCK_STREAM, 0); |
|
|
|
|
if (conn < 0) { |
|
|
|
|
throw string("[ERROR] Unable to open TCP socket "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -121,24 +159,31 @@ client::client(const string address, const ushort port, const uint timeout, SSL_ |
|
|
|
|
addr.sin_addr.s_addr = inet_addr(_address.c_str()); |
|
|
|
|
addr.sin_port = htons(port); |
|
|
|
|
|
|
|
|
|
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) { |
|
|
|
|
if (connect(conn, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) { |
|
|
|
|
throw string("Unable to connect to server "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if __linux__ |
|
|
|
|
struct timeval tv; |
|
|
|
|
tv.tv_sec = timeout/1000; |
|
|
|
|
tv.tv_usec = (timeout%1000)*1000; |
|
|
|
|
|
|
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { |
|
|
|
|
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { |
|
|
|
|
throw string("[ERROR] Unable to set timeout "); |
|
|
|
|
} |
|
|
|
|
#elif _WIN32 |
|
|
|
|
DWORD tv = timeout; |
|
|
|
|
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) { |
|
|
|
|
throw string("[ERROR] Unable to set timeout "); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
if (securefds) { |
|
|
|
|
ssl = SSL_new(securefds); |
|
|
|
|
if (!ssl) { |
|
|
|
|
throw string("[ERROR] Creating SSL object "); |
|
|
|
|
} |
|
|
|
|
SSL_set_fd(ssl, sock); |
|
|
|
|
SSL_set_fd(ssl, conn); |
|
|
|
|
|
|
|
|
|
// Perform the SSL handshake
|
|
|
|
|
if (SSL_connect(ssl) <= 0) { |
|
|
|
@ -150,65 +195,6 @@ client::client(const string address, const ushort port, const uint timeout, SSL_ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destruktor varijable tipa client |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
client::~client () { |
|
|
|
|
|
|
|
|
|
if (ssl) { |
|
|
|
|
SSL_shutdown(ssl); |
|
|
|
|
SSL_free(ssl); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (sock <= 0) { |
|
|
|
|
throw string("[ERROR] The socket is already closed ");
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else if (close(sock) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to close socket "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metoda klase client za slanje podataka preko soketa |
|
|
|
|
* Prima string koji će biti poslan |
|
|
|
|
* Vraća logički statu poređenja psolanih karaktera i karaktera u stringu |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool client::tell (const string msg) { |
|
|
|
|
size_t sended = 0; |
|
|
|
|
if (ssl) { |
|
|
|
|
sended = SSL_write(ssl, msg.c_str(), msg.length()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
sended = write(sock, msg.c_str(), msg.length()); |
|
|
|
|
} |
|
|
|
|
return sended == msg.length(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metoda klase client za primanje poruke preko soketa |
|
|
|
|
* Prima dozvoljeni broj karaktera koji će primiti |
|
|
|
|
* Vraća string primljene poruke |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
string client::obey (size_t byte_limit) { |
|
|
|
|
char res[byte_limit] = {0}; |
|
|
|
|
|
|
|
|
|
if (ssl) { |
|
|
|
|
SSL_read(ssl, res, byte_limit); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
read(sock , res, byte_limit); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return string(res); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Konstruktor varijable tipa commint |
|
|
|
|
* Prima pokazivač na inicijaliziranu varijablu tipa, port,
|
|
|
|
@ -217,7 +203,7 @@ string client::obey (size_t byte_limit) { |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
srv = _srv; |
|
|
|
|
socklen_t len = sizeof(struct sockaddr_in); |
|
|
|
|
|
|
|
|
@ -225,6 +211,7 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
throw string("[ERROR] Unable to accept client connection "); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#if __linux__ |
|
|
|
|
struct timeval tv; |
|
|
|
|
tv.tv_sec = timeout/1000; |
|
|
|
|
tv.tv_usec = (timeout%1000)*1000; |
|
|
|
@ -232,6 +219,15 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { |
|
|
|
|
throw string("[ERROR] Unable to set timeout "); |
|
|
|
|
} |
|
|
|
|
#elif _WIN32 |
|
|
|
|
DWORD tv = timeout; |
|
|
|
|
|
|
|
|
|
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) { |
|
|
|
|
throw string("[ERROR] Unable to set timeout "); |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (securefds) { |
|
|
|
@ -259,11 +255,12 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Destruktor varijable tipa comming |
|
|
|
|
* Destruktor varijable tipa client |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
comming::~comming() { |
|
|
|
|
client::~client () { |
|
|
|
|
|
|
|
|
|
if (ssl) { |
|
|
|
|
SSL_shutdown(ssl); |
|
|
|
@ -274,44 +271,57 @@ comming::~comming() { |
|
|
|
|
throw string("[ERROR] The socket is already closed ");
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
else if (close(conn) != 0) { |
|
|
|
|
else { |
|
|
|
|
#if __linux__ |
|
|
|
|
if (close(conn) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to close socket "); |
|
|
|
|
} |
|
|
|
|
#elif _WIN32 |
|
|
|
|
if (closesocket(conn) != 0) { |
|
|
|
|
throw string("[ERROR] Unable to close socket "); |
|
|
|
|
} |
|
|
|
|
//WSACleanup();
|
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metoda klase comming za slanje podataka preko soketa |
|
|
|
|
* Metoda klase client za slanje podataka preko soketa |
|
|
|
|
* Prima string koji će biti poslan |
|
|
|
|
* Vraća logički statu poređenja psolanih karaktera i karaktera u stringu |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
bool comming::tell (const string msg) { |
|
|
|
|
ssize_t sended = 0; |
|
|
|
|
|
|
|
|
|
bool client::push (const string msg) { |
|
|
|
|
size_t sended = 0; |
|
|
|
|
if (ssl) { |
|
|
|
|
sended = SSL_write(ssl, msg.c_str(), msg.length()); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
sended = write(conn, msg.c_str(), msg.length()); |
|
|
|
|
sended = send(conn, msg.c_str(), msg.length(), 0); |
|
|
|
|
} |
|
|
|
|
return sended == msg.length(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metoda klase comming za primanje poruke preko soketa |
|
|
|
|
* Metoda klase client za primanje poruke preko soketa |
|
|
|
|
* Prima dozvoljeni broj karaktera koji će primiti |
|
|
|
|
* Vraća string primljene poruke |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
string comming::obey (size_t byte_limit) { |
|
|
|
|
string client::pull (size_t byte_limit) { |
|
|
|
|
char res[byte_limit] = {0}; |
|
|
|
|
|
|
|
|
|
if (ssl) { |
|
|
|
|
SSL_read(ssl, res, byte_limit); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
read(conn , res, byte_limit); |
|
|
|
|
recv(conn , res, byte_limit, 0); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return string(res); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|