mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-04-23 16:39:16 -04:00
Start of major improvements to bitdht. This checkin adds the bulk of the
code for UDP connection setup. The code compiles, but is not yet functional. Specific changes: * Added srcId to PotentialPeer calls, to determine possible proxies. * Added PotentialProxy list to bdQuery() code. * Tweaked bdspace::find_nearest_nodes() to include peer flags. * added bdconnection.[h|cc] files, these contain most of the new code. * Added ConnectionRequest() ConnectionAuth() and ConnectionCallback() to interface * Added new connection messages to bdmsgs.cc * Added stats for new connection messages. * separated send_query() into own function. * Added bdStdLoadNodeId() to allow id to be imported from a string. * Added Export of bdSpace::getDhtBucket() for external display. * moved various definitions to bdiface.h for external use (bdPeer, bdBucket) git-svn-id: http://svn.code.sf.net/p/retroshare/code/branches/v0.5-peernet@4250 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
parent
fb0ce727e0
commit
6b8f66a22b
1376
libbitdht/src/bitdht/bdconnection.cc
Normal file
1376
libbitdht/src/bitdht/bdconnection.cc
Normal file
File diff suppressed because it is too large
Load Diff
144
libbitdht/src/bitdht/bdconnection.h
Normal file
144
libbitdht/src/bitdht/bdconnection.h
Normal file
@ -0,0 +1,144 @@
|
||||
#ifndef BITDHT_CONNECTION_H
|
||||
#define BITDHT_CONNECTION_H
|
||||
|
||||
/*
|
||||
* bitdht/bdconnection.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".
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#include "bitdht/bdiface.h"
|
||||
|
||||
|
||||
/************************************************************************************************************
|
||||
************************************** ProxyTuple + Connection State ****************************************
|
||||
************************************************************************************************************/
|
||||
|
||||
#define BITDHT_CONNREQUEST_INIT 1
|
||||
#define BITDHT_CONNREQUEST_INPROGRESS 2
|
||||
#define BITDHT_CONNREQUEST_DONE 3
|
||||
|
||||
|
||||
#define BITDHT_CONNECTION_WAITING_AUTH 1
|
||||
#define BITDHT_CONNECTION_WAITING_REPLY 2
|
||||
#define BITDHT_CONNECTION_WAITING_START 3
|
||||
#define BITDHT_CONNECTION_WAITING_ACK 4
|
||||
#define BITDHT_CONNECTION_COMPLETED 5
|
||||
|
||||
|
||||
#define BD_CONNECTION_START_RETRY_PERIOD 10
|
||||
#define BD_CONNECTION_START_MAX_RETRY 3
|
||||
#define BD_CONNECTION_MAX_TIMEOUT 30
|
||||
|
||||
|
||||
|
||||
class bdProxyTuple
|
||||
{
|
||||
public:
|
||||
bdProxyTuple() { return; }
|
||||
bdProxyTuple(bdNodeId *s, bdNodeId *p, bdNodeId *d)
|
||||
:srcId(*s), proxyId(*p), destId(*d) { return; }
|
||||
|
||||
bdNodeId srcId;
|
||||
bdNodeId proxyId;
|
||||
bdNodeId destId;
|
||||
};
|
||||
|
||||
int operator<(const bdProxyTuple &a, const bdProxyTuple &b);
|
||||
int operator==(const bdProxyTuple &a, const bdProxyTuple &b);
|
||||
|
||||
|
||||
class bdConnection
|
||||
{
|
||||
public:
|
||||
bdConnection();
|
||||
|
||||
/** Functions to tweak the connection status */
|
||||
|
||||
// User initialised Connection.
|
||||
int ConnectionSetup(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
|
||||
// Initialise a new Connection. (receiving a Connection Request)
|
||||
int ConnectionRequestDirect(bdId *id, bdId *srcConnAddr, bdId *destConnAddr);
|
||||
int ConnectionRequestProxy(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
int ConnectionRequestEnd(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
|
||||
// Setup Finishing Stage, (receiving a Connection Reply).
|
||||
int upgradeProxyConnectionToFinish(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status);
|
||||
|
||||
int AuthoriseDirectConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
int AuthoriseProxyConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
int AuthoriseEndConnection(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
|
||||
/* Connection State, and TimeStamp of Update */
|
||||
int mState;
|
||||
time_t mLastEvent;
|
||||
|
||||
/* Addresses of Start/Proxy/End Nodes */
|
||||
bdId mSrcId;
|
||||
bdId mDestId;
|
||||
bdId mProxyId;
|
||||
|
||||
/* Where we are in the connection,
|
||||
* and what connection mode.
|
||||
*/
|
||||
int mPoint;
|
||||
int mMode;
|
||||
|
||||
/* must have ip:ports of connection ends (if proxied) */
|
||||
bdId mSrcConnAddr;
|
||||
bdId mDestConnAddr;
|
||||
|
||||
int mBandwidth;
|
||||
|
||||
/* START/ACK Finishing ****/
|
||||
time_t mLastStart; /* timer for retries */
|
||||
int mRetryCount; /* retry counter */
|
||||
|
||||
bool mSrcAck;
|
||||
bool mDestAck;
|
||||
|
||||
// Completion TS.
|
||||
time_t mCompletedTS;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class bdConnectionRequest
|
||||
{
|
||||
public:
|
||||
int setupDirectConnection(struct sockaddr_in *laddr, bdNodeId *target);
|
||||
int setupProxyConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
|
||||
int addPotentialProxy(bdId *srcId);
|
||||
|
||||
std::list<bdId> mPotentialProxies;
|
||||
|
||||
int mState;
|
||||
|
||||
int stuff;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include "util/bdnet.h"
|
||||
@ -121,15 +122,60 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0;
|
||||
|
||||
#define BITDHT_PEER_STATUS_MASK_RECVD 0x000000ff
|
||||
#define BITDHT_PEER_STATUS_MASK_DHT 0x0000ff00
|
||||
#define BITDHT_PEER_STATUS_MASK_KNOWN 0x00ff0000
|
||||
|
||||
#define BITDHT_PEER_STATUS_RECV_PONG 0x00000001
|
||||
#define BITDHT_PEER_STATUS_RECV_NODES 0x00000002
|
||||
#define BITDHT_PEER_STATUS_RECV_HASHES 0x00000004
|
||||
#define BITDHT_PEER_STATUS_RECV_CONNECT_MSG 0x00000008
|
||||
|
||||
#define BITDHT_PEER_STATUS_DHT_ENGINE 0x00000100
|
||||
#define BITDHT_PEER_STATUS_DHT_APPL 0x00000200
|
||||
#define BITDHT_PEER_STATUS_DHT_VERSION 0x00000400
|
||||
|
||||
#define BITDHT_PEER_STATUS_DHT_WHITELIST 0x00010000
|
||||
#define BITDHT_PEER_STATUS_DHT_FOF 0x00020000
|
||||
#define BITDHT_PEER_STATUS_DHT_FRIEND 0x00040000
|
||||
|
||||
|
||||
|
||||
#define BITDHT_CONNECT_MASK_MODE 0x000000ff
|
||||
#define BITDHT_CONNECT_MASK_ANSWER 0x0000ff00
|
||||
|
||||
#define BITDHT_CONNECT_MODE_DIRECT 0x00000001
|
||||
#define BITDHT_CONNECT_MODE_PROXY 0x00000002
|
||||
#define BITDHT_CONNECT_MODE_RELAY 0x00000004
|
||||
|
||||
#define BITDHT_CONNECT_ANSWER_OKAY 0x00000100
|
||||
#define BITDHT_CONNECT_ANSWER_NOK 0x00000200
|
||||
#define BITDHT_CONNECT_ANSWER_NCONN 0x00000400
|
||||
|
||||
#define BITDHT_CONNECT_ANSWER_TOAUTH 0x00001000
|
||||
|
||||
|
||||
/* Definitions of bdSpace Peer and Bucket are publically available,
|
||||
* so we can expose the bucket entries for the gui.
|
||||
*/
|
||||
|
||||
class bdPeer
|
||||
{
|
||||
public:
|
||||
bdId mPeerId;
|
||||
uint32_t mPeerFlags;
|
||||
time_t mLastSendTime;
|
||||
time_t mLastRecvTime;
|
||||
time_t mFoundTime; /* time stamp that peer was found */
|
||||
};
|
||||
|
||||
class bdBucket
|
||||
{
|
||||
public:
|
||||
|
||||
bdBucket();
|
||||
/* list so we can queue properly */
|
||||
std::list<bdPeer> entries;
|
||||
};
|
||||
|
||||
|
||||
/* Status options */
|
||||
#define BITDHT_QUERY_READY 1
|
||||
@ -145,6 +191,18 @@ virtual void bdPrintNodeId(std::ostream &out, const bdNodeId *a) = 0;
|
||||
#define BITDHT_QFLAGS_DO_IDLE 2
|
||||
#define BITDHT_QFLAGS_INTERNAL 4 // means it runs through startup.
|
||||
|
||||
/* Connect Callback Flags */
|
||||
#define BITDHT_CONNECT_CB_AUTH 1
|
||||
#define BITDHT_CONNECT_CB_PENDING 2
|
||||
#define BITDHT_CONNECT_CB_START 3
|
||||
#define BITDHT_CONNECT_CB_PROXY 4
|
||||
#define BITDHT_CONNECT_CB_FAILED 5
|
||||
|
||||
#define BD_PROXY_CONNECTION_UNKNOWN_POINT 0
|
||||
#define BD_PROXY_CONNECTION_START_POINT 1
|
||||
#define BD_PROXY_CONNECTION_MID_POINT 2
|
||||
#define BD_PROXY_CONNECTION_END_POINT 3
|
||||
|
||||
class BitDhtCallback
|
||||
{
|
||||
public:
|
||||
@ -156,6 +214,11 @@ virtual int dhtNodeCallback(const bdId * /*id*/, uint32_t /*peerflags*/) { ret
|
||||
// must be implemented.
|
||||
virtual int dhtPeerCallback(const bdId *id, uint32_t status) = 0;
|
||||
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status) = 0;
|
||||
|
||||
// connection callback. Not required for basic behaviour, but forced for initial development.
|
||||
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
||||
uint32_t mode, uint32_t point, uint32_t cbtype) = 0; /* { return 0; } */
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -168,6 +231,10 @@ virtual void addFindNode(bdNodeId *id, uint32_t mode) = 0;
|
||||
virtual void removeFindNode(bdNodeId *id) = 0;
|
||||
virtual void findDhtValue(bdNodeId *id, std::string key, uint32_t mode) = 0;
|
||||
|
||||
/***** Connections Requests *****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode) = 0;
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer) = 0;
|
||||
|
||||
/***** Add / Remove Callback Clients *****/
|
||||
virtual void addCallback(BitDhtCallback *cb) = 0;
|
||||
virtual void removeCallback(BitDhtCallback *cb) = 0;
|
||||
@ -175,6 +242,7 @@ virtual void removeCallback(BitDhtCallback *cb) = 0;
|
||||
/***** Get Results Details *****/
|
||||
virtual int getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from) = 0;
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value) = 0;
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket) = 0;
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht() = 0;
|
||||
|
@ -54,6 +54,7 @@
|
||||
* #define DEBUG_MGR_PKT 1
|
||||
***/
|
||||
|
||||
//#define DEBUG_MGR 1
|
||||
|
||||
bdNodeManager::bdNodeManager(bdNodeId *id, std::string dhtVersion, std::string bootfile, bdDhtFunctions *fns)
|
||||
:bdNode(id, dhtVersion, bootfile, fns)
|
||||
@ -423,9 +424,9 @@ void bdNodeManager::QueryRandomLocalNet()
|
||||
int bdNodeManager::status()
|
||||
{
|
||||
/* do status of bdNode */
|
||||
#ifdef DEBUG_MGR
|
||||
//#ifdef DEBUG_MGR
|
||||
printState();
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
checkStatus();
|
||||
|
||||
@ -434,10 +435,10 @@ int bdNodeManager::status()
|
||||
mBdNetworkSize = mNodeSpace.calcNetworkSizeWithFlag(
|
||||
BITDHT_PEER_STATUS_DHT_APPL);
|
||||
|
||||
#ifdef DEBUG_MGR
|
||||
//#ifdef DEBUG_MGR
|
||||
std::cerr << "BitDHT NetworkSize: " << mNetworkSize << std::endl;
|
||||
std::cerr << "BitDHT App NetworkSize: " << mBdNetworkSize << std::endl;
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
@ -794,6 +795,11 @@ int bdNodeManager::getDhtValue(const bdNodeId *id, std::string key, std::string
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bdNodeManager::getDhtBucket(const int idx, bdBucket &bucket)
|
||||
{
|
||||
return mNodeSpace.getDhtBucket(idx, bucket);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/***** Add / Remove Callback Clients *****/
|
||||
@ -870,7 +876,7 @@ void bdNodeManager::doPeerCallback(const bdId *id, uint32_t status)
|
||||
|
||||
#ifdef DEBUG_MGR
|
||||
std::cerr << "bdNodeManager::doPeerCallback()";
|
||||
mFns->bdPrintNodeId(std::cerr, id);
|
||||
mFns->bdPrintId(std::cerr, id);
|
||||
std::cerr << "status: " << status;
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
@ -900,6 +906,8 @@ void bdNodeManager::doValueCallback(const bdNodeId *id, std::string key, uint32_
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************* Internals *************************/
|
||||
int bdNodeManager::isBitDhtPacket(char *data, int size, struct sockaddr_in & from)
|
||||
|
||||
@ -1006,12 +1014,12 @@ bdDebugCallback::~bdDebugCallback()
|
||||
{
|
||||
}
|
||||
|
||||
int bdDebugCallback::dhtPeerCallback(const bdNodeId *id, uint32_t status)
|
||||
int bdDebugCallback::dhtPeerCallback(const bdId *id, uint32_t status)
|
||||
{
|
||||
#ifdef DEBUG_MGR
|
||||
std::cerr << "bdDebugCallback::dhtPeerCallback() Id: ";
|
||||
#endif
|
||||
bdStdPrintNodeId(std::cerr, id);
|
||||
bdStdPrintId(std::cerr, id);
|
||||
#ifdef DEBUG_MGR
|
||||
std::cerr << " status: " << std::hex << status << std::dec << std::endl;
|
||||
#endif
|
||||
@ -1033,3 +1041,65 @@ int bdDebugCallback::dhtValueCallback(const bdNodeId *id, std::string key, uint3
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/******************* Connection Stuff ********************/
|
||||
|
||||
|
||||
|
||||
void bdNodeManager::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode)
|
||||
{
|
||||
bdNode::requestConnection(laddr, target, mode);
|
||||
}
|
||||
|
||||
void bdNodeManager::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer)
|
||||
{
|
||||
if (answer)
|
||||
{
|
||||
AuthConnectionOk(srcId, proxyId, destId, mode, loc);
|
||||
}
|
||||
else
|
||||
{
|
||||
AuthConnectionNo(srcId, proxyId, destId, mode, loc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/***** Connections Requests *****/
|
||||
|
||||
// Overloaded from bdnode for external node callback.
|
||||
void bdNodeManager::callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int point, int cbtype)
|
||||
{
|
||||
std::cerr << "bdNodeManager::callbackConnect()";
|
||||
std::cerr << std::endl;
|
||||
|
||||
#ifdef DEBUG_MGR
|
||||
#endif
|
||||
/* search list */
|
||||
std::list<BitDhtCallback *>::iterator it;
|
||||
for(it = mCallbacks.begin(); it != mCallbacks.end(); it++)
|
||||
{
|
||||
(*it)->dhtConnectCallback(srcId, proxyId, destId, mode, point, cbtype);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int bdDebugCallback::dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
||||
uint32_t mode, uint32_t point, uint32_t cbtype)
|
||||
{
|
||||
#ifdef DEBUG_MGR
|
||||
std::cerr << "bdDebugCallback::dhtConnectCallback() Type: " << cbtype;
|
||||
std::cerr << " srcId: ";
|
||||
bdStdPrintId(std::cerr, srcId);
|
||||
std::cerr << " proxyId: ";
|
||||
bdStdPrintId(std::cerr, proxyId);
|
||||
std::cerr << " destId: ";
|
||||
bdStdPrintId(std::cerr, destId);
|
||||
std::cerr << " mode: " << mode;
|
||||
std::cerr << " point: " << point << std::endl;
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
@ -113,6 +113,13 @@ virtual void removeCallback(BitDhtCallback *cb);
|
||||
/***** Get Results Details *****/
|
||||
virtual int getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from);
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value);
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
/***** Connection Interface ****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
uint32_t mode, uint32_t loc, uint32_t answer);
|
||||
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht();
|
||||
@ -124,7 +131,9 @@ virtual uint32_t statsBDVersionSize(); /* same version as us! */
|
||||
|
||||
// Overloaded from bdnode for external node callback.
|
||||
virtual void addPeer(const bdId *id, uint32_t peerflags);
|
||||
|
||||
// Overloaded from bdnode for external node callback.
|
||||
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype);
|
||||
|
||||
int isBitDhtPacket(char *data, int size, struct sockaddr_in &from);
|
||||
private:
|
||||
@ -162,8 +171,10 @@ class bdDebugCallback: public BitDhtCallback
|
||||
{
|
||||
public:
|
||||
~bdDebugCallback();
|
||||
virtual int dhtPeerCallback(const bdNodeId *id, uint32_t status);
|
||||
virtual int dhtPeerCallback(const bdId *id, uint32_t status);
|
||||
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status);
|
||||
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
||||
uint32_t mode, uint32_t point, uint32_t cbtype);
|
||||
|
||||
};
|
||||
|
||||
|
@ -831,3 +831,176 @@ int beMsgGetUInt32(be_node *n, uint32_t *port)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/********************************************************************************************************************
|
||||
* CONNECTION EXTENSIONS
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
ping Query = {"t":"aa", "y":"q", "q":"ping", "a":{"id":"abcdefghij0123456789"}}
|
||||
bencoded = d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe
|
||||
*/
|
||||
|
||||
/*
|
||||
Response = {"t":"aa", "y":"r", "r": {"id":"mnopqrstuvwxyz123456"}}
|
||||
bencoded = d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re
|
||||
*/
|
||||
|
||||
/*
|
||||
find_node Query = {"t":"aa", "y":"q", "q":"find_node", "a": {"id":"abcdefghij0123456789", "target":"mnopqrstuvwxyz123456"}}
|
||||
bencoded = d1:ad2:id20:abcdefghij01234567896:target20:mnopqrstuvwxyz123456e1:q9:find_node1:t2:aa1:y1:qe
|
||||
*/
|
||||
|
||||
#if 0
|
||||
int bitdht_find_node_msg(bdToken *tid, bdNodeId *id, bdNodeId *target,
|
||||
char *msg, int avail)
|
||||
{
|
||||
#ifdef DEBUG_MSGS
|
||||
fprintf(stderr, "bitdht_find_node_msg()\n");
|
||||
#endif
|
||||
|
||||
be_node *dict = be_create_dict();
|
||||
|
||||
be_node *iddict = be_create_dict();
|
||||
be_node *idnode = be_create_str_wlen((char *) id->data, BITDHT_KEY_LEN);
|
||||
be_node *targetnode = be_create_str_wlen((char *) target->data, BITDHT_KEY_LEN);
|
||||
|
||||
be_node *tidnode = be_create_str_wlen((char *) tid->data, tid->len);
|
||||
be_node *yqrnode = be_create_str("q");
|
||||
be_node *findnode = be_create_str("find_node");
|
||||
|
||||
be_add_keypair(iddict, "id", idnode);
|
||||
be_add_keypair(iddict, "target", targetnode);
|
||||
be_add_keypair(dict, "a", iddict);
|
||||
|
||||
be_add_keypair(dict, "t", tidnode);
|
||||
be_add_keypair(dict, "y", yqrnode);
|
||||
be_add_keypair(dict, "q", findnode);
|
||||
|
||||
#ifdef DEBUG_MSG_DUMP
|
||||
/* dump answer */
|
||||
be_dump(dict);
|
||||
#endif
|
||||
|
||||
int blen = be_encode(dict, msg, avail);
|
||||
be_free(dict);
|
||||
|
||||
return blen;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****
|
||||
* Thinking about the format of this message.
|
||||
* id: ownId is stanard in all other messages, so should keep the same!.
|
||||
* src:
|
||||
* target:
|
||||
* mode: d,p or r
|
||||
*
|
||||
* A -> B -> C
|
||||
* direct: A ------> B
|
||||
* ---> id:A src:A target:B mode:d
|
||||
* <--- id:B src:A target:B mode:d a:OK
|
||||
*
|
||||
* proxy: A ------> B -------> C
|
||||
* A->B id:A src:A target:C mode:p q
|
||||
*
|
||||
* a)
|
||||
* B->A id:B src:A target:C mode:p r:NOK
|
||||
*
|
||||
* b)
|
||||
* B->C id:B src:A target:C mode:p q
|
||||
* C->B id:C src:A target:C mode:p r:NOK
|
||||
* B->A id:B src:A target:C mode:p r:NOK
|
||||
*
|
||||
* c)
|
||||
* B->C id:B src:A target:C mode:p q
|
||||
* C->B id:C src:A target:C mode:p r:OK
|
||||
* B->A id:B src:A target:C mode:p r:OK
|
||||
* connect happens.
|
||||
* Dropped packets will affect this!
|
||||
*
|
||||
*
|
||||
* REQUIRED BITS FOR A MESSAGE
|
||||
* 1) DIRECT
|
||||
* -> REQUEST, ownId, targetId, transId, mode.
|
||||
* -> RESPONSE, ownId, targetId, transId, mode, answer.
|
||||
*
|
||||
* 2) PROXY
|
||||
*/
|
||||
|
||||
|
||||
int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int status, char *msg, int avail)
|
||||
{
|
||||
#ifdef DEBUG_MSGS
|
||||
fprintf(stderr, "bitdht_connect_genmsg()\n");
|
||||
#endif
|
||||
|
||||
be_node *dict = be_create_dict();
|
||||
|
||||
be_node *iddict = be_create_dict();
|
||||
|
||||
be_node *idnode = be_create_str_wlen((char *) id->data, BITDHT_KEY_LEN);
|
||||
|
||||
std::string srcEnc = encodeCompactNodeId(src);
|
||||
std::string destEnc = encodeCompactNodeId(dest);
|
||||
|
||||
be_node *srcnode = be_create_str_wlen(srcEnc.c_str(), BITDHT_COMPACTNODEID_LEN);
|
||||
be_node *destnode = be_create_str_wlen(destEnc.c_str(), BITDHT_COMPACTNODEID_LEN);
|
||||
be_node *typenode = be_create_int(msgtype);
|
||||
be_node *statusnode = be_create_int(status);
|
||||
|
||||
be_node *tidnode = be_create_str_wlen((char *) tid->data, tid->len);
|
||||
be_node *yqrnode = be_create_str("q");
|
||||
be_node *cmdnode = be_create_str("connect");
|
||||
|
||||
#define CONNECT_MODE_DIRECT 0x0001
|
||||
#define CONNECT_MODE_PROXY 0x0002
|
||||
#define CONNECT_MODE_RELAY 0x0004
|
||||
|
||||
be_node *modenode = NULL;
|
||||
switch(mode)
|
||||
{
|
||||
case CONNECT_MODE_DIRECT:
|
||||
modenode = be_create_str("d");
|
||||
break;
|
||||
case CONNECT_MODE_PROXY:
|
||||
modenode = be_create_str("p");
|
||||
break;
|
||||
case CONNECT_MODE_RELAY:
|
||||
modenode = be_create_str("r");
|
||||
break;
|
||||
default:
|
||||
modenode = be_create_str("u");
|
||||
break;
|
||||
}
|
||||
|
||||
be_add_keypair(iddict, "id", idnode);
|
||||
be_add_keypair(iddict, "src", srcnode);
|
||||
be_add_keypair(iddict, "dest", destnode);
|
||||
be_add_keypair(iddict, "mode", modenode);
|
||||
be_add_keypair(iddict, "status", statusnode);
|
||||
be_add_keypair(iddict, "type", typenode);
|
||||
|
||||
be_add_keypair(dict, "a", iddict);
|
||||
|
||||
be_add_keypair(dict, "t", tidnode);
|
||||
be_add_keypair(dict, "y", yqrnode);
|
||||
be_add_keypair(dict, "q", cmdnode);
|
||||
|
||||
#ifdef DEBUG_MSG_DUMP
|
||||
/* dump answer */
|
||||
be_dump(dict);
|
||||
#endif
|
||||
|
||||
int blen = be_encode(dict, msg, avail);
|
||||
be_free(dict);
|
||||
|
||||
return blen;
|
||||
}
|
||||
|
||||
|
||||
|
@ -45,6 +45,16 @@
|
||||
#define BITDHT_MSG_TYPE_POST_HASH 8
|
||||
#define BITDHT_MSG_TYPE_REPLY_POST 9
|
||||
|
||||
// THESE ARE EXTENSIONS
|
||||
// CONNECTIONS.
|
||||
#define BITDHT_MSG_TYPE_CONNECT_REQUEST 101
|
||||
#define BITDHT_MSG_TYPE_CONNECT_REPLY 102
|
||||
#define BITDHT_MSG_TYPE_CONNECT_START 103
|
||||
#define BITDHT_MSG_TYPE_CONNECT_ACK 104
|
||||
|
||||
// FANCY HASHES.
|
||||
|
||||
|
||||
#define BITDHT_COMPACTNODEID_LEN 26
|
||||
#define BITDHT_COMPACTPEERID_LEN 6
|
||||
|
||||
@ -78,6 +88,10 @@ int bitdht_reply_announce_msg(bdToken *tid, bdNodeId *id,
|
||||
char *msg, int avail);
|
||||
|
||||
|
||||
// Extensions.
|
||||
int bitdht_connect_genmsg(bdToken *tid, bdNodeId *id, int msgtype, bdId *src, bdId *dest, int mode, int status, char *msg, int avail);
|
||||
|
||||
|
||||
//int response_peers_message()
|
||||
//int response_closestnodes_message()
|
||||
|
||||
|
@ -80,7 +80,7 @@ void bdNode::restartNode()
|
||||
bdPeer peer;
|
||||
while(mStore.getPeer(&peer))
|
||||
{
|
||||
addPotentialPeer(&(peer.mPeerId));
|
||||
addPotentialPeer(&(peer.mPeerId), NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@ -317,21 +317,15 @@ void bdNode::iteration()
|
||||
/* go through the possible queries */
|
||||
if (query->nextQuery(id, targetNodeId))
|
||||
{
|
||||
/* push out query */
|
||||
bdToken transId;
|
||||
genNewTransId(&transId);
|
||||
//registerOutgoingMsg(&id, &transId, BITDHT_MSG_TYPE_FIND_NODE);
|
||||
|
||||
msgout_find_node(&id, &transId, &targetNodeId);
|
||||
|
||||
#ifdef DEBUG_NODE_MSGS
|
||||
std::cerr << "bdNode::iteration() Find Node Req for : ";
|
||||
std::cerr << "bdNode::iteration() send_query(";
|
||||
mFns->bdPrintId(std::cerr, &id);
|
||||
std::cerr << " searching for : ";
|
||||
std::cerr << ",";
|
||||
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
||||
std::cerr << ")";
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
mCounterQueryNode++;
|
||||
send_query(&id, &targetNodeId);
|
||||
sentMsgs++;
|
||||
sentQueries++;
|
||||
}
|
||||
@ -376,6 +370,26 @@ void bdNode::iteration()
|
||||
//printQueries();
|
||||
}
|
||||
|
||||
void bdNode::send_query(bdId *id, bdNodeId *targetNodeId)
|
||||
{
|
||||
/* push out query */
|
||||
bdToken transId;
|
||||
genNewTransId(&transId);
|
||||
//registerOutgoingMsg(&id, &transId, BITDHT_MSG_TYPE_FIND_NODE);
|
||||
|
||||
msgout_find_node(id, &transId, targetNodeId);
|
||||
|
||||
#ifdef DEBUG_NODE_MSGS
|
||||
std::cerr << "bdNode::send_query() Find Node Req for : ";
|
||||
mFns->bdPrintId(std::cerr, &id);
|
||||
std::cerr << " searching for : ";
|
||||
mFns->bdPrintNodeId(std::cerr, &targetNodeId);
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
mCounterQueryNode++;
|
||||
}
|
||||
|
||||
|
||||
#define LPF_FACTOR (0.90)
|
||||
|
||||
void bdNode::doStats()
|
||||
@ -408,12 +422,31 @@ void bdNode::doStats()
|
||||
mLpfRecvReplyQueryHash *= (LPF_FACTOR);
|
||||
mLpfRecvReplyQueryHash += (1.0 - LPF_FACTOR) * mCounterRecvReplyQueryHash;
|
||||
|
||||
// connection stats.
|
||||
mLpfConnectRequest *= (LPF_FACTOR);
|
||||
mLpfConnectRequest += (1.0 - LPF_FACTOR) * mCounterConnectRequest;
|
||||
mLpfConnectReply *= (LPF_FACTOR);
|
||||
mLpfConnectReply += (1.0 - LPF_FACTOR) * mCounterConnectReply;
|
||||
mLpfConnectStart *= (LPF_FACTOR);
|
||||
mLpfConnectStart += (1.0 - LPF_FACTOR) * mCounterConnectStart;
|
||||
mLpfConnectAck *= (LPF_FACTOR);
|
||||
mLpfConnectAck += (1.0 - LPF_FACTOR) * mCounterConnectAck;
|
||||
|
||||
mLpfRecvConnectRequest *= (LPF_FACTOR);
|
||||
mLpfRecvConnectRequest += (1.0 - LPF_FACTOR) * mCounterRecvConnectRequest;
|
||||
mLpfRecvConnectReply *= (LPF_FACTOR);
|
||||
mLpfRecvConnectReply += (1.0 - LPF_FACTOR) * mCounterRecvConnectReply;
|
||||
mLpfRecvConnectStart *= (LPF_FACTOR);
|
||||
mLpfRecvConnectStart += (1.0 - LPF_FACTOR) * mCounterRecvConnectStart;
|
||||
mLpfRecvConnectAck *= (LPF_FACTOR);
|
||||
mLpfRecvConnectAck += (1.0 - LPF_FACTOR) * mCounterRecvConnectAck;
|
||||
|
||||
resetCounters();
|
||||
}
|
||||
|
||||
void bdNode::printStats(std::ostream &out)
|
||||
{
|
||||
|
||||
out << "bdNode::printStats()" << std::endl;
|
||||
out << " Send Recv: ";
|
||||
out << std::endl;
|
||||
@ -438,6 +471,24 @@ void bdNode::printStats(std::ostream &out)
|
||||
out << " mLpfRecvQueryHash/sec : " << std::setw(10) << mLpfRecvQueryHash;
|
||||
out << std::endl;
|
||||
out << std::endl;
|
||||
|
||||
out << " mLpfConnectRequest/sec : " << std::setw(10) << mLpfConnectRequest;
|
||||
out << std::endl;
|
||||
out << " mLpfConnectReply/sec : " << std::setw(10) << mLpfConnectReply;
|
||||
out << std::endl;
|
||||
out << " mLpfConnectStart/sec : " << std::setw(10) << mLpfConnectStart;
|
||||
out << std::endl;
|
||||
out << " mLpfConnectAck/sec : " << std::setw(10) << mLpfConnectAck;
|
||||
out << std::endl;
|
||||
out << " mLpfRecvConnectReq/sec : " << std::setw(10) << mLpfRecvConnectRequest;
|
||||
out << std::endl;
|
||||
out << " mLpfRecvConnReply/sec : " << std::setw(10) << mLpfRecvConnectReply;
|
||||
out << std::endl;
|
||||
out << " mLpfRecvConnStart/sec : " << std::setw(10) << mLpfRecvConnectStart;
|
||||
out << std::endl;
|
||||
out << " mLpfRecvConnectAck/sec : " << std::setw(10) << mLpfRecvConnectAck;
|
||||
out << std::endl;
|
||||
out << std::endl;
|
||||
}
|
||||
|
||||
void bdNode::resetCounters()
|
||||
@ -456,6 +507,16 @@ void bdNode::resetCounters()
|
||||
mCounterRecvQueryHash = 0;
|
||||
mCounterRecvReplyFindNode = 0;
|
||||
mCounterRecvReplyQueryHash = 0;
|
||||
|
||||
mCounterConnectRequest = 0;
|
||||
mCounterConnectReply = 0;
|
||||
mCounterConnectStart = 0;
|
||||
mCounterConnectAck = 0;
|
||||
|
||||
mCounterRecvConnectRequest = 0;
|
||||
mCounterRecvConnectReply = 0;
|
||||
mCounterRecvConnectStart = 0;
|
||||
mCounterRecvConnectAck = 0;
|
||||
}
|
||||
|
||||
void bdNode::resetStats()
|
||||
@ -479,14 +540,14 @@ void bdNode::resetStats()
|
||||
}
|
||||
|
||||
|
||||
void bdNode::checkPotentialPeer(bdId *id)
|
||||
void bdNode::checkPotentialPeer(bdId *id, bdId *src)
|
||||
{
|
||||
bool isWorthyPeer = false;
|
||||
/* also push to queries */
|
||||
std::list<bdQuery *>::iterator it;
|
||||
for(it = mLocalQueries.begin(); it != mLocalQueries.end(); it++)
|
||||
{
|
||||
if ((*it)->addPotentialPeer(id, 0))
|
||||
if ((*it)->addPotentialPeer(id, src, 0))
|
||||
{
|
||||
isWorthyPeer = true;
|
||||
}
|
||||
@ -494,12 +555,12 @@ void bdNode::checkPotentialPeer(bdId *id)
|
||||
|
||||
if (isWorthyPeer)
|
||||
{
|
||||
addPotentialPeer(id);
|
||||
addPotentialPeer(id, src);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void bdNode::addPotentialPeer(bdId *id)
|
||||
void bdNode::addPotentialPeer(bdId *id, bdId *src)
|
||||
{
|
||||
mPotentialPeers.push_back(*id);
|
||||
}
|
||||
@ -571,21 +632,23 @@ void bdNode::addQuery(const bdNodeId *id, uint32_t qflags)
|
||||
{
|
||||
|
||||
std::list<bdId> startList;
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
|
||||
mNodeSpace.find_nearest_nodes(id, BITDHT_QUERY_START_PEERS, startList, nearest);
|
||||
//mNodeSpace.find_nearest_nodes(id, BITDHT_QUERY_START_PEERS, startList, nearest, 0);
|
||||
|
||||
mNodeSpace.find_nearest_nodes(id, BITDHT_QUERY_START_PEERS, nearest);
|
||||
|
||||
fprintf(stderr, "bdNode::addQuery(");
|
||||
mFns->bdPrintNodeId(std::cerr, id);
|
||||
fprintf(stderr, ")\n");
|
||||
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
startList.push_back(it->second);
|
||||
}
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
startList.push_back(it->second);
|
||||
}
|
||||
|
||||
bdQuery *query = new bdQuery(id, startList, qflags, mFns);
|
||||
bdQuery *query = new bdQuery(id, startList, qflags, mFns);
|
||||
mLocalQueries.push_back(query);
|
||||
}
|
||||
|
||||
@ -645,12 +708,15 @@ void bdNode::processRemoteQuery()
|
||||
case BD_QUERY_NEIGHBOURS:
|
||||
{
|
||||
/* search bdSpace for neighbours */
|
||||
std::list<bdId> excludeList;
|
||||
std::list<bdId> nearList;
|
||||
//std::list<bdId> excludeList;
|
||||
|
||||
std::list<bdId> nearList;
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
|
||||
mNodeSpace.find_nearest_nodes(&(query.mQuery), BITDHT_QUERY_NEIGHBOUR_PEERS, excludeList, nearest);
|
||||
//mNodeSpace.find_nearest_nodes(&(query.mQuery), BITDHT_QUERY_NEIGHBOUR_PEERS, excludeList, nearest, 0);
|
||||
|
||||
mNodeSpace.find_nearest_nodes(&(query.mQuery), BITDHT_QUERY_NEIGHBOUR_PEERS, nearest);
|
||||
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
@ -1588,7 +1654,7 @@ void bdNode::msgin_reply_find_node(bdId *id, bdToken *transId, std::list<bdId> &
|
||||
/* add neighbours to the potential list */
|
||||
for(it = nodes.begin(); it != nodes.end(); it++)
|
||||
{
|
||||
checkPotentialPeer(&(*it));
|
||||
checkPotentialPeer(&(*it), id);
|
||||
}
|
||||
|
||||
/* received reply - so peer must be good */
|
||||
@ -1874,3 +1940,6 @@ bdNodeNetMsg::~bdNodeNetMsg()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -34,6 +34,8 @@
|
||||
#include "bitdht/bdhash.h"
|
||||
#include "bitdht/bdhistory.h"
|
||||
|
||||
#include "bitdht/bdconnection.h"
|
||||
|
||||
|
||||
#define BD_QUERY_NEIGHBOURS 1
|
||||
#define BD_QUERY_HASH 2
|
||||
@ -106,13 +108,17 @@ class bdNode
|
||||
virtual void addPeer(const bdId *id, uint32_t peerflags);
|
||||
|
||||
void printState();
|
||||
void checkPotentialPeer(bdId *id);
|
||||
void addPotentialPeer(bdId *id);
|
||||
void checkPotentialPeer(bdId *id, bdId *src);
|
||||
void addPotentialPeer(bdId *id, bdId *src);
|
||||
|
||||
void addQuery(const bdNodeId *id, uint32_t qflags);
|
||||
void clearQuery(const bdNodeId *id);
|
||||
void QueryStatus(std::map<bdNodeId, bdQueryStatus> &statusMap);
|
||||
|
||||
/* connection functions */
|
||||
void requestConnection(bdNodeId *id, uint32_t modes);
|
||||
void allowConnection(bdNodeId *id, uint32_t modes);
|
||||
|
||||
void iterationOff();
|
||||
void iteration();
|
||||
void processRemoteQuery();
|
||||
@ -127,6 +133,8 @@ void incomingMsg(struct sockaddr_in *addr, char *msg, int len);
|
||||
void sendPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
|
||||
/* internal assistance functions */
|
||||
void send_query(bdId *id, bdNodeId *targetNodeId); /* message out */
|
||||
|
||||
/* output functions (send msg) */
|
||||
void msgout_ping(bdId *id, bdToken *transId);
|
||||
@ -184,6 +192,65 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
void resetCounters();
|
||||
void resetStats();
|
||||
|
||||
|
||||
/****************************** Connection Code (in bdconnection.cc) ****************************/
|
||||
|
||||
/* Connections: Messages */
|
||||
|
||||
void msgout_connect_genmsg(bdId *id, bdToken *transId, int msgtype,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status);
|
||||
void msgin_connect_genmsg(bdId *id, bdToken *transId, int type,
|
||||
bdId *srcAddr, bdId *destAddr, int mode, int status);
|
||||
|
||||
/* Connections: Initiation */
|
||||
|
||||
int requestConnection(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
int requestConnection_direct(struct sockaddr_in *laddr, bdNodeId *target);
|
||||
int requestConnection_proxy(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
|
||||
int checkExistingConnectionAttempt(bdNodeId *target);
|
||||
void addPotentialConnectionProxy(bdId *srcId, bdId *target);
|
||||
void iterateConnectionRequests();
|
||||
|
||||
/* Connections: Outgoing */
|
||||
|
||||
int startConnectionAttempt(bdId *proxyId, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
void AuthConnectionOk(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
void AuthConnectionNo(bdId *srcId, bdId *proxyId, bdId *destId, int mode, int loc);
|
||||
void iterateConnections();
|
||||
|
||||
|
||||
/* Connections: Utility State */
|
||||
|
||||
bdConnection *findExistingConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
bdConnection *newConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
int cleanConnection(bdNodeId *srcId, bdNodeId *proxyId, bdNodeId *destId);
|
||||
|
||||
int determinePosition(bdNodeId *sender, bdNodeId *src, bdNodeId *dest);
|
||||
int determineProxyId(bdNodeId *sender, bdNodeId *src, bdNodeId *dest, bdNodeId *proxyId);
|
||||
|
||||
bdConnection *findExistingConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
bdConnection *newConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
int cleanConnectionBySender(bdId *sender, bdId *src, bdId *dest);
|
||||
|
||||
// Overloaded Generalised Connection Callback.
|
||||
virtual void callbackConnect(bdId *srcId, bdId *proxyId, bdId *destId,
|
||||
int mode, int point, int cbtype);
|
||||
|
||||
/* Connections: */
|
||||
int recvedConnectionRequest(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
int recvedConnectionReply(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int status);
|
||||
int recvedConnectionStart(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode, int bandwidth);
|
||||
int recvedConnectionAck(bdId *id, bdId *srcConnAddr, bdId *destConnAddr, int mode);
|
||||
|
||||
private:
|
||||
|
||||
std::map<bdProxyTuple, bdConnection> mConnections;
|
||||
std::map<bdNodeId, bdConnectionRequest> mConnectionRequests;
|
||||
|
||||
|
||||
/****************************** Connection Code (in bdconnection.cc) ****************************/
|
||||
|
||||
protected:
|
||||
|
||||
|
||||
@ -218,6 +285,11 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
double mCounterQueryHash;
|
||||
double mCounterReplyFindNode;
|
||||
double mCounterReplyQueryHash;
|
||||
// connection stats.
|
||||
double mCounterConnectRequest;
|
||||
double mCounterConnectReply;
|
||||
double mCounterConnectStart;
|
||||
double mCounterConnectAck;
|
||||
|
||||
double mCounterRecvPing;
|
||||
double mCounterRecvPong;
|
||||
@ -225,6 +297,11 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
double mCounterRecvQueryHash;
|
||||
double mCounterRecvReplyFindNode;
|
||||
double mCounterRecvReplyQueryHash;
|
||||
// connection stats.
|
||||
double mCounterRecvConnectRequest;
|
||||
double mCounterRecvConnectReply;
|
||||
double mCounterRecvConnectStart;
|
||||
double mCounterRecvConnectAck;
|
||||
|
||||
double mLpfOutOfDatePing;
|
||||
double mLpfPings;
|
||||
@ -233,6 +310,11 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
double mLpfQueryHash;
|
||||
double mLpfReplyFindNode;
|
||||
double mLpfReplyQueryHash;
|
||||
// connection stats.
|
||||
double mLpfConnectRequest;
|
||||
double mLpfConnectReply;
|
||||
double mLpfConnectStart;
|
||||
double mLpfConnectAck;
|
||||
|
||||
double mLpfRecvPing;
|
||||
double mLpfRecvPong;
|
||||
@ -240,6 +322,12 @@ void recvPkt(char *msg, int len, struct sockaddr_in addr);
|
||||
double mLpfRecvQueryHash;
|
||||
double mLpfRecvReplyFindNode;
|
||||
double mLpfRecvReplyQueryHash;
|
||||
// connection stats.
|
||||
double mLpfRecvConnectRequest;
|
||||
double mLpfRecvConnectReply;
|
||||
double mLpfRecvConnectStart;
|
||||
double mLpfRecvConnectAck;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
@ -337,7 +337,10 @@ int bdSpace::clear()
|
||||
}
|
||||
|
||||
|
||||
int bdSpace::find_nearest_nodes(const bdNodeId *id, int number, std::list<bdId> /*excluding*/, std::multimap<bdMetric, bdId> &nearest)
|
||||
|
||||
int bdSpace::find_nearest_nodes_with_flags(const bdNodeId *id, int number,
|
||||
std::list<bdId> /* excluding */,
|
||||
std::multimap<bdMetric, bdId> &nearest, uint32_t with_flag)
|
||||
{
|
||||
std::multimap<bdMetric, bdId> closest;
|
||||
std::multimap<bdMetric, bdId>::iterator mit;
|
||||
@ -363,16 +366,19 @@ int bdSpace::find_nearest_nodes(const bdNodeId *id, int number, std::list<bdId>
|
||||
{
|
||||
for(eit = it->entries.begin(); eit != it->entries.end(); eit++)
|
||||
{
|
||||
mFns->bdDistance(id, &(eit->mPeerId.id), &dist);
|
||||
closest.insert(std::pair<bdMetric, bdId>(dist, eit->mPeerId));
|
||||
if ((!with_flag) || (with_flag & eit->mPeerFlags))
|
||||
{
|
||||
mFns->bdDistance(id, &(eit->mPeerId.id), &dist);
|
||||
closest.insert(std::pair<bdMetric, bdId>(dist, eit->mPeerId));
|
||||
|
||||
#if 0
|
||||
std::cerr << "Added NodeId: ";
|
||||
bdPrintNodeId(std::cerr, &(eit->mPeerId.id));
|
||||
std::cerr << " Metric: ";
|
||||
bdPrintNodeId(std::cerr, &(dist));
|
||||
std::cerr << std::endl;
|
||||
std::cerr << "Added NodeId: ";
|
||||
bdPrintNodeId(std::cerr, &(eit->mPeerId.id));
|
||||
std::cerr << " Metric: ";
|
||||
bdPrintNodeId(std::cerr, &(dist));
|
||||
std::cerr << std::endl;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -429,6 +435,15 @@ int bdSpace::find_nearest_nodes(const bdNodeId *id, int number, std::list<bdId>
|
||||
return 1;
|
||||
}
|
||||
|
||||
int bdSpace::find_nearest_nodes(const bdNodeId *id, int number,
|
||||
std::multimap<bdMetric, bdId> &nearest)
|
||||
{
|
||||
std::list<bdId> excluding;
|
||||
uint32_t with_flag = 0;
|
||||
|
||||
return find_nearest_nodes_with_flags(id, number, excluding, nearest, with_flag);
|
||||
}
|
||||
|
||||
|
||||
int bdSpace::out_of_date_peer(bdId &id)
|
||||
{
|
||||
@ -591,6 +606,7 @@ int bdSpace::add_peer(const bdId *id, uint32_t peerflags)
|
||||
newPeer.mPeerId = *id;
|
||||
newPeer.mLastRecvTime = ts;
|
||||
newPeer.mLastSendTime = ts; //????
|
||||
newPeer.mFoundTime = ts;
|
||||
newPeer.mPeerFlags = peerflags;
|
||||
|
||||
buck.entries.push_back(newPeer);
|
||||
@ -764,6 +780,16 @@ int bdSpace::printDHT()
|
||||
}
|
||||
|
||||
|
||||
int bdSpace::getDhtBucket(const int idx, bdBucket &bucket)
|
||||
{
|
||||
if ((idx < 0) || (idx > (int) buckets.size() - 1 ))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
bucket = buckets[idx];
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t bdSpace::calcNetworkSize()
|
||||
{
|
||||
std::vector<bdBucket>::iterator it;
|
||||
|
@ -116,6 +116,10 @@ int operator==(const bdId &a, const bdId &b);
|
||||
|
||||
//std::string bdConvertToPrintable(std::string input);
|
||||
|
||||
/****
|
||||
* DEFINED in bdiface.h
|
||||
*
|
||||
|
||||
class bdPeer
|
||||
{
|
||||
public:
|
||||
@ -124,21 +128,22 @@ class bdPeer
|
||||
uint32_t mPeerFlags;
|
||||
time_t mLastSendTime;
|
||||
time_t mLastRecvTime;
|
||||
time_t mFoundTime; /* time stamp that peer was found */
|
||||
time_t mFoundTime; // time stamp that peer was found
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
class bdBucket
|
||||
{
|
||||
public:
|
||||
|
||||
bdBucket();
|
||||
|
||||
/* list so we can queue properly */
|
||||
// list so we can queue properly
|
||||
std::list<bdPeer> entries;
|
||||
};
|
||||
*
|
||||
*
|
||||
*****/
|
||||
|
||||
class bdSpace
|
||||
{
|
||||
@ -150,11 +155,16 @@ int clear();
|
||||
|
||||
/* accessors */
|
||||
int find_nearest_nodes(const bdNodeId *id, int number,
|
||||
std::list<bdId> excluding, std::multimap<bdMetric, bdId> &nearest);
|
||||
std::multimap<bdMetric, bdId> &nearest);
|
||||
|
||||
int find_nearest_nodes_with_flags(const bdNodeId *id, int number,
|
||||
std::list<bdId> excluding,
|
||||
std::multimap<bdMetric, bdId> &nearest, uint32_t with_flag);
|
||||
|
||||
int out_of_date_peer(bdId &id); // side-effect updates, send flag on peer.
|
||||
int add_peer(const bdId *id, uint32_t mode);
|
||||
int printDHT();
|
||||
int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
uint32_t calcNetworkSize();
|
||||
uint32_t calcNetworkSizeWithFlag(uint32_t withFlag);
|
||||
|
@ -104,6 +104,17 @@ bool bdQuery::result(std::list<bdId> &answer)
|
||||
return (i > 0);
|
||||
}
|
||||
|
||||
bool bdQuery::proxies(std::list<bdId> &answer)
|
||||
{
|
||||
/* get all the matches to our query */
|
||||
std::list<bdPeer>::iterator it;
|
||||
int i = 0;
|
||||
for(it = mPotentialProxies.begin(); it != mPotentialProxies.end(); it++, i++)
|
||||
{
|
||||
answer.push_back(it->mPeerId);
|
||||
}
|
||||
return (i > 0);
|
||||
}
|
||||
|
||||
int bdQuery::nextQuery(bdId &id, bdNodeId &targetNodeId)
|
||||
{
|
||||
@ -399,7 +410,7 @@ int bdQuery::addPeer(const bdId *id, uint32_t mode)
|
||||
* simple list of closest.
|
||||
*/
|
||||
|
||||
int bdQuery::addPotentialPeer(const bdId *id, uint32_t mode)
|
||||
int bdQuery::addPotentialPeer(const bdId *id, const bdId *src, uint32_t mode)
|
||||
{
|
||||
bdMetric dist;
|
||||
time_t ts = time(NULL);
|
||||
@ -541,6 +552,44 @@ int bdQuery::addPotentialPeer(const bdId *id, uint32_t mode)
|
||||
#ifdef DEBUG_QUERY
|
||||
fprintf(stderr, "Flagging as Potential Peer!\n");
|
||||
#endif
|
||||
|
||||
/* finally if it is an exact match, add as potential proxy */
|
||||
int bucket = mFns->bdBucketDistance(&dist);
|
||||
if ((bucket == 0) && (src != NULL))
|
||||
{
|
||||
|
||||
#ifdef DEBUG_QUERY
|
||||
fprintf(stderr, "Bucket = 0, Have Potential Proxy!\n");
|
||||
#endif
|
||||
std::list<bdPeer>::iterator it;
|
||||
for(it = mPotentialProxies.begin(); it != mPotentialProxies.end(); it++)
|
||||
{
|
||||
if (*src == it->mPeerId)
|
||||
{
|
||||
/* found it ;( */
|
||||
#ifdef DEBUG_QUERY
|
||||
fprintf(stderr, "Source Already in Potential Proxy List, updating timestamp!\n");
|
||||
#endif
|
||||
it->mLastRecvTime = ts;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (it == mPotentialProxies.end())
|
||||
{
|
||||
#ifdef DEBUG_QUERY
|
||||
fprintf(stderr, "Adding Source to Potential Proxy List:\n");
|
||||
#endif
|
||||
/* add it in */
|
||||
bdPeer peer;
|
||||
peer.mPeerId = *src;
|
||||
peer.mLastSendTime = 0;
|
||||
peer.mLastRecvTime = ts;
|
||||
peer.mFoundTime = ts;
|
||||
|
||||
mPotentialProxies.push_back(peer);
|
||||
}
|
||||
}
|
||||
|
||||
retval = 1;
|
||||
return retval;
|
||||
}
|
||||
@ -590,6 +639,18 @@ int bdQuery::printQuery()
|
||||
fprintf(stderr," LastRecv: %ld ago", ts-it->second.mLastRecvTime);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
std::list<bdPeer>::iterator lit;
|
||||
fprintf(stderr, "\nPotential Proxies:\n");
|
||||
for(lit = mPotentialProxies.begin(); lit != mPotentialProxies.end(); lit++)
|
||||
{
|
||||
fprintf(stderr, "ProxyId: ");
|
||||
mFns->bdPrintId(std::cerr, &(lit->mPeerId));
|
||||
fprintf(stderr," Found: %ld ago", ts-lit->mFoundTime);
|
||||
fprintf(stderr," LastSent: %ld ago", ts-lit->mLastSendTime);
|
||||
fprintf(stderr," LastRecv: %ld ago", ts-lit->mLastRecvTime);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#else
|
||||
// shortened version.
|
||||
fprintf(stderr, "Closest Available Peer: ");
|
||||
@ -615,6 +676,18 @@ int bdQuery::printQuery()
|
||||
fprintf(stderr," LastRecv: %ld ago", ts-it->second.mLastRecvTime);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
std::list<bdPeer>::iterator lit;
|
||||
fprintf(stderr, "Potential Proxies:\n");
|
||||
for(lit = mPotentialProxies.begin(); lit != mPotentialProxies.end(); lit++)
|
||||
{
|
||||
fprintf(stderr, "ProxyId: ");
|
||||
mFns->bdPrintId(std::cerr, &(lit->mPeerId));
|
||||
fprintf(stderr," Found: %ld ago", ts-lit->mFoundTime);
|
||||
fprintf(stderr," LastSent: %ld ago", ts-lit->mLastSendTime);
|
||||
fprintf(stderr," LastRecv: %ld ago", ts-lit->mLastRecvTime);
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
|
@ -44,12 +44,13 @@ class bdQuery
|
||||
|
||||
// get the answer.
|
||||
bool result(std::list<bdId> &answer);
|
||||
bool proxies(std::list<bdId> &answer);
|
||||
|
||||
// returning results get passed to all queries.
|
||||
//void addNode(const bdId *id, int mode);
|
||||
int nextQuery(bdId &id, bdNodeId &targetId);
|
||||
int addPeer(const bdId *id, uint32_t mode);
|
||||
int addPotentialPeer(const bdId *id, uint32_t mode);
|
||||
int addPotentialPeer(const bdId *id, const bdId *src, uint32_t mode);
|
||||
int printQuery();
|
||||
|
||||
// searching for
|
||||
@ -67,6 +68,7 @@ int printQuery();
|
||||
// closest peers
|
||||
std::multimap<bdMetric, bdPeer> mClosest;
|
||||
std::multimap<bdMetric, bdPeer> mPotentialClosest;
|
||||
std::list<bdPeer> mPotentialProxies;
|
||||
|
||||
bdDhtFunctions *mFns;
|
||||
};
|
||||
|
@ -116,6 +116,43 @@ 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;
|
||||
if (input.size() < reqlen)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < BITDHT_KEY_LEN; i++)
|
||||
{
|
||||
char ch1 = input[2 * i];
|
||||
char ch2 = input[2 * i + 1];
|
||||
uint8_t value1 = 0;
|
||||
uint8_t value2 = 0;
|
||||
|
||||
/* do char1 */
|
||||
if (ch1 >= '0' && ch1 <= '9')
|
||||
value1 = (ch1 - '0');
|
||||
else if (ch1 >= 'A' && ch1 <= 'F')
|
||||
value1 = (ch1 - 'A' + 10);
|
||||
else if (ch1 >= 'a' && ch1 <= 'f')
|
||||
value1 = (ch1 - 'a' + 10);
|
||||
|
||||
/* do char2 */
|
||||
if (ch2 >= '0' && ch2 <= '9')
|
||||
value2 = (ch2 - '0');
|
||||
else if (ch2 >= 'A' && ch2 <= 'F')
|
||||
value2 = (ch2 - 'A' + 10);
|
||||
else if (ch2 >= 'a' && ch2 <= 'f')
|
||||
value2 = (ch2 - 'a' + 10);
|
||||
|
||||
a_data[i] = (value1 << 4) + value2;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string bdStdConvertToPrintable(std::string input)
|
||||
{
|
||||
std::ostringstream out;
|
||||
|
@ -52,6 +52,8 @@ int bdStdBucketDistance(const bdNodeId *a, const bdNodeId *b);
|
||||
|
||||
void bdStdRandomMidId(const bdNodeId *target, const bdNodeId *other, bdNodeId *mid);
|
||||
|
||||
int bdStdLoadNodeId(bdNodeId *id, std::string input);
|
||||
|
||||
void bdStdPrintId(std::ostream &out, const bdId *a);
|
||||
void bdStdPrintNodeId(std::ostream &out, const bdNodeId *a);
|
||||
|
||||
|
@ -103,6 +103,7 @@ HEADERS += \
|
||||
udp/udplayer.h \
|
||||
udp/udpstack.h \
|
||||
udp/udpbitdht.h \
|
||||
bitdht/bdconnection.h \
|
||||
|
||||
SOURCES += \
|
||||
bitdht/bencode.c \
|
||||
@ -121,5 +122,6 @@ SOURCES += \
|
||||
udp/udplayer.cc \
|
||||
udp/udpstack.cc \
|
||||
udp/udpbitdht.cc \
|
||||
bitdht/bdconnection.cc \
|
||||
|
||||
|
||||
|
@ -93,7 +93,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
if (it != nodes.end())
|
||||
{
|
||||
nit->second->addPotentialPeer((bdId *) &(it->first));
|
||||
nit->second->addPotentialPeer((bdId *) &(it->first), NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ int main(int argc, char **argv)
|
||||
int peeridx = rand() % n_nodes;
|
||||
|
||||
bdId pid = portIdx[peeridx];
|
||||
node->addPotentialPeer(&pid);
|
||||
node->addPotentialPeer(&pid, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,7 @@ int main(int argc, char **argv)
|
||||
std::multimap<bdMetric, bdId> nearest;
|
||||
std::multimap<bdMetric, bdId>::iterator it;
|
||||
|
||||
space.find_nearest_nodes(&(queryId.id), N_PEERS_TO_START, startList, nearest);
|
||||
space.find_nearest_nodes(&(queryId.id), N_PEERS_TO_START, nearest);
|
||||
|
||||
for(it = nearest.begin(); it != nearest.end(); it++)
|
||||
{
|
||||
|
@ -56,10 +56,9 @@ int main(int argc, char **argv)
|
||||
{
|
||||
bdId tmpId;
|
||||
bdStdRandomId(&tmpId);
|
||||
std::list<bdId> list1;
|
||||
std::multimap<bdMetric, bdId> list2;
|
||||
|
||||
space.find_nearest_nodes(&(tmpId.id), N_PEERS_TO_FIND, list1, list2);
|
||||
space.find_nearest_nodes(&(tmpId.id), N_PEERS_TO_FIND, list2);
|
||||
}
|
||||
|
||||
return 1;
|
||||
|
@ -118,6 +118,24 @@ void UdpBitDht::removeCallback(BitDhtCallback *cb)
|
||||
mBitDhtManager->removeCallback(cb);
|
||||
}
|
||||
|
||||
void UdpBitDht::ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
mBitDhtManager->ConnectionRequest(laddr, target, mode);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void UdpBitDht::ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
mBitDhtManager->ConnectionAuth(srcId, proxyId, destId, mode, loc, answer);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int UdpBitDht::getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
@ -132,6 +150,13 @@ int UdpBitDht::getDhtValue(const bdNodeId *id, std::string key, std::string &va
|
||||
return mBitDhtManager->getDhtValue(id, key, value);
|
||||
}
|
||||
|
||||
int UdpBitDht::getDhtBucket(const int idx, bdBucket &bucket)
|
||||
{
|
||||
bdStackMutex stack(dhtMtx); /********** MUTEX LOCKED *************/
|
||||
|
||||
return mBitDhtManager->getDhtBucket(idx, bucket);
|
||||
}
|
||||
|
||||
|
||||
/* stats and Dht state */
|
||||
int UdpBitDht:: startDht()
|
||||
|
@ -67,9 +67,14 @@ virtual void findDhtValue(bdNodeId *id, std::string key, uint32_t mode);
|
||||
virtual void addCallback(BitDhtCallback *cb);
|
||||
virtual void removeCallback(BitDhtCallback *cb);
|
||||
|
||||
/***** Connections Requests *****/
|
||||
virtual void ConnectionRequest(struct sockaddr_in *laddr, bdNodeId *target, uint32_t mode);
|
||||
virtual void ConnectionAuth(bdId *srcId, bdId *proxyId, bdId *destId, uint32_t mode, uint32_t loc, uint32_t answer);
|
||||
|
||||
/***** Get Results Details *****/
|
||||
virtual int getDhtPeerAddress(const bdNodeId *id, struct sockaddr_in &from);
|
||||
virtual int getDhtValue(const bdNodeId *id, std::string key, std::string &value);
|
||||
virtual int getDhtBucket(const int idx, bdBucket &bucket);
|
||||
|
||||
/* stats and Dht state */
|
||||
virtual int startDht();
|
||||
|
Loading…
x
Reference in New Issue
Block a user