From 0191072326543e61256e38e27c5f2accebe30c86 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 1 Nov 2021 22:01:59 +0100 Subject: [PATCH] added response system from friend server --- libretroshare/src/friend_server/fsitem.h | 3 ++ libretroshare/src/pgp/pgphandler.cc | 14 +++++--- retroshare-friendserver/src/friendserver.cc | 39 ++++++++++++++++++--- retroshare-friendserver/src/friendserver.h | 7 +++- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/libretroshare/src/friend_server/fsitem.h b/libretroshare/src/friend_server/fsitem.h index d523f5726..ca6d4d076 100644 --- a/libretroshare/src/friend_server/fsitem.h +++ b/libretroshare/src/friend_server/fsitem.h @@ -123,15 +123,18 @@ public: void serial_process(RsGenericSerializer::SerializeJob j,RsGenericSerializer::SerializeContext& ctx) override { + RS_SERIAL_PROCESS(nonce); RS_SERIAL_PROCESS(friend_invites); } virtual void clear() override { friend_invites.clear(); + nonce = 0; } // specific members for that item + uint64_t nonce; std::map friend_invites; }; diff --git a/libretroshare/src/pgp/pgphandler.cc b/libretroshare/src/pgp/pgphandler.cc index b1e96b00b..b0ffd31cd 100644 --- a/libretroshare/src/pgp/pgphandler.cc +++ b/libretroshare/src/pgp/pgphandler.cc @@ -1926,7 +1926,10 @@ bool PGPHandler::locked_syncPublicKeyring() #else if(-1 == stat64(_pubring_path.c_str(), &buf)) #endif + { std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _pubring_path << ". Can't sync public keyring." << std::endl; + buf.st_mtime = 0; + } if(_pubring_last_update_time < buf.st_mtime) { @@ -1968,12 +1971,13 @@ bool PGPHandler::locked_syncTrustDatabase() librs::util::ConvertUtf8ToUtf16(_trustdb_path, wfullname); if(-1 == _wstati64(wfullname.c_str(), &buf)) #else - if(-1 == stat64(_trustdb_path.c_str(), &buf)) + if(-1 == stat64(_trustdb_path.c_str(), &buf)) #endif - { - std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _trustdb_path << ". Will force write it." << std::endl; - _trustdb_changed = true ; // we force write of trust database if it does not exist. - } + { + std::cerr << "PGPHandler::syncDatabase(): can't stat file " << _trustdb_path << ". Will force write it." << std::endl; + _trustdb_changed = true ; // we force write of trust database if it does not exist. + buf.st_mtime = 0; + } if(_trustdb_last_update_time < buf.st_mtime) { diff --git a/retroshare-friendserver/src/friendserver.cc b/retroshare-friendserver/src/friendserver.cc index b2c2ba38b..c74fd4801 100644 --- a/retroshare-friendserver/src/friendserver.cc +++ b/retroshare-friendserver/src/friendserver.cc @@ -70,12 +70,21 @@ void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *it RsDbg() << *item ; // First of all, read PGP key and short invites, parse them, and check that they contain the same information + RsPeerId pid; + RsPgpFingerprint fpr; - FriendServer::handleIncomingClientData(item->pgp_public_key_b64,item->short_invite); + std::map::iterator pi = handleIncomingClientData(item->pgp_public_key_b64,item->short_invite); + // No need to test for it==mCurrentClients.end() because it will be directly caught by the exception handling below even before. // Respond with a list of potential friends + RsFriendServerServerResponseItem *sr_item = new RsFriendServerServerResponseItem; + sr_item->nonce = pi->second.last_nonce; + sr_item->friend_invites = computeListOfFriendInvites(item->n_requested_friends,pi->first,pi->second.pgp_fingerprint); + sr_item->PeerId(item->PeerId()); + + mni->SendItem(sr_item); } catch(std::exception& e) { @@ -89,7 +98,27 @@ void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *it mni->closeConnection(item->PeerId()); } -bool FriendServer::handleIncomingClientData(const std::string& pgp_public_key_b64,const std::string& short_invite_b64) +std::map FriendServer::computeListOfFriendInvites(uint32_t nb_reqs_invites, const RsPeerId &pid, const RsPgpFingerprint &fpr) +{ + // For now, returns the first nb_reqs_invites from the currently known peer, that would not be the peer who's asking + + std::map res; + + for(auto it:mCurrentClientPeers) + { + if(it.first == pid) + continue; + + res.insert(std::make_pair(it.second.short_certificate,false)); // for now we say that peers havn't been warned already + + if(res.size() >= nb_reqs_invites) + break; + } + + return res; +} + +std::map::iterator FriendServer::handleIncomingClientData(const std::string& pgp_public_key_b64,const std::string& short_invite_b64) { RsDbg() << " Checking item data..."; @@ -146,9 +175,11 @@ bool FriendServer::handleIncomingClientData(const std::string& pgp_public_key_b6 pi.short_certificate = short_invite_b64; pi.last_connection_TS = time(nullptr); - pi.last_nonce = RsRandom::random_u64(); - return true; + while(pi.last_nonce == 0) // reuse the same identifier (so it's not really a nonce, but it's kept secret whatsoever). + pi.last_nonce = RsRandom::random_u64(); + + return mCurrentClientPeers.find(shortInviteDetails.id); } diff --git a/retroshare-friendserver/src/friendserver.h b/retroshare-friendserver/src/friendserver.h index 711256d69..ba5f9d06b 100644 --- a/retroshare-friendserver/src/friendserver.h +++ b/retroshare-friendserver/src/friendserver.h @@ -32,6 +32,7 @@ class RsFriendServerClientPublishItem; struct PeerInfo { + RsPgpFingerprint pgp_fingerprint; std::string short_certificate; rstime_t last_connection_TS; uint64_t last_nonce; @@ -53,7 +54,11 @@ private: void handleClientRemove(const RsFriendServerClientRemoveItem *item); void handleClientPublish(const RsFriendServerClientPublishItem *item); - bool handleIncomingClientData(const std::string& pgp_public_key_b64,const std::string& short_invite_b64); + // Adds the incoming peer data to the list of current clients and returns the + std::map::iterator handleIncomingClientData(const std::string& pgp_public_key_b64,const std::string& short_invite_b64); + + // Computes the appropriate list of short invites to send to a given peer. + std::map computeListOfFriendInvites(uint32_t nb_reqs_invites,const RsPeerId& pid,const RsPgpFingerprint& fpr); void autoWash(); void debugPrint();