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

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

@ -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, 500, 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.

@ -5,35 +5,36 @@
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); // sleep(5);
myclient.tell(fromclient); myclient.tell(fromclient);
// myclient.~comming(); // 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.
Loading…
Cancel
Save