Lots of little changes to libretroshare. Improvements mainly focused

on configuration storage and loading:
 * Improved Configuration Manager (almost finished)
 * Mutex protections for Configuration system
 * added Configuration storage to p3ConnectMgr.
 * added Configuration storage to p3MsgService.
 * bugfixes in p3GeneralConfig.
 * Added Config Save notification where necessary.
 * added safe FinalConfigSave before exit().
 * added RsPeerNetItem + RsPeerStunItem (serialiser)
 * reordered startup for correct config loading.
 * enabled Loading of certs in AuthXPGP.
 * move sockaddr_clear() to util/rsnet.h
 * switched p3MsgService sendMessage checking to pqiMonitor syste,.
 * corrected pqiarchive saving of PeerIds.
 * added setNetworkMode() & setVisState() to p3ConnectMgr.
 * added Mutex protection to p3ranking.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@336 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2008-02-07 16:18:34 +00:00
parent 2c69fd7eaf
commit 806b8285f2
36 changed files with 1712 additions and 599 deletions

View file

@ -7,7 +7,9 @@ RS_TOP_DIR = ..
include $(RS_TOP_DIR)/scripts/config.mk
###############################################################
BASE_OBJ = pqi_base.o pqidebug.o pqisecurity.o pqinetwork.o
BASE_OBJ = pqidebug.o pqisecurity.o pqinetwork.o
#pqi_base.o
LOOP_OBJ = pqiloopback.o
STREAM_OBJ = pqistreamer.o pqiarchive.o pqibin.o
MGR_OBJ = pqimonitor.o p3dhtmgr.o p3connmgr.o p3cfgmgr.o p3authmgr.o

View file

@ -248,7 +248,7 @@ SSL_CTX *AuthXPGP::getCTX()
return sslctx;
}
int AuthXPGP::setConfigDirectories(const char *cdir, const char *ndir)
int AuthXPGP::setConfigDirectories(std::string configfile, std::string neighdir)
{
#ifdef AUTHXPGP_DEBUG
std::cerr << "AuthXPGP::setConfigDirectories()";
@ -256,8 +256,8 @@ int AuthXPGP::setConfigDirectories(const char *cdir, const char *ndir)
#endif
xpgpMtx.lock(); /***** LOCK *****/
mCertDir = cdir;
mNeighDir = ndir;
mCertConfigFile = configfile;
mNeighDir = neighdir;
xpgpMtx.unlock(); /**** UNLOCK ****/
return 1;
@ -1510,7 +1510,22 @@ std::list<std::string> getXPGPsigners(XPGP *cert)
return signers;
}
// other fns
std::string getCertName(XPGP *xpgp)
{
std::string name = xpgp->name;
// strip out bad chars.
for(int i = 0; i < (signed) name.length(); i++)
{
if ((name[i] == '/') || (name[i] == ' ') || (name[i] == '=') ||
(name[i] == '\\') || (name[i] == '\t') || (name[i] == '\n'))
{
name[i] = '_';
}
}
return name;
}
/********** SSL ERROR STUFF ******************************************/
@ -1563,39 +1578,69 @@ int printSSLError(SSL *ssl, int retval, int err, unsigned long err2,
}
#if 0
/***************************** OLD STORAGE of CERTS *************************
* We will retain the existing CERT storage format for the moment....
* This will enable the existing certs to be loaded in.
*
* BUT Save will change the format - removing the options from
* the configuration file. This will mean that we can catch NEW/OLD formats.
*
* We only want to load old format ONCE. as we'll use it to get
* the list of existing friends...
*
*
*
*/
int AuthXPGP::saveCertificates(const char *fname)
bool AuthXPGP::saveCertificates()
{
// construct file name.
//
// create the file in memory - hash + sign.
// write out data to a file.
std::string neighdir = certdir + "/" + neighbourdir + "/";
std::string configname = certdir + "/";
configname += fname;
xpgpMtx.lock(); /***** LOCK *****/
std::string configfile = mCertConfigFile;
std::string neighdir = mNeighDir;
xpgpMtx.unlock(); /**** UNLOCK ****/
std::map<std::string, std::string>::iterator mit;
std::string conftxt;
std::string empty("");
unsigned int i;
std::list<cert *>::iterator it;
for(it = peercerts.begin(); it != peercerts.end(); it++)
#ifdef AUTHXPGP_DEBUG
std::cerr << "AuthXPGP::saveCertificates()";
std::cerr << std::endl;
#endif
xpgpMtx.lock(); /***** LOCK *****/
/* iterate through both lists */
std::map<std::string, xpgpcert *>::iterator it;
for(it = mCerts.begin(); it != mCerts.end(); it++)
{
std::string neighfile = neighdir + getCertName(*it) + ".pqi";
savecertificate((*it), neighfile.c_str());
conftxt += "CERT ";
conftxt += getCertName(*it);
conftxt += "\n";
conftxt += (*it) -> Hash();
conftxt += "\n";
std::cerr << std::endl;
if (it->second->trustLvl > TRUST_SIGN_BASIC)
{
XPGP *xpgp = it->second->certificate;
std::string hash;
std::string neighfile = neighdir + getCertName(xpgp) + ".pqi";
if (saveXPGPToFile(xpgp, neighfile, hash))
{
conftxt += "CERT ";
conftxt += getCertName(xpgp);
conftxt += "\n";
conftxt += hash;
conftxt += "\n";
std::cerr << std::endl;
}
}
}
// now work out signature of it all. This relies on the
// EVP library of openSSL..... We are going to use signing
// for the moment.
@ -1631,38 +1676,76 @@ int AuthXPGP::saveCertificates(const char *fname)
}
std::cerr << std::endl;
FILE *cfd = fopen(configname.c_str(), "wb");
FILE *cfd = fopen(configfile.c_str(), "wb");
int wrec;
if (1 != (wrec = fwrite(conftxt.c_str(), conftxt.length(), 1, cfd)))
{
std::cerr << "Error writing: " << configname << std::endl;
std::cerr << "Error writing: " << configfile << std::endl;
std::cerr << "Wrote: " << wrec << "/" << 1 << " Records" << std::endl;
}
EVP_MD_CTX_destroy(mdctx);
fclose(cfd);
return 1;
xpgpMtx.unlock(); /**** UNLOCK ****/
return true;
}
int sslroot::loadCertificates(const char *conf_fname)
/******
* Special version for backwards compatibility
*
* has two extra parameters.
* bool oldFormat & std::map<std::string, std::string> keyvaluemap
*
* We'll leave these in for the next couple of months...
* so that old versions will automatically be converted to the
* new format!
*
*/
bool AuthXPGP::loadCertificates()
{
// open the configuration file.
//
// read in CERT + Hash.
bool oldFormat;
std::map<std::string, std::string> keyValueMap;
// construct file name.
//
// create the file in memory - hash + sign.
// write out data to a file.
return loadCertificates(oldFormat, keyValueMap);
}
/*********************
* NOTE no need to Lock here. locking handled in ProcessXPGP()
*/
static const uint32_t OPT_LEN = 16;
static const uint32_t VAL_LEN = 1000;
bool AuthXPGP::loadCertificates(bool &oldFormat, std::map<std::string, std::string> &keyValueMap)
{
/*******************************************
* open the configuration file.
* read in CERT + Hash.
*
* construct file name.
* create the file in memory - hash + sign.
* write out data to a file.
*****************************************/
xpgpMtx.lock(); /***** LOCK *****/
std::string configfile = mCertConfigFile;
std::string neighdir = mNeighDir;
xpgpMtx.unlock(); /**** UNLOCK ****/
/* add on the slash */
if (neighdir != "")
{
neighdir += "/";
}
oldFormat = false;
std::string neighdir = certdir + "/" + neighbourdir + "/";
std::string configname = certdir + "/";
configname += conf_fname;
// save name for later save attempts.
certfile = conf_fname;
std::string conftxt;
unsigned int maxnamesize = 1024;
@ -1671,12 +1754,12 @@ int sslroot::loadCertificates(const char *conf_fname)
int c;
unsigned int i;
FILE *cfd = fopen(configname.c_str(), "rb");
FILE *cfd = fopen(configfile.c_str(), "rb");
if (cfd == NULL)
{
std::cerr << "Unable to Load Configuration File!" << std::endl;
std::cerr << "File: " << configname << std::endl;
return -1;
std::cerr << "File: " << configfile << std::endl;
return false;
}
std::list<std::string> fnames;
@ -1807,9 +1890,6 @@ int sslroot::loadCertificates(const char *conf_fname)
// continue...
for(i = 0; (name[i] != '\n') && (i < signlen); i++);
//printf("Stepping over [%d] %0x\n", i, name[i]);
if (i != signlen)
{
for(i++; i < signlen; i++)
@ -1881,32 +1961,34 @@ int sslroot::loadCertificates(const char *conf_fname)
{
std::cerr << "ERROR VALIDATING CONFIGURATION!" << std::endl;
std::cerr << "PLEASE FIX!" << std::endl;
return -1;
return false;
}
std::list<std::string>::iterator it;
std::list<std::string>::iterator it2;
for(it = fnames.begin(), it2 = hashes.begin(); it != fnames.end(); it++, it2++)
{
std::string neighfile = neighdir + (*it) + ".pqi";
cert *nc = loadcertificate(neighfile.c_str(), (*it2));
if (nc != NULL)
XPGP *xpgp = loadXPGPFromFile(neighfile, (*it2));
if (xpgp != NULL)
{
if (0 > addCertificate(nc))
std::string id;
if (ProcessXPGP(xpgp, id))
{
// cleanup.
std::cerr << "Updated Certificate....but no";
std::cerr << " need for addition";
std::cerr << "Loaded Certificate: " << id;
std::cerr << std::endl;
// X509_free(nc -> certificate);
//delete nc;
}
}
}
for(mit = tmpsettings.begin(); mit != tmpsettings.end(); mit++)
{
settings[mit -> first] = mit -> second;
keyValueMap[mit -> first] = mit -> second;
}
return 1;
if (keyValueMap.size() > 0)
{
oldFormat = true;
}
return true;
}
#endif

View file

@ -84,7 +84,7 @@ virtual bool active();
virtual int InitAuth(const char *srvr_cert, const char *priv_key,
const char *passwd);
virtual bool CloseAuth();
virtual int setConfigDirectories(const char *cdir, const char *ndir);
virtual int setConfigDirectories(std::string confFile, std::string neighDir);
/*********** Overloaded Functions from p3AuthMgr **********/
@ -102,8 +102,11 @@ virtual bool isAuthenticated(std::string id);
virtual std::string getName(std::string id);
virtual bool getDetails(std::string id, pqiAuthDetails &details);
/* Load/Save certificates */
/* High Level Load/Save Configuration */
virtual bool saveCertificates();
virtual bool loadCertificates();
/* Load/Save certificates */
virtual bool LoadCertificateFromString(std::string pem, std::string &id);
virtual std::string SaveCertificateToString(std::string id);
virtual bool LoadCertificateFromFile(std::string filename, std::string &id);
@ -131,6 +134,8 @@ bool ValidateCertificateXPGP(XPGP *xpgp, std::string &peerId); /* validate + ge
bool FailedCertificateXPGP(XPGP *xpgp, bool incoming); /* store for discovery */
bool CheckCertificateXPGP(std::string peerId, XPGP *xpgp); /* check that they are exact match */
/* Special Config Loading (backwards compatibility) */
bool loadCertificates(bool &oldFormat, std::map<std::string, std::string> &keyValueMap);
private:
@ -154,7 +159,7 @@ bool locked_FindCert(std::string id, xpgpcert **cert);
RsMutex xpgpMtx; /**** LOCKING */
int init;
std::string mCertDir;
std::string mCertConfigFile;
std::string mNeighDir;
SSL_CTX *sslctx;

View file

@ -102,7 +102,7 @@ bool p3DummyAuthMgr::CloseAuth()
return true;
}
int p3DummyAuthMgr::setConfigDirectories(const char *cdir, const char *ndir)
int p3DummyAuthMgr::setConfigDirectories(std::string confFile, std::string neighDir)
{
return 1;
}
@ -187,6 +187,16 @@ bool p3DummyAuthMgr::getDetails(std::string id, pqiAuthDetails &details)
return false;
}
bool p3DummyAuthMgr::saveCertificates()
{
return false;
}
bool p3DummyAuthMgr::loadCertificates()
{
return false;
}
bool p3DummyAuthMgr::LoadCertificateFromString(std::string pem, std::string &id)
{
return false;

View file

@ -79,7 +79,7 @@ virtual bool active() = 0;
virtual int InitAuth(const char *srvr_cert, const char *priv_key,
const char *passwd) = 0;
virtual bool CloseAuth() = 0;
virtual int setConfigDirectories(const char *cdir, const char *ndir) = 0;
virtual int setConfigDirectories(std::string confFile, std::string neighDir) = 0;
/* get Certificate Ids */
@ -95,6 +95,10 @@ virtual bool isAuthenticated(std::string id) = 0;
virtual std::string getName(std::string id) = 0;
virtual bool getDetails(std::string id, pqiAuthDetails &details) = 0;
/* High Level Load/Save Configuration */
virtual bool saveCertificates() = 0;
virtual bool loadCertificates() = 0;
/* Load/Save certificates */
virtual bool LoadCertificateFromString(std::string pem, std::string &id) = 0;
@ -117,6 +121,8 @@ virtual bool TrustCertificate(std::string id, bool trust) = 0;
/* Sign / Encrypt / Verify Data (TODO) */
//virtual bool encryptData(std::string recipientId, std::string plaindata, std::string &result);
};
@ -132,7 +138,7 @@ virtual bool active();
virtual int InitAuth(const char *srvr_cert, const char *priv_key,
const char *passwd);
virtual bool CloseAuth();
virtual int setConfigDirectories(const char *cdir, const char *ndir);
virtual int setConfigDirectories(std::string confFile, std::string neighDir);
/* get Certificate Ids */
@ -148,8 +154,11 @@ virtual bool isAuthenticated(std::string id);
virtual std::string getName(std::string id);
virtual bool getDetails(std::string id, pqiAuthDetails &details);
/* Load/Save certificates */
/* High Level Load/Save Configuration */
virtual bool saveCertificates();
virtual bool loadCertificates();
/* Load/Save certificates */
virtual bool LoadCertificateFromString(std::string pem, std::string &id);
virtual std::string SaveCertificateToString(std::string id);
virtual bool LoadCertificateFromFile(std::string filename, std::string &id);
@ -164,6 +173,7 @@ virtual bool SignCertificate(std::string id);
virtual bool RevokeCertificate(std::string id);
virtual bool TrustCertificate(std::string id, bool trust);
std::string mOwnId;
std::map<std::string, pqiAuthDetails> mPeerList;
};

View file

@ -26,13 +26,15 @@
#include "pqi/p3cfgmgr.h"
#include "pqi/pqibin.h"
#include "pqi/pqiarchive.h"
#include "pqi/pqistreamer.h"
#include "serialiser/rsconfigitems.h"
#define CONFIG_DEBUG 1
p3ConfigMgr::p3ConfigMgr(std::string dir, std::string fname, std::string signame)
:basedir(dir), metafname(fname), metasigfname(signame)
:basedir(dir), metafname(fname), metasigfname(signame),
mConfigSaveActive(true)
{
@ -42,16 +44,33 @@ void p3ConfigMgr::tick()
{
bool toSave = false;
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
/* iterate through and check if any have changed */
std::map<uint32_t, pqiConfig *>::iterator it;
for(it = configs.begin(); it != configs.end(); it++)
{
if (it->second->ConfInd.Changed(0))
if (it->second->HasConfigChanged(0))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::tick() Config Changed - Element: ";
std::cerr << it->first;
std::cerr << std::endl;
#endif
toSave = true;
}
}
/* disable saving before exit */
if (!mConfigSaveActive)
{
toSave = false;
}
}
if (toSave)
{
saveConfiguration();
@ -61,67 +80,221 @@ void p3ConfigMgr::tick()
void p3ConfigMgr::saveConfiguration()
{
/* setup metaconfig */
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration()";
std::cerr << std::endl;
#endif
RsConfigKeyValueSet *item = new RsConfigKeyValueSet();
std::map<uint32_t, pqiConfig *>::iterator it;
for(it = configs.begin(); it != configs.end(); it++)
{
if (it->second->ConfInd.Changed(1))
if (it->second->HasConfigChanged(1))
{
it->second->saveConfiguration(basedir);
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Saving Element: ";
std::cerr << it->first;
std::cerr << std::endl;
#endif
it->second->saveConfiguration();
}
/* save metaconfig */
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Element: ";
std::cerr << it->first << " Hash: " << it->second->Hash();
std::cerr << std::endl;
#endif
RsTlvKeyValue kv;
{
std::ostringstream out;
out << it->first;
kv.key = out.str();
}
kv.value = it->second->Hash();
item->tlvkvs.pairs.push_back(kv);
}
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::saveConfiguration() Complete MetaConfigItem: ";
std::cerr << std::endl;
item->print(std::cerr, 20);
#endif
/* Write the data to a stream */
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
BinInterface *bio = new BinFileInterface(metafname.c_str(), bioflags);
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsGeneralConfigSerialiser());
pqistreamer stream(rss, "CONFIG", bio, 0);
stream.SendItem(item);
stream.tick();
stream.tick();
/* get hash */
std::string totalhash = bio->gethash();
/* sign the hash of the data */
/* write signature to configuration */
}
void p3ConfigMgr::loadConfiguration()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::loadConfiguration()";
std::cerr << std::endl;
#endif
/* Write the data to a stream */
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
BinInterface *bio = new BinFileInterface(metafname.c_str(), bioflags);
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsGeneralConfigSerialiser());
pqistreamer stream(rss, "CONFIG", bio, 0);
stream.tick();
stream.tick();
RsItem *rsitem = stream.GetItem();
RsConfigKeyValueSet *item = dynamic_cast<RsConfigKeyValueSet *>(rsitem);
if (!item)
{
delete rsitem;
return;
}
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::loadConfiguration() Loaded MetaConfigItem: ";
std::cerr << std::endl;
item->print(std::cerr, 20);
#endif
std::string totalhash = bio->gethash();
/* check it TODO */
/* sign the hash of the data */
/* check signature with configuration */
/* extract info from KeyValueSet */
std::list<RsTlvKeyValue>::iterator it;
for(it = item->tlvkvs.pairs.begin(); it != item->tlvkvs.pairs.end(); it++)
{
/* find the configuration */
uint32_t confId = atoi(it->key.c_str());
std::string hashin = it->value;
std::map<uint32_t, pqiConfig *>::iterator cit;
cit = configs.find(confId);
if (cit != configs.end())
{
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::loadConfiguration() Element: ";
std::cerr << confId << " Hash: " << hashin;
std::cerr << std::endl;
#endif
(cit->second)->loadConfiguration(hashin);
/* force config to NOT CHANGED */
cit->second->HasConfigChanged(0);
cit->second->HasConfigChanged(1);
}
}
#ifdef CONFIG_DEBUG
std::cerr << "p3ConfigMgr::loadConfiguration() Done!";
std::cerr << std::endl;
#endif
}
void p3ConfigMgr::addConfiguration(uint32_t type, pqiConfig *conf)
void p3ConfigMgr::addConfiguration(std::string file, pqiConfig *conf)
{
configs[type] = conf;
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
/* construct filename */
std::string filename = basedir;
if (basedir != "")
{
filename += "/";
}
filename += file;
conf->setFilename(filename);
configs[conf->Type()] = conf;
}
p3Config::p3Config(uint32_t t, std::string name)
:pqiConfig(t, name)
void p3ConfigMgr::completeConfiguration()
{
saveConfiguration();
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
mConfigSaveActive = false;
}
p3Config::p3Config(uint32_t t)
:pqiConfig(t)
{
return;
}
bool p3Config::loadConfiguration(std::string basedir, std::string &loadHash)
bool p3Config::loadConfiguration(std::string &loadHash)
{
std::list<RsItem *> load;
std::list<RsItem *>::iterator it;
std::string fname = basedir;
if (fname != "")
fname += "/";
fname += Filename();
std::string fname = Filename();
BinFileInterface *bio = new BinFileInterface(fname.c_str(),
BIN_FLAGS_READABLE | BIN_FLAGS_HASH_DATA);
pqiarchive archive(setupSerialiser(), bio, BIN_FLAGS_READABLE);
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_READABLE;
uint32_t stream_flags = BIN_FLAGS_READABLE;
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
pqistreamer stream(setupSerialiser(), "CONFIG", bio, stream_flags);
RsItem *item = NULL;
while(NULL != (item = archive.GetItem()))
stream.tick();
while(NULL != (item = stream.GetItem()))
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfiguration() loaded item:";
std::cerr << std::endl;
item->print(std::cerr, 0);
std::cerr << std::endl;
#endif
load.push_back(item);
stream.tick();
}
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfiguration() loaded " << load.size();
std::cerr << " Elements from File: " << fname;
std::cerr << std::endl;
#endif
/* check hash */
std::string hashstr = archive.gethash();
std::string hashstr = bio->gethash();
if (hashstr != loadHash)
{
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::loadConfiguration() ERROR: Hash != MATCHloaded";
std::cerr << std::endl;
#endif
/* bad load */
for(it = load.begin(); it != load.end(); it++)
{
@ -137,32 +310,49 @@ bool p3Config::loadConfiguration(std::string basedir, std::string &loadHash)
}
bool p3Config::saveConfiguration(std::string basedir)
bool p3Config::saveConfiguration()
{
bool toKeep;
std::list<RsItem *> toSave = saveList(toKeep);
std::string fname = basedir;
if (fname != "")
fname += "/";
fname += Filename();
bool cleanup = true;
std::list<RsItem *> toSave = saveList(cleanup);
BinFileInterface *bio = new BinFileInterface(fname.c_str(),
BIN_FLAGS_WRITEABLE | BIN_FLAGS_HASH_DATA);
std::string fname = Filename();
uint32_t arch_flags = BIN_FLAGS_WRITEABLE;
if (toKeep)
arch_flags |= BIN_FLAGS_NO_DELETE;
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::saveConfiguration() toSave " << toSave.size();
std::cerr << " Elements to File: " << fname;
std::cerr << std::endl;
#endif
pqiarchive archive(setupSerialiser(), bio, arch_flags);
uint32_t bioflags = BIN_FLAGS_HASH_DATA | BIN_FLAGS_WRITEABLE;
uint32_t stream_flags = BIN_FLAGS_WRITEABLE;
if (!cleanup)
stream_flags |= BIN_FLAGS_NO_DELETE;
BinInterface *bio = new BinFileInterface(fname.c_str(), bioflags);
pqistreamer stream(setupSerialiser(), "CONFIG", bio, stream_flags);
std::list<RsItem *>::iterator it;
for(it = toSave.begin(); it != toSave.end(); it++)
{
archive.SendItem(*it);
#ifdef CONFIG_DEBUG
std::cerr << "p3Config::saveConfiguration() save item:";
std::cerr << std::endl;
(*it)->print(std::cerr, 0);
std::cerr << std::endl;
#endif
stream.SendItem(*it);
stream.tick();
}
/* final tick for anymore writing */
stream.tick();
/* store the hash */
setHash(archive.gethash());
setHash(bio->gethash());
saveDone(); /* callback to inherited class to unlock any Mutexes
* protecting saveList() data
*/
/* else okay */
return true;
@ -172,7 +362,7 @@ bool p3Config::saveConfiguration(std::string basedir)
/**************************** CONFIGURATION CLASSES ********************/
p3GeneralConfig::p3GeneralConfig()
:p3Config(CONFIG_TYPE_GENERAL, "gen.set")
:p3Config(CONFIG_TYPE_GENERAL)
{
return;
}
@ -180,6 +370,12 @@ p3GeneralConfig::p3GeneralConfig()
// General Configuration System
std::string p3GeneralConfig::getSetting(std::string opt)
{
#ifdef CONFIG_DEBUG
std::cerr << "p3GeneralConfig::getSetting(" << opt << ")";
std::cerr << std::endl;
#endif
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
/* extract from config */
std::map<std::string, std::string>::iterator it;
if (settings.end() == (it = settings.find(opt)))
@ -192,6 +388,13 @@ std::string p3GeneralConfig::getSetting(std::string opt)
void p3GeneralConfig::setSetting(std::string opt, std::string val)
{
#ifdef CONFIG_DEBUG
std::cerr << "p3GeneralConfig::setSetting(" << opt << " = " << val << ")";
std::cerr << std::endl;
#endif
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
/* extract from config */
std::map<std::string, std::string>::iterator it;
if (settings.end() != (it = settings.find(opt)))
@ -203,40 +406,149 @@ void p3GeneralConfig::setSetting(std::string opt, std::string val)
}
}
ConfInd.IndicateChanged();
settings[opt] = val;
}
/* outside mutex */
IndicateConfigChanged();
return;
}
/* TODO ******/
RsSerialiser *p3GeneralConfig::setupSerialiser()
{
RsSerialiser *rss = NULL;
RsSerialiser *rss = new RsSerialiser();
rss->addSerialType(new RsGeneralConfigSerialiser());
return rss;
}
std::list<RsItem *> p3GeneralConfig::saveList(bool &cleanup)
{
cleanup = false;
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
#ifdef CONFIG_DEBUG
std::cerr << "p3GeneralConfig::saveList() KV sets: " << settings.size();
std::cerr << std::endl;
#endif
cleanup = true;
std::list<RsItem *> savelist;
//RsGenConfItem *item = ...
RsConfigKeyValueSet *item = new RsConfigKeyValueSet();
std::map<std::string, std::string>::iterator it;
for(it = settings.begin(); it != settings.end(); it++)
{
RsTlvKeyValue kv;
kv.key = it->first;
kv.value = it->second;
item->tlvkvs.pairs.push_back(kv);
/* make sure we don't overload it */
if (item->tlvkvs.TlvSize() > 4000)
{
savelist.push_back(item);
item = new RsConfigKeyValueSet();
}
}
if (item->tlvkvs.pairs.size() > 0)
{
savelist.push_back(item);
}
return savelist;
}
bool p3GeneralConfig::loadList(std::list<RsItem *> load)
{
/* add into settings */
#ifdef CONFIG_DEBUG
std::cerr << "p3GeneralConfig::loadList() count: " << load.size();
std::cerr << std::endl;
#endif
return false;
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
/* add into settings */
RsConfigKeyValueSet *item = NULL;
std::list<RsItem *>::iterator it;
std::list<RsTlvKeyValue>::iterator kit;
for(it = load.begin(); it != load.end();)
{
item = dynamic_cast<RsConfigKeyValueSet *>(*it);
if (item)
{
for(kit = item->tlvkvs.pairs.begin();
kit != item->tlvkvs.pairs.end(); kit++)
{
settings[kit->key] = kit->value;
}
}
/* cleanup */
delete (*it);
it = load.erase(it);
}
return true;
}
/**** MUTEX NOTE:
* have protected all, but think that
* only the Indication and hash really need it
*/
pqiConfig::pqiConfig(uint32_t t)
:ConfInd(2), type(t)
{
return;
}
pqiConfig::~pqiConfig()
{
return;
}
uint32_t pqiConfig::Type()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
return type;
}
std::string pqiConfig::Filename()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
return filename;
}
std::string pqiConfig::Hash()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
return hash;
}
void pqiConfig::IndicateConfigChanged()
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
ConfInd.IndicateChanged();
}
bool pqiConfig::HasConfigChanged(uint16_t idx)
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
return ConfInd.Changed(idx);
}
void pqiConfig::setFilename(std::string name)
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
filename = name;
}
void pqiConfig::setHash(std::string h)
{
RsStackMutex stack(cfgMtx); /***** LOCK STACK MUTEX ****/
hash = h;
}

View file

@ -34,6 +34,7 @@
#include "pqi/pqi_base.h"
#include "pqi/pqiindic.h"
#include "pqi/pqinetwork.h"
#include "util/rsthreads.h"
/***** Configuration Management *****
*
@ -52,40 +53,61 @@
*
*/
/**** THESE are STORED in CONFIGURATION FILES....
* Cannot be changed
*
*********************/
const uint32_t CONFIG_TYPE_GENERAL = 0x0001;
const uint32_t CONFIG_TYPE_STUNLIST = 0x0002;
const uint32_t CONFIG_TYPE_PEERS = 0x0002;
const uint32_t CONFIG_TYPE_FSERVER = 0x0003;
const uint32_t CONFIG_TYPE_PEERS = 0x0004;
const uint32_t CONFIG_TYPE_MSGS = 0x0004;
class p3ConfigMgr;
class pqiConfig
{
public:
pqiConfig(uint32_t t, std::string defaultname)
:ConfInd(2), type(t), filename(defaultname)
{
return;
}
virtual ~pqiConfig() { return; }
pqiConfig(uint32_t t);
virtual ~pqiConfig();
virtual bool loadConfiguration(std::string filename, std::string &load) = 0;
virtual bool saveConfiguration(std::string filename) = 0;
virtual bool loadConfiguration(std::string &loadHash) = 0;
virtual bool saveConfiguration() = 0;
uint32_t Type();
std::string Filename();
std::string Hash();
protected:
void IndicateConfigChanged();
void setHash(std::string h);
RsMutex cfgMtx;
private:
void setFilename(std::string name);
bool HasConfigChanged(uint16_t idx);
Indicator ConfInd;
uint32_t Type() { return type; }
std::string Filename() { return filename; }
std::string Hash() { return hash; }
protected:
void setHash(std::string h) { hash = h; }
private:
uint32_t type;
std::string filename;
std::string hash;
friend class p3ConfigMgr;
/* so it can access:
* setFilename() and HasConfigChanged()
*/
};
/**** MUTEX NOTE
* None - because no-one calls any functions
* besides tick() when the system is running.
*/
class p3ConfigMgr
{
public:
@ -94,16 +116,25 @@ class p3ConfigMgr
void tick();
void saveConfiguration();
void loadConfiguration();
void addConfiguration(uint32_t type, pqiConfig *conf);
void addConfiguration(std::string file, pqiConfig *conf);
/* saves config, and disables further saving
* used for exiting the system
*/
void completeConfiguration();
private:
/* these are constants - so shouldn't need mutex */
const std::string basedir;
const std::string metafname;
const std::string metasigfname;
RsMutex cfgMtx; /* below is protected */
bool mConfigSaveActive;
std::map<uint32_t, pqiConfig *> configs;
std::string basedir;
std::string metafname;
std::string metasigfname;
};
@ -111,10 +142,10 @@ class p3Config: public pqiConfig
{
public:
p3Config(uint32_t t, std::string name);
p3Config(uint32_t t);
virtual bool loadConfiguration(std::string basedir, std::string &loadHash);
virtual bool saveConfiguration(std::string basedir);
virtual bool loadConfiguration(std::string &loadHash);
virtual bool saveConfiguration();
protected:
@ -122,7 +153,11 @@ virtual bool saveConfiguration(std::string basedir);
virtual RsSerialiser *setupSerialiser() = 0;
virtual std::list<RsItem *> saveList(bool &cleanup) = 0;
virtual bool loadList(std::list<RsItem *> load) = 0;
/**
* callback for mutex unlocking
* in derived classes (should only be needed if cleanup = false)
*/
virtual void saveDone() { return; }
}; /* end of p3Config */
@ -145,6 +180,7 @@ virtual bool loadList(std::list<RsItem *> load);
private:
/* protected by pqiConfig mutex as well! */
std::map<std::string, std::string> settings;
};

View file

@ -27,6 +27,8 @@
#include "tcponudp/tou.h"
#include "util/rsprint.h"
#include "serialiser/rsconfigitems.h"
/* Network setup States */
const uint32_t RS_NET_UNKNOWN = 0x0001;
@ -45,23 +47,6 @@ const uint32_t MAX_UPNP_INIT = 10; /* seconds UPnP timeout */
#define CONN_DEBUG 1
p3ConnectMgr::p3ConnectMgr(p3AuthMgr *am)
:p3Config(CONFIG_TYPE_PEERS, "peers.cfg"),
mAuthMgr(am), mDhtMgr(NULL), mUpnpMgr(NULL), mNetStatus(RS_NET_UNKNOWN),
mStunStatus(0), mStatusChanged(false)
{
mUpnpAddrValid = false;
mStunAddrValid = false;
/* setup basics of own state */
if (am)
{
ownState.id = mAuthMgr->OwnId();
ownState.name = mAuthMgr->getName(ownState.id);
}
return;
}
peerConnectAddress::peerConnectAddress()
:type(0), ts(0)
@ -78,22 +63,56 @@ peerAddrInfo::peerAddrInfo()
}
peerConnectState::peerConnectState()
:id("unknown"), name("nameless"), state(0), actions(0),
:id("unknown"),
netMode(RS_NET_MODE_UNKNOWN), visState(RS_VIS_STATE_STD),
source(0),
inConnAttempt(0), connAttemptTS(0),
lastcontact(0)
lastcontact(0),
//lc_timestamp(0), lr_timestamp(0),
//nc_timestamp(0), nc_timeintvl(0)
name("nameless"), state(0), actions(0),
source(0),
inConnAttempt(0), connAttemptTS(0)
{
sockaddr_clear(&lastaddr);
sockaddr_clear(&localaddr);
sockaddr_clear(&serveraddr);
return;
}
p3ConnectMgr::p3ConnectMgr(p3AuthMgr *am)
:p3Config(CONFIG_TYPE_PEERS),
mAuthMgr(am), mDhtMgr(NULL), mUpnpMgr(NULL), mNetStatus(RS_NET_UNKNOWN),
mStunStatus(0), mStatusChanged(false)
{
mUpnpAddrValid = false;
mStunAddrValid = false;
/* setup basics of own state */
if (am)
{
ownState.id = mAuthMgr->OwnId();
ownState.name = mAuthMgr->getName(ownState.id);
ownState.netMode = RS_NET_MODE_UDP;
}
return;
}
void p3ConnectMgr::setOwnNetConfig(uint32_t netMode, uint32_t visState)
{
ownState.netMode = netMode;
ownState.visState = visState;
/* if we've started up - then tweak Dht On/Off */
if (mNetStatus != RS_NET_UNKNOWN)
{
mDhtMgr->setDhtOn(!(ownState.visState & RS_VIS_STATE_NODHT));
}
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
/***** Framework / initial implementation for a connection manager.
*
* This needs a state machine for Initialisation.
@ -374,6 +393,7 @@ void p3ConnectMgr::netUdpCheck()
{
mode |= RS_NET_CONN_TCP_EXTERNAL;
}
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
connMtx.unlock(); /* UNLOCK MUTEX */
@ -567,6 +587,8 @@ void p3ConnectMgr::stunCollect(std::string id, struct sockaddr_in addr, uint32_t
#endif
/* push to the front */
mStunList.push_front(id);
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
}
else
@ -581,6 +603,8 @@ void p3ConnectMgr::stunCollect(std::string id, struct sockaddr_in addr, uint32_t
/* move to front */
mStunList.erase(it);
mStunList.push_front(id);
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
}
@ -1036,6 +1060,7 @@ void p3ConnectMgr::peerStatus(std::string id,
}
/* not updating VIS status??? */
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
else if (source == RS_CB_PERSON)
{
@ -1083,6 +1108,7 @@ void p3ConnectMgr::peerStatus(std::string id,
{
it->second.visState |= RS_VIS_STATE_NODHT;
}
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
if (!isFriend)
@ -1216,7 +1242,7 @@ void p3ConnectMgr::peerConnectRequest(std::string id, uint32_t type)
/*******************************************************************/
/*******************************************************************/
bool p3ConnectMgr::addFriend(std::string id)
bool p3ConnectMgr::addFriend(std::string id, uint32_t netMode, uint32_t visState, time_t lastContact)
{
/* so three possibilities
* (1) already exists as friend -> do nothing.
@ -1276,12 +1302,18 @@ bool p3ConnectMgr::addFriend(std::string id)
it->second.state = RS_PEER_S_FRIEND;
it->second.actions = RS_PEER_NEW;
/* setup connectivity parameters */
it->second.visState = visState;
it->second.netMode = netMode;
it->second.lastcontact = lastContact;
mStatusChanged = true;
/* add peer to DHT (if not dark) */
if (it->second.visState & RS_VIS_STATE_NODHT)
{
/* hidden from DHT world */
mDhtMgr->dropPeer(id);
}
else
{
@ -1289,6 +1321,9 @@ bool p3ConnectMgr::addFriend(std::string id)
}
connMtx.unlock(); /* UNLOCK MUTEX */
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
@ -1320,8 +1355,9 @@ bool p3ConnectMgr::addFriend(std::string id)
pstate.state = RS_PEER_S_FRIEND;
pstate.actions = RS_PEER_NEW;
pstate.visState = RS_VIS_STATE_STD;
pstate.netMode = RS_NET_MODE_UNKNOWN;
pstate.visState = visState;
pstate.netMode = netMode;
pstate.lastcontact = lastContact;
/* addr & timestamps -> auto cleared */
@ -1334,6 +1370,8 @@ bool p3ConnectMgr::addFriend(std::string id)
connMtx.unlock(); /* UNLOCK MUTEX */
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
@ -1373,6 +1411,8 @@ bool p3ConnectMgr::removeFriend(std::string id)
connMtx.unlock(); /* UNLOCK MUTEX */
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return success;
}
@ -1611,6 +1651,7 @@ bool p3ConnectMgr::setLocalAddress(std::string id, struct sockaddr_in addr)
if (id == mAuthMgr->OwnId())
{
ownState.localaddr = addr;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
@ -1626,6 +1667,7 @@ bool p3ConnectMgr::setLocalAddress(std::string id, struct sockaddr_in addr)
/* "it" points to peer */
it->second.localaddr = addr;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
@ -1637,6 +1679,7 @@ bool p3ConnectMgr::setExtAddress(std::string id, struct sockaddr_in addr)
if (id == mAuthMgr->OwnId())
{
ownState.serveraddr = addr;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
@ -1652,17 +1695,93 @@ bool p3ConnectMgr::setExtAddress(std::string id, struct sockaddr_in addr)
/* "it" points to peer */
it->second.serveraddr = addr;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
return true;
}
bool p3ConnectMgr::setNetworkMode(std::string id, uint32_t netMode)
{
if (id == mAuthMgr->OwnId())
{
uint32_t visState = ownState.visState;
setOwnNetConfig(netMode, visState);
return true;
}
/* check if it is a friend */
std::map<std::string, peerConnectState>::iterator it;
if (mFriendList.end() == (it = mFriendList.find(id)))
{
if (mOthersList.end() == (it = mOthersList.find(id)))
{
return false;
}
}
/* "it" points to peer */
it->second.netMode = netMode;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
connMtx.lock(); /* LOCK MUTEX */
connMtx.unlock(); /* UNLOCK MUTEX */
return false;
}
bool p3ConnectMgr::setVisState(std::string id, uint32_t visState)
{
if (id == mAuthMgr->OwnId())
{
uint32_t netMode = ownState.netMode;
setOwnNetConfig(netMode, visState);
return true;
}
/* check if it is a friend */
std::map<std::string, peerConnectState>::iterator it;
bool isFriend = false;
if (mFriendList.end() == (it = mFriendList.find(id)))
{
if (mOthersList.end() == (it = mOthersList.find(id)))
{
return false;
}
}
else
{
isFriend = true;
}
/* "it" points to peer */
it->second.visState = visState;
if (isFriend)
{
/* toggle DHT state */
if (it->second.visState & RS_VIS_STATE_NODHT)
{
/* hidden from DHT world */
mDhtMgr->dropPeer(id);
}
else
{
mDhtMgr->findPeer(id);
}
}
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
connMtx.lock(); /* LOCK MUTEX */
connMtx.unlock(); /* UNLOCK MUTEX */
return false;
}
bool p3ConnectMgr::getUPnPState()
{
return mUpnpMgr->getUPnPActive();
@ -1717,6 +1836,8 @@ bool p3ConnectMgr::checkNetAddress()
std::cerr << "p3ConnectMgr::checkNetAddress() Local Address Not Found: Using Preferred Interface: ";
std::cerr << inet_ntoa(ownState.localaddr.sin_addr);
std::cerr << std::endl;
IndicateConfigChanged(); /**** INDICATE MSG CONFIG CHANGED! *****/
}
if ((isPrivateNet(&(ownState.localaddr.sin_addr))) ||
@ -1751,7 +1872,6 @@ bool p3ConnectMgr::checkNetAddress()
// ensure that address family is set, otherwise windows Barfs.
ownState.localaddr.sin_family = AF_INET;
ownState.serveraddr.sin_family = AF_INET;
ownState.lastaddr.sin_family = AF_INET;
std::cerr << "p3ConnectMgr::checkNetAddress() Final Local Address: ";
std::cerr << inet_ntoa(ownState.localaddr.sin_addr);
@ -1766,12 +1886,10 @@ bool p3ConnectMgr::checkNetAddress()
/*******************************************************************/
/* Key Functions to be overloaded for Full Configuration */
/**** TODO ****/
RsSerialiser *p3ConnectMgr::setupSerialiser()
{
RsSerialiser *rss = new RsSerialiser();
//rss->addSerialType(new RsConfigSerialiser());
rss->addSerialType(new RsPeerConfigSerialiser());
return rss;
}
@ -1782,101 +1900,129 @@ std::list<RsItem *> p3ConnectMgr::saveList(bool &cleanup)
/* create a list of current peers */
std::list<RsItem *> saveData;
cleanup = true;
connMtx.lock(); /* LOCK MUTEX */
connMtx.unlock(); /* UNLOCK MUTEX */
RsPeerNetItem *item = new RsPeerNetItem();
item->clear();
item->pid = getOwnId();
item->netMode = ownState.netMode;
item->visState = ownState.visState;
item->lastContact = ownState.lastcontact;
item->localaddr = ownState.localaddr;
item->remoteaddr = ownState.serveraddr;
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::saveList() Own Config Item:";
std::cerr << std::endl;
item->print(std::cerr, 10);
std::cerr << std::endl;
#endif
saveData.push_back(item);
/* iterate through all friends and save */
std::map<std::string, peerConnectState>::iterator it;
for(it = mFriendList.begin(); it != mFriendList.end(); it++)
{
item = new RsPeerNetItem();
item->clear();
item->pid = it->first;
item->netMode = (it->second).netMode;
item->visState = (it->second).visState;
item->lastContact = (it->second).lastcontact;
item->localaddr = (it->second).localaddr;
item->remoteaddr = (it->second).serveraddr;
saveData.push_back(item);
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::saveList() Peer Config Item:";
std::cerr << std::endl;
item->print(std::cerr, 10);
std::cerr << std::endl;
#endif
}
RsPeerStunItem *sitem = new RsPeerStunItem();
std::list<std::string>::iterator sit;
for(sit = mStunList.begin(); sit != mStunList.end(); sit++)
{
sitem->stunList.ids.push_back(*sit);
}
saveData.push_back(sitem);
return saveData;
}
bool p3ConnectMgr::loadList(std::list<RsItem *> load)
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::loadList() Item Count: " << load.size();
std::cerr << std::endl;
#endif
/* load the list of peers */
std::list<RsItem *>::iterator it;
for(it = load.begin(); it != load.end(); it++)
{
RsPeerNetItem *pitem = dynamic_cast<RsPeerNetItem *>(*it);
RsPeerStunItem *sitem = dynamic_cast<RsPeerStunItem *>(*it);
connMtx.lock(); /* LOCK MUTEX */
connMtx.unlock(); /* UNLOCK MUTEX */
if (pitem)
{
if (pitem->pid == getOwnId())
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::loadList() Own Config Item:";
std::cerr << std::endl;
pitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
/* add ownConfig */
setOwnNetConfig(pitem->netMode, pitem->visState);
setLocalAddress(pitem->pid, pitem->localaddr);
setExtAddress(pitem->pid, pitem->remoteaddr);
}
else
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::loadList() Peer Config Item:";
std::cerr << std::endl;
pitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
/* ************* */
addFriend(pitem->pid, pitem->netMode, pitem->visState, pitem->lastContact);
setLocalAddress(pitem->pid, pitem->localaddr);
setExtAddress(pitem->pid, pitem->remoteaddr);
}
}
else if (sitem)
{
#ifdef CONN_DEBUG
std::cerr << "p3ConnectMgr::loadList() Stun Config Item:";
std::cerr << std::endl;
sitem->print(std::cerr, 10);
std::cerr << std::endl;
#endif
std::list<std::string>::iterator sit;
for(sit = sitem->stunList.ids.begin();
sit != sitem->stunList.ids.end(); sit++)
{
mStunList.push_back(*sit);
}
}
connMtx.lock(); /* LOCK MUTEX */
connMtx.unlock(); /* UNLOCK MUTEX */
delete (*it);
}
return true;
}
/*******************************************************************/
#if 0
/******************************** Load/Save Config *********************************
* Configuration Loading / Saving.
*/
void p3ConnectMgr::saveConfiguration()
{
return;
}
void p3ConnectMgr::loadConfiguration()
{
/* open the config file....
* load:
* (1) a list of friends
* (2) a list of stun peers
*/
return;
}
std::string configFile = configMgr->getConfigDir();
configFile += "rsnet.state";
/* open file */
while(NULL != (item = config.read()))
{
switch(item->SubType())
{
case OWN_CONFIG:
{
RsPeerConfigItem *peeritem = (RsPeerConfigItem *) item;
setupConfig(peeritem);
break;
}
case PEER_CONFIG:
{
RsPeerConfigItem *peeritem = (RsPeerConfigItem *) item;
addPeer(peeritem);
break;
}
case STUNLIST:
{
RsStunConfigItem *slitem = (RsStunConfigItem *) item;
/* add to existing list */
for(it = slitem.begin();
it = slitem.end(); it++)
{
mStunList.push_back(*it);
}
break;
}
default:
break;
}
delete item;
}
}
void p3ConnectMgr::setupOwnNetConfig(RsPeerConfigItem *item)
{
return;
}
#endif

View file

@ -35,6 +35,15 @@
#include "util/rsthreads.h"
/* RS_VIS_STATE_XXXX
* determines how public this peer wants to be...
*
* STD = advertise to Peers / DHT checking etc
* GRAY = share with friends / but not DHT
* DARK = hidden from all
* BROWN? = hidden from friends / but on DHT
*/
const uint32_t RS_VIS_STATE_NODISC = 0x0001;
const uint32_t RS_VIS_STATE_NODHT = 0x0002;
@ -43,6 +52,8 @@ const uint32_t RS_VIS_STATE_GRAY = RS_VIS_STATE_NODHT;
const uint32_t RS_VIS_STATE_DARK = RS_VIS_STATE_NODISC | RS_VIS_STATE_NODHT;
const uint32_t RS_VIS_STATE_BROWN = RS_VIS_STATE_NODISC;
const uint32_t RS_NET_MODE_UNKNOWN = 0x0000;
const uint32_t RS_NET_MODE_EXT = 0x0001;
const uint32_t RS_NET_MODE_UPNP = 0x0002;
@ -92,28 +103,21 @@ class peerConnectState
peerConnectState(); /* init */
std::string id;
uint32_t netMode; /* EXT / UPNP / UDP / INVALID */
uint32_t visState; /* STD, GRAY, DARK */
struct sockaddr_in localaddr, serveraddr;
time_t lastcontact;
/***** Below here not stored permanently *****/
std::string name;
uint32_t state;
uint32_t actions;
uint32_t netMode; /* EXT / UPNP / UDP / INVALID */
/* Fix this up! */
// public for the moment.
struct sockaddr_in lastaddr, localaddr, serveraddr;
/* determines how public this peer wants to be...
*
* STD = advertise to Peers / DHT checking etc
* GRAY = share with friends / but not DHT
* DARK = hidden from all
* BROWN? = hidden from friends / but on DHT
*/
uint32_t visState; /* STD, GRAY, DARK */
uint32_t source; /* most current source */
peerAddrInfo dht;
peerAddrInfo disc;
@ -125,15 +129,6 @@ class peerConnectState
peerConnectAddress currentConnAddr;
std::list<peerConnectAddress> connAddrs;
time_t lastcontact;
/* stuff here un-used at the moment */
//time_t c_timestamp; // last connect timestamp
//time_t lr_timestamp; // last receive timestamp
//time_t nc_timestamp; // next connect timestamp.
//time_t nc_timeintvl; // next connect time interval.
};
@ -157,12 +152,17 @@ bool getUPnPState();
bool getUPnPEnabled();
bool getDHTEnabled();
void setOwnNetConfig(uint32_t netMode, uint32_t visState);
bool setLocalAddress(std::string id, struct sockaddr_in addr);
bool setExtAddress(std::string id, struct sockaddr_in addr);
bool setNetworkMode(std::string id, uint32_t netMode);
bool setVisState(std::string id, uint32_t visState);
/* add/remove friends */
bool addFriend(std::string);
bool addFriend(std::string id, uint32_t netMode = RS_NET_MODE_UDP,
uint32_t visState = RS_VIS_STATE_STD , time_t lastContact = 0);
bool removeFriend(std::string);
bool addNeighbour(std::string);

View file

@ -47,6 +47,10 @@
int getPQIsearchId();
int fixme(char *str, int n);
#if 0 /* removing old stuff */
struct chan_id
{
int route[10];
@ -200,6 +204,7 @@ private:
std::list<std::string> groups;
};
#endif /* removing old stuff */
class RateInterface
{
@ -377,9 +382,10 @@ virtual ~NetBinInterface() { return; }
};
#define CHAN_SIGN_SIZE 16
#define CERTSIGNLEN 16
#define PQI_PEERID_LENGTH 16
#define CERTSIGNLEN 16 /* actual byte length of signature */
#define PQI_PEERID_LENGTH 32 /* When expanded into a string */
#if 0
class certsign
{
public:
@ -421,5 +427,8 @@ const int PQI_FI_SUBTYPE_ERROR = 8;
const int PQI_FD_FLAG_ENDSTREAM = 1;
#endif /* removing old stuff */
#endif // PQI_BASE_ITEM_HEADER

View file

@ -55,7 +55,7 @@ struct pqiarchive_header
uint32_t type;
uint32_t length;
uint32_t ts;
uint8_t personSig[CERTSIGNLEN];
uint8_t personSig[PQI_PEERID_LENGTH];
};
const int PQIARCHIVE_TYPE_PQITEM = 0x0001;

View file

@ -298,7 +298,7 @@ int pqihandler::GetItems()
// check security... is output allowed.
if(0 < secpolicy_check((it -> second) -> sp,
PQI_ITEM_TYPE_ITEM, PQI_INCOMING))
0, PQI_INCOMING)) // PQI_ITEM_TYPE_ITEM, PQI_INCOMING))
{
// if yes... attempt to read.
while((item = (mod -> pqi) -> GetItem()) != NULL)

View file

@ -36,15 +36,15 @@
class Indicator
{
public:
Indicator(int n = 1)
Indicator(uint16_t n = 1)
:num(n), changeFlags(n) {IndicateChanged();}
void IndicateChanged()
{
for(int i = 0; i < num; i++)
for(uint16_t i = 0; i < num; i++)
changeFlags[i]=true;
}
bool Changed(int idx = 0)
bool Changed(uint16_t idx = 0)
{
/* catch overflow */
if (idx > num - 1)
@ -56,7 +56,7 @@ bool Changed(int idx = 0)
}
private:
int num;
uint16_t num;
std::vector<bool> changeFlags;
};

View file

@ -46,13 +46,6 @@ int errno;
#endif
void sockaddr_clear(struct sockaddr_in *addr)
{
memset(addr, 0, sizeof(struct sockaddr_in));
addr->sin_family = AF_INET;
}
/********************************** WINDOWS/UNIX SPECIFIC PART ******************/
#ifndef WINDOWS_SYS

View file

@ -97,8 +97,6 @@ extern int errno; /* Define extern errno, to duplicate unix behaviour */
#include <string>
#include <list>
void sockaddr_clear(struct sockaddr_in *addr1);
// Same def - different functions...
std::ostream &showSocketError(std::ostream &out);

View file

@ -26,6 +26,7 @@
#include "util/rsnet.h"
#include "pqi/pqissl.h"
#include "pqi/pqinetwork.h"