Compare commits

...

14 Commits

  1. 4
      .vscode/settings.json
  2. 4
      .vscode/tasks.json
  3. 13
      lib/ip.hpp
  4. 100
      lib/tcp_socket.hpp
  5. 303
      src/tcp_socket.cpp
  6. 67
      test/client.cpp
  7. BIN
      test/client.exe
  8. BIN
      test/client.o
  9. 1
      test/compile-client.ps1
  10. 1
      test/compile-server.ps1
  11. 83
      test/server.cpp
  12. BIN
      test/server.exe
  13. BIN
      test/server.o

@ -44,6 +44,8 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"thread": "cpp",
"chrono": "cpp"
}
}

@ -6,11 +6,11 @@
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/client.cpp",
"${fileDirname}/server.cpp",
"${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp",
"-o",
"${fileDirname}/client.o",
"${fileDirname}/server.o",
"-lssl",
"-lcrypto"
],

@ -4,9 +4,16 @@
#include <iostream>
#include <string>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
// #include <sstream>
#include <WinSock.h>
#include <ws2tcpip.h>
#endif
using namespace std;

@ -3,16 +3,40 @@
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <chrono>
#include <stdexcept>
#include <openssl/ssl.h>
#include <openssl/err.h>
#define SOCKET_TIMEOUT 1000; // timeout u us
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
#include <WinSock.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#define ushort u_short
#define uint u_int
#endif
#include "ip.hpp"
using namespace std;
using namespace chrono;
class client;
// class secure;
// class server;
/**
* Server klasa za TCP/IP soket
@ -21,12 +45,22 @@ using namespace std;
class server {
public:
int sock;
#if __linux__
int sock;
#elif _WIN32
WSADATA wsa;
SOCKET sock;
#endif
struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
server (const ushort port, const uint limit = 1000);
server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
~server ();
void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&), const uint timeout = 100);
};
/**
@ -51,40 +85,52 @@ class secure {
class client {
public:
int sock;
// zajedničke
#if __linux__
int conn; // mijenja sock
#elif _WIN32
WSADATA wsa;
SOCKET conn; // mijenja sock
#endif
struct sockaddr_in addr;
SSL* ssl = NULL;
uint _timeout = 100; // timeout u ms
// server s klijentima
const server* srv;
// klijent sa serverom
string ipv4;
string ipv6;
// konstruktor za klijente bez servera
client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
// konstruktor za klijente sa serverom
client (const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL);
~client ();
bool tell (const string msg);
string obey (size_t byte_limit = 1024);
bool push (const string msg);
string pull (size_t byte_limit = 1024);
};
/**
* Klasa za inicijalizaciju dolaznih veza
* Definira se na serverskom tipu aplikacija i predstavlja identifikator klijenta
*/
class ConnectionException : public exception {
public:
ConnectionException(const string& message, const string& data, const bool& interrupted = false): message_(message), data_(data), interrupted_(interrupted) {}
class comming {
public:
const server *srv;
struct sockaddr_in addr;
int conn;
string ipv4;
string ipv6;
SSL* ssl = NULL;
virtual const char* what() const noexcept {
return message_.c_str();
}
comming(const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL);
~comming();
bool tell (const string msg);
string obey (size_t byte_limit = 1024);
const string& getData() const {
return data_;
}
const bool& isInterrupted() const {
return interrupted_;
}
private:
string message_;
string data_;
bool interrupted_;
};
#endif

@ -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 queue, 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,20 +24,69 @@ server::server (const ushort port, const uint limit) {
}
int opt=1;
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#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 ");
}
if (listen(sock, limit) < 0) {
if (listen(sock, queue) < 0) {
throw string("[ERROR] It is not possible to set the allowed number of waiting clients ");
}
}
/**
* Metoda za sinkroni rad s klijentima, prima pokazivač na funkciju i timeout;
* Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju;
* Nije moguće proslijediti druge parametre;
*/
void server::sync(void (*handlecli)(client&), const uint timeout) {
do {
client cli(this, timeout, securefds);
handlecli(cli);
} while (true);
}
/**
* Metoda za asinkdorni rad s klijentima, prima limit, pokazivač na funkciju i timeout;
* Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju;
* Nije moguće proslijediti druge parametre;
*/
void server::async(const uint limit, void (*handlecli)(client&), const uint timeout) {
for (uint i=0; i<limit; i++) {
thr.push_back(thread([&](){
client *cli = new client(this, timeout, securefds);
while (true) {
try {
handlecli(*cli);
} catch (const ConnectionException err) {
if (err.isInterrupted()) {
cli->~client();
cli = new client(this, timeout, securefds);
}
}
}
}));
}
for (uint i=0; i<limit; i++) {
thr[i].join();
}
}
/**
* Destruktor varijable tipa server
*/
@ -42,10 +98,21 @@ server::~server () {
throw string("[ERROR] The socket is already closed ");
}
else if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
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
}
}
/**
@ -109,9 +176,16 @@ secure::~secure () {
*/
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
_timeout = timeout;
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 +195,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 ");
}
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
#if __linux__
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = SOCKET_TIMEOUT;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
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 +231,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,22 +239,30 @@ 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);
_timeout = timeout;
if ((conn = accept(srv->sock, (struct sockaddr *)&(srv->addr), (socklen_t*)&len)) < 0) {
throw string("[ERROR] Unable to accept client connection ");
}
struct timeval tv;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
#if __linux__
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = SOCKET_TIMEOUT;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
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);
@ -259,11 +289,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,43 +305,105 @@ comming::~comming() {
throw string("[ERROR] The socket is already closed ");
}
else if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
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;
if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length());
}
else {
sended = write(conn, msg.c_str(), msg.length());
bool client::push(const string msg) {
size_t total_sent = 0;
size_t msg_length = msg.length();
while (total_sent < msg_length) {
size_t sent = 0;
if (ssl) {
sent = SSL_write(ssl, msg.c_str() + total_sent, msg_length - total_sent);
} else {
sent = send(conn, msg.c_str() + total_sent, msg_length - total_sent, 0);
}
if (sent == -1) {
throw string("[ERRNO] (push) - Error code: " + to_string(errno) + " Detail: " + strerror(errno));
}
total_sent += sent;
}
return sended == msg.length();
}
return true;
}
/**
* 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
*
* Funkcija baca izuzetke koji se moraju uhvatiti za pravilno rukovođenje vezom
* Potrebno je i baciti dalje taj izuzetak ukoliko se koriste server async metode
* PRILOG
* ----------------------------------------------------------------
* try {
fromclient = cli.pull();
}
catch(const ConnectionException except) {
if (except.isInterrupted()) {
throw except;
}
else {
cout << "[EXCEPT] " << except.what() << endl;
fromclient = except.getData();
}
}
* -----------------------------------------------------------------
*
*/
string comming::obey (size_t byte_limit) {
string client::pull(size_t byte_limit) {
char res[byte_limit] = {0};
size_t total_received = 0;
auto start = high_resolution_clock::now();
if (ssl) {
SSL_read(ssl, res, byte_limit);
}
else {
read(conn , res, byte_limit);
while (total_received < byte_limit) {
ssize_t received = 0;
if (ssl) {
received = SSL_read(ssl, res + total_received, byte_limit - total_received);
} else {
received = recv(conn, res + total_received, byte_limit - total_received, 0);
}
cout << "Primljeno " << received << endl;
if (received == -1) {
throw ConnectionException(strerror(errno), string(res, total_received));
} else if (received == 0) {
throw ConnectionException("The socket is broken", string(res), true);
}
total_received += received;
auto cycle = high_resolution_clock::now();
if (duration_cast<milliseconds>(cycle - start).count() > _timeout) {
cout << "TIMEOUT" << endl;
throw ConnectionException("Timeout", string(res));
}
}
return string(res);

@ -1,28 +1,79 @@
#include <iostream>
#include <string>
#include <chrono>
#include "../lib/tcp_socket.hpp"
using namespace std;
using namespace chrono;
int main() {
try {
secure crypto;
cout << "init cert " << endl;
// uint n = 10000;
client myserver("127.0.0.1", 5000, 5000, crypto.fds);
// vector<thread> thr;
// for (uint i=0; i<n; i++) {
// thr.push_back(thread([](uint a){
// client myserver("127.0.0.1", 5000, 500);
// string sends = "Hello world " + to_string(a);
// myserver.push(sends);
// cout << myserver.pull() << endl;
// }, i));
// }
// for (uint i=0; i<n; i++) {
// thr[i].join();
// }
// secure crypto;
// cout << "init cert " << endl;
// client myserver("127.0.0.1", 5000, 5000, crypto.fds);
// client myserver("localhost", 8000, 5000, crypto.fds);
// client myserver("localhost", 5000);
cout << "init client " << endl;
// cout << "init client " << endl;
// string sends = "Hello world!";
// cout << myserver.push(sends) << " " << sends.length() << endl;
// cout << "wait client " << endl;
// cout << myserver.pull();
auto t1 = high_resolution_clock::now();
client mycli("127.0.0.1", 5000);
auto t2 = high_resolution_clock::now();
cout << "Connecting : " << duration_cast<microseconds>(t2 - t1).count() << endl;
string sends = "Hello world!";
cout << myserver.tell(sends) << " " << sends.length() << endl;
cout << "wait client " << endl;
cout << myserver.obey();
while (true) {
auto t3 = high_resolution_clock::now();
mycli.push("Helooo");
auto t4 = high_resolution_clock::now();
cout << "Send : " << duration_cast<microseconds>(t4 - t3).count() << endl;
auto t5 = high_resolution_clock::now();
string msg;
try {
msg = mycli.pull();
} catch (const ConnectionException err) {
cout << err.what() << endl;
msg = err.getData();
}
cout << msg << endl;
auto t6 = high_resolution_clock::now();
cout << "Recive : " << duration_cast<microseconds>(t6 - t5).count() << endl;
// break;
}
}
catch (const string err) {
cout << err << endl;
}

Binary file not shown.

Binary file not shown.

@ -0,0 +1 @@
g++ client.cpp ../src/* -o client.exe -lssl -lcrypto -lws2_32

@ -0,0 +1 @@
g++ server.cpp ../src/* -o server.exe -lssl -lcrypto -lws2_32

@ -1,26 +1,45 @@
#include <iostream>
#include <chrono>
#include "../lib/tcp_socket.hpp"
using namespace std;
using namespace chrono;
int main() {
try{
cout << "init server " << endl;
server myserver(5000);
cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init client " << endl;
// cout << "init cert " << endl;
// secure crypto ("../example/cert.pem", "../example/privkey.pem");
// cout << "init server " << endl;
// server myserver(5000, 100, crypto.fds);
// cout << "init cert " << endl;
// secure crypto ("../example/cert.pem", "../example/privkey.pem");
// cout << "init server " << endl;
// server myserver(8000, 100, crypto.fds);
comming myclient(&myserver, 100, crypto.fds);
// comming myclient(&myserver, 100);
cout << "wait client " << myclient.ipv4 << endl;
// cout << "init client " << endl;
string fromclient = myclient.obey();
cout << "tell client " << fromclient << endl;
// // client myclient(&myserver, 100, crypto.fds);
// // comming myclient(&myserver, 100);
// myserver.accept();
// // cout << "wait client " << myclient.ipv4 << endl;
// cout << "wait client " << myserver.cli->ipv4 << endl;
// string fromclient = myserver.cli->pull();
// // string fromclient = myclient.pull();
// cout << "tell client " << fromclient << endl;
// // usleep(600*1000);
// sleep(5);
// myserver.cli->push(fromclient);
// string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull();
// cout << "tell client " << fromclient << endl;
// usleep(600*1000);
sleep(5);
myclient.tell(fromclient);
//sleep(5);
// myserver.cli->push(fromclient);
// myclient.push(fromclient);
// myclient.~comming();
// while (true) {
@ -33,6 +52,46 @@ int main() {
// }
// sleep(80);
cout << "init server " << endl;
server myserver(5000, 100);
cout << "init client " << endl;
myserver.async(8, [](client &cli) {
auto t3 = high_resolution_clock::now();
string fromclient;
try {
fromclient = cli.pull();
}
catch(const ConnectionException except) {
if (except.isInterrupted()) {
throw except;
}
else {
cout << "[EXCEPT] " << except.what() << endl;
fromclient = except.getData();
}
}
auto t4 = high_resolution_clock::now();
cout << "Recive : " << duration_cast<microseconds>(t4 - t3).count() << endl;
cout << "> " << fromclient << endl;
auto t5 = high_resolution_clock::now();
cli.push(fromclient);
auto t6 = high_resolution_clock::now();
cout << "Response : " << duration_cast<microseconds>(t6 - t5).count() << endl;
});
// string teststr = " Idemooo";
// myserver.sync([](client &cli) {
// cout << "Klijent " << cli.ipv4 << endl;
// string fromclient = cli.pull();
// cout << "S klijenta " << fromclient << endl;
// // fromclient += teststr;
// cli.push(fromclient);
// });
}
catch(const string err) {
cout << err << endl;

Binary file not shown.

Binary file not shown.
Loading…
Cancel
Save