mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-15 17:37:12 -05:00
fixed issue with double delete of keys, refactored key sharing logic
into attemptrecvkeys and receivepubkeys functions (minor changes to loadlist) git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@3979 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
c7b28dfb7d
commit
dddc9c383c
@ -128,7 +128,7 @@ int p3GroupDistrib::tick()
|
|||||||
{
|
{
|
||||||
RsStackMutex stack(distribMtx);
|
RsStackMutex stack(distribMtx);
|
||||||
toPublish = (mPendingPubKeyRecipients.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime));
|
toPublish = (mPendingPubKeyRecipients.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime));
|
||||||
attemptRecv = (mRecvdPubKeys.size() > 0) && (now > (time_t) (mPubPeriod + mLastKeyPublishTime));
|
attemptRecv = (mRecvdPubKeys.size() > 0); // attempt to load stored keys in case user has subscribed
|
||||||
}
|
}
|
||||||
|
|
||||||
if(toPublish){
|
if(toPublish){
|
||||||
@ -520,39 +520,15 @@ bool p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey)
|
|||||||
{
|
{
|
||||||
|
|
||||||
#ifdef DISTRIB_DEBUG
|
#ifdef DISTRIB_DEBUG
|
||||||
std::cerr << "p3GroupDistrib::loadGroupKey() Group Not Found - Checking to see if shared Key";
|
|
||||||
std::cerr << std::endl;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// if this is an in-date full publish key then keep to see if group arrives later
|
std::cerr << "p3GroupDistrib::loadGroupKey() Group for key not found: discarding";
|
||||||
if(((time_t)(newKey->key.startTS + mStorePeriod) > time(NULL)) && (newKey->key.keyFlags & RSTLV_KEY_TYPE_FULL))
|
|
||||||
{
|
|
||||||
|
|
||||||
// make sure key does not exist
|
|
||||||
if(mRecvdPubKeys.find(gid) == mRecvdPubKeys.end()){
|
|
||||||
mRecvdPubKeys.insert(std::pair<std::string, RsDistribGrpKey*>(gid, newKey));
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
#ifdef DISTRIB_DEBUG
|
|
||||||
std::cerr << "p3GroupDistrib::loadGroupKey() Key already received; discarding";
|
|
||||||
std::cerr << std::endl;
|
|
||||||
#endif
|
|
||||||
delete newKey;
|
|
||||||
return false ;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
#ifdef DISTRIB_DEBUG
|
|
||||||
std::cerr << "p3GroupDistrib::loadGroupKey() Key out of date: discarding";
|
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
delete newKey;
|
delete newKey;
|
||||||
|
newKey = NULL;
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -590,23 +566,20 @@ bool p3GroupDistrib::loadGroupKey(RsDistribGrpKey *newKey)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (updateOk)
|
||||||
|
locked_notifyGroupChanged(it->second, GRP_LOAD_KEY);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef DISTRIB_DEBUG
|
#ifdef DISTRIB_DEBUG
|
||||||
std::cerr << "p3GroupDistrib::loadGroupKey() Done - Cleaning up.";
|
std::cerr << "p3GroupDistrib::loadGroupKey() Done - Cleaning up.";
|
||||||
std::cerr << std::endl;
|
std::cerr << std::endl;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
GroupInfo *gi = locked_getGroupInfo(gid);
|
|
||||||
|
|
||||||
// special case, if user is not subscribed then save key
|
|
||||||
if(!(gi->flags & RS_DISTRIB_SUBSCRIBED) && updateOk){
|
|
||||||
|
|
||||||
mRecvdPubKeys.insert(std::pair<std::string, RsDistribGrpKey*>(gid, newKey));
|
|
||||||
return updateOk;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!updateOk)
|
if(!updateOk)
|
||||||
delete newKey;
|
delete newKey;
|
||||||
|
|
||||||
|
newKey = NULL;
|
||||||
return updateOk;
|
return updateOk;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1265,9 +1238,6 @@ bool p3GroupDistrib::subscribeToGroup(const std::string &grpId, bool subscrib
|
|||||||
{
|
{
|
||||||
git->second.flags |= RS_DISTRIB_SUBSCRIBED;
|
git->second.flags |= RS_DISTRIB_SUBSCRIBED;
|
||||||
|
|
||||||
// if(attemptPublishKeysRecvd(git->second))
|
|
||||||
// git->second.flags |= RS_DISTRIB_PUBLISH;
|
|
||||||
|
|
||||||
locked_notifyGroupChanged(git->second, GRP_SUBSCRIBED);
|
locked_notifyGroupChanged(git->second, GRP_SUBSCRIBED);
|
||||||
mGroupsRepublish = true;
|
mGroupsRepublish = true;
|
||||||
|
|
||||||
@ -1310,23 +1280,58 @@ bool p3GroupDistrib::subscribeToGroup(const std::string &grpId, bool subscrib
|
|||||||
|
|
||||||
bool p3GroupDistrib::attemptPublishKeysRecvd()
|
bool p3GroupDistrib::attemptPublishKeysRecvd()
|
||||||
{
|
{
|
||||||
std::map<std::string, RsDistribGrpKey*>::iterator mit = mRecvdPubKeys.begin();
|
|
||||||
|
|
||||||
for(;mit != mRecvdPubKeys.end();)
|
#ifdef DISTRIB_DEBUG
|
||||||
{
|
std::cerr << "p3GroupDistrib::attemptPublishKeysRecvd() " << std::endl;
|
||||||
bool ok = loadGroupKey(mit->second);
|
#endif
|
||||||
|
|
||||||
if(!ok)
|
RsStackMutex stack(distribMtx);
|
||||||
{
|
|
||||||
std::map<std::string, RsDistribGrpKey*>::iterator tmp(mit);
|
|
||||||
++mit ;
|
|
||||||
|
|
||||||
mRecvdPubKeys.erase(tmp) ;
|
std::list<std::string> toDelete;
|
||||||
|
std::list<std::string>::iterator sit;
|
||||||
|
std::map<std::string, GroupInfo>::iterator it;
|
||||||
|
|
||||||
|
std::map<std::string, RsDistribGrpKey*>::iterator mit;
|
||||||
|
mit = mRecvdPubKeys.begin();
|
||||||
|
|
||||||
|
// add received keys for groups that are present
|
||||||
|
for(; mit != mRecvdPubKeys.end(); mit++){
|
||||||
|
|
||||||
|
it = mGroups.find(mit->first);
|
||||||
|
|
||||||
|
// group has not arrived yet don't attempt to add
|
||||||
|
if(it == mGroups.end()){
|
||||||
|
|
||||||
|
continue;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(locked_updateGroupPublishKey(it->second, mit->second)){
|
||||||
|
|
||||||
|
locked_notifyGroupChanged(it->second, GRP_LOAD_KEY);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
++mit ;
|
if(it->second.flags & RS_DISTRIB_SUBSCRIBED){
|
||||||
|
// remove key shared flag so key is not loaded back into mrecvdpubkeys
|
||||||
|
mit->second->key.keyFlags &= (~RSTLV_KEY_TYPE_SHARED);
|
||||||
|
|
||||||
|
delete (mit->second);
|
||||||
|
toDelete.push_back(mit->first);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sit = toDelete.begin();
|
||||||
|
|
||||||
|
for(; sit != toDelete.end(); sit++)
|
||||||
|
mRecvdPubKeys.erase(*sit);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1555,7 +1560,19 @@ bool p3GroupDistrib::loadList(std::list<RsItem *>& load)
|
|||||||
}
|
}
|
||||||
else if ((newKey = dynamic_cast<RsDistribGrpKey *>(*lit)))
|
else if ((newKey = dynamic_cast<RsDistribGrpKey *>(*lit)))
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// for shared keys keep
|
||||||
|
if(newKey->key.keyFlags & RSTLV_KEY_TYPE_SHARED){
|
||||||
|
|
||||||
|
mRecvdPubKeys.insert(std::pair<std::string, RsDistribGrpKey*>(
|
||||||
|
newKey->grpId, newKey));
|
||||||
|
mPubKeyAvailableGrpId.insert(newKey->grpId);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
loadGroupKey(newKey);
|
loadGroupKey(newKey);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else if ((newMsg = dynamic_cast<RsDistribSignedMsg *>(*lit)))
|
else if ((newMsg = dynamic_cast<RsDistribSignedMsg *>(*lit)))
|
||||||
{
|
{
|
||||||
@ -2109,15 +2126,46 @@ void p3GroupDistrib::receivePubKeys(){
|
|||||||
|
|
||||||
|
|
||||||
RsItem* item;
|
RsItem* item;
|
||||||
GroupInfo *gi = NULL;
|
|
||||||
std::string gid;
|
std::string gid;
|
||||||
|
|
||||||
|
std::map<std::string, GroupInfo>::iterator it;
|
||||||
|
std::list<std::string> toDelete;
|
||||||
|
std::list<std::string>::iterator sit;
|
||||||
|
|
||||||
|
|
||||||
|
// load received keys
|
||||||
while(NULL != (item = recvItem())){
|
while(NULL != (item = recvItem())){
|
||||||
|
|
||||||
RsDistribGrpKey* key_item = dynamic_cast<RsDistribGrpKey*>(item);
|
RsDistribGrpKey* key_item = dynamic_cast<RsDistribGrpKey*>(item);
|
||||||
|
|
||||||
if(key_item != NULL){
|
if(key_item != NULL){
|
||||||
|
|
||||||
|
it = mGroups.find(key_item->grpId);
|
||||||
|
|
||||||
|
// if group does not exist keep to see if it arrives later
|
||||||
|
if(it == mGroups.end()){
|
||||||
|
|
||||||
|
// make sure key is in date
|
||||||
|
if(((time_t)(key_item->key.startTS + mStorePeriod) > time(NULL)) &&
|
||||||
|
(key_item->key.keyFlags & RSTLV_KEY_TYPE_FULL)){
|
||||||
|
|
||||||
|
// make sure keys does not exist in recieved list, then delete
|
||||||
|
if(mRecvdPubKeys.find(gid) == mRecvdPubKeys.end()){
|
||||||
|
|
||||||
|
// id key as shared so on loadlist sends back mRecvdPubKeys
|
||||||
|
key_item->key.keyFlags |= RSTLV_KEY_TYPE_SHARED;
|
||||||
|
mRecvdPubKeys.insert(std::pair<std::string,
|
||||||
|
RsDistribGrpKey*>(key_item->grpId, key_item));
|
||||||
|
|
||||||
|
mPubKeyAvailableGrpId.insert(key_item->grpId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}else{
|
||||||
|
delete key_item;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DISTRIB_DEBUG
|
#ifdef DISTRIB_DEBUG
|
||||||
std::cerr << "p3GroupDistrib::locked_receiveKeys()" << std::endl;
|
std::cerr << "p3GroupDistrib::locked_receiveKeys()" << std::endl;
|
||||||
@ -2128,27 +2176,43 @@ void p3GroupDistrib::receivePubKeys(){
|
|||||||
if(key_item->key.keyFlags & RSTLV_KEY_TYPE_FULL){
|
if(key_item->key.keyFlags & RSTLV_KEY_TYPE_FULL){
|
||||||
|
|
||||||
gid = key_item->grpId;
|
gid = key_item->grpId;
|
||||||
if(loadGroupKey(key_item)){
|
|
||||||
|
|
||||||
if(NULL != (gi = locked_getGroupInfo(gid))){
|
// add key if user is subscribed if not store it until user subscribes
|
||||||
locked_notifyGroupChanged(*gi, GRP_LOAD_KEY);
|
|
||||||
gi = NULL;
|
if(locked_updateGroupPublishKey(it->second, key_item)){
|
||||||
|
|
||||||
|
mPubKeyAvailableGrpId.insert(key_item->grpId);
|
||||||
|
locked_notifyGroupChanged(it->second, GRP_LOAD_KEY);
|
||||||
|
|
||||||
|
// keep key if user not subscribed
|
||||||
|
if(it->second.flags & RS_DISTRIB_SUBSCRIBED){
|
||||||
|
|
||||||
|
delete key_item;
|
||||||
|
|
||||||
|
}else{
|
||||||
|
|
||||||
|
// make sure keys does not exist in recieved list
|
||||||
|
if(mRecvdPubKeys.find(gid) == mRecvdPubKeys.end()){
|
||||||
|
|
||||||
|
// id key as shared so on loadlist sends back to mRecvdPubKeys
|
||||||
|
key_item->key.keyFlags |= RSTLV_KEY_TYPE_SHARED;
|
||||||
|
mRecvdPubKeys.insert(std::pair<std::string,
|
||||||
|
RsDistribGrpKey*>(key_item->grpId, key_item));
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mPubKeyAvailableGrpId.insert(gid);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
std::cerr << "p3GroupDistrib::locked_receiveKeys():" << "Not full public key"
|
std::cerr << "p3GroupDistrib::locked_receiveKeys():" << "Not full public key"
|
||||||
<< "Deleting item"<< std::endl;
|
<< "Deleting item"<< std::endl;
|
||||||
|
|
||||||
delete key_item;
|
delete key_item;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
||||||
delete item;
|
delete item;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2166,6 +2230,7 @@ void p3GroupDistrib::receivePubKeys(){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string p3GroupDistrib::publishMsg(RsDistribMsg *msg, bool personalSign)
|
std::string p3GroupDistrib::publishMsg(RsDistribMsg *msg, bool personalSign)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user