diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b6751dc..f5a89d4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,9 +2,9 @@ cmake_minimum_required(VERSION 3.0) cmake_policy(SET CMP0017 NEW) # need include() with .cmake project(pip) set(pip_MAJOR 2) -set(pip_MINOR 21) +set(pip_MINOR 22) set(pip_REVISION 0) -set(pip_SUFFIX alpha) +set(pip_SUFFIX ) set(pip_COMPANY SHS) set(pip_DOMAIN org.SHS) diff --git a/libs/cloud/picloudtcp.cpp b/libs/cloud/picloudtcp.cpp index 97b8faac..b10f64eb 100644 --- a/libs/cloud/picloudtcp.cpp +++ b/libs/cloud/picloudtcp.cpp @@ -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::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 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; + } } diff --git a/libs/crypt/picrypt.cpp b/libs/crypt/picrypt.cpp index 56ab0f29..1d15160a 100644 --- a/libs/crypt/picrypt.cpp +++ b/libs/crypt/picrypt.cpp @@ -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 diff --git a/libs/main/cloud/picloudtcp.h b/libs/main/cloud/picloudtcp.h index ad255481..93cad83e 100644 --- a/libs/main/cloud/picloudtcp.h +++ b/libs/main/cloud/picloudtcp.h @@ -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 parseHeader(PIByteArray & ba); PIByteArray parseData(PIByteArray & ba); PIPair 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; }; diff --git a/libs/main/containers/pideque.h b/libs/main/containers/pideque.h index 82d00b45..e2ebe89b 100644 --- a/libs/main/containers/pideque.h +++ b/libs/main/containers/pideque.h @@ -181,6 +181,12 @@ public: return true; } inline bool operator !=(const PIDeque & t) const {return !(*this == t);} + inline bool operator >(const PIDeque & 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]) diff --git a/libs/main/crypt/picrypt.h b/libs/main/crypt/picrypt.h index 67fe3a6b..e437951a 100644 --- a/libs/main/crypt/picrypt.h +++ b/libs/main/crypt/picrypt.h @@ -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()); diff --git a/utils/cloud_dispatcher/cloudserver.cpp b/utils/cloud_dispatcher/cloudserver.cpp index 3a25a7d0..d3c58aee 100644 --- a/utils/cloud_dispatcher/cloudserver.cpp +++ b/utils/cloud_dispatcher/cloudserver.cpp @@ -1,8 +1,8 @@ #include "cloudserver.h" -CloudServer::CloudServer(DispatcherClient * c, const PIString & sname) : server(c) { - setName(sname); - server_name = sname; +CloudServer::CloudServer(DispatcherClient * c, const PIByteArray & sname) : server(c) { + setName(sname.toHex()); + server_uuid = sname; CONNECTL(c, dataReadedServer, ([this](uint id, PIByteArray & ba){ DispatcherClient * cl = index_clients.value(id, nullptr); if (cl) cl->sendData(ba); @@ -17,8 +17,8 @@ CloudServer::~CloudServer() { } -PIString CloudServer::serverName() const { - return server_name; +PIByteArray CloudServer::serverUUID() const { + return server_uuid; } @@ -49,7 +49,7 @@ PIVector CloudServer::getClients() { void CloudServer::printStatus() { - piCout << " " << "Clients for" << server->address() << server_name << ":"; + piCout << " " << "Clients for" << server->address() << server_uuid.toHex() << ":"; for (auto c: clients) { piCout << " " << c->address() << c->clientId(); } diff --git a/utils/cloud_dispatcher/cloudserver.h b/utils/cloud_dispatcher/cloudserver.h index 8b3864b9..14573a67 100644 --- a/utils/cloud_dispatcher/cloudserver.h +++ b/utils/cloud_dispatcher/cloudserver.h @@ -7,9 +7,9 @@ class CloudServer : public PIObject { PIOBJECT(CloudServer) public: - CloudServer(DispatcherClient * c, const PIString & sname); + CloudServer(DispatcherClient * c, const PIByteArray & sname); ~CloudServer(); - PIString serverName() const; + PIByteArray serverUUID() const; void addClient(DispatcherClient * c); void removeClient(DispatcherClient * c); PIVector getClients(); @@ -20,7 +20,7 @@ private: DispatcherClient * server; PIVector clients; PIMap index_clients; - PIString server_name; + PIByteArray server_uuid; }; #endif // CLOUDSERVER_H diff --git a/utils/cloud_dispatcher/dispatcherclient.cpp b/utils/cloud_dispatcher/dispatcherclient.cpp index 289e6e1d..7e89d901 100644 --- a/utils/cloud_dispatcher/dispatcherclient.cpp +++ b/utils/cloud_dispatcher/dispatcherclient.cpp @@ -101,7 +101,7 @@ void DispatcherClient::readed(PIByteArray & ba) { switch (hdr.first) { case PICloud::TCP::Connect: { tcp.setRole(hdr.second); - PIString sn = tcp.parseConnect_d(ba); + PIByteArray sn = tcp.parseConnect_d(ba); if (hdr.second == PICloud::TCP::Server) registerServer(sn, this); if (hdr.second == PICloud::TCP::Client) registerClient(sn, this); return;} diff --git a/utils/cloud_dispatcher/dispatcherclient.h b/utils/cloud_dispatcher/dispatcherclient.h index 3b4dd1c2..7cb9629e 100644 --- a/utils/cloud_dispatcher/dispatcherclient.h +++ b/utils/cloud_dispatcher/dispatcherclient.h @@ -23,8 +23,8 @@ public: PIString address(); uint clientId() const {return client_id;} EVENT1(disconnectEvent, DispatcherClient *, client) - EVENT2(registerServer, PIString, sname, DispatcherClient *, client) - EVENT2(registerClient, PIString, sname, DispatcherClient *, client) + EVENT2(registerServer, const PIByteArray &, sname, DispatcherClient *, client) + EVENT2(registerClient, const PIByteArray &, sname, DispatcherClient *, client) EVENT1(dataReaded, PIByteArray &, ba) EVENT2(dataReadedServer, uint, id, PIByteArray &, ba) diff --git a/utils/cloud_dispatcher/dispatcherserver.cpp b/utils/cloud_dispatcher/dispatcherserver.cpp index d161e62c..cdf79ecd 100644 --- a/utils/cloud_dispatcher/dispatcherserver.cpp +++ b/utils/cloud_dispatcher/dispatcherserver.cpp @@ -70,12 +70,12 @@ void DispatcherServer::updateConnectionsTile(TileList * tl) { case PICloud::TCP::Client : { role = "Client"; CloudServer * cs = index_c_clients.value(c, nullptr); - if (cs) role += " \"" + cs->serverName() + "\""; + if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\""; } break; case PICloud::TCP::Server : { role = "Server"; CloudServer * cs = index_c_servers.value(c, nullptr); - if (cs) role += " \"" + cs->serverName() + "\""; + if (cs) role += " \"" + cs->serverUUID().toHex().left(8) + "...\""; } break; default: break; @@ -91,7 +91,7 @@ void DispatcherServer::updateServersTile(TileList * tl, PISetcontent.clear(); auto mi = c_servers.makeIterator(); while (mi.next()) { - tl->content << TileList::Row(mi.value()->serverName() + " - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat()); + tl->content << TileList::Row(mi.value()->serverUUID().toHex().left(8) + "... - " + PIString::fromNumber(mi.value()->getClients().size()), PIScreenTypes::CellFormat()); if (servers.contains(mi.value()->getConnection())) tl->selected << (tl->content.size_s() - 1); } map_mutex.unlock(); @@ -167,7 +167,7 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) { csc->close(); csc->deleteLater(); } - c_servers.remove(cs->serverName()); + c_servers.remove(cs->serverUUID()); index_c_servers.removeOne(client); delete cs; } @@ -185,13 +185,13 @@ void DispatcherServer::disconnectClient(DispatcherClient *client) { void DispatcherServer::newConnection(PIEthernet *cl) { DispatcherClient * client = new DispatcherClient(cl, client_gid++); CONNECTU(client, disconnectEvent, this, disconnectClient); - CONNECTL(client, registerServer, [this](PIString sname, DispatcherClient * c){ + CONNECTL(client, registerServer, [this](const PIByteArray & sname, DispatcherClient * c){ map_mutex.lock(); CloudServer * cs = c_servers.value(sname, nullptr); if (cs) { rm_clients << c; } else { - piCoutObj << "add new Server ->" << sname; + piCoutObj << "add new Server ->" << sname.toHex(); CloudServer * cs = new CloudServer(c, sname); c_servers.insert(sname, cs); index_c_servers.insert(c, cs); @@ -199,11 +199,11 @@ void DispatcherServer::newConnection(PIEthernet *cl) { } map_mutex.unlock(); }); - CONNECTL(client, registerClient, [this](PIString sname, DispatcherClient * c){ + CONNECTL(client, registerClient, [this](const PIByteArray & sname, DispatcherClient * c){ map_mutex.lock(); CloudServer * cs = c_servers.value(sname, nullptr); if (cs) { - piCoutObj << "add new Client to Server ->" << sname; + piCoutObj << "add new Client to Server ->" << sname.toHex(); cs->addClient(c); index_c_clients.insert(c, cs); c->authorise(true); diff --git a/utils/cloud_dispatcher/dispatcherserver.h b/utils/cloud_dispatcher/dispatcherserver.h index b8320fd6..1ac53897 100644 --- a/utils/cloud_dispatcher/dispatcherserver.h +++ b/utils/cloud_dispatcher/dispatcherserver.h @@ -28,7 +28,7 @@ private: PIEthernet eth; PIVector clients; - PIMap c_servers; + PIMap c_servers; PIMap index_c_servers; PIMap index_c_clients; PIVector rm_clients;