2011-12-19 21:07:00 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/dht: p3bitdht.h
|
|
|
|
*
|
|
|
|
* BitDht interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2009-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 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 "util/rsnet.h"
|
|
|
|
#include "dht/p3bitdht.h"
|
|
|
|
|
|
|
|
#include "tcponudp/udprelay.h"
|
|
|
|
#include "bitdht/bdstddht.h"
|
|
|
|
|
|
|
|
#include "serialiser/rsconfigitems.h"
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************
|
|
|
|
********** External RsDHT Interface for Dht-Relay Control *************************************
|
|
|
|
************************************************************************************************/
|
|
|
|
|
|
|
|
int p3BitDht::setupRelayDefaults()
|
|
|
|
{
|
2012-01-20 12:50:19 -05:00
|
|
|
uint32_t mode = RSDHT_RELAY_ENABLED | RSDHT_RELAY_MODE_OFF;
|
2011-12-19 21:07:00 -05:00
|
|
|
setRelayMode(mode);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**** THIS IS A TEMPORARY HACK INTERFACE - UNTIL WE HAVE MORE SOFISTICATED SYSTEM
|
|
|
|
* NB: using bdNodeIds here, rather than SSL IDS.
|
|
|
|
*****/
|
|
|
|
|
|
|
|
int p3BitDht::getRelayServerList(std::list<std::string> &ids)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
|
|
|
ids = mRelayServerList;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::addRelayServer(std::string id)
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
it = std::find(mRelayServerList.begin(), mRelayServerList.end(), id);
|
|
|
|
if (it == mRelayServerList.end())
|
|
|
|
{
|
|
|
|
mRelayServerList.push_back(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::removeRelayServer(std::string id)
|
|
|
|
{
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
it = std::find(mRelayServerList.begin(), mRelayServerList.end(), id);
|
|
|
|
if (it != mRelayServerList.end())
|
|
|
|
{
|
|
|
|
mRelayServerList.erase(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::pushRelayServers()
|
|
|
|
{
|
|
|
|
std::list<std::string> servers;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
|
|
|
servers = mRelayServerList;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<std::string>::iterator it;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = servers.begin(); it != servers.end(); ++it)
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
|
|
|
/* push it to dht */
|
|
|
|
uint32_t bdflags = BITDHT_PEER_STATUS_DHT_RELAY_SERVER;
|
|
|
|
bdId id;
|
|
|
|
bdStdLoadNodeId(&(id.id), *it);
|
|
|
|
|
|
|
|
mUdpBitDht->updateKnownPeer(&id, 0, bdflags);
|
|
|
|
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t p3BitDht::getRelayMode()
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
|
|
|
|
return mRelayMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::setRelayMode(uint32_t mode)
|
|
|
|
{
|
2012-01-13 20:54:17 -05:00
|
|
|
std::cerr << "p3BitDht::setRelayMode(" << mode << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2011-12-19 21:07:00 -05:00
|
|
|
if (mode & RSDHT_RELAY_ENABLED)
|
|
|
|
{
|
|
|
|
mUdpBitDht->ConnectionOptions(
|
|
|
|
BITDHT_CONNECT_MODE_DIRECT | BITDHT_CONNECT_MODE_PROXY | BITDHT_CONNECT_MODE_RELAY,
|
|
|
|
BITDHT_CONNECT_OPTION_AUTOPROXY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mUdpBitDht->ConnectionOptions(
|
|
|
|
BITDHT_CONNECT_MODE_DIRECT | BITDHT_CONNECT_MODE_PROXY,
|
|
|
|
BITDHT_CONNECT_OPTION_AUTOPROXY);
|
|
|
|
}
|
|
|
|
|
|
|
|
int relaymode = mode & RSDHT_RELAY_MODE_MASK;
|
|
|
|
|
|
|
|
switch(relaymode)
|
|
|
|
{
|
|
|
|
case RSDHT_RELAY_MODE_OFF:
|
|
|
|
mUdpBitDht->setDhtMode(BITDHT_MODE_RELAYSERVERS_IGNORED);
|
|
|
|
break;
|
|
|
|
case RSDHT_RELAY_MODE_ON:
|
|
|
|
pushRelayServers();
|
|
|
|
mUdpBitDht->setDhtMode(BITDHT_MODE_RELAYSERVERS_FLAGGED);
|
|
|
|
break;
|
|
|
|
case RSDHT_RELAY_MODE_SERVER:
|
|
|
|
pushRelayServers();
|
|
|
|
mUdpBitDht->setDhtMode(BITDHT_MODE_RELAYSERVERS_SERVER);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
mRelayMode = mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndicateConfigChanged();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::getRelayAllowance(" << classIdx << "): ";
|
|
|
|
if ((classIdx >= 0) && (classIdx < RSDHT_RELAY_NUM_CLASS))
|
|
|
|
{
|
|
|
|
count = mRelay->getRelayClassMax(classIdx);
|
|
|
|
bandwidth = mRelay->getRelayClassBandwidth(classIdx);
|
|
|
|
|
|
|
|
std::cerr << " count: " << count << " bandwidth: " << bandwidth;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
std::cerr << " Invalid classIdx";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3BitDht::setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::getRelayAllowance(" << classIdx << ", ";
|
|
|
|
std::cerr << ", " << count << ", " << bandwidth << ")";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
int retval = mRelay->setRelayClassMax(classIdx, count, bandwidth);
|
|
|
|
IndicateConfigChanged();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************************
|
|
|
|
********** External RsDHT Interface for Dht-Relay Control *************************************
|
|
|
|
************************************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************/
|
|
|
|
/*********************** p3config ******************************/
|
|
|
|
/* Key Functions to be overloaded for Full Configuration */
|
|
|
|
RsSerialiser *p3BitDht::setupSerialiser()
|
|
|
|
{
|
|
|
|
RsSerialiser *rss = new RsSerialiser ;
|
|
|
|
rss->addSerialType(new RsGeneralConfigSerialiser());
|
|
|
|
return rss ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3BitDht::saveList(bool &cleanup, std::list<RsItem *> &saveList)
|
|
|
|
{
|
|
|
|
cleanup = true;
|
|
|
|
|
|
|
|
std::cerr << "p3BitDht::saveList()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
RsConfigKeyValueSet *config = new RsConfigKeyValueSet();
|
|
|
|
|
|
|
|
RsTlvKeyValue kv;
|
|
|
|
|
|
|
|
/* Push Relay Class Stuff */
|
|
|
|
int i;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(i = 0; i < RSDHT_RELAY_NUM_CLASS; ++i)
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
rs_sprintf(kv.key, "RELAY_CLASS%d_COUNT", i);
|
|
|
|
rs_sprintf(kv.value, "%d", mRelay->getRelayClassMax(i));
|
2011-12-19 21:07:00 -05:00
|
|
|
config->tlvkvs.pairs.push_back(kv);
|
|
|
|
|
2012-04-14 18:38:24 -04:00
|
|
|
rs_sprintf(kv.key, "RELAY_CLASS%d_BANDWIDTH", i);
|
|
|
|
rs_sprintf(kv.value, "%d", mRelay->getRelayClassBandwidth(i));
|
|
|
|
config->tlvkvs.pairs.push_back(kv);
|
2011-12-19 21:07:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add RelayMode */
|
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
kv.key = "RELAY_MODE";
|
|
|
|
rs_sprintf(kv.value, "%lu", mRelayMode);
|
2011-12-19 21:07:00 -05:00
|
|
|
config->tlvkvs.pairs.push_back(kv);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add Servers */
|
|
|
|
std::list<std::string>::iterator it;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(i = 0, it = mRelayServerList.begin(); it != mRelayServerList.end(); ++it, ++i)
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
2012-04-14 18:38:24 -04:00
|
|
|
rs_sprintf(kv.key, "RELAY_SERVER%d", i);
|
|
|
|
kv.value = *it;
|
2011-12-19 21:07:00 -05:00
|
|
|
config->tlvkvs.pairs.push_back(kv);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "BITDHT Save Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
config->print(std::cerr, 0);
|
|
|
|
|
2015-05-25 10:02:45 -04:00
|
|
|
saveList.push_back(config);
|
2011-12-19 21:07:00 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3BitDht::saveDone()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3BitDht::loadList(std::list<RsItem *>& load)
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::loadList()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
2014-10-21 18:33:02 -04:00
|
|
|
if (load.empty() || (load.size() > 1))
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3BitDht::loadList() Error only expecting 1 item";
|
|
|
|
std::cerr << std::endl;
|
2015-12-30 18:20:09 -05:00
|
|
|
|
|
|
|
for(std::list<RsItem*>::iterator it=load.begin();it!=load.end();++it)
|
|
|
|
delete *it ;
|
|
|
|
|
|
|
|
load.clear() ;
|
2011-12-19 21:07:00 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
RsItem *item = load.front();
|
|
|
|
|
|
|
|
RsConfigKeyValueSet *config = dynamic_cast<RsConfigKeyValueSet *>(item);
|
|
|
|
|
|
|
|
if (!config)
|
|
|
|
{
|
|
|
|
/* error */
|
|
|
|
std::cerr << "p3BitDht::loadList() Error expecting item = config";
|
|
|
|
std::cerr << std::endl;
|
2015-12-30 18:20:09 -05:00
|
|
|
delete item ;
|
2011-12-19 21:07:00 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cerr << "BITDHT Load Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
config->print(std::cerr, 0);
|
|
|
|
|
|
|
|
std::list<std::string> servers;
|
|
|
|
int peers[RSDHT_RELAY_NUM_CLASS] = {0};
|
|
|
|
int bandwidth[RSDHT_RELAY_NUM_CLASS] = {0};
|
|
|
|
|
|
|
|
bool haveMode = false;
|
|
|
|
int mode = 0;
|
|
|
|
|
|
|
|
std::list<RsTlvKeyValue>::iterator it;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(it = config->tlvkvs.pairs.begin(); it != config->tlvkvs.pairs.end(); ++it)
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
|
|
|
std::string key = it->key;
|
|
|
|
std::string value = it->value;
|
|
|
|
if (0 == strncmp(key.c_str(), "RELAY_SERVER", 12))
|
|
|
|
{
|
|
|
|
/* add to RELAY_SERVER List */
|
|
|
|
servers.push_back(value);
|
|
|
|
std::cerr << "p3BitDht::loadList() Found Server: " << value;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else if (0 == strncmp(key.c_str(), "RELAY_MODE", 10))
|
|
|
|
{
|
|
|
|
mode = atoi(value.c_str());
|
|
|
|
haveMode = true;
|
|
|
|
|
|
|
|
std::cerr << "p3BitDht::loadList() Found Mode: " << mode;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
else if (0 == strncmp(key.c_str(), "RELAY_CLASS", 11))
|
|
|
|
{
|
|
|
|
/* len check */
|
|
|
|
if (key.length() < 14)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int idx = 0;
|
|
|
|
uint32_t val = atoi(value.c_str());
|
|
|
|
switch(key[11])
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case '0':
|
|
|
|
idx = 0;
|
|
|
|
break;
|
|
|
|
case '1':
|
|
|
|
idx = 1;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
idx = 2;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
idx = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key[13] == 'C')
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::loadList() Found Count(" << idx << "): ";
|
|
|
|
std::cerr << val;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
peers[idx] = val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::loadList() Found Bandwidth(" << idx << "): ";
|
|
|
|
std::cerr << val;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
bandwidth[idx] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "p3BitDht::loadList() Unknown Key:value: " << key;
|
|
|
|
std::cerr << ":" << value;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Cleanup config.
|
|
|
|
delete config;
|
|
|
|
|
|
|
|
{
|
|
|
|
RsStackMutex stack(dhtMtx); /*********** LOCKED **********/
|
|
|
|
mRelayServerList = servers;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
2014-10-24 18:07:26 -04:00
|
|
|
for(i = 0; i < RSDHT_RELAY_NUM_CLASS; ++i)
|
2011-12-19 21:07:00 -05:00
|
|
|
{
|
|
|
|
mRelay->setRelayClassMax(i, peers[i], bandwidth[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (haveMode)
|
|
|
|
{
|
|
|
|
setRelayMode(mode);
|
|
|
|
}
|
|
|
|
|
2015-12-30 18:20:09 -05:00
|
|
|
load.clear() ;
|
2011-12-19 21:07:00 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************/
|
|
|
|
|