Improvements to the Bad Peer tracking in the DHT.

* Added Interfaces for sharing Bad Peers.
 * Added bdPeerQueue class for storing
 * added doInfoCallback() to inform libretroshare



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4686 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-11-25 00:48:33 +00:00
parent f01d06c806
commit 8d4a7ed4f0
9 changed files with 116 additions and 2 deletions

View File

@ -30,6 +30,9 @@
#include <iostream>
/*****
* #define DEBUG_FRIENDLIST 1
****/
bdFriendEntry::bdFriendEntry()
{
@ -139,10 +142,12 @@ bool bdFriendList::findPeerEntry(const bdNodeId *id, bdFriendEntry &entry)
it = mPeers.find(*id);
if (it == mPeers.end())
{
#ifdef DEBUG_FRIENDLIST
std::cerr << "bdFriendList::getPeerEntry() Peer(";
bdStdPrintNodeId(std::cerr, id);
std::cerr << ") is unknown!";
std::cerr << std::endl;
#endif
return false;
}
@ -175,3 +180,37 @@ bool bdFriendList::print(std::ostream &out)
bdPeerQueue::bdPeerQueue()
{
return;
}
bool bdPeerQueue::queuePeer(const bdId *id, uint32_t flags)
{
bdFriendEntry entry;
entry.mPeerId = *id;
entry.mFlags = flags;
entry.mLastSeen = time(NULL);
mPeerQueue.push_back(entry);
return true;
}
bool bdPeerQueue::popPeer(bdId *id, uint32_t &flags)
{
if (mPeerQueue.size() > 0)
{
bdFriendEntry entry = mPeerQueue.front();
mPeerQueue.pop_front();
*id = entry.mPeerId;
flags = entry.mFlags;
return true;
}
return false;
}

View File

@ -84,6 +84,20 @@ bool print(std::ostream &out);
std::map<bdNodeId, bdFriendEntry> mPeers;
};
class bdPeerQueue
{
public:
bdPeerQueue();
bool queuePeer(const bdId *id, uint32_t flags);
bool popPeer(bdId *id, uint32_t &flags);
private:
std::list<bdFriendEntry> mPeerQueue;
};
#endif

View File

@ -304,6 +304,8 @@ class bdQuerySummary
#define BD_PROXY_CONNECTION_MID_POINT 2
#define BD_PROXY_CONNECTION_END_POINT 3
#define BITDHT_INFO_CB_TYPE_BADPEER 1
class BitDhtCallback
{
public:
@ -320,6 +322,9 @@ virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t statu
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode) = 0; /* { return 0; } */
// Generic Info callback - initially will be used to provide bad peers.
virtual int dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info) = 0;
};
@ -327,6 +332,9 @@ class BitDhtInterface
{
public:
/* bad peer notification */
virtual void addBadPeer(const struct sockaddr_in &addr, uint32_t source, uint32_t reason, uint32_t age) = 0;
/* Friend Tracking */
virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags) = 0;

View File

@ -158,6 +158,12 @@ bool bdNodeManager::setAttachMode(bool on)
}
/* Friend Tracking */
void bdNodeManager::addBadPeer(const struct sockaddr_in &addr, uint32_t source, uint32_t reason, uint32_t age)
{
std::cerr << "bdNodeManager::addBadPeer() not implemented yet!";
std::cerr << std::endl;
}
void bdNodeManager::updateKnownPeer(const bdId *id, uint32_t /* type */, uint32_t flags)
{
mFriendList.updatePeer(id, flags);
@ -624,6 +630,7 @@ int bdNodeManager::status()
#endif
checkStatus();
checkBadPeerStatus();
/* update the network numbers */
mNetworkSize = mNodeSpace.calcNetworkSize();
@ -927,6 +934,18 @@ bdNodeManager::checkPingStatus()
}
#endif
int bdNodeManager::checkBadPeerStatus()
{
bdId id;
uint32_t flags;
std::string nullstr;
while(mBadPeerQueue.popPeer(&id, flags))
{
doInfoCallback(&id, BITDHT_INFO_CB_TYPE_BADPEER, flags, nullstr);
}
return 1;
}
int bdNodeManager::SearchOutOfDate()
{
@ -1141,6 +1160,23 @@ void bdNodeManager::doValueCallback(const bdNodeId *id, std::string key, uint32_
}
void bdNodeManager::doInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info)
{
std::cerr << "bdNodeManager::doInfoCallback()";
std::cerr << std::endl;
#ifdef DEBUG_MGR
#endif
/* search list */
std::list<BitDhtCallback *>::iterator it;
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
{
(*it)->dhtInfoCallback(id, type, flags, info);
}
return;
}
#define BITDHT_IDENTITY_STRING_V1 "d1:"
#define BITDHT_IDENTITY_SIZE_V1 3
#define BITDHT_PACKET_MIN_SIZE 4

View File

@ -102,7 +102,10 @@ class bdNodeManager: public bdNode, public BitDhtInterface
void iteration();
/***** Functions to Call down to bdNodeManager ****/
/* Friend Tracking */
virtual void addBadPeer(const struct sockaddr_in &addr, uint32_t source, uint32_t reason, uint32_t age);
virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags);
/* Request DHT Peer Lookup */
@ -152,10 +155,12 @@ int isBitDhtPacket(char *data, int size, struct sockaddr_in &from);
void doNodeCallback(const bdId *id, uint32_t peerflags);
void doPeerCallback(const bdId *id, uint32_t status);
void doValueCallback(const bdNodeId *id, std::string key, uint32_t status);
void doInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info);
int status();
int checkStatus();
int checkPingStatus();
int checkBadPeerStatus();
int SearchOutOfDate();
void startQueries();

View File

@ -429,13 +429,15 @@ void bdNode::checkPotentialPeer(bdId *id, bdId *src)
std::cerr << std::endl;
#ifdef TEST_BAD_PEER
std::cerr << "IN TEST MODE... so letting it through.";
std::cerr << "IN TEST MODE... Notifying, but letting it through.";
std::cerr << std::endl;
mBadPeerQueue.queuePeer(id, 0);
#else
mFilterPeers->addBadPeer(id, 0);
// Stores in queue for later callback and desemination around the network.
mBadPeerList->queuePeer(id, 0);
mBadPeerQueue.queuePeer(id, 0);
std::list<struct sockaddr_in> filteredIPs;
mFilterPeers->filteredIPs(filteredIPs);

View File

@ -244,6 +244,7 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
bdHashSpace mHashSpace;
bdFriendList mFriendList;
bdPeerQueue mBadPeerQueue;
private:

View File

@ -89,6 +89,14 @@ UdpBitDht::~UdpBitDht()
/***** Functions to Call down to bdNodeManager ****/
/* Friend Tracking */
void UdpBitDht::addBadPeer(const struct sockaddr_in &addr, uint32_t source, uint32_t reason, uint32_t age)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
mBitDhtManager->addBadPeer(addr, source, reason, age);
}
void UdpBitDht::updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags)
{
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/

View File

@ -59,6 +59,7 @@ virtual ~UdpBitDht();
/***** Functions to Call down to bdNodeManager ****/
/* Friend Tracking */
virtual void addBadPeer(const struct sockaddr_in &addr, uint32_t source, uint32_t reason, uint32_t age);
virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags);
/* Request DHT Peer Lookup */