diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 574311bb8..39e4b20f7 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -404,9 +404,7 @@ HEADERS += pqi/authssl.h \ pqi/pqiqosstreamer.h \ pqi/sslfns.h \ pqi/pqinetstatebox.h \ - pqi/p3servicecontrol.h \ - -# pqi/p3dhtmgr.h \ + pqi/p3servicecontrol.h HEADERS += rsserver/p3face.h \ rsserver/p3history.h \ @@ -653,8 +651,8 @@ equals(RS_UPNP_LIB, miniupnpc) { contains(RS_UPNP_LIB, upnp) { HEADERS += rs_upnp/upnp18_retrocompat.h - HEADERS += rs_upnp/UPnPBase.h rs_upnp/upnphandler_linux.h - SOURCES += rs_upnp/UPnPBase.cpp rs_upnp/upnphandler_linux.cc + HEADERS += rs_upnp/UPnPBase.h rs_upnp/upnphandler_libupnp.h + SOURCES += rs_upnp/UPnPBase.cpp rs_upnp/upnphandler_libupnp.cc } # new gxs cache system diff --git a/libretroshare/src/pqi/p3dhtmgr.cc b/libretroshare/src/pqi/p3dhtmgr.cc deleted file mode 100644 index d54a1cbf2..000000000 --- a/libretroshare/src/pqi/p3dhtmgr.cc +++ /dev/null @@ -1,1795 +0,0 @@ -/******************************************************************************* - * libretroshare/src/pqi: p3dhtmgr.cc * - * * - * libretroshare: retroshare core library * - * * - * Copyright 2004-2008 by Robert Fernie. * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU Lesser General Public License as * - * published by the Free Software Foundation, either version 3 of the * - * License, or (at your option) any later version. * - * * - * This program 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 Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public License * - * along with this program. If not, see . * - * * - *******************************************************************************/ - -#include -#include -#include -#include -#include "util/rstime.h" - -#include "pqi/p3dhtmgr.h" -#include "pqi/p3peermgr.h" -#include "pqi/p3linkmgr.h" - -#include "util/rsprint.h" -#include "util/rsdebug.h" -#include "util/rsstring.h" -const int p3dhtzone = 3892; - -/***** - * #define DHT_DEBUG 1 - * #define DHT_LOG 1 // FOR LOGGING DHT STUFF. - * #define P3DHTMGR_USE_LOCAL_UDP_CONN 1 // For Testing only - ****/ - -/**** DHT State Variables **** - * TODO: - * (1) notify call in. - * (2) publish/search parameters. - * (3) callback. - * (4) example - * - */ - -/**** DHT State Variables ****/ - -#define DHT_STATE_OFF 0 -#define DHT_STATE_INIT 1 -#define DHT_STATE_CHECK_PEERS 2 -#define DHT_STATE_FIND_STUN 3 -#define DHT_STATE_ACTIVE 4 - -/* TIMEOUTS (Key ones in .h) */ -#define DHT_RESTART_PERIOD 300 /* 5 min */ -#define DHT_DEFAULT_PERIOD 300 /* Default period if no work to do */ -#define DHT_MIN_PERIOD 1 /* to ensure we don't get too many requests */ -#define DHT_SHORT_PERIOD 10 /* a short period */ - -#define DHT_DEFAULT_WAITTIME 1 /* Std sleep break period */ - -#define DHT_NUM_BOOTSTRAP_BINS 8 -#define DHT_MIN_BOOTSTRAP_REQ_PERIOD 5 - -void printDhtPeerEntry(dhtPeerEntry *ent, std::ostream &out); - -/* Interface class for DHT data */ - -dhtPeerEntry::dhtPeerEntry() - :state(DHT_PEER_INIT), lastTS(0), - notifyPending(0), notifyTS(0), - type(DHT_ADDR_INVALID) -{ - laddr.sin_addr.s_addr = 0; - laddr.sin_port = 0; - laddr.sin_family = 0; - - raddr.sin_addr.s_addr = 0; - raddr.sin_port = 0; - raddr.sin_family = 0; - - return; -} - -p3DhtMgr::p3DhtMgr(RsPeerId id, pqiConnectCb *cb) - :pqiNetAssistConnect(id, cb), dhtMtx("p3DhtMgr"), mStunRequired(true) -{ - /* setup own entry */ - dhtMtx.lock(); /* LOCK MUTEX */ - - ownEntry.id = id; - ownEntry.state = DHT_PEER_INIT; - ownEntry.type = DHT_ADDR_INVALID; - ownEntry.lastTS = 0; - - ownEntry.notifyPending = 0; - ownEntry.notifyTS = 0; - - ownEntry.hash1 = RsUtil::HashId(id, false); - ownEntry.hash2 = RsUtil::HashId(id, true); - - mDhtModifications = true; - mDhtOn = false; - mDhtState = DHT_STATE_OFF; - - mBootstrapAllowed = true; - mLastBootstrapListTS = 0; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return; -} - - /* OVERLOADED from pqiNetAssistConnect - */ - -void p3DhtMgr::shutdown() -{ - /* ??? */ - -} - -void p3DhtMgr::restart() -{ - /* ??? */ -} - -void p3DhtMgr::enable(bool on) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtModifications = true; - mDhtOn = on; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ -} - -bool p3DhtMgr::getEnabled() -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - bool on = mDhtOn; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return on; -} - -bool p3DhtMgr::getActive() -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - bool act = dhtActive(); - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return act; -} - -void p3DhtMgr::setBootstrapAllowed(bool on) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - mBootstrapAllowed = on; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ -} - -bool p3DhtMgr::getBootstrapAllowed() -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - bool on = mBootstrapAllowed; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return on; -} - -/******************************** PEER MANAGEMENT ********************************** - * - */ - /* set key data */ -bool p3DhtMgr::setExternalInterface( - struct sockaddr_in laddr, - struct sockaddr_in raddr, - uint32_t type) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtModifications = true; - ownEntry.laddr = laddr; - ownEntry.raddr = raddr; - ownEntry.type = type; - ownEntry.state = DHT_PEER_ADDR_KNOWN; /* will force republish */ - ownEntry.lastTS = 0; /* will force republish */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::setExternalInterface()"; - std::cerr << " laddr: " << rs_inet_ntoa(ownEntry.laddr.sin_addr); - std::cerr << " lport: " << ntohs(ownEntry.laddr.sin_port); - std::cerr << " raddr: " << rs_inet_ntoa(ownEntry.raddr.sin_addr); - std::cerr << " rport: " << ntohs(ownEntry.raddr.sin_port); - std::cerr << " type: " << ownEntry.type; - std::cerr << " state: " << ownEntry.state; - std::cerr << std::endl; -#endif - -#ifdef DHT_LOGS - /* Log External Interface too */ - std::string out = "p3DhtMgr::setExternalInterface()"; - out += " laddr: " + rs_inet_ntoa(ownEntry.laddr.sin_addr); - rs_sprintf_append(out, " lport: %u", ntohs(ownEntry.laddr.sin_port)); - out += " raddr: " + rs_inet_ntoa(ownEntry.raddr.sin_addr); - rs_sprintf_append(out, " rport: %u", ntohs(ownEntry.raddr.sin_port)); - rs_sprintf_append(out, " type: %lu", ownEntry.type); - rs_sprintf_append(out, " state: %lu", ownEntry.state); - - rslog(RSL_WARNING, p3dhtzone, out); -#endif - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - checkOwnDHTKeys(); - return true; -} - - - /* add / remove peers */ -bool p3DhtMgr::findPeer(const RsPeerId& id) -{ - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ - - mDhtModifications = true; - - std::map::iterator it; - it = peers.find(id); - if (it != peers.end()) - { - /* reset some of it */ - it->second.state = DHT_PEER_INIT; - - // No point destroying a valid address!. - //it->second.type = DHT_ADDR_INVALID; - - // Reset notify - it->second.notifyPending = 0; - it->second.notifyTS = 0; - - return true; - } - - /* if they are not in the list -> add */ - dhtPeerEntry ent; - ent.id = id; - ent.state = DHT_PEER_INIT; - ent.type = DHT_ADDR_INVALID; - ent.lastTS = 0; - - ent.notifyPending = 0; - ent.notifyTS = 0; - - /* fill in hashes */ - ent.hash1 = RsUtil::HashId(id, false); - ent.hash2 = RsUtil::HashId(id, true); - - /* other fields don't matter */ - - peers[id] = ent; - - return true; -} - -bool p3DhtMgr::dropPeer(const RsPeerId& id) -{ - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ - - mDhtModifications = true; - - /* once we are connected ... don't worry about them anymore */ - std::map::iterator it; - it = peers.find(id); - if (it == peers.end()) - { - return false; - } - - /* leave it in there - just switch to off */ - it->second.state = DHT_PEER_OFF; - - return true; -} - - /* post DHT key saying we should connect */ -bool p3DhtMgr::notifyPeer(const RsPeerId& id) -{ - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::notifyPeer() " << id.toStdString() << std::endl; -#endif - - std::map::iterator it; - it = peers.find(id); - if (it == peers.end()) - { - return false; - } - /* ignore OFF peers */ - if (it->second.state == DHT_PEER_OFF) - { - return false; - } - - - rstime_t now = time(NULL); - - if (now - it->second.notifyTS < 2 * DHT_NOTIFY_PERIOD) - { - /* drop the notify (too soon) */ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::notifyPeer() TO SOON - DROPPING" << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::notifyPeer() Id: " + id.toStdString() + " TO SOON - DROPPING"); - - } -#endif - return false; - } - - it->second.notifyPending = RS_CONNECT_ACTIVE; - it->second.notifyTS = time(NULL); - - /* Trigger search if not found! */ - if (it->second.state != DHT_PEER_FOUND) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::notifyPeer() PEER NOT FOUND - Trigger search" << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::notifyPeer() Id: " + id.toStdString() + " PEER NOT FOUND - Trigger Search"); - } -#endif - it->second.lastTS = 0; - } - - mDhtModifications = true; /* no wait! */ - - return true; -} - - /* extract current peer status */ -bool p3DhtMgr::getPeerStatus(const RsPeerId &id, - struct sockaddr_storage &laddr, - struct sockaddr_storage &raddr, - uint32_t &type, uint32_t &state) -{ - RsStackMutex stack(dhtMtx); /* LOCK MUTEX */ - - std::map::iterator it; - it = peers.find(id); - - /* ignore OFF peers */ - if ((it == peers.end()) || (it->second.state == DHT_PEER_OFF)) - { - return false; - } - - laddr = it->second.laddr; - raddr = it->second.raddr; - type = it->second.type; - state = it->second.type; - - return true; -} - -/********************************* STUN HANDLING ********************************** - * add / cleanup / stun. - * - */ - - /* stun */ -bool p3DhtMgr::addStun(std::string id) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtModifications = true; - - std::list::iterator it; - it = std::find(stunIds.begin(), stunIds.end(), id); - if (it != stunIds.end()) - { - dhtMtx.unlock(); /* UNLOCK MUTEX */ - return false; - } - stunIds.push_back(id); - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return true; -} - -bool p3DhtMgr::enableStun(bool on) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtModifications = true; - - if (!on) - { - /* clear up */ - stunIds.clear(); - } - - mStunRequired = on; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return true; -} - - -void p3DhtMgr::run() -{ - /* - * - */ - - while(1) - { - checkDHTStatus(); - - -#ifdef DHT_DEBUG - status(std::cerr); -#endif - - dhtMtx.lock(); /* LOCK MUTEX */ - - uint32_t dhtState = mDhtState; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - int period = 60; /* default wait */ - switch(dhtState) - { - case DHT_STATE_INIT: -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() state = INIT -> wait" << std::endl; -#endif - period = 10; - break; - case DHT_STATE_CHECK_PEERS: -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() state = CHECK_PEERS -> do stuff" << std::endl; -#endif - checkPeerDHTKeys(); - checkStunState(); - period = DHT_MIN_PERIOD; - break; - case DHT_STATE_FIND_STUN: -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() state = FIND_STUN -> do stuff" << std::endl; -#endif - doStun(); - checkPeerDHTKeys(); /* keep on going - as we could be in this state for a while */ - checkStunState(); - period = DHT_MIN_PERIOD; - break; - case DHT_STATE_ACTIVE: - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() state = ACTIVE -> do stuff" << std::endl; -#endif - - period = checkOwnDHTKeys(); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() checkOwnDHTKeys() period: " << period << std::endl; -#endif - int tmpperiod = checkNotifyDHT(); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() checkNotifyDHT() period: " << tmpperiod << std::endl; -#endif - int tmpperiod2 = checkPeerDHTKeys(); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() checkPeerDHTKeys() period: " << tmpperiod2 << std::endl; -#endif - if (tmpperiod < period) - period = tmpperiod; - if (tmpperiod2 < period) - period = tmpperiod2; - - /* finally we need to keep stun going */ - if (checkStunState_Active()) - { - /* still more stun to do */ - period = DHT_SHORT_PERIOD; - doStun(); - } - - } - break; - default: - case DHT_STATE_OFF: -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() state = OFF -> wait" << std::endl; -#endif - period = 60; - break; - } - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() sleeping for: " << period << std::endl; -#endif - - /* Breakable sleep loop */ - - bool toBreak = false; - int waittime = 1; - int i; - for(i = 0; i < period; i += waittime) - { - if (period-i > DHT_DEFAULT_WAITTIME) - { - waittime = DHT_DEFAULT_WAITTIME; - } - else - { - waittime = period-i; - } - - dhtMtx.lock(); /* LOCK MUTEX */ - - if (mDhtModifications) - { - mDhtModifications = false; - toBreak = true; - } - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - if (toBreak) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::run() breaking sleep" << std::endl; -#endif - - break; /* speed up config modifications */ - } - -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ -#ifndef WINDOWS_SYS - sleep(waittime); -#else - Sleep(1000 * waittime); -#endif -/********************************** WINDOWS/UNIX SPECIFIC PART ******************/ - - } - } -} - - -int p3DhtMgr::checkOwnDHTKeys() -{ - int repubPeriod = 10000; - rstime_t now = time(NULL); - - /* in order of importance: - * (1) Check for Own Key publish. - * (2) Check for notification requests - * (3) Check for Peers - */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys()" << std::endl; -#endif - - dhtMtx.lock(); /* LOCK MUTEX */ - - dhtPeerEntry peer = ownEntry; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - /* publish ourselves if necessary */ - if (peer.state >= DHT_PEER_ADDR_KNOWN) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() OWN ADDR KNOWN" << std::endl; -#endif - if ((peer.state < DHT_PEER_PUBLISHED) || - (now - peer.lastTS > DHT_PUBLISH_PERIOD)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() OWN ADDR REPUB" << std::endl; -#endif - -#ifdef DHT_DEBUG - std::cerr << "PUBLISH: "; - std::cerr << " hash1: " << RsUtil::BinToHex(peer.hash1); - std::cerr << " laddr: " << rs_inet_ntoa(peer.laddr.sin_addr); - std::cerr << ":" << ntohs(peer.laddr.sin_port); - std::cerr << " raddr: " << rs_inet_ntoa(peer.raddr.sin_addr); - std::cerr << ":" << ntohs(peer.raddr.sin_port); - std::cerr << " type: " << peer.type; - std::cerr << std::endl; -#endif - -#ifdef DHT_LOGS - { - /* Log */ - std::string out = "p3DhtMgr::checkOwnDHTKeys() PUBLISH OWN ADDR:"; - out += " hash1: " + RsUtil::BinToHex(peer.hash1); - out += " laddr: " + rs_inet_ntoa(peer.laddr.sin_addr); - rs_sprintf_append(out, ":%u", ntohs(peer.laddr.sin_port)); - out += " raddr: " + rs_inet_ntoa(peer.raddr.sin_addr); - rs_sprintf_append(out, ":%u", ntohs(peer.raddr.sin_port)); - rs_sprintf_append(out, " type: %lu", peer.type); - - rslog(RSL_WARNING, p3dhtzone, out); - } -#endif - - /* publish own key */ - if (dhtPublish(peer.hash1, peer.laddr, peer.raddr, peer.type, "")) - { - dhtMtx.lock(); /* LOCK MUTEX */ - - ownEntry.lastTS = now; - ownEntry.state = DHT_PEER_PUBLISHED; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - } - - /* dhtBootstrap -> if allowed and EXT port */ - if (peer.type & RS_NET_CONN_TCP_EXTERNAL) - { - dhtMtx.lock(); /* LOCK MUTEX */ - - bool doBootstrapPub = mBootstrapAllowed; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - if (doBootstrapPub) - { - dhtBootstrap(randomBootstrapId(), peer.hash1, ""); - } - } - - /* restart immediately */ - repubPeriod = DHT_MIN_PERIOD; - return repubPeriod; - } - else - { - if (now - peer.lastTS < DHT_PUBLISH_PERIOD) - { - repubPeriod = DHT_PUBLISH_PERIOD - - (now - peer.lastTS); - } - else - { - repubPeriod = 10; - } -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() repub in: "; - std::cerr << repubPeriod << std::endl; -#endif - } - - - /* check for connect requests */ - //if ((peer.state == DHT_PEER_PUBLISHED) && - // (!(peer.type & RS_NET_CONN_TCP_EXTERNAL))) - if (peer.state == DHT_PEER_PUBLISHED) - { - if (now - peer.notifyTS >= DHT_NOTIFY_PERIOD) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() check for Notify (rep=0)"; - std::cerr << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::checkOwnDHTKeys() Checking for NOTIFY"); - } -#endif - - if (dhtSearch(peer.hash1, DHT_MODE_NOTIFY)) - { - dhtMtx.lock(); /* LOCK MUTEX */ - - ownEntry.notifyTS = now; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - } - - /* restart immediately */ - repubPeriod = DHT_MIN_PERIOD; - return repubPeriod; - } - else - { - repubPeriod = DHT_NOTIFY_PERIOD - - (now - peer.notifyTS); - if (repubPeriod < DHT_MIN_PERIOD) - { - repubPeriod = DHT_MIN_PERIOD; - } -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() check Notify in: "; - std::cerr << repubPeriod << std::endl; -#endif - } - } - else - { - if (peer.state != DHT_PEER_PUBLISHED) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() No Notify until Published"; - std::cerr << std::endl; -#endif - } - else if (peer.type & RS_NET_CONN_TCP_EXTERNAL) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() No Notify because have Ext Addr"; - std::cerr << std::endl; -#endif - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() No Notify: Unknown Reason"; - std::cerr << std::endl; -#endif - } - } - - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkOwnDHTKeys() PEER ADDR UNKNOWN" << std::endl; -#endif - repubPeriod = 10; - } - return repubPeriod; -} - - -int p3DhtMgr::checkPeerDHTKeys() -{ - /* now loop through the peers */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkPeerDHTKeys()" << std::endl; -#endif - - dhtMtx.lock(); /* LOCK MUTEX */ - - /* iterate through and find min time and suitable candidate */ - std::map::iterator it,pit; - rstime_t now = time(NULL); - uint32_t period = 0; - uint32_t repeatPeriod = 6000; - - pit = peers.end(); - rstime_t pTS = now; - - for(it = peers.begin(); it != peers.end(); it++) - { - /* ignore OFF peers */ - if (it->second.state == DHT_PEER_OFF) - { - continue; - } - - rstime_t delta = now - it->second.lastTS; - if (it->second.state < DHT_PEER_FOUND) - { - period = DHT_SEARCH_PERIOD; - } - else /* (it->second.state == DHT_PEER_FOUND) */ - { - period = DHT_CHECK_PERIOD; - } -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkPeerDHTKeys() Peer: " << it->second.id.toStdString(); - std::cerr << " Period: " << period; - std::cerr << " Delta: " << delta; - std::cerr << std::endl; -#endif - - - if ((unsigned) delta >= period) - { - if (it->second.lastTS < pTS) - { - pit = it; - pTS = it->second.lastTS; - } - repeatPeriod = DHT_MIN_PERIOD; - } - else if (period - delta < repeatPeriod) - { - repeatPeriod = period - delta; - } - } - - /* now have - peer to handle, and period to next call */ - - if (pit == peers.end()) - { - dhtMtx.unlock(); /* UNLOCK MUTEX */ - return repeatPeriod; - } - - /* update timestamp - * clear FOUND or INIT state. - * */ - - pit->second.lastTS = now; - pit->second.state = DHT_PEER_SEARCH; - - dhtPeerEntry peer = (pit->second); - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - /* now search for the peer */ - dhtSearch(peer.hash1, DHT_MODE_SEARCH); - - /* results come through callback */ - return repeatPeriod; -} - - -int p3DhtMgr::checkNotifyDHT() -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkNotifyDHT()" << std::endl; -#endif - /* now loop through the peers */ - uint32_t notifyType = 0; - dhtPeerEntry peer; - dhtPeerEntry own; - - { - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ - - /* iterate through and find min time and suitable candidate */ - std::map::iterator it; - rstime_t now = time(NULL); - int repeatPeriod = DHT_DEFAULT_PERIOD; - - /* find the first with a notify flag */ - for(it = peers.begin(); it != peers.end(); it++) - { - /* ignore OFF peers */ - if (it->second.state == DHT_PEER_OFF) - { - continue; - } - - if (it->second.notifyPending) - { - /* if very old drop it */ - if (now - it->second.notifyTS > DHT_NOTIFY_PERIOD) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkNotifyDHT() Dropping OLD Notify: "; - std::cerr << it->first << std::endl; -#endif - it->second.notifyPending = 0; - } - - if (it->second.state == DHT_PEER_FOUND) - { - notifyType = it->second.notifyPending; - break; - } - } - } - - /* now have - peer to handle */ - if (it == peers.end()) - { - return repeatPeriod; - } - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkNotifyDHT() Notify From: "; - std::cerr << it->first << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::checkNotifyDHT() Notify Request for Known Peer: " + it->first); - } -#endif - - /* update timestamp */ - it->second.notifyTS = now; - it->second.notifyPending = 0; - - peer = (it->second); - own = ownEntry; - - } /******* UNLOCK ******/ - - if (notifyType == RS_CONNECT_ACTIVE) - { - /* publish notification (publish Our Id) - * We publish the connection attempt on peers hash, - * using our alternative hash.. - * */ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkNotifyDHT() Posting Active Notify"; - std::cerr << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::checkNotifyDHT() POST DHT (Active Notify) for Peer: " + peer.id); - } -#endif - - dhtNotify(peer.hash1, own.hash2, ""); - } - - /* feedback to say we started it! */ -#ifdef P3DHTMGR_USE_LOCAL_UDP_CONN - mConnCb->peerConnectRequest(peer.id, peer.laddr,peer.laddr,peer.laddr, RS_CB_DHT, 0, 0,0); -#else - mConnCb->peerConnectRequest(peer.id, peer.raddr,peer.laddr,peer.laddr, RS_CB_DHT, 0, 0, 0); -#endif - - return DHT_MIN_PERIOD; -} - - - -int p3DhtMgr::doStun() -{ - if (stunIds.size() < 1) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::doStun() Failed -> no Ids left" << std::endl; -#endif - return 0; - } - - dhtMtx.lock(); /* LOCK MUTEX */ - - bool stunRequired = mStunRequired; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - /* now loop through the peers */ - if (!stunRequired) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::doStun() not Required" << std::endl; -#endif - return 0; - } - - /* pop the front one */ - std::string activeStunId = stunIds.front(); - - stunIds.pop_front(); - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::doStun() STUN: " << RsUtil::BinToHex(activeStunId); - std::cerr << std::endl; -#endif - - /* look it up! */ - dhtSearch(activeStunId, DHT_MODE_SEARCH); - - return 1; -} - - - -int p3DhtMgr::checkStunState() -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkStunState()" << std::endl; -#endif - dhtMtx.lock(); /* LOCK MUTEX */ - - /* now loop through the peers */ - if (!mStunRequired) - { - mDhtState = DHT_STATE_ACTIVE; - } - - if (mDhtState == DHT_STATE_CHECK_PEERS) - { - /* check that they have all be searched for */ - std::map::iterator it; - for(it = peers.begin(); it != peers.end(); it++) - { - if (it->second.state == DHT_PEER_INIT) - { - break; - } - } - - if (it == peers.end()) - { - /* we've checked all peers */ - mDhtState = DHT_STATE_FIND_STUN; - } - } - else if (mDhtState == DHT_STATE_FIND_STUN) - { - } - - /* if we run out of stun peers -> get some more */ - if (stunIds.size() < 1) - { -#ifdef DHT_DEBUG - std::cerr << "WARNING: out of Stun Peers - Fetching some more" << std::endl; -#endif - mDhtState = DHT_STATE_ACTIVE; - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - /* this is a locked function */ - getDhtBootstrapList(); - - dhtMtx.lock(); /* LOCK MUTEX */ - } - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - return 1; -} - -int p3DhtMgr::checkStunState_Active() -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkStunState_Active()" << std::endl; -#endif - dhtMtx.lock(); /* LOCK MUTEX */ - - bool stunReq = mStunRequired; - bool moreIds = ((mStunRequired) && (stunIds.size() < 1)); - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - if (moreIds) - /* if we run out of stun peers -> get some more */ - { -#ifdef DHT_DEBUG - std::cerr << "WARNING: out of Stun Peers - getting more" << std::endl; -#endif - getDhtBootstrapList(); - } - - return stunReq; -} - -bool p3DhtMgr::getDhtBootstrapList() -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::getDHTBootstrapList()" << std::endl; -#endif - dhtMtx.lock(); /* LOCK MUTEX */ - - rstime_t now = time(NULL); - if (now - mLastBootstrapListTS < DHT_MIN_BOOTSTRAP_REQ_PERIOD) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::getDHTBootstrapList() Waiting: "; - std::cerr << DHT_MIN_BOOTSTRAP_REQ_PERIOD-(now-mLastBootstrapListTS); - std::cerr << " secs" << std::endl; -#endif - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return false; - } - - mLastBootstrapListTS = now; - std::string bootId = randomBootstrapId(); - - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::getDHTBootstrapList() bootId: 0x"; - std::cerr << RsUtil::BinToHex(bootId) << std::endl; -#endif - - dhtSearch(bootId, DHT_MODE_SEARCH); - - return true; -} - - -std::string p3DhtMgr::BootstrapId(uint32_t bin) -{ - /* generate these from an equation! (makes it easy) - * Make sure that NUM_BOOTSTRAP_BINS doesn't affect ids - */ - - uint32_t id = (bin % DHT_NUM_BOOTSTRAP_BINS) * 1234; - - std::string genId; - rs_sprintf(genId, "BootstrapId%lu", id); - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::BootstrapId() generatedId: "; - std::cerr << genId << std::endl; -#endif - - /* now hash this to create a bootstrap Bin Id */ - std::string bootId = RsUtil::HashId(genId, false); - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::BootstrapId() bootId: 0x"; - std::cerr << RsUtil::BinToHex(bootId) << std::endl; -#endif - - return bootId; -} - -std::string p3DhtMgr::randomBootstrapId() -{ - uint32_t rnd = DHT_NUM_BOOTSTRAP_BINS * (rand() / (RAND_MAX + 1.0)); - - return BootstrapId(rnd); -} - - - -void p3DhtMgr::checkDHTStatus() -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - bool isActive = (mDhtState != DHT_STATE_OFF); - - bool toShutdown = false; - bool toStartup = false; - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() mDhtState: " << mDhtState << std::endl; - std::cerr << "p3DhtMgr::checkDhtStatus() mDhtOn : " << mDhtOn << std::endl; -#endif - - if ((isActive) && (!mDhtOn)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() Active & !mDhtOn -> toShutdown" << std::endl; -#endif - toShutdown = true; - } - - if ((!isActive) && (mDhtOn)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() !Active & mDhtOn -> toStartup" << std::endl; -#endif - toStartup = true; - } - - /* restart if it has shutdown */ - if (isActive && mDhtOn) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() Active & mDhtOn" << std::endl; -#endif - if (dhtActive()) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() dhtActive() = true" << std::endl; -#endif - if (mDhtState == DHT_STATE_INIT) - { - mDhtState = DHT_STATE_CHECK_PEERS; -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() Switching to CHECK PEERS" << std::endl; -#endif - } - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() dhtActive() = false" << std::endl; -#endif - if (mDhtActiveTS - time(NULL) > DHT_RESTART_PERIOD) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() restart Period..." << std::endl; -#endif - toShutdown = true; - toStartup = true; - } - } - } - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - if (toShutdown) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() toShutdown = true -> shutdown()" << std::endl; -#endif - if (dhtShutdown()) - { - clearDhtData(); - - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtState = DHT_STATE_OFF; -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() mDhtState -> OFF" << std::endl; -#endif - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - } - } - - if (toStartup) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() toStartup = true -> dhtInit()" << std::endl; -#endif - if (dhtInit()) - { - - dhtMtx.lock(); /* LOCK MUTEX */ - - mDhtState = DHT_STATE_INIT; - mDhtActiveTS = time(NULL); - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::checkDhtStatus() mDhtState -> INIT" << std::endl; -#endif - - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - } - } -} - -void p3DhtMgr::clearDhtData() -{ - std::cerr << "p3DhtMgr::clearDhtData() DUMMY FN" << std::endl; -} - - -/****************************** REAL DHT INTERFACE ********************************* - * publish/search/result. - * - * dummy implementation for testing. - */ - -int p3DhtMgr::status(std::ostream &out) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - - out << "p3DhtMgr::status() ************************************" << std::endl; - out << "mDhtState: " << mDhtState << std::endl; - out << "mDhtOn : " << mDhtOn << std::endl; - out << "dhtActive: " << dhtActive() << std::endl; - - /* now own state */ - out << "OWN DETAILS -------------------------------------------" << std::endl; - printDhtPeerEntry(&ownEntry, out); - out << "OWN DETAILS END----------------------------------------" << std::endl; - - /* now peers states */ - std::map::iterator it; - out << "PEER DETAILS ------------------------------------------" << std::endl; - for(it = peers.begin(); it != peers.end(); it++) - { - printDhtPeerEntry(&(it->second), out); - } - out << "PEER DETAILS END---------------------------------------" << std::endl; - - /* now stun states */ - out << "STUN DETAILS ------------------------------------------" << std::endl; - out << "Available Stun Ids: " << stunIds.size() << std::endl; - out << "STUN DETAILS END---------------------------------------" << std::endl; - - - out << "p3DhtMgr::status() END ********************************" << std::endl; - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - return 0; -} - - - - - -bool p3DhtMgr::dhtInit() -{ - std::cerr << "p3DhtMgr::dhtInit() DUMMY FN" << std::endl; - return true; -} - -bool p3DhtMgr::dhtShutdown() -{ - std::cerr << "p3DhtMgr::dhtShutdown() DUMMY FN" << std::endl; - return true; -} - -bool p3DhtMgr::dhtActive() -{ - std::cerr << "p3DhtMgr::dhtActive() DUMMY FN" << std::endl; - return true; -} - -bool p3DhtMgr::publishDHT(std::string /*key*/, std::string /*value*/, uint32_t /*ttl*/) -{ - std::cerr << "p3DhtMgr::publishDHT() DUMMY FN" << std::endl; - return false; -} - -bool p3DhtMgr::searchDHT(std::string /*idhash*/) -{ - std::cerr << "p3DhtMgr::searchDHT() DUMMY FN" << std::endl; - return false; -} - - - - -/****************************** INTERMEDIATE DHT INTERFACE ********************************* - * publish/search/result. - * - * Take the 'real' parameters and create the key/value parameters for the real dht. - */ - - -bool p3DhtMgr::dhtPublish(std::string idhash, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, - uint32_t type, std::string sign) -{ - /* remove unused parameter warnings */ - (void) sign; - - /* ... goes and searches */ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtPublish()" << std::endl; - - std::cerr << "PUBLISHing: idhash: " << RsUtil::BinToHex(idhash); - std::cerr << " laddr: " << rs_inet_ntoa(laddr.sin_addr); - std::cerr << ":" << ntohs(laddr.sin_port); - std::cerr << " raddr: " << rs_inet_ntoa(raddr.sin_addr); - std::cerr << ":" << ntohs(raddr.sin_port); - std::cerr << " type: " << type; - std::cerr << " sign: " << sign; - std::cerr << std::endl; -#endif - - /* Create a Value from addresses and type */ - /* to store the ip address and flags */ - - std::string value; - rs_sprintf(value, "RSDHT:%02d: ", DHT_MODE_SEARCH); - rs_sprintf_append(value, "IPL=%s:%u, ", rs_inet_ntoa(laddr.sin_addr).c_str(), ntohs(laddr.sin_port)); - rs_sprintf_append(value, "IPE=%s:%u, ", rs_inet_ntoa(raddr.sin_addr).c_str(), ntohs(raddr.sin_port)); - rs_sprintf_append(value, "type=%04x, ", type); - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtPublish()" << std::endl; - - std::cerr << "PUBLISH: key: " << RsUtil::BinToHex(idhash); - std::cerr << " value: " << value; - std::cerr << std::endl; -#endif - - /* call to the real DHT */ - return publishDHT(idhash, value, DHT_TTL_PUBLISH); -} - -bool p3DhtMgr::dhtNotify(std::string idhash, std::string ownIdHash, std::string /*sign*/) -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtNotify()" << std::endl; -#endif - - std::string value; - rs_sprintf(value, "RSDHT:%02d:%s", DHT_MODE_NOTIFY, ownIdHash.c_str()); - - /* call to the real DHT */ - return publishDHT(idhash, value, DHT_TTL_NOTIFY); -} - -bool p3DhtMgr::dhtSearch(std::string idhash, uint32_t /*mode*/) -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtSearch()" << std::endl; -#endif - - /* call to the real DHT */ - return searchDHT(idhash); -} - - -bool p3DhtMgr::dhtBootstrap(std::string storehash, std::string ownIdHash, std::string /*sign*/) -{ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtBootstrap()" << std::endl; -#endif - - std::string value; - rs_sprintf(value, "RSDHT:%02d:%s", DHT_MODE_BOOTSTRAP, ownIdHash.c_str()); - - /* call to the real DHT */ - return publishDHT(storehash, value, DHT_TTL_BOOTSTRAP); -} - - -/****************************** DHT FEEDBACK INTERFACE ********************************* - * Two functions... - * (1) The interpretation function. - * (2) callback handling. - * - */ - -bool p3DhtMgr::resultDHT(std::string key, std::string value) -{ - /* so .... two possibilities. - * - * RSDHT:01: IPL ... IPE ... TYPE ... (advertising) - * RSDHT:02: HASH (connect request) - * - */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() key: 0x" << RsUtil::BinToHex(key); - std::cerr << " value: 0x" << RsUtil::BinToHex(value) << std::endl; -#endif - - /* variables for dhtResult() call */ - struct sockaddr_in laddr; - struct sockaddr_in raddr; - std::string sign; - - - int32_t reqType; - uint32_t loc; - if (1 > sscanf(value.c_str(), "RSDHT:%d: %n", &reqType, &loc)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() Not RSDHT msg -> discarding" << std::endl; -#endif - /* failed */ - return false; - } - - - dhtMtx.lock(); /* LOCK MUTEX */ - std::string ownhash = ownEntry.hash1; - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - switch(reqType) - { - case DHT_MODE_NOTIFY: - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() NOTIFY msg" << std::endl; -#endif - - if (ownhash != key) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() NOTIFY msg not for us -> discarding" << std::endl; -#endif - return false; - } - - /* get the hash */ - std::string notifyHash = value.substr(loc); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() NOTIFY msg HASH:-> 0x" << RsUtil::BinToHex(notifyHash) << "<-" << std::endl; -#endif - /* call out */ - dhtResultNotify(notifyHash); - - break; - } - - case DHT_MODE_SEARCH: - { - - if (ownhash == key) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() SEARCH msg is OWN PUBLISH -> discarding" << std::endl; -#endif - return false; - } - - uint32_t a1,b1,c1,d1,e1; - uint32_t a2,b2,c2,d2,e2; - uint32_t flags; - - if (sscanf(&((value.c_str())[loc]), " IPL=%d.%d.%d.%d:%d, IPE=%d.%d.%d.%d:%d, type=%x", - &a1, &b1, &c1, &d1, &e1, &a2, &b2, &c2, &d2, &e2, &flags) != 11) - { - /* failed to scan */ -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() SEARCH parse failed of:" << (&((value.c_str())[loc])); - std::cerr << std::endl; -#endif - return false; - } - - std::string out1; - rs_sprintf(out1, "%d.%d.%d.%d", a1, b1, c1, d1); - inet_aton(out1.c_str(), &(laddr.sin_addr)); - laddr.sin_port = htons(e1); - laddr.sin_family = AF_INET; - - std::string out2; - rs_sprintf(out2, "%d.%d.%d.%d", a2, b2, c2, d2); - inet_aton(out2.c_str(), &(raddr.sin_addr)); - raddr.sin_port = htons(e2); - raddr.sin_family = AF_INET; - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() SEARCH laddr: " << out1 << ":" << e1; - std::cerr << " raddr: " << out2 << ":" << e2; - std::cerr << " flags: " << flags; - std::cerr << std::endl; -#endif - - return dhtResultSearch(key, laddr, raddr, flags, sign); - - break; - } - - case DHT_MODE_BOOTSTRAP: - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() BOOTSTRAP msg" << std::endl; -#endif - - /* get the hash */ - std::string bootId = value.substr(loc); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::resultDHT() BOOTSTRAP msg, IdHash:->" << RsUtil::BinToHex(bootId) << "<-" << std::endl; -#endif - /* call out */ - dhtResultBootstrap(bootId); - - break; - } - - default: - - return false; - break; - } - - return false; -} - - - - -bool p3DhtMgr::dhtResultBootstrap(std::string idhash) -{ - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResultBootstrap() from idhash: "; - std::cerr << RsUtil::BinToHex(idhash) << std::endl; -#endif - - /* Temp - to avoid duplication during testing */ - if (stunIds.end() == std::find(stunIds.begin(), stunIds.end(), idhash)) - { - stunIds.push_back(idhash); -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResultBootstrap() adding to StunList"; - std::cerr << std::endl; -#endif - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResultBootstrap() DUPLICATE not adding to List"; - std::cerr << std::endl; -#endif - } - - - return true; -} - - - - -bool p3DhtMgr::dhtResultNotify(std::string idhash) -{ - RsStackMutex stack(dhtMtx); /***** LOCK MUTEX *****/ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResultNotify() from idhash: "; - std::cerr << RsUtil::BinToHex(idhash) << std::endl; -#endif - std::map::iterator it; - rstime_t now = time(NULL); - - /* if notify - we must match on the second hash */ - for(it = peers.begin(); (it != peers.end()) && ((it->second).hash2 != idhash); it++) ; - - /* update data */ - /* ignore OFF peers */ - if ((it != peers.end()) && (it->second.state != DHT_PEER_OFF)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResult() NOTIFY for id: " << it->first << std::endl; -#endif -#ifdef DHT_LOGS - { - /* Log */ - rslog(RSL_WARNING, p3dhtzone, "p3DhtMgr::dhtResultNotify() NOTIFY from Id: " + it->first); - } -#endif - - /* delay callback -> if they are not found */ - it->second.notifyTS = now; - it->second.notifyPending = RS_CONNECT_PASSIVE; - mDhtModifications = true; /* no wait! */ - - if (it->second.state != DHT_PEER_FOUND) - { - /* flag for immediate search */ - it->second.lastTS = 0; - } - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResult() unknown NOTIFY "; - std::cerr << RsUtil::BinToHex(idhash) << std::endl; -#endif - } - - return true; -} - - -bool p3DhtMgr::dhtResultSearch(std::string idhash, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, - uint32_t type, std::string /*sign*/) -{ - dhtMtx.lock(); /* LOCK MUTEX */ - -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResultSearch() for idhash: "; - std::cerr << RsUtil::BinToHex(idhash) << std::endl; -#endif - std::map::iterator it; - bool doCb = false; - bool doStun = false; - uint32_t stunFlags = 0; - rstime_t now = time(NULL); - - dhtPeerEntry ent; - - /* if search - we must match on the second hash */ - for(it = peers.begin(); (it != peers.end()) && ((it->second).hash1 != idhash); it++) ; - - /* update data */ - /* ignore OFF peers */ - if ((it != peers.end()) && (it->second.state != DHT_PEER_OFF)) - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResult() SEARCH for id: " << it->first << std::endl; -#endif - it->second.lastTS = now; - -#ifdef DHT_LOGS - { - /* Log */ - std::string out ="p3DhtMgr::dhtSearchResult() for Id: " + it->first; - rs_sprintf_append(out, " laddr: %s:%u", rs_inet_ntoa(laddr.sin_addr).c_str(), ntohs(laddr.sin_port)); - rs_sprintf_append(out, " raddr: %s:%u", rs_inet_ntoa(raddr.sin_addr).c_str(), ntohs(raddr.sin_port)); - rs_sprintf_append(out, " type: %lu", ownEntry.type); - - rslog(RSL_WARNING, p3dhtzone, out); - } -#endif - - /* update info .... always */ - it->second.state = DHT_PEER_FOUND; - it->second.laddr = laddr; - it->second.raddr = raddr; - it->second.type = type; - - /* Do callback all the time */ - ent = it->second; - doCb = true; - - if (it->second.notifyPending) - { - /* no wait if we have pendingNotification */ - mDhtModifications = true; - } - - /* do Stun always */ - doStun = true; - stunFlags = RS_STUN_FRIEND | RS_STUN_ONLINE; - } - else - { -#ifdef DHT_DEBUG - std::cerr << "p3DhtMgr::dhtResult() SEARCH(stun) for idhash: "; - std::cerr << RsUtil::BinToHex(idhash) << std::endl; -#endif - /* stun result? */ - doStun = true; - stunFlags = RS_STUN_ONLINE; - } - - dhtMtx.unlock(); /* UNLOCK MUTEX */ - - if (doCb) - { - pqiIpAddrSet addrs; - - pqiIpAddress laddr; - laddr.mAddr = ent.laddr; - laddr.mSeenTime = time(NULL); - laddr.mSrc = RS_CB_DHT; - - addrs.updateLocalAddrs(laddr); - - pqiIpAddress eaddr; - eaddr.mAddr = ent.raddr; - eaddr.mSeenTime = time(NULL); - eaddr.mSrc = RS_CB_DHT; - - addrs.updateExtAddrs(eaddr); - - mConnCb->peerStatus(ent.id, addrs, ent.type, 0, RS_CB_DHT); - } - - if (doStun) - { - //mConnCb->stunStatus(idhash, raddr, type, stunFlags); - } - - return true; -} - - - -/******************************** AUX FUNCTIONS ********************************** - * - */ - -void printDhtPeerEntry(dhtPeerEntry *ent, std::ostream &out) -{ - - out << "DhtEntry: ID: " << ent->id.toStdString(); - out << " State: " << ent->state; - out << " lastTS: " << ent->lastTS; - out << " notifyPending: " << ent->notifyPending; - out << " notifyTS: " << ent->notifyTS; - out << std::endl; - out << " laddr: " << rs_inet_ntoa(ent->laddr.sin_addr) << ":" << ntohs(ent->laddr.sin_port); - out << " raddr: " << rs_inet_ntoa(ent->raddr.sin_addr) << ":" << ntohs(ent->raddr.sin_port); - out << " type: " << ent->type; - out << std::endl; - out << " hash1: " << RsUtil::BinToHex(ent->hash1); - out << std::endl; - out << " hash2: " << RsUtil::BinToHex(ent->hash2); - out << std::endl; - return; -} - - diff --git a/libretroshare/src/pqi/p3dhtmgr.h b/libretroshare/src/pqi/p3dhtmgr.h deleted file mode 100644 index b0b2fb7b1..000000000 --- a/libretroshare/src/pqi/p3dhtmgr.h +++ /dev/null @@ -1,246 +0,0 @@ -/******************************************************************************* - * libretroshare/src/pqi: p3dhtmgr.h * - * * - * libretroshare: retroshare core library * - * * - * Copyright 2004-2008 by Robert Fernie. * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU Lesser General Public License as * - * published by the Free Software Foundation, either version 3 of the * - * License, or (at your option) any later version. * - * * - * This program 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 Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public License * - * along with this program. If not, see . * - * * - *******************************************************************************/ - -#ifndef MRK_P3_DHT_MANAGER_HEADER -#define MRK_P3_DHT_MANAGER_HEADER - -/* Interface class for DHT data */ - -#include -#include -#include "pqi/pqinetwork.h" - -#include "util/rsthreads.h" -#include "pqi/pqimonitor.h" - -#include "pqi/pqiassist.h" - -/* All other #defs are in .cc */ -#define DHT_ADDR_INVALID 0xff -#define DHT_ADDR_TCP 0x01 -#define DHT_ADDR_UDP 0x02 - - -/* for DHT peer STATE */ -#define DHT_PEER_OFF 0 -#define DHT_PEER_INIT 1 -#define DHT_PEER_SEARCH 2 -#define DHT_PEER_FOUND 3 - -/* for DHT peer STATE (ownEntry) */ -#define DHT_PEER_ADDR_KNOWN 4 -#define DHT_PEER_PUBLISHED 5 - -/* Interface with Real DHT Implementation */ -#define DHT_MODE_SEARCH 1 -#define DHT_MODE_PUBLISH 1 -#define DHT_MODE_NOTIFY 2 -#define DHT_MODE_BOOTSTRAP 3 - - -/* TIMEOUTS: Reference Values are set here... */ - -#define DHT_SEARCH_PERIOD 1800 /* PeerKeys: if we haven't found them: 30 min */ -#define DHT_CHECK_PERIOD 1800 /* PeerKeys: re-lookup peer: 30 min */ -#define DHT_PUBLISH_PERIOD 1800 /* OwnKey: 30 min */ -#define DHT_NOTIFY_PERIOD 300 /* 5 min - Notify Check period */ - -/* TTLs for DHTs posts */ -#define DHT_TTL_PUBLISH (DHT_PUBLISH_PERIOD + 120) // for a little overlap. -#define DHT_TTL_NOTIFY (DHT_NOTIFY_PERIOD + 60) // for time to find it... -#define DHT_TTL_BOOTSTRAP (DHT_PUBLISH_PERIOD) // To start with. - -class dhtPeerEntry -{ - public: - dhtPeerEntry(); - - RsPeerId id; - uint32_t state; - rstime_t lastTS; - - uint32_t notifyPending; - rstime_t notifyTS; - - struct sockaddr_in laddr, raddr; - uint32_t type; /* ADDR_TYPE as defined above */ - - std::string hash1; /* SHA1 Hash of id */ - std::string hash2; /* SHA1 Hash of reverse Id */ -}; - -class p3DhtMgr: public pqiNetAssistConnect, public RsThread -{ - /* - */ - public: - p3DhtMgr(RsPeerId id, pqiConnectCb *cb); - - /********** External DHT Interface ************************ - * These Functions are the external interface - * for the DHT, and must be non-blocking and return quickly - */ - - /* OVERLOADED From pqiNetAssistConnect. */ - -virtual void enable(bool on); -virtual void shutdown(); -virtual void restart(); - -virtual bool getEnabled(); /* on */ -virtual bool getActive(); /* actually working */ - -virtual void setBootstrapAllowed(bool on); -virtual bool getBootstrapAllowed(); - - /* set key data */ -virtual bool setExternalInterface(struct sockaddr_in laddr, - struct sockaddr_in raddr, uint32_t type); - - /* add / remove peers */ -virtual bool findPeer(const RsPeerId& id); -virtual bool dropPeer(const RsPeerId& id); - - /* post DHT key saying we should connect (callback when done) */ -virtual bool notifyPeer(const RsPeerId& id); - - /* extract current peer status */ -virtual bool getPeerStatus(const RsPeerId& id, - struct sockaddr_storage &laddr, struct sockaddr_storage &raddr, - uint32_t &type, uint32_t &mode); - - /* stun */ -virtual bool enableStun(bool on); -virtual bool addStun(std::string id); - //doneStun(); - - /********** Higher Level DHT Work Functions ************************ - * These functions translate from the strings/addresss to - * key/value pairs. - */ - public: - - /* results from DHT proper */ -virtual bool dhtResultNotify(std::string id); -virtual bool dhtResultSearch(std::string id, - struct sockaddr_in &laddr, struct sockaddr_in &raddr, - uint32_t type, std::string sign); - -virtual bool dhtResultBootstrap(std::string idhash); - - protected: - - /* can block briefly (called only from thread) */ -virtual bool dhtPublish(std::string idhash, - struct sockaddr_in &laddr, - struct sockaddr_in &raddr, - uint32_t type, std::string sign); - -virtual bool dhtNotify(std::string idhash, std::string ownIdHash, - std::string sign); - -virtual bool dhtSearch(std::string idhash, uint32_t mode); - -virtual bool dhtBootstrap(std::string idhash, std::string ownIdHash, - std::string sign); /* to publish bootstrap */ - - - - /********** Actual DHT Work Functions ************************ - * These involve a very simple LOW-LEVEL interface ... - * - * publish - * search - * result - * - */ - - public: - - /* Feedback callback (handled here) */ -virtual bool resultDHT(std::string key, std::string value); - - protected: - -virtual bool dhtInit(); -virtual bool dhtShutdown(); -virtual bool dhtActive(); -virtual int status(std::ostream &out); - -virtual bool publishDHT(std::string key, std::string value, uint32_t ttl); -virtual bool searchDHT(std::string key); - - - - /********** Internal DHT Threading ************************ - * - */ - - public: - -virtual void run(); - - private: - - /* search scheduling */ -void checkDHTStatus(); -int checkStunState(); -int checkStunState_Active(); /* when in active state */ -int doStun(); -int checkPeerDHTKeys(); -int checkOwnDHTKeys(); -int checkNotifyDHT(); - -void clearDhtData(); - - /* IP Bootstrap */ -bool getDhtBootstrapList(); -std::string BootstrapId(uint32_t bin); -std::string randomBootstrapId(); - - /* other feedback through callback */ - // use pqiNetAssistConnect.. version pqiConnectCb *connCb; - - /* protected by Mutex */ - RsMutex dhtMtx; - - bool mDhtOn; /* User desired state */ - bool mDhtModifications; /* any user requests? */ - - dhtPeerEntry ownEntry; - rstime_t ownNotifyTS; - std::map peers; - - std::list stunIds; - bool mStunRequired; - - uint32_t mDhtState; - rstime_t mDhtActiveTS; - - bool mBootstrapAllowed; - rstime_t mLastBootstrapListTS; -}; - - -#endif // MRK_P3_DHT_MANAGER_HEADER - - diff --git a/libretroshare/src/pqi/p3linkmgr.cc b/libretroshare/src/pqi/p3linkmgr.cc index fec5b8e5f..100a16cad 100644 --- a/libretroshare/src/pqi/p3linkmgr.cc +++ b/libretroshare/src/pqi/p3linkmgr.cc @@ -28,7 +28,6 @@ #include "rsserver/p3face.h" #include "pqi/authssl.h" -#include "pqi/p3dhtmgr.h" // Only need it for constants. #include "tcponudp/tou.h" #include "util/extaddrfinder.h" #include "util/dnsresolver.h" diff --git a/libretroshare/src/pqi/p3netmgr.cc b/libretroshare/src/pqi/p3netmgr.cc index 4a539701c..8eebd4c1e 100644 --- a/libretroshare/src/pqi/p3netmgr.cc +++ b/libretroshare/src/pqi/p3netmgr.cc @@ -79,16 +79,12 @@ const uint32_t MAX_UPNP_COMPLETE = 600; /* 10 min... seems to take a while */ // #define NETMGR_DEBUG_TICK 1 // #define NETMGR_DEBUG_STATEBOX 1 -pqiNetStatus::pqiNetStatus() - :mLocalAddrOk(false), mExtAddrOk(false), mExtAddrStableOk(false), - mUpnpOk(false), mDhtOk(false), mResetReq(false) +pqiNetStatus::pqiNetStatus() : + mExtAddrOk(false), mExtAddrStableOk(false), mUpnpOk(false), mDhtOk(false), + mDhtNetworkSize(0), mDhtRsNetworkSize(0), mResetReq(false) { - mDhtNetworkSize = 0; - mDhtRsNetworkSize = 0; - sockaddr_storage_clear(mLocalAddr); sockaddr_storage_clear(mExtAddr); - return; } @@ -96,7 +92,6 @@ pqiNetStatus::pqiNetStatus() void pqiNetStatus::print(std::ostream &out) { out << "pqiNetStatus: "; - out << "mLocalAddrOk: " << mLocalAddrOk; out << " mExtAddrOk: " << mExtAddrOk; out << " mExtAddrStableOk: " << mExtAddrStableOk; out << std::endl; @@ -108,14 +103,13 @@ void pqiNetStatus::print(std::ostream &out) out << std::endl; out << "mLocalAddr: " << sockaddr_storage_tostring(mLocalAddr) << " "; out << "mExtAddr: " << sockaddr_storage_tostring(mExtAddr) << " "; - out << " NetOk: " << NetOk(); out << std::endl; } -p3NetMgrIMPL::p3NetMgrIMPL() - :mPeerMgr(NULL), mLinkMgr(NULL), mNetMtx("p3NetMgr"), - mNetStatus(RS_NET_UNKNOWN), mStatusChanged(false) +p3NetMgrIMPL::p3NetMgrIMPL() : mPeerMgr(nullptr), mLinkMgr(nullptr), + mNetMtx("p3NetMgr"), mNetStatus(RS_NET_UNKNOWN), mStatusChanged(false), + mDoNotNetCheckUntilTs(0) { { @@ -130,7 +124,6 @@ p3NetMgrIMPL::p3NetMgrIMPL() mNetFlags = pqiNetStatus(); mOldNetFlags = pqiNetStatus(); - mLastSlowTickTime = 0; mOldNatType = RSNET_NATTYPE_UNKNOWN; mOldNatHole = RSNET_NATHOLE_UNKNOWN; sockaddr_storage_clear(mLocalAddr); @@ -415,47 +408,37 @@ void p3NetMgrIMPL::netStartup() void p3NetMgrIMPL::tick() { - rstime_t now = time(NULL); - bool doSlowTick = false; + rstime_t now = time(nullptr); + rstime_t dontCheckNetUntil; + { RS_STACK_MUTEX(mNetMtx); dontCheckNetUntil = mDoNotNetCheckUntilTs; } + + if(now >= dontCheckNetUntil) netStatusTick(); + + uint32_t netStatus; { RS_STACK_MUTEX(mNetMtx); netStatus = mNetStatus; } + switch (netStatus) { - RsStackMutex stack(mNetMtx); /************** LOCK MUTEX ***************/ - if (now > mLastSlowTickTime) + case RS_NET_LOOPBACK: + if(dontCheckNetUntil <= now) { - mLastSlowTickTime = now; - doSlowTick = true; + RS_STACK_MUTEX(mNetMtx); + mDoNotNetCheckUntilTs = now + 30; } - } - - if (doSlowTick) - { - slowTick(); - } -} - - -void p3NetMgrIMPL::slowTick() -{ - netTick(); - netAssistTick(); - updateNetStateBox_temporal(); - + break; + default: + netAssistTick(); + updateNetStateBox_temporal(); #ifdef RS_USE_DHT_STUNNER - if (mDhtStunner) - { - mDhtStunner->tick(); - } - - if (mProxyStunner) - { - mProxyStunner->tick(); - } + if (mDhtStunner) mDhtStunner->tick(); + if (mProxyStunner) mProxyStunner->tick(); #endif // RS_USE_DHT_STUNNER + break; + } } #define STARTUP_DELAY 5 -void p3NetMgrIMPL::netTick() +void p3NetMgrIMPL::netStatusTick() { #ifdef NETMGR_DEBUG_TICK @@ -986,10 +969,9 @@ bool p3NetMgrIMPL::checkNetAddress() if (mNetMode & RS_NET_MODE_TRY_LOOPBACK) { -#if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET) - std::cerr << "p3NetMgrIMPL::checkNetAddress() LOOPBACK ... forcing to 127.0.0.1"; - std::cerr << std::endl; -#endif + RsInfo() << __PRETTY_FUNCTION__ <<" network mode set to LOOPBACK," + << " forcing address to 127.0.0.1" << std::endl; + sockaddr_storage_ipv4_aton(prefAddr, "127.0.0.1"); validAddr = true; } @@ -1034,137 +1016,77 @@ bool p3NetMgrIMPL::checkNetAddress() break; } } + + /* If no satisfactory local address has been found yet relax and + * accept also loopback addresses */ + if(!validAddr) for (auto it = addrs.begin(); it!=addrs.end(); ++it) + { + sockaddr_storage& addr(*it); + if( sockaddr_storage_isValidNet(addr) && + sockaddr_storage_ipv6_to_ipv4(addr) ) + { + prefAddr = addr; + validAddr = true; + break; + } + } } } if (!validAddr) { - Dbg3() << __PRETTY_FUNCTION__ << " no valid local network address found" - << std::endl; + RsErr() << __PRETTY_FUNCTION__ << " no valid local network address " + <<" found. Report to developers." << std::endl; + print_stacktrace(); + return false; } - /* check addresses */ - - { - RS_STACK_MUTEX(mNetMtx); + { RS_STACK_MUTEX(mNetMtx); sockaddr_storage_copy(mLocalAddr, oldAddr); addrChanged = !sockaddr_storage_sameip(prefAddr, mLocalAddr); -#ifdef NETMGR_DEBUG_TICK - std::cerr << "p3NetMgrIMPL::checkNetAddress()"; - std::cerr << std::endl; - std::cerr << "Current Local: " << sockaddr_storage_tostring(mLocalAddr); - std::cerr << std::endl; - std::cerr << "Current Preferred: " << sockaddr_storage_iptostring(prefAddr); - std::cerr << std::endl; -#endif - -#ifdef NETMGR_DEBUG_RESET - if (addrChanged) - { - std::cerr << "p3NetMgrIMPL::checkNetAddress() Address Changed!"; - std::cerr << std::endl; - std::cerr << "Current Local: " << sockaddr_storage_tostring(mLocalAddr); - std::cerr << std::endl; - std::cerr << "Current Preferred: " << sockaddr_storage_iptostring(prefAddr); - std::cerr << std::endl; - } -#endif - // update address. sockaddr_storage_copyip(mLocalAddr, prefAddr); sockaddr_storage_copy(mLocalAddr, mNetFlags.mLocalAddr); if(sockaddr_storage_isLoopbackNet(mLocalAddr)) - { -#ifdef NETMGR_DEBUG - std::cerr << "p3NetMgrIMPL::checkNetAddress() laddr: Loopback" << std::endl; -#endif - mNetFlags.mLocalAddrOk = false; mNetStatus = RS_NET_LOOPBACK; - } - else if (!sockaddr_storage_isValidNet(mLocalAddr)) - { -#ifdef NETMGR_DEBUG - std::cerr << "p3NetMgrIMPL::checkNetAddress() laddr: invalid" << std::endl; -#endif - mNetFlags.mLocalAddrOk = false; - } - else - { -#ifdef NETMGR_DEBUG_TICK - std::cerr << "p3NetMgrIMPL::checkNetAddress() laddr okay" << std::endl; -#endif - mNetFlags.mLocalAddrOk = true; - } - - int port = sockaddr_storage_port(mLocalAddr); - if ((port < PQI_MIN_PORT) || (port > PQI_MAX_PORT)) + // Check if local port is valid, reset it if not + if (!sockaddr_storage_port(mLocalAddr)) { -#ifdef NETMGR_DEBUG - std::cerr << "p3NetMgrIMPL::checkNetAddress() Correcting Port to DEFAULT" << std::endl; -#endif - uint16_t new_port = htons(PQI_MIN_PORT_RNG + (RSRandom::random_u32() % (PQI_MAX_PORT - PQI_MIN_PORT_RNG))); - sockaddr_storage_setport(mLocalAddr, new_port); + /* Using same port as external may make some NAT happier */ + uint16_t port = sockaddr_storage_port(mExtAddr); + /* Avoid to automatically set a local port to a reserved one < 1024 + * that needs special permissions or root access. + * This do not impede the user to set a reserved port manually, + * which make sense in some cases. */ + while (port < 1025) + port = static_cast(RsRandom::random_u32()); + + sockaddr_storage_setport(mLocalAddr, htons(port)); addrChanged = true; + + RsWarn() << __PRETTY_FUNCTION__ << " local port was 0, corrected " + <<"to: " << port << std::endl; } - -#if DEAD_CODE - /* Enabling this piece of code breaks setup where an additional BOFH - * overlooked port like 80 or 443 is manually forwarded to RetroShare to - * avoid restrictive firewals. - * In the case of a real mismatch, it is not really problematic, as our - * peers would get and then attempt to connect also to the right port. - */ - - /* if localaddr == serveraddr, then ensure that the ports - * are the same (modify server)... this mismatch can - * occur when the local port is changed.... - */ - if ( sockaddr_storage_sameip(mLocalAddr, mExtAddr) - && sockaddr_storage_port(mLocalAddr) != sockaddr_storage_port(mExtAddr) ) - { -#ifdef NETMGR_DEBUG_RESET - std::cerr << __PRETTY_FUNCTION__ << " local and external ports are" - << " not the same. Setting external port to " - << sockaddr_storage_port(mLocalAddr) << std::endl; -#endif - sockaddr_storage_setport(mExtAddr, sockaddr_storage_port(mLocalAddr)); - addrChanged = true; - } -#endif // DEAD_CODE - -#ifdef NETMGR_DEBUG_TICK - std::cerr << __PRETTY_FUNCTION__ << " Final Local Address: " - << sockaddr_storage_tostring(mLocalAddr) << std::endl; -#endif - - } + } // RS_STACK_MUTEX(mNetMtx); if (addrChanged) { -#ifdef NETMGR_DEBUG_RESET - std::cerr << "p3NetMgrIMPL::checkNetAddress() local address changed, resetting network." << std::endl; - std::cerr << std::endl; -#endif + RsInfo() << __PRETTY_FUNCTION__ << " local address changed, resetting" + <<" network." << std::endl; - if (mPeerMgr) - { - mPeerMgr->UpdateOwnAddress(mLocalAddr, mExtAddr); - } - - std::cerr << __PRETTY_FUNCTION__ - << " local address changed, resetting network" << std::endl; + if (mPeerMgr) mPeerMgr->UpdateOwnAddress(mLocalAddr, mExtAddr); netReset(); } - return 1; + return true; } @@ -2051,3 +1973,6 @@ void p3NetMgrIMPL::updateNetStateBox_reset() } p3NetMgr::~p3NetMgr() = default; +pqiNetAssist::~pqiNetAssist() = default; +pqiNetAssistPeerShare::~pqiNetAssistPeerShare() = default; +pqiNetAssistConnect::~pqiNetAssistConnect() = default; diff --git a/libretroshare/src/pqi/p3netmgr.h b/libretroshare/src/pqi/p3netmgr.h index cf2705d60..f7775757d 100644 --- a/libretroshare/src/pqi/p3netmgr.h +++ b/libretroshare/src/pqi/p3netmgr.h @@ -44,13 +44,10 @@ class DNSResolver ; -class pqiNetStatus +struct pqiNetStatus { - public: - pqiNetStatus(); - bool mLocalAddrOk; // Local address is not loopback. bool mExtAddrOk; // have external address. bool mExtAddrStableOk; // stable external address. bool mUpnpOk; // upnp is ok. @@ -65,11 +62,6 @@ class pqiNetStatus bool mResetReq; // Not Used yet!. void print(std::ostream &out); - - bool NetOk() // minimum to believe network is okay.` - { - return (mLocalAddrOk && mExtAddrOk); - } }; class p3PeerMgr; @@ -211,11 +203,6 @@ void addNetListener(pqiNetListener *listener); // SHOULD MAKE THIS PROTECTED. bool checkNetAddress(); /* check our address is sensible */ -protected: - -void slowTick(); - - protected: /****************** Internal Interface *******************/ bool enableNetAssistFirewall(bool on); @@ -248,7 +235,7 @@ bool netAssistAttach(bool on); void netReset(); void statusTick(); -void netTick(); +void netStatusTick(); void netStartup(); /* startup the bits */ @@ -335,7 +322,7 @@ void netStatusReset_locked(); // Improved NetStatusBox, which uses the Stunners! pqiNetStateBox mNetStateBox; - rstime_t mLastSlowTickTime; + rstime_t mDoNotNetCheckUntilTs; uint32_t mOldNatType; uint32_t mOldNatHole; diff --git a/libretroshare/src/pqi/p3peermgr.cc b/libretroshare/src/pqi/p3peermgr.cc index e871e4ddd..59d9ae986 100644 --- a/libretroshare/src/pqi/p3peermgr.cc +++ b/libretroshare/src/pqi/p3peermgr.cc @@ -34,11 +34,6 @@ #include "pqi/p3historymgr.h" #include "pqi/pqinetwork.h" // for getLocalAddresses -//#include "pqi/p3dhtmgr.h" // Only need it for constants. -//#include "tcponudp/tou.h" -//#include "util/extaddrfinder.h" -//#include "util/dnsresolver.h" - #include "util/rsprint.h" #include "util/rsstring.h" #include "util/rsdebug.h" diff --git a/libretroshare/src/pqi/pqi_base.h b/libretroshare/src/pqi/pqi_base.h index e220a376d..51980b501 100644 --- a/libretroshare/src/pqi/pqi_base.h +++ b/libretroshare/src/pqi/pqi_base.h @@ -37,12 +37,6 @@ struct RSTrafficClue; #include "serialiser/rsserial.h" #include "retroshare/rstypes.h" - -#define PQI_MIN_PORT 10 // TO ALLOW USERS TO HAVE PORT 80! - was 1024 -#define PQI_MIN_PORT_RNG 1024 -#define PQI_MAX_PORT 65535 -#define PQI_DEFAULT_PORT 7812 - int getPQIsearchId(); int fixme(char *str, int n); diff --git a/libretroshare/src/pqi/pqiassist.h b/libretroshare/src/pqi/pqiassist.h index 1bc00fbe9..e7b508a3e 100644 --- a/libretroshare/src/pqi/pqiassist.h +++ b/libretroshare/src/pqi/pqiassist.h @@ -19,8 +19,7 @@ * along with this program. If not, see . * * * *******************************************************************************/ -#ifndef MRK_PQI_ASSIST_H -#define MRK_PQI_ASSIST_H +#pragma once #include #include @@ -28,25 +27,15 @@ #include "pqi/pqinetwork.h" #include "pqi/pqimonitor.h" -/********** - * This header file provides two interfaces for assisting - * the connections to friends. - * - * pqiNetAssistFirewall - which will provides interfaces - * to functionality like upnp and apple's equivalent. - * - * pqiNetAssistConnect - which will provides interfaces - * to other networks (DHT) etc that can provide information. - * These classes would be expected to use the pqiMonitor - * callback system to notify the connectionMgr. - * - ***/ +/** + * @file + * This header file provides interfaces for assisting the connections to + * friends. + */ class pqiNetAssist { - public: - -virtual ~pqiNetAssist() { return; } +public: /* External Interface */ virtual void enable(bool on) = 0; @@ -58,28 +47,24 @@ virtual bool getActive() = 0; virtual int tick() { return 0; } /* for internal accounting */ + virtual ~pqiNetAssist(); }; - -#define PFP_TYPE_UDP 0x0001 -#define PFP_TYPE_TCP 0x0002 - -class PortForwardParams +struct PortForwardParams { - public: uint32_t fwdId; uint32_t status; uint32_t typeFlags; - struct sockaddr_storage intAddr; - struct sockaddr_storage extaddr; + sockaddr_storage intAddr; + sockaddr_storage extaddr; }; +/** + * Provides interfaces to functionality like upnp and apple's equivalent. + */ class pqiNetAssistFirewall: public pqiNetAssist { - public: - -virtual ~pqiNetAssistFirewall() { return; } - +public: /* the address that the listening port is on */ virtual void setInternalPort(unsigned short iport_in) = 0; virtual void setExternalPort(unsigned short eport_in) = 0; @@ -102,11 +87,13 @@ virtual bool statusPortForward(const uint32_t fwdId, PortForwardParams ¶m class pqiNetAssistPeerShare { - public: - - /* share Addresses for various reasons (bad peers, etc) */ -virtual void updatePeer(const RsPeerId& id, const struct sockaddr_storage &addr, int type, int reason, int age) = 0; +public: + /** share Addresses for various reasons (bad peers, etc) */ + virtual void updatePeer( + const RsPeerId& id, const struct sockaddr_storage &addr, + int type, int reason, int age ) = 0; + virtual ~pqiNetAssistPeerShare(); }; @@ -141,20 +128,22 @@ virtual int tick() = 0; /* for internal accounting */ #define NETASSIST_KNOWN_PEER_TYPE_MASK 0xff00 +/** + * Provides interfaces to other networks like DHT that can provide information. + * These classes would be expected to use the pqiMonitor callback system to + * notify the connectionMgr. + */ class pqiNetAssistConnect: public pqiNetAssist { - /* - */ - public: - pqiNetAssistConnect(const RsPeerId& id, pqiConnectCb *cb) - :mPeerId(id), mConnCb(cb) { return; } +public: + pqiNetAssistConnect(const RsPeerId& id, pqiConnectCb *cb) : + mPeerId(id), mConnCb(cb) {} /********** External DHT Interface ************************ * These Functions are the external interface * for the DHT, and must be non-blocking and return quickly */ - /* add / remove peers */ virtual bool findPeer(const RsPeerId& id) = 0; virtual bool dropPeer(const RsPeerId& id) = 0; @@ -175,11 +164,9 @@ virtual bool setAttachMode(bool on) = 0; // FIXUP. /***** Stats for Network / DHT *****/ virtual bool getNetworkStats(uint32_t &netsize, uint32_t &localnetsize) = 0; // DEPRECIATE. - protected: + ~pqiNetAssistConnect() override; + +protected: RsPeerId mPeerId; pqiConnectCb *mConnCb; }; - - -#endif /* MRK_PQI_ASSIST_H */ - diff --git a/libretroshare/src/pqi/pqimonitor.h b/libretroshare/src/pqi/pqimonitor.h index eaba16b73..34a858e66 100644 --- a/libretroshare/src/pqi/pqimonitor.h +++ b/libretroshare/src/pqi/pqimonitor.h @@ -63,10 +63,6 @@ const uint32_t RS_STUN_FRIEND = 0x0020; const uint32_t RS_STUN_FRIEND_OF_FRIEND = 0x0040; -// for the old p3dhtmgr - amazed it still compiles ;) -#define RS_CONNECT_PASSIVE 1 -#define RS_CONNECT_ACTIVE 2 - #define RS_CB_DHT 0x0001 /* from dht */ #define RS_CB_DISC 0x0002 /* from peers */ #define RS_CB_PERSON 0x0003 /* from connection */ diff --git a/libretroshare/src/retroshare/rsconfig.h b/libretroshare/src/retroshare/rsconfig.h index 88177df2d..b1522330a 100644 --- a/libretroshare/src/retroshare/rsconfig.h +++ b/libretroshare/src/retroshare/rsconfig.h @@ -206,11 +206,11 @@ struct RSTrafficClue : RsSerializable struct RsConfigNetStatus : RsSerializable { - RsConfigNetStatus() + RsConfigNetStatus() : netLocalOk(true) { localPort = extPort = 0 ; firewalled = forwardPort = false ; - DHTActive = uPnPActive = netLocalOk = netUpnpOk = netDhtOk = netStunOk = netExtAddressOk = false ; + DHTActive = uPnPActive = netUpnpOk = netDhtOk = netStunOk = netExtAddressOk = false ; uPnPState = 0 ; //DHTPeers = 0 ; netDhtNetSize = netDhtRsNetSize = 0; @@ -235,7 +235,8 @@ struct RsConfigNetStatus : RsSerializable int uPnPState; /* Flags for Network Status */ - bool netLocalOk; /* That we've talked to someone! */ + RS_DEPRECATED + bool netLocalOk; /// As of today it's meaningless bool netUpnpOk; /* upnp is enabled and active */ bool netDhtOk; /* response from dht */ bool netStunOk; /* recvd stun / udp packets */ diff --git a/libretroshare/src/rs_upnp/upnphandler_linux.cc b/libretroshare/src/rs_upnp/upnphandler_libupnp.cc similarity index 98% rename from libretroshare/src/rs_upnp/upnphandler_linux.cc rename to libretroshare/src/rs_upnp/upnphandler_libupnp.cc index df9bc66e9..17e096a08 100644 --- a/libretroshare/src/rs_upnp/upnphandler_linux.cc +++ b/libretroshare/src/rs_upnp/upnphandler_libupnp.cc @@ -1,5 +1,5 @@ /******************************************************************************* - * libretroshare/src/upnp: upnphandler_linux.cc * + * libretroshare/src/upnp: upnphandler_libupnp.cc * * * * libretroshare: retroshare core library * * * @@ -28,7 +28,7 @@ extern "C" { #endif /* This stuff is actually C */ -#include "rs_upnp/upnphandler_linux.h" +#include "rs_upnp/upnphandler_libupnp.h" #include "util/rsnet.h" diff --git a/libretroshare/src/rs_upnp/upnphandler_linux.h b/libretroshare/src/rs_upnp/upnphandler_libupnp.h similarity index 95% rename from libretroshare/src/rs_upnp/upnphandler_linux.h rename to libretroshare/src/rs_upnp/upnphandler_libupnp.h index dc5207a7c..bc04d6d36 100644 --- a/libretroshare/src/rs_upnp/upnphandler_linux.h +++ b/libretroshare/src/rs_upnp/upnphandler_libupnp.h @@ -1,5 +1,5 @@ /******************************************************************************* - * libretroshare/src/upnp: upnphandler_linux.h * + * libretroshare/src/upnp: upnphandler_libupnp.h * * * * libretroshare: retroshare core library * * * @@ -19,8 +19,7 @@ * along with this program. If not, see . * * * *******************************************************************************/ -#ifndef _RS_UPNP_IFACE_H -#define _RS_UPNP_IFACE_H +#pragma once #include @@ -99,5 +98,3 @@ class upnphandler: public pqiNetAssistFirewall /* info from upnp */ int CtrlPointCallbackEventHandler(Upnp_EventType ,void* , void*); - -#endif /* _RS_UPNP_IFACE_H */ diff --git a/libretroshare/src/rs_upnp/upnphandler_miniupnp.h b/libretroshare/src/rs_upnp/upnphandler_miniupnp.h index e1616a0b9..d07304724 100644 --- a/libretroshare/src/rs_upnp/upnphandler_miniupnp.h +++ b/libretroshare/src/rs_upnp/upnphandler_miniupnp.h @@ -19,12 +19,9 @@ * along with this program. If not, see . * * * *******************************************************************************/ -//windows/osx (miniupnpc) implementation -#ifndef _RS_UPNP_IFACE_H -#define _RS_UPNP_IFACE_H +#pragma once #include - #include #include @@ -127,5 +124,3 @@ bool checkUPnPActive(); std::list activeForwards; }; - -#endif /* _RS_UPNP_IFACE_H */ diff --git a/libretroshare/src/rsserver/p3serverconfig.cc b/libretroshare/src/rsserver/p3serverconfig.cc index 4e22c3a4d..f31529fa3 100644 --- a/libretroshare/src/rsserver/p3serverconfig.cc +++ b/libretroshare/src/rsserver/p3serverconfig.cc @@ -160,7 +160,6 @@ int p3ServerConfig::getConfigNetStatus(RsConfigNetStatus &status) pqiNetStatus nstatus; mNetMgr->getNetStatus(nstatus); - status.netLocalOk = nstatus.mLocalAddrOk; status.netUpnpOk = nstatus.mUpnpOk; status.netStunOk = false; status.netExtAddressOk = nstatus.mExtAddrOk; diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 3b1f172cf..cbf2ff1c9 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -702,7 +702,7 @@ RsGRouter *rsGRouter = NULL ; #include "util/rsrandom.h" #ifdef RS_USE_LIBUPNP -# include "rs_upnp/upnphandler_linux.h" +# include "rs_upnp/upnphandler_libupnp.h" #else // def RS_USE_LIBUPNP # include "rs_upnp/upnphandler_miniupnp.h" #endif // def RS_USE_LIBUPNP