2018-05-28 22:03:39 +02:00
/*******************************************************************************
* libretroshare / src / pqi : p3netmgr . cc *
* *
* libretroshare : retroshare core library *
* *
* Copyright 2007 - 2011 by Robert Fernie . *
* Copyright ( C ) 2015 - 2018 Gioacchino Mazzurco < gio @ eigenlab . org > *
* *
* 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 < https : //www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2018-10-07 01:34:05 +02:00
# include "util/rstime.h"
2018-02-21 04:24:12 +01:00
# include <vector>
2013-10-21 11:00:49 +00:00
2011-07-09 09:36:37 +00:00
# include "pqi/p3netmgr.h"
2011-07-09 18:39:34 +00:00
# include "pqi/p3peermgr.h"
# include "pqi/p3linkmgr.h"
2011-07-09 09:36:37 +00:00
# include "util/rsnet.h"
# include "util/rsrandom.h"
2011-08-04 22:57:12 +00:00
# include "util/rsdebug.h"
2011-07-09 09:36:37 +00:00
# include "util/extaddrfinder.h"
# include "util/dnsresolver.h"
2018-02-19 23:23:15 +01:00
2016-06-18 13:00:15 +02:00
struct RsLog : : logInfo p3netmgrzoneInfo = { RsLog : : Default , " p3netmgr " } ;
# define p3netmgrzone &p3netmgrzoneInfo
2011-07-09 09:36:37 +00:00
2017-04-24 22:47:08 +02:00
# include "rsitems/rsconfigitems.h"
2011-07-09 09:36:37 +00:00
# include "retroshare/rsiface.h"
2011-07-12 14:16:46 +00:00
# include "retroshare/rsconfig.h"
2015-06-10 19:15:07 +00:00
# include "retroshare/rsbanlist.h"
2011-07-09 09:36:37 +00:00
/* Network setup States */
const uint32_t RS_NET_NEEDS_RESET = 0x0000 ;
const uint32_t RS_NET_UNKNOWN = 0x0001 ;
const uint32_t RS_NET_UPNP_INIT = 0x0002 ;
const uint32_t RS_NET_UPNP_SETUP = 0x0003 ;
const uint32_t RS_NET_EXT_SETUP = 0x0004 ;
const uint32_t RS_NET_DONE = 0x0005 ;
const uint32_t RS_NET_LOOPBACK = 0x0006 ;
2017-07-15 14:38:17 +02:00
//const uint32_t RS_NET_DOWN = 0x0007;
2011-07-09 09:36:37 +00:00
/* Stun modes (TODO) */
2017-07-15 14:38:17 +02:00
//const uint32_t RS_STUN_DHT = 0x0001;
//const uint32_t RS_STUN_DONE = 0x0002;
//const uint32_t RS_STUN_LIST_MIN = 100;
//const uint32_t RS_STUN_FOUND_MIN = 10;
2011-07-09 09:36:37 +00:00
const uint32_t MAX_UPNP_INIT = 60 ; /* seconds UPnP timeout */
const uint32_t MAX_UPNP_COMPLETE = 600 ; /* 10 min... seems to take a while */
2017-07-15 14:38:17 +02:00
//const uint32_t MAX_NETWORK_INIT = 70; /* timeout before network reset */
2011-07-09 09:36:37 +00:00
2017-07-15 14:38:17 +02:00
//const uint32_t MIN_TIME_BETWEEN_NET_RESET = 5;
2011-07-09 09:36:37 +00:00
/****
2011-07-10 18:19:46 +00:00
* # define NETMGR_DEBUG 1
* # define NETMGR_DEBUG_RESET 1
* # define NETMGR_DEBUG_TICK 1
2011-07-12 14:16:46 +00:00
* # define NETMGR_DEBUG_STATEBOX 1
2011-07-09 09:36:37 +00:00
* * */
2015-05-25 16:51:15 +00:00
// #define NETMGR_DEBUG 1
// #define NETMGR_DEBUG_RESET 1
// #define NETMGR_DEBUG_TICK 1
// #define NETMGR_DEBUG_STATEBOX 1
2011-07-09 09:36:37 +00:00
pqiNetStatus : : pqiNetStatus ( )
: mLocalAddrOk ( false ) , mExtAddrOk ( false ) , mExtAddrStableOk ( false ) ,
mUpnpOk ( false ) , mDhtOk ( false ) , mResetReq ( false )
{
mDhtNetworkSize = 0 ;
mDhtRsNetworkSize = 0 ;
2013-09-13 14:35:19 +00:00
sockaddr_storage_clear ( mLocalAddr ) ;
sockaddr_storage_clear ( mExtAddr ) ;
2011-07-09 09:36:37 +00:00
return ;
}
void pqiNetStatus : : print ( std : : ostream & out )
{
out < < " pqiNetStatus: " ;
out < < " mLocalAddrOk: " < < mLocalAddrOk ;
out < < " mExtAddrOk: " < < mExtAddrOk ;
out < < " mExtAddrStableOk: " < < mExtAddrStableOk ;
out < < std : : endl ;
out < < " mUpnpOk: " < < mUpnpOk ;
out < < " mDhtOk: " < < mDhtOk ;
out < < " mResetReq: " < < mResetReq ;
out < < std : : endl ;
out < < " mDhtNetworkSize: " < < mDhtNetworkSize < < " mDhtRsNetworkSize: " < < mDhtRsNetworkSize ;
out < < std : : endl ;
2013-09-13 14:35:19 +00:00
out < < " mLocalAddr: " < < sockaddr_storage_tostring ( mLocalAddr ) < < " " ;
out < < " mExtAddr: " < < sockaddr_storage_tostring ( mExtAddr ) < < " " ;
2011-07-09 09:36:37 +00:00
out < < " NetOk: " < < NetOk ( ) ;
out < < std : : endl ;
}
2011-07-13 14:22:25 +00:00
p3NetMgrIMPL : : p3NetMgrIMPL ( )
2011-07-09 18:39:34 +00:00
: mPeerMgr ( NULL ) , mLinkMgr ( NULL ) , mNetMtx ( " p3NetMgr " ) ,
2011-07-09 09:36:37 +00:00
mNetStatus ( RS_NET_UNKNOWN ) , mStatusChanged ( false )
{
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetMode = RS_NET_MODE_UDP ;
mUseExtAddrFinder = true ;
mExtAddrFinder = new ExtAddrFinder ( ) ;
mNetInitTS = 0 ;
mNetFlags = pqiNetStatus ( ) ;
mOldNetFlags = pqiNetStatus ( ) ;
2011-07-09 18:39:34 +00:00
2011-07-12 14:16:46 +00:00
mLastSlowTickTime = 0 ;
mOldNatType = RSNET_NATTYPE_UNKNOWN ;
mOldNatHole = RSNET_NATHOLE_UNKNOWN ;
2013-09-13 14:35:19 +00:00
sockaddr_storage_clear ( mLocalAddr ) ;
sockaddr_storage_clear ( mExtAddr ) ;
2013-09-24 05:30:01 +00:00
// force to IPv4 for the moment.
mLocalAddr . ss_family = AF_INET ;
mExtAddr . ss_family = AF_INET ;
2013-09-28 08:09:59 +00:00
// default to full.
mVsDisc = RS_VS_DISC_FULL ;
mVsDht = RS_VS_DHT_FULL ;
2013-09-24 05:30:01 +00:00
2011-07-09 09:36:37 +00:00
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-09 09:36:37 +00:00
std : : cerr < < " p3NetMgr() Startup " < < std : : endl ;
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr() Startup, resetting network " ) ;
2011-07-09 09:36:37 +00:00
netReset ( ) ;
return ;
}
2011-07-13 23:23:47 +00:00
void p3NetMgrIMPL : : setManagers ( p3PeerMgr * peerMgr , p3LinkMgr * linkMgr )
2011-07-09 18:39:34 +00:00
{
mPeerMgr = peerMgr ;
mLinkMgr = linkMgr ;
2011-07-09 09:36:37 +00:00
}
2016-06-21 13:55:23 +02:00
# ifdef RS_USE_DHT_STUNNER
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : setAddrAssist ( pqiAddrAssist * dhtStun , pqiAddrAssist * proxyStun )
2011-07-09 09:36:37 +00:00
{
2011-07-10 00:41:39 +00:00
mDhtStunner = dhtStun ;
mProxyStunner = proxyStun ;
2011-07-09 09:36:37 +00:00
}
2016-06-21 13:55:23 +02:00
# endif // RS_USE_DHT_STUNNER
2011-07-09 09:36:37 +00:00
2011-07-10 01:49:39 +00:00
2011-07-09 09:36:37 +00:00
/***** Framework / initial implementation for a connection manager.
*
* This needs a state machine for Initialisation .
*
* Network state :
* RS_NET_UNKNOWN
* RS_NET_EXT_UNKNOWN * forwarded port ( but Unknown Ext IP ) *
* RS_NET_EXT_KNOWN * forwarded port with known IP / Port . *
*
* RS_NET_UPNP_CHECK * checking for UPnP *
* RS_NET_UPNP_KNOWN * confirmed UPnP ext Ip / port *
*
* RS_NET_UDP_UNKNOWN * not Ext / UPnP - to determine Ext IP / Port *
* RS_NET_UDP_KNOWN * have Stunned for Ext Addr *
*
* Transitions :
*
* RS_NET_UNKNOWN - ( config ) - > RS_NET_EXT_UNKNOWN
* RS_NET_UNKNOWN - ( config ) - > RS_NET_UPNP_UNKNOWN
* RS_NET_UNKNOWN - ( config ) - > RS_NET_UDP_UNKNOWN
*
* RS_NET_EXT_UNKNOWN - ( DHT ( ip ) / Stun ) - > RS_NET_EXT_KNOWN
*
* RS_NET_UPNP_UNKNOWN - ( Upnp ) - > RS_NET_UPNP_KNOWN
* RS_NET_UPNP_UNKNOWN - ( timout / Upnp ) - > RS_NET_UDP_UNKNOWN
*
* RS_NET_UDP_UNKNOWN - ( stun ) - > RS_NET_UDP_KNOWN
*
*
* STUN state :
* RS_STUN_INIT * done nothing *
* RS_STUN_DHT * looking up peers *
* RS_STUN_DONE * found active peer and stunned *
*
*
* Steps .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* ( 1 ) Startup .
* - UDP port setup .
* - DHT setup .
* - Get Stun Keys - > add to DHT .
* - Feedback from DHT - > ask UDP to stun .
*
* ( 1 ) determine Network mode .
* If external Port . . . . Done :
* ( 2 )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Stable operation :
* ( 1 ) tick and check peers .
* ( 2 ) handle callback .
* ( 3 ) notify of new / failed connections .
*
*
*/
/* Called to reseet the whole network stack. this call is
* triggered by udp stun address tracking .
*
* must :
* - reset UPnP and DHT .
* -
*/
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netReset ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() Called " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_ALERT , p3netmgrzone , " p3NetMgr::netReset() Called " ) ;
2011-07-09 09:36:37 +00:00
shutdown ( ) ; /* blocking shutdown call */
// Will initiate a new call for determining the external ip.
if ( mUseExtAddrFinder )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() restarting AddrFinder " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mExtAddrFinder - > reset ( ) ;
}
else
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() ExtAddrFinder Disabled " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() resetting NetStatus " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
/* reset tcp network - if necessary */
{
/* NOTE: nNetListeners should be protected via the Mutex.
* HOWEVER , as we NEVER change this list - once its setup
* we can get away without it - and assume its constant .
*
* NB : ( * it ) - > reset_listener must be out of the mutex ,
* as it calls back to p3ConnMgr .
*/
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2013-09-13 14:35:19 +00:00
struct sockaddr_storage iaddr = mLocalAddr ;
2011-07-09 09:36:37 +00:00
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() resetting listeners " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
std : : list < pqiNetListener * > : : const_iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mNetListeners . begin ( ) ; it ! = mNetListeners . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( * it ) - > resetListener ( iaddr ) ;
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() reset listener " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
}
}
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetStatus = RS_NET_UNKNOWN ;
netStatusReset_locked ( ) ;
}
2011-07-12 14:16:46 +00:00
updateNetStateBox_reset ( ) ;
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netReset() done " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netStatusReset_locked ( )
2011-07-09 09:36:37 +00:00
{
2011-07-13 14:22:25 +00:00
//std::cerr << "p3NetMgrIMPL::netStatusReset()" << std::endl;;
2011-07-09 09:36:37 +00:00
mNetFlags = pqiNetStatus ( ) ;
}
2011-07-09 18:39:34 +00:00
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : shutdown ( ) /* blocking shutdown call */
2011-07-09 18:39:34 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::shutdown() " ;
2011-07-09 18:39:34 +00:00
std : : cerr < < std : : endl ;
# endif
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetStatus = RS_NET_UNKNOWN ;
mNetInitTS = time ( NULL ) ;
netStatusReset_locked ( ) ;
}
netAssistFirewallShutdown ( ) ;
netAssistConnectShutdown ( ) ;
return true ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netStartup ( )
2011-07-09 09:36:37 +00:00
{
/* startup stuff */
/* StunInit gets a list of peers, and asks the DHT to find them...
* This is needed for all systems so startup straight away
*/
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
netDhtInit ( ) ;
/* decide which net setup mode we're going into
*/
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetInitTS = time ( NULL ) ;
netStatusReset_locked ( ) ;
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() resetting mNetInitTS / Status " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mNetMode & = ~ ( RS_NET_MODE_ACTUAL ) ;
switch ( mNetMode & RS_NET_MODE_TRYMODE )
{
case RS_NET_MODE_TRY_EXT : /* v similar to UDP */
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() TRY_EXT mode " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
mNetMode | = RS_NET_MODE_EXT ;
mNetStatus = RS_NET_EXT_SETUP ;
break ;
case RS_NET_MODE_TRY_UDP :
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() TRY_UDP mode " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
mNetMode | = RS_NET_MODE_UDP ;
mNetStatus = RS_NET_EXT_SETUP ;
break ;
2013-09-09 02:10:49 +00:00
case RS_NET_MODE_TRY_LOOPBACK :
std : : cerr < < " p3NetMgrIMPL::netStartup() TRY_LOOPBACK mode " ;
std : : cerr < < std : : endl ;
mNetMode | = RS_NET_MODE_HIDDEN ;
mNetStatus = RS_NET_LOOPBACK ;
break ;
2011-07-09 09:36:37 +00:00
default : // Fall through.
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() UNKNOWN mode " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
case RS_NET_MODE_TRY_UPNP :
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netStartup() TRY_UPNP mode " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
/* Force it here (could be default!) */
mNetMode | = RS_NET_MODE_TRY_UPNP ;
mNetMode | = RS_NET_MODE_UDP ; /* set to UDP, upgraded is UPnP is Okay */
mNetStatus = RS_NET_UPNP_INIT ;
break ;
}
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : tick ( )
2011-07-12 14:16:46 +00:00
{
2018-10-07 01:34:05 +02:00
rstime_t now = time ( NULL ) ;
2011-07-12 14:16:46 +00:00
bool doSlowTick = false ;
{
RsStackMutex stack ( mNetMtx ) ; /************** LOCK MUTEX ***************/
if ( now > mLastSlowTickTime )
{
mLastSlowTickTime = now ;
doSlowTick = true ;
}
}
if ( doSlowTick )
{
slowTick ( ) ;
}
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : slowTick ( )
2011-07-09 09:36:37 +00:00
{
netTick ( ) ;
2012-01-14 16:41:00 +00:00
netAssistTick ( ) ;
2011-07-12 14:16:46 +00:00
updateNetStateBox_temporal ( ) ;
2016-06-21 13:55:23 +02:00
# ifdef RS_USE_DHT_STUNNER
2011-07-12 14:16:46 +00:00
if ( mDhtStunner )
{
mDhtStunner - > tick ( ) ;
}
if ( mProxyStunner )
{
mProxyStunner - > tick ( ) ;
}
2016-06-21 13:55:23 +02:00
# endif // RS_USE_DHT_STUNNER
2011-07-09 09:36:37 +00:00
}
2011-07-09 18:39:34 +00:00
# define STARTUP_DELAY 5
2011-07-09 09:36:37 +00:00
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netTick ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_TICK
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() " < < std : : endl ;
2011-07-30 12:18:56 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() mNetMode: " < < std : : hex < < mNetMode ;
std : : cerr < < " ACTUALMODE: " < < ( mNetMode & RS_NET_MODE_ACTUAL ) ;
std : : cerr < < " TRYMODE: " < < ( mNetMode & RS_NET_MODE_TRYMODE ) ;
std : : cerr < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
// Check whether we are stuck on loopback. This happens if RS starts when
// the computer is not yet connected to the internet. In such a case we
// periodically check for a local net address.
//
checkNetAddress ( ) ;
2011-07-09 18:39:34 +00:00
uint32_t netStatus = 0 ;
2018-10-07 01:34:05 +02:00
rstime_t age = 0 ;
2011-07-09 18:39:34 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /************** LOCK MUTEX ***************/
2011-07-09 09:36:37 +00:00
2011-07-09 18:39:34 +00:00
netStatus = mNetStatus ;
age = time ( NULL ) - mNetInitTS ;
2011-07-09 09:36:37 +00:00
2011-07-09 18:39:34 +00:00
}
2011-07-09 09:36:37 +00:00
switch ( netStatus )
{
case RS_NET_NEEDS_RESET :
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: NEEDS_RESET " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr::netTick() RS_NET_NEEDS_RESET, resetting network " ) ;
2011-08-04 22:57:12 +00:00
2011-07-09 09:36:37 +00:00
netReset ( ) ;
break ;
case RS_NET_UNKNOWN :
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: UNKNOWN " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
/* add a small delay to stop restarting straight after a RESET
* This is so can we shutdown cleanly
*/
if ( age < STARTUP_DELAY )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() Delaying Startup " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
}
else
{
netStartup ( ) ;
}
break ;
case RS_NET_UPNP_INIT :
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: UPNP_INIT " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
netUpnpInit ( ) ;
break ;
case RS_NET_UPNP_SETUP :
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: UPNP_SETUP " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
netUpnpCheck ( ) ;
break ;
case RS_NET_EXT_SETUP :
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: EXT_SETUP " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
netExtCheck ( ) ;
break ;
case RS_NET_DONE :
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_TICK
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: DONE " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
break ;
case RS_NET_LOOPBACK :
//don't do a shutdown because a client in a computer without local network might be usefull for debug.
//shutdown();
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netTick() STATUS: RS_NET_LOOPBACK " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
default :
break ;
}
return ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netDhtInit ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netDhtInit() " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
uint32_t vs = 0 ;
{
RsStackMutex stack ( mNetMtx ) ; /*********** LOCKED MUTEX ************/
2013-09-28 08:09:59 +00:00
vs = mVsDht ;
2011-07-09 09:36:37 +00:00
}
2013-09-28 08:09:59 +00:00
enableNetAssistConnect ( vs ! = RS_VS_DHT_OFF ) ;
2011-07-09 09:36:37 +00:00
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netUpnpInit ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpInit() " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
uint16_t eport , iport ;
mNetMtx . lock ( ) ; /* LOCK MUTEX */
/* get the ports from the configuration */
mNetStatus = RS_NET_UPNP_SETUP ;
2013-09-13 14:35:19 +00:00
iport = sockaddr_storage_port ( mLocalAddr ) ;
eport = sockaddr_storage_port ( mExtAddr ) ;
2011-07-09 09:36:37 +00:00
if ( ( eport < 1000 ) | | ( eport > 30000 ) )
{
eport = iport ;
}
mNetMtx . unlock ( ) ; /* UNLOCK MUTEX */
netAssistFirewallPorts ( iport , eport ) ;
enableNetAssistFirewall ( true ) ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netUpnpCheck ( )
2011-07-09 09:36:37 +00:00
{
/* grab timestamp */
mNetMtx . lock ( ) ; /* LOCK MUTEX */
2018-10-07 01:34:05 +02:00
rstime_t delta = time ( NULL ) - mNetInitTS ;
2011-07-09 09:36:37 +00:00
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpCheck() age: " < < delta < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mNetMtx . unlock ( ) ; /* UNLOCK MUTEX */
2013-09-13 14:35:19 +00:00
struct sockaddr_storage extAddr ;
2011-07-09 09:36:37 +00:00
int upnpState = netAssistFirewallActive ( ) ;
2018-10-07 01:34:05 +02:00
if ( ( ( upnpState = = 0 ) & & ( delta > ( rstime_t ) MAX_UPNP_INIT ) ) | |
( ( upnpState > 0 ) & & ( delta > ( rstime_t ) MAX_UPNP_COMPLETE ) ) )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpCheck() " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < " Upnp Check failed. " < < std : : endl ;
# endif
/* fallback to UDP startup */
mNetMtx . lock ( ) ; /* LOCK MUTEX */
/* UPnP Failed us! */
mNetStatus = RS_NET_EXT_SETUP ;
mNetFlags . mUpnpOk = false ;
mNetMtx . unlock ( ) ; /* UNLOCK MUTEX */
}
else if ( ( upnpState > 0 ) & & netAssistExtAddress ( extAddr ) )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpCheck() " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < " Upnp Check success state: " < < upnpState < < std : : endl ;
# endif
/* switch to UDP startup */
mNetMtx . lock ( ) ; /* LOCK MUTEX */
/* Set Net Status flags ....
* we now have external upnp address . Golden !
* don ' t set netOk flag until have seen some traffic .
*/
2013-09-13 14:35:19 +00:00
if ( sockaddr_storage_isValidNet ( extAddr ) )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpCheck() " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " UpnpAddr: " < < sockaddr_storage_tostring ( extAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
mNetFlags . mUpnpOk = true ;
mNetFlags . mExtAddr = extAddr ;
mNetFlags . mExtAddrOk = true ;
mNetFlags . mExtAddrStableOk = true ;
mNetStatus = RS_NET_EXT_SETUP ;
/* Fix netMode & Clear others! */
mNetMode = RS_NET_MODE_TRY_UPNP | RS_NET_MODE_UPNP ;
}
mNetMtx . unlock ( ) ; /* UNLOCK MUTEX */
}
else
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netUpnpCheck() " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < " Upnp Check Continues: status: " < < upnpState < < std : : endl ;
# endif
}
}
2015-06-01 22:03:07 +00:00
class ZeroInt
{
public :
ZeroInt ( ) { n = 0 ; }
uint32_t n ;
} ;
2011-07-09 09:36:37 +00:00
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : netExtCheck ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2011-07-12 14:16:46 +00:00
bool netSetupDone = false ;
2011-07-09 09:36:37 +00:00
{
2018-02-26 08:49:06 +01:00
RS_STACK_MUTEX ( mNetMtx ) ;
2011-07-09 09:36:37 +00:00
bool isStable = false ;
2018-02-26 08:49:06 +01:00
sockaddr_storage tmpip ;
2011-07-09 09:36:37 +00:00
2018-02-26 08:49:06 +01:00
std : : map < sockaddr_storage , ZeroInt > address_votes ;
2015-06-01 22:03:07 +00:00
2015-09-23 21:45:15 -04:00
/* check for External Address */
2011-07-09 09:36:37 +00:00
/* in order of importance */
/* (1) UPnP -> which handles itself */
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext Not Ok " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
/* net Assist */
2018-02-26 08:49:06 +01:00
if ( netAssistExtAddress ( tmpip ) & &
sockaddr_storage_isValidNet ( tmpip ) & &
sockaddr_storage_ipv6_to_ipv4 ( tmpip ) )
2015-09-23 21:45:15 -04:00
{
2018-02-26 08:49:06 +01:00
if ( ! rsBanList | |
rsBanList - > isAddressAccepted (
tmpip , RSBANLIST_CHECKING_FLAGS_BLACKLIST ) )
2015-09-23 21:45:15 -04:00
{
2018-02-26 08:49:06 +01:00
// must be stable???
isStable = true ;
mNetFlags . mExtAddrOk = true ;
mNetFlags . mExtAddrStableOk = isStable ;
address_votes [ tmpip ] . n + + ;
std : : cerr < < __PRETTY_FUNCTION__ < < " NetAssistAddress "
< < " reported external address "
< < sockaddr_storage_iptostring ( tmpip )
< < std : : endl ;
2015-09-23 21:45:15 -04:00
}
else
2018-02-26 08:49:06 +01:00
std : : cerr < < " (SS) netAssisExternalAddress returned banned "
< < " own IP " < < sockaddr_storage_iptostring ( tmpip )
< < " (banned). Rejecting. " < < std : : endl ;
2015-09-23 21:45:15 -04:00
}
2011-07-09 09:36:37 +00:00
}
2015-09-23 21:45:15 -04:00
# ifdef ALLOW_DHT_STUNNER
// (cyril) I disabled this because it's pretty dangerous. The DHT can report a wrong address quite easily
// if the other DHT peers are not collaborating.
2011-08-02 19:05:27 +00:00
/* Next ask the DhtStunner */
{
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext Not Ok, Checking DhtStunner " < < std : : endl ;
# endif
uint8_t isstable = 0 ;
2013-09-13 14:35:19 +00:00
struct sockaddr_storage tmpaddr ;
sockaddr_storage_clear ( tmpaddr ) ;
2011-08-02 19:05:27 +00:00
if ( mDhtStunner )
{
2015-09-23 21:45:15 -04:00
/* input network bits */
if ( mDhtStunner - > getExternalAddr ( tmpaddr , isstable ) )
{
2018-01-01 15:49:49 +01:00
if ( ( rsBanList = = NULL ) | | rsBanList - > isAddressAccepted ( tmpaddr , RSBANLIST_CHECKING_FLAGS_BLACKLIST ) )
2015-09-23 21:45:15 -04:00
{
// must be stable???
isStable = ( isstable = = 1 ) ;
//mNetFlags.mExtAddr = tmpaddr;
mNetFlags . mExtAddrOk = true ;
mNetFlags . mExtAddrStableOk = isStable ;
address_votes [ tmpaddr ] . n + + ;
2011-08-02 19:05:27 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2015-09-23 21:45:15 -04:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() From DhtStunner: " ;
std : : cerr < < sockaddr_storage_tostring ( tmpaddr ) ;
std : : cerr < < " Stable: " < < ( uint32_t ) isstable ;
std : : cerr < < std : : endl ;
# endif
}
else
std : : cerr < < " (SS) DHTStunner returned wrong own IP " < < sockaddr_storage_iptostring ( tmpaddr ) < < " (banned). Rejecting. " < < std : : endl ;
}
2011-08-02 19:05:27 +00:00
}
}
2015-09-23 21:45:15 -04:00
# endif
2011-08-02 19:05:27 +00:00
2018-02-26 08:49:06 +01:00
/* ask ExtAddrFinder */
2011-07-09 09:36:37 +00:00
{
/* ExtAddrFinder */
if ( mUseExtAddrFinder )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() checking ExtAddrFinder " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2013-09-13 14:35:19 +00:00
bool extFinderOk = mExtAddrFinder - > hasValidIP ( tmpip ) ;
2018-02-26 08:49:06 +01:00
if ( extFinderOk & & sockaddr_storage_ipv6_to_ipv4 ( tmpip ) )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
/* best guess at port */
2013-09-13 14:35:19 +00:00
sockaddr_storage_setport ( tmpip , sockaddr_storage_port ( mLocalAddr ) ) ;
2015-09-23 21:45:15 -04:00
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " ExtAddr: " < < sockaddr_storage_tostring ( tmpip ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
mNetFlags . mExtAddrOk = true ;
2015-09-23 21:45:15 -04:00
address_votes [ tmpip ] . n + + ;
2015-06-01 22:03:07 +00:00
2018-02-26 08:49:06 +01:00
/* XXX HACK TO FIX drbob: ALLOWING
* ExtAddrFinder - > ExtAddrStableOk = true
* ( which it is not normally ) */
2011-07-09 09:36:37 +00:00
mNetFlags . mExtAddrStableOk = true ;
2018-02-26 08:49:06 +01:00
std : : cerr < < __PRETTY_FUNCTION__ < < " ExtAddrFinder "
< < " reported external address "
< < sockaddr_storage_iptostring ( tmpip )
< < std : : endl ;
2011-07-09 09:36:37 +00:00
}
}
}
2018-02-26 08:49:06 +01:00
/* also ask peer mgr. */
2015-09-23 21:45:15 -04:00
if ( mPeerMgr )
{
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std : : cerr < < " p3NetMgrIMPL::netExtCheck() checking mPeerMgr " < < std : : endl ;
# endif
2018-02-26 08:49:06 +01:00
uint8_t isstable ; // unused
sockaddr_storage tmpaddr ;
2015-09-23 21:45:15 -04:00
2018-02-26 08:49:06 +01:00
if ( mPeerMgr - > getExtAddressReportedByFriends ( tmpaddr , isstable ) & &
sockaddr_storage_ipv6_to_ipv4 ( tmpaddr ) )
2015-09-23 21:45:15 -04:00
{
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext supplied by ExtAddrFinder " < < std : : endl ;
# endif
/* best guess at port */
2018-02-26 08:49:06 +01:00
sockaddr_storage_setport ( tmpaddr ,
sockaddr_storage_port ( mLocalAddr ) ) ;
2015-09-23 21:45:15 -04:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
std : : cerr < < " p3NetMgrIMPL::netExtCheck() " ;
std : : cerr < < " ExtAddr: " < < sockaddr_storage_tostring ( tmpip ) ;
std : : cerr < < std : : endl ;
# endif
mNetFlags . mExtAddrOk = true ;
mNetFlags . mExtAddrStableOk = isstable ;
2018-02-26 08:49:06 +01:00
address_votes [ tmpaddr ] . n + + ;
std : : cerr < < __PRETTY_FUNCTION__ < < " PeerMgr reported external "
< < " address "
< < sockaddr_storage_iptostring ( tmpaddr ) < < std : : endl ;
2015-09-23 21:45:15 -04:00
}
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2018-02-26 08:49:06 +01:00
else
std : : cerr < < " No reliable address returned. " < < std : : endl ;
2015-09-23 21:45:15 -04:00
# endif
}
2018-02-26 08:49:06 +01:00
2015-09-23 21:45:15 -04:00
/* any other sources ??? */
2011-07-09 09:36:37 +00:00
/* finalise address */
if ( mNetFlags . mExtAddrOk )
2015-09-23 21:45:15 -04:00
{
// look at votes.
2015-06-01 22:03:07 +00:00
2015-09-23 21:45:15 -04:00
std : : cerr < < " Figuring out ext addr from voting: " < < std : : endl ;
uint32_t admax = 0 ;
2015-06-10 19:15:07 +00:00
2015-09-23 21:45:15 -04:00
for ( std : : map < sockaddr_storage , ZeroInt > : : const_iterator it ( address_votes . begin ( ) ) ; it ! = address_votes . end ( ) ; + + it )
{
std : : cerr < < " Vote: " < < sockaddr_storage_iptostring ( it - > first ) < < " : " < < it - > second . n < < " votes. " ;
2015-06-01 22:03:07 +00:00
2015-09-23 21:45:15 -04:00
if ( it - > second . n > admax )
{
mNetFlags . mExtAddr = it - > first ;
admax = it - > second . n ;
2015-06-10 19:15:07 +00:00
2015-09-23 21:45:15 -04:00
std : : cerr < < " Kept! " < < std : : endl ;
}
else
std : : cerr < < " Discarded. " < < std : : endl ;
}
2011-07-09 09:36:37 +00:00
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2015-09-23 21:45:15 -04:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() " ;
std : : cerr < < " ExtAddr: " < < sockaddr_storage_tostring ( mNetFlags . mExtAddr ) ;
std : : cerr < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2015-09-23 21:45:15 -04:00
//update ip address list
mExtAddr = mNetFlags . mExtAddr ;
2011-07-09 09:36:37 +00:00
2015-09-23 21:45:15 -04:00
mNetStatus = RS_NET_DONE ;
netSetupDone = true ;
2011-07-12 14:16:46 +00:00
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2015-09-23 21:45:15 -04:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext Ok: RS_NET_DONE " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2011-07-12 14:16:46 +00:00
2015-09-23 21:45:15 -04:00
if ( ! mNetFlags . mExtAddrStableOk )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2015-09-23 21:45:15 -04:00
std : : cerr < < " p3NetMgrIMPL::netUdpCheck() UDP Unstable :( " ;
std : : cerr < < std : : endl ;
std : : cerr < < " p3NetMgrIMPL::netUdpCheck() We are unreachable " ;
std : : cerr < < std : : endl ;
std : : cerr < < " netMode => RS_NET_MODE_UNREACHABLE " ;
std : : cerr < < std : : endl ;
# endif
// Due to the new UDP connections - we can still connect some of the time!
// So limit warning!
//mNetMode &= ~(RS_NET_MODE_ACTUAL);
//mNetMode |= RS_NET_MODE_UNREACHABLE;
/* send a system warning message */
//pqiNotify *notify = getPqiNotify();
//if (notify)
{
2017-07-21 19:02:34 +02:00
//std::string title =
// "Warning: Bad Firewall Configuration";
2015-09-23 21:45:15 -04:00
std : : string msg ;
msg + = " **** WARNING **** \n " ;
msg + = " Retroshare has detected that you are behind " ;
msg + = " a restrictive Firewall \n " ;
msg + = " \n " ;
msg + = " You will have limited connectivity to other firewalled peers \n " ;
msg + = " \n " ;
msg + = " You can fix this by: \n " ;
msg + = " (1) opening an External Port \n " ;
msg + = " (2) enabling UPnP, or \n " ;
msg + = " (3) get a new (approved) Firewall/Router \n " ;
//notify->AddSysMessage(0, RS_SYS_WARNING, title, msg);
std : : cerr < < msg < < std : : endl ;
}
}
}
2011-07-09 09:36:37 +00:00
if ( mNetFlags . mExtAddrOk )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() setting netAssistSetAddress() " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
netAssistSetAddress ( mNetFlags . mLocalAddr , mNetFlags . mExtAddr , mNetMode ) ;
}
/* flag unreachables! */
if ( ( mNetFlags . mExtAddrOk ) & & ( ! mNetFlags . mExtAddrStableOk ) )
{
2011-07-10 18:19:46 +00:00
# if defined(NETMGR_DEBUG_TICK) || defined(NETMGR_DEBUG_RESET)
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() Ext Unstable - Unreachable Check " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
}
2011-07-12 14:16:46 +00:00
}
if ( netSetupDone )
{
2011-08-07 21:11:00 +00:00
std : : cerr < < " p3NetMgrIMPL::netExtCheck() netSetupDone " < < std : : endl ;
2011-07-12 14:16:46 +00:00
/* Setup NetStateBox with this info */
updateNetStateBox_startup ( ) ;
2011-08-02 19:05:27 +00:00
/* update PeerMgr with correct info */
2011-11-22 13:24:42 +00:00
if ( mPeerMgr )
{
mPeerMgr - > UpdateOwnAddress ( mLocalAddr , mExtAddr ) ;
}
/* inform DHT about our external address */
2014-03-17 20:56:06 +00:00
RsPeerId fakeId ;
2011-11-22 13:24:42 +00:00
netAssistKnownPeer ( fakeId , mExtAddr , NETASSIST_KNOWN_PEER_SELF | NETASSIST_KNOWN_PEER_ONLINE ) ;
2011-08-04 22:57:12 +00:00
2018-02-26 08:49:06 +01:00
std : : cerr < < __PRETTY_FUNCTION__ < < " Network Setup Complete "
< < std : : endl ;
2011-07-09 09:36:37 +00:00
}
}
2011-07-09 18:39:34 +00:00
/**********************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Interfaces * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-09 09:36:37 +00:00
2015-03-09 20:00:41 +01:00
bool p3NetMgrIMPL : : checkNetAddress ( )
2011-07-09 09:36:37 +00:00
{
bool addrChanged = false ;
bool validAddr = false ;
2018-05-22 19:17:37 +02:00
sockaddr_storage prefAddr ;
sockaddr_storage oldAddr ;
2011-07-09 09:36:37 +00:00
2013-09-09 02:10:49 +00:00
if ( mNetMode & RS_NET_MODE_TRY_LOOPBACK )
{
2015-06-12 21:04:11 +00:00
# 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
2018-05-22 19:17:37 +02:00
sockaddr_storage_ipv4_aton ( prefAddr , " 127.0.0.1 " ) ;
2013-09-09 02:10:49 +00:00
validAddr = true ;
}
else
{
2018-02-19 23:23:15 +01:00
/* TODO: Sat Oct 24 15:51:24 CEST 2015 The fact of having just one local
* address is a flawed assumption , this should be redesigned as soon as
* possible . It will require complete reenginering of the network layer
2018-02-21 04:24:12 +01:00
* code . */
2018-02-26 08:49:06 +01:00
/* For retro-compatibility strictly accept only IPv4 addresses here,
* IPv6 addresses are handled in a retro - compatible manner in
* p3PeerMgrIMPL : : UpdateOwnAddress */
2018-02-21 04:24:12 +01:00
std : : vector < sockaddr_storage > addrs ;
2015-03-09 20:00:41 +01:00
if ( getLocalAddresses ( addrs ) )
2018-02-19 23:23:15 +01:00
{
2018-05-22 19:17:37 +02:00
for ( auto it = addrs . begin ( ) ; it ! = addrs . end ( ) ; + + it )
2018-02-19 23:23:15 +01:00
{
2018-02-21 04:24:12 +01:00
sockaddr_storage & addr ( * it ) ;
2018-02-19 23:23:15 +01:00
if ( sockaddr_storage_isValidNet ( addr ) & &
! sockaddr_storage_isLoopbackNet ( addr ) & &
2018-02-26 08:49:06 +01:00
! sockaddr_storage_isLinkLocalNet ( addr ) & &
sockaddr_storage_ipv6_to_ipv4 ( addr ) )
2015-03-09 20:00:41 +01:00
{
2018-02-19 23:23:15 +01:00
prefAddr = addr ;
2015-03-09 20:00:41 +01:00
validAddr = true ;
2018-02-19 23:23:15 +01:00
break ;
2015-03-09 20:00:41 +01:00
}
2018-02-19 23:23:15 +01:00
}
2018-02-21 04:24:12 +01:00
/* If no satisfactory local address has been found yet relax and
2018-02-26 08:49:06 +01:00
* accept also link local addresses */
2018-02-21 04:24:12 +01:00
if ( ! validAddr ) for ( auto it = addrs . begin ( ) ; it ! = addrs . end ( ) ; + + it )
2018-02-19 23:23:15 +01:00
{
2018-02-21 04:24:12 +01:00
sockaddr_storage & addr ( * it ) ;
2018-02-19 23:23:15 +01:00
if ( sockaddr_storage_isValidNet ( addr ) & &
2018-02-24 14:07:25 +01:00
! sockaddr_storage_isLoopbackNet ( addr ) & &
2018-02-26 08:49:06 +01:00
sockaddr_storage_ipv6_to_ipv4 ( addr ) )
2018-02-19 23:23:15 +01:00
{
prefAddr = addr ;
validAddr = true ;
break ;
}
}
}
2013-09-09 02:10:49 +00:00
}
2011-07-09 09:36:37 +00:00
/* if we don't have a valid address - reset */
if ( ! validAddr )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() no Valid Network Address, resetting network. " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr::checkNetAddress() No Valid Network Address, resetting network " ) ;
2011-07-09 09:36:37 +00:00
netReset ( ) ;
return false ;
}
/* check addresses */
{
2018-02-26 08:49:06 +01:00
RS_STACK_MUTEX ( mNetMtx ) ;
2018-05-22 19:17:37 +02:00
sockaddr_storage_copy ( mLocalAddr , oldAddr ) ;
2013-09-13 14:35:19 +00:00
addrChanged = ! sockaddr_storage_sameip ( prefAddr , mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_TICK
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " Current Local: " < < sockaddr_storage_tostring ( mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " Current Preferred: " < < sockaddr_storage_iptostring ( prefAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-09 09:36:37 +00:00
if ( addrChanged )
{
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() Address Changed! " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " Current Local: " < < sockaddr_storage_tostring ( mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " Current Preferred: " < < sockaddr_storage_iptostring ( prefAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
}
# endif
// update address.
2013-09-13 14:35:19 +00:00
sockaddr_storage_copyip ( mLocalAddr , prefAddr ) ;
2018-05-22 19:17:37 +02:00
sockaddr_storage_copy ( mLocalAddr , mNetFlags . mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
2013-09-13 14:35:19 +00:00
if ( sockaddr_storage_isLoopbackNet ( mLocalAddr ) )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() laddr: Loopback " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mNetFlags . mLocalAddrOk = false ;
mNetStatus = RS_NET_LOOPBACK ;
}
2013-09-13 14:35:19 +00:00
else if ( ! sockaddr_storage_isValidNet ( mLocalAddr ) )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() laddr: invalid " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mNetFlags . mLocalAddrOk = false ;
}
else
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_TICK
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() laddr okay " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
mNetFlags . mLocalAddrOk = true ;
}
2013-09-13 14:35:19 +00:00
int port = sockaddr_storage_port ( mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
if ( ( port < PQI_MIN_PORT ) | | ( port > PQI_MAX_PORT ) )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() Correcting Port to DEFAULT " < < std : : endl ;
2011-07-09 09:36:37 +00:00
# endif
2018-03-03 01:55:49 +01:00
uint16_t new_port = htons ( PQI_MIN_PORT_RNG + ( RSRandom : : random_u32 ( ) % ( PQI_MAX_PORT - PQI_MIN_PORT_RNG ) ) ) ;
2013-09-13 14:35:19 +00:00
sockaddr_storage_setport ( mLocalAddr , new_port ) ;
2011-07-09 09:36:37 +00:00
addrChanged = true ;
}
/* if localaddr = serveraddr, then ensure that the ports
* are the same ( modify server ) . . . this mismatch can
* occur when the local port is changed . . . .
*/
2018-06-08 14:32:25 +02:00
if ( sockaddr_storage_sameip ( mLocalAddr , mExtAddr ) & & sockaddr_storage_port ( mLocalAddr ) ! = sockaddr_storage_port ( mExtAddr ) )
2011-07-09 09:36:37 +00:00
{
2018-06-08 14:32:25 +02:00
# ifdef NETMGR_DEBUG_RESET
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() local and external ports are not the same. Setting external port to " < < sockaddr_storage_port ( mLocalAddr ) < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
sockaddr_storage_setport ( mExtAddr , sockaddr_storage_port ( mLocalAddr ) ) ;
2018-05-22 19:17:37 +02:00
addrChanged = true ;
2011-07-09 09:36:37 +00:00
}
// ensure that address family is set, otherwise windows Barfs.
2013-09-13 14:35:19 +00:00
//mLocalAddr.sin_family = AF_INET;
//mExtAddr.sin_family = AF_INET;
2011-07-09 09:36:37 +00:00
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_TICK
2013-09-13 14:35:19 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() Final Local Address: " < < sockaddr_storage_tostring ( mLocalAddr ) ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
}
if ( addrChanged )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::checkNetAddress() local address changed, resetting network. " < < std : : endl ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
2011-11-22 13:24:42 +00:00
if ( mPeerMgr )
{
mPeerMgr - > UpdateOwnAddress ( mLocalAddr , mExtAddr ) ;
}
2018-02-26 08:49:06 +01:00
std : : cerr < < __PRETTY_FUNCTION__
< < " local address changed, resetting network " < < std : : endl ;
2011-07-09 09:36:37 +00:00
netReset ( ) ;
}
return 1 ;
}
2011-07-09 18:39:34 +00:00
/**********************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Interfaces * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* to allow resets of network stuff */
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : addNetListener ( pqiNetListener * listener )
2011-07-09 18:39:34 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetListeners . push_back ( listener ) ;
}
2011-07-09 09:36:37 +00:00
2013-09-13 14:35:19 +00:00
bool p3NetMgrIMPL : : setLocalAddress ( const struct sockaddr_storage & addr )
2011-07-09 18:39:34 +00:00
{
bool changed = false ;
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2014-10-18 00:19:05 +00:00
if ( ! sockaddr_storage_same ( mLocalAddr , addr ) )
2011-07-09 18:39:34 +00:00
{
changed = true ;
}
mLocalAddr = addr ;
}
if ( changed )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::setLocalAddress() Calling NetReset " < < std : : endl ;
2011-07-09 18:39:34 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr::setLocalAddress() local address changed, resetting network " ) ;
2011-07-09 18:39:34 +00:00
netReset ( ) ;
}
return true ;
}
2015-06-01 22:03:07 +00:00
bool p3NetMgrIMPL : : getExtAddress ( struct sockaddr_storage & addr )
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
if ( mNetFlags . mExtAddrOk )
{
2015-06-06 13:50:11 +00:00
addr = mNetFlags . mExtAddr ;
2015-06-01 22:03:07 +00:00
return true ;
}
else
return false ;
}
2011-07-09 18:39:34 +00:00
2013-09-13 14:35:19 +00:00
bool p3NetMgrIMPL : : setExtAddress ( const struct sockaddr_storage & addr )
2011-07-09 18:39:34 +00:00
{
bool changed = false ;
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2016-01-25 22:39:10 -05:00
if ( ! sockaddr_storage_same ( mExtAddr , addr ) )
2011-07-09 18:39:34 +00:00
{
changed = true ;
}
mExtAddr = addr ;
}
if ( changed )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::setExtAddress() Calling NetReset " < < std : : endl ;
2011-07-09 18:39:34 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr::setExtAddress() ext address changed, resetting network " ) ;
2011-07-09 18:39:34 +00:00
netReset ( ) ;
}
return true ;
}
2011-07-09 09:36:37 +00:00
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : setNetworkMode ( uint32_t netMode )
2011-07-09 18:39:34 +00:00
{
uint32_t oldNetMode ;
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
/* only change TRY flags */
2011-07-09 09:36:37 +00:00
2011-07-09 18:39:34 +00:00
oldNetMode = mNetMode ;
2015-05-25 14:02:45 +00:00
# ifdef NETMGR_DEBUG
std : : cerr < < " p3NetMgrIMPL::setNetworkMode() " ;
std : : cerr < < " Existing netMode: " < < mNetMode ;
std : : cerr < < " Input netMode: " < < netMode ;
std : : cerr < < std : : endl ;
# endif
2011-07-09 18:39:34 +00:00
mNetMode & = ~ ( RS_NET_MODE_TRYMODE ) ;
switch ( netMode & RS_NET_MODE_ACTUAL )
{
case RS_NET_MODE_EXT :
mNetMode | = RS_NET_MODE_TRY_EXT ;
break ;
case RS_NET_MODE_UPNP :
mNetMode | = RS_NET_MODE_TRY_UPNP ;
break ;
2013-09-03 13:35:39 +00:00
case RS_NET_MODE_HIDDEN :
2013-09-09 02:10:49 +00:00
mNetMode | = RS_NET_MODE_TRY_LOOPBACK ;
2013-09-03 13:35:39 +00:00
break ;
2011-07-09 18:39:34 +00:00
default :
case RS_NET_MODE_UDP :
mNetMode | = RS_NET_MODE_TRY_UDP ;
break ;
}
}
if ( ( netMode & RS_NET_MODE_ACTUAL ) ! = ( oldNetMode & RS_NET_MODE_ACTUAL ) )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG_RESET
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::setNetworkMode() Calling NetReset " < < std : : endl ;
2011-07-09 18:39:34 +00:00
# endif
2012-04-14 22:38:24 +00:00
rslog ( RSL_WARNING , p3netmgrzone , " p3NetMgr::setNetworkMode() Net Mode changed, resetting network " ) ;
2011-07-09 18:39:34 +00:00
netReset ( ) ;
}
return true ;
}
2013-09-28 08:09:59 +00:00
bool p3NetMgrIMPL : : setVisState ( uint16_t vs_disc , uint16_t vs_dht )
2011-07-09 18:39:34 +00:00
{
2011-08-12 13:42:30 +00:00
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2013-09-28 08:09:59 +00:00
mVsDisc = vs_disc ;
2015-05-25 14:02:45 +00:00
mVsDht = vs_dht ;
2011-07-09 18:39:34 +00:00
2011-08-12 13:42:30 +00:00
/* if we've started up - then tweak Dht On/Off */
if ( mNetStatus ! = RS_NET_UNKNOWN )
{
2013-09-28 08:09:59 +00:00
enableNetAssistConnect ( mVsDht ! = RS_VS_DHT_OFF ) ;
2011-07-09 18:39:34 +00:00
}
2011-08-12 13:42:30 +00:00
2011-07-09 18:39:34 +00:00
return true ;
}
/**********************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Interfaces * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-09 09:36:37 +00:00
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : addNetAssistFirewall ( uint32_t id , pqiNetAssistFirewall * fwAgent )
2011-07-09 09:36:37 +00:00
{
mFwAgents [ id ] = fwAgent ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : enableNetAssistFirewall ( bool on )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > enable ( on ) ;
}
return true ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistFirewallEnabled ( )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( it - > second ) - > getEnabled ( ) )
{
return true ;
}
}
return false ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistFirewallActive ( )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( it - > second ) - > getActive ( ) )
{
return true ;
}
}
return false ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistFirewallShutdown ( )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > shutdown ( ) ;
}
return true ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistFirewallPorts ( uint16_t iport , uint16_t eport )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > setInternalPort ( iport ) ;
( it - > second ) - > setExternalPort ( eport ) ;
}
return true ;
}
2013-09-13 14:35:19 +00:00
bool p3NetMgrIMPL : : netAssistExtAddress ( struct sockaddr_storage & extAddr )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mFwAgents . begin ( ) ; it ! = mFwAgents . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( it - > second ) - > getActive ( ) )
{
if ( ( it - > second ) - > getExternalAddress ( extAddr ) )
{
return true ;
}
}
}
return false ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : addNetAssistConnect ( uint32_t id , pqiNetAssistConnect * dht )
2011-07-09 09:36:37 +00:00
{
mDhts [ id ] = dht ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : enableNetAssistConnect ( bool on )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::enableNetAssistConnect( " < < on < < " ) " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > enable ( on ) ;
}
return true ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistConnectEnabled ( )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( it - > second ) - > getEnabled ( ) )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectEnabled() YES " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
return true ;
}
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectEnabled() NO " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistConnectActive ( )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( it - > second ) - > getActive ( ) )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectActive() ACTIVE " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
return true ;
}
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectActive() INACTIVE " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistConnectStats ( uint32_t & netsize , uint32_t & localnetsize )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
if ( ( ( it - > second ) - > getActive ( ) ) & & ( ( it - > second ) - > getNetworkStats ( netsize , localnetsize ) ) )
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectStats( " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < netsize < < " , " < < localnetsize < < " ) " ;
std : : cerr < < std : : endl ;
# endif
return true ;
}
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectStats() INACTIVE " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : netAssistConnectShutdown ( )
2011-07-09 09:36:37 +00:00
{
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistConnectShutdown() " ;
2011-07-09 09:36:37 +00:00
std : : cerr < < std : : endl ;
# endif
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > shutdown ( ) ;
}
return true ;
}
2014-03-17 20:56:06 +00:00
bool p3NetMgrIMPL : : netAssistFriend ( const RsPeerId & id , bool on )
2011-07-09 09:36:37 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2011-07-10 19:50:12 +00:00
# ifdef NETMGR_DEBUG
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistFriend( " < < id < < " , " < < on < < " ) " ;
2011-07-10 19:50:12 +00:00
std : : cerr < < std : : endl ;
# endif
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
2011-07-10 19:50:12 +00:00
if ( on )
( it - > second ) - > findPeer ( id ) ;
else
( it - > second ) - > dropPeer ( id ) ;
2011-07-09 09:36:37 +00:00
}
return true ;
}
2014-03-17 20:56:06 +00:00
bool p3NetMgrIMPL : : netAssistKnownPeer ( const RsPeerId & id , const struct sockaddr_storage & addr , uint32_t flags )
2011-11-22 13:24:42 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
# ifdef NETMGR_DEBUG
std : : cerr < < " p3NetMgrIMPL::netAssistKnownPeer( " < < id < < " ) " ;
std : : cerr < < std : : endl ;
# endif
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-11-22 13:24:42 +00:00
{
( it - > second ) - > addKnownPeer ( id , addr , flags ) ;
}
return true ;
}
2013-09-13 14:35:19 +00:00
bool p3NetMgrIMPL : : netAssistBadPeer ( const struct sockaddr_storage & addr , uint32_t reason , uint32_t flags , uint32_t age )
2011-11-25 00:58:01 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
# ifdef NETMGR_DEBUG
2013-09-13 14:35:19 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistBadPeer( " < < sockaddr_storage_iptostring ( addr ) < < " ) " ;
2011-11-25 00:58:01 +00:00
std : : cerr < < std : : endl ;
# endif
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-11-25 00:58:01 +00:00
{
( it - > second ) - > addBadPeer ( addr , reason , flags , age ) ;
}
return true ;
}
2011-11-22 13:24:42 +00:00
2011-07-21 11:28:19 +00:00
bool p3NetMgrIMPL : : netAssistAttach ( bool on )
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
# ifdef NETMGR_DEBUG
std : : cerr < < " p3NetMgrIMPL::netAssistAttach( " < < on < < " ) " ;
std : : cerr < < std : : endl ;
# endif
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-21 11:28:19 +00:00
{
( it - > second ) - > setAttachMode ( on ) ;
}
return true ;
}
2011-07-13 22:23:40 +00:00
2014-03-17 20:56:06 +00:00
bool p3NetMgrIMPL : : netAssistStatusUpdate ( const RsPeerId & id , int state )
2011-07-13 22:23:40 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
# ifdef NETMGR_DEBUG
2013-09-13 14:35:19 +00:00
std : : cerr < < " p3NetMgrIMPL::netAssistStatusUpdate( " < < id < < " , " < < state < < " ) " ;
2011-07-13 22:23:40 +00:00
std : : cerr < < std : : endl ;
# endif
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-13 22:23:40 +00:00
{
( it - > second ) - > ConnectionFeedback ( id , state ) ;
}
return true ;
}
2013-09-13 14:35:19 +00:00
bool p3NetMgrIMPL : : netAssistSetAddress ( const struct sockaddr_storage & /*laddr*/ ,
const struct sockaddr_storage & /*eaddr*/ ,
2011-08-12 13:42:30 +00:00
uint32_t /*mode*/ )
2011-07-09 09:36:37 +00:00
{
#if 0
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-09 09:36:37 +00:00
{
( it - > second ) - > setExternalInterface ( laddr , eaddr , mode ) ;
}
# endif
return true ;
}
2012-01-14 16:41:00 +00:00
void p3NetMgrIMPL : : netAssistTick ( )
2011-07-10 19:50:12 +00:00
{
std : : map < uint32_t , pqiNetAssistConnect * > : : iterator it ;
2014-10-24 22:07:26 +00:00
for ( it = mDhts . begin ( ) ; it ! = mDhts . end ( ) ; + + it )
2011-07-10 19:50:12 +00:00
{
( it - > second ) - > tick ( ) ;
}
2012-01-14 16:41:00 +00:00
std : : map < uint32_t , pqiNetAssistFirewall * > : : iterator fit ;
2014-10-24 22:07:26 +00:00
for ( fit = mFwAgents . begin ( ) ; fit ! = mFwAgents . end ( ) ; + + fit )
2012-01-14 16:41:00 +00:00
{
( fit - > second ) - > tick ( ) ;
}
2011-07-10 19:50:12 +00:00
return ;
}
2011-07-09 09:36:37 +00:00
/**********************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * Network State * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : getUPnPState ( )
2011-07-09 09:36:37 +00:00
{
return netAssistFirewallActive ( ) ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : getUPnPEnabled ( )
2011-07-09 09:36:37 +00:00
{
return netAssistFirewallEnabled ( ) ;
}
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : getDHTEnabled ( )
2011-07-09 09:36:37 +00:00
{
return netAssistConnectEnabled ( ) ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : getNetStatus ( pqiNetStatus & status )
2011-07-09 09:36:37 +00:00
{
/* cannot lock local stack, then call DHT... as this can cause lock up */
/* must extract data... then update mNetFlags */
bool dhtOk = netAssistConnectActive ( ) ;
2016-05-12 20:00:44 +02:00
uint32_t netsize = 0 , rsnetsize = 0 ;
2011-07-09 09:36:37 +00:00
netAssistConnectStats ( netsize , rsnetsize ) ;
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
/* quick update of the stuff that can change! */
mNetFlags . mDhtOk = dhtOk ;
mNetFlags . mDhtNetworkSize = netsize ;
mNetFlags . mDhtRsNetworkSize = rsnetsize ;
status = mNetFlags ;
}
2011-07-09 18:39:34 +00:00
/**********************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ExtAddrFinder * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-13 14:22:25 +00:00
bool p3NetMgrIMPL : : getIPServersEnabled ( )
2011-07-09 18:39:34 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mUseExtAddrFinder ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : getIPServersList ( std : : list < std : : string > & ip_servers )
2011-07-09 18:39:34 +00:00
{
mExtAddrFinder - > getIPServersList ( ip_servers ) ;
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : setIPServersEnabled ( bool b )
2011-07-09 18:39:34 +00:00
{
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mUseExtAddrFinder = b ;
}
2011-07-10 18:19:46 +00:00
# ifdef NETMGR_DEBUG
2011-07-09 18:39:34 +00:00
std : : cerr < < " p3NetMgr: setIPServers to " < < b < < std : : endl ;
# endif
}
2011-07-12 14:16:46 +00:00
/**********************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * NetStateBox * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2011-07-13 14:22:25 +00:00
uint32_t p3NetMgrIMPL : : getNetStateMode ( )
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mNetStateBox . getNetStateMode ( ) ;
}
2011-07-13 14:22:25 +00:00
uint32_t p3NetMgrIMPL : : getNetworkMode ( )
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mNetStateBox . getNetworkMode ( ) ;
}
2011-07-13 14:22:25 +00:00
uint32_t p3NetMgrIMPL : : getNatTypeMode ( )
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mNetStateBox . getNatTypeMode ( ) ;
}
2011-07-13 14:22:25 +00:00
uint32_t p3NetMgrIMPL : : getNatHoleMode ( )
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mNetStateBox . getNatHoleMode ( ) ;
}
2011-07-13 14:22:25 +00:00
uint32_t p3NetMgrIMPL : : getConnectModes ( )
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
return mNetStateBox . getConnectModes ( ) ;
}
/* These are the regular updates from Dht / Stunners */
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : updateNetStateBox_temporal ( )
2011-07-12 14:16:46 +00:00
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
struct sockaddr_storage tmpaddr ;
sockaddr_storage_clear ( tmpaddr ) ;
2011-07-12 14:16:46 +00:00
2016-06-21 13:55:23 +02:00
# ifdef RS_USE_DHT_STUNNER
2016-06-21 14:02:52 +02:00
uint8_t isstable = 0 ;
2011-07-12 14:16:46 +00:00
if ( mDhtStunner )
{
/* input network bits */
if ( mDhtStunner - > getExternalAddr ( tmpaddr , isstable ) )
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressStunDht ( tmpaddr , isstable ) ;
2011-07-12 14:16:46 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() DhtStunner: " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < sockaddr_storage_tostring ( tmpaddr ) ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " Stable: " < < ( uint32_t ) isstable ;
std : : cerr < < std : : endl ;
# endif
}
}
if ( mProxyStunner )
{
/* input network bits */
if ( mProxyStunner - > getExternalAddr ( tmpaddr , isstable ) )
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressStunProxy ( tmpaddr , isstable ) ;
2011-07-12 14:16:46 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() ProxyStunner: " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < sockaddr_storage_tostring ( tmpaddr ) ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " Stable: " < < ( uint32_t ) isstable ;
std : : cerr < < std : : endl ;
# endif
}
}
2016-06-21 13:55:23 +02:00
# endif // RS_USE_DHT_STUNNER
2011-07-12 14:16:46 +00:00
{
bool dhtOn = netAssistConnectEnabled ( ) ;
bool dhtActive = netAssistConnectActive ( ) ;
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetStateBox . setDhtState ( dhtOn , dhtActive ) ;
}
/* now we check if a WebIP address is required? */
2016-06-21 13:55:23 +02:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-12 14:16:46 +00:00
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
uint32_t netstate = mNetStateBox . getNetStateMode ( ) ;
uint32_t netMode = mNetStateBox . getNetworkMode ( ) ;
uint32_t natType = mNetStateBox . getNatTypeMode ( ) ;
uint32_t natHole = mNetStateBox . getNatHoleMode ( ) ;
uint32_t connect = mNetStateBox . getConnectModes ( ) ;
std : : string netstatestr = NetStateNetStateString ( netstate ) ;
std : : string connectstr = NetStateConnectModesString ( connect ) ;
std : : string natholestr = NetStateNatHoleString ( natHole ) ;
std : : string nattypestr = NetStateNatTypeString ( natType ) ;
std : : string netmodestr = NetStateNetworkModeString ( netMode ) ;
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() NetStateBox Thinking " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
std : : cerr < < " \t NetState: " < < netstatestr ;
std : : cerr < < std : : endl ;
2017-07-21 19:02:34 +02:00
std : : cerr < < " \t ConnectModes: " < < connectstr ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
std : : cerr < < " \t NetworkMode: " < < netmodestr ;
std : : cerr < < std : : endl ;
std : : cerr < < " \t NatHole: " < < natholestr ;
std : : cerr < < std : : endl ;
std : : cerr < < " \t NatType: " < < nattypestr ;
std : : cerr < < std : : endl ;
}
2016-06-21 13:55:23 +02:00
# endif
2011-07-12 14:16:46 +00:00
updateNatSetting ( ) ;
}
2011-07-13 11:41:25 +00:00
# define NET_STUNNER_PERIOD_FAST (-1) // default of Stunner.
2011-07-17 16:24:45 +00:00
# define NET_STUNNER_PERIOD_SLOW (120) // This needs to be as small Routers will allow... try 2 minutes.
// FOR TESTING ONLY.
//#define NET_STUNNER_PERIOD_SLOW (60) // 3 minutes.
2011-07-12 14:16:46 +00:00
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : updateNatSetting ( )
2011-07-12 14:16:46 +00:00
{
bool updateRefreshRate = false ;
uint32_t natType = RSNET_NATTYPE_UNKNOWN ;
uint32_t natHole = RSNET_NATHOLE_UNKNOWN ;
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
natType = mNetStateBox . getNatTypeMode ( ) ;
natHole = mNetStateBox . getNatHoleMode ( ) ;
if ( ( natType ! = mOldNatType ) | | ( natHole ! = mOldNatHole ) )
{
mOldNatType = natType ;
mOldNatHole = natHole ;
updateRefreshRate = true ;
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() NatType Change! " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " \t NatType: " < < NetStateNatTypeString ( natType ) ;
std : : cerr < < " \t NatHole: " < < NetStateNatHoleString ( natHole ) ;
std : : cerr < < std : : endl ;
# endif
}
}
// MUST also use this chance to set ATTACH flag for DHT.
if ( updateRefreshRate )
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_temporal() Updating Refresh Rate, based on changed NatType " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
# endif
2016-06-21 13:55:23 +02:00
# ifdef RS_USE_DHT_STUNNER
2011-07-12 14:16:46 +00:00
switch ( natType )
{
case RSNET_NATTYPE_RESTRICTED_CONE :
{
if ( ( natHole = = RSNET_NATHOLE_NONE ) | | ( natHole = = RSNET_NATHOLE_UNKNOWN ) )
{
mProxyStunner - > setRefreshPeriod ( NET_STUNNER_PERIOD_FAST ) ;
}
else
{
mProxyStunner - > setRefreshPeriod ( NET_STUNNER_PERIOD_SLOW ) ;
}
break ;
}
case RSNET_NATTYPE_NONE :
case RSNET_NATTYPE_UNKNOWN :
case RSNET_NATTYPE_SYMMETRIC :
2011-07-21 11:28:19 +00:00
case RSNET_NATTYPE_DETERM_SYM :
2011-07-12 14:16:46 +00:00
case RSNET_NATTYPE_FULL_CONE :
case RSNET_NATTYPE_OTHER :
mProxyStunner - > setRefreshPeriod ( NET_STUNNER_PERIOD_SLOW ) ;
break ;
}
2016-06-21 13:55:23 +02:00
# endif // RS_USE_DHT_STUNNER
2011-07-21 11:28:19 +00:00
/* This controls the Attach mode of the DHT...
* which effectively makes the DHT " attach " to Open Nodes .
* So that messages can get through .
* We only want to be attached - if we don ' t have a stable DHT port .
*/
if ( ( natHole = = RSNET_NATHOLE_NONE ) | | ( natHole = = RSNET_NATHOLE_UNKNOWN ) )
{
switch ( natType )
{
/* switch to attach mode if we have a bad firewall */
case RSNET_NATTYPE_UNKNOWN :
case RSNET_NATTYPE_SYMMETRIC :
case RSNET_NATTYPE_RESTRICTED_CONE :
case RSNET_NATTYPE_DETERM_SYM :
case RSNET_NATTYPE_OTHER :
netAssistAttach ( true ) ;
break ;
/* switch off attach mode if we have a nice firewall */
case RSNET_NATTYPE_NONE :
case RSNET_NATTYPE_FULL_CONE :
netAssistAttach ( false ) ;
break ;
}
}
else
{
// Switch off Firewall Mode (Attach)
netAssistAttach ( false ) ;
}
2011-07-12 14:16:46 +00:00
}
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : updateNetStateBox_startup ( )
2011-07-12 14:16:46 +00:00
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
# endif
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
/* fill in the data */
2013-09-13 14:35:19 +00:00
struct sockaddr_storage tmpip ;
2011-07-12 14:16:46 +00:00
/* net Assist */
if ( netAssistExtAddress ( tmpip ) )
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " Ext supplied from netAssistExternalAddress() " ;
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
if ( sockaddr_storage_isValidNet ( tmpip ) )
2011-07-12 14:16:46 +00:00
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " netAssist Returned: " < < sockaddr_storage_tostring ( tmpip ) ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressUPnP ( true , tmpip ) ;
2011-07-12 14:16:46 +00:00
}
else
{
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressUPnP ( false , tmpip ) ;
2011-07-12 14:16:46 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " ERROR Bad Address supplied from netAssistExternalAddress() " ;
std : : cerr < < std : : endl ;
# endif
}
}
else
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " netAssistExtAddress() is not active " ;
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressUPnP ( false , tmpip ) ;
2011-07-12 14:16:46 +00:00
}
/* ExtAddrFinder */
if ( mUseExtAddrFinder )
{
2013-09-13 14:35:19 +00:00
bool extFinderOk = mExtAddrFinder - > hasValidIP ( tmpip ) ;
2011-07-12 14:16:46 +00:00
if ( extFinderOk )
{
/* best guess at port */
2013-09-13 14:35:19 +00:00
sockaddr_storage_setport ( tmpip , sockaddr_storage_port ( mNetFlags . mLocalAddr ) ) ;
2011-07-12 14:16:46 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2013-09-13 14:35:19 +00:00
std : : cerr < < " ExtAddrFinder Returned: " < < sockaddr_storage_iptostring ( tmpip ) ;
2011-07-12 14:16:46 +00:00
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressWebIP ( true , tmpip ) ;
2011-07-12 14:16:46 +00:00
}
else
{
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressWebIP ( false , tmpip ) ;
2011-07-12 14:16:46 +00:00
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " ExtAddrFinder hasn't found an address yet " ;
std : : cerr < < std : : endl ;
# endif
}
}
else
{
# ifdef NETMGR_DEBUG_STATEBOX
2011-07-13 14:22:25 +00:00
std : : cerr < < " p3NetMgrIMPL::updateNetStateBox_startup() " ;
2011-07-12 14:16:46 +00:00
std : : cerr < < " ExtAddrFinder is not active " ;
std : : cerr < < std : : endl ;
# endif
2013-09-13 14:35:19 +00:00
mNetStateBox . setAddressWebIP ( false , tmpip ) ;
2011-07-12 14:16:46 +00:00
}
2011-07-30 16:57:40 +00:00
/* finally - if the user has set Forwarded, pass it on */
if ( mNetMode & RS_NET_MODE_TRY_EXT )
{
mNetStateBox . setPortForwarded ( true , 0 ) ; // Port unknown for now.
}
2011-07-12 14:16:46 +00:00
}
}
2011-07-13 14:22:25 +00:00
void p3NetMgrIMPL : : updateNetStateBox_reset ( )
2011-07-12 14:16:46 +00:00
{
{
RsStackMutex stack ( mNetMtx ) ; /****** STACK LOCK MUTEX *******/
mNetStateBox . reset ( ) ;
2011-07-29 15:20:40 +00:00
mOldNatHole = RSNET_NATHOLE_UNKNOWN ;
mOldNatType = RSNET_NATTYPE_UNKNOWN ;
2011-07-12 14:16:46 +00:00
}
}