diff --git a/libretroshare/src/dht/p3bitdht.cc b/libretroshare/src/dht/p3bitdht.cc index 77a403699..a6616bb08 100644 --- a/libretroshare/src/dht/p3bitdht.cc +++ b/libretroshare/src/dht/p3bitdht.cc @@ -83,7 +83,7 @@ virtual int dhtInfoCallback(const bdId *id, uint32_t type, uint32_t flags, std:: p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm, UdpStack *udpstack, std::string bootstrapfile) - :pqiNetAssistConnect(id, cb), mNetMgr(nm), dhtMtx("p3BitDht") + :p3Config(CONFIG_TYPE_BITDHT), pqiNetAssistConnect(id, cb), mNetMgr(nm), dhtMtx("p3BitDht") { mDhtStunner = NULL; mProxyStunner = NULL; @@ -133,6 +133,7 @@ p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm, p3BdCallback *bdcb = new p3BdCallback(this); mUdpBitDht->addCallback(bdcb); +#if 0 /* enable all modes */ /* 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. @@ -143,6 +144,9 @@ p3BitDht::p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm, BITDHT_CONNECT_MODE_PROXY, BITDHT_CONNECT_OPTION_AUTOPROXY); +#endif + + setupRelayDefaults(); } p3BitDht::~p3BitDht() diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index 5c0cfb3a7..5df81e6d2 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -28,6 +28,7 @@ #define MRK_P3_BITDHT_H #include "pqi/pqiassist.h" +#include "pqi/p3cfgmgr.h" #include "retroshare/rsdht.h" #include @@ -138,7 +139,7 @@ class UdpRelayReceiver; class UdpStunner; class p3NetMgr; -class p3BitDht: public pqiNetAssistConnect, public RsDht +class p3BitDht: public p3Config, public pqiNetAssistConnect, public RsDht { public: p3BitDht(std::string id, pqiConnectCb *cb, p3NetMgr *nm, @@ -164,12 +165,6 @@ virtual int getRelayProxies(std::list &relayProxies); virtual std::string getUdpAddressString(); - // Interface for controlling Relays & DHT Relay Mode -virtual uint32_t getRelayMode(); -virtual int setRelayMode(uint32_t mode); - -virtual int getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth); -virtual int setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth); /*********************************************************************************************** ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* @@ -281,17 +276,48 @@ int RelayHandler_InstallRelayConnection(const bdId *srcId, const bdId *destId, int RelayHandler_LogFailedProxyAttempt(const bdId *srcId, const bdId *destId, uint32_t mode, uint32_t errcode); + +/*********************************************************************************************** + ******************** Relay Config Stuff (TEMP - MOSTLY, in p3bitdht_relay.cc) ***************** + ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* +************************************************************************************************/ + + // Interface for controlling Relays & DHT Relay Mode +virtual int getRelayServerList(std::list &ids); +virtual int addRelayServer(std::string ids); +virtual int removeRelayServer(std::string ids); + +virtual uint32_t getRelayMode(); +virtual int setRelayMode(uint32_t mode); + +virtual int getRelayAllowance(int classIdx, uint32_t &count, uint32_t &bandwidth); +virtual int setRelayAllowance(int classIdx, uint32_t count, uint32_t bandwidth); + + private: + + // Relay Handling Code / Variables (Mutex Protected). +int setupRelayDefaults(); +int pushRelayServers(); + + std::list mRelayServerList; + uint32_t mRelayMode; + + protected: +/*****************************************************************/ +/*********************** p3config ******************************/ + /* Key Functions to be overloaded for Full Configuration */ + virtual RsSerialiser *setupSerialiser(); + virtual bool saveList(bool &cleanup, std::list&); + virtual void saveDone(); + virtual bool loadList(std::list& load); +/*****************************************************************/ + /*********************************************************************************************** ************************** Internal Accounting (p3bitdht_peers.cc) **************************** ************************************************************************************************/ public: -//bool findPeer(std::string pid) -//bool dropPeer(std::string pid); -//int addFriend(const std::string pid); -//int addFriendOfFriend(const std::string pid); -//int addOther(const std::string pid); int removePeer(const std::string pid); // Can be used externally too. diff --git a/libretroshare/src/dht/p3bitdht_interface.cc b/libretroshare/src/dht/p3bitdht_interface.cc index 7ed33fe54..ae28ab032 100644 --- a/libretroshare/src/dht/p3bitdht_interface.cc +++ b/libretroshare/src/dht/p3bitdht_interface.cc @@ -197,54 +197,6 @@ std::string p3BitDht::getUdpAddressString() /*********************************************************************************************** ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* ************************************************************************************************/ -/*********************************************************************************************** - ********** External RsDHT Interface for Dht-Relay Control ************************************* -************************************************************************************************/ - -uint32_t p3BitDht::getRelayMode() -{ - - return 1; -} - -int p3BitDht::setRelayMode(uint32_t mode) -{ - - 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; - - return mRelay->setRelayClassMax(classIdx, count, bandwidth); -} - - -/*********************************************************************************************** - ********** External RsDHT Interface for Dht-Relay Control ************************************* -************************************************************************************************/ - void convertBdPeerToRsDhtPeer(RsDhtPeer &peer, const bdPeer &int_peer) { diff --git a/libretroshare/src/dht/p3bitdht_relay.cc b/libretroshare/src/dht/p3bitdht_relay.cc new file mode 100644 index 000000000..d4f8fadb6 --- /dev/null +++ b/libretroshare/src/dht/p3bitdht_relay.cc @@ -0,0 +1,415 @@ +/* + * 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() +{ + //uint32_t mode = RSDHT_RELAY_ENABLED | RSDHT_RELAY_MODE_OFF; + uint32_t mode = RSDHT_RELAY_MODE_OFF; + 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 &ids) +{ + RsStackMutex stack(dhtMtx); /*********** LOCKED **********/ + + ids = mRelayServerList; + + return 1; +} + +int p3BitDht::addRelayServer(std::string id) +{ + RsStackMutex stack(dhtMtx); /*********** LOCKED **********/ + + std::list::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::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 servers; + + { + RsStackMutex stack(dhtMtx); /*********** LOCKED **********/ + + servers = mRelayServerList; + } + + std::list::iterator it; + for(it = servers.begin(); it != servers.end(); it++) + { + /* 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) +{ + 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 &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; + for(i = 0; i < RSDHT_RELAY_NUM_CLASS; i++) + { + std::ostringstream keyout; + keyout << "RELAY_CLASS" << i; + + std::string countkey = keyout.str() + "_COUNT"; + std::string bandkey = keyout.str() + "_BANDWIDTH"; + + std::ostringstream countout; + std::ostringstream bandout; + countout << mRelay->getRelayClassMax(i); + bandout << mRelay->getRelayClassBandwidth(i); + + kv.key = countkey; + kv.value = countout.str(); + config->tlvkvs.pairs.push_back(kv); + + kv.key = bandkey; + kv.value = bandout.str(); + config->tlvkvs.pairs.push_back(kv); + } + + /* add RelayMode */ + { + std::ostringstream out; + out << mRelayMode; + + kv.key = "RELAY_MODE"; + kv.value = out.str(); + config->tlvkvs.pairs.push_back(kv); + } + + /* add Servers */ + std::list::iterator it; + for(i = 0, it = mRelayServerList.begin(); it != mRelayServerList.end(); it++, i++) + { + std::ostringstream key; + key << "RELAY_SERVER" << i; + + kv.key = key.str(); + kv.value = *it; + config->tlvkvs.pairs.push_back(kv); + } + + std::cerr << "BITDHT Save Item:"; + std::cerr << std::endl; + + config->print(std::cerr, 0); + + saveList.push_back(config); + + return true; +} + +void p3BitDht::saveDone() +{ + return; +} + +bool p3BitDht::loadList(std::list& load) +{ + std::cerr << "p3BitDht::loadList()"; + std::cerr << std::endl; + + if ((load.size() == 0) || (load.size() > 1)) + { + /* error */ + std::cerr << "p3BitDht::loadList() Error only expecting 1 item"; + std::cerr << std::endl; + return false; + } + RsItem *item = load.front(); + + RsConfigKeyValueSet *config = dynamic_cast(item); + + if (!config) + { + /* error */ + std::cerr << "p3BitDht::loadList() Error expecting item = config"; + std::cerr << std::endl; + return false; + } + + std::cerr << "BITDHT Load Item:"; + std::cerr << std::endl; + + config->print(std::cerr, 0); + + std::list servers; + int peers[RSDHT_RELAY_NUM_CLASS] = {0}; + int bandwidth[RSDHT_RELAY_NUM_CLASS] = {0}; + + bool haveMode = false; + int mode = 0; + + std::list::iterator it; + for(it = config->tlvkvs.pairs.begin(); it != config->tlvkvs.pairs.end(); it++) + { + 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; + for(i = 0; i < RSDHT_RELAY_NUM_CLASS; i++) + { + mRelay->setRelayClassMax(i, peers[i], bandwidth[i]); + } + + if (haveMode) + { + setRelayMode(mode); + } + + return true; +} + +/*****************************************************************/ + diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index ce12bc809..1fe429cf0 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -74,6 +74,7 @@ SOURCES += dht/p3bitdht.cc \ dht/p3bitdht_interface.cc \ dht/p3bitdht_peers.cc \ dht/p3bitdht_peernet.cc \ + dht/p3bitdht_relay.cc \ dht/connectstatebox.cc HEADERS += tcponudp/udppeer.h \ diff --git a/libretroshare/src/pqi/p3cfgmgr.h b/libretroshare/src/pqi/p3cfgmgr.h index 86cb2f3cc..f093c02c5 100644 --- a/libretroshare/src/pqi/p3cfgmgr.h +++ b/libretroshare/src/pqi/p3cfgmgr.h @@ -86,11 +86,16 @@ const uint32_t CONFIG_TYPE_HISTORY = 0x0015; /// turtle router const uint32_t CONFIG_TYPE_TURTLE = 0x0020; +/// dht (relay stuff mainly) +const uint32_t CONFIG_TYPE_BITDHT = 0x0030; + /* standard services */ const uint32_t CONFIG_TYPE_QBLOG = 0x0101; const uint32_t CONFIG_TYPE_FORUMS = 0x0102; const uint32_t CONFIG_TYPE_CHANNELS = 0x0103; + + /* CACHE ID Must be at the END so that other configurations * are loaded First (Cache Config --> Cache Loading) */ diff --git a/libretroshare/src/retroshare/rsdht.h b/libretroshare/src/retroshare/rsdht.h index a7b53213f..b8ae12794 100644 --- a/libretroshare/src/retroshare/rsdht.h +++ b/libretroshare/src/retroshare/rsdht.h @@ -79,6 +79,15 @@ extern RsDht *rsDht; #define RSDHT_RELAY_CLASS_FRIENDS 3 +#define RSDHT_RELAY_MODE_MASK 0x00f0 + +#define RSDHT_RELAY_ENABLED 0x0001 + +#define RSDHT_RELAY_MODE_OFF 0x0010 +#define RSDHT_RELAY_MODE_ON 0x0020 +#define RSDHT_RELAY_MODE_SERVER 0x0040 + + class RsDhtPeer { public: @@ -169,6 +178,10 @@ virtual std::string getUdpAddressString() = 0; // Interface for controlling Relays & DHT Relay Mode +virtual int getRelayServerList(std::list &ids) = 0; +virtual int addRelayServer(std::string ids) = 0; +virtual int removeRelayServer(std::string ids) = 0; + virtual uint32_t getRelayMode() = 0; virtual int setRelayMode(uint32_t mode) = 0; diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index b3b7dc8c5..6c17ab049 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -2212,6 +2212,10 @@ int RsServer::StartupRetroShare() mConfigMgr->addConfiguration("turtle.cfg", tr); mConfigMgr->addConfiguration("p3disc.cfg", ad); +#ifdef RS_USE_BITDHT + mConfigMgr->addConfiguration("bitdht.cfg", mBitDht); +#endif + mPluginsManager->addConfigurations(mConfigMgr) ; ftserver->addConfiguration(mConfigMgr);