From 4d2d825d5b4bba31f66c822207a87bee6b648350 Mon Sep 17 00:00:00 2001 From: sehraf Date: Tue, 21 Jun 2016 13:55:23 +0200 Subject: [PATCH] add new option to qmake to enable dht stunner removed dht stunner code when option is not set --- libretroshare/src/dht/p3bitdht.cc | 12 +++++++++++- libretroshare/src/dht/p3bitdht.h | 9 ++++++++- libretroshare/src/dht/p3bitdht_interface.cc | 7 +++++-- libretroshare/src/dht/p3bitdht_peernet.cc | 18 +++++++++++++++++- libretroshare/src/dht/p3bitdht_peers.cc | 4 ++-- libretroshare/src/libretroshare.pro | 18 ++++++++++++++---- libretroshare/src/pqi/p3netmgr.cc | 18 ++++++++++-------- libretroshare/src/pqi/p3netmgr.h | 4 ++++ libretroshare/src/pqi/pqiassist.h | 3 ++- libretroshare/src/rsserver/rsinit.cc | 17 ++++++++++++++++- 10 files changed, 89 insertions(+), 21 deletions(-) diff --git a/libretroshare/src/dht/p3bitdht.cc b/libretroshare/src/dht/p3bitdht.cc index bbbeb9ad4..3868ec497 100644 --- a/libretroshare/src/dht/p3bitdht.cc +++ b/libretroshare/src/dht/p3bitdht.cc @@ -30,7 +30,9 @@ #include "bitdht/bdstddht.h" #include "tcponudp/udprelay.h" +#ifdef RS_USE_DHT_STUNNER #include "tcponudp/udpstunner.h" +#endif // RS_USE_DHT_STUNNER #include @@ -86,8 +88,10 @@ p3BitDht::p3BitDht(const RsPeerId& id, pqiConnectCb *cb, p3NetMgr *nm, UdpStack *udpstack, std::string bootstrapfile,const std::string& filteredipfile) :p3Config(), pqiNetAssistConnect(id, cb), mNetMgr(nm), dhtMtx("p3BitDht") { +#ifdef RS_USE_DHT_STUNNER mDhtStunner = NULL; mProxyStunner = NULL; +#endif mRelay = NULL; mPeerSharer = NULL; @@ -168,13 +172,19 @@ bool p3BitDht::getOwnDhtId(std::string &ownDhtId) return true; } - +#ifdef RS_USE_DHT_STUNNER void p3BitDht::setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay) { mDhtStunner = dhtStunner; mProxyStunner = proxyStunner; mRelay = relay; } +#else // RS_USE_DHT_STUNNER +void p3BitDht::setupConnectBits(UdpRelayReceiver *relay) +{ + mRelay = relay; +} +#endif //RS_USE_DHT_STUNNER void p3BitDht::setupPeerSharer(pqiNetAssistPeerShare *sharer) { diff --git a/libretroshare/src/dht/p3bitdht.h b/libretroshare/src/dht/p3bitdht.h index fdc832449..92c2e626d 100644 --- a/libretroshare/src/dht/p3bitdht.h +++ b/libretroshare/src/dht/p3bitdht.h @@ -136,7 +136,9 @@ class p3BitDhtRelayHandler class UdpRelayReceiver; +#ifdef RS_USE_DHT_STUNNER class UdpStunner; +#endif // RS_USE_DHT_STUNNER class p3NetMgr; class p3BitDht: public p3Config, public pqiNetAssistConnect, public RsDht @@ -174,8 +176,11 @@ public: ********** External RsDHT Interface (defined in libretroshare/src/retroshare/rsdht.h) ********* ************************************************************************************************/ - +#ifdef RS_USE_DHT_STUNNER void setupConnectBits(UdpStunner *dhtStunner, UdpStunner *proxyStunner, UdpRelayReceiver *relay); +#else // RS_USE_DHT_STUNNER + void setupConnectBits(UdpRelayReceiver *relay); +#endif // RS_USE_DHT_STUNNER void setupPeerSharer(pqiNetAssistPeerShare *sharer); void modifyNodesPerBucket(uint16_t count); @@ -359,8 +364,10 @@ private: int removeTranslation_locked(const RsPeerId& pid); UdpBitDht *mUdpBitDht; /* has own mutex, is static except for creation/destruction */ +#ifdef RS_USE_DHT_STUNNER UdpStunner *mDhtStunner; UdpStunner *mProxyStunner; +#endif // RS_USE_DHT_STUNNER UdpRelayReceiver *mRelay; p3NetMgr *mNetMgr; diff --git a/libretroshare/src/dht/p3bitdht_interface.cc b/libretroshare/src/dht/p3bitdht_interface.cc index 7eaff764e..fb8f00318 100644 --- a/libretroshare/src/dht/p3bitdht_interface.cc +++ b/libretroshare/src/dht/p3bitdht_interface.cc @@ -152,7 +152,7 @@ std::string p3BitDht::getUdpAddressString() struct sockaddr_in extAddr; uint8_t extStable; - +#ifdef RS_USE_DHT_STUNNER if (mDhtStunner->externalAddr(extAddr, extStable)) { rs_sprintf_append(out, " DhtExtAddr: %s:%u", rs_inet_ntoa(extAddr.sin_addr).c_str(), ntohs(extAddr.sin_port)); @@ -187,7 +187,10 @@ std::string p3BitDht::getUdpAddressString() { out += " ProxyExtAddr: Unknown "; } - +#else // RS_USE_DHT_STUNNER + // whitespaces taken from above + out = " DhtExtAddr: Unknown ProxyExtAddr: Unknown "; +#endif // RS_USE_DHT_STUNNER return out; } diff --git a/libretroshare/src/dht/p3bitdht_peernet.cc b/libretroshare/src/dht/p3bitdht_peernet.cc index 97f116b7d..c0cafff7a 100644 --- a/libretroshare/src/dht/p3bitdht_peernet.cc +++ b/libretroshare/src/dht/p3bitdht_peernet.cc @@ -81,6 +81,7 @@ int p3BitDht::InfoCallback(const bdId *id, uint32_t /*type*/, uint32_t /*flags*/ mPeerSharer->updatePeer(rsid, tmpaddr, outtype, outreason, outage); } +#ifdef RS_USE_DHT_STUNNER /* call to the Stunners to drop the address as well */ /* IDEALLY these addresses should all be filtered at UdpLayer level instead! */ if (mDhtStunner) @@ -91,6 +92,7 @@ int p3BitDht::InfoCallback(const bdId *id, uint32_t /*type*/, uint32_t /*flags*/ { mProxyStunner->dropStunPeer(addr); } +#endif // RS_USE_DHT_STUNNER return 1; } @@ -135,7 +137,7 @@ int p3BitDht::NodeCallback(const bdId *id, uint32_t peerflags) } } - +#ifdef RS_USE_DHT_STUNNER if ((mProxyStunner) && (mProxyStunner->needStunPeers())) { #ifdef DEBUG_BITDHT_COMMON @@ -156,6 +158,7 @@ int p3BitDht::NodeCallback(const bdId *id, uint32_t peerflags) #endif mDhtStunner->addStunPeer(id->addr, NULL); } +#endif // RS_USE_DHT_STUNNER } return 1; } @@ -731,6 +734,7 @@ int p3BitDht::ConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId std::cerr << std::endl; } +#ifdef RS_USE_DHT_STUNNER UdpStunner *stunner = mProxyStunner; if (!proxyPort) { @@ -849,6 +853,9 @@ int p3BitDht::ConnectCallback(const bdId *srcId, const bdId *proxyId, const bdId std::cerr << std::endl; #endif } +#else // RS_USE_DHT_STUNNER + connectionAllowed = BITDHT_CONNECT_ERROR_TEMPUNAVAIL; +#endif // RS_USE_DHT_STUNNER } ca.mMode = mode; @@ -1234,6 +1241,7 @@ int p3BitDht::doActions() std::cerr << std::endl; } } +#ifdef RS_USE_DHT_STUNNER UdpStunner *stunner = mProxyStunner; if (!proxyPort) { @@ -1333,6 +1341,10 @@ int p3BitDht::doActions() connectionReqFailed = true; failReason = CSB_UPDATE_RETRY_ATTEMPT; } +#else // RS_USE_DHT_STUNNER + connectionReqFailed = true; + failReason = CSB_UPDATE_RETRY_ATTEMPT; +#endif //RS_USE_DHT_STUNNER } if (doConnectionRequest) @@ -1416,7 +1428,9 @@ int p3BitDht::doActions() { std::cerr << "PeerAction: ERROR ERROR, we grabd Exclusive Port to do this, trying emergency release"; std::cerr << std::endl; +#ifdef RS_USE_DHT_STUNNER mProxyStunner->releaseExclusiveMode(pid,false); +#endif // RS_USE_DHT_STUNNER } } } @@ -2378,6 +2392,7 @@ void p3BitDht::ReleaseProxyExclusiveMode_locked(DhtPeerDetails *dpd, bool addrCh if (dpd->mExclusiveProxyLock) { +#ifdef RS_USE_DHT_STUNNER if (mProxyStunner->releaseExclusiveMode(pid, addrChgLikely)) { dpd->mExclusiveProxyLock = false; @@ -2389,6 +2404,7 @@ void p3BitDht::ReleaseProxyExclusiveMode_locked(DhtPeerDetails *dpd, bool addrCh #endif } else +#endif // RS_USE_DHT_STUNNER { dpd->mExclusiveProxyLock = false; diff --git a/libretroshare/src/dht/p3bitdht_peers.cc b/libretroshare/src/dht/p3bitdht_peers.cc index 742361dab..0543b91b2 100644 --- a/libretroshare/src/dht/p3bitdht_peers.cc +++ b/libretroshare/src/dht/p3bitdht_peers.cc @@ -202,7 +202,7 @@ int p3BitDht::addBadPeer(const struct sockaddr_storage &addr, uint32_t /*reason* addrv4.sin_addr = ap->sin_addr; addrv4.sin_port = ap->sin_port; - +#ifdef RS_USE_DHT_STUNNER if (mDhtStunner) { mDhtStunner->dropStunPeer(addrv4); @@ -211,7 +211,7 @@ int p3BitDht::addBadPeer(const struct sockaddr_storage &addr, uint32_t /*reason* { mProxyStunner->dropStunPeer(addrv4); } - +#endif // RS_USE_DHT_STUNNER return 1; } diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 127da6bd5..6499243b8 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -10,6 +10,10 @@ DESTDIR = lib #CONFIG += dsdv +# the dht stunner is used to obtain RS' external ip addr. when it is natted +# this system is unreliable and rs supports a newer and better one (asking connected peers) +# CONFIG += useDhtStunner + profiling { QMAKE_CXXFLAGS -= -fomit-frame-pointer QMAKE_CXXFLAGS *= -pg -g -fno-omit-frame-pointer @@ -47,8 +51,7 @@ SOURCES *= serialiser/rstlvdsdv.cc \ bitdht { HEADERS += dht/p3bitdht.h \ - dht/connectstatebox.h \ - dht/stunaddrassist.h + dht/connectstatebox.h SOURCES += dht/p3bitdht.cc \ dht/p3bitdht_interface.cc \ @@ -62,7 +65,6 @@ HEADERS += tcponudp/udppeer.h \ tcponudp/tcppacket.h \ tcponudp/tcpstream.h \ tcponudp/tou.h \ - tcponudp/udpstunner.h \ tcponudp/udprelay.h \ SOURCES += tcponudp/udppeer.cc \ @@ -70,9 +72,17 @@ SOURCES += tcponudp/udppeer.cc \ tcponudp/tcpstream.cc \ tcponudp/tou.cc \ tcponudp/bss_tou.c \ - tcponudp/udpstunner.cc \ tcponudp/udprelay.cc \ + useDhtStunner { + HEADERS += dht/stunaddrassist.h \ + tcponudp/udpstunner.h + + SOURCES += tcponudp/udpstunner.cc + + DEFINES += RS_USE_DHT_STUNNER + } + DEFINES *= RS_USE_BITDHT BITDHT_DIR = ../../libbitdht/src diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 620175c25..cf18267fe 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -169,12 +169,13 @@ void p3NetMgrIMPL::setManagers(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr) // mDhtMgr = dhtMgr; //} +#ifdef RS_USE_DHT_STUNNER void p3NetMgrIMPL::setAddrAssist(pqiAddrAssist *dhtStun, pqiAddrAssist *proxyStun) { mDhtStunner = dhtStun; mProxyStunner = proxyStun; } - +#endif // RS_USE_DHT_STUNNER /***** Framework / initial implementation for a connection manager. @@ -445,6 +446,7 @@ void p3NetMgrIMPL::slowTick() netAssistTick(); updateNetStateBox_temporal(); +#ifdef RS_USE_DHT_STUNNER if (mDhtStunner) { mDhtStunner->tick(); @@ -454,7 +456,7 @@ void p3NetMgrIMPL::slowTick() { mProxyStunner->tick(); } - +#endif // RS_USE_DHT_STUNNER } #define STARTUP_DELAY 5 @@ -1739,6 +1741,7 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() struct sockaddr_storage tmpaddr; sockaddr_storage_clear(tmpaddr); +#ifdef RS_USE_DHT_STUNNER if (mDhtStunner) { @@ -1776,6 +1779,7 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() } } +#endif // RS_USE_DHT_STUNNER { @@ -1789,7 +1793,7 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() /* now we check if a WebIP address is required? */ - +#ifdef NETMGR_DEBUG_STATEBOX { RsStackMutex stack(mNetMtx); /****** STACK LOCK MUTEX *******/ @@ -1805,7 +1809,6 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() std::string nattypestr = NetStateNatTypeString(natType); std::string netmodestr = NetStateNetworkModeString(netMode); -#ifdef NETMGR_DEBUG_STATEBOX std::cerr << "p3NetMgrIMPL::updateNetStateBox_temporal() NetStateBox Thinking"; std::cerr << std::endl; std::cerr << "\tNetState: " << netstatestr; @@ -1818,10 +1821,9 @@ void p3NetMgrIMPL::updateNetStateBox_temporal() std::cerr << std::endl; std::cerr << "\tNatType: " << nattypestr; std::cerr << std::endl; -#endif } - +#endif updateNatSetting(); @@ -1869,7 +1871,7 @@ void p3NetMgrIMPL::updateNatSetting() std::cerr << std::endl; #endif - +#ifdef RS_USE_DHT_STUNNER switch(natType) { case RSNET_NATTYPE_RESTRICTED_CONE: @@ -1894,7 +1896,7 @@ void p3NetMgrIMPL::updateNatSetting() mProxyStunner->setRefreshPeriod(NET_STUNNER_PERIOD_SLOW); break; } - +#endif // RS_USE_DHT_STUNNER /* This controls the Attach mode of the DHT... * which effectively makes the DHT "attach" to Open Nodes. diff --git a/libretroshare/src/pqi/p3netmgr.h b/libretroshare/src/pqi/p3netmgr.h index afc1bda1a..1b379afec 100644 --- a/libretroshare/src/pqi/p3netmgr.h +++ b/libretroshare/src/pqi/p3netmgr.h @@ -209,7 +209,9 @@ virtual bool getDHTEnabled(); /************************************************************************************************/ void setManagers(p3PeerMgr *peerMgr, p3LinkMgr *linkMgr); +#ifdef RS_USE_DHT_STUNNER void setAddrAssist(pqiAddrAssist *dhtStun, pqiAddrAssist *proxyStun); +#endif // RS_USE_DHT_STUNNER void tick(); @@ -319,8 +321,10 @@ private: p3LinkMgr *mLinkMgr; //p3BitDht *mBitDht; +#ifdef RS_USE_DHT_STUNNER pqiAddrAssist *mDhtStunner; pqiAddrAssist *mProxyStunner; +#endif // RS_USE_DHT_STUNNER RsMutex mNetMtx; /* protects below */ diff --git a/libretroshare/src/pqi/pqiassist.h b/libretroshare/src/pqi/pqiassist.h index d5c7da4cb..9f93b07df 100644 --- a/libretroshare/src/pqi/pqiassist.h +++ b/libretroshare/src/pqi/pqiassist.h @@ -115,6 +115,7 @@ virtual void updatePeer(const RsPeerId& id, const struct sockaddr_storage &addr }; +#ifdef RS_USE_DHT_STUNNER /* this is for the Stunners * * @@ -132,7 +133,7 @@ virtual void setRefreshPeriod(int32_t period) = 0; virtual int tick() = 0; /* for internal accounting */ }; - +#endif // RS_USE_DHT_STUNNER #define NETASSIST_KNOWN_PEER_OFFLINE 0x0001 #define NETASSIST_KNOWN_PEER_ONLINE 0x0002 diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 733009932..1ccafd3c5 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -77,7 +77,9 @@ #include "grouter/p3grouter.h" #endif +#ifdef RS_USE_DHT_STUNNER #include "tcponudp/udpstunner.h" +#endif // RS_USE_DHT_STUNNER // #define GPG_DEBUG // #define AUTHSSL_DEBUG @@ -901,7 +903,9 @@ RsGRouter *rsGRouter = NULL ; #ifdef RS_USE_BITDHT #include "dht/p3bitdht.h" +#ifdef RS_USE_DHT_STUNNER #include "dht/stunaddrassist.h" +#endif // RS_USE_DHT_STUNNER #include "udp/udpstack.h" #include "tcponudp/udppeer.h" @@ -1137,6 +1141,7 @@ int RsServer::StartupRetroShare() UdpSubReceiver *udpReceivers[RSUDP_NUM_TOU_RECVERS]; int udpTypes[RSUDP_NUM_TOU_RECVERS]; +#ifdef RS_USE_DHT_STUNNER // FIRST DHT STUNNER. UdpStunner *mDhtStunner = new UdpStunner(mDhtStack); mDhtStunner->setTargetStunPeriod(300); /* slow (5mins) */ @@ -1145,9 +1150,11 @@ int RsServer::StartupRetroShare() #ifdef LOCALNET_TESTING mDhtStunner->SetAcceptLocalNet(); #endif +#endif // RS_USE_DHT_STUNNER + // NEXT BITDHT. - p3BitDht *mBitDht = new p3BitDht(ownId, mLinkMgr, mNetMgr, mDhtStack, bootstrapfile, filteredipfile); + p3BitDht *mBitDht = new p3BitDht(ownId, mLinkMgr, mNetMgr, mDhtStack, bootstrapfile, filteredipfile); /* install external Pointer for Interface */ rsDht = mBitDht; @@ -1188,6 +1195,7 @@ int RsServer::StartupRetroShare() rsFixedUdpStack *mProxyStack = new rsFixedUdpStack(sndladdr); #endif +#ifdef RS_USE_DHT_STUNNER // FIRSTLY THE PROXY STUNNER. UdpStunner *mProxyStunner = new UdpStunner(mProxyStack); mProxyStunner->setTargetStunPeriod(300); /* slow (5mins) */ @@ -1196,6 +1204,7 @@ int RsServer::StartupRetroShare() #ifdef LOCALNET_TESTING mProxyStunner->SetAcceptLocalNet(); #endif +#endif // RS_USE_DHT_STUNNER // FINALLY THE PROXY UDP CONNECTIONS @@ -1206,9 +1215,15 @@ int RsServer::StartupRetroShare() // REAL INITIALISATION - WITH THREE MODES tou_init((void **) udpReceivers, udpTypes, RSUDP_NUM_TOU_RECVERS); +#ifdef RS_USE_DHT_STUNNER mBitDht->setupConnectBits(mDhtStunner, mProxyStunner, mRelay); +#else // RS_USE_DHT_STUNNER + mBitDht->setupConnectBits(mRelay); +#endif // RS_USE_DHT_STUNNER +#ifdef RS_USE_DHT_STUNNER mNetMgr->setAddrAssist(new stunAddrAssist(mDhtStunner), new stunAddrAssist(mProxyStunner)); +#endif // RS_USE_DHT_STUNNER #else /* install NULL Pointer for rsDht Interface */ rsDht = NULL;