Compare commits

...

2 Commits

Author SHA1 Message Date
marcelb
93d72e16aa Add timeout callculate and return string in obey methods 2023-06-27 21:29:05 +02:00
marcelb
24e2a4e4a9 Fix SSL handshake error, set timeout and SSL prefun calls 2023-06-27 16:31:14 +02:00
7 changed files with 91 additions and 43 deletions

31
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,31 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/client.cpp",
"${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp",
"-o",
"${fileDirname}/client.o",
"-lssl",
"-lcrypto"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

View File

@ -55,7 +55,7 @@ class client {
struct sockaddr_in addr; struct sockaddr_in addr;
SSL* ssl = NULL; SSL* ssl = NULL;
client (const string address, const ushort port, const uint timeout = 500, SSL_CTX* securefds = NULL); client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
~client (); ~client ();
bool tell (const string msg); bool tell (const string msg);
string obey (size_t byte_limit = 1024); string obey (size_t byte_limit = 1024);

View File

@ -53,6 +53,11 @@ server::~server () {
*/ */
secure::secure() { secure::secure() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
fds = SSL_CTX_new(SSLv23_client_method()); fds = SSL_CTX_new(SSLv23_client_method());
if (!fds) { if (!fds) {
throw string("[ERROR] Creating SSL context "); throw string("[ERROR] Creating SSL context ");
@ -71,7 +76,7 @@ secure::secure(const string cert, const string priv) {
SSL_load_error_strings(); SSL_load_error_strings();
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
// Create an SSL context // Create an SSL context
fds = SSL_CTX_new(SSLv23_server_method()); fds = SSL_CTX_new(SSLv23_server_method());
if (!fds) { if (!fds) {
throw string("[ERROR] Creating SSL context "); throw string("[ERROR] Creating SSL context ");
@ -121,8 +126,8 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
} }
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; tv.tv_sec = timeout/1000;
tv.tv_usec = timeout*1000; tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(sock, 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 "); throw string("[ERROR] Unable to set timeout ");
@ -135,12 +140,13 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
} }
SSL_set_fd(ssl, sock); SSL_set_fd(ssl, sock);
}
// Perform the SSL handshake // Perform the SSL handshake
if (SSL_connect(ssl) <= 0) { if (SSL_connect(ssl) <= 0) {
SSL_free(ssl); SSL_free(ssl);
throw string("[ERROR] Performing SSL handshake "); throw string("[ERROR] Performing SSL handshake ");
}
} }
} }
@ -200,7 +206,7 @@ string client::obey (size_t byte_limit) {
read(sock , res, byte_limit); read(sock , res, byte_limit);
} }
return (string) res; return string(res);
} }
/** /**
@ -220,8 +226,8 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
} }
struct timeval tv; struct timeval tv;
tv.tv_sec = 0; // za sad 2 sekunde timeout, harkodirano tv.tv_sec = timeout/1000;
tv.tv_usec = timeout*1000; tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(conn, 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 "); throw string("[ERROR] Unable to set timeout ");
@ -234,6 +240,7 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
throw string("[ERROR] Creating SSL object "); throw string("[ERROR] Creating SSL object ");
} }
SSL_set_fd(ssl, conn); SSL_set_fd(ssl, conn);
// Perform SSL handshake // Perform SSL handshake
if (SSL_accept(ssl) <= 0) { if (SSL_accept(ssl) <= 0) {
SSL_free(ssl); SSL_free(ssl);
@ -306,5 +313,5 @@ string comming::obey (size_t byte_limit) {
read(conn , res, byte_limit); read(conn , res, byte_limit);
} }
return (string) res; return string(res);
} }

View File

@ -8,11 +8,19 @@ int main() {
try { try {
secure crypto; secure crypto;
client myserver("localhost", 5000, 500, crypto.fds); cout << "init cert " << endl;
string sends = "Hello world!";
cout << myserver.tell(sends) << " " << sends.length() << endl; client myserver("127.0.0.1", 5000, 5000, crypto.fds);
cout << myserver.obey(); // client myserver("localhost", 5000);
cout << "init client " << endl;
string sends = "Hello world!";
cout << myserver.tell(sends) << " " << sends.length() << endl;
cout << "wait client " << endl;
cout << myserver.obey();
} }
catch (const string err) { catch (const string err) {

Binary file not shown.

View File

@ -5,35 +5,37 @@
using namespace std; using namespace std;
int main() { int main() {
try{ try{
cout << "init server " << endl; cout << "init server " << endl;
server myserver(5000, 10); server myserver(5000);
cout << "init cert " << endl; cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem"); secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init client " << endl; cout << "init client " << endl;
comming myclient(&myserver, 1000, crypto.fds); comming myclient(&myserver, 100, crypto.fds);
cout << "wait client " << myclient.ipv4 << endl; // comming myclient(&myserver, 100);
cout << "wait client " << myclient.ipv4 << endl;
string fromclient = myclient.obey(); string fromclient = myclient.obey();
cout << "tell client " << fromclient << endl; cout << "tell client " << fromclient << endl;
sleep(5); // usleep(600*1000);
myclient.tell(fromclient); sleep(5);
// myclient.~comming(); myclient.tell(fromclient);
// myclient.~comming();
// while (true) { // while (true) {
// comming myclient(&myserver, 1000); // comming myclient(&myserver, 1000);
// string fromclient = myclient.obey(); // string fromclient = myclient.obey();
// cout << fromclient << " " << myclient.conn << endl; // cout << fromclient << " " << myclient.conn << endl;
// cout << "Poslano: " << myclient.tell(fromclient) << "Veličin: " << fromclient.length() << endl; // cout << "Poslano: " << myclient.tell(fromclient) << "Veličin: " << fromclient.length() << endl;
// // myclient.~comming(); // // myclient.~comming();
// cout << "IPv4 " << myclient.ipv4 << " ipv6 " << myclient.ipv6; // cout << "IPv4 " << myclient.ipv4 << " ipv6 " << myclient.ipv6;
// } // }
// sleep(80); // sleep(80);
} }
catch(const string err) { catch(const string err) {
cout << err << endl; cout << err << endl;
} }
return 0; return 0;
} }

Binary file not shown.