Fix SSL handshake error, set timeout and SSL prefun calls

dev v0.3_beta
marcelb 1 year ago
parent 3944512585
commit 24e2a4e4a9
  1. 31
      .vscode/tasks.json
  2. 2
      lib/tcp_socket.hpp
  3. 17
      src/tcp_socket.cpp
  4. 18
      test/client.cpp
  5. BIN
      test/client.o
  6. 53
      test/server.cpp
  7. BIN
      test/server.o

31
.vscode/tasks.json vendored

@ -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"
}

@ -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);

@ -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 ");
@ -71,7 +76,7 @@ secure::secure(const string cert, const string priv) {
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
// Create an SSL context
// Create an SSL context
fds = SSL_CTX_new(SSLv23_server_method());
if (!fds) {
throw string("[ERROR] Creating SSL context ");
@ -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 ");
if (SSL_connect(ssl) <= 0) {
SSL_free(ssl);
throw string("[ERROR] Performing SSL handshake ");
}
}
}
@ -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);

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

Binary file not shown.

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

Binary file not shown.
Loading…
Cancel
Save