PICloud change protocol > server_uuid

This commit is contained in:
2021-04-12 11:35:34 +03:00
parent f75ce1e8e0
commit dbd2267a8c
12 changed files with 56 additions and 33 deletions

View File

@@ -28,7 +28,7 @@ const char hash_def_key[] = "_picrypt_";
PICloud::TCP::Header::Header() {
version = Version_1;
version = Version_2;
}
@@ -42,15 +42,20 @@ void PICloud::TCP::setRole(PICloud::TCP::Role r) {
void PICloud::TCP::setServerName(const PIString & server_name) {
sname = server_name;
suuid = PICrypt::hash(server_name);
}
void PICloud::TCP::sendStart() {
//piCout << "sendStart";
if (suuid.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, server not set, invoke setServerName first";
return;
}
header.type = PICloud::TCP::Connect;
PIByteArray ba;
ba << header << sname;
ba << header;
ba.append(suuid);
streampacker->send(ba);
}
@@ -100,7 +105,7 @@ PIPair<PICloud::TCP::Type, PICloud::TCP::Role> PICloud::TCP::parseHeader(PIByteA
PICloud::TCP::Header hdr;
ba >> hdr;
if (hdr.version != header.version) {
piCout << "[PICloud]" << "invalid TCP version!";
piCout << "[PICloud]" << "invalid PICloud::TCP version!";
return ret;
}
ret.first = (Type)hdr.type;
@@ -128,10 +133,13 @@ PIPair<uint, PIByteArray> PICloud::TCP::parseDataServer(PIByteArray & ba) {
}
PIString PICloud::TCP::parseConnect_d(PIByteArray & ba) {
PIString ret;
ba >> ret;
return ret;
PIByteArray PICloud::TCP::parseConnect_d(PIByteArray & ba) {
if (ba.size() != PICrypt::sizeHash()) {
piCout << "PICloud ERROR, invalid server uuid";
return PIByteArray();
} else {
return ba;
}
}

View File

@@ -169,6 +169,11 @@ PIByteArray PICrypt::hash(const PIByteArray & data) {
}
size_t PICrypt::sizeHash() {
return crypto_generichash_BYTES;
}
ullong PICrypt::shorthash(const PIString& s, PIByteArray key) {
ullong hash = 0;
#ifdef PIP_CRYPT

View File

@@ -38,6 +38,7 @@ class PIP_CLOUD_EXPORT TCP {
public:
enum Version {
Version_1 = 1,
Version_2 = 2,
};
enum Role {
@@ -66,7 +67,7 @@ public:
PIPair<PICloud::TCP::Type, PICloud::TCP::Role> parseHeader(PIByteArray & ba);
PIByteArray parseData(PIByteArray & ba);
PIPair<uint, PIByteArray> parseDataServer(PIByteArray & ba);
PIString parseConnect_d(PIByteArray & ba);
PIByteArray parseConnect_d(PIByteArray & ba);
uint parseConnect(PIByteArray & ba);
uint parseDisconnect(PIByteArray & ba);
@@ -79,7 +80,7 @@ private:
};
Header header;
PIString sname;
PIByteArray suuid;
PIStreamPacker * streampacker;
};

View File

@@ -181,6 +181,12 @@ public:
return true;
}
inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);}
inline bool operator >(const PIDeque<T> & t) const {
if (pid_size != t.pid_size) return pid_size > t.pid_size;
for (size_t i = 0; i < pid_size; ++i)
if (t[i] != (*this)[i]) return t[i] > (*this)[i];
return false;
}
inline bool contains(const T & v) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i)
if (v == pid_data[i])

View File

@@ -58,6 +58,9 @@ public:
//! Generate hash from bytearray
static PIByteArray hash(const PIByteArray & data);
//! Returns hash size
static size_t sizeHash();
//! Generate short hash from string "s", may be used for hash table
static ullong shorthash(const PIString & s, PIByteArray key = PIByteArray());