Improvement to BanPeers Sharing Code.

* Added p3BanList as a service.
 * Added interfaces to communicate addresses.
 * Added debug to p3BanList.
 * Fixed several bugs in the AddEntry/Condense
 * Fixed Mutex deadlocks.

Improvements to Dsdv code too.
 * Added p3Dsdv as a service.
 * Added Function to create a TEST service for routing.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4687 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-11-25 00:58:01 +00:00
parent 8d4a7ed4f0
commit 85ea54395b
15 changed files with 304 additions and 18 deletions

View file

@ -37,6 +37,8 @@
* #define DEBUG_BANLIST 1
****/
#define DEBUG_BANLIST 1
/* DEFINE INTERFACE POINTER! */
//RsBanList *rsBanList = NULL;
@ -89,6 +91,12 @@ bool p3BanList::processIncoming()
bool updated = false;
while(NULL != (item = recvItem()))
{
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::processingIncoming() Received Item:";
std::cerr << std::endl;
item->print(std::cerr);
std::cerr << std::endl;
#endif
switch(item->PacketSubType())
{
default:
@ -134,6 +142,24 @@ bool p3BanList::recvBanItem(RsBanListItem *item)
return updated;
}
/* overloaded from pqiNetAssistSharePeer */
void p3BanList::updatePeer(std::string id, struct sockaddr_in addr, int type, int reason, int age)
{
std::string ownId = mLinkMgr->getOwnId();
int int_reason = 0;
addBanEntry(ownId, addr, RSBANLIST_SOURCE_SELF, int_reason, age);
/* process */
{
RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/
mBanSet.clear();
condenseBanSources_locked();
}
}
bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in &addr, uint32_t level, uint32_t reason, uint32_t age)
{
RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/
@ -141,13 +167,19 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in
time_t now = time(NULL);
bool updated = false;
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::addBanEntry() Addr: " << rs_inet_ntoa(addr.sin_addr) << " Level: " << level;
std::cerr << " Reason: " << reason << " Age: " << age;
std::cerr << std::endl;
#endif
std::map<std::string, BanList>::iterator it;
it = mBanSources.find(peerId);
if (it == mBanSources.end())
{
BanList bl;
bl.mPeerId = peerId;
bl.mLastUpdate = 0;
bl.mLastUpdate = now;
mBanSources[peerId] = bl;
it = mBanSources.find(peerId);
@ -164,6 +196,9 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in
blp.reason = reason;
blp.level = level;
blp.mTs = now - age;
it->second.mBanPeers[addr.sin_addr.s_addr] = blp;
it->second.mLastUpdate = now;
updated = true;
}
else
@ -178,6 +213,8 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in
mit->second.reason = reason;
mit->second.level = level;
mit->second.mTs = now - age;
it->second.mLastUpdate = now;
updated = true;
}
}
@ -190,13 +227,29 @@ int p3BanList::condenseBanSources_locked()
time_t now = time(NULL);
std::string ownId = mLinkMgr->getOwnId();
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << std::endl;
#endif
std::map<std::string, BanList>::const_iterator it;
for(it = mBanSources.begin(); it != mBanSources.end(); it++)
{
if (now - it->second.mLastUpdate > RSBANLIST_ENTRY_MAX_AGE)
{
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << " Ignoring Out-Of-Date peer: " << it->first;
std::cerr << std::endl;
#endif
continue;
}
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << " Condensing Info from peer: " << it->first;
std::cerr << std::endl;
#endif
std::map<uint32_t, BanListPeer>::const_iterator lit;
for(lit = it->second.mBanPeers.begin();
@ -205,6 +258,12 @@ int p3BanList::condenseBanSources_locked()
/* check timestamp */
if (now - lit->second.mTs > RSBANLIST_ENTRY_MAX_AGE)
{
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << " Ignoring Out-Of-Date Entry for: ";
std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr);
std::cerr << std::endl;
#endif
continue;
}
@ -224,9 +283,21 @@ int p3BanList::condenseBanSources_locked()
bp.level = lvl;
bp.addr.sin_port = 0;
mBanSet[lit->second.addr.sin_addr.s_addr] = bp;
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << " Added New Entry for: ";
std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr);
std::cerr << std::endl;
#endif
}
else
{
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked()";
std::cerr << " Merging Info for: ";
std::cerr << rs_inet_ntoa(lit->second.addr.sin_addr);
std::cerr << std::endl;
#endif
/* update if necessary */
if (lvl == sit->second.level)
{
@ -239,6 +310,15 @@ int p3BanList::condenseBanSources_locked()
}
}
}
#ifdef DEBUG_BANLIST
std::cerr << "p3BanList::condenseBanSources_locked() Printing New Set:";
std::cerr << std::endl;
#endif
printBanSet_locked(std::cerr);
}
@ -255,10 +335,15 @@ int p3BanList::sendPackets()
if (now - pt > RSBANLIST_SEND_PERIOD)
{
sendBanLists();
printBanSources(std::cerr);
printBanSet(std::cerr);
RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/
std::cerr << "p3BanList::sendPackets() Regular Broadcast";
std::cerr << std::endl;
printBanSources_locked(std::cerr);
printBanSet_locked(std::cerr);
mSentListTime = now;
}
return true ;
@ -325,11 +410,9 @@ int p3BanList::sendBanSet(std::string peerid)
}
int p3BanList::printBanSet(std::ostream &out)
int p3BanList::printBanSet_locked(std::ostream &out)
{
RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/
out << "p3BanList::printBanSet";
out << "p3BanList::printBanSet_locked()";
out << std::endl;
time_t now = time(NULL);
@ -352,10 +435,8 @@ int p3BanList::printBanSet(std::ostream &out)
int p3BanList::printBanSources(std::ostream &out)
int p3BanList::printBanSources_locked(std::ostream &out)
{
RsStackMutex stack(mBanMtx); /****** LOCKED MUTEX *******/
time_t now = time(NULL);
std::map<std::string, BanList>::const_iterator it;

View file

@ -65,13 +65,18 @@ class BanList
* Exchange list of Banned IP addresses with peers.
*/
class p3BanList: /* public RsBanList, */ public p3Service /* , public p3Config, public pqiMonitor */
class p3BanList: /* public RsBanList, */ public p3Service, public pqiNetAssistPeerShare /* , public p3Config, public pqiMonitor */
{
public:
p3BanList(p3LinkMgr *lm, p3NetMgr *nm);
/***** overloaded from RsBanList *****/
/***** overloaded from pqiNetAssistPeerShare *****/
virtual void updatePeer(std::string id, struct sockaddr_in addr, int type, int reason, int age);
/***** overloaded from p3Service *****/
/*!
* This retrieves all chat msg items and also (important!)
@ -93,8 +98,6 @@ class p3BanList: /* public RsBanList, */ public p3Service /* , public p3Config,
void sendBanLists();
int sendBanSet(std::string peerid);
int printBanSources(std::ostream &out);
int printBanSet(std::ostream &out);
/*!
* Interface stuff.
@ -115,6 +118,8 @@ class p3BanList: /* public RsBanList, */ public p3Service /* , public p3Config,
RsMutex mBanMtx;
int condenseBanSources_locked();
int printBanSources_locked(std::ostream &out);
int printBanSet_locked(std::ostream &out);
time_t mSentListTime;
std::map<std::string, BanList> mBanSources;

View file

@ -29,11 +29,14 @@
//#include "serialiser/rsdsdvitems.h"
#include "services/p3dsdv.h"
#include "pqi/p3linkmgr.h"
#include "util/rsrandom.h"
#include <openssl/sha.h>
/****
* #define DEBUG_DSDV 1
****/
#define DEBUG_DSDV 1
/* DEFINE INTERFACE POINTER! */
RsDsdv *rsDsdv = NULL;
@ -98,8 +101,16 @@ int p3Dsdv::sendTables()
if (now - tt > DSDV_BROADCAST_PERIOD)
{
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::sendTables() Broadcast Time";
std::cerr << std::endl;
#endif
generateRoutingTables(false);
printDsdvTable(std::cerr);
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
mSentTablesTime = now;
mSentIncrementTime = now;
@ -359,11 +370,68 @@ void p3Dsdv::statusChange(const std::list<pqipeer> &plist)
}
}
int p3Dsdv::addTestService()
{
RsDsdvId testId;
int rndhash1[SHA_DIGEST_LENGTH / 4];
int rndhash2[SHA_DIGEST_LENGTH / 4];
std::ostringstream rh, sh;
std::string realHash;
std::string seedHash;
int i;
for(i = 0; i < SHA_DIGEST_LENGTH / 4; i++)
{
rndhash1[i] = RSRandom::random_u32();
rndhash2[i] = RSRandom::random_u32();
}
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
rh << std::setw(2) << std::setfill('0') << std::hex << (uint32_t) ((uint8_t *) rndhash1)[i];
sh << std::setw(2) << std::setfill('0') << std::hex << (uint32_t) ((uint8_t *) rndhash2)[i];
}
realHash = rh.str();
seedHash = sh.str();
uint8_t sha_hash[SHA_DIGEST_LENGTH];
memset(sha_hash,0,SHA_DIGEST_LENGTH*sizeof(uint8_t)) ;
SHA_CTX *sha_ctx = new SHA_CTX;
SHA1_Init(sha_ctx);
SHA1_Update(sha_ctx, realHash.c_str(), realHash.length());
SHA1_Update(sha_ctx, seedHash.c_str(), seedHash.length());
SHA1_Final(sha_hash, sha_ctx);
delete sha_ctx;
std::ostringstream keystr;
for(int i = 0; i < SHA_DIGEST_LENGTH; i++)
{
keystr << std::setw(2) << std::setfill('0') << std::hex << (uint32_t) (sha_hash)[i];
}
testId.mIdType = RSDSDV_IDTYPE_TEST;
testId.mAnonChunk = seedHash;
testId.mHash = keystr.str();
addDsdvId(&testId, realHash);
return 1;
}
int p3Dsdv::addDsdvId(RsDsdvId *id, std::string realHash)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::addDsdvId() ID: " << *id << " RealHash: " << realHash;
std::cerr << std::endl;
#endif
time_t now = time(NULL);
/* check for duplicate */
@ -407,6 +475,11 @@ int p3Dsdv::dropDsdvId(RsDsdvId *id)
{
RsStackMutex stack(mDsdvMtx); /****** LOCKED MUTEX *******/
#ifdef DEBUG_DSDV
std::cerr << "p3Dsdv::dropDsdvId() ID: " << *id;
std::cerr << std::endl;
#endif
/* This should send out an infinity packet... and flag for deletion */
std::map<std::string, RsDsdvTableEntry>::iterator it;
@ -436,9 +509,12 @@ int p3Dsdv::printDsdvTable(std::ostream &out)
{
RsDsdvTableEntry &v = it->second;
out << v.mDest;
out << " BR: " << v.mBestRoute;
out << " SR: " << v.mBestRoute;
out << " Flags: " << v.mFlags;
out << std::endl;
out << "\tBR: " << v.mBestRoute;
out << std::endl;
out << "\tSR: " << v.mBestRoute;
out << std::endl;
out << "\tFlags: " << v.mFlags;
out << " Own: " << v.mOwnSource;
if (v.mMatched)
{

View file

@ -60,6 +60,8 @@ int addDsdvId(RsDsdvId *id, std::string realHash);
int dropDsdvId(RsDsdvId *id);
int printDsdvTable(std::ostream &out);
int addTestService();
private:
int sendTables();