diff --git a/libbitdht/src/bitdht/bdiface.h b/libbitdht/src/bitdht/bdiface.h index f981420d2..af59eab38 100644 --- a/libbitdht/src/bitdht/bdiface.h +++ b/libbitdht/src/bitdht/bdiface.h @@ -107,6 +107,23 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0; +// DHT MODES +#define BITDHT_MODE_TRAFFIC_MASK 0x00000f00 +#define BITDHT_MODE_RELAY_MASK 0x0000f000 + +// These are not ORd - only one can apply. +#define BITDHT_MODE_TRAFFIC_DEFAULT 0x00000000 +#define BITDHT_MODE_TRAFFIC_HIGH 0x00000100 +#define BITDHT_MODE_TRAFFIC_MED 0x00000200 +#define BITDHT_MODE_TRAFFIC_LOW 0x00000300 +#define BITDHT_MODE_TRAFFIC_TRICKLE 0x00000400 + +// These are not ORd - only one can apply. +#define BITDHT_MODE_RELAYS_IGNORED 0x00000000 +#define BITDHT_MODE_RELAYS_FLAGGED 0x00001000 +#define BITDHT_MODE_RELAYS_ONLY 0x00002000 + + /* NODE OPTIONS */ #define BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT 0x00000001 #define BITDHT_OPTIONS_ENABLE_RELAYS 0x00000002 @@ -306,6 +323,7 @@ class bdQuerySummary #define BITDHT_INFO_CB_TYPE_BADPEER 1 + class BitDhtCallback { public: @@ -370,6 +388,8 @@ virtual int stopDht() = 0; virtual int stateDht() = 0; /* STOPPED, STARTING, ACTIVE, FAILED */ virtual uint32_t statsNetworkSize() = 0; virtual uint32_t statsBDVersionSize() = 0; /* same version as us! */ + +virtual uint32_t setDhtMode(uint32_t dhtFlags) = 0; }; diff --git a/libbitdht/src/bitdht/bdmanager.cc b/libbitdht/src/bitdht/bdmanager.cc index 9a324f4c2..0833468f0 100644 --- a/libbitdht/src/bitdht/bdmanager.cc +++ b/libbitdht/src/bitdht/bdmanager.cc @@ -144,6 +144,15 @@ uint32_t bdNodeManager::statsBDVersionSize() return mBdNetworkSize; } +uint32_t bdNodeManager::setDhtMode(uint32_t dhtFlags) +{ + /* handle options here? */ + setNodeDhtMode(dhtFlags); + + return dhtFlags; +} + + bool bdNodeManager::setAttachMode(bool on) { if (on) diff --git a/libbitdht/src/bitdht/bdmanager.h b/libbitdht/src/bitdht/bdmanager.h index 5343e32ce..b29aaa759 100644 --- a/libbitdht/src/bitdht/bdmanager.h +++ b/libbitdht/src/bitdht/bdmanager.h @@ -140,6 +140,9 @@ virtual int stopDht(); virtual int stateDht(); /* STOPPED, STARTING, ACTIVE, FAILED */ virtual uint32_t statsNetworkSize(); virtual uint32_t statsBDVersionSize(); /* same version as us! */ + +virtual uint32_t setDhtMode(uint32_t dhtFlags); + /******************* Internals *************************/ // Overloaded from bdnode for external node callback. diff --git a/libbitdht/src/bitdht/bdnode.cc b/libbitdht/src/bitdht/bdnode.cc index 31635aba0..4ebcd8676 100644 --- a/libbitdht/src/bitdht/bdnode.cc +++ b/libbitdht/src/bitdht/bdnode.cc @@ -83,8 +83,15 @@ void bdNode::init() //setNodeOptions(BITDHT_OPTIONS_MAINTAIN_UNSTABLE_PORT); setNodeOptions(0); + mNodeDhtMode = 0; + setNodeDhtMode(BITDHT_MODE_TRAFFIC_DEFAULT); + } +/* Unfortunately I've ended up with 2 calls down through the heirarchy... + * not ideal - must clean this up one day. + */ + #define ATTACH_NUMBER 5 void bdNode::setNodeOptions(uint32_t optFlags) { @@ -99,6 +106,55 @@ void bdNode::setNodeOptions(uint32_t optFlags) } } +#define BDNODE_HIGH_MSG_RATE 50 +#define BDNODE_MED_MSG_RATE 10 +#define BDNODE_LOW_MSG_RATE 5 +#define BDNODE_TRICKLE_MSG_RATE 3 + +uint32_t bdNode::setNodeDhtMode(uint32_t dhtFlags) +{ + uint32_t origFlags = mNodeDhtMode; + mNodeDhtMode = dhtFlags; + + uint32_t traffic = dhtFlags & BITDHT_MODE_TRAFFIC_MASK; + + switch(traffic) + { + default: + case BITDHT_MODE_TRAFFIC_DEFAULT: + case BITDHT_MODE_TRAFFIC_LOW: + mMaxAllowedMsgs = BDNODE_HIGH_MSG_RATE; + break; + case BITDHT_MODE_TRAFFIC_HIGH: + mMaxAllowedMsgs = BDNODE_LOW_MSG_RATE; + break; + case BITDHT_MODE_TRAFFIC_MED: + mMaxAllowedMsgs = BDNODE_MED_MSG_RATE; + break; + case BITDHT_MODE_TRAFFIC_TRICKLE: + mMaxAllowedMsgs = BDNODE_TRICKLE_MSG_RATE; + break; + } + + uint32_t relay = dhtFlags & BITDHT_MODE_RELAY_MASK; + if (relay != (origFlags & BITDHT_MODE_RELAY_MASK)) + { + /* changed */ + switch(relay) + { + default: + case BITDHT_MODE_RELAYS_IGNORED: + break; + case BITDHT_MODE_RELAYS_FLAGGED: + break; + case BITDHT_MODE_RELAYS_ONLY: + break; + } + } + + return dhtFlags; +} + void bdNode::getOwnId(bdNodeId *id) { @@ -169,7 +225,12 @@ void bdNode::printState() #ifdef USE_HISTORY mHistory.printMsgs(); #endif + std::cerr << "Outstanding Potential Peers: " << mPotentialPeers.size(); + std::cerr << std::endl; + std::cerr << "Outstanding Query Requests: " << mRemoteQueries.size(); + std::cerr << std::endl; + mAccount.printStats(std::cerr); } @@ -214,50 +275,25 @@ void bdNode::iteration() * so allow up to 90% of messages to be pings. * * ignore responses to other peers... as the number is very small generally + * + * The Rate IS NOW DEFINED IN NodeDhtMode. */ -#define BDNODE_MESSAGE_RATE_HIGH 1 -#define BDNODE_MESSAGE_RATE_MED 2 -#define BDNODE_MESSAGE_RATE_LOW 3 -#define BDNODE_MESSAGE_RATE_TRICKLE 4 - -#define BDNODE_HIGH_MSG_RATE 100 -#define BDNODE_MED_MSG_RATE 50 -#define BDNODE_LOW_MSG_RATE 20 -#define BDNODE_TRICKLE_MSG_RATE 5 - - int maxMsgs = BDNODE_MED_MSG_RATE; - int mAllowedMsgRate = BDNODE_MESSAGE_RATE_MED; - - switch(mAllowedMsgRate) - { - case BDNODE_MESSAGE_RATE_HIGH: - maxMsgs = BDNODE_HIGH_MSG_RATE; - break; - - case BDNODE_MESSAGE_RATE_MED: - maxMsgs = BDNODE_MED_MSG_RATE; - break; - - case BDNODE_MESSAGE_RATE_LOW: - maxMsgs = BDNODE_LOW_MSG_RATE; - break; - - case BDNODE_MESSAGE_RATE_TRICKLE: - maxMsgs = BDNODE_TRICKLE_MSG_RATE; - break; - - default: - break; - - } - - - - int allowedPings = 0.9 * maxMsgs; + int allowedPings = 0.9 * mMaxAllowedMsgs; int sentMsgs = 0; int sentPings = 0; +#define BDNODE_MAX_POTENTIAL_PEERS_MULTIPLIER 5 + + /* Disable Queries if our Ping Queue is too long */ + if (mPotentialPeers.size() > mMaxAllowedMsgs * BDNODE_MAX_POTENTIAL_PEERS_MULTIPLIER) + { + std::cerr << "bdNode::iteration() Disabling Queries until PotentialPeer Queue reduced"; + std::cerr << std::endl; + allowedPings = mMaxAllowedMsgs; + } + + while((mPotentialPeers.size() > 0) && (sentMsgs < allowedPings)) { /* check history ... is we have pinged them already... @@ -301,10 +337,11 @@ void bdNode::iteration() } } - + /* allow each query to send up to one query... until maxMsgs has been reached */ - int sentQueries = mQueryMgr->iterateQueries(maxMsgs-sentMsgs); + int sentQueries = mQueryMgr->iterateQueries(mMaxAllowedMsgs-sentMsgs); sentMsgs += sentQueries; + #ifdef DEBUG_NODE_ACTIONS std::cerr << "bdNode::iteration() maxMsgs: " << maxMsgs << " sentPings: " << sentPings; diff --git a/libbitdht/src/bitdht/bdnode.h b/libbitdht/src/bitdht/bdnode.h index e95fddea7..965b76b90 100644 --- a/libbitdht/src/bitdht/bdnode.h +++ b/libbitdht/src/bitdht/bdnode.h @@ -123,6 +123,7 @@ class bdNode: public bdNodePublisher void init(); /* sets up the self referential classes (mQueryMgr & mConnMgr) */ void setNodeOptions(uint32_t optFlags); + uint32_t setNodeDhtMode(uint32_t dhtFlags); /* startup / shutdown node */ void restartNode(); @@ -249,6 +250,8 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr); private: uint32_t mNodeOptionFlags; + uint32_t mNodeDhtMode; + uint32_t mMaxAllowedMsgs; bdHistory mHistory; /* for understanding the DHT */ @@ -258,6 +261,7 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr); std::list mOutgoingMsgs; std::list mIncomingMsgs; + }; diff --git a/libbitdht/src/udp/udpbitdht.cc b/libbitdht/src/udp/udpbitdht.cc index d9477a699..6474bd444 100644 --- a/libbitdht/src/udp/udpbitdht.cc +++ b/libbitdht/src/udp/udpbitdht.cc @@ -249,6 +249,14 @@ uint32_t UdpBitDht::statsBDVersionSize() return mBitDhtManager->statsBDVersionSize(); } +uint32_t UdpBitDht::setDhtMode(uint32_t dhtFlags) +{ + bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/ + + return mBitDhtManager->setDhtMode(dhtFlags); +} + + /******************* Internals *************************/ /***** Iteration / Loop Management *****/ diff --git a/libbitdht/src/udp/udpbitdht.h b/libbitdht/src/udp/udpbitdht.h index 3717ab718..52742b912 100644 --- a/libbitdht/src/udp/udpbitdht.h +++ b/libbitdht/src/udp/udpbitdht.h @@ -93,6 +93,7 @@ virtual int stopDht(); virtual int stateDht(); virtual uint32_t statsNetworkSize(); virtual uint32_t statsBDVersionSize(); +virtual uint32_t setDhtMode(uint32_t dhtFlags); /******************* Internals *************************/ /***** Iteration / Loop Management *****/ diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index 502400c09..01ba24e49 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -879,8 +879,12 @@ bool p3LinkMgrIMPL::connectResult(const std::string &id, bool success, uint32_t /* always switch it off now */ mNetMgr->netAssistFriend(id,false); - /* inform NetMgr that we know this peers address */ - mNetMgr->netAssistKnownPeer(id,remote_peer_address, NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_ONLINE); + /* inform NetMgr that we know this peers address: but only if external address */ + if (isExternalNet(&(remote_peer_address.sin_addr))) + { + mNetMgr->netAssistKnownPeer(id,remote_peer_address, + NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_ONLINE); + } } else { diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 6c23d3266..3678d6845 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -2135,10 +2135,12 @@ int RsServer::StartupRetroShare() pqih -> addService(mBanList); mBitDht->setupPeerSharer(mBanList); +#ifdef RS_DSDVTEST p3Dsdv *mDsdv = new p3Dsdv(mLinkMgr); pqih -> addService(mDsdv); rsDsdv = mDsdv; mDsdv->addTestService(); +#endif #endif // MINIMAL_LIBRS diff --git a/libretroshare/src/services/p3banlist.cc b/libretroshare/src/services/p3banlist.cc index 0acde8696..32d1fa117 100644 --- a/libretroshare/src/services/p3banlist.cc +++ b/libretroshare/src/services/p3banlist.cc @@ -103,7 +103,8 @@ bool p3BanList::processIncoming() break; case RS_PKT_SUBTYPE_BANLIST_ITEM: { - updated = (updated || recvBanItem((RsBanListItem *) item)); + // Order is important!. + updated = (recvBanItem((RsBanListItem *) item) || updated); } break; } @@ -136,8 +137,9 @@ bool p3BanList::recvBanItem(RsBanListItem *item) std::list::const_iterator it; for(it = item->peerList.entries.begin(); it != item->peerList.entries.end(); it++) { - updated = (updated || addBanEntry(item->PeerId(), - it->addr, it->level, it->reason, it->age)); + // Order is important!. + updated = (addBanEntry(item->PeerId(), it->addr, it->level, + it->reason, it->age) || updated); } return updated; } @@ -173,6 +175,17 @@ bool p3BanList::addBanEntry(const std::string &peerId, const struct sockaddr_in std::cerr << std::endl; #endif + /* Only Accept it - if external address */ + if (!isExternalNet(&(addr.sin_addr))) + { +#ifdef DEBUG_BANLIST + std::cerr << "p3BanList::addBanEntry() Ignoring Non External Addr: " << rs_inet_ntoa(addr.sin_addr); + std::cerr << std::endl; +#endif + return false; + } + + std::map::iterator it; it = mBanSources.find(peerId); if (it == mBanSources.end())