diff --git a/CPPTools/Net.cpp b/CPPTools/Net.cpp index 04a9d75..5aa1029 100644 --- a/CPPTools/Net.cpp +++ b/CPPTools/Net.cpp @@ -44,8 +44,8 @@ namespace IO { - void NetClient::sharedSetup() { - if (preferEncrypted != CryptoLevel::None) keys = Crypto::RSA::rsa_gen_keys(); + void NetClient::sharedSetup(bool setupKeys) { + if (setupKeys && (preferEncrypted != CryptoLevel::None)) keys = Crypto::RSA::rsa_gen_keys(); packets = new std::vector(); sparse = new std::vector(); outPacketBuf = new std::vector(); @@ -58,8 +58,9 @@ namespace IO { if (send(_socket, &cryptoPref, 1, 0) == SOCKET_ERROR) throw new std::exception(); // Cannot establish connection :( if (!noThread) listener = std::thread([](NetClient& cli) { while (cli._open) { cli.update(); Sleep(25); } }, std::ref(*this)); // Setup separate thread for reading new data } - NetClient::NetClient(char* ipAddr, char* port, CryptoLevel preferEncrypted) : - commTime(time(nullptr)), preferEncrypted(preferEncrypted), startNegotiate(false) + NetClient::NetClient(char* ipAddr, char* port, CryptoLevel preferEncrypted) : NetClient(ipAddr, port, preferEncrypted, true) {} + NetClient::NetClient(char* ipAddr, char* port, CryptoLevel preferEncrypted, bool setupKeys) : + preferEncrypted(preferEncrypted), startNegotiate(false) { _socket = INVALID_SOCKET; this->noThread = false; @@ -102,15 +103,17 @@ namespace IO { if (_socket == INVALID_SOCKET) throw new std::exception(); - sharedSetup(); + sharedSetup(setupKeys); } + NetClient::NetClient(char* ipAddr, char* port, Crypto::RSA::KeyData& keys, CryptoLevel level) : NetClient(ipAddr, port, level, false) { this->keys = keys; } + NetClient::NetClient(SOCKET wrap, bool noThread, Crypto::RSA::KeyData& keys, CryptoLevel preferEncrypted, bool startNegotiate) : - commTime(time(nullptr)), preferEncrypted(preferEncrypted), startNegotiate(startNegotiate) + preferEncrypted(preferEncrypted), startNegotiate(startNegotiate) { _socket = wrap; this->noThread = noThread; - sharedSetup(); + sharedSetup(true); } NetClient::~NetClient() { @@ -357,20 +360,10 @@ namespace IO { - bool NetServer::close() { - if (!_open) return false; - _open = false; - for (ulong_64b t = clients->size(); t > 0; --t) { - NetClient* s = clients->at(t - 1); - s->close(); - clients->pop_back(); - delete s; - } - return true; - } - NetServer::NetServer(char* port, std::function f = nullptr, CryptoLevel pref = CryptoLevel::None) : pref(pref) { - if (pref != CryptoLevel::None) keys = Crypto::RSA::rsa_gen_keys(); + + + void NetServer::sharedSetup(char* port, std::function f) { _open = true; timeoutHandler = NULL; onDestroy = NULL; @@ -448,7 +441,29 @@ namespace IO { closesocket(_server); close(); }); + } + bool NetServer::close() { + if (!_open) return false; + _open = false; + for (ulong_64b t = clients->size(); t > 0; --t) { + NetClient* s = clients->at(t - 1); + s->close(); + clients->pop_back(); + delete s; + } + return true; + } + + NetServer::NetServer(char* port, std::function f, CryptoLevel pref) : pref(pref) { + if (pref != CryptoLevel::None) keys = Crypto::RSA::rsa_gen_keys(); + sharedSetup(port, f); + } + + + NetServer::NetServer(char* port, std::function f, Crypto::RSA::KeyData& keys, CryptoLevel level) : pref(level) { + this->keys = keys; + sharedSetup(port, f); } NetServer::~NetServer() { @@ -520,7 +535,7 @@ namespace IO { client.write(message, size); } else { - for (size_t t = 0; t < size; ++t) buffer->push_back(message[t]); + for (std::streamsize t = 0; t < size; ++t) buffer->push_back(message[t]); if (buffer->size() > STREAM_BUFMIN) flush(); } } diff --git a/CPPTools/Net.h b/CPPTools/Net.h index 056ec18..9dce259 100644 --- a/CPPTools/Net.h +++ b/CPPTools/Net.h @@ -67,9 +67,10 @@ namespace IO { Crypto::RSA::KeyData keys; // Client's keysets (if using encryption) CryptoPP::RSAFunction pK; // Remote host's public key (if using encryption) + NetClient(char*, char*, CryptoLevel, bool); // Underlying setup for regular constructors NetClient(SOCKET, bool, CryptoLevel, bool);// Special setup constructor NetClient(SOCKET, bool, Crypto::RSA::KeyData&, CryptoLevel = CryptoLevel::None, bool = false);// Create wrapper for existing socket - void sharedSetup(); // Setup function for all constructor + void sharedSetup(bool); // Setup function for all constructor bool _write(char*, ulong_64b); // Internal write function. Doesn't do any of the fancy auto encryption: just raw write... bool writeBufferedPackets(); // Flushes and deletes buffer void update(); // Read incoming data and store in buffers @@ -84,6 +85,7 @@ namespace IO { public: time_t commTime; // Latest time a transaction occurred NetClient(char* ipAddr, char* port, CryptoLevel = CryptoLevel::None);// Standard constructor for creating connection + NetClient(char* ipAddr, char* port, Crypto::RSA::KeyData&, CryptoLevel);// Standard constructor for creating connection with predefined keys ~NetClient(); bool close(); void closeWrite(); @@ -109,9 +111,10 @@ namespace IO { private: CryptoLevel pref; Crypto::RSA::KeyData keys; // Server's keysets (if using encryption) - std::function onDestroy; volatile bool _open; + + void sharedSetup(char* port, std::function f); void updateClients(); protected: std::thread clientListener; @@ -119,7 +122,8 @@ namespace IO { std::vector* clients; public: std::function timeoutHandler; - NetServer(char* port, std::function, CryptoLevel); + NetServer(char* port, std::function = nullptr, CryptoLevel = CryptoLevel::None); + NetServer(char* port, std::function, Crypto::RSA::KeyData&, CryptoLevel); ~NetServer(); bool isOpen(); CryptoLevel getCryptoPreference(); diff --git a/CPPTools/Support.cpp b/CPPTools/Support.cpp index 75de7ba..c019f03 100644 --- a/CPPTools/Support.cpp +++ b/CPPTools/Support.cpp @@ -80,17 +80,23 @@ namespace Tools { char* c = (char*)data; ulong_64b lastNonZero = 0; - for (ulong_64b t = 0; t < size; ++t) if (c[t] != 0) lastNonZero = t; - if (lastNonZero == 0) return (char*)memset(malloc(1), '0', 1); + for (ulong_64b t = size; t > 0; --t) if (c[t - 1] != 0) { + lastNonZero = t - 1; + goto Ayy; + } + return new char[2]{ '0', 0 }; - char* c1 = (char*)malloc(lastNonZero * 2); - for (ulong_64b t = 0; t < lastNonZero; ++t) { - c1[2 * t] = (c[t]) & 15; - if (c1[(2 * t)] < 9) c1[(2 * t)] += 48; + Ayy: + char* c1 = (char*)new char[1 + ((lastNonZero + 1) * 2)]; + c1[lastNonZero * 2] = 0; + for (ulong_64b j = lastNonZero + 1; j > 0; --j) { + ulong_64b t = 1 + lastNonZero - j; + c1[2 * t] = (c[j - 1] >> 4) & 15; + if (c1[(2 * t)] < 10) c1[(2 * t)] += 48; else c1[(2 * t)] += 55; - c1[(2 * t) + 1] = (c[t] >> 4) & 15; - if (c1[(2 * t) + 1] < 9) c1[(2 * t) + 1] += 48; + c1[(2 * t) + 1] = (c[j - 1]) & 15; + if (c1[(2 * t) + 1] < 10) c1[(2 * t) + 1] += 48; else c1[(2 * t) + 1] += 55; } return c1; diff --git a/CPPTools/Support.h b/CPPTools/Support.h index 63b24ca..40c6514 100644 --- a/CPPTools/Support.h +++ b/CPPTools/Support.h @@ -21,7 +21,7 @@ namespace Tools { ulong_64b lastIndexOf(char*, char); char* copydata(const char*, ulong_64b); char* toHexString(const void* data, ulong_64b size); - char* toHexString(ulong_64b value); + char* toHexString(ulong_64b); bool isDigit(char c); bool isNumber(char* c); bool isIP(char* c); diff --git a/deps.zip b/deps.zip new file mode 100644 index 0000000..182af0d Binary files /dev/null and b/deps.zip differ