From 4b7a2160b975e9e192e819672892995a0b58deea Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 11 Feb 2014 20:58:33 +0000 Subject: [PATCH] ID cleaning - saving work git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.6-IdCleaning@7108 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/retroshare/rsconfig.h | 2 +- libretroshare/src/retroshare/rsdisc.h | 7 +- libretroshare/src/retroshare/rsmsgs.h | 4 +- libretroshare/src/retroshare/rsstatus.h | 7 +- libretroshare/src/rsserver/p3msgs.cc | 2 +- libretroshare/src/rsserver/p3msgs.h | 2 +- libretroshare/src/rsserver/p3serverconfig.cc | 2 +- libretroshare/src/rsserver/p3serverconfig.h | 2 +- libretroshare/src/rsserver/p3status.cc | 4 +- libretroshare/src/rsserver/p3status.h | 4 +- .../src/serialiser/rsdiscovery2items.cc | 18 +- .../src/serialiser/rsdiscovery2items.h | 6 +- libretroshare/src/services/p3banlist.cc | 20 +- libretroshare/src/services/p3bwctrl.cc | 18 +- libretroshare/src/services/p3bwctrl.h | 4 +- libretroshare/src/services/p3discovery2.cc | 10 +- libretroshare/src/services/p3discovery2.h | 10 +- libretroshare/src/services/p3msgservice.cc | 445 +++++++++--------- libretroshare/src/services/p3msgservice.h | 19 +- libretroshare/src/services/p3statusservice.cc | 44 +- libretroshare/src/services/p3statusservice.h | 6 +- 21 files changed, 327 insertions(+), 309 deletions(-) diff --git a/libretroshare/src/retroshare/rsconfig.h b/libretroshare/src/retroshare/rsconfig.h index e33ba53e6..1a5eca28d 100644 --- a/libretroshare/src/retroshare/rsconfig.h +++ b/libretroshare/src/retroshare/rsconfig.h @@ -239,7 +239,7 @@ virtual int getConfigNetStatus(RsConfigNetStatus &status) = 0; //virtual int getConfigStartup(RsConfigStartup ¶ms) = 0; virtual int getTotalBandwidthRates(RsConfigDataRates &rates) = 0; -virtual int getAllBandwidthRates(std::map &ratemap) = 0; +virtual int getAllBandwidthRates(std::map &ratemap) = 0; /* From RsInit */ diff --git a/libretroshare/src/retroshare/rsdisc.h b/libretroshare/src/retroshare/rsdisc.h index a4f6352a9..404d877a0 100644 --- a/libretroshare/src/retroshare/rsdisc.h +++ b/libretroshare/src/retroshare/rsdisc.h @@ -30,6 +30,7 @@ #include #include #include +#include /* The Main Interface Class - for information about your Peers */ class RsDisc; @@ -42,9 +43,9 @@ class RsDisc RsDisc() { return; } virtual ~RsDisc() { return; } -virtual bool getDiscFriends(const std::string &id, std::list& friends) = 0; -virtual bool getDiscPgpFriends(const std::string &pgpid, std::list& gpg_friends) = 0; -virtual bool getPeerVersion(const std::string &id, std::string &versions) = 0; +virtual bool getDiscFriends(const RsPeerId &id, std::list& friends) = 0; +virtual bool getDiscPgpFriends(const PGPIdType &pgpid, std::list& gpg_friends) = 0; +virtual bool getPeerVersion(const RsPeerId &id, std::string &versions) = 0; virtual bool getWaitingDiscCount(unsigned int *sendCount, unsigned int *recvCount) = 0; }; diff --git a/libretroshare/src/retroshare/rsmsgs.h b/libretroshare/src/retroshare/rsmsgs.h index 319ecb84b..8c383f0e7 100644 --- a/libretroshare/src/retroshare/rsmsgs.h +++ b/libretroshare/src/retroshare/rsmsgs.h @@ -238,7 +238,7 @@ extern RsMsgs *rsMsgs; struct DistantOfflineMessengingInvite { PGPIdType issuer_pgp_id ; - std::string hash ; + Sha1CheckSum hash ; time_t time_of_validity ; }; @@ -290,7 +290,7 @@ virtual bool createDistantOfflineMessengingInvite(time_t validity_time_stamp, st virtual bool getDistantOfflineMessengingInvites(std::vector& invites) = 0 ; virtual void enableDistantMessaging(bool b) = 0; virtual bool distantMessagingEnabled() = 0; -virtual bool getDistantMessageHash(const PGPIdType& pgp_id, std::string& hash) = 0; +virtual bool getDistantMessageHash(const PGPIdType& pgp_id, Sha1CheckSum& hash) = 0; /****************************************/ /* Chat */ diff --git a/libretroshare/src/retroshare/rsstatus.h b/libretroshare/src/retroshare/rsstatus.h index 6ebb9f469..00b82eab7 100644 --- a/libretroshare/src/retroshare/rsstatus.h +++ b/libretroshare/src/retroshare/rsstatus.h @@ -34,6 +34,7 @@ extern RsStatus *rsStatus; #include #include #include +#include const uint32_t RS_STATUS_OFFLINE = 0x0000; @@ -58,7 +59,7 @@ class StatusInfo } public: - std::string id; + RsPeerId id; uint32_t status; time_t time_stamp; /// for owner time set, and for their peers time sent }; @@ -88,7 +89,7 @@ class RsStatus * This retrieves the status info one peer * @param statusInfo is populated with client's peer's status */ - virtual bool getStatus(const std::string &id, StatusInfo &statusInfo) = 0; + virtual bool getStatus(const RsPeerId &id, StatusInfo &statusInfo) = 0; /** * send the client's status to his/her peers @@ -96,7 +97,7 @@ class RsStatus * @param status the status of the peers * @return will return false if status info does not belong to client */ - virtual bool sendStatus(const std::string &id, uint32_t status) = 0; + virtual bool sendStatus(const RsPeerId &id, uint32_t status) = 0; }; diff --git a/libretroshare/src/rsserver/p3msgs.cc b/libretroshare/src/rsserver/p3msgs.cc index da5faf51f..40aec9f66 100644 --- a/libretroshare/src/rsserver/p3msgs.cc +++ b/libretroshare/src/rsserver/p3msgs.cc @@ -116,7 +116,7 @@ bool p3Msgs::distantMessagingEnabled() { return mMsgSrv->distantMessagingEnabled(); } -bool p3Msgs::getDistantMessageHash(const PGPIdType& pgp_id,std::string& hash) +bool p3Msgs::getDistantMessageHash(const PGPIdType& pgp_id,Sha1CheckSum& hash) { return mMsgSrv->getDistantMessageHash(pgp_id,hash); } diff --git a/libretroshare/src/rsserver/p3msgs.h b/libretroshare/src/rsserver/p3msgs.h index 211363f3c..d815cadee 100644 --- a/libretroshare/src/rsserver/p3msgs.h +++ b/libretroshare/src/rsserver/p3msgs.h @@ -85,7 +85,7 @@ class p3Msgs: public RsMsgs virtual void enableDistantMessaging(bool b) ; virtual bool distantMessagingEnabled() ; - virtual bool getDistantMessageHash(const PGPIdType& pgp_id,std::string& hash) ; + virtual bool getDistantMessageHash(const PGPIdType& pgp_id,Sha1CheckSum& hash) ; /*! * gets avatar from peer, image data in jpeg format diff --git a/libretroshare/src/rsserver/p3serverconfig.cc b/libretroshare/src/rsserver/p3serverconfig.cc index 180483bc2..c535e68d0 100644 --- a/libretroshare/src/rsserver/p3serverconfig.cc +++ b/libretroshare/src/rsserver/p3serverconfig.cc @@ -206,7 +206,7 @@ int p3ServerConfig::getTotalBandwidthRates(RsConfigDataRates &rates) } -int p3ServerConfig::getAllBandwidthRates(std::map &ratemap) +int p3ServerConfig::getAllBandwidthRates(std::map &ratemap) { if (rsBandwidthControl) { diff --git a/libretroshare/src/rsserver/p3serverconfig.h b/libretroshare/src/rsserver/p3serverconfig.h index 8a65ddeb9..530fcd009 100644 --- a/libretroshare/src/rsserver/p3serverconfig.h +++ b/libretroshare/src/rsserver/p3serverconfig.h @@ -57,7 +57,7 @@ virtual int getConfigStartup(RsConfigStartup ¶ms); /***** for RsConfig -> p3BandwidthControl ****/ virtual int getTotalBandwidthRates(RsConfigDataRates &rates); -virtual int getAllBandwidthRates(std::map &ratemap); +virtual int getAllBandwidthRates(std::map &ratemap); /* From RsInit */ diff --git a/libretroshare/src/rsserver/p3status.cc b/libretroshare/src/rsserver/p3status.cc index 7ec0d285a..a1733a86c 100644 --- a/libretroshare/src/rsserver/p3status.cc +++ b/libretroshare/src/rsserver/p3status.cc @@ -47,12 +47,12 @@ bool p3Status::getStatusList(std::list& statusInfo){ return mStatusSrv->getStatusList(statusInfo); } -bool p3Status::getStatus(const std::string &id, StatusInfo &statusInfo) +bool p3Status::getStatus(const RsPeerId &id, StatusInfo &statusInfo) { return mStatusSrv->getStatus(id, statusInfo); } -bool p3Status::sendStatus(const std::string &id, uint32_t status){ +bool p3Status::sendStatus(const RsPeerId &id, uint32_t status){ return mStatusSrv->sendStatus(id, status); } diff --git a/libretroshare/src/rsserver/p3status.h b/libretroshare/src/rsserver/p3status.h index 53584fa3f..ca7a0e508 100644 --- a/libretroshare/src/rsserver/p3status.h +++ b/libretroshare/src/rsserver/p3status.h @@ -45,8 +45,8 @@ public: virtual bool getOwnStatus(StatusInfo& statusInfo); virtual bool getStatusList(std::list& statusInfo); - virtual bool getStatus(const std::string &id, StatusInfo &statusInfo); - virtual bool sendStatus(const std::string &id, uint32_t status); + virtual bool getStatus(const RsPeerId &id, StatusInfo &statusInfo); + virtual bool sendStatus(const RsPeerId &id, uint32_t status); private: diff --git a/libretroshare/src/serialiser/rsdiscovery2items.cc b/libretroshare/src/serialiser/rsdiscovery2items.cc index f66faac67..3dfe7a164 100644 --- a/libretroshare/src/serialiser/rsdiscovery2items.cc +++ b/libretroshare/src/serialiser/rsdiscovery2items.cc @@ -442,7 +442,7 @@ std::ostream &RsDiscPgpCertItem::print(std::ostream &out, uint16_t indent) uint32_t RsDiscSerialiser::sizePgpCert(RsDiscPgpCertItem *item) { uint32_t s = 8; /* header */ - s += GetTlvStringSize(item->pgpId); + s += item->pgpId.serial_size(); s += GetTlvStringSize(item->pgpCert); return s; } @@ -471,7 +471,7 @@ bool RsDiscSerialiser::serialisePgpCert(RsDiscPgpCertItem *item, void *data, offset += 8; /* add mandatory parts first */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PGPID, item->pgpId); + ok &= item->pgpId.serialise(data, tlvsize, offset) ; ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PGPCERT, item->pgpCert); if (offset != tlvsize) { @@ -523,7 +523,7 @@ RsDiscPgpCertItem *RsDiscSerialiser::deserialisePgpCert(void *data, uint32_t *pk offset += 8; /* get mandatory parts first */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PGPID, item->pgpId); + ok &= item->pgpId.deserialise(data, rssize, offset) ; ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PGPCERT, item->pgpCert); if (offset != rssize) { @@ -659,8 +659,8 @@ std::ostream &RsDiscContactItem::print(std::ostream &out, uint16_t indent) uint32_t RsDiscSerialiser::sizeContact(RsDiscContactItem *item) { uint32_t s = 8; /* header */ - s += GetTlvStringSize(item->pgpId); - s += GetTlvStringSize(item->sslId); + s += item->pgpId.serial_size(); + s += item->sslId.serial_size(); s += GetTlvStringSize(item->location); s += GetTlvStringSize(item->version); @@ -733,8 +733,8 @@ bool RsDiscSerialiser::serialiseContact(RsDiscContactItem *item, void *data, offset += 8; /* add mandatory parts first */ - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PGPID, item->pgpId); - ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_PEERID, item->sslId); + ok &= item->pgpId.serialise(data, tlvsize, offset) ; + ok &= item->sslId.serialise(data, tlvsize, offset) ; ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_LOCATION, item->location); ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_VERSION, item->version); @@ -818,8 +818,8 @@ RsDiscContactItem *RsDiscSerialiser::deserialiseContact(void *data, uint32_t *pk offset += 8; /* get mandatory parts first */ - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PGPID, item->pgpId); - ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_PEERID, item->sslId); + ok &= item->pgpId.deserialise(data, rssize, offset) ; + ok &= item->pgpId.deserialise(data, rssize, offset) ; ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_LOCATION, item->location); ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_VERSION, item->version); diff --git a/libretroshare/src/serialiser/rsdiscovery2items.h b/libretroshare/src/serialiser/rsdiscovery2items.h index ff8ac1491..f62d728e4 100644 --- a/libretroshare/src/serialiser/rsdiscovery2items.h +++ b/libretroshare/src/serialiser/rsdiscovery2items.h @@ -86,7 +86,7 @@ virtual ~RsDiscPgpCertItem(); virtual void clear(); virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); - std::string pgpId; + PGPIdType pgpId; std::string pgpCert; }; @@ -106,8 +106,8 @@ virtual ~RsDiscContactItem(); virtual void clear(); virtual std::ostream &print(std::ostream &out, uint16_t indent = 0); - std::string pgpId; - std::string sslId; + PGPIdType pgpId; + RsPeerId sslId; // COMMON std::string location; diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index d531a1d3e..7f7bc183f 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -143,9 +143,9 @@ bool p3BanList::recvBanItem(RsBanListItem *item) } /* overloaded from pqiNetAssistSharePeer */ -void p3BanList::updatePeer(std::string /*id*/, const struct sockaddr_storage &addr, int /*type*/, int /*reason*/, int age) +void p3BanList::updatePeer(const RsPeerId& /*id*/, const struct sockaddr_storage &addr, int /*type*/, int /*reason*/, int age) { - std::string ownId = mLinkMgr->getOwnId(); + RsPeerId ownId = mLinkMgr->getOwnId(); int int_reason = 0; addBanEntry(ownId, addr, RSBANLIST_SOURCE_SELF, int_reason, age); @@ -160,7 +160,7 @@ void p3BanList::updatePeer(std::string /*id*/, const struct sockaddr_storage &ad } -bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, uint32_t age) +bool p3BanList::addBanEntry(const RsPeerId &peerId, const struct sockaddr_storage &addr, int level, uint32_t reason, uint32_t age) { RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/ @@ -184,7 +184,7 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_sto } - std::map::iterator it; + std::map::iterator it; it = mBanSources.find(peerId); if (it == mBanSources.end()) { @@ -247,14 +247,14 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_sto int p3BanList::condenseBanSources_locked() { time_t now = time(NULL); - std::string ownId = mLinkMgr->getOwnId(); + RsPeerId ownId = mLinkMgr->getOwnId(); #ifdef DEBUG_BANLIST std::cerr << "p3BanList::condenseBanSources_locked()"; std::cerr << std::endl; #endif - std::map::const_iterator it; + std::map::const_iterator it; for(it = mBanSources.begin(); it != mBanSources.end(); it++) { if (now - it->second.mLastUpdate > RSBANLIST_ENTRY_MAX_AGE) @@ -385,7 +385,7 @@ void p3BanList::sendBanLists() /* we ping our peers */ /* who is online? */ - std::list idList; + std::list idList; mLinkMgr->getOnlineList(idList); @@ -395,7 +395,7 @@ void p3BanList::sendBanLists() #endif /* prepare packets */ - std::list::iterator it; + std::list::iterator it; for(it = idList.begin(); it != idList.end(); it++) { #ifdef DEBUG_BANLIST @@ -408,7 +408,7 @@ void p3BanList::sendBanLists() -int p3BanList::sendBanSet(std::string peerid) +int p3BanList::sendBanSet(const RsPeerId& peerid) { /* */ RsBanListItem *item = new RsBanListItem(); @@ -471,7 +471,7 @@ int p3BanList::printBanSources_locked(std::ostream &out) { time_t now = time(NULL); - std::map::const_iterator it; + std::map::const_iterator it; for(it = mBanSources.begin(); it != mBanSources.end(); it++) { out << "BanList from: " << it->first; diff --git a/libretroshare/src/services/p3bwctrl.cc b/libretroshare/src/services/p3bwctrl.cc index efaa6cc7e..02344a701 100644 --- a/libretroshare/src/services/p3bwctrl.cc +++ b/libretroshare/src/services/p3bwctrl.cc @@ -92,12 +92,12 @@ int p3BandwidthControl::status() bool p3BandwidthControl::checkAvailableBandwidth() { /* check each connection status */ - std::map rateMap; + std::map rateMap; RsBwRates total; mPg->ExtractRates(rateMap, total); - std::map::iterator it; - std::map::iterator bit; + std::map::iterator it; + std::map::iterator bit; /* have to merge with existing list, * erasing as we go ... then any left have to deal with @@ -107,7 +107,7 @@ bool p3BandwidthControl::checkAvailableBandwidth() mTotalRates = total; time_t now = time(NULL); - std::list oldIds; // unused for now! + std::list oldIds; // unused for now! for(bit = mBwMap.begin(); bit != mBwMap.end(); bit++) { @@ -189,7 +189,7 @@ bool p3BandwidthControl::processIncoming() /* For each packet */ RsStackMutex stack(mBwMtx); /****** LOCKED MUTEX *******/ - std::map::iterator bit; + std::map::iterator bit; bit = mBwMap.find(bci->PeerId()); if (bit == mBwMap.end()) @@ -231,11 +231,11 @@ int p3BandwidthControl::getTotalBandwidthRates(RsConfigDataRates &rates) return 1; } -int p3BandwidthControl::getAllBandwidthRates(std::map &ratemap) +int p3BandwidthControl::getAllBandwidthRates(std::map &ratemap) { RsStackMutex stack(mBwMtx); /****** LOCKED MUTEX *******/ - std::map::iterator bit; + std::map::iterator bit; for(bit = mBwMap.begin(); bit != mBwMap.end(); bit++) { RsConfigDataRates rates; @@ -279,7 +279,7 @@ int p3BandwidthControl::printRateInfo_locked(std::ostream &out) out << " MaxOut: " << mTotalRates.mMaxRateOut; out << std::endl; - std::map::iterator bit; + std::map::iterator bit; for(bit = mBwMap.begin(); bit != mBwMap.end(); bit++) { out << "\t" << bit->first; @@ -305,7 +305,7 @@ void p3BandwidthControl::statusChange(const std::list &plist) if (it->actions & RS_PEER_DISCONNECTED) { /* remove from map */ - std::map::iterator bit; + std::map::iterator bit; bit = mBwMap.find(it->id); if (bit == mBwMap.end()) { diff --git a/libretroshare/src/services/p3bwctrl.h b/libretroshare/src/services/p3bwctrl.h index 8f8812bfa..305c29f52 100644 --- a/libretroshare/src/services/p3bwctrl.h +++ b/libretroshare/src/services/p3bwctrl.h @@ -88,7 +88,7 @@ class p3BandwidthControl: public p3Service, public pqiMonitor /***** for RsConfig (not directly overloaded) ****/ virtual int getTotalBandwidthRates(RsConfigDataRates &rates); - virtual int getAllBandwidthRates(std::map &ratemap); + virtual int getAllBandwidthRates(std::map &ratemap); @@ -121,7 +121,7 @@ class p3BandwidthControl: public p3Service, public pqiMonitor time_t mLastCheck; RsBwRates mTotalRates; - std::map mBwMap; + std::map mBwMap; }; diff --git a/libretroshare/src/services/p3discovery2.cc b/libretroshare/src/services/p3discovery2.cc index cb4c08921..c2966cecb 100644 --- a/libretroshare/src/services/p3discovery2.cc +++ b/libretroshare/src/services/p3discovery2.cc @@ -255,7 +255,7 @@ int p3discovery2::handleIncoming() { recvOwnContactInfo(item->PeerId(), contact); } - else if (rsPeers->servicePermissionFlags_sslid(item->PeerId()) & RS_SERVICE_PERM_DISCOVERY) + else if (rsPeers->servicePermissionFlags(item->PeerId()) & RS_SERVICE_PERM_DISCOVERY) { processContactInfo(item->PeerId(), contact); } @@ -268,7 +268,7 @@ int p3discovery2::handleIncoming() } /* any other packets should be dropped if they don't have permission */ - if(!(rsPeers->servicePermissionFlags_sslid(item->PeerId()) & RS_SERVICE_PERM_DISCOVERY)) + if(!(rsPeers->servicePermissionFlags(item->PeerId()) & RS_SERVICE_PERM_DISCOVERY)) { delete item; continue; @@ -773,7 +773,7 @@ void p3discovery2::sendContactInfo_locked(const PGPID &aboutId, const SSLID &toI std::cerr << "p3discovery2::sendContactInfo_locked() aboutPGPId: " << aboutId << " toId: " << toId; std::cerr << std::endl; #endif - if (!(rsPeers->servicePermissionFlags_sslid(toId) & RS_SERVICE_PERM_DISCOVERY)) + if (!(rsPeers->servicePermissionFlags(toId) & RS_SERVICE_PERM_DISCOVERY)) { #ifdef P3DISC_DEBUG std::cerr << "p3discovery2::sendContactInfo_locked() discovery disabled for SSLID: " << toId; @@ -961,7 +961,7 @@ void p3discovery2::recvPGPCertificateRequest(const SSLID &fromId, const RsDiscPg std::cerr << std::endl; #endif - std::list::const_iterator it; + std::list::const_iterator it; for(it = item->pgpIdSet.ids.begin(); it != item->pgpIdSet.ids.end(); it++) { // NB: This doesn't include own certificates? why not. @@ -1084,7 +1084,7 @@ void p3discovery2::statusChange(const std::list &plist) /*************************************************************************************/ /* Extracting Network Graph Details */ /*************************************************************************************/ -bool p3discovery2::getDiscFriends(const std::string& id, std::list &proxyIds) +bool p3discovery2::getDiscFriends(const RsPeerId& id, std::list &proxyIds) { // This is treated appart, because otherwise we don't receive any disc info about us if(id == rsPeers->getOwnId()) // SSL id diff --git a/libretroshare/src/services/p3discovery2.h b/libretroshare/src/services/p3discovery2.h index 2b3984890..b6e27326f 100644 --- a/libretroshare/src/services/p3discovery2.h +++ b/libretroshare/src/services/p3discovery2.h @@ -41,8 +41,8 @@ -typedef std::string PGPID; -typedef std::string SSLID; +typedef PGPIdType PGPID; +typedef SSLIdType SSLID; class DiscSslInfo { @@ -88,9 +88,9 @@ virtual ~p3discovery2(); int tick(); /* external interface */ -virtual bool getDiscFriends(const std::string &id, std::list &friends); -virtual bool getDiscPgpFriends(const std::string &pgpid, std::list &gpg_friends); -virtual bool getPeerVersion(const std::string &id, std::string &version); +virtual bool getDiscFriends(const RsPeerId &id, std::list &friends); +virtual bool getDiscPgpFriends(const PGPIdType &pgpid, std::list &gpg_friends); +virtual bool getPeerVersion(const RsPeerId &id, std::string &version); virtual bool getWaitingDiscCount(unsigned int *sendCount, unsigned int *recvCount); /************* from AuthGPService ****************/ diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index eac572fbe..4e2a42591 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -90,8 +90,8 @@ p3MsgService::p3MsgService(p3LinkMgr *lm) #ifdef GROUTER // Debug stuff. Create a random key and register it. - std::string own_ssl_id = rsPeers->getOwnId() ; - std::string gpg_id = rsPeers->getGPGOwnId() ; + const RsPeerId& own_ssl_id = rsPeers->getOwnId() ; + const PGPIdType& gpg_id = rsPeers->getGPGOwnId() ; RsPeerDetails d; rsPeers->getPeerDetails(gpg_id,d) ; @@ -276,6 +276,22 @@ void p3MsgService::checkSizeAndSendMessage(RsMsgItem *msg) static const uint32_t MAX_STRING_SIZE = 15000 ; std::cerr << "Msg is size " << msg->message.size() << std::endl; + Sha1CheckSum hash ; + + if( msg->msgFlags & RS_MSG_FLAGS_DISTANT ) + { + RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ + +#ifdef DEBUG_DISTANT_MSG + std::cerr << "checkOutgoingMessages(): removing pending message flag for hash " << (*it)->PeerId() << "." << std::endl; +#endif + if(!locked_findHashForVirtualPeerId(msg->PeerId(),hash)) + { + std::cerr << "p3MsgService::checkSizeAndSendMessage(): Cannot find hash for virtual peer id " << msg->PeerId() << ". This is unexpected!" << std::endl; + return ; + } + _messenging_contacts[hash].pending_messages = false ; + } while(msg->message.size() > MAX_STRING_SIZE) { @@ -295,7 +311,11 @@ void p3MsgService::checkSizeAndSendMessage(RsMsgItem *msg) item->msgFlags |= RS_MSG_FLAGS_PARTIAL ; if(msg->msgFlags & RS_MSG_FLAGS_DISTANT) - sendPrivateMsgItem(item) ; + { + + + sendPrivateMsgItem(hash,item) ; + } else sendItem(item) ; } @@ -304,7 +324,7 @@ void p3MsgService::checkSizeAndSendMessage(RsMsgItem *msg) #endif if(msg->msgFlags & RS_MSG_FLAGS_DISTANT) - sendPrivateMsgItem(msg) ; + sendPrivateMsgItem(hash,msg) ; else sendItem(msg) ; } @@ -344,29 +364,29 @@ int p3MsgService::checkOutgoingMessages() #ifdef GROUTER tunnel_is_ok = true ; #else - const RsPeerId& hash = mit->second->PeerId() ; - std::map::iterator it = _messenging_contacts.find(hash) ; - - if(it != _messenging_contacts.end()) - { - tunnel_is_ok = (it->second.status == RS_DISTANT_MSG_STATUS_TUNNEL_OK) ; -#ifdef DEBUG_DISTANT_MSG - std::cerr << "checkOutGoingMessages(): distant contact found. tunnel_is_ok = " << tunnel_is_ok << std::endl; -#endif - } - else - { -#ifdef DEBUG_DISTANT_MSG - std::cerr << "checkOutGoingMessages(): distant contact not found. Asking for tunnels for hash " << hash << std::endl; -#endif - // no. Ask for monitoring tunnels. - rsTurtle->monitorTunnels(hash,this) ; - tunnel_is_ok = false ; - - DistantMessengingContact& contact( _messenging_contacts[hash] ) ; - contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_DN ; - contact.pending_messages = true ; - } +// const RsPeerId& hash = mit->second->PeerId() ; +// std::map::iterator it = _messenging_contacts.find(hash) ; +// +// if(it != _messenging_contacts.end()) +// { +// tunnel_is_ok = (it->second.status == RS_DISTANT_MSG_STATUS_TUNNEL_OK) ; +//#ifdef DEBUG_DISTANT_MSG +// std::cerr << "checkOutGoingMessages(): distant contact found. tunnel_is_ok = " << tunnel_is_ok << std::endl; +//#endif +// } +// else +// { +//#ifdef DEBUG_DISTANT_MSG +// std::cerr << "checkOutGoingMessages(): distant contact not found. Asking for tunnels for hash " << hash << std::endl; +//#endif +// // no. Ask for monitoring tunnels. +// rsTurtle->monitorTunnels(hash,this) ; +// tunnel_is_ok = false ; +// +// DistantMessengingContact& contact( _messenging_contacts[hash] ) ; +// contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_DN ; +// contact.pending_messages = true ; +// } #endif } @@ -413,20 +433,8 @@ int p3MsgService::checkOutgoingMessages() } for(std::list::const_iterator it(output_queue.begin());it!=output_queue.end();++it) - { checkSizeAndSendMessage(*it) ; - if( (*it)->msgFlags & RS_MSG_FLAGS_DISTANT ) - { - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - - _messenging_contacts[(*it)->PeerId()].pending_messages = false ; -#ifdef DEBUG_DISTANT_MSG - std::cerr << "checkOutgoingMessages(): removing pending message flag for hash " << (*it)->PeerId() << "." << std::endl; -#endif - } - } - if(changed) RsServer::notify()->notifyListChange(NOTIFY_LIST_MESSAGELIST,NOTIFY_TYPE_MOD); @@ -467,14 +475,14 @@ bool p3MsgService::saveList(bool& cleanup, std::list& itemList) for(mit4 = mParentId.begin(); mit4 != mParentId.end(); mit4++) itemList.push_back(mit4->second); - for(std::map::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) - { - RsPublicMsgInviteConfigItem *item = new RsPublicMsgInviteConfigItem ; - item->hash = it->first ; - item->time_stamp = it->second.time_of_validity ; - - itemList.push_back(item) ; - } +// for(std::map::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) +// { +// RsPublicMsgInviteConfigItem *item = new RsPublicMsgInviteConfigItem ; +// item->hash = it->first ; +// item->time_stamp = it->second.time_of_validity ; +// +// itemList.push_back(item) ; +// } RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet ; RsTlvKeyValue kv; kv.key = "DISTANT_MESSAGES_ENABLED" ; @@ -550,7 +558,7 @@ bool p3MsgService::loadList(std::list& load) RsMsgTags* mti; RsMsgSrcId* msi; RsMsgParentId* msp; - RsPublicMsgInviteConfigItem* msv; +// RsPublicMsgInviteConfigItem* msv; std::list items; std::list::iterator it; @@ -600,10 +608,10 @@ bool p3MsgService::loadList(std::list& load) { mParentId.insert(std::pair(msp->msgId, msp)); } - else if(NULL != (msv = dynamic_cast(*it))) - { - _messenging_invites[msv->hash].time_of_validity = msv->time_stamp ; - } +// else if(NULL != (msv = dynamic_cast(*it))) +// { +// _messenging_invites[msv->hash].time_of_validity = msv->time_stamp ; +// } RsConfigKeyValueSet *vitem = NULL ; @@ -1082,16 +1090,16 @@ bool p3MsgService::MessageSend(MessageInfo &info) RsMsgItem *msg = initMIRsMsg(info, mLinkMgr->getOwnId()); if (msg) { - std::list::iterator it ; + std::list::iterator it ; // Update destination ids in place of distant message hash, since this Outgoing message is for display // - for(it = msg->msgbcc.ids.begin(); it != msg->msgbcc.ids.end(); it++) - if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; - for(it = msg->msgcc.ids.begin(); it != msg->msgcc.ids.end(); it++) - if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; - for(it = msg->msgto.ids.begin(); it != msg->msgto.ids.end(); it++) - if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; +// for(it = msg->msgbcc.ids.begin(); it != msg->msgbcc.ids.end(); it++) +// if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; +// for(it = msg->msgcc.ids.begin(); it != msg->msgcc.ids.end(); it++) +// if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; +// for(it = msg->msgto.ids.begin(); it != msg->msgto.ids.end(); it++) +// if(info.encryption_keys.find(*it) != info.encryption_keys.end()) *it = info.encryption_keys[*it] ; if (msg->msgFlags & RS_MSG_FLAGS_SIGNED) msg->msgFlags |= RS_MSG_FLAGS_SIGNATURE_CHECKS; // this is always true, since we are sending the message @@ -1774,15 +1782,17 @@ RsMsgItem *p3MsgService::initMIRsMsg(MessageInfo &info, const RsPeerId &to) // all its content. // - if(info.encryption_keys.find(to) != info.encryption_keys.end()) - encryptMessage(info.encryption_keys[to],msg) ; + if(!info.encryption_keys.empty()) + std::cerr << "(WW) Cannot encrypt message. Code needs to be improved." << std::endl; +// if(info.encryption_keys.find(to) != info.encryption_keys.end()) +// encryptMessage(info.encryption_keys[to],msg) ; //std::cerr << "p3MsgService::initMIRsMsg()" << std::endl; //msg->print(std::cerr); return msg; } -bool p3MsgService::encryptMessage(const std::string& pgp_id,RsMsgItem *item) +bool p3MsgService::encryptMessage(const PGPIdType& pgp_id,RsMsgItem *item) { #ifdef DEBUG_DISTANT_MSG std::cerr << "Encrypting message with public key " << pgp_id << " in place." << std::endl; @@ -2009,7 +2019,7 @@ bool p3MsgService::decryptMessage(const std::string& mId) std::cerr << " Fingerprint = " << fingerprint.toStdString() << std::endl; signature_present = true ; - signature_ok = AuthGPG::getAuthGPG()->VerifySignBin(decrypted_data, 1+PGP_KEY_ID_SIZE+item_size, &decrypted_data[1+PGP_KEY_ID_SIZE+item_size], decrypted_size - PGP_KEY_ID_SIZE - item_size - 1, fingerprint.toStdString()) ; + signature_ok = AuthGPG::getAuthGPG()->VerifySignBin(decrypted_data, 1+PGP_KEY_ID_SIZE+item_size, &decrypted_data[1+PGP_KEY_ID_SIZE+item_size], decrypted_size - PGP_KEY_ID_SIZE - item_size - 1, fingerprint) ; } else if(1 + item_size + PGP_KEY_ID_SIZE == decrypted_size) std::cerr << " No signature in this packet" << std::endl; @@ -2027,8 +2037,8 @@ bool p3MsgService::decryptMessage(const std::string& mId) std::cerr << " Decrypted message was succesfully deserialized. New message:" << std::endl; item->print(std::cerr,0) ; #endif - std::string own_hash ; - const PGPIdType& own_pgp_id = AuthGPG::getAuthGPG()->getGPGOwnId(); + Sha1CheckSum own_hash ; + const PGPIdType& own_pgp_id = AuthGPG::getAuthGPG()->getGPGOwnId(); getDistantMessageHash(own_pgp_id,own_hash) ; { @@ -2041,11 +2051,12 @@ bool p3MsgService::decryptMessage(const std::string& mId) msgi.msgId = msgId ; // restore the correct message id, to make it consistent msgi.msgFlags &= ~RS_MSG_FLAGS_ENCRYPTED ; // just in case. msgi.msgFlags |= RS_MSG_FLAGS_DECRYPTED ; // previousy encrypted msg is now decrypted - msgi.PeerId(senders_id.toStdString()) ; - - for(std::list::iterator it(msgi.msgto.ids.begin());it!=msgi.msgto.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; - for(std::list::iterator it(msgi.msgcc.ids.begin());it!=msgi.msgcc.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; - for(std::list::iterator it(msgi.msgbcc.ids.begin());it!=msgi.msgbcc.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; + +// NEEDS TO BE DONE +// msgi.PeerId(senders_id) ; +// for(std::list::iterator it(msgi.msgto.ids.begin());it!=msgi.msgto.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; +// for(std::list::iterator it(msgi.msgcc.ids.begin());it!=msgi.msgcc.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; +// for(std::list::iterator it(msgi.msgbcc.ids.begin());it!=msgi.msgbcc.ids.end();++it) if(*it == own_hash) *it = own_pgp_id ; if(signature_present) { @@ -2065,14 +2076,16 @@ bool p3MsgService::decryptMessage(const std::string& mId) RsMsgSrcId* msi = new RsMsgSrcId(); msi->msgId = msgi.msgId; - msi->srcId = senders_id ; + // NEEDS TO BE DONE + // msi->srcId = senders_id ; mSrcIds.insert(std::pair(msi->msgId, msi)); } else { - std::cerr << "Substituting source name for message id " << msgi.msgId << ": " << it->second->srcId << " -> " << senders_id << std::endl; - it->second->srcId = senders_id ; + std::cerr << "Substituting source name for message id " << msgi.msgId << ": " << it->second->srcId << " -> " << senders_id << std::endl; + // NEEDS TO BE DONE + //it->second->srcId = senders_id ; } } delete item ; @@ -2098,17 +2111,14 @@ void p3MsgService::connectToTurtleRouter(p3turtle *pt) bool p3MsgService::createDistantOfflineMessengingInvite(time_t time_of_validity,TurtleFileHash& hash) { - unsigned char hash_bytes[DISTANT_MSG_HASH_SIZE] ; - RSRandom::random_bytes( hash_bytes, DISTANT_MSG_HASH_SIZE) ; - - hash = Sha1CheckSum(hash_bytes).toStdString() ; + hash = Sha1CheckSum::random().toStdString() ; DistantMessengingInvite invite ; invite.time_of_validity = time_of_validity + time(NULL); { RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - _messenging_invites[hash] = invite ; + _messenging_invites[Sha1CheckSum(hash)] = invite ; } IndicateConfigChanged() ; @@ -2119,7 +2129,7 @@ void p3MsgService::enableDistantMessaging(bool b) { // compute the hash - std::string hash ; + Sha1CheckSum hash ; if(!getDistantMessageHash(AuthGPG::getAuthGPG()->getGPGOwnId(),hash)) return ; @@ -2132,7 +2142,7 @@ void p3MsgService::enableDistantMessaging(bool b) { RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - std::map::iterator it = _messenging_invites.find(hash) ; + std::map::iterator it = _messenging_invites.find(hash) ; if(b && it == _messenging_invites.end()) { @@ -2144,8 +2154,8 @@ void p3MsgService::enableDistantMessaging(bool b) std::cerr << "Notifying the global router." << std::endl; // Debug stuff. Create a random key and register it. - std::string own_ssl_id = rsPeers->getOwnId() ; - std::string gpg_id = rsPeers->getGPGOwnId() ; + const RsPeerId& own_ssl_id = rsPeers->getOwnId() ; + const PGPIdType& gpg_id = rsPeers->getGPGOwnId() ; RsPeerDetails d; rsPeers->getPeerDetails(gpg_id,d) ; @@ -2178,7 +2188,7 @@ void p3MsgService::enableDistantMessaging(bool b) #ifdef DEBUG_DISTANT_MSG std::cerr << "List of distant message invites: " << std::endl; - for(std::map::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) + for(std::map::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) std::cerr << " hash = " << it->first << std::endl; #endif } @@ -2186,7 +2196,7 @@ bool p3MsgService::distantMessagingEnabled() { // compute the hash - std::string hash ; + Sha1CheckSum hash ; if(!getDistantMessageHash(AuthGPG::getAuthGPG()->getGPGOwnId(),hash)) return false; @@ -2200,13 +2210,7 @@ bool p3MsgService::distantMessagingEnabled() } bool p3MsgService::getDistantMessageHash(const PGPIdType& pgp_id,Sha1CheckSum& hash) { - if(pgp_id.length() != 16) - { - std::cerr << "pgp id \"" << pgp_id << "\" is not valid! Something definitly wrong." << std::endl; - return false; - } - - hash = RsDirUtil::sha1sum((uint8_t*)pgp_id.c_str(),16).toStdString() ; + hash = RsDirUtil::sha1sum((uint8_t*)pgp_id.toByteArray(),PGPIdType::SIZE_IN_BYTES); // Also check that we have the public key. @@ -2217,7 +2221,7 @@ bool p3MsgService::getDistantOfflineMessengingInvites(std::vector::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) + for(std::map::const_iterator it(_messenging_invites.begin());it!=_messenging_invites.end();++it) { DistantOfflineMessengingInvite invite ; invite.hash = it->first ; @@ -2263,13 +2267,13 @@ void p3MsgService::manageDistantPeers() // clean dead invites. // - for(std::map::iterator it(_messenging_invites.begin());it!=_messenging_invites.end();) + for(std::map::iterator it(_messenging_invites.begin());it!=_messenging_invites.end();) if(it->second.time_of_validity < now) { #ifdef DEBUG_DISTANT_MSG std::cerr << " Removing outdated invite " << it->second.time_of_validity << ", hash=" << it->first << std::endl; #endif - std::map::iterator tmp(it) ; + std::map::iterator tmp(it) ; ++tmp ; _messenging_invites.erase(it) ; it = tmp ; @@ -2277,68 +2281,68 @@ void p3MsgService::manageDistantPeers() else ++it ; - // clean dead contacts. - // - for(std::map::iterator it(_messenging_contacts.begin());it!=_messenging_contacts.end();) - if((!it->second.pending_messages) && it->second.status == RS_DISTANT_MSG_STATUS_TUNNEL_DN) - { -#ifdef DEBUG_DISTANT_MSG - std::cerr << " Removing dead contact with no pending msgs and dead tunnel. hash=" << it->first << std::endl; -#endif - std::map::iterator tmp(it) ; - ++tmp ; - _messenging_contacts.erase(it) ; - it = tmp ; - } - else - ++it ; +// // clean dead contacts. +// // +// for(std::map::iterator it(_messenging_contacts.begin());it!=_messenging_contacts.end();) +// if((!it->second.pending_messages) && it->second.status == RS_DISTANT_MSG_STATUS_TUNNEL_DN) +// { +//#ifdef DEBUG_DISTANT_MSG +// std::cerr << " Removing dead contact with no pending msgs and dead tunnel. hash=" << it->first << std::endl; +//#endif +// std::map::iterator tmp(it) ; +// ++tmp ; +// _messenging_contacts.erase(it) ; +// it = tmp ; +// } +// else +// ++it ; } } -void p3MsgService::addVirtualPeer(const TurtleFileHash& hash, const TurtleVirtualPeerId& vpid,RsTurtleGenericTunnelItem::Direction /*dir*/) -{ - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - // A new tunnel has been created. We need to flush pending messages for the corresponding peer. - - //std::map::const_iterator it = _messenging_contacts.find(hash) ; - - DistantMessengingContact& contact(_messenging_contacts[hash]) ; // possibly creates it. - - contact.virtual_peer_id = vpid ; - contact.last_hit_time = time(NULL) ; - contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_OK ; - -#ifdef DEBUG_DISTANT_MSG - std::cerr << "p3MsgService::addVirtualPeer(): adding virtual peer " << vpid << " for hash " << hash << std::endl; -#endif -} -void p3MsgService::removeVirtualPeer(const TurtleFileHash& hash, const TurtleVirtualPeerId& vpid) -{ -#ifdef DEBUG_DISTANT_MSG - std::cerr << "Removing virtual peer " << vpid << " for hash " << hash << std::endl; -#endif - - bool remove_tunnel = false ; - { - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - - DistantMessengingContact& contact(_messenging_contacts[hash]) ; // possibly creates it. - - contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_DN ; - contact.virtual_peer_id.clear() ; - - if(!contact.pending_messages) - remove_tunnel = true ; - } - - if(remove_tunnel) // We do that whenever we're client or server. But normally, we should only do it when we're client. - { -#ifdef DEBUG_DISTANT_MSG - std::cerr << "Also removing tunnel, since pending messages have been sent." << std::endl; -#endif - mTurtle->stopMonitoringTunnels(hash) ; - } -} +// void p3MsgService::addVirtualPeer(const TurtleFileHash& hash, const TurtleVirtualPeerId& vpid,RsTurtleGenericTunnelItem::Direction /*dir*/) +// { +// RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ +// // A new tunnel has been created. We need to flush pending messages for the corresponding peer. +// +// //std::map::const_iterator it = _messenging_contacts.find(hash) ; +// +// DistantMessengingContact& contact(_messenging_contacts[hash]) ; // possibly creates it. +// +// contact.virtual_peer_id = vpid ; +// contact.last_hit_time = time(NULL) ; +// contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_OK ; +// +// #ifdef DEBUG_DISTANT_MSG +// std::cerr << "p3MsgService::addVirtualPeer(): adding virtual peer " << vpid << " for hash " << hash << std::endl; +// #endif +// } +// void p3MsgService::removeVirtualPeer(const TurtleFileHash& hash, const TurtleVirtualPeerId& vpid) +// { +// #ifdef DEBUG_DISTANT_MSG +// std::cerr << "Removing virtual peer " << vpid << " for hash " << hash << std::endl; +// #endif +// +// bool remove_tunnel = false ; +// { +// RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ +// +// DistantMessengingContact& contact(_messenging_contacts[hash]) ; // possibly creates it. +// +// contact.status = RS_DISTANT_MSG_STATUS_TUNNEL_DN ; +// contact.virtual_peer_id.clear() ; +// +// if(!contact.pending_messages) +// remove_tunnel = true ; +// } +// +// if(remove_tunnel) // We do that whenever we're client or server. But normally, we should only do it when we're client. +// { +// #ifdef DEBUG_DISTANT_MSG +// std::cerr << "Also removing tunnel, since pending messages have been sent." << std::endl; +// #endif +// mTurtle->stopMonitoringTunnels(hash) ; +// } +// } #ifdef DEBUG_DISTANT_MSG static void printBinaryData(void *data,uint32_t size) @@ -2353,7 +2357,7 @@ static void printBinaryData(void *data,uint32_t size) } #endif -void p3MsgService::sendTurtleData(const std::string& hash,RsMsgItem *msgitem) +void p3MsgService::sendTurtleData(const Sha1CheckSum& hash,RsMsgItem *msgitem) { // The item is serialized and turned into a generic turtle item. @@ -2386,7 +2390,7 @@ void p3MsgService::sendTurtleData(const std::string& hash,RsMsgItem *msgitem) RsPeerId virtual_peer_id ; { RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - std::map::const_iterator it = _messenging_contacts.find(hash) ; + std::map::const_iterator it = _messenging_contacts.find(hash) ; if(it == _messenging_contacts.end()) { @@ -2410,63 +2414,63 @@ void p3MsgService::sendTurtleData(const std::string& hash,RsMsgItem *msgitem) mTurtle->sendTurtleData(virtual_peer_id,item) ; } -void p3MsgService::receiveTurtleData(RsTurtleGenericTunnelItem *gitem,const std::string& hash, - const std::string& virtual_peer_id,RsTurtleGenericTunnelItem::Direction /*direction*/) -{ - RsTurtleGenericDataItem *item = dynamic_cast(gitem) ; - - if(item == NULL) - { - std::cerr << "(EE) p3MsgService::receiveTurtleData(): item is not a data item. That is an error." << std::endl; - return ; - } -#ifdef DISABLE_DISTANT_MESSAGES - std::cerr << "Received distant message item. Protocol is not yet finalized. Droping the item." << std::endl; - delete item ; - return ; -#endif - -#ifdef DEBUG_DISTANT_MSG - std::cerr << "p3MsgService::sendTurtleData(): Receiving through virtual peer: " << virtual_peer_id << std::endl; - std::cerr << " gitem->data_size = " << item->data_size << std::endl; - std::cerr << " data = " ; - printBinaryData(item->data_bytes,item->data_size) ; - std::cerr << std::endl; -#else - (void) virtual_peer_id; -#endif - - { - RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ - std::map::iterator it = _messenging_contacts.find(hash) ; - - if(it == _messenging_contacts.end()) - { - std::cerr << "(EE) p3MsgService::sendTurtleData(): Can't find hash " << hash << " in recorded contact list." << std::endl; - return ; - } - - it->second.status = RS_DISTANT_MSG_STATUS_TUNNEL_OK ; - it->second.last_hit_time = time(NULL) ; - } - - RsItem *itm = _serialiser->deserialise(item->data_bytes,&item->data_size) ; - RsMsgItem *mitm = dynamic_cast(itm) ; - - if(mitm != NULL) - { - mitm->PeerId(hash) ; - mitm->msgto.ids.push_back(rsPeers->getGPGOwnId()) ; - handleIncomingItem(mitm) ; - } - else - { - std::cerr << "(EE) p3MsgService::receiveTurtleData(): received item is not a RsMsgItem!!" << std::endl; - delete itm ; - } -} +//void p3MsgService::receiveTurtleData(RsTurtleGenericTunnelItem *gitem,const Sha1CheckSum& hash, +// const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction /*direction*/) +//{ +// RsTurtleGenericDataItem *item = dynamic_cast(gitem) ; +// +// if(item == NULL) +// { +// std::cerr << "(EE) p3MsgService::receiveTurtleData(): item is not a data item. That is an error." << std::endl; +// return ; +// } +//#ifdef DISABLE_DISTANT_MESSAGES +// std::cerr << "Received distant message item. Protocol is not yet finalized. Droping the item." << std::endl; +// delete item ; +// return ; +//#endif +// +//#ifdef DEBUG_DISTANT_MSG +// std::cerr << "p3MsgService::sendTurtleData(): Receiving through virtual peer: " << virtual_peer_id << std::endl; +// std::cerr << " gitem->data_size = " << item->data_size << std::endl; +// std::cerr << " data = " ; +// printBinaryData(item->data_bytes,item->data_size) ; +// std::cerr << std::endl; +//#else +// (void) virtual_peer_id; +//#endif +// +// { +// RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/ +// std::map::iterator it = _messenging_contacts.find(hash) ; +// +// if(it == _messenging_contacts.end()) +// { +// std::cerr << "(EE) p3MsgService::sendTurtleData(): Can't find hash " << hash << " in recorded contact list." << std::endl; +// return ; +// } +// +// it->second.status = RS_DISTANT_MSG_STATUS_TUNNEL_OK ; +// it->second.last_hit_time = time(NULL) ; +// } +// +// RsItem *itm = _serialiser->deserialise(item->data_bytes,&item->data_size) ; +// RsMsgItem *mitm = dynamic_cast(itm) ; +// +// if(mitm != NULL) +// { +// mitm->PeerId(hash) ; +// mitm->msgto.ids.push_back(rsPeers->getGPGOwnId()) ; +// handleIncomingItem(mitm) ; +// } +// else +// { +// std::cerr << "(EE) p3MsgService::receiveTurtleData(): received item is not a RsMsgItem!!" << std::endl; +// delete itm ; +// } +//} #ifdef GROUTER -void p3MsgService::sendGRouterData(const std::string& hash,RsMsgItem *msgitem) +void p3MsgService::sendGRouterData(const Sha1CheckSum& hash,RsMsgItem *msgitem) { // The item is serialized and turned into a generic turtle item. @@ -2496,7 +2500,18 @@ void p3MsgService::receiveGRouterData(RsGRouterGenericDataItem *gitem,const GRou } #endif -void p3MsgService::sendPrivateMsgItem(RsMsgItem *msgitem) +bool p3MsgService::locked_findHashForVirtualPeerId(const RsPeerId& vpid,Sha1CheckSum& hash) +{ + for(std::map::const_iterator it = _messenging_contacts.begin(); it!=_messenging_contacts.end();++it) + if(it->second.virtual_peer_id == vpid) + { + hash = it->first ; + return true ; + } + return false ; +} + +void p3MsgService::sendPrivateMsgItem(const Sha1CheckSum& hash,RsMsgItem *msgitem) { #ifdef DEBUG_DISTANT_MSG std::cerr << "p3MsgService::sendDistanteMsgItem(): sending distant msg item to peer " << msgitem->PeerId() << std::endl; @@ -2506,7 +2521,7 @@ void p3MsgService::sendPrivateMsgItem(RsMsgItem *msgitem) // allocate a new contact. If it does not exist, set its tunnel state to DN // - std::map::iterator it = _messenging_contacts.find(msgitem->PeerId()) ; + std::map::iterator it = _messenging_contacts.find(hash) ; if(it == _messenging_contacts.end()) { @@ -2528,9 +2543,9 @@ void p3MsgService::sendPrivateMsgItem(RsMsgItem *msgitem) std::cerr << " Flushing msg " << msgitem->msgId << " for peer id " << msgitem->PeerId() << std::endl; #endif #ifdef GROUTER - sendGRouterData(msgitem->PeerId(),msgitem) ; + sendGRouterData(hash,msgitem) ; #else - sendTurtleData(msgitem->PeerId(),msgitem) ; + sendTurtleData(hash,msgitem) ; #endif } diff --git a/libretroshare/src/services/p3msgservice.h b/libretroshare/src/services/p3msgservice.h index 82e7ff199..b7a2b2a26 100644 --- a/libretroshare/src/services/p3msgservice.h +++ b/libretroshare/src/services/p3msgservice.h @@ -134,16 +134,16 @@ int checkOutgoingMessages(); bool distantMessagingEnabled() ; bool getDistantMessageHash(const PGPIdType& pgp_id,Sha1CheckSum &hash) ; - void sendPrivateMsgItem(RsMsgItem *) ; - private: + void sendPrivateMsgItem(const Sha1CheckSum& hash,RsMsgItem *) ; + // This maps contains the current invitations to respond to. - // The map is indexed by the hash - std::map _messenging_invites ; + // The map is indexed by the hash + std::map _messenging_invites ; // This contains the ongoing tunnel handling contacts. - // The map is indexed by the hash - std::map _messenging_contacts ; + // The map is indexed by the hash + std::map _messenging_contacts ; // Overloaded from RsTurtleClientService @@ -151,18 +151,19 @@ int checkOutgoingMessages(); virtual void receiveGRouterData(RsGRouterGenericDataItem *item, const GRouterKeyId& key) ; #endif virtual bool handleTunnelRequest(const Sha1CheckSum& hash,const RsPeerId& peer_id) ; - virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const std::string& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ; + virtual void receiveTurtleData(RsTurtleGenericTunnelItem *item,const Sha1CheckSum& hash,const RsPeerId& virtual_peer_id,RsTurtleGenericTunnelItem::Direction direction) ; void addVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&,RsTurtleGenericTunnelItem::Direction dir) ; void removeVirtualPeer(const TurtleFileHash&, const TurtleVirtualPeerId&) ; // Utility functions bool encryptMessage(const PGPIdType& pgp_id,RsMsgItem *msg) ; + bool locked_findHashForVirtualPeerId(const RsPeerId& pid,Sha1CheckSum& hash) ; void manageDistantPeers() ; - void sendTurtleData(const std::string& hash,RsMsgItem *) ; + void sendTurtleData(const Sha1CheckSum& hash,RsMsgItem *) ; #ifdef GROUTER - void sendGRouterData(const std::string& hash,RsMsgItem *) ; + void sendGRouterData(const Sha1CheckSum& hash,RsMsgItem *) ; #endif void handleIncomingItem(RsMsgItem *) ; diff --git a/libretroshare/src/services/p3statusservice.cc b/libretroshare/src/services/p3statusservice.cc index b7ae85c01..89fd69f9d 100644 --- a/libretroshare/src/services/p3statusservice.cc +++ b/libretroshare/src/services/p3statusservice.cc @@ -64,8 +64,8 @@ bool p3StatusService::getOwnStatus(StatusInfo& statusInfo) std::cerr << "p3StatusService::getOwnStatus() " << std::endl; #endif - std::map::iterator it; - std::string ownId = mLinkMgr->getOwnId(); + std::map::iterator it; + const RsPeerId& ownId = mLinkMgr->getOwnId(); RsStackMutex stack(mStatusMtx); it = mStatusInfoMap.find(ownId); @@ -77,7 +77,7 @@ bool p3StatusService::getOwnStatus(StatusInfo& statusInfo) statusInfo.id = ownId; statusInfo.status = RS_STATUS_ONLINE; - std::pair pr(ownId, statusInfo); + std::pair pr(ownId, statusInfo); mStatusInfoMap.insert(pr); IndicateConfigChanged(); @@ -100,7 +100,7 @@ bool p3StatusService::getStatusList(std::list& statusInfo) RsStackMutex stack(mStatusMtx); // fill up statusInfo list with this information - std::map::iterator mit; + std::map::iterator mit; for(mit = mStatusInfoMap.begin(); mit != mStatusInfoMap.end(); mit++){ statusInfo.push_back(mit->second); } @@ -108,7 +108,7 @@ bool p3StatusService::getStatusList(std::list& statusInfo) return true; } -bool p3StatusService::getStatus(const std::string &id, StatusInfo &statusInfo) +bool p3StatusService::getStatus(const RsPeerId &id, StatusInfo &statusInfo) { #ifdef STATUS_DEBUG std::cerr << "p3StatusService::getStatus() " << std::endl; @@ -116,7 +116,7 @@ bool p3StatusService::getStatus(const std::string &id, StatusInfo &statusInfo) RsStackMutex stack(mStatusMtx); - std::map::iterator mit = mStatusInfoMap.find(id); + std::map::iterator mit = mStatusInfoMap.find(id); if (mit == mStatusInfoMap.end()) { /* return fake status info as offline */ @@ -133,10 +133,10 @@ bool p3StatusService::getStatus(const std::string &id, StatusInfo &statusInfo) } /* id = "", status is sent to all online peers */ -bool p3StatusService::sendStatus(const std::string &id, uint32_t status) +bool p3StatusService::sendStatus(const RsPeerId &id, uint32_t status) { StatusInfo statusInfo; - std::list onlineList; + std::list onlineList; { RsStackMutex stack(mStatusMtx); @@ -150,7 +150,7 @@ bool p3StatusService::sendStatus(const std::string &id, uint32_t status) // If your id is not set, set it if(mStatusInfoMap.find(statusInfo.id) == mStatusInfoMap.end()){ - std::pair pr(statusInfo.id, statusInfo); + std::pair pr(statusInfo.id, statusInfo); mStatusInfoMap.insert(pr); IndicateConfigChanged(); } else if(mStatusInfoMap[statusInfo.id].status != statusInfo.status){ @@ -160,14 +160,14 @@ bool p3StatusService::sendStatus(const std::string &id, uint32_t status) } } - if (id.empty()) { + if (id.isNull()) { mLinkMgr->getOnlineList(onlineList); } else { onlineList.push_back(id); } } - std::list::iterator it; + std::list::iterator it; #ifdef STATUS_DEBUG std::cerr << "p3StatusService::sendStatus() " << std::endl; @@ -184,7 +184,7 @@ bool p3StatusService::sendStatus(const std::string &id, uint32_t status) } /* send notify of own status change */ - RsServer::notify()->notifyPeerStatusChanged(statusInfo.id, statusInfo.status); + RsServer::notify()->notifyPeerStatusChanged(statusInfo.id.toStdString(), statusInfo.status); return true; } @@ -193,7 +193,7 @@ bool p3StatusService::sendStatus(const std::string &id, uint32_t status) void p3StatusService::receiveStatusQueue() { - std::map changed; + std::map changed; { RsStackMutex stack(mStatusMtx); @@ -217,7 +217,7 @@ void p3StatusService::receiveStatusQueue() std::cerr << "Got status Item" << std::endl; #endif - std::map::iterator mit = mStatusInfoMap.find(status_item->PeerId()); + std::map::iterator mit = mStatusInfoMap.find(status_item->PeerId()); if(mit != mStatusInfoMap.end()){ mit->second.id = status_item->PeerId(); @@ -241,9 +241,9 @@ void p3StatusService::receiveStatusQueue() } /* UNLOCKED */ if (changed.size()) { - std::map::iterator it; + std::map::iterator it; for (it = changed.begin(); it != changed.end(); it++) { - RsServer::notify()->notifyPeerStatusChanged(it->first, it->second); + RsServer::notify()->notifyPeerStatusChanged(it->first.toStdString(), it->second); } RsServer::notify()->notifyPeerStatusChangedSummary(); } @@ -266,7 +266,7 @@ bool p3StatusService::saveList(bool& cleanup, std::list& ilist){ RsStatusItem* own_status = new RsStatusItem; StatusInfo own_info; - std::map::iterator it; + std::map::iterator it; { RsStackMutex stack(mStatusMtx); @@ -315,7 +315,7 @@ bool p3StatusService::loadList(std::list& load){ { RsStackMutex stack(mStatusMtx); - std::pair pr(mLinkMgr->getOwnId(), own_info); + std::pair pr(mLinkMgr->getOwnId(), own_info); mStatusInfoMap.insert(pr); } @@ -362,12 +362,12 @@ void p3StatusService::statusChange(const std::list &plist) } /* UNLOCKED */ changedState = true; - RsServer::notify()->notifyPeerStatusChanged(it->id, RS_STATUS_OFFLINE); + RsServer::notify()->notifyPeerStatusChanged(it->id.toStdString(), RS_STATUS_OFFLINE); } if (it->actions & RS_PEER_CONNECTED) { /* send current status, only call getOwnStatus once in the loop */ - if (statusInfo.id.empty() == false || getOwnStatus(statusInfo)) { + if (statusInfo.id.isNull() == false || getOwnStatus(statusInfo)) { sendStatus(it->id, statusInfo.status); } @@ -384,7 +384,7 @@ void p3StatusService::statusChange(const std::list &plist) } /* UNLOCKED */ changedState = true; - RsServer::notify()->notifyPeerStatusChanged(it->id, RS_STATUS_ONLINE); + RsServer::notify()->notifyPeerStatusChanged(it->id.toStdString(), RS_STATUS_ONLINE); } } else if (it->actions & RS_PEER_MOVED) { /* now handle remove */ @@ -395,7 +395,7 @@ void p3StatusService::statusChange(const std::list &plist) } /* UNLOCKED */ changedState = true; - RsServer::notify()->notifyPeerStatusChanged(it->id, RS_STATUS_OFFLINE); + RsServer::notify()->notifyPeerStatusChanged(it->id.toStdString(), RS_STATUS_OFFLINE); } } diff --git a/libretroshare/src/services/p3statusservice.h b/libretroshare/src/services/p3statusservice.h index 0512bec7c..fb43f03e9 100644 --- a/libretroshare/src/services/p3statusservice.h +++ b/libretroshare/src/services/p3statusservice.h @@ -65,9 +65,9 @@ virtual void statusChange(const std::list &plist); */ virtual bool getOwnStatus(StatusInfo& statusInfo); virtual bool getStatusList(std::list& statusInfo); -virtual bool getStatus(const std::string &id, StatusInfo &statusInfo); +virtual bool getStatus(const RsPeerId &id, StatusInfo &statusInfo); /* id = "", status is sent to all online peers */ -virtual bool sendStatus(const std::string &id, uint32_t status); +virtual bool sendStatus(const RsPeerId &id, uint32_t status); /******************************/ @@ -95,7 +95,7 @@ virtual void receiveStatusQueue(); p3LinkMgr *mLinkMgr; -std::map mStatusInfoMap; +std::map mStatusInfoMap; RsMutex mStatusMtx;