From 4cd7523947fa8464e06d7efd243bcc46ab68667d Mon Sep 17 00:00:00 2001 From: drbob Date: Tue, 22 Nov 2011 13:16:03 +0000 Subject: [PATCH] Incremental Improvements to DHT: Added Knowledge of Friends, Friends of Friends and Relays. - Added bdfriendlist class to store information. - New interface function updateKnownPeer(). - includes IP addresses, if known. (for filtering) Bad Peer Filtering. - Check IDs against known peer IP addresses. - Added checks in checkPotentialPeer(). - Added checks in addPeer(). - Running in TestMode at the moment (Bad Peers are only flagged). Other Fixes: - Removed some warning about int/uint comparisons. - added bdSpace::flagpeer() fn to support above. git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4680 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libbitdht/src/bitdht/bdaccount.cc | 2 +- libbitdht/src/bitdht/bdconnection.cc | 2 +- libbitdht/src/bitdht/bdfriendlist.cc | 177 +++++++++++++++++++++++++++ libbitdht/src/bitdht/bdfriendlist.h | 89 ++++++++++++++ libbitdht/src/bitdht/bdiface.h | 23 +++- libbitdht/src/bitdht/bdmanager.cc | 7 +- libbitdht/src/bitdht/bdmanager.h | 3 + libbitdht/src/bitdht/bdnode.cc | 115 ++++++++++++++++- libbitdht/src/bitdht/bdnode.h | 4 + libbitdht/src/bitdht/bdpeer.cc | 52 +++++++- libbitdht/src/bitdht/bdpeer.h | 3 + libbitdht/src/bitdht/bdstddht.cc | 2 +- libbitdht/src/libbitdht.pro | 2 + libbitdht/src/udp/udpbitdht.cc | 8 ++ libbitdht/src/udp/udpbitdht.h | 4 + libbitdht/src/util/bdbloom.cc | 12 +- 16 files changed, 487 insertions(+), 18 deletions(-) create mode 100644 libbitdht/src/bitdht/bdfriendlist.cc create mode 100644 libbitdht/src/bitdht/bdfriendlist.h diff --git a/libbitdht/src/bitdht/bdaccount.cc b/libbitdht/src/bitdht/bdaccount.cc index 232f586ea..bf955b930 100644 --- a/libbitdht/src/bitdht/bdaccount.cc +++ b/libbitdht/src/bitdht/bdaccount.cc @@ -64,7 +64,7 @@ bdAccount::bdAccount() void bdAccount::incCounter(uint32_t idx, bool out) { - if (idx > mNoStats-1) + if ((signed) idx > mNoStats-1) { std::cerr << "bdAccount::incCounter() Invalid Index"; std::cerr << std::endl; diff --git a/libbitdht/src/bitdht/bdconnection.cc b/libbitdht/src/bitdht/bdconnection.cc index f31008dd4..28e24ca64 100644 --- a/libbitdht/src/bitdht/bdconnection.cc +++ b/libbitdht/src/bitdht/bdconnection.cc @@ -670,7 +670,7 @@ void bdConnectManager::iterateConnectionRequests() it->second.mState = BITDHT_CONNREQUEST_DONE; it->second.mStateTS = now; } - else if (it->second.mRecycled > it->second.mGoodProxies.size() * MAX_NUM_RETRIES) + else if ((unsigned) it->second.mRecycled > it->second.mGoodProxies.size() * MAX_NUM_RETRIES) { #ifdef DEBUG_NODE_CONNECTION std::cerr << "bdConnectManager::iterateConnectionAttempt() to many retries => DONE"; diff --git a/libbitdht/src/bitdht/bdfriendlist.cc b/libbitdht/src/bitdht/bdfriendlist.cc new file mode 100644 index 000000000..c91198e32 --- /dev/null +++ b/libbitdht/src/bitdht/bdfriendlist.cc @@ -0,0 +1,177 @@ + +/* + * bitdht/bdfriendlist.cc + * + * BitDHT: An Flexible DHT library. + * + * Copyright 2011 by Robert Fernie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 3 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "bitdht@lunamutt.com". + * + */ + +#include "bitdht/bdfriendlist.h" +#include "bitdht/bdstddht.h" +#include "bitdht/bdpeer.h" + +#include + + +bdFriendEntry::bdFriendEntry() +{ + mFlags = 0; + mLastSeen = 0; +} + +bool bdFriendEntry::addrKnown(struct sockaddr_in *addr) +{ + if (mFlags & BD_FRIEND_ENTRY_ADDR_OK) + { + if (mFlags & BD_FRIEND_ENTRY_ONLINE) + { + *addr = mPeerId.addr; + return true; + } + + + if (time(NULL) - mLastSeen < BD_FRIEND_ENTRY_TIMEOUT) + { + *addr = mPeerId.addr; + return true; + } + } + return false; +} + +uint32_t bdFriendEntry::getPeerFlags() +{ + return mFlags & BD_FRIEND_ENTRY_MASK_KNOWN; +} + + +bdFriendList::bdFriendList(const bdNodeId *ownId) +{ + bdId tmpId; + tmpId.id = *ownId; + updatePeer(&tmpId, BD_FRIEND_ENTRY_SELF); +} + +/****** + * Simple logic: timestamp is set with address. + * if ONLINE, then address will be dropped as soon as OFFLINE. + * if ADDR_OK, then address will be dropped after XX seconds. + * + * ONLINE - will be used with friends. + * ADDR_OK - will potentially be used for friends of friends (but not for now). + *****/ + + /* catch-all interface function */ +bool bdFriendList::updatePeer(const bdId *id, uint32_t flags) +{ + std::cerr << "bdFriendList::updatePeer() Peer("; + bdStdPrintId(std::cerr, id); + std::cerr << ") Flags: " << flags; + std::cerr << std::endl; + + std::map::iterator it; + it = mPeers.find(id->id); + if (it == mPeers.end()) + { + bdFriendEntry entry; + entry.mPeerId.id = id->id; + entry.mFlags = 0; + entry.mLastSeen = 0; + + mPeers[id->id] = entry; + it = mPeers.find(id->id); + } + + /* update all */ + it->second.mFlags = flags; + if (it->second.mFlags & (BD_FRIEND_ENTRY_ADDR_OK | BD_FRIEND_ENTRY_ONLINE)) + { + it->second.mFlags |= BD_FRIEND_ENTRY_ADDR_OK; + + it->second.mPeerId.addr = id->addr; + it->second.mLastSeen = time(NULL); + } + return true; +} + +bool bdFriendList::removePeer(const bdNodeId *id) +{ + /* see if it exists... */ + std::map::iterator it; + it = mPeers.find(*id); + if (it == mPeers.end()) + { + std::cerr << "bdFriendList::removeFriend() Peer("; + bdStdPrintNodeId(std::cerr, id); + std::cerr << ") is unknown!"; + std::cerr << std::endl; + + return false; + } + + mPeers.erase(*id); + + return true; +} + +bool bdFriendList::findPeerEntry(const bdNodeId *id, bdFriendEntry &entry) +{ + /* see if it exists... */ + std::map::iterator it; + it = mPeers.find(*id); + if (it == mPeers.end()) + { + std::cerr << "bdFriendList::getPeerEntry() Peer("; + bdStdPrintNodeId(std::cerr, id); + std::cerr << ") is unknown!"; + std::cerr << std::endl; + + return false; + } + + entry = it->second; + + + return true; +} + + +bool bdFriendList::print(std::ostream &out) +{ + time_t now = time(NULL); + + out << "bdFriendList::print()"; + out << std::endl; + + std::map::iterator it; + for(it = mPeers.begin(); it != mPeers.end(); it++) + { + bdStdPrintId(out, &(it->second.mPeerId)); + out << " Flags: " << it->second.mFlags; + out << " Seen: " << now - it->second.mLastSeen; + out << std::endl; + } + + return true; +} + + + diff --git a/libbitdht/src/bitdht/bdfriendlist.h b/libbitdht/src/bitdht/bdfriendlist.h new file mode 100644 index 000000000..8a744ee45 --- /dev/null +++ b/libbitdht/src/bitdht/bdfriendlist.h @@ -0,0 +1,89 @@ +#ifndef BITDHT_FRIEND_LIST_H +#define BITDHT_FRIEND_LIST_H + +/* + * bitdht/bdfriendlist.h + * + * BitDHT: An Flexible DHT library. + * + * Copyright 2011 by Robert Fernie + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License Version 3 as published by the Free Software Foundation. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + * + * Please report all bugs and problems to "bitdht@lunamutt.com". + * + */ + +/* + * This class maintains a list of current friends and friends-of-friends. + * It should also be updated when a peer's address has been identified. + * + * It is used for selecting preferential peers for DHT Connections. + * and for detecting bad peers that are duplicating real RS peers. + * + */ + + +#include "bitdht/bdiface.h" +#include + +#define BD_FRIEND_ENTRY_TIMEOUT 10 + + +//#define BD_FRIEND_ENTRY_ONLINE 0x0001 +//#define BD_FRIEND_ENTRY_ADDR_OK 0x0002 + +//#define BD_FRIEND_ENTRY_WHITELIST BITDHT_PEER_STATUS_DHT_WHITELIST +//#define BD_FRIEND_ENTRY_FOF BITDHT_PEER_STATUS_DHT_FOF +//#define BD_FRIEND_ENTRY_FRIEND BITDHT_PEER_STATUS_DHT_FRIEND +//#define BD_FRIEND_ENTRY_RELAY_SERVER BITDHT_PEER_STATUS_DHT_RELAY_SERVER + +//#define BD_FRIEND_ENTRY_SELF BITDHT_PEER_STATUS_DHT_SELF + +//#define BD_FRIEND_ENTRY_MASK_KNOWN BITDHT_PEER_STATUS_MASK_KNOWN + +class bdFriendEntry +{ + public: + bdFriendEntry(); + +bool addrKnown(struct sockaddr_in *addr); +uint32_t getPeerFlags(); + + bdId mPeerId; + uint32_t mFlags; + time_t mLastSeen; +}; + +class bdFriendList +{ + + public: + bdFriendList(const bdNodeId *ownid); + +bool updatePeer(const bdId *id, uint32_t flags); +bool removePeer(const bdNodeId *id); + +bool findPeerEntry(const bdNodeId *id, bdFriendEntry &entry); + +bool print(std::ostream &out); + private: + + std::map mPeers; +}; + + +#endif + diff --git a/libbitdht/src/bitdht/bdiface.h b/libbitdht/src/bitdht/bdiface.h index 103d254fe..87618c10d 100644 --- a/libbitdht/src/bitdht/bdiface.h +++ b/libbitdht/src/bitdht/bdiface.h @@ -109,7 +109,7 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0; /* NODE OPTIONS */ #define BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT 0x00000001 - +#define BITDHT_OPTIONS_ENABLE_RELAYS 0x00000002 /* peer flags @@ -143,13 +143,15 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0; #define BITDHT_PEER_STATUS_DHT_WHITELIST 0x00010000 #define BITDHT_PEER_STATUS_DHT_FOF 0x00020000 #define BITDHT_PEER_STATUS_DHT_FRIEND 0x00040000 +#define BITDHT_PEER_STATUS_DHT_RELAY_SERVER 0x00080000 // (Flag must be enabled) +#define BITDHT_PEER_STATUS_DHT_SELF 0x00100000 // EXTRA FLAGS are our internal thoughts about the peer. #define BITDHT_PEER_EXFLAG_MASK_BASIC 0x000000ff #define BITDHT_PEER_EXFLAG_UNSTABLE 0x00000001 // Port changes. #define BITDHT_PEER_EXFLAG_ATTACHED 0x00000002 // We will ping in heavily. (if unstable) - +#define BITDHT_PEER_EXFLAG_BADPEER 0x00000004 // For testing, we flag rather than discard. @@ -198,6 +200,20 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0; #define BITDHT_CONNECT_ERROR_USER 0x0000000d +/*************/ +// FRIEND_ENTRY_FLAGS... used by updateKnownPeers(). + +#define BD_FRIEND_ENTRY_ONLINE 0x0001 +#define BD_FRIEND_ENTRY_ADDR_OK 0x0002 + +#define BD_FRIEND_ENTRY_WHITELIST BITDHT_PEER_STATUS_DHT_WHITELIST +#define BD_FRIEND_ENTRY_FOF BITDHT_PEER_STATUS_DHT_FOF +#define BD_FRIEND_ENTRY_FRIEND BITDHT_PEER_STATUS_DHT_FRIEND +#define BD_FRIEND_ENTRY_RELAY_SERVER BITDHT_PEER_STATUS_DHT_RELAY_SERVER + +#define BD_FRIEND_ENTRY_SELF BITDHT_PEER_STATUS_DHT_SELF + +#define BD_FRIEND_ENTRY_MASK_KNOWN BITDHT_PEER_STATUS_MASK_KNOWN @@ -311,6 +327,9 @@ class BitDhtInterface { public: + /* Friend Tracking */ +virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags) = 0; + /***** Request Lookup (DHT Peer & Keyword) *****/ virtual void addFindNode(bdNodeId *id, uint32_t mode) = 0; virtual void removeFindNode(bdNodeId *id) = 0; diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index b1116afe8..5e12a9d67 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -157,8 +157,11 @@ bool bdNodeManager::setAttachMode(bool on) return on; } - - + /* Friend Tracking */ +void bdNodeManager::updateKnownPeer(const bdId *id, uint32_t /* type */, uint32_t flags) +{ + mFriendList.updatePeer(id, flags); +} void bdNodeManager::addFindNode(bdNodeId *id, uint32_t qflags) { diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index cfb308693..9deee454e 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -102,6 +102,9 @@ class bdNodeManager: public bdNode, public BitDhtInterface void iteration(); /***** Functions to Call down to bdNodeManager ****/ + /* Friend Tracking */ +virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags); + /* Request DHT Peer Lookup */ /* Request Keyword Lookup */ virtual void addFindNode(bdNodeId *id, uint32_t mode); diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index 672df2870..150d8057d 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -66,7 +66,7 @@ bdNode::bdNode(bdNodeId *ownId, std::string dhtVersion, std::string bootfile, bdDhtFunctions *fns) - :mNodeSpace(ownId, fns), mQueryMgr(NULL), mConnMgr(NULL), mFilterPeers(NULL), mOwnId(*ownId), mDhtVersion(dhtVersion), mStore(bootfile, fns), mFns(fns) + :mNodeSpace(ownId, fns), mQueryMgr(NULL), mConnMgr(NULL), mFilterPeers(NULL), mOwnId(*ownId), mDhtVersion(dhtVersion), mStore(bootfile, fns), mFns(fns), mFriendList(ownId) { init(); /* (uses this pointers) stuff it - do it here! */ @@ -88,7 +88,7 @@ void bdNode::init() #define ATTACH_NUMBER 5 void bdNode::setNodeOptions(uint32_t optFlags) { - mNodeOptionFlags = optFlags; + mNodeOptionFlags = optFlags; if (optFlags & BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT) { mNodeSpace.setAttachedFlag(BITDHT_PEER_STATUS_DHT_ENGINE | BITDHT_PEER_STATUS_DHT_ENGINE_VERSION, ATTACH_NUMBER); @@ -392,9 +392,61 @@ void bdNode::send_connect_msg(bdId *id, int msgtype, bdId *srcAddr, bdId *destAd +#define TEST_BAD_PEER 1 void bdNode::checkPotentialPeer(bdId *id, bdId *src) { + /* Check BadPeer Filters for Potential Peers too */ + + /* first check the filters */ + if (!mFilterPeers->addrOkay(&(id->addr))) + { + std::cerr << "bdNode::checkPotentialPeer("; + mFns->bdPrintId(std::cerr, id); + std::cerr << ") BAD ADDRESS!!!! SHOULD DISCARD POTENTIAL PEER"; + std::cerr << std::endl; + +#ifdef TEST_BAD_PEER + std::cerr << "IN TEST MODE... so letting it through."; + std::cerr << std::endl; +#else + return; +#endif + } + + /* is it masquarading? */ + bdFriendEntry entry; + if (mFriendList.findPeerEntry(&(id->id), entry)) + { + struct sockaddr_in knownAddr; + if (entry.addrKnown(&knownAddr)) + { + if (knownAddr.sin_addr.s_addr != id->addr.sin_addr.s_addr) + { + std::cerr << "bdNode::checkPotentialPeer("; + mFns->bdPrintId(std::cerr, id); + std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << std::endl; + +#ifdef TEST_BAD_PEER + std::cerr << "IN TEST MODE... so letting it through."; + std::cerr << std::endl; +#else + + mFilterPeers->addBadPeer(id, 0); + // Stores in queue for later callback and desemination around the network. + mBadPeerList->queuePeer(id, 0); + + std::list filteredIPs; + mFilterPeers->filteredIPs(filteredIPs); + mStore.filterIpList(filteredIPs); + + return; +#endif + } + } + } + bool isWorthyPeer = mQueryMgr->checkPotentialPeer(id, src); if (isWorthyPeer) @@ -445,9 +497,66 @@ void bdNode::addPeer(const bdId *id, uint32_t peerflags) return; } +// NB: TODO CLEANUP THIS CODE - ONCE LOGIC IS TESTED! + + /* next we check if it is a friend, whitelist etc, and adjust flags */ + bdFriendEntry entry; + +#ifdef TEST_BAD_PEER + bool peerBad = false; +#endif + if (mFriendList.findPeerEntry(&(id->id), entry)) + { + /* found! */ + peerflags |= entry.getPeerFlags(); // translates internal into general ones. + + struct sockaddr_in knownAddr; + if (entry.addrKnown(&knownAddr)) + { + if (knownAddr.sin_addr.s_addr != id->addr.sin_addr.s_addr) + { + std::cerr << "bdNode::addPeer("; + mFns->bdPrintId(std::cerr, id); + std::cerr << ", " << std::hex << peerflags << std::dec; + std::cerr << ") MASQARADING AS KNOWN PEER - FLAGGING AS BAD"; + std::cerr << std::endl; + +#ifdef TEST_BAD_PEER + peerBad = true; +#else + mFilterPeers->addBadPeer(id, peerflags); + // Stores in queue for later callback and desemination around the network. + mBadPeerList->queuePeer(id, peerflags); + + std::list filteredIPs; + mFilterPeers->filteredIPs(filteredIPs); + mStore.filterIpList(filteredIPs); + + // DO WE EXPLICITLY NEED TO DO THIS, OR WILL THEY JUST BE DROPPED? + //mNodeSpace.remove_badpeer(id); + //mQueryMgr->remove_badpeer(id); + + return; +#endif + } + } + } + mQueryMgr->addPeer(id, peerflags); mNodeSpace.add_peer(id, peerflags); +#ifdef TEST_BAD_PEER + // NOTE: We will push bad peers to Query in the testing case. + // This allows us to test the multiple solutions... as well. + // In normal behaviour - they will just get stripped and never added. + if (peerBad) + { + mNodeSpace.flagpeer(id, 0, BITDHT_PEER_EXFLAG_BADPEER); + //mQueryMgr->flag_badpeer(id); + } +#endif + + bdPeer peer; peer.mPeerId = *id; peer.mPeerFlags = peerflags; @@ -1529,7 +1638,7 @@ void bdNode::msgin_pong(bdId *id, bdToken *transId, bdToken *versionId) { sameDhtVersion = false; std::cerr << "bdNode::msgin_pong() STRANGE Peer Version: "; - for(int i = 0; i < versionId->len; i++) + for(uint32_t i = 0; i < versionId->len; i++) { std::cerr << versionId->data[i]; } diff --git a/libbitdht/src/bitdht/bdnode.h b/libbitdht/src/bitdht/bdnode.h index 9bfbda74e..09fafb119 100644 --- a/libbitdht/src/bitdht/bdnode.h +++ b/libbitdht/src/bitdht/bdnode.h @@ -37,6 +37,8 @@ #include "bitdht/bdconnection.h" #include "bitdht/bdaccount.h" +#include "bitdht/bdfriendlist.h" + class bdFilter; @@ -241,6 +243,8 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr); bdDhtFunctions *mFns; bdHashSpace mHashSpace; + bdFriendList mFriendList; + private: uint32_t mNodeOptionFlags; diff --git a/libbitdht/src/bitdht/bdpeer.cc b/libbitdht/src/bitdht/bdpeer.cc index 86057373d..0740d2e77 100644 --- a/libbitdht/src/bitdht/bdpeer.cc +++ b/libbitdht/src/bitdht/bdpeer.cc @@ -444,7 +444,7 @@ int bdSpace::scanOutOfDatePeers(std::list &peerIds) * */ bool doAttached = (mAttachedCount > 0); - int attachedCount = 0; + uint32_t attachedCount = 0; std::map closest; std::map::iterator mit; @@ -538,13 +538,20 @@ int bdSpace::scanOutOfDatePeers(std::list &peerIds) } +/* Attached should be changed to preferentially choose the ones we want + * If we have RELAYS enabled - then we want them to attach to the RELAYS + * Not sure about this yet! + * + * Have to think about it a bit more. (and redesign code to handle multiple passes) + */ + int bdSpace::updateAttachedPeers() { /* * */ bool doAttached = (mAttachedCount > 0); - int attachedCount = 0; + uint32_t attachedCount = 0; // Must scan through - otherwise we can never remove Attached state. // It is only once every 10 minutes or so! @@ -762,7 +769,48 @@ int bdSpace::add_peer(const bdId *id, uint32_t peerflags) return add; } + /* flag peer */ +bool bdSpace::flagpeer(const bdId *id, uint32_t flags, uint32_t ex_flags) +{ +#ifdef DEBUG_BD_SPACE + fprintf(stderr, "bdSpace::flagpeer()\n"); +#endif + /* calculate metric */ + bdMetric met; + mFns->bdDistance(&(mOwnId), &(id->id), &met); + int bucket = mFns->bdBucketDistance(&met); + + + /* select correct bucket */ + bdBucket &buck = buckets[bucket]; + + /* loop through ids, to find it */ + std::list::iterator it; + for(it = buck.entries.begin(); it != buck.entries.end(); it++) + { + /* similar id check */ + if (mFns->bdSimilarId(id, &(it->mPeerId))) + { +#ifdef DEBUG_BD_SPACE + fprintf(stderr, "peer:"); + mFns->bdPrintId(std::cerr, id); + fprintf(stderr, " bucket: %d", bucket); + fprintf(stderr, "\n"); + fprintf(stderr, "Original Flags: %x Extra: %x\n", + it->mPeerFlags, it->mExtraFlags); +#endif + it->mPeerFlags |= flags; + it->mExtraFlags |= ex_flags; + +#ifdef DEBUG_BD_SPACE + fprintf(stderr, "Updated Flags: %x Extra: %x\n", + it->mPeerFlags, it->mExtraFlags); +#endif + } + } + return true; +} /* print tables. */ diff --git a/libbitdht/src/bitdht/bdpeer.h b/libbitdht/src/bitdht/bdpeer.h index 65b931f3c..fdba9e1aa 100644 --- a/libbitdht/src/bitdht/bdpeer.h +++ b/libbitdht/src/bitdht/bdpeer.h @@ -193,6 +193,9 @@ bool findRandomPeerWithFlag(bdId &id, uint32_t withFlag); /* to add later */ int updateOwnId(bdNodeId *newOwnId); + /* flag peer */ +bool flagpeer(const bdId *id, uint32_t flags, uint32_t ex_flags); + private: std::vector buckets; diff --git a/libbitdht/src/bitdht/bdstddht.cc b/libbitdht/src/bitdht/bdstddht.cc index a8872bcde..974e06fbb 100644 --- a/libbitdht/src/bitdht/bdstddht.cc +++ b/libbitdht/src/bitdht/bdstddht.cc @@ -141,7 +141,7 @@ void bdStdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *m int bdStdLoadNodeId(bdNodeId *id, std::string input) { uint8_t *a_data = (uint8_t *) id->data; - int reqlen = BITDHT_KEY_LEN * 2; + uint32_t reqlen = BITDHT_KEY_LEN * 2; if (input.size() < reqlen) { return 0; diff --git a/libbitdht/src/libbitdht.pro b/libbitdht/src/libbitdht.pro index a260a592f..2e120942d 100644 --- a/libbitdht/src/libbitdht.pro +++ b/libbitdht/src/libbitdht.pro @@ -109,6 +109,7 @@ HEADERS += \ bitdht/bdaccount.h \ bitdht/bdquerymgr.h \ util/bdbloom.h \ + bitdht/bdfriendlist.h \ SOURCES += \ bitdht/bencode.c \ @@ -133,5 +134,6 @@ SOURCES += \ bitdht/bdaccount.cc \ bitdht/bdquerymgr.cc \ util/bdbloom.cc \ + bitdht/bdfriendlist.cc \ diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index 60ec526b0..a94ddc869 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/src/udp/udpbitdht.cc @@ -88,6 +88,14 @@ UdpBitDht::~UdpBitDht() /*********** External Interface to the World ************/ /***** Functions to Call down to bdNodeManager ****/ + /* Friend Tracking */ +void UdpBitDht::updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags) +{ + bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ + + mBitDhtManager->updateKnownPeer(id, type, flags); +} + /* Request DHT Peer Lookup */ /* Request Keyword Lookup */ void UdpBitDht::addFindNode(bdNodeId *id, uint32_t mode) diff --git a/libbitdht/src/udp/udpbitdht.h b/libbitdht/src/udp/udpbitdht.h index 712979551..c4e0289cc 100644 --- a/libbitdht/src/udp/udpbitdht.h +++ b/libbitdht/src/udp/udpbitdht.h @@ -57,6 +57,10 @@ virtual ~UdpBitDht(); /*********** External Interface to the World (BitDhtInterface) ************/ /***** Functions to Call down to bdNodeManager ****/ + + /* Friend Tracking */ +virtual void updateKnownPeer(const bdId *id, uint32_t type, uint32_t flags); + /* Request DHT Peer Lookup */ /* Request Keyword Lookup */ virtual void addFindNode(bdNodeId *id, uint32_t mode); diff --git a/libbitdht/src/util/bdbloom.cc b/libbitdht/src/util/bdbloom.cc index 4c44f3ea5..3aefbeb22 100644 --- a/libbitdht/src/util/bdbloom.cc +++ b/libbitdht/src/util/bdbloom.cc @@ -87,7 +87,7 @@ uint8_t convertCharToUint8(char ch1, char ch2) int bloomFilter::setFilterBits(const std::string &hex) { - int bytes = (mFilterBits / BITS_PER_BYTE); + uint32_t bytes = (mFilterBits / BITS_PER_BYTE); if (mFilterBits % BITS_PER_BYTE) { bytes++; @@ -100,7 +100,7 @@ int bloomFilter::setFilterBits(const std::string &hex) // convert to binary array. uint8_t *tmparray = (uint8_t *) malloc(bytes); - int i = 0; + uint32_t i = 0; for(i = 0; i < bytes; i++) { @@ -186,7 +186,7 @@ uint32_t bloomFilter::filterBits() uint32_t bloomFilter::countBits() { int count = 0; - int i; + uint32_t i; for(i = 0; i < mFilterBits; i++) { if (mBits[i]) @@ -206,7 +206,7 @@ void bloomFilter::printFilter(std::ostream &out) out << std::endl; out << "BITS: "; - int i; + uint32_t i; for(i = 0; i < mFilterBits; i++) { if ((i > 0) && (i % 32 == 0)) @@ -236,7 +236,7 @@ void bloomFilter::setHashFunction(int idx, uint32_t (*hashfn)(const std::string void bloomFilter::add(const std::string &hex) { uint32_t (*hashfn)(const std::string &); - int i; + uint32_t i; for(i = 0; i < mNoHashs; i++) { hashfn = mHashFns[i]; @@ -253,7 +253,7 @@ void bloomFilter::add(const std::string &hex) bool bloomFilter::test(const std::string &hex) { uint32_t (*hashfn)(const std::string &); - int i; + uint32_t i; for(i = 0; i < mNoHashs; i++) { hashfn = mHashFns[i];