Fixed minor bugs
Added support for pre-created keys to be supplied
This commit is contained in:
parent
be17d51c3f
commit
d13e86fc75
@ -44,8 +44,8 @@ namespace IO {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void NetClient::sharedSetup() {
|
void NetClient::sharedSetup(bool setupKeys) {
|
||||||
if (preferEncrypted != CryptoLevel::None) keys = Crypto::RSA::rsa_gen_keys();
|
if (setupKeys && (preferEncrypted != CryptoLevel::None)) keys = Crypto::RSA::rsa_gen_keys();
|
||||||
packets = new std::vector<Packet>();
|
packets = new std::vector<Packet>();
|
||||||
sparse = new std::vector<char>();
|
sparse = new std::vector<char>();
|
||||||
outPacketBuf = new std::vector<Packet>();
|
outPacketBuf = new std::vector<Packet>();
|
||||||
@ -58,8 +58,9 @@ namespace IO {
|
|||||||
if (send(_socket, &cryptoPref, 1, 0) == SOCKET_ERROR) throw new std::exception(); // Cannot establish connection :(
|
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
|
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) :
|
NetClient::NetClient(char* ipAddr, char* port, CryptoLevel preferEncrypted) : NetClient(ipAddr, port, preferEncrypted, true) {}
|
||||||
commTime(time(nullptr)), preferEncrypted(preferEncrypted), startNegotiate(false)
|
NetClient::NetClient(char* ipAddr, char* port, CryptoLevel preferEncrypted, bool setupKeys) :
|
||||||
|
preferEncrypted(preferEncrypted), startNegotiate(false)
|
||||||
{
|
{
|
||||||
_socket = INVALID_SOCKET;
|
_socket = INVALID_SOCKET;
|
||||||
this->noThread = false;
|
this->noThread = false;
|
||||||
@ -102,15 +103,17 @@ namespace IO {
|
|||||||
|
|
||||||
if (_socket == INVALID_SOCKET) throw new std::exception();
|
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) :
|
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;
|
_socket = wrap;
|
||||||
this->noThread = noThread;
|
this->noThread = noThread;
|
||||||
sharedSetup();
|
sharedSetup(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
NetClient::~NetClient() {
|
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<bool(NetClient*)> 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<bool(NetClient*)> f) {
|
||||||
_open = true;
|
_open = true;
|
||||||
timeoutHandler = NULL;
|
timeoutHandler = NULL;
|
||||||
onDestroy = NULL;
|
onDestroy = NULL;
|
||||||
@ -448,7 +441,29 @@ namespace IO {
|
|||||||
closesocket(_server);
|
closesocket(_server);
|
||||||
close();
|
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<bool(NetClient*)> f, CryptoLevel pref) : pref(pref) {
|
||||||
|
if (pref != CryptoLevel::None) keys = Crypto::RSA::rsa_gen_keys();
|
||||||
|
sharedSetup(port, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
NetServer::NetServer(char* port, std::function<bool(NetClient*)> f, Crypto::RSA::KeyData& keys, CryptoLevel level) : pref(level) {
|
||||||
|
this->keys = keys;
|
||||||
|
sharedSetup(port, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
NetServer::~NetServer() {
|
NetServer::~NetServer() {
|
||||||
@ -520,7 +535,7 @@ namespace IO {
|
|||||||
client.write(message, size);
|
client.write(message, size);
|
||||||
}
|
}
|
||||||
else {
|
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();
|
if (buffer->size() > STREAM_BUFMIN) flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,9 +67,10 @@ namespace IO {
|
|||||||
Crypto::RSA::KeyData keys; // Client's keysets (if using encryption)
|
Crypto::RSA::KeyData keys; // Client's keysets (if using encryption)
|
||||||
CryptoPP::RSAFunction pK; // Remote host's public key (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, CryptoLevel, bool);// Special setup constructor
|
||||||
NetClient(SOCKET, bool, Crypto::RSA::KeyData&, CryptoLevel = CryptoLevel::None, bool = false);// Create wrapper for existing socket
|
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 _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
|
bool writeBufferedPackets(); // Flushes and deletes buffer
|
||||||
void update(); // Read incoming data and store in buffers
|
void update(); // Read incoming data and store in buffers
|
||||||
@ -84,6 +85,7 @@ namespace IO {
|
|||||||
public:
|
public:
|
||||||
time_t commTime; // Latest time a transaction occurred
|
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, 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();
|
~NetClient();
|
||||||
bool close();
|
bool close();
|
||||||
void closeWrite();
|
void closeWrite();
|
||||||
@ -109,9 +111,10 @@ namespace IO {
|
|||||||
private:
|
private:
|
||||||
CryptoLevel pref;
|
CryptoLevel pref;
|
||||||
Crypto::RSA::KeyData keys; // Server's keysets (if using encryption)
|
Crypto::RSA::KeyData keys; // Server's keysets (if using encryption)
|
||||||
|
|
||||||
std::function<void()> onDestroy;
|
std::function<void()> onDestroy;
|
||||||
volatile bool _open;
|
volatile bool _open;
|
||||||
|
|
||||||
|
void sharedSetup(char* port, std::function<bool(NetClient*)> f);
|
||||||
void updateClients();
|
void updateClients();
|
||||||
protected:
|
protected:
|
||||||
std::thread clientListener;
|
std::thread clientListener;
|
||||||
@ -119,7 +122,8 @@ namespace IO {
|
|||||||
std::vector<NetClient*>* clients;
|
std::vector<NetClient*>* clients;
|
||||||
public:
|
public:
|
||||||
std::function<bool(NetClient*)> timeoutHandler;
|
std::function<bool(NetClient*)> timeoutHandler;
|
||||||
NetServer(char* port, std::function<bool(NetClient*)>, CryptoLevel);
|
NetServer(char* port, std::function<bool(NetClient*)> = nullptr, CryptoLevel = CryptoLevel::None);
|
||||||
|
NetServer(char* port, std::function<bool(NetClient*)>, Crypto::RSA::KeyData&, CryptoLevel);
|
||||||
~NetServer();
|
~NetServer();
|
||||||
bool isOpen();
|
bool isOpen();
|
||||||
CryptoLevel getCryptoPreference();
|
CryptoLevel getCryptoPreference();
|
||||||
|
@ -80,17 +80,23 @@ namespace Tools {
|
|||||||
char* c = (char*)data;
|
char* c = (char*)data;
|
||||||
|
|
||||||
ulong_64b lastNonZero = 0;
|
ulong_64b lastNonZero = 0;
|
||||||
for (ulong_64b t = 0; t < size; ++t) if (c[t] != 0) lastNonZero = t;
|
for (ulong_64b t = size; t > 0; --t) if (c[t - 1] != 0) {
|
||||||
if (lastNonZero == 0) return (char*)memset(malloc(1), '0', 1);
|
lastNonZero = t - 1;
|
||||||
|
goto Ayy;
|
||||||
|
}
|
||||||
|
return new char[2]{ '0', 0 };
|
||||||
|
|
||||||
char* c1 = (char*)malloc(lastNonZero * 2);
|
Ayy:
|
||||||
for (ulong_64b t = 0; t < lastNonZero; ++t) {
|
char* c1 = (char*)new char[1 + ((lastNonZero + 1) * 2)];
|
||||||
c1[2 * t] = (c[t]) & 15;
|
c1[lastNonZero * 2] = 0;
|
||||||
if (c1[(2 * t)] < 9) c1[(2 * t)] += 48;
|
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;
|
else c1[(2 * t)] += 55;
|
||||||
|
|
||||||
c1[(2 * t) + 1] = (c[t] >> 4) & 15;
|
c1[(2 * t) + 1] = (c[j - 1]) & 15;
|
||||||
if (c1[(2 * t) + 1] < 9) c1[(2 * t) + 1] += 48;
|
if (c1[(2 * t) + 1] < 10) c1[(2 * t) + 1] += 48;
|
||||||
else c1[(2 * t) + 1] += 55;
|
else c1[(2 * t) + 1] += 55;
|
||||||
}
|
}
|
||||||
return c1;
|
return c1;
|
||||||
|
@ -21,7 +21,7 @@ namespace Tools {
|
|||||||
ulong_64b lastIndexOf(char*, char);
|
ulong_64b lastIndexOf(char*, char);
|
||||||
char* copydata(const char*, ulong_64b);
|
char* copydata(const char*, ulong_64b);
|
||||||
char* toHexString(const void* data, ulong_64b size);
|
char* toHexString(const void* data, ulong_64b size);
|
||||||
char* toHexString(ulong_64b value);
|
char* toHexString(ulong_64b);
|
||||||
bool isDigit(char c);
|
bool isDigit(char c);
|
||||||
bool isNumber(char* c);
|
bool isNumber(char* c);
|
||||||
bool isIP(char* c);
|
bool isIP(char* c);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user