2010-07-17 13:00:19 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/dht: p3bitdht.h
|
|
|
|
*
|
|
|
|
* BitDht interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2009-2010 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 2 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 "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "dht/p3bitdht.h"
|
|
|
|
|
|
|
|
#include "bitdht/bdstddht.h"
|
2011-07-09 14:39:34 -04:00
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
#include "tcponudp/udprelay.h"
|
|
|
|
#include "tcponudp/udpstunner.h"
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* This is a conversion callback class between pqi interface
|
|
|
|
* and the BitDht Interface.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
/**** EXTERNAL INTERFACE DHT POINTER *****/
|
|
|
|
RsDht *rsDht = NULL;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
class p3BdCallback: public BitDhtCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
p3BdCallback(p3BitDht *parent)
|
|
|
|
:mParent(parent) { return; }
|
|
|
|
|
|
|
|
virtual int dhtNodeCallback(const bdId *id, uint32_t peerflags)
|
|
|
|
{
|
|
|
|
return mParent->NodeCallback(id, peerflags);
|
|
|
|
}
|
|
|
|
|
2010-11-22 19:06:54 -05:00
|
|
|
virtual int dhtPeerCallback(const bdId *id, uint32_t status)
|
2010-07-17 13:00:19 -04:00
|
|
|
{
|
|
|
|
return mParent->PeerCallback(id, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int dhtValueCallback(const bdNodeId *id, std::string key, uint32_t status)
|
|
|
|
{
|
|
|
|
return mParent->ValueCallback(id, key, status);
|
|
|
|
}
|
|
|
|
|
2011-06-29 06:46:11 -04:00
|
|
|
virtual int dhtConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId *destId,
|
2011-07-17 19:41:30 -04:00
|
|
|
uint32_t mode, uint32_t point, uint32_t param, uint32_t cbtype, uint32_t errcode)
|
2011-06-29 06:46:11 -04:00
|
|
|
{
|
2011-07-17 19:41:30 -04:00
|
|
|
return mParent->ConnectCallback(srcId, proxyId, destId, mode, point, param, cbtype, errcode);
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
|
2011-11-24 19:58:01 -05:00
|
|
|
virtual int dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std::string info)
|
|
|
|
{
|
|
|
|
return mParent->InfoCallback(id, type, flags, info);
|
|
|
|
}
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
p3BitDht *mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-03-17 16:56:06 -04:00
|
|
|
p3BitDht::p3BitDht(const RsPeerId& id, pqiConnectCb *cb, p3NetMgr *nm,
|
2011-07-09 20:39:15 -04:00
|
|
|
UdpStack *udpstack, std::string bootstrapfile)
|
2011-12-19 21:07:00 -05:00
|
|
|
:p3Config(CONFIG_TYPE_BITDHT), pqiNetAssistConnect(id, cb), mNetMgr(nm), dhtMtx("p3BitDht")
|
2010-07-17 13:00:19 -04:00
|
|
|
{
|
2011-07-06 09:04:50 -04:00
|
|
|
mDhtStunner = NULL;
|
|
|
|
mProxyStunner = NULL;
|
|
|
|
mRelay = NULL;
|
|
|
|
|
2011-11-24 19:58:01 -05:00
|
|
|
mPeerSharer = NULL;
|
|
|
|
|
2011-12-13 11:19:37 -05:00
|
|
|
mRelayHandler = NULL;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
std::string dhtVersion = "RS51"; // should come from elsewhere!
|
2011-07-13 05:12:31 -04:00
|
|
|
mOwnRsId = id;
|
2010-07-17 13:00:19 -04:00
|
|
|
|
2012-01-10 18:28:33 -05:00
|
|
|
mMinuteTS = 0;
|
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::p3BitDht()" << std::endl;
|
|
|
|
std::cerr << "Using Id: " << id;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Using Bootstrap File: " << bootstrapfile;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Converting OwnId to bdNodeId....";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-10-01 15:48:53 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* setup ownId */
|
2011-07-09 20:39:15 -04:00
|
|
|
storeTranslation_locked(id);
|
2011-07-13 05:12:31 -04:00
|
|
|
lookupNodeId_locked(id, &mOwnDhtId);
|
2010-07-17 13:00:19 -04:00
|
|
|
|
2010-10-01 15:48:53 -04:00
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "Own NodeId: ";
|
2011-07-13 05:12:31 -04:00
|
|
|
bdStdPrintNodeId(std::cerr, &mOwnDhtId);
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-10-01 15:48:53 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* standard dht behaviour */
|
2012-01-13 20:54:17 -05:00
|
|
|
//bdDhtFunctions *stdfns = new bdStdDht();
|
|
|
|
mDhtFns = new bdModDht();
|
2010-07-17 13:00:19 -04:00
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht() startup ... creating UdpBitDht";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-31 14:14:10 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* create dht */
|
2012-01-13 20:54:17 -05:00
|
|
|
mUdpBitDht = new UdpBitDht(udpstack, &mOwnDhtId, dhtVersion, bootstrapfile, mDhtFns);
|
2010-07-31 14:14:10 -04:00
|
|
|
udpstack->addReceiver(mUdpBitDht);
|
2010-07-17 13:00:19 -04:00
|
|
|
|
|
|
|
/* setup callback to here */
|
|
|
|
p3BdCallback *bdcb = new p3BdCallback(this);
|
|
|
|
mUdpBitDht->addCallback(bdcb);
|
|
|
|
|
2011-12-19 21:07:00 -05:00
|
|
|
#if 0
|
2011-07-15 12:05:37 -04:00
|
|
|
/* enable all modes */
|
2011-07-28 09:05:20 -04:00
|
|
|
/* Switched to only Proxy Mode - as Direct Connections can be unreliable - as they share the UDP with the DHT....
|
|
|
|
* We'll get these working properly and then if necessary get Direct further tested.
|
|
|
|
*/
|
2011-07-15 12:05:37 -04:00
|
|
|
mUdpBitDht->ConnectionOptions(
|
2011-07-18 04:37:11 -04:00
|
|
|
// BITDHT_CONNECT_MODE_DIRECT | BITDHT_CONNECT_MODE_PROXY | BITDHT_CONNECT_MODE_RELAY,
|
2011-07-28 09:05:20 -04:00
|
|
|
//BITDHT_CONNECT_MODE_DIRECT | BITDHT_CONNECT_MODE_PROXY,
|
|
|
|
BITDHT_CONNECT_MODE_PROXY,
|
2011-07-15 12:05:37 -04:00
|
|
|
BITDHT_CONNECT_OPTION_AUTOPROXY);
|
|
|
|
|
2011-12-19 21:07:00 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
setupRelayDefaults();
|
2012-01-19 11:23:57 -05:00
|
|
|
|
|
|
|
clearDataRates();
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p3BitDht::~p3BitDht()
|
|
|
|
{
|
2010-07-31 14:14:10 -04:00
|
|
|
//udpstack->removeReceiver(mUdpBitDht);
|
2010-07-17 13:00:19 -04:00
|
|
|
delete mUdpBitDht;
|
|
|
|
}
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2012-12-17 14:15:04 -05:00
|
|
|
bool p3BitDht::getOwnDhtId(std::string &ownDhtId)
|
|
|
|
{
|
|
|
|
bdStdPrintNodeId(ownDhtId, &(mOwnDhtId), false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-06 09:04:50 -04:00
|
|
|
void p3BitDht::setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay)
|
|
|
|
{
|
|
|
|
mDhtStunner = dhtStunner;
|
|
|
|
mProxyStunner = proxyStunner;
|
|
|
|
mRelay = relay;
|
|
|
|
}
|
|
|
|
|
2011-11-24 19:58:01 -05:00
|
|
|
void p3BitDht::setupPeerSharer(pqiNetAssistPeerShare *sharer)
|
|
|
|
{
|
|
|
|
mPeerSharer = sharer;
|
|
|
|
}
|
2011-07-06 09:04:50 -04:00
|
|
|
|
2012-01-13 20:54:17 -05:00
|
|
|
/* Tweak the DHT Parameters */
|
|
|
|
void p3BitDht::modifyNodesPerBucket(uint16_t count)
|
|
|
|
{
|
|
|
|
bdModDht *modFns = (bdModDht *) mDhtFns;
|
|
|
|
modFns->setNodesPerBucket(count);
|
|
|
|
}
|
2011-12-13 11:19:37 -05:00
|
|
|
|
|
|
|
/* Support for Outsourced Relay Handling */
|
|
|
|
|
|
|
|
void p3BitDht::installRelayHandler(p3BitDhtRelayHandler *handler)
|
|
|
|
{
|
|
|
|
/* The Handler is mutex protected, as its installation can occur when the dht is already running */
|
|
|
|
RsStackMutex stack(dhtMtx); /********* LOCKED *********/
|
|
|
|
|
|
|
|
mRelayHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
UdpRelayReceiver *p3BitDht::getRelayReceiver()
|
|
|
|
{
|
|
|
|
return mRelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-07-31 14:14:10 -04:00
|
|
|
void p3BitDht::start()
|
|
|
|
{
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht::start()";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-31 14:14:10 -04:00
|
|
|
|
|
|
|
mUdpBitDht->start(); /* starts up the bitdht thread */
|
2010-10-17 16:56:19 -04:00
|
|
|
|
2010-10-31 09:53:28 -04:00
|
|
|
/* dht switched on by config later. */
|
2010-07-31 14:14:10 -04:00
|
|
|
}
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* pqiNetAssist - external interface functions */
|
|
|
|
void p3BitDht::enable(bool on)
|
|
|
|
{
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-10-31 09:53:28 -04:00
|
|
|
std::cerr << "p3BitDht::enable(" << on << ")";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-10-31 09:53:28 -04:00
|
|
|
|
|
|
|
if (on)
|
|
|
|
{
|
|
|
|
mUdpBitDht->startDht();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mUdpBitDht->stopDht();
|
|
|
|
}
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void p3BitDht::shutdown() /* blocking call */
|
|
|
|
{
|
2010-10-31 09:53:28 -04:00
|
|
|
mUdpBitDht->stopDht();
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void p3BitDht::restart()
|
|
|
|
{
|
2010-10-31 09:53:28 -04:00
|
|
|
mUdpBitDht->stopDht();
|
|
|
|
mUdpBitDht->startDht();
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::getEnabled()
|
|
|
|
{
|
2010-10-31 09:53:28 -04:00
|
|
|
return (mUdpBitDht->stateDht() != 0);
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::getActive()
|
|
|
|
{
|
2010-10-31 09:53:28 -04:00
|
|
|
return (mUdpBitDht->stateDht() >= BITDHT_MGR_STATE_ACTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::getNetworkStats(uint32_t &netsize, uint32_t &localnetsize)
|
|
|
|
{
|
|
|
|
netsize = mUdpBitDht->statsNetworkSize();
|
|
|
|
localnetsize = mUdpBitDht->statsBDVersionSize();
|
|
|
|
return true;
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
#if 0
|
2010-07-17 13:00:19 -04:00
|
|
|
/* pqiNetAssistConnect - external interface functions */
|
|
|
|
/* add / remove peers */
|
|
|
|
bool p3BitDht::findPeer(std::string pid)
|
|
|
|
{
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht::findPeer(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-31 14:14:10 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* convert id -> NodeId */
|
|
|
|
if (!storeTranslation(pid))
|
|
|
|
{
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::findPeer() Failed to storeTranslation";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bdNodeId nid;
|
|
|
|
if (!lookupNodeId(pid, &nid))
|
|
|
|
{
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::findPeer() Failed to lookupNodeId";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::findPeer() calling AddFindNode() with pid => NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &nid);
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-10-01 15:48:53 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* add in peer */
|
2010-07-31 14:14:10 -04:00
|
|
|
mUdpBitDht->addFindNode(&nid, BITDHT_QFLAGS_DO_IDLE);
|
2010-07-17 13:00:19 -04:00
|
|
|
|
2010-08-04 05:54:41 -04:00
|
|
|
return true ;
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::dropPeer(std::string pid)
|
|
|
|
{
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht::dropPeer(" << pid << ")";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-31 14:14:10 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* convert id -> NodeId */
|
|
|
|
bdNodeId nid;
|
|
|
|
if (!lookupNodeId(pid, &nid))
|
|
|
|
{
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::dropPeer() Failed to lookup NodeId";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::dropPeer() Translated to NodeId: ";
|
|
|
|
bdStdPrintNodeId(std::cerr, &nid);
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-10-01 15:48:53 -04:00
|
|
|
|
2010-11-22 19:06:54 -05:00
|
|
|
/* remove in peer */
|
|
|
|
mUdpBitDht->removeFindNode(&nid);
|
2010-07-17 13:00:19 -04:00
|
|
|
|
|
|
|
/* remove from translation */
|
|
|
|
if (!removeTranslation(pid))
|
|
|
|
{
|
2010-10-01 15:48:53 -04:00
|
|
|
std::cerr << "p3BitDht::dropPeer() Failed to removeTranslation";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* error */
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-04 05:54:41 -04:00
|
|
|
|
|
|
|
return true ;
|
2010-07-17 13:00:19 -04:00
|
|
|
}
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
/* extract current peer status */
|
2014-03-17 16:56:06 -04:00
|
|
|
bool p3BitDht::getPeerStatus(const RsPeerId& id,
|
2013-09-13 10:35:19 -04:00
|
|
|
struct sockaddr_storage &/*laddr*/, struct sockaddr_storage &/*raddr*/,
|
2011-08-12 09:42:30 -04:00
|
|
|
uint32_t &/*type*/, uint32_t &/*mode*/)
|
2010-07-17 13:00:19 -04:00
|
|
|
{
|
2011-08-12 09:42:30 -04:00
|
|
|
/* remove unused parameter warnings */
|
|
|
|
(void) id;
|
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht::getPeerStatus(" << id << ")";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-17 13:00:19 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-13 10:35:19 -04:00
|
|
|
bool p3BitDht::getExternalInterface(struct sockaddr_storage &/*raddr*/,
|
2011-08-12 09:42:30 -04:00
|
|
|
uint32_t &/*mode*/)
|
2010-07-17 13:00:19 -04:00
|
|
|
{
|
|
|
|
|
2011-06-24 11:10:52 -04:00
|
|
|
#ifdef DEBUG_BITDHT
|
2010-07-31 14:14:10 -04:00
|
|
|
std::cerr << "p3BitDht::getExternalInterface()";
|
|
|
|
std::cerr << std::endl;
|
2011-06-24 11:10:52 -04:00
|
|
|
#endif
|
2010-07-31 14:14:10 -04:00
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-21 07:28:19 -04:00
|
|
|
bool p3BitDht::setAttachMode(bool on)
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef DEBUG_BITDHT
|
|
|
|
std::cerr << "p3BitDht::setAttachMode(" << on << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mUdpBitDht->setAttachMode(on);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|