Compare commits
No commits in common. "v0.4_win+clean" and "dev" have entirely different histories.
v0.4_win+c
...
dev
@ -3,30 +3,17 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <string.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#if __linux__
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <unistd.h>
|
||||
#elif _WIN32
|
||||
// #include <sstream>
|
||||
#include <WinSock.h>
|
||||
#include <ws2tcpip.h>
|
||||
#endif
|
||||
|
||||
#include "ip.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class client;
|
||||
// class secure;
|
||||
// class server;
|
||||
|
||||
|
||||
/**
|
||||
* Server klasa za TCP/IP soket
|
||||
* Instanca se incijalizira kada pokrećemo server
|
||||
@ -36,15 +23,10 @@ class server {
|
||||
public:
|
||||
int sock;
|
||||
struct sockaddr_in addr;
|
||||
SSL_CTX* securefds = NULL;
|
||||
|
||||
server (const ushort port, const uint limit = 1000, SSL_CTX* _securefds = NULL);
|
||||
server (const ushort port, const uint limit = 1000);
|
||||
~server ();
|
||||
|
||||
// one klijent
|
||||
client* cli;
|
||||
void accept(const uint timeout = 100);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@ -69,23 +51,40 @@ class secure {
|
||||
|
||||
class client {
|
||||
public:
|
||||
// zajedničke
|
||||
int conn; // mijenja sock
|
||||
int sock;
|
||||
struct sockaddr_in addr;
|
||||
SSL* ssl = NULL;
|
||||
// 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 push (const string msg);
|
||||
string pull (size_t byte_limit = 1024);
|
||||
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;
|
||||
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);
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
@ -4,8 +4,7 @@
|
||||
* Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju
|
||||
*/
|
||||
|
||||
server::server (const ushort port, const uint limit, SSL_CTX* _securefds) {
|
||||
securefds = _securefds;
|
||||
server::server (const ushort port, const uint limit) {
|
||||
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = INADDR_ANY;
|
||||
@ -32,10 +31,6 @@ server::server (const ushort port, const uint limit, SSL_CTX* _securefds) {
|
||||
|
||||
}
|
||||
|
||||
void server::accept(const uint timeout) {
|
||||
cli = new client(this, timeout, securefds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destruktor varijable tipa server
|
||||
*/
|
||||
@ -43,9 +38,6 @@ void server::accept(const uint timeout) {
|
||||
|
||||
server::~server () {
|
||||
|
||||
cli->~client();
|
||||
cli = NULL;
|
||||
|
||||
if (sock<=0) {
|
||||
throw string("[ERROR] The socket is already closed ");
|
||||
}
|
||||
@ -118,17 +110,8 @@ secure::~secure () {
|
||||
|
||||
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
|
||||
|
||||
#if _WIN32
|
||||
WSADATA wsa;
|
||||
SOCKET s;
|
||||
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
|
||||
//printf("Failed. Error Code : %d",WSAGetLastError());
|
||||
throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
|
||||
}
|
||||
#endif
|
||||
|
||||
conn = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (conn < 0) {
|
||||
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
throw string("[ERROR] Unable to open TCP socket ");
|
||||
}
|
||||
|
||||
@ -138,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(conn, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
|
||||
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
|
||||
throw string("Unable to connect to server ");
|
||||
}
|
||||
|
||||
@ -146,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(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
|
||||
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
|
||||
throw string("[ERROR] Unable to set timeout ");
|
||||
}
|
||||
|
||||
@ -155,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, conn);
|
||||
SSL_set_fd(ssl, sock);
|
||||
|
||||
// Perform the SSL handshake
|
||||
if (SSL_connect(ssl) <= 0) {
|
||||
@ -167,6 +150,65 @@ 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,
|
||||
@ -175,7 +217,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
|
||||
*/
|
||||
|
||||
|
||||
client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
|
||||
comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
|
||||
srv = _srv;
|
||||
socklen_t len = sizeof(struct sockaddr_in);
|
||||
|
||||
@ -217,16 +259,11 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Destruktor varijable tipa client
|
||||
* Destruktor varijable tipa comming
|
||||
*/
|
||||
|
||||
client::~client () {
|
||||
|
||||
#if _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
comming::~comming() {
|
||||
|
||||
if (ssl) {
|
||||
SSL_shutdown(ssl);
|
||||
@ -240,18 +277,16 @@ client::~client () {
|
||||
else if (close(conn) != 0) {
|
||||
throw string("[ERROR] Unable to close socket ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Metoda klase client za slanje podataka preko soketa
|
||||
* Metoda klase comming 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::push (const string msg) {
|
||||
size_t sended = 0;
|
||||
bool comming::tell (const string msg) {
|
||||
ssize_t sended = 0;
|
||||
if (ssl) {
|
||||
sended = SSL_write(ssl, msg.c_str(), msg.length());
|
||||
}
|
||||
@ -261,14 +296,15 @@ bool client::push (const string msg) {
|
||||
return sended == msg.length();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Metoda klase client za primanje poruke preko soketa
|
||||
* Metoda klase comming za primanje poruke preko soketa
|
||||
* Prima dozvoljeni broj karaktera koji će primiti
|
||||
* Vraća string primljene poruke
|
||||
*/
|
||||
|
||||
string client::pull (size_t byte_limit) {
|
||||
char res[byte_limit] = {0};
|
||||
string comming::obey (size_t byte_limit) {
|
||||
char res[byte_limit] = {0};
|
||||
|
||||
if (ssl) {
|
||||
SSL_read(ssl, res, byte_limit);
|
||||
@ -279,6 +315,3 @@ string client::pull (size_t byte_limit) {
|
||||
|
||||
return string(res);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -17,10 +17,10 @@ int main() {
|
||||
|
||||
|
||||
string sends = "Hello world!";
|
||||
cout << myserver.push(sends) << " " << sends.length() << endl;
|
||||
cout << myserver.tell(sends) << " " << sends.length() << endl;
|
||||
cout << "wait client " << endl;
|
||||
|
||||
cout << myserver.pull();
|
||||
cout << myserver.obey();
|
||||
|
||||
}
|
||||
catch (const string err) {
|
||||
|
BIN
test/client.o
BIN
test/client.o
Binary file not shown.
@ -6,26 +6,21 @@ using namespace std;
|
||||
|
||||
int main() {
|
||||
try{
|
||||
cout << "init server " << endl;
|
||||
server myserver(5000);
|
||||
cout << "init cert " << endl;
|
||||
secure crypto ("../example/cert.pem", "../example/privkey.pem");
|
||||
cout << "init server " << endl;
|
||||
server myserver(5000, 100, crypto.fds);
|
||||
|
||||
cout << "init client " << endl;
|
||||
|
||||
// client myclient(&myserver, 100, crypto.fds);
|
||||
comming myclient(&myserver, 100, crypto.fds);
|
||||
// comming myclient(&myserver, 100);
|
||||
myserver.accept();
|
||||
// cout << "wait client " << myclient.ipv4 << endl;
|
||||
cout << "wait client " << myserver.cli->ipv4 << endl;
|
||||
cout << "wait client " << myclient.ipv4 << endl;
|
||||
|
||||
string fromclient = myserver.cli->pull();
|
||||
// string fromclient = myclient.pull();
|
||||
string fromclient = myclient.obey();
|
||||
cout << "tell client " << fromclient << endl;
|
||||
// usleep(600*1000);
|
||||
sleep(5);
|
||||
myserver.cli->push(fromclient);
|
||||
// myclient.push(fromclient);
|
||||
myclient.tell(fromclient);
|
||||
// myclient.~comming();
|
||||
|
||||
// while (true) {
|
||||
|
BIN
test/server.o
BIN
test/server.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user