Integration incoming and outgoing clients into one class

cleaning
Marcel Bandić 1 year ago
parent 93d72e16aa
commit 9e14e361e3
  1. 46
      lib/tcp_socket.hpp
  2. 96
      src/tcp_socket.cpp
  3. 4
      test/client.cpp
  4. BIN
      test/client.o
  5. 6
      test/server.cpp
  6. BIN
      test/server.o

@ -3,6 +3,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
@ -27,6 +28,10 @@ class server {
server (const ushort port, const uint limit = 1000);
~server ();
// dok god živi server žive i klijenti - klijetni moraju biti dio servera
// omogućiti enkripciju i na nivou servera
};
/**
@ -51,40 +56,21 @@ class secure {
class client {
public:
int sock;
// zajedničke
int conn; // mijenja sock
struct sockaddr_in addr;
SSL* ssl = NULL;
client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
~client ();
bool tell (const string msg);
string obey (size_t byte_limit = 1024);
};
/**
* Klasa za inicijalizaciju dolaznih veza
* Definira se na serverskom tipu aplikacija i predstavlja identifikator klijenta
*/
class comming {
public:
const server *srv;
struct sockaddr_in addr;
int conn;
// klijent sa serverom
string ipv4;
string ipv6;
SSL* ssl = NULL;
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);
// 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 push (const string msg);
string pull (size_t byte_limit = 1024);
};
#endif

@ -110,8 +110,8 @@ 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) {
conn = socket(AF_INET, SOCK_STREAM, 0);
if (conn < 0) {
throw string("[ERROR] Unable to open TCP socket ");
}
@ -121,7 +121,7 @@ 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 ");
}
@ -129,7 +129,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
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 ");
}
@ -138,7 +138,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
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 +150,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,8 +158,8 @@ string client::obey (size_t byte_limit) {
*/
comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
srv = _srv;
client::client(const uint timeout, SSL_CTX* securefds) {
// srv = _srv;
socklen_t len = sizeof(struct sockaddr_in);
if ((conn = accept(srv->sock, (struct sockaddr *)&(srv->addr), (socklen_t*)&len)) < 0) {
@ -259,11 +200,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);
@ -277,16 +219,18 @@ comming::~comming() {
else if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
}
/**
* 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());
}
@ -296,15 +240,14 @@ bool comming::tell (const string msg) {
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) {
char res[byte_limit] = {0};
string client::pull (size_t byte_limit) {
char res[byte_limit] = {0};
if (ssl) {
SSL_read(ssl, res, byte_limit);
@ -315,3 +258,6 @@ string comming::obey (size_t byte_limit) {
return string(res);
}

@ -17,10 +17,10 @@ int main() {
string sends = "Hello world!";
cout << myserver.tell(sends) << " " << sends.length() << endl;
cout << myserver.push(sends) << " " << sends.length() << endl;
cout << "wait client " << endl;
cout << myserver.obey();
cout << myserver.pull();
}
catch (const string err) {

Binary file not shown.

@ -12,15 +12,15 @@ int main() {
secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init client " << endl;
comming myclient(&myserver, 100, crypto.fds);
client myclient(&myserver, 100, crypto.fds);
// comming myclient(&myserver, 100);
cout << "wait client " << myclient.ipv4 << endl;
string fromclient = myclient.obey();
string fromclient = myclient.pull();
cout << "tell client " << fromclient << endl;
// usleep(600*1000);
sleep(5);
myclient.tell(fromclient);
myclient.push(fromclient);
// myclient.~comming();
// while (true) {

Binary file not shown.
Loading…
Cancel
Save