Improvements to DHT to support Relays.

- Updated DhtMode Flags.
 - Allowed DhtMode Flags to be set independently.
 - Set RelayMode to ConnectionManager.
 - Added dropRelayServers() / pingRelayServers()
 - Added Support fns for above two functions.



git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-dhtmods@4729 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2011-12-20 02:03:25 +00:00
parent 203fd6bffc
commit 52e9a27f7b
9 changed files with 211 additions and 30 deletions

View File

@ -86,6 +86,13 @@ void bdConnectManager::setConnectionOptions(uint32_t allowedModes, uint32_t flag
}
/* Setup Relay Mode */
void bdConnectManager::setRelayMode(uint32_t mode)
{
mRelayMode = mode;
}
void bdConnectManager::shutdownConnections()
{
#ifdef DEBUG_NODE_CONNECTION
@ -2085,6 +2092,15 @@ int bdConnectManager::recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId
/* remove connection */
bdConnectManager::cleanConnectionBySender(id, srcConnAddr, destConnAddr);
/* WILL NEED CALLBACK FOR FAILED PROXY ATTEMPT - TO SUPPORT RELAYS PROPERLY
* NODE needs to know PEERS to potentially WHITELIST!
*/
if (mRelayMode == BITDHT_RELAYS_SERVER)
{
callbackConnect(&(conn->mSrcId),&(conn->mProxyId),&(conn->mDestId),
conn->mMode, conn->mPoint, param, BITDHT_CONNECT_CB_AUTH,
BITDHT_CONNECT_ERROR_NOADDRESS);
}
}
}
else

View File

@ -255,6 +255,9 @@ class bdConnectManager
int recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int delayOrBandwidth);
int recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
/* setup Relay Mode */
void setRelayMode(uint32_t mode);
private:
std::map<bdProxyTuple, bdConnection> mConnections;
@ -263,6 +266,8 @@ class bdConnectManager
uint32_t mConfigAllowedModes;
bool mConfigAutoProxy;
uint32_t mRelayMode;
/****************************** Connection Code (in bdconnection.cc) ****************************/
private:

View File

@ -159,6 +159,33 @@ bool bdFriendList::findPeerEntry(const bdNodeId *id, bdFriendEntry &entry)
}
bool bdFriendList::findPeersWithFlags(uint32_t flags, std::list<bdNodeId> &peerList)
{
#ifdef DEBUG_FRIENDLIST
std::cerr << "bdFriendList::findPeersWithFlags(" << flags << ")";
std::cerr << std::endl;
#endif
/* see if it exists... */
std::map<bdNodeId, bdFriendEntry>::iterator it;
for(it = mPeers.begin(); it != mPeers.end(); it++)
{
/* if they have ALL of the flags we specified */
if ((it->second.getPeerFlags() & flags) == flags)
{
#ifdef DEBUG_FRIENDLIST
std::cerr << "bdFriendList::findPeersWithFlags() Found: ";
bdStdPrintNodeId(std::cerr, id);
std::cerr << std::endl;
#endif
peerList.push_back(it->second.mPeerId.id);
}
}
return (peerList.size() > 0);
}
bool bdFriendList::print(std::ostream &out)
{
time_t now = time(NULL);

View File

@ -77,6 +77,7 @@ bool updatePeer(const bdId *id, uint32_t flags);
bool removePeer(const bdNodeId *id);
bool findPeerEntry(const bdNodeId *id, bdFriendEntry &entry);
bool findPeersWithFlags(uint32_t flags, std::list<bdNodeId> &peerList);
bool print(std::ostream &out);
private:

View File

@ -109,19 +109,20 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0;
// DHT MODES
#define BITDHT_MODE_TRAFFIC_MASK 0x00000f00
#define BITDHT_MODE_RELAY_MASK 0x0000f000
#define BITDHT_MODE_RELAYSERVER_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
#define BITDHT_MODE_TRAFFIC_DEFAULT BITDHT_MODE_TRAFFIC_LOW
// 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
#define BITDHT_MODE_RELAYSERVERS_IGNORED 0x00001000
#define BITDHT_MODE_RELAYSERVERS_FLAGGED 0x00002000
#define BITDHT_MODE_RELAYSERVERS_ONLY 0x00003000
#define BITDHT_MODE_RELAYSERVERS_SERVER 0x00004000
/* NODE OPTIONS */
@ -303,9 +304,9 @@ class bdQuerySummary
/* Query Flags */
#define BITDHT_QFLAGS_NONE 0x0000
#define BITDHT_QFLAGS_DISGUISE 0x0001
#define BITDHT_QFLAGS_DISGUISE 0x0001 // Don't search directly for target.
#define BITDHT_QFLAGS_DO_IDLE 0x0002
#define BITDHT_QFLAGS_INTERNAL 0x0004 // runs through startup.
#define BITDHT_QFLAGS_INTERNAL 0x0004 // runs through startup. (limited callback)
#define BITDHT_QFLAGS_UPDATES 0x0008 // Do regular updates.
/* Connect Callback Flags */
@ -323,6 +324,12 @@ class bdQuerySummary
#define BITDHT_INFO_CB_TYPE_BADPEER 1
/* Relay Modes */
#define BITDHT_RELAYS_OFF 0
#define BITDHT_RELAYS_ON 1
#define BITDHT_RELAYS_ONLY 2
#define BITDHT_RELAYS_SERVER 3
class BitDhtCallback
{

View File

@ -111,47 +111,81 @@ void bdNode::setNodeOptions(uint32_t optFlags)
#define BDNODE_LOW_MSG_RATE 5
#define BDNODE_TRICKLE_MSG_RATE 3
/* So we are setting this up so you can independently update each parameter....
* if the mask is empty - it'll use the previous parameter.
*
*/
uint32_t bdNode::setNodeDhtMode(uint32_t dhtFlags)
{
std::cerr << "bdNode::setNodeDhtMode(" << dhtFlags << "), origFlags: " << mNodeDhtMode;
std::cerr << std::endl;
uint32_t origFlags = mNodeDhtMode;
mNodeDhtMode = dhtFlags;
uint32_t traffic = dhtFlags & BITDHT_MODE_TRAFFIC_MASK;
switch(traffic)
if (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;
switch(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;
}
}
else
{
dhtFlags |= (origFlags & BITDHT_MODE_TRAFFIC_MASK);
}
uint32_t relay = dhtFlags & BITDHT_MODE_RELAY_MASK;
if (relay != (origFlags & BITDHT_MODE_RELAY_MASK))
uint32_t relay = dhtFlags & BITDHT_MODE_RELAYSERVER_MASK;
if ((relay) && (relay != (origFlags & BITDHT_MODE_RELAYSERVER_MASK)))
{
/* changed */
switch(relay)
{
default:
case BITDHT_MODE_RELAYS_IGNORED:
case BITDHT_MODE_RELAYSERVERS_IGNORED:
mRelayMode = BITDHT_RELAYS_OFF;
dropRelayServers();
break;
case BITDHT_MODE_RELAYS_FLAGGED:
case BITDHT_MODE_RELAYSERVERS_FLAGGED:
mRelayMode = BITDHT_RELAYS_ON;
pingRelayServers();
break;
case BITDHT_MODE_RELAYS_ONLY:
case BITDHT_MODE_RELAYSERVERS_ONLY:
mRelayMode = BITDHT_RELAYS_ONLY;
pingRelayServers();
break;
case BITDHT_MODE_RELAYSERVERS_SERVER:
mRelayMode = BITDHT_RELAYS_SERVER;
pingRelayServers();
break;
}
mConnMgr->setRelayMode(mRelayMode);
}
else
{
dhtFlags |= (origFlags & BITDHT_MODE_RELAYSERVER_MASK);
}
mNodeDhtMode = dhtFlags;
std::cerr << "bdNode::setNodeDhtMode() newFlags: " << mNodeDhtMode;
std::cerr << std::endl;
return dhtFlags;
}
@ -2206,5 +2240,55 @@ bdNodeNetMsg::~bdNodeNetMsg()
/**************** In/Out of Relay Mode ******************/
void bdNode::dropRelayServers()
{
/* We leave them there... just drop the flags */
uint32_t flags = BITDHT_PEER_STATUS_DHT_RELAY_SERVER;
std::list<bdNodeId> peerList;
std::list<bdNodeId>::iterator it;
mFriendList.findPeersWithFlags(flags, peerList);
for(it = peerList.begin(); it != peerList.end(); it++)
{
mFriendList.removePeer(&(*it));
}
mNodeSpace.clean_node_flags(flags);
}
void bdNode::pingRelayServers()
{
/* if Relay's have been switched on, do search/ping to locate servers */
std::cerr << "bdNode::pingRelayServers()";
std::cerr << std::endl;
bool doSearch = true;
uint32_t flags = BITDHT_PEER_STATUS_DHT_RELAY_SERVER;
std::list<bdNodeId> peerList;
std::list<bdNodeId>::iterator it;
mFriendList.findPeersWithFlags(flags, peerList);
for(it = peerList.begin(); it != peerList.end(); it++)
{
if (doSearch)
{
uint32_t qflags = BITDHT_QFLAGS_INTERNAL | BITDHT_QFLAGS_DISGUISE;
mQueryMgr->addQuery(&(*it), qflags);
std::cerr << "bdNode::pingRelayServers() Adding Internal Search for Relay Server: ";
mFns->bdPrintNodeId(std::cerr, &(*it));
std::cerr << std::endl;
}
else
{
/* try ping - if we have an address??? */
}
}
}

View File

@ -158,6 +158,9 @@ class bdNode: public bdNodePublisher
int outgoingMsg(struct sockaddr_in *addr, char *msg, int *len);
void incomingMsg(struct sockaddr_in *addr, char *msg, int len);
// For Relay Mode switching.
void dropRelayServers();
void pingRelayServers();
// Below is internal Management of incoming / outgoing messages.
private:
@ -251,7 +254,9 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
uint32_t mNodeOptionFlags;
uint32_t mNodeDhtMode;
uint32_t mMaxAllowedMsgs;
uint32_t mRelayMode;
bdHistory mHistory; /* for understanding the DHT */

View File

@ -436,6 +436,39 @@ int bdSpace::find_exactnode(const bdId *id, bdPeer &peer)
}
int bdSpace::clean_node_flags(uint32_t flags)
{
std::cerr << "bdSpace::clean_node_flags(" << flags << ")";
std::cerr << std::endl;
int count = 0;
std::vector<bdBucket>::iterator bit;
for(bit = buckets.begin(); bit != buckets.end(); bit++)
{
std::list<bdPeer>::iterator eit;
for(eit = bit->entries.begin(); eit != bit->entries.end(); eit++)
{
if (flags & eit->mPeerFlags)
{
std::cerr << "bdSpace::clean_node_flags() Found Match: ";
mFns->bdPrintId(std::cerr, &(eit->mPeerId));
std::cerr << " withFlags: " << eit->mPeerFlags;
std::cerr << std::endl;
count++;
eit->mPeerFlags &= ~flags;
}
}
}
std::cerr << "bdSpace::clean_node_flags() Cleaned " << count << " Matching Peers";
std::cerr << std::endl;
return count;
}
#define BITDHT_ATTACHED_SEND_PERIOD 17
int bdSpace::scanOutOfDatePeers(std::list<bdId> &peerIds)

View File

@ -190,6 +190,9 @@ uint32_t calcSpaceSizeWithFlag(uint32_t withFlag);
/* special function to enable DHT localisation (i.e find peers from own network) */
bool findRandomPeerWithFlag(bdId &id, uint32_t withFlag);
/* strip out flags - to switch in/out of relay mode */
int clean_node_flags(uint32_t flags);
/* to add later */
int updateOwnId(bdNodeId *newOwnId);