diff --git a/libbitdht/src/bitdht/bdfriendlist.cc b/libbitdht/src/bitdht/bdfriendlist.cc index c91198e32..630aba2a0 100644 --- a/libbitdht/src/bitdht/bdfriendlist.cc +++ b/libbitdht/src/bitdht/bdfriendlist.cc @@ -30,6 +30,9 @@ #include +/***** + * #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; +} + + + diff --git a/libbitdht/src/bitdht/bdfriendlist.h b/libbitdht/src/bitdht/bdfriendlist.h index 8a744ee45..825f5ad65 100644 --- a/libbitdht/src/bitdht/bdfriendlist.h +++ b/libbitdht/src/bitdht/bdfriendlist.h @@ -84,6 +84,20 @@ bool print(std::ostream &out); std::map mPeers; }; +class bdPeerQueue +{ + + public: + bdPeerQueue(); + +bool queuePeer(const bdId *id, uint32_t flags); +bool popPeer(bdId *id, uint32_t &flags); + + private: + + std::list mPeerQueue; +}; + #endif diff --git a/libbitdht/src/bitdht/bdiface.h b/libbitdht/src/bitdht/bdiface.h index 87618c10d..f981420d2 100644 --- a/libbitdht/src/bitdht/bdiface.h +++ b/libbitdht/src/bitdht/bdiface.h @@ -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; diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 5e12a9d67..9a324f4c2 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -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::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 diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index 9deee454e..5343e32ce 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -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(); diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index 150d8057d..31635aba0 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -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 filteredIPs; mFilterPeers->filteredIPs(filteredIPs); diff --git a/libbitdht/src/bitdht/bdnode.h b/libbitdht/src/bitdht/bdnode.h index 09fafb119..e95fddea7 100644 --- a/libbitdht/src/bitdht/bdnode.h +++ b/libbitdht/src/bitdht/bdnode.h @@ -244,6 +244,7 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr); bdHashSpace mHashSpace; bdFriendList mFriendList; + bdPeerQueue mBadPeerQueue; private: diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index a94ddc869..d9477a699 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/src/udp/udpbitdht.cc @@ -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 *************/ diff --git a/libbitdht/src/udp/udpbitdht.h b/libbitdht/src/udp/udpbitdht.h index c4e0289cc..3717ab718 100644 --- a/libbitdht/src/udp/udpbitdht.h +++ b/libbitdht/src/udp/udpbitdht.h @@ -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 */