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;
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 ();
bool tell (const string msg);
string obey (size_t byte_limit = 1024);

View File

@ -53,6 +53,11 @@ server::~server () {
*/
secure::secure() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
fds = SSL_CTX_new(SSLv23_client_method());
if (!fds) {
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;
tv.tv_sec = 0;
tv.tv_usec = timeout*1000;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
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);
}
// Perform the SSL handshake
if (SSL_connect(ssl) <= 0) {
SSL_free(ssl);
throw string("[ERROR] Performing SSL handshake ");
}
}
}
@ -200,7 +206,7 @@ string client::obey (size_t 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;
tv.tv_sec = 0; // za sad 2 sekunde timeout, harkodirano
tv.tv_usec = timeout*1000;
tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
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 ");
}
SSL_set_fd(ssl, conn);
// Perform SSL handshake
if (SSL_accept(ssl) <= 0) {
SSL_free(ssl);
@ -306,5 +313,5 @@ string comming::obey (size_t byte_limit) {
read(conn , res, byte_limit);
}
return (string) res;
return string(res);
}

View File

@ -9,9 +9,17 @@ int main() {
try {
secure crypto;
client myserver("localhost", 5000, 500, crypto.fds);
cout << "init cert " << endl;
client myserver("127.0.0.1", 5000, 5000, crypto.fds);
// 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();
}

Binary file not shown.

View File

@ -5,18 +5,20 @@
using namespace std;
int main() {
try{
try{
cout << "init server " << endl;
server myserver(5000, 10);
server myserver(5000);
cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init client " << endl;
comming myclient(&myserver, 1000, crypto.fds);
comming myclient(&myserver, 100, crypto.fds);
// comming myclient(&myserver, 100);
cout << "wait client " << myclient.ipv4 << endl;
string fromclient = myclient.obey();
cout << "tell client " << fromclient << endl;
// usleep(600*1000);
sleep(5);
myclient.tell(fromclient);
// myclient.~comming();
@ -31,9 +33,9 @@ try{
// }
// sleep(80);
}
catch(const string err) {
}
catch(const string err) {
cout << err << endl;
}
}
return 0;
}

Binary file not shown.