Cleaned up rsPeers to remove 10 minute reconnect. The reconnect was caused by

the periodical call of cleanupUsedLocation, which was reloading all the GPG Keys - for each cleanup Key.
This required cleaning up various bits (AuthGPG, rsPeers, p3Disc).

 * moved rsPeer::cleanUnusedLocations => p3PeerMgr::removeUnusedLocations();
 	- made the function light-weight, no loading of GPG Keys.
 * moved getSSLCertsFromGPG() fn from rsPeers => p3PeerMgr
 * added p3PeerMgr::removeAllFriendLocations() to cleanup when removing a GPG Friend.
 * cleaned up rsPeers, made addFriend() / removeFriend() universal functions.
 * remove bool   p3Peers::setAcceptToConnectGPGCertificate(const std::string &gpg_id, bool acceptance)
 * removed all Dummy SSL Ids. - There is no need for this!
 * Major changes to p3disc to correct FLAWED logic.  (needs more testing).
 * removed as many dependancies on retroshare/rspeer.h as possible.
 * changed p3turtle dependancy on rsPeers ==> mLinkMgr.
 * added p3LinkMgr::getPeerName() and p3PeerMgr::getPeerName()
 * added return 1 to pqissllistener::closeConnection().
 * removed unused code from AuthGPG().
 * removed storeAllKeys() calls in AuthGPG::AllowConnection().



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@4542 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-08-07 21:11:00 +00:00
parent 80629c5d9b
commit 8e3e7d5209
20 changed files with 1000 additions and 673 deletions

View file

@ -25,8 +25,9 @@
*/
#include "authgpg.h"
#include "retroshare/rsiface.h"
#include "retroshare/rsinit.h"
#include "retroshare/rsiface.h" // For rsicontrol.
#include "retroshare/rspeers.h" // For RsPeerDetails.
#include <util/rsdir.h>
#include <iostream>
#include <sstream>
@ -64,18 +65,10 @@ AuthGPG *AuthGPG::getAuthGPG()
}
/* Turn a set of parameters into a string */
#if 0
static std::string setKeyPairParams(bool useRsa, unsigned int blen,
std::string name, std::string comment, std::string email);
#endif
static std::string setKeyPairParams(bool useRsa, unsigned int blen,
std::string name, std::string comment, std::string email,
std::string inPassphrase);
#ifdef UNUSED_CODE
static gpgme_key_t getKey(gpgme_ctx_t, std::string, std::string, std::string);
#endif
static gpg_error_t keySignCallback(void *, gpgme_status_code_t, \
const char *, int);
@ -152,15 +145,6 @@ AuthGPGimpl::AuthGPGimpl()
gpgme_set_locale(NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL));
#endif
#ifndef WINDOWS_SYS
/* setup the engine (gpg2) */
// if (GPG_ERR_NO_ERROR != gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, "/usr/bin/gpg2", NULL))
// {
// std::cerr << "Error creating Setting engine" << std::endl;
// return;
// }
#endif
if (GPG_ERR_NO_ERROR != gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP))
{
std::cerr << "Error check engine version" << std::endl;
@ -995,12 +979,6 @@ bool AuthGPGimpl::VerifySignature(const void *data, int datalen, const void *sig
RsStackMutex stack(gpgMtxEngine); /******* LOCKED ******/
// if(siglen==73)
// {
// //std::cerr << "Reducing to 72 to overcome an old bug." << std::endl ;
// siglen=72 ;
// }
if (GPG_ERR_NO_ERROR != gpgme_data_new_from_mem(&gpgmeData, (const char *) data, datalen, 1))
{
std::cerr << "Error create Data" << std::endl;
@ -1515,25 +1493,24 @@ bool AuthGPGimpl::LoadCertificateFromString(const std::string &str, std::string
/*************************************/
/* These take PGP Ids */
bool AuthGPGimpl::setAcceptToConnectGPGCertificate(const std::string &gpg_id, bool acceptance)
bool AuthGPGimpl::AllowConnection(const std::string &gpg_id, bool accept)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::markGPGCertificateAsFriends(" << gpg_id << ")" << std::endl;
std::cerr << "AuthGPGimpl::AllowConnection(" << gpg_id << ")" << std::endl;
#endif
/* reload stuff now ... */
storeAllKeys();
{
RsStackMutex stack(gpgMtxData);
certmap::iterator it;
if (mKeyList.end() == (it = mKeyList.find(gpg_id))) {
return false;
}
it->second.accept_connection = acceptance;
mAcceptToConnectMap[gpg_id] = acceptance;
}
storeAllKeys();
/* Was a "Reload Certificates" here -> be shouldn't be needed -> and very expensive, try without. */
{
RsStackMutex stack(gpgMtxData);
certmap::iterator it;
if (mKeyList.end() == (it = mKeyList.find(gpg_id)))
{
return false;
}
it->second.accept_connection = accept;
mAcceptToConnectMap[gpg_id] = accept;
}
IndicateConfigChanged();
@ -1730,69 +1707,6 @@ int AuthGPGimpl::privateTrustCertificate(const std::string &id, int trustlvl)
return 1;
}
/* This function to print Data */
#if 0
void showData(gpgme_data_t dh)
{
#define BUF_SIZE 512
char buf[BUF_SIZE + 1];
int ret;
ret = gpgme_data_seek (dh, 0, SEEK_SET);
if (ret)
{
std::cerr << "Fail data seek" << std::endl;
// fail_if_err (gpgme_err_code_from_errno (errno));
}
while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0)
fwrite (buf, ret, 1, stdout);
if (ret < 0)
{
std::cerr << "Fail data seek" << std::endl;
//fail_if_err (gpgme_err_code_from_errno (errno));
}
}
#endif
#if 0
static std::string setKeyPairParams(bool useRsa, unsigned int blen,
std::string name, std::string comment, std::string email)
{
std::ostringstream params;
params << "<GnupgKeyParms format=\"internal\">"<< std::endl;
if (useRsa)
{
params << "Key-Type: RSA"<< std::endl;
if (blen < 1024)
{
#ifdef GPG_DEBUG
std::cerr << "Weak Key... strengthing..."<< std::endl;
#endif
blen = 1024;
}
blen = ((blen / 512) * 512); /* make multiple of 512 */
params << "Key-Length: "<< blen << std::endl;
}
else
{
params << "Key-Type: DSA"<< std::endl;
params << "Key-Length: 1024"<< std::endl;
params << "Subkey-Type: ELG-E"<< std::endl;
params << "Subkey-Length: 1024"<< std::endl;
}
params << "Name-Real: "<< name << std::endl;
params << "Name-Comment: "<< comment << std::endl;
params << "Name-Email: "<< email << std::endl;
params << "Expire-Date: 0"<< std::endl;
params << "</GnupgKeyParms>"<< std::endl;
return params.str();
}
#endif
static std::string setKeyPairParams(bool useRsa, unsigned int blen,
std::string name, std::string comment, std::string email,
std::string inPassphrase)
@ -2357,21 +2271,30 @@ bool AuthGPGimpl::saveList(bool& cleanup, std::list<RsItem*>& lst)
// Now save config for network digging strategies
RsConfigKeyValueSet *vitem = new RsConfigKeyValueSet ;
std::map<std::string, bool>::iterator mapIt;
for (mapIt = mAcceptToConnectMap.begin(); mapIt != mAcceptToConnectMap.end(); mapIt++) {
if (mapIt->first == mOwnGpgId) {
continue;
}
RsTlvKeyValue kv;
kv.key = mapIt->first;
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::saveList() called (mapIt->second) : " << (mapIt->second) << std::endl ;
#endif
kv.value = (mapIt->second)?"TRUE":"FALSE" ;
vitem->tlvkvs.pairs.push_back(kv) ;
for (mapIt = mAcceptToConnectMap.begin(); mapIt != mAcceptToConnectMap.end(); mapIt++)
{
// skip our own id.
if (mapIt->first == mOwnGpgId)
{
continue;
}
// skip if we dont accept them.
if (!(mapIt->second))
{
continue;
}
RsTlvKeyValue kv;
kv.key = mapIt->first;
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::saveList() called (mapIt->second) : " << (mapIt->second) << std::endl ;
#endif
kv.value = "TRUE";
vitem->tlvkvs.pairs.push_back(kv) ;
}
lst.push_back(vitem);
return true ;
return true;
}
bool AuthGPGimpl::loadList(std::list<RsItem*>& load)
@ -2385,31 +2308,36 @@ bool AuthGPGimpl::loadList(std::list<RsItem*>& load)
RsStackMutex stack(gpgMtxData); /******* LOCKED ******/
/* load the list of accepted gpg keys */
std::list<RsItem *>::iterator it;
for(it = load.begin(); it != load.end(); it++) {
RsConfigKeyValueSet *vitem = dynamic_cast<RsConfigKeyValueSet *>(*it);
for(it = load.begin(); it != load.end(); it++)
{
RsConfigKeyValueSet *vitem = dynamic_cast<RsConfigKeyValueSet *>(*it);
if(vitem)
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::loadList() General Variable Config Item:" << std::endl;
vitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
if(vitem) {
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::loadList() General Variable Config Item:" << std::endl;
vitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
std::list<RsTlvKeyValue>::iterator kit;
for(kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); kit++)
{
if (kit->key == mOwnGpgId)
{
continue;
}
std::list<RsTlvKeyValue>::iterator kit;
for(kit = vitem->tlvkvs.pairs.begin(); kit != vitem->tlvkvs.pairs.end(); kit++) {
if (kit->key == mOwnGpgId) {
continue;
}
mAcceptToConnectMap[kit->key] = (kit->value == "TRUE");
//set the gpg key
certmap::iterator it;
if (mKeyList.end() != (it = mKeyList.find(kit->key))) {
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::loadList() setting accept to : " << (kit->value == "TRUE");
std::cerr << " for gpg key id : " << kit->key << std::endl;
#endif
it->second.accept_connection = (kit->value == "TRUE");
}
/* only allowed in the map if the gpg certificate exists */
certmap::iterator it;
if (mKeyList.end() != (it = mKeyList.find(kit->key)))
{
#ifdef GPG_DEBUG
std::cerr << "AuthGPGimpl::loadList() setting accept to : " << (kit->value == "TRUE");
std::cerr << " for gpg key id : " << kit->key << std::endl;
#endif
mAcceptToConnectMap[kit->key] = (kit->value == "TRUE");
it->second.accept_connection = (kit->value == "TRUE");
}
}
}
delete (*it);

View file

@ -43,7 +43,7 @@
#include <openssl/ssl.h>
#include <openssl/evp.h>
#include "util/rsthreads.h"
#include "retroshare/rspeers.h"
//#include "retroshare/rspeers.h"
#include <string>
#include <list>
#include <set>
@ -52,6 +52,8 @@
#define MAX_GPG_SIGNATURE_SIZE 4096
class RsPeerDetails;
/*!
* gpgcert is the identifier for a person.
* It is a wrapper class for a GPGme OpenPGP certificate.
@ -225,7 +227,9 @@ virtual std::string SaveCertificateToString(const std::string &id,bool include_s
* done in gpgroot already.
*
****/
virtual bool setAcceptToConnectGPGCertificate(const std::string &gpg_id, bool acceptance) = 0; //don't act on the gpg key, use a seperate set
virtual bool AllowConnection(const std::string &gpg_id, bool accept) = 0;
virtual bool SignCertificateLevel0(const std::string &id) = 0;
virtual bool RevokeCertificate(const std::string &id) = 0; /* Particularly hard - leave for later */
//virtual bool TrustCertificateNone(std::string id) = 0;
@ -345,7 +349,8 @@ virtual std::string SaveCertificateToString(const std::string &id,bool include_s
* done in gpgroot already.
*
****/
virtual bool setAcceptToConnectGPGCertificate(const std::string &gpg_id, bool acceptance); //don't act on the gpg key, use a seperate set
virtual bool AllowConnection(const std::string &gpg_id, bool accept);
virtual bool SignCertificateLevel0(const std::string &id);
virtual bool RevokeCertificate(const std::string &id); /* Particularly hard - leave for later */

View file

@ -37,6 +37,8 @@
#include "serialiser/rsconfigitems.h"
#include "util/rsdir.h"
#include "retroshare/rspeers.h" // for RsPeerDetails structure
/******************** notify of new Cert **************************/
#include "pqinotify.h"

View file

@ -49,7 +49,6 @@
#include "pqi/pqi_base.h"
#include "pqi/pqinetwork.h"
//#include "rsiface/rspeers.h"
#include "pqi/p3cfgmgr.h"
typedef std::string SSL_id;

View file

@ -24,7 +24,7 @@
*/
#include "util/rsdir.h"
#include "retroshare/rspeers.h"
//#include "retroshare/rspeers.h"
#include "pqi/p3cfgmgr.h"
#include "pqi/authssl.h"
#include "pqi/pqibin.h"

View file

@ -237,6 +237,12 @@ void p3LinkMgrIMPL::getFriendList(std::list<std::string> &ssl_peers)
}
bool p3LinkMgrIMPL::getPeerName(const std::string &ssl_id, std::string &name)
{
return mPeerMgr->getPeerName(ssl_id, name);
}
int p3LinkMgrIMPL::getFriendCount()
{
RsStackMutex stack(mLinkMtx); /****** STACK LOCK MUTEX *******/

View file

@ -169,6 +169,7 @@ virtual ~p3LinkMgr() { return; }
virtual const std::string getOwnId() = 0;
virtual bool isOnline(const std::string &ssl_id) = 0;
virtual void getOnlineList(std::list<std::string> &ssl_peers) = 0;
virtual bool getPeerName(const std::string &ssl_id, std::string &name) = 0;
/**************** handle monitors *****************/
virtual void addMonitor(pqiMonitor *mon) = 0;
@ -224,6 +225,7 @@ class p3LinkMgrIMPL: public p3LinkMgr
virtual const std::string getOwnId();
virtual bool isOnline(const std::string &ssl_id);
virtual void getOnlineList(std::list<std::string> &ssl_peers);
virtual bool getPeerName(const std::string &ssl_id, std::string &name);
/**************** handle monitors *****************/

View file

@ -882,6 +882,8 @@ void p3NetMgrIMPL::netExtCheck()
if (netSetupDone)
{
std::cerr << "p3NetMgrIMPL::netExtCheck() netSetupDone" << std::endl;
/* Setup NetStateBox with this info */
updateNetStateBox_startup();

View file

@ -42,7 +42,10 @@ const int p3peermgrzone = 9531;
#include "serialiser/rsconfigitems.h"
#include "pqi/pqinotify.h"
#include "retroshare/rsiface.h"
#include "retroshare/rsiface.h" // Needed for rsicontrol (should remove this dependancy)
#include "retroshare/rspeers.h" // Needed for Group Parameters.
#include <sstream>
@ -110,6 +113,7 @@ p3PeerMgrIMPL::p3PeerMgrIMPL()
mOwnState.name = AuthGPG::getAuthGPG()->getGPGOwnName();
mOwnState.location = AuthSSL::getAuthSSL()->getOwnLocation();
mOwnState.netMode = RS_NET_MODE_UPNP; // Default to UPNP.
mOwnState.visState = 0;
lastGroupId = 1;
@ -156,11 +160,14 @@ void p3PeerMgrIMPL::setOwnVisState(uint32_t visState)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
std::ostringstream out;
out << "p3PeerMgr::setOwnVisState()";
out << "Existing vis: " << mOwnState.visState;
out << "Input vis: " << visState;
rslog(RSL_WARNING, p3peermgrzone, out.str());
#ifdef PEER_DEBUG
std::cerr << "p3PeerMgrIMPL::setOwnVisState()";
std::cerr << "Existing vis: " << mOwnState.visState;
std::cerr << "Input vis: " << visState;
std::cerr << std::endl;
std::cerr << out.str() << std::endl;
#endif
mOwnState.visState = visState;
@ -182,13 +189,13 @@ void p3PeerMgrIMPL::tick()
time_t now = time(NULL) ;
if(now > last_friends_check + INTERVAL_BETWEEN_LOCATION_CLEANING && rsPeers != NULL)
if(now - last_friends_check > INTERVAL_BETWEEN_LOCATION_CLEANING)
{
std::cerr << "p3PeerMgrIMPL::tick(): cleaning unused locations." << std::endl ;
rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::tick() cleanUnusedLocations()");
rslog(RSL_WARNING, p3peermgrzone, "p3PeerMgr::tick() removeUnusedLocations()");
rsPeers->cleanUnusedLocations() ;
removeUnusedLocations() ;
last_friends_check = now ;
}
}
@ -225,6 +232,23 @@ bool p3PeerMgrIMPL::isFriend(const std::string &id)
return ret;
}
bool p3PeerMgrIMPL::getPeerName(const std::string &ssl_id, std::string &name)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
/* check for existing */
std::map<std::string, peerState>::iterator it;
it = mFriendList.find(ssl_id);
if (it == mFriendList.end())
{
return false;
}
name = it->second.name + " (" + it->second.location + ")";
return true;
}
bool p3PeerMgrIMPL::getFriendNetStatus(const std::string &id, peerState &state)
{
@ -998,6 +1022,14 @@ bool p3PeerMgrIMPL::setLocation(const std::string &id, const std::string &loc
bool p3PeerMgrIMPL::setVisState(const std::string &id, uint32_t visState)
{
{
std::ostringstream out;
out << "p3PeerMgr::setVisState(";
out << id;
out << ", " << visState << ")";
rslog(RSL_WARNING, p3peermgrzone, out.str());
}
if (id == AuthSSL::getAuthSSL()->OwnId())
{
setOwnVisState(visState);
@ -1630,3 +1662,118 @@ bool p3PeerMgrIMPL::assignPeersToGroup(const std::string &groupId, const std::li
return changed;
}
/**********************************************************************
**********************************************************************
******************** Stuff moved from p3peers ************************
**********************************************************************
**********************************************************************/
bool p3PeerMgrIMPL::removeAllFriendLocations(const std::string &gpgid)
{
std::list<std::string> sslIds;
if (!getAssociatedPeers(gpgid, sslIds))
{
return false;
}
std::list<std::string>::iterator it;
for(it = sslIds.begin(); it != sslIds.end(); it++)
{
removeFriend(*it);
}
return true;
}
bool p3PeerMgrIMPL::getAssociatedPeers(const std::string &gpg_id, std::list<std::string> &ids)
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
#ifdef P3PEERS_DEBUG
std::cerr << "p3PeerMgr::getAssociatedPeers() for id : " << gpg_id << std::endl;
#endif
int count = 0;
std::map<std::string, peerState>::iterator it;
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
{
if (it->second.gpg_id == gpg_id)
{
count++;
ids.push_back(it->first);
#ifdef P3PEERS_DEBUG
std::cerr << "p3PeerMgr::getAssociatedPeers() found ssl id : " << it->first << std::endl;
#endif
}
}
return (count > 0);
}
/* This only removes SSL certs, that are old... Can end up with no Certs per GPG Id
* We are removing the concept of a "DummyId" - There is no need for it.
*/
bool isDummyFriend(std::string id)
{
bool ret = (id.substr(0,5) == "dummy");
return ret;
}
#define VERY_OLD_PEER (30 * 24 * 3600) // 30 days.
bool p3PeerMgrIMPL::removeUnusedLocations()
{
std::list<std::string> toRemove;
{
RsStackMutex stack(mPeerMtx); /****** STACK LOCK MUTEX *******/
#ifdef P3PEERS_DEBUG
std::cerr << "p3PeerMgr::removeUnusedLocations()" << std::endl;
#endif
time_t now = time(NULL);
std::map<std::string, peerState>::iterator it;
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
{
if (now - it->second.lastcontact > VERY_OLD_PEER)
{
toRemove.push_back(it->first);
#ifdef P3PEERS_DEBUG
std::cerr << "p3PeerMgr::removeUnusedLocations() removing Old SSL Id: " << it->first << std::endl;
#endif
}
if (isDummyFriend(it->first))
{
toRemove.push_back(it->first);
#ifdef P3PEERS_DEBUG
std::cerr << "p3PeerMgr::removeUnusedLocations() removing Dummy Id: " << it->first << std::endl;
#endif
}
}
}
std::list<std::string>::iterator it;
for(it = toRemove.begin(); it != toRemove.end(); it++)
{
removeFriend(*it);
}
}

View file

@ -127,6 +127,8 @@ virtual bool removeFriend(const std::string &ssl_id) = 0;
virtual bool isFriend(const std::string &ssl_id) = 0;
virtual bool getAssociatedPeers(const std::string &gpg_id, std::list<std::string> &ids) = 0;
virtual bool removeAllFriendLocations(const std::string &gpgid) = 0;
/******************** Groups **********************/
@ -178,6 +180,8 @@ virtual bool getOwnNetStatus(peerState &state) = 0;
virtual bool getFriendNetStatus(const std::string &id, peerState &state) = 0;
virtual bool getOthersNetStatus(const std::string &id, peerState &state) = 0;
virtual bool getPeerName(const std::string &ssl_id, std::string &name) = 0;
/************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/
@ -208,6 +212,9 @@ virtual bool removeFriend(const std::string &ssl_id);
virtual bool isFriend(const std::string &ssl_id);
virtual bool getAssociatedPeers(const std::string &gpg_id, std::list<std::string> &ids);
virtual bool removeAllFriendLocations(const std::string &gpgid);
/******************** Groups **********************/
/* This is solely used by p3peers - makes sense */
@ -257,6 +264,8 @@ virtual bool getOwnNetStatus(peerState &state);
virtual bool getFriendNetStatus(const std::string &id, peerState &state);
virtual bool getOthersNetStatus(const std::string &id, peerState &state);
virtual bool getPeerName(const std::string &ssl_id, std::string &name);
/************* DEPRECIATED FUNCTIONS (TO REMOVE) ********/
@ -282,8 +291,13 @@ int getConnectAddresses(const std::string &id,
struct sockaddr_in &lAddr, struct sockaddr_in &eAddr,
pqiIpAddrSet &histAddrs, std::string &dyndns);
protected:
/* Internal Functions */
bool removeUnusedLocations();
void printPeerLists(std::ostream &out);
protected:

View file

@ -493,6 +493,8 @@ int pqissllistenbase::closeConnection(int fd, SSL *ssl)
/************************** WINDOWS/UNIX SPECIFIC PART ******************/
// free connection.
SSL_free(ssl);
return 1;
}