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,
|
|
|
|
uint32_t mode, uint32_t point, uint32_t cbtype, uint32_t errcode)
|
|
|
|
{
|
2011-07-15 07:13:20 -04:00
|
|
|
return mParent->ConnectCallback(srcId, proxyId, destId, mode, point, cbtype, errcode);
|
2011-06-29 06:46:11 -04:00
|
|
|
}
|
|
|
|
|
2010-07-17 13:00:19 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
p3BitDht *mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-07-09 20:39:15 -04:00
|
|
|
p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm,
|
|
|
|
UdpStack *udpstack, std::string bootstrapfile)
|
|
|
|
: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;
|
|
|
|
|
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
|
|
|
|
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 */
|
|
|
|
bdDhtFunctions *stdfns = new bdStdDht();
|
|
|
|
|
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 */
|
2011-07-13 05:12:31 -04:00
|
|
|
mUdpBitDht = new UdpBitDht(udpstack, &mOwnDhtId, dhtVersion, bootstrapfile, stdfns);
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
void p3BitDht::setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay)
|
|
|
|
{
|
|
|
|
mDhtStunner = dhtStunner;
|
|
|
|
mProxyStunner = proxyStunner;
|
|
|
|
mRelay = relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
bool p3BitDht::getPeerStatus(std::string id,
|
2010-07-31 14:14:10 -04:00
|
|
|
struct sockaddr_in &laddr, struct sockaddr_in &raddr,
|
|
|
|
uint32_t &type, 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::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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::getExternalInterface(struct sockaddr_in &raddr,
|
|
|
|
uint32_t &mode)
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|