diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..989190b --- /dev/null +++ b/.vscode/tasks.json @@ -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" +} \ No newline at end of file diff --git a/lib/tcp_socket.hpp b/lib/tcp_socket.hpp index c732f4b..d19a52f 100644 --- a/lib/tcp_socket.hpp +++ b/lib/tcp_socket.hpp @@ -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); diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 2734743..2f2dad4 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -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); diff --git a/test/client.cpp b/test/client.cpp index 6584194..a026c56 100644 --- a/test/client.cpp +++ b/test/client.cpp @@ -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) { diff --git a/test/client.o b/test/client.o index 5b42f43..788827d 100755 Binary files a/test/client.o and b/test/client.o differ diff --git a/test/server.cpp b/test/server.cpp index 32582e2..1a0e44f 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -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; } \ No newline at end of file diff --git a/test/server.o b/test/server.o index 3915198..b4ab8be 100755 Binary files a/test/server.o and b/test/server.o differ