2011-07-09 05:36:37 -04:00
/*
* libretroshare / src / pqi : p3linkmgr . cc
*
* 3 P / PQI network interface for RetroShare .
*
* Copyright 2007 - 2011 by Robert Fernie .
*
* This library is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation .
*
* This library is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Library General Public License for more details .
*
* You should have received a copy of the GNU Library General Public
* License along with this library ; if not , write to the Free Software
* Foundation , Inc . , 59 Temple Place , Suite 330 , Boston , MA 02111 - 1307
* USA .
*
* Please report all bugs and problems to " retroshare@lunamutt.com " .
*
*/
# include "pqi/p3linkmgr.h"
2011-07-09 14:39:34 -04:00
# include "pqi/p3peermgr.h"
# include "pqi/p3netmgr.h"
2014-01-07 17:51:22 -05:00
# include "rsserver/p3face.h"
2011-07-09 05:36:37 -04:00
# 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"
# include "util/rsnet.h"
# include "pqi/authgpg.h"
# include "util/rsprint.h"
# include "util/rsdebug.h"
2012-04-14 18:38:24 -04:00
# include "util/rsstring.h"
2017-04-24 16:47:08 -04:00
# include "rsitems/rsconfigitems.h"
2012-01-08 17:15:19 -05:00
2011-07-09 05:36:37 -04:00
# include "retroshare/rsiface.h"
2012-01-08 17:15:19 -05:00
# include "retroshare/rspeers.h"
2015-05-25 10:02:45 -04:00
# include "retroshare/rsdht.h"
2015-05-31 15:52:51 -04:00
# include "retroshare/rsbanlist.h"
2011-07-09 05:36:37 -04:00
/* Network setup States */
2016-06-18 07:00:15 -04:00
static struct RsLog : : logInfo p3connectzoneInfo = { RsLog : : Default , " p3connect " } ;
# define p3connectzone &p3connectzoneInfo
2011-07-09 05:36:37 -04:00
/****
2011-07-10 14:19:46 -04:00
* # define LINKMGR_DEBUG 1
2011-07-10 20:52:12 -04:00
* # define LINKMGR_DEBUG_CONNFAIL 1
2011-07-11 08:11:58 -04:00
* # define LINKMGR_DEBUG_ACTIONS 1
2012-01-13 20:54:17 -05:00
* # define LINKMGR_DEBUG_LINKTYPE 1
2011-07-09 05:36:37 -04:00
* * */
2011-07-18 04:37:11 -04:00
/****
* # define DISABLE_UDP_CONNECTIONS 1
* * */
2011-07-10 14:19:46 -04:00
2011-07-09 05:36:37 -04:00
/****
* # define P3CONNMGR_NO_TCP_CONNECTIONS 1
* * */
/****
* # define P3CONNMGR_NO_AUTO_CONNECTION 1
* * */
2011-07-18 20:17:54 -04:00
//#define P3CONNMGR_NO_TCP_CONNECTIONS 1
2011-07-18 20:06:09 -04:00
2011-07-09 05:36:37 -04:00
const uint32_t P3CONNMGR_TCP_DEFAULT_DELAY = 3 ; /* 2 Seconds? is it be enough! */
2017-07-15 08:58:55 -04:00
//const uint32_t P3CONNMGR_UDP_DEFAULT_DELAY = 3; /* 2 Seconds? is it be enough! */
2011-07-09 05:36:37 -04:00
const uint32_t P3CONNMGR_TCP_DEFAULT_PERIOD = 10 ;
2011-07-18 20:06:09 -04:00
const uint32_t P3CONNMGR_UDP_DEFAULT_PERIOD = 30 ; // this represents how long it stays at the default TTL (4), before rising.
2011-07-09 05:36:37 -04:00
# define MAX_AVAIL_PERIOD 230 //times a peer stay in available state when not connected
# define MIN_RETRY_PERIOD 140
# define MAX_RANDOM_ATTEMPT_OFFSET 6 // seconds.
void printConnectState ( std : : ostream & out , peerConnectState & peer ) ;
peerConnectAddress : : peerConnectAddress ( )
2015-07-13 07:03:18 -04:00
: delay ( 0 ) , period ( 0 ) , type ( 0 ) , flags ( 0 ) , ts ( 0 ) , bandwidth ( 0 ) , domain_port ( 0 )
2011-07-09 05:36:37 -04:00
{
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( addr ) ;
2015-07-13 07:03:18 -04:00
sockaddr_storage_clear ( proxyaddr ) ;
sockaddr_storage_clear ( srcaddr ) ;
2011-07-09 05:36:37 -04:00
}
peerAddrInfo : : peerAddrInfo ( )
: found ( false ) , type ( 0 ) , ts ( 0 )
{
}
peerConnectState : : peerConnectState ( )
2014-03-17 16:56:06 -04:00
: connecttype ( 0 ) ,
2011-07-09 05:36:37 -04:00
lastavailable ( 0 ) ,
lastattempt ( 0 ) ,
2011-07-09 14:39:34 -04:00
name ( " " ) ,
2011-07-09 05:36:37 -04:00
state ( 0 ) , actions ( 0 ) ,
source ( 0 ) ,
2013-08-22 03:26:13 -04:00
inConnAttempt ( 0 ) ,
wasDeniedConnection ( false ) , deniedTS ( false ) , deniedInConnAttempt ( false )
2011-07-09 05:36:37 -04:00
{
}
std : : string textPeerConnectState ( peerConnectState & state )
{
2014-03-17 16:56:06 -04:00
return " Id: " + state . id . toStdString ( ) + " \n " ;
2011-07-09 05:36:37 -04:00
}
2011-07-09 14:39:34 -04:00
/*********
* NOTES :
*
* p3LinkMgr doesn ' t store anything . All configuration is handled by p3PeerMgr .
*
* p3LinkMgr recvs the Discovery / Dht / Status updates . . . . tries the address .
* at success the address is pushed to p3PeerMgr for storage .
*
*/
2011-07-09 05:36:37 -04:00
2011-07-13 10:22:25 -04:00
p3LinkMgrIMPL : : p3LinkMgrIMPL ( p3PeerMgrIMPL * peerMgr , p3NetMgrIMPL * netMgr )
2011-07-09 14:39:34 -04:00
: mPeerMgr ( peerMgr ) , mNetMgr ( netMgr ) , mLinkMtx ( " p3LinkMgr " ) , mStatusChanged ( false )
2011-07-09 05:36:37 -04:00
{
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2011-07-09 14:39:34 -04:00
mDNSResolver = new DNSResolver ( ) ;
2011-07-09 05:36:37 -04:00
mRetryPeriod = MIN_RETRY_PERIOD ;
/* setup Banned Ip Address - static for now
*/
2013-09-13 10:35:19 -04:00
struct sockaddr_storage bip ;
sockaddr_storage_clear ( bip ) ;
2015-05-31 15:52:51 -04:00
struct sockaddr_in * addr = ( struct sockaddr_in * ) & bip ;
2013-09-13 10:35:19 -04:00
addr - > sin_family = AF_INET ;
addr - > sin_addr . s_addr = 1 ;
addr - > sin_port = htons ( 0 ) ;
2011-07-09 05:36:37 -04:00
mBannedIpList . push_back ( bip ) ;
}
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-09 05:36:37 -04:00
std : : cerr < < " p3LinkMgr() Startup " < < std : : endl ;
# endif
return ;
}
2015-06-18 13:05:02 -04:00
p3LinkMgrIMPL : : ~ p3LinkMgrIMPL ( )
{
delete ( mDNSResolver ) ;
}
2013-09-13 10:35:19 -04:00
bool p3LinkMgrIMPL : : setLocalAddress ( const struct sockaddr_storage & addr )
2011-07-09 21:49:39 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
mLocalAddress = addr ;
2011-07-19 14:55:32 -04:00
return true ;
2011-07-09 21:49:39 -04:00
}
2013-09-13 10:35:19 -04:00
bool p3LinkMgrIMPL : : getLocalAddress ( struct sockaddr_storage & addr )
2011-07-09 21:49:39 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2013-09-13 10:35:19 -04:00
addr = mLocalAddress ;
return true ;
2011-07-09 21:49:39 -04:00
}
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : isOnline ( const RsPeerId & ssl_id )
2011-07-09 21:49:39 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 21:49:39 -04:00
it = mFriendList . find ( ssl_id ) ;
if ( it = = mFriendList . end ( ) )
{
return false ;
}
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
return true ;
}
return false ;
}
2011-07-09 05:36:37 -04:00
2012-01-08 17:15:19 -05:00
2014-03-17 16:56:06 -04:00
uint32_t p3LinkMgrIMPL : : getLinkType ( const RsPeerId & ssl_id )
2012-01-08 17:15:19 -05:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2012-01-08 17:15:19 -05:00
it = mFriendList . find ( ssl_id ) ;
if ( it = = mFriendList . end ( ) )
{
return 0 ;
}
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
return it - > second . linkType ;
}
return 0 ;
}
2014-03-17 16:56:06 -04:00
void p3LinkMgrIMPL : : getOnlineList ( std : : list < RsPeerId > & ssl_peers )
2011-07-09 14:39:34 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = mFriendList . begin ( ) ; it ! = mFriendList . end ( ) ; + + it )
2011-07-09 14:39:34 -04:00
{
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
ssl_peers . push_back ( it - > first ) ;
}
2011-07-09 05:36:37 -04:00
}
2011-07-09 14:39:34 -04:00
return ;
}
2011-07-09 05:36:37 -04:00
2014-03-17 16:56:06 -04:00
void p3LinkMgrIMPL : : getFriendList ( std : : list < RsPeerId > & ssl_peers )
2011-07-09 14:39:34 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = mFriendList . begin ( ) ; it ! = mFriendList . end ( ) ; + + it )
2011-07-09 05:36:37 -04:00
{
2011-07-09 14:39:34 -04:00
ssl_peers . push_back ( it - > first ) ;
2011-07-09 05:36:37 -04:00
}
2011-07-09 14:39:34 -04:00
return ;
2011-07-09 05:36:37 -04:00
}
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : getPeerName ( const RsPeerId & ssl_id , std : : string & name )
2011-08-07 17:11:00 -04:00
{
return mPeerMgr - > getPeerName ( ssl_id , name ) ;
}
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : getFriendNetStatus ( const RsPeerId & id , peerConnectState & state )
2011-07-09 21:49:39 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 21:49:39 -04:00
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
return false ;
}
state = it - > second ;
2013-11-30 08:48:39 -05:00
2011-07-09 21:49:39 -04:00
return true ;
}
2011-07-09 14:39:34 -04:00
2014-03-17 16:56:06 -04:00
void p3LinkMgrIMPL : : setFriendVisibility ( const RsPeerId & id , bool isVisible )
2011-07-09 20:41:39 -04:00
{
/* set visibility */
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 20:41:39 -04:00
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
/* */
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::setFriendVisibility() ERROR peer unknown: " < < id ;
2011-07-09 20:41:39 -04:00
std : : cerr < < std : : endl ;
return ;
}
2011-07-09 05:36:37 -04:00
2011-07-09 20:41:39 -04:00
if ( it - > second . dhtVisible = = isVisible )
{
/* no change in state */
return ;
}
it - > second . dhtVisible = isVisible ;
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
/* dont worry about it */
return ;
}
}
2011-07-09 05:36:37 -04:00
2011-07-09 20:41:39 -04:00
/* switch the NetAssistOn/Off */
mNetMgr - > netAssistFriend ( id , isVisible ) ;
}
2011-07-09 05:36:37 -04:00
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : tick ( )
2011-07-09 05:36:37 -04:00
{
statusTick ( ) ;
tickMonitors ( ) ;
}
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : statusTick ( )
2011-07-09 05:36:37 -04:00
{
/* iterate through peers ...
* if been available for long time . . . remove flag
* if last attempt a while - retryConnect .
* etc .
*/
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG_TICK
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::statusTick() " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > retryIds ;
2011-07-09 05:36:37 -04:00
//std::list<std::string> dummyToRemove;
{
time_t now = time ( NULL ) ;
time_t oldavail = now - MAX_AVAIL_PERIOD ;
time_t retry = now - mRetryPeriod ;
RsStackMutex stack ( mLinkMtx ) ; /****** LOCK MUTEX ******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = mFriendList . begin ( ) ; it ! = mFriendList . end ( ) ; + + it )
2011-07-09 05:36:37 -04:00
{
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
continue ;
}
if ( ( it - > second . state & RS_PEER_S_ONLINE ) & &
( it - > second . lastavailable < oldavail ) )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG_TICK
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::statusTick() ONLINE TIMEOUT for: " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < it - > first ;
std : : cerr < < std : : endl ;
# endif
it - > second . state & = ( ~ RS_PEER_S_ONLINE ) ;
}
if ( it - > second . lastattempt < retry )
{
retryIds . push_back ( it - > first ) ;
}
}
}
# ifndef P3CONNMGR_NO_AUTO_CONNECTION
2017-07-26 04:30:33 -04:00
std : : list < RsPeerId > : : iterator it2 ;
for ( it2 = retryIds . begin ( ) ; it2 ! = retryIds . end ( ) ; + + it2 )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG_TICK
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::statusTick() RETRY TIMEOUT for: " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < * it2 ;
std : : cerr < < std : : endl ;
# endif
/* retry it! */
retryConnect ( * it2 ) ;
}
# endif
}
/******************************** Network Status *********************************
* Configuration Loading / Saving .
*/
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : addMonitor ( pqiMonitor * mon )
2011-07-09 05:36:37 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
std : : list < pqiMonitor * > : : iterator it ;
it = std : : find ( clients . begin ( ) , clients . end ( ) , mon ) ;
if ( it ! = clients . end ( ) )
{
return ;
}
mon - > setLinkMgr ( this ) ;
clients . push_back ( mon ) ;
return ;
}
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : removeMonitor ( pqiMonitor * mon )
2011-07-09 05:36:37 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
std : : list < pqiMonitor * > : : iterator it ;
it = std : : find ( clients . begin ( ) , clients . end ( ) , mon ) ;
if ( it = = clients . end ( ) )
{
return ;
}
( * it ) - > setLinkMgr ( NULL ) ;
clients . erase ( it ) ;
return ;
}
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : tickMonitors ( )
2011-07-09 05:36:37 -04:00
{
bool doStatusChange = false ;
std : : list < pqipeer > actionList ;
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 05:36:37 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
if ( mStatusChanged )
{
2011-07-11 08:11:58 -04:00
# ifdef LINKMGR_DEBUG_ACTIONS
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::tickMonitors() StatusChanged! List: " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
/* assemble list */
2014-10-24 18:07:26 -04:00
for ( it = mFriendList . begin ( ) ; it ! = mFriendList . end ( ) ; + + it )
2011-07-09 05:36:37 -04:00
{
if ( it - > second . actions )
{
/* add in */
pqipeer peer ;
peer . id = it - > second . id ;
peer . name = it - > second . name ;
peer . state = it - > second . state ;
peer . actions = it - > second . actions ;
/* reset action */
it - > second . actions = 0 ;
actionList . push_back ( peer ) ;
2011-07-11 08:11:58 -04:00
# ifdef LINKMGR_DEBUG_ACTIONS
2014-03-17 16:56:06 -04:00
std : : cerr < < " Friend: " < < peer . name < < " Id: " < < peer . id . toStdString ( ) < < " State: " < < peer . state ;
2011-07-09 05:36:37 -04:00
if ( peer . state & RS_PEER_S_FRIEND )
std : : cerr < < " S:RS_PEER_S_FRIEND " ;
if ( peer . state & RS_PEER_S_ONLINE )
std : : cerr < < " S:RS_PEER_S_ONLINE " ;
if ( peer . state & RS_PEER_S_CONNECTED )
std : : cerr < < " S:RS_PEER_S_CONNECTED " ;
std : : cerr < < " Actions: " < < peer . actions ;
if ( peer . actions & RS_PEER_NEW )
std : : cerr < < " A:RS_PEER_NEW " ;
if ( peer . actions & RS_PEER_MOVED )
std : : cerr < < " A:RS_PEER_MOVED " ;
if ( peer . actions & RS_PEER_CONNECTED )
std : : cerr < < " A:RS_PEER_CONNECTED " ;
if ( peer . actions & RS_PEER_DISCONNECTED )
std : : cerr < < " A:RS_PEER_DISCONNECTED " ;
if ( peer . actions & RS_PEER_CONNECT_REQ )
std : : cerr < < " A:RS_PEER_CONNECT_REQ " ;
std : : cerr < < std : : endl ;
# endif
/* notify GUI */
if ( peer . actions & RS_PEER_CONNECTED )
{
2014-01-07 17:51:22 -05:00
p3Notify * notify = RsServer : : notify ( ) ;
2011-07-09 05:36:37 -04:00
if ( notify )
{
2014-03-17 16:56:06 -04:00
notify - > AddPopupMessage ( RS_POPUP_CONNECT , peer . id . toStdString ( ) , " " , " Online: " ) ;
2014-11-11 15:28:45 -05:00
notify - > AddFeedItem ( RS_FEED_ITEM_PEER_CONNECT , peer . id . toStdString ( ) ) ;
2011-07-09 05:36:37 -04:00
}
}
}
}
2011-07-11 08:11:58 -04:00
2011-07-09 05:36:37 -04:00
/* do the Others as well! */
2014-10-24 18:07:26 -04:00
for ( it = mOthersList . begin ( ) ; it ! = mOthersList . end ( ) ; + + it )
2011-07-09 05:36:37 -04:00
{
if ( it - > second . actions )
{
/* add in */
pqipeer peer ;
peer . id = it - > second . id ;
peer . name = it - > second . name ;
peer . state = it - > second . state ;
peer . actions = it - > second . actions ;
/* reset action */
it - > second . actions = 0 ;
2011-07-11 08:11:58 -04:00
# ifdef LINKMGR_DEBUG_ACTIONS
2014-03-17 16:56:06 -04:00
std : : cerr < < " Other: " < < peer . name < < " Id: " < < peer . id . toStdString ( ) < < " State: " < < peer . state ;
2011-07-09 05:36:37 -04:00
if ( peer . state & RS_PEER_S_FRIEND )
std : : cerr < < " S:RS_PEER_S_FRIEND " ;
if ( peer . state & RS_PEER_S_ONLINE )
std : : cerr < < " S:RS_PEER_S_ONLINE " ;
if ( peer . state & RS_PEER_S_CONNECTED )
std : : cerr < < " S:RS_PEER_S_CONNECTED " ;
std : : cerr < < " Actions: " < < peer . actions ;
if ( peer . actions & RS_PEER_NEW )
std : : cerr < < " A:RS_PEER_NEW " ;
if ( peer . actions & RS_PEER_MOVED )
std : : cerr < < " A:RS_PEER_MOVED " ;
if ( peer . actions & RS_PEER_CONNECTED )
std : : cerr < < " A:RS_PEER_CONNECTED " ;
if ( peer . actions & RS_PEER_DISCONNECTED )
std : : cerr < < " A:RS_PEER_DISCONNECTED " ;
if ( peer . actions & RS_PEER_CONNECT_REQ )
std : : cerr < < " A:RS_PEER_CONNECT_REQ " ;
std : : cerr < < std : : endl ;
# endif
actionList . push_back ( peer ) ;
}
}
mStatusChanged = false ;
doStatusChange = true ;
}
} /****** UNLOCK STACK MUTEX ******/
/* NOTE - clients is accessed without mutex protection!!!!
* At the moment this is okay - as they are only added at the start .
* IF this changes - - - - must fix with second Mutex .
*/
if ( doStatusChange )
{
2011-07-11 08:11:58 -04:00
# ifdef LINKMGR_DEBUG_ACTIONS
2011-07-09 05:36:37 -04:00
std : : cerr < < " Sending to " < < clients . size ( ) < < " monitorClients " < < std : : endl ;
# endif
/* send to all monitors */
std : : list < pqiMonitor * > : : iterator mit ;
2014-10-24 18:07:26 -04:00
for ( mit = clients . begin ( ) ; mit ! = clients . end ( ) ; + + mit )
2011-07-09 05:36:37 -04:00
{
( * mit ) - > statusChange ( actionList ) ;
}
}
# ifdef WINDOWS_SYS
///////////////////////////////////////////////////////////
// hack for too many connections
/* notify all monitors */
std : : list < pqiMonitor * > : : iterator mit ;
2014-10-24 18:07:26 -04:00
for ( mit = clients . begin ( ) ; mit ! = clients . end ( ) ; + + mit ) {
2011-07-09 05:36:37 -04:00
( * mit ) - > statusChanged ( ) ;
}
///////////////////////////////////////////////////////////
# endif
2011-07-11 08:11:58 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
/* Now Cleanup OthersList (served its purpose (MOVE Action)) */
mOthersList . clear ( ) ;
}
2011-07-09 05:36:37 -04:00
}
2014-03-17 16:56:06 -04:00
const RsPeerId & p3LinkMgrIMPL : : getOwnId ( )
2011-07-09 05:36:37 -04:00
{
2014-03-17 16:56:06 -04:00
return AuthSSL : : getAuthSSL ( ) - > OwnId ( ) ;
2011-07-09 05:36:37 -04:00
}
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : connectAttempt ( const RsPeerId & id , struct sockaddr_storage & raddr ,
2013-09-13 10:35:19 -04:00
struct sockaddr_storage & proxyaddr ,
struct sockaddr_storage & srcaddr ,
2013-09-03 09:35:39 -04:00
uint32_t & delay , uint32_t & period , uint32_t & type , uint32_t & flags , uint32_t & bandwidth ,
std : : string & domain_addr , uint16_t & domain_port )
2011-07-09 05:36:37 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
/* check for existing */
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 05:36:37 -04:00
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() FAILED Not in FriendList! id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return false ;
}
if ( it - > second . connAddrs . size ( ) < 1 )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() FAILED No ConnectAddresses id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return false ;
}
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() Already FLAGGED as connected!!!! " < < std : : endl ;
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() But allowing anyway!!! " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2011-08-06 08:27:23 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::connectAttempt() ERROR ALREADY CONNECTED " ) ;
2011-07-09 05:36:37 -04:00
}
2014-03-17 16:56:06 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::connectAttempt() called id: " + id . toStdString ( ) ) ;
2011-08-06 08:27:23 -04:00
2011-07-09 05:36:37 -04:00
it - > second . lastattempt = time ( NULL ) ;
it - > second . inConnAttempt = true ;
it - > second . currentConnAddrAttempt = it - > second . connAddrs . front ( ) ;
it - > second . connAddrs . pop_front ( ) ;
2011-07-14 10:56:33 -04:00
raddr = it - > second . currentConnAddrAttempt . addr ;
2011-07-09 05:36:37 -04:00
delay = it - > second . currentConnAddrAttempt . delay ;
period = it - > second . currentConnAddrAttempt . period ;
type = it - > second . currentConnAddrAttempt . type ;
2011-07-13 18:23:40 -04:00
flags = it - > second . currentConnAddrAttempt . flags ;
2011-07-09 05:36:37 -04:00
2011-07-14 10:56:33 -04:00
proxyaddr = it - > second . currentConnAddrAttempt . proxyaddr ;
srcaddr = it - > second . currentConnAddrAttempt . srcaddr ;
bandwidth = it - > second . currentConnAddrAttempt . bandwidth ;
2013-09-03 09:35:39 -04:00
domain_addr = it - > second . currentConnAddrAttempt . domain_addr ;
domain_port = it - > second . currentConnAddrAttempt . domain_port ;
2012-01-08 17:15:19 -05:00
/********* Setup LinkType parameters **********/
# define TRICKLE_LIMIT 2001 // 2kb
# define LOW_BANDWIDTH_LIMIT 5001 // 5kb
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() Setting up LinkType " < < std : : endl ;
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type = " < < type < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType = 0 ;
if ( type & RS_NET_CONN_TCP_ALL )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type & TCP_ALL => TCP_UNKNOWN " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_TCP_UNKNOWN ;
}
else if ( type & RS_NET_CONN_UDP_ALL )
{
if ( flags & RS_CB_FLAG_MODE_UDP_DIRECT )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type & UDP_ALL && flags & DIRECT => UDP_DIRECT " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_UDP_DIRECT ;
}
else if ( flags & RS_CB_FLAG_MODE_UDP_PROXY )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type & UDP_ALL && flags & PROXY => UDP_PROXY " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_UDP_PROXY ;
}
else if ( flags & RS_CB_FLAG_MODE_UDP_RELAY )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type & UDP_ALL && flags & RELAY => UDP_RELAY " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_UDP_RELAY ;
}
else
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() type & UDP_ALL && else => UDP_UNKNOWN " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_UDP_UNKNOWN ;
}
}
else
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() else => TRANS_UNKNOWN " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_TRANS_UNKNOWN ;
}
2012-01-13 20:54:17 -05:00
if ( ( type & RS_NET_CONN_UDP_ALL ) & & ( flags & RS_CB_FLAG_MODE_UDP_RELAY ) )
2012-01-08 17:15:19 -05:00
{
if ( bandwidth < TRICKLE_LIMIT )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() flags & RELAY && band < TRICKLE => SPEED_TRICKLE " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_SPEED_TRICKLE ;
}
else if ( bandwidth < LOW_BANDWIDTH_LIMIT )
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() flags & RELAY && band < LOW => SPEED_LOW " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_SPEED_LOW ;
}
else
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() flags & RELAY && else => SPEED_NORMAL " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_SPEED_NORMAL ;
}
}
else
{
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() else => SPEED_NORMAL " < < std : : endl ;
# endif
2012-01-08 17:15:19 -05:00
it - > second . linkType | = RS_NET_CONN_SPEED_NORMAL ;
}
uint32_t connType = mPeerMgr - > getConnectionType ( id ) ;
it - > second . linkType | = connType ;
2012-01-13 20:54:17 -05:00
# ifdef LINKMGR_DEBUG_LINKTYPE
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() connType: " < < connType < < std : : endl ;
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() final LinkType: " < < it - > second . linkType < < std : : endl ;
2012-01-08 17:15:19 -05:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() found an address: id: " < < id < < std : : endl ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " laddr: " < < sockaddr_storage_tostring ( raddr ) < < " delay: " < < delay < < " period: " < < period ;
2012-01-08 17:15:19 -05:00
std : : cerr < < " type: " < < type < < std : : endl ;
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() set LinkType to: " < < it - > second . linkType < < std : : endl ;
2012-01-13 20:54:17 -05:00
# endif
2012-01-08 17:15:19 -05:00
2012-01-13 20:54:17 -05:00
/********* Setup LinkType parameters **********/
2011-07-09 05:36:37 -04:00
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() found an address: id: " < < id < < std : : endl ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " laddr: " < < sockaddr_storage_tostring ( raddr ) < < " delay: " < < delay < < " period: " < < period ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " type: " < < type < < std : : endl ;
2012-01-08 17:15:19 -05:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() set LinkType to: " < < it - > second . linkType < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2013-09-13 10:35:19 -04:00
if ( sockaddr_storage_isnull ( raddr ) ) {
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectAttempt() WARNING: address or port is null " < < std : : endl ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " type: " < < type < < std : : endl ;
# endif
}
return true ;
}
/****************************
* Update state ,
* trigger retry if necessary ,
*
* remove from DHT ?
*
*/
2015-05-16 07:19:53 -04:00
bool p3LinkMgrIMPL : : connectResult ( const RsPeerId & id , bool success , bool isIncomingConnection , uint32_t flags , const struct sockaddr_storage & remote_peer_address )
2011-07-09 05:36:37 -04:00
{
2011-07-10 15:50:12 -04:00
bool doDhtAssist = false ;
2011-07-09 14:39:34 -04:00
bool updatePeerAddr = false ;
2011-07-10 20:52:12 -04:00
bool updateLastContact = false ;
2011-07-09 05:36:37 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2011-07-09 14:39:34 -04:00
if ( id = = getOwnId ( ) )
{
2014-03-17 16:56:06 -04:00
rslog ( RSL_ALERT , p3connectzone , " p3LinkMgrIMPL::connectResult() ERROR Trying to Connect to OwnId: " + id . toStdString ( ) ) ;
2012-01-18 17:51:38 -05:00
2011-07-09 05:36:37 -04:00
return false ;
}
/* check for existing */
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 05:36:37 -04:00
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
2014-03-17 16:56:06 -04:00
rslog ( RSL_ALERT , p3connectzone , " p3LinkMgrIMPL::connectResult() ERROR Missing Friend: " + id . toStdString ( ) ) ;
2012-01-18 17:51:38 -05:00
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() ERROR, missing Friend " < < " id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return false ;
}
2012-01-18 17:51:38 -05:00
/* now we can tell if we think we were connected - proper point to log */
{
2014-03-17 16:56:06 -04:00
std : : string out = " p3LinkMgrIMPL::connectResult() id: " + id . toStdString ( ) ;
2012-01-18 17:51:38 -05:00
if ( success )
{
2012-04-14 18:38:24 -04:00
out + = " SUCCESS " ;
2012-01-18 17:51:38 -05:00
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2012-04-14 18:38:24 -04:00
out + = " WARNING: State says: Already Connected " ;
2012-01-18 17:51:38 -05:00
}
}
else
{
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2012-04-14 18:38:24 -04:00
out + = " FAILURE OF THE CONNECTION (Was Connected) " ;
2012-01-18 17:51:38 -05:00
}
else
{
2012-04-14 18:38:24 -04:00
out + = " FAILED ATTEMPT (Not Connected) " ;
2012-01-18 17:51:38 -05:00
}
}
2012-04-14 18:38:24 -04:00
rslog ( RSL_WARNING , p3connectzone , out ) ;
2012-01-18 17:51:38 -05:00
}
2011-07-09 05:36:37 -04:00
if ( success )
{
/* update address (should also come through from DISC) */
2015-05-25 10:02:45 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Connect!: id: " < < id < < std : : endl ;
2015-05-25 10:02:45 -04:00
std : : cerr < < " Success: " < < success < < " flags: " < < flags < < " , remote IP = " < < sockaddr_storage_iptostring ( remote_peer_address ) < < std : : endl ;
2011-07-10 20:52:12 -04:00
# endif
2011-07-09 05:36:37 -04:00
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Connect!: id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Success: " < < success < < " flags: " < < flags < < std : : endl ;
2011-07-13 10:22:25 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::connectResult() Success " ) ;
2012-01-18 17:51:38 -05:00
# endif
2011-07-09 05:36:37 -04:00
/* change state */
it - > second . state | = RS_PEER_S_CONNECTED ;
it - > second . actions | = RS_PEER_CONNECTED ;
it - > second . connecttype = flags ;
2013-11-30 08:48:39 -05:00
it - > second . connectaddr = remote_peer_address ;
2011-07-09 05:36:37 -04:00
2015-05-16 07:19:53 -04:00
it - > second . actAsServer = isIncomingConnection ;
2011-07-10 20:52:12 -04:00
updateLastContact = true ; /* time of connect */
2011-07-09 05:36:37 -04:00
/* only update the peer's address if we were in a connect attempt.
* Otherwise , they connected to us , and the address will be a
* random port of their outgoing TCP socket
*
* NB even if we received the connection , the IP address is likely to okay .
*/
//used to send back to the peer it's own ext address
//it->second.currentserveraddr = remote_peer_address;
2011-07-09 14:39:34 -04:00
// THIS TEST IS A Bit BAD XXX, we should update their address anyway...
// This means we only update connections that we've made.. so maybe not too bad?
2011-07-09 05:36:37 -04:00
if ( ( it - > second . inConnAttempt ) & &
2013-09-13 10:35:19 -04:00
( sockaddr_storage_same ( it - > second . currentConnAddrAttempt . addr , remote_peer_address ) ) )
2011-07-09 14:39:34 -04:00
{
updatePeerAddr = true ;
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() adding current peer address in list. " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
}
/* remove other attempts */
it - > second . inConnAttempt = false ;
it - > second . connAddrs . clear ( ) ;
mStatusChanged = true ;
}
else
{
2011-07-10 20:52:12 -04:00
# ifdef LINKMGR_DEBUG_CONNFAIL
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Disconnect/Fail: flags: " < < flags < < " id: " < < id ;
2011-07-10 20:52:12 -04:00
std : : cerr < < std : : endl ;
if ( it - > second . inConnAttempt )
{
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Likely Connect Fail, as inConnAttempt Flag is set " ;
2011-07-10 20:52:12 -04:00
std : : cerr < < std : : endl ;
}
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Likely DISCONNECT, as state set to Connected " ;
2011-07-10 20:52:12 -04:00
std : : cerr < < std : : endl ;
}
# endif
2011-07-09 05:36:37 -04:00
it - > second . inConnAttempt = false ;
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Disconnect/Fail: id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Success: " < < success < < " flags: " < < flags < < std : : endl ;
# endif
/* if currently connected -> flag as failed */
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
it - > second . state & = ( ~ RS_PEER_S_CONNECTED ) ;
it - > second . actions | = RS_PEER_DISCONNECTED ;
mStatusChanged = true ;
2011-07-10 20:52:12 -04:00
updateLastContact = true ; /* time of disconnect */
2011-07-09 05:36:37 -04:00
}
if ( it - > second . connAddrs . size ( ) > = 1 )
{
it - > second . actions | = RS_PEER_CONNECT_REQ ;
mStatusChanged = true ;
}
2011-07-10 15:50:12 -04:00
if ( it - > second . dhtVisible )
{
doDhtAssist = true ;
}
2011-07-09 05:36:37 -04:00
}
}
2011-07-09 14:39:34 -04:00
if ( updatePeerAddr )
{
pqiIpAddress raddr ;
raddr . mAddr = remote_peer_address ;
raddr . mSeenTime = time ( NULL ) ;
raddr . mSrc = 0 ;
2011-07-10 15:50:12 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Success and we initiated connection... Updating Address " ;
2011-07-10 15:50:12 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 14:39:34 -04:00
mPeerMgr - > updateCurrentAddress ( id , raddr ) ;
}
2011-07-10 15:50:12 -04:00
2011-07-10 20:52:12 -04:00
if ( updateLastContact )
{
mPeerMgr - > updateLastContact ( id ) ;
}
2011-07-10 15:50:12 -04:00
2011-07-13 18:23:40 -04:00
/* inform NetAssist of result. This is slightly duplicating below, as we switch it on/off
* in a second anyway . However , the FAILURE of UDP connection , must be informed .
*
* actually the way the DHT works at the moment , both forms of feedback are required .
* this handles connection requests , the other searches . As they are independent . . . do both .
*/
if ( flags = = RS_NET_CONN_UDP_ALL )
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::connectResult() Sending Feedback for UDP connection " ;
std : : cerr < < std : : endl ;
2015-09-15 11:14:55 -04:00
# endif
2011-07-13 18:23:40 -04:00
if ( success )
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::connectResult() UDP Update CONNECTED to: " < < id ;
std : : cerr < < std : : endl ;
2015-09-15 11:14:55 -04:00
# endif
2011-07-13 18:23:40 -04:00
mNetMgr - > netAssistStatusUpdate ( id , NETMGR_DHT_FEEDBACK_CONNECTED ) ;
}
else
{
# ifdef LINKMGR_DEBUG
2015-09-15 11:14:55 -04:00
2011-07-13 18:23:40 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() UDP Update FAILED to: " < < id ;
std : : cerr < < std : : endl ;
2015-09-15 11:14:55 -04:00
# endif
2011-07-13 18:23:40 -04:00
/* have no differentiation between failure and closed? */
mNetMgr - > netAssistStatusUpdate ( id , NETMGR_DHT_FEEDBACK_CONN_FAILED ) ;
}
}
2011-07-10 15:50:12 -04:00
if ( success )
{
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Success switching off DhtAssist for friend: " < < id ;
2011-07-10 15:50:12 -04:00
std : : cerr < < std : : endl ;
# endif
/* always switch it off now */
2011-11-22 08:24:42 -05:00
mNetMgr - > netAssistFriend ( id , false ) ;
2011-12-01 12:21:52 -05:00
/* inform NetMgr that we know this peers address: but only if external address */
2013-09-13 10:35:19 -04:00
if ( sockaddr_storage_isExternalNet ( remote_peer_address ) )
2011-12-01 12:21:52 -05:00
{
mNetMgr - > netAssistKnownPeer ( id , remote_peer_address ,
NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_ONLINE ) ;
}
2011-07-10 15:50:12 -04:00
}
else
{
if ( doDhtAssist )
{
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Fail, Enabling DhtAssist for: " < < id ;
2011-07-10 15:50:12 -04:00
std : : cerr < < std : : endl ;
# endif
mNetMgr - > netAssistFriend ( id , true ) ;
}
else
{
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::connectResult() Fail, No DhtAssist, as No DHT visibility for: " < < id ;
2011-07-10 15:50:12 -04:00
std : : cerr < < std : : endl ;
# endif
}
2011-11-22 08:24:42 -05:00
/* inform NetMgr that this peer is offline */
mNetMgr - > netAssistKnownPeer ( id , remote_peer_address , NETASSIST_KNOWN_PEER_FRIEND | NETASSIST_KNOWN_PEER_OFFLINE ) ;
2011-07-10 15:50:12 -04:00
}
2011-07-09 05:36:37 -04:00
2011-07-10 15:50:12 -04:00
return success ;
2011-07-09 05:36:37 -04:00
}
/******************************** Feedback ...... *********************************
* From various sources
*/
2013-08-22 03:26:13 -04:00
// from pqissl, when a connection failed due to security
2016-06-02 08:42:32 -04:00
void p3LinkMgrIMPL : : notifyDeniedConnection ( const RsPgpId & gpgid , const RsPeerId & sslid , const std : : string & sslcn , const struct sockaddr_storage & /*addr*/ , bool incoming )
2013-08-22 03:26:13 -04:00
{
std : : cerr < < " p3LinkMgrIMPL::notifyDeniedConnection() " ;
std : : cerr < < " pgpid: " < < gpgid ;
std : : cerr < < " sslid: " < < sslid ;
std : : cerr < < " sslcn: " < < sslcn ;
std : : cerr < < std : : endl ;
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2013-08-22 03:26:13 -04:00
it = mFriendList . find ( sslid ) ;
if ( it = = mFriendList . end ( ) )
{
std : : cerr < < " p3LinkMgrIMPL::notifyDeniedConnection() of NON-FRIEND: " < < sslid ;
std : : cerr < < std : : endl ;
return ;
}
it - > second . wasDeniedConnection = true ;
it - > second . deniedTS = time ( NULL ) ;
if ( ( ! incoming ) & & it - > second . inConnAttempt )
{
it - > second . deniedInConnAttempt = true ;
it - > second . deniedConnectionAttempt = it - > second . currentConnAddrAttempt ;
std : : cerr < < " p3LinkMgrIMPL::notifyDeniedConnection() Denied In Connection Attempt " ;
std : : cerr < < std : : endl ;
}
else
{
it - > second . deniedInConnAttempt = false ;
std : : cerr < < " p3LinkMgrIMPL::notifyDeniedConnection() Denied NOT In Connection Attempt " ;
std : : cerr < < std : : endl ;
}
return ;
}
2011-07-09 05:36:37 -04:00
2014-03-17 16:56:06 -04:00
void p3LinkMgrIMPL : : peerStatus ( const RsPeerId & id , const pqiIpAddrSet & addrs ,
2011-07-09 05:36:37 -04:00
uint32_t type , uint32_t flags , uint32_t source )
{
/* HACKED UP FIX ****/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 05:36:37 -04:00
bool isFriend = true ;
time_t now = time ( NULL ) ;
peerAddrInfo details ;
details . type = type ;
details . found = true ;
details . addrs = addrs ;
details . ts = now ;
2011-07-09 14:39:34 -04:00
bool updateNetConfig = ( source = = RS_CB_PERSON ) ;
2013-09-28 04:09:59 -04:00
uint32_t peer_vs_disc = 0 ;
uint32_t peer_vs_dht = 0 ;
2011-07-09 14:39:34 -04:00
uint32_t peerNetMode = 0 ;
2011-07-09 05:36:37 -04:00
2011-07-09 14:39:34 -04:00
uint32_t ownNetMode = mNetMgr - > getNetworkMode ( ) ;
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2011-07-09 05:36:37 -04:00
{
/* Log */
2014-03-17 16:56:06 -04:00
std : : string out = " p3LinkMgrIMPL::peerStatus() id: " + id . toStdString ( ) ;
2012-04-14 18:38:24 -04:00
rs_sprintf_append ( out , " type: %lu flags: %lu source: %lu \n " , type , flags , source ) ;
2011-07-09 05:36:37 -04:00
addrs . printAddrs ( out ) ;
2012-04-14 18:38:24 -04:00
rslog ( RSL_WARNING , p3connectzone , out ) ;
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2012-04-14 18:38:24 -04:00
std : : cerr < < out < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
}
/* look up the id */
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
/* check Others list */
isFriend = false ;
it = mOthersList . find ( id ) ;
if ( it = = mOthersList . end ( ) )
{
/* not found - ignore */
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Peer Not Found - Ignore " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return ;
}
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Peer is in mOthersList " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
}
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Current Peer State: " < < std : : endl ;
2011-07-09 05:36:37 -04:00
printConnectState ( std : : cerr , it - > second ) ;
std : : cerr < < std : : endl ;
# endif
/* update the status */
/* if source is DHT */
if ( source = = RS_CB_DHT )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Update From DHT: " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
/* DHT can tell us about
* 1 ) connect type ( UDP / TCP / etc )
* 2 ) local / external address
*/
it - > second . source = RS_CB_DHT ;
it - > second . dht = details ;
/* If we get a info -> then they are online */
it - > second . state | = RS_PEER_S_ONLINE ;
it - > second . lastavailable = now ;
}
else if ( source = = RS_CB_DISC )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Update From DISC: " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
/* DISC can tell us about
* 1 ) connect type ( UDP / TCP / etc )
* 2 ) local / external addresses
*/
it - > second . source = RS_CB_DISC ;
it - > second . disc = details ;
if ( flags & RS_NET_FLAGS_ONLINE )
{
it - > second . actions | = RS_PEER_ONLINE ;
it - > second . state | = RS_PEER_S_ONLINE ;
it - > second . lastavailable = now ;
mStatusChanged = true ;
}
}
else if ( source = = RS_CB_PERSON )
{
/* PERSON can tell us about
* 1 ) online / offline
* 2 ) connect address
* - > update all !
*/
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Update From PERSON: " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
it - > second . source = RS_CB_PERSON ;
it - > second . peer = details ;
it - > second . state | = RS_PEER_S_ONLINE ;
it - > second . lastavailable = now ;
/* must be online to recv info (should be connected too!)
* but no need for action as should be connected already
2011-07-09 14:39:34 -04:00
*
* One problem with these states . . . is that we will never find them via DHT
* if these flags are switched off here . . . If we get a connection attempt via DHT
* we should switch the DHT search back on .
2011-07-09 05:36:37 -04:00
*/
2011-07-09 14:39:34 -04:00
peerNetMode = 0 ; //it->second.netMode &= (~RS_NET_MODE_ACTUAL); /* clear actual flags */
2011-07-09 05:36:37 -04:00
if ( flags & RS_NET_FLAGS_EXTERNAL_ADDR )
{
2011-07-09 14:39:34 -04:00
peerNetMode = RS_NET_MODE_EXT ;
2011-07-09 05:36:37 -04:00
}
else if ( flags & RS_NET_FLAGS_STABLE_UDP )
{
2011-07-09 14:39:34 -04:00
peerNetMode = RS_NET_MODE_UDP ;
2011-07-09 05:36:37 -04:00
}
else
{
2011-07-09 14:39:34 -04:00
peerNetMode = RS_NET_MODE_UNREACHABLE ;
2011-07-09 05:36:37 -04:00
}
/* always update VIS status */
if ( flags & RS_NET_FLAGS_USE_DISC )
{
2013-09-28 04:09:59 -04:00
peer_vs_disc = RS_VS_DISC_FULL ;
2011-07-09 05:36:37 -04:00
}
else
{
2013-09-28 04:09:59 -04:00
peer_vs_disc = RS_VS_DISC_OFF ;
2011-07-09 05:36:37 -04:00
}
if ( flags & RS_NET_FLAGS_USE_DHT )
{
2013-09-28 04:09:59 -04:00
peer_vs_dht = RS_VS_DHT_FULL ;
2011-07-09 05:36:37 -04:00
}
else
{
2013-09-28 04:09:59 -04:00
peer_vs_dht = RS_VS_DHT_OFF ;
2011-07-09 05:36:37 -04:00
}
2011-07-09 14:39:34 -04:00
2011-07-09 05:36:37 -04:00
}
/* Determine Reachability (only advisory) */
2011-07-09 14:39:34 -04:00
if ( ownNetMode & RS_NET_MODE_UDP )
2011-07-09 05:36:37 -04:00
{
if ( ( details . type & RS_NET_CONN_UDP_DHT_SYNC ) | |
( details . type & RS_NET_CONN_TCP_EXTERNAL ) )
{
/* reachable! */
it - > second . state & = ( ~ RS_PEER_S_UNREACHABLE ) ;
}
else
{
/* unreachable */
it - > second . state | = RS_PEER_S_UNREACHABLE ;
}
}
2011-07-09 14:39:34 -04:00
else if ( ownNetMode & RS_NET_MODE_UNREACHABLE )
2011-07-09 05:36:37 -04:00
{
if ( details . type & RS_NET_CONN_TCP_EXTERNAL )
{
/* reachable! */
it - > second . state & = ( ~ RS_PEER_S_UNREACHABLE ) ;
}
else
{
/* unreachable */
it - > second . state | = RS_PEER_S_UNREACHABLE ;
}
}
else
{
it - > second . state & = ( ~ RS_PEER_S_UNREACHABLE ) ;
}
if ( ! isFriend )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() NOT FRIEND " < < " id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
{
2011-07-13 10:22:25 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::peerStatus() NO CONNECT (not friend) " ) ;
2011-07-09 05:36:37 -04:00
}
return ;
}
/* if already connected -> done */
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() PEER ONLINE ALREADY " < < " id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
{
/* Log */
2011-07-13 10:22:25 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::peerStatus() NO CONNECT (already connected!) " ) ;
2011-07-09 05:36:37 -04:00
}
return ;
}
2011-07-09 14:39:34 -04:00
2011-07-09 05:36:37 -04:00
} /****** STACK UNLOCK MUTEX *******/
2011-07-09 14:39:34 -04:00
bool newAddrs = mPeerMgr - > updateAddressList ( id , addrs ) ;
if ( updateNetConfig )
{
2013-09-28 04:09:59 -04:00
mPeerMgr - > setVisState ( id , peer_vs_disc , peer_vs_dht ) ;
2011-07-09 14:39:34 -04:00
mPeerMgr - > setNetworkMode ( id , peerNetMode ) ;
}
2011-07-09 05:36:37 -04:00
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() " < < " id: " < < id ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " type: " < < type < < " flags: " < < flags ;
std : : cerr < < " source: " < < source < < std : : endl ;
std : : cerr < < " addrs: " < < std : : endl ;
2012-04-14 18:38:24 -04:00
std : : string out ;
addrs . printAddrs ( out ) ;
std : : cerr < < out < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
# ifndef P3CONNMGR_NO_AUTO_CONNECTION
# ifndef P3CONNMGR_NO_TCP_CONNECTIONS
if ( newAddrs )
{
retryConnectTCP ( id ) ;
}
# endif // P3CONNMGR_NO_TCP_CONNECTIONS
# else
# endif // P3CONNMGR_NO_AUTO_CONNECTION
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerStatus() Resulting Peer State: " < < std : : endl ;
2011-07-09 05:36:37 -04:00
printConnectState ( std : : cerr , it - > second ) ;
std : : cerr < < std : : endl ;
# endif
}
2011-07-14 10:56:33 -04:00
/* This has become very unwieldy - as extra arguments are required for UDP connections */
2014-03-17 16:56:06 -04:00
void p3LinkMgrIMPL : : peerConnectRequest ( const RsPeerId & id , const struct sockaddr_storage & raddr , const struct sockaddr_storage & proxyaddr , const struct sockaddr_storage & srcaddr ,
2011-07-14 10:56:33 -04:00
uint32_t source , uint32_t flags , uint32_t delay , uint32_t bandwidth )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 18:23:40 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() id: " < < id ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " raddr: " < < sockaddr_storage_tostring ( raddr ) ;
std : : cerr < < " proxyaddr: " < < sockaddr_storage_tostring ( proxyaddr ) ;
std : : cerr < < " srcaddr: " < < sockaddr_storage_tostring ( srcaddr ) ;
2011-07-13 18:23:40 -04:00
std : : cerr < < " source: " < < source ;
std : : cerr < < " flags: " < < flags ;
std : : cerr < < " delay: " < < delay ;
2011-07-14 10:56:33 -04:00
std : : cerr < < " bandwidth: " < < bandwidth ;
2011-07-13 18:23:40 -04:00
std : : cerr < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
{
/* Log */
2014-03-17 16:56:06 -04:00
std : : string out = " p3LinkMgrIMPL::peerConnectRequest() id: " + id . toStdString ( ) ;
2013-09-13 10:35:19 -04:00
out + = " raddr: " ;
out + = sockaddr_storage_tostring ( raddr ) ;
out + = " proxyaddr: " ;
out + = sockaddr_storage_tostring ( proxyaddr ) ;
out + = " srcaddr: " ;
out + = sockaddr_storage_tostring ( srcaddr ) ;
2012-04-14 18:38:24 -04:00
rs_sprintf_append ( out , " source: %lu " , source ) ;
rs_sprintf_append ( out , " flags: %lu " , flags ) ;
rs_sprintf_append ( out , " delay: %lu " , delay ) ;
rs_sprintf_append ( out , " bandwidth: %lu " , bandwidth ) ;
2011-07-14 10:56:33 -04:00
2012-04-14 18:38:24 -04:00
rslog ( RSL_WARNING , p3connectzone , out ) ;
2011-07-09 05:36:37 -04:00
}
/******************** TCP PART *****************************/
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 18:23:40 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() (From DHT Only) " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
if ( source = = RS_CB_DHT )
{
2011-07-18 20:06:09 -04:00
2011-07-13 18:23:40 -04:00
if ( flags & RS_CB_FLAG_MODE_TCP )
{
2011-07-18 20:06:09 -04:00
# ifndef P3CONNMGR_NO_TCP_CONNECTIONS
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 18:23:40 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() DHT says Online ==> so try TCP " ;
std : : cerr < < std : : endl ;
2011-07-10 14:19:46 -04:00
# endif
2011-07-13 18:23:40 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-13 18:23:40 -04:00
if ( mFriendList . end ( ) = = ( it = mFriendList . find ( id ) ) )
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() ERROR Peer is not Friend " < < std : : endl ;
# endif
return ;
}
/* if already connected -> done */
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() ERROR Peer Already Connected " < < std : : endl ;
# endif
2011-08-06 08:27:23 -04:00
return ;
2011-07-13 18:23:40 -04:00
}
/* setup specific attempt for DHT found address. */
2013-09-13 10:35:19 -04:00
locked_ConnectAttempt_SpecificAddress ( & ( it - > second ) , raddr ) ;
2011-07-13 18:23:40 -04:00
}
retryConnect ( id ) ;
2011-07-18 20:06:09 -04:00
# endif
2011-07-13 18:23:40 -04:00
}
else
{
/* UDP Attempt! */
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() DHT says CONNECT ==> tryConnectUDP() " ;
std : : cerr < < std : : endl ;
# endif
2011-07-14 10:56:33 -04:00
tryConnectUDP ( id , raddr , proxyaddr , srcaddr , flags , delay , bandwidth ) ;
2011-07-13 18:23:40 -04:00
}
2011-07-09 05:36:37 -04:00
return ;
2011-07-13 18:23:40 -04:00
2011-07-09 05:36:37 -04:00
}
else
{ // IS THIS USED???
2011-07-13 18:23:40 -04:00
std : : cerr < < " p3LinkMgrIMPL::peerConnectRequest() ERROR source OTHER ==> NOOP " < < std : : endl ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
2011-07-09 05:36:37 -04:00
return ;
}
}
/*******************************************************************/
/*******************************************************************/
/*************** External Control ****************/
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : retryConnect ( const RsPeerId & id )
2011-07-09 05:36:37 -04:00
{
/* push all available addresses onto the connect addr stack */
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::retryConnect() id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
# ifndef P3CONNMGR_NO_TCP_CONNECTIONS
retryConnectTCP ( id ) ;
# endif // P3CONNMGR_NO_TCP_CONNECTIONS
return true ;
}
2014-03-17 16:56:06 -04:00
bool p3LinkMgrIMPL : : tryConnectUDP ( const RsPeerId & id , const struct sockaddr_storage & rUdpAddr ,
2013-09-13 10:35:19 -04:00
const struct sockaddr_storage & proxyaddr , const struct sockaddr_storage & srcaddr ,
2011-07-14 10:56:33 -04:00
uint32_t flags , uint32_t delay , uint32_t bandwidth )
2011-07-09 05:36:37 -04:00
{
2011-07-14 11:01:41 -04:00
# ifdef DISABLE_UDP_CONNECTIONS
std : : cerr < < " p3LinkMgrIMPL::tryConnectUDP() CONNECTIONS DISABLED FOR NOW... id: " < < id < < std : : endl ;
std : : cerr < < " p3LinkMgrIMPL::tryConnectUDP() PARAMS id: " < < id ;
std : : cerr < < " raddr: " < < rs_inet_ntoa ( rUdpAddr . sin_addr ) < < " : " < < ntohs ( rUdpAddr . sin_port ) ;
std : : cerr < < " proxyaddr: " < < rs_inet_ntoa ( proxyaddr . sin_addr ) < < " : " < < ntohs ( proxyaddr . sin_port ) ;
std : : cerr < < " srcaddr: " < < rs_inet_ntoa ( srcaddr . sin_addr ) < < " : " < < ntohs ( srcaddr . sin_port ) ;
std : : cerr < < " flags: " < < flags ;
std : : cerr < < " delay: " < < delay ;
std : : cerr < < " bandwidth: " < < bandwidth ;
std : : cerr < < std : : endl ;
mNetMgr - > netAssistStatusUpdate ( id , NETMGR_DHT_FEEDBACK_CONN_FAILED ) ;
return false ;
# endif
2013-09-11 03:00:33 -04:00
if ( mPeerMgr - > isHidden ( ) )
{
2015-04-02 15:24:54 -04:00
# ifdef LINKMGR_DEBUG
2013-09-11 03:00:33 -04:00
std : : cerr < < " p3LinkMgrIMPL::tryConnectUDP() isHidden(): no connection attempts for : " < < id ;
std : : cerr < < std : : endl ;
2015-04-02 15:24:54 -04:00
# endif
2013-09-11 03:00:33 -04:00
return false ;
}
2011-07-14 11:01:41 -04:00
2011-07-09 05:36:37 -04:00
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
/* push all available addresses onto the connect addr stack */
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-14 11:01:41 -04:00
std : : cerr < < " p3LinkMgrIMPL::tryConnectUDP() id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
if ( id = = getOwnId ( ) ) {
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgrIMPL::retryConnectUDP() Failed, connecting to own id: " ) ;
2011-07-09 05:36:37 -04:00
# endif
return false ;
}
/* look up the id */
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 05:36:37 -04:00
if ( mFriendList . end ( ) = = ( it = mFriendList . find ( id ) ) )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::retryConnectUDP() Peer is not Friend " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return false ;
}
/* if already connected -> done */
if ( it - > second . state & RS_PEER_S_CONNECTED )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::retryConnectUDP() Peer Already Connected " < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2013-09-03 10:51:23 -04:00
return false ;
2011-07-09 05:36:37 -04:00
}
/* Explicit Request to start the UDP connection */
2013-09-13 10:35:19 -04:00
if ( sockaddr_storage_isValidNet ( rUdpAddr ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-09 05:36:37 -04:00
std : : cerr < < " Adding udp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Addr: " < < sockaddr_storage_tostring ( rUdpAddr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
pca . addr = rUdpAddr ;
pca . type = RS_NET_CONN_UDP_PEER_SYNC ;
2011-07-13 18:23:40 -04:00
pca . delay = delay ;
2011-07-09 05:36:37 -04:00
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_UDP_DEFAULT_PERIOD ;
2011-07-13 18:23:40 -04:00
pca . flags = flags ;
2011-07-14 10:56:33 -04:00
pca . proxyaddr = proxyaddr ;
pca . srcaddr = srcaddr ;
pca . bandwidth = bandwidth ;
2011-07-09 05:36:37 -04:00
2011-07-13 18:23:40 -04:00
// Push address to the front... so it happens quickly (before any timings are lost).
addAddressIfUnique ( it - > second . connAddrs , pca , true ) ;
2011-07-09 05:36:37 -04:00
}
/* finish it off */
return locked_ConnectAttempt_Complete ( & ( it - > second ) ) ;
}
2015-12-31 09:42:22 -05:00
/* push all available addresses onto the connect addr stack...
* with the following exceptions :
* - id is our own
* - id is not our friend
* - id is already connected
* - id is hidden but of an unkown type
* - we are hidden but id is not
*/
bool p3LinkMgrIMPL : : retryConnectTCP ( const RsPeerId & id )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::retryConnectTCP() id: " < < id < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2015-12-31 09:42:22 -05:00
if ( id = = getOwnId ( ) ) return false ;
2011-07-09 05:36:37 -04:00
2015-12-31 09:42:22 -05:00
{
RS_STACK_MUTEX ( mLinkMtx ) ;
std : : map < RsPeerId , peerConnectState > : : iterator it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) ) return false ;
if ( it - > second . state & RS_PEER_S_CONNECTED ) return false ;
}
// Extract the required info from p3PeerMgr
// first possibility - is it a hidden peer
2013-09-03 09:35:39 -04:00
if ( mPeerMgr - > isHiddenPeer ( id ) )
{
2015-09-15 11:10:48 -04:00
/* check for valid hidden type */
uint32_t type = mPeerMgr - > getHiddenType ( id ) ;
2015-12-31 09:42:22 -05:00
if ( type & ( ~ RS_HIDDEN_TYPE_MASK ) ) return false ;
2015-09-15 11:10:48 -04:00
2015-12-31 09:42:22 -05:00
/* then we just have one connect attempt via the Proxy */
2013-09-13 10:35:19 -04:00
struct sockaddr_storage proxy_addr ;
2013-09-03 09:35:39 -04:00
std : : string domain_addr ;
uint16_t domain_port ;
2015-12-31 09:42:22 -05:00
if ( mPeerMgr - > getProxyAddress ( id , proxy_addr , domain_addr , domain_port ) )
2013-09-03 09:35:39 -04:00
{
2015-12-31 09:42:22 -05:00
RS_STACK_MUTEX ( mLinkMtx ) ;
std : : map < RsPeerId , peerConnectState > : : iterator it = mFriendList . find ( id ) ;
if ( it ! = mFriendList . end ( ) )
2013-09-03 09:35:39 -04:00
{
2015-09-15 10:18:46 -04:00
locked_ConnectAttempt_ProxyAddress ( & ( it - > second ) , type , proxy_addr , domain_addr , domain_port ) ;
2013-09-03 09:35:39 -04:00
return locked_ConnectAttempt_Complete ( & ( it - > second ) ) ;
}
}
2013-09-11 03:00:33 -04:00
return false ;
}
2013-09-03 09:35:39 -04:00
2015-12-31 09:42:22 -05:00
if ( mPeerMgr - > isHidden ( ) ) return false ;
2013-09-13 10:35:19 -04:00
struct sockaddr_storage lAddr ;
struct sockaddr_storage eAddr ;
2011-07-09 14:39:34 -04:00
pqiIpAddrSet histAddrs ;
std : : string dyndns ;
2011-07-09 20:41:39 -04:00
if ( mPeerMgr - > getConnectAddresses ( id , lAddr , eAddr , histAddrs , dyndns ) )
2011-07-09 14:39:34 -04:00
{
2015-12-31 09:42:22 -05:00
RS_STACK_MUTEX ( mLinkMtx ) ;
2011-07-09 14:39:34 -04:00
2015-12-31 09:42:22 -05:00
std : : map < RsPeerId , peerConnectState > : : iterator it = mFriendList . find ( id ) ;
if ( it ! = mFriendList . end ( ) )
2011-07-09 14:39:34 -04:00
{
2013-09-13 10:35:19 -04:00
locked_ConnectAttempt_CurrentAddresses ( & ( it - > second ) , lAddr , eAddr ) ;
2011-07-09 14:39:34 -04:00
2015-12-31 09:42:22 -05:00
uint16_t dynPort = 0 ;
if ( ! sockaddr_storage_isnull ( eAddr ) ) dynPort = sockaddr_storage_port ( eAddr ) ;
if ( ! dynPort & & ! sockaddr_storage_isnull ( lAddr ) )
2013-09-13 10:35:19 -04:00
dynPort = sockaddr_storage_port ( lAddr ) ;
2011-07-09 14:39:34 -04:00
if ( dynPort )
locked_ConnectAttempt_AddDynDNS ( & ( it - > second ) , dyndns , dynPort ) ;
2012-01-08 17:15:19 -05:00
locked_ConnectAttempt_HistoricalAddresses ( & ( it - > second ) , histAddrs ) ;
2015-12-31 09:42:22 -05:00
// finish it off
2011-07-09 14:39:34 -04:00
return locked_ConnectAttempt_Complete ( & ( it - > second ) ) ;
}
2011-07-10 14:19:46 -04:00
else
2015-12-31 09:42:22 -05:00
std : : cerr < < " p3LinkMgrIMPL::retryConnectTCP() ERROR failed to find friend data : " < < id < < std : : endl ;
2011-07-10 14:19:46 -04:00
}
else
2015-12-31 09:42:22 -05:00
std : : cerr < < " p3LinkMgrIMPL::retryConnectTCP() ERROR failed to get addresses from PeerMgr for: " < < id < < std : : endl ;
2011-07-09 14:39:34 -04:00
return false ;
2011-07-09 05:36:37 -04:00
}
# define MAX_TCP_ADDR_AGE (3600 * 24 * 14) // two weeks in seconds.
2015-10-17 04:27:49 -04:00
bool p3LinkMgrIMPL : : locked_CheckPotentialAddr ( const struct sockaddr_storage & addr , time_t age )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_CheckPotentialAddr( " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < sockaddr_storage_tostring ( addr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " , " < < age < < " ) " ;
std : : cerr < < std : : endl ;
# endif
/*
* if it is old - quick rejection
*/
if ( age > MAX_TCP_ADDR_AGE )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - TOO OLD " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
/* if invalid - quick rejection */
2015-10-17 04:27:49 -04:00
if ( ! sockaddr_storage_isValidNet ( addr ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - INVALID " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
2015-05-31 15:52:51 -04:00
std : : list < struct sockaddr_storage > : : const_iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = mBannedIpList . begin ( ) ; it ! = mBannedIpList . end ( ) ; + + it )
2015-05-25 10:02:45 -04:00
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " Checking IP w.r.t. banned IP " < < sockaddr_storage_iptostring ( * it ) < < std : : endl ;
# endif
2013-09-13 10:35:19 -04:00
if ( sockaddr_storage_sameip ( * it , addr ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_CheckPotentialAddr() REJECTING - ON BANNED IPLIST " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
return false ;
}
}
2015-05-31 15:52:51 -04:00
if ( rsBanList ! = NULL & & ! rsBanList - > isAddressAccepted ( addr , RSBANLIST_CHECKING_FLAGS_BLACKLIST ) )
2015-05-25 10:02:45 -04:00
{
# ifdef LINKMGR_DEBUG
2015-05-31 15:52:51 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_CheckPotentialAddr() adding to local Banned IPList " ;
std : : cerr < < std : : endl ;
2015-05-25 10:02:45 -04:00
# endif
2015-05-31 15:52:51 -04:00
return false ;
2015-05-25 10:02:45 -04:00
}
2011-07-09 05:36:37 -04:00
2015-10-17 04:27:49 -04:00
return true ;
2011-07-09 05:36:37 -04:00
}
2013-09-13 10:35:19 -04:00
void p3LinkMgrIMPL : : locked_ConnectAttempt_SpecificAddress ( peerConnectState * peer , const struct sockaddr_storage & remoteAddr )
2011-07-13 18:23:40 -04:00
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses() " ;
std : : cerr < < std : : endl ;
# endif
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( remoteAddr , 0 ) )
2011-07-13 18:23:40 -04:00
{
# ifdef LINKMGR_DEBUG
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_SpecificAddresses() " ;
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Addr: " < < sockaddr_storage_tostring ( remoteAddr ) ;
2011-07-13 18:23:40 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
2013-09-13 10:35:19 -04:00
pca . addr = remoteAddr ;
2011-07-13 18:23:40 -04:00
pca . type = RS_NET_CONN_TCP_EXTERNAL ;
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-13 18:23:40 -04:00
addAddressIfUnique ( peer - > connAddrs , pca , false ) ;
}
}
2013-09-13 10:35:19 -04:00
void p3LinkMgrIMPL : : locked_ConnectAttempt_CurrentAddresses ( peerConnectState * peer , const struct sockaddr_storage & localAddr , const struct sockaddr_storage & serverAddr )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
// Just push all the addresses onto the stack.
/* try "current addresses" first */
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( localAddr , 0 ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Current Local Addr: " < < sockaddr_storage_tostring ( localAddr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
2013-09-13 10:35:19 -04:00
pca . addr = localAddr ;
2011-07-09 05:36:37 -04:00
pca . type = RS_NET_CONN_TCP_LOCAL ;
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-13 18:23:40 -04:00
addAddressIfUnique ( peer - > connAddrs , pca , false ) ;
2011-07-09 05:36:37 -04:00
}
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( serverAddr , 0 ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_CurrentAddresses() " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Current Ext Addr: " < < sockaddr_storage_tostring ( serverAddr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
2013-09-13 10:35:19 -04:00
pca . addr = serverAddr ;
2011-07-09 05:36:37 -04:00
pca . type = RS_NET_CONN_TCP_EXTERNAL ;
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-13 18:23:40 -04:00
addAddressIfUnique ( peer - > connAddrs , pca , false ) ;
2011-07-09 05:36:37 -04:00
}
}
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : locked_ConnectAttempt_HistoricalAddresses ( peerConnectState * peer , const pqiIpAddrSet & ipAddrs )
2011-07-09 05:36:37 -04:00
{
/* now try historical addresses */
/* try local addresses first */
2011-07-09 14:39:34 -04:00
std : : list < pqiIpAddress > : : const_iterator ait ;
2011-07-09 05:36:37 -04:00
time_t now = time ( NULL ) ;
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2015-05-25 10:02:45 -04:00
for ( ait = ipAddrs . mLocal . mAddrs . begin ( ) ; ait ! = ipAddrs . mLocal . mAddrs . end ( ) ; + + ait )
2011-07-09 05:36:37 -04:00
{
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( ait - > mAddr , now - ait - > mSeenTime ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Local Addr: " < < sockaddr_storage_tostring ( ait - > mAddr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
pca . addr = ait - > mAddr ;
pca . type = RS_NET_CONN_TCP_LOCAL ;
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-13 18:23:40 -04:00
addAddressIfUnique ( peer - > connAddrs , pca , false ) ;
2011-07-09 05:36:37 -04:00
}
}
2011-07-09 14:39:34 -04:00
for ( ait = ipAddrs . mExt . mAddrs . begin ( ) ;
2014-10-24 18:07:26 -04:00
ait ! = ipAddrs . mExt . mAddrs . end ( ) ; + + ait )
2011-07-09 05:36:37 -04:00
{
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( ait - > mAddr , now - ait - > mSeenTime ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_HistoricalAddresses() " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " Ext Addr: " < < sockaddr_storage_tostring ( ait - > mAddr ) ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
# endif
peerConnectAddress pca ;
pca . addr = ait - > mAddr ;
pca . type = RS_NET_CONN_TCP_EXTERNAL ;
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-13 18:23:40 -04:00
addAddressIfUnique ( peer - > connAddrs , pca , false ) ;
2011-07-09 05:36:37 -04:00
}
}
}
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : locked_ConnectAttempt_AddDynDNS ( peerConnectState * peer , std : : string dyndns , uint16_t port )
2011-07-09 05:36:37 -04:00
{
/* try dyndns address too */
2013-09-13 10:35:19 -04:00
struct sockaddr_storage addr ;
2011-07-10 14:19:46 -04:00
if ( ! dyndns . empty ( ) & & port )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() Looking up DynDNS address: " < < dyndns < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2011-07-10 14:19:46 -04:00
if ( mDNSResolver - > getIPAddressFromString ( dyndns , addr ) )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < " Adding tcp connection attempt: " ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " DynDNS Addr: " < < sockaddr_storage_iptostring ( addr ) ;
std : : cerr < < " : " < < port ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2011-07-10 14:19:46 -04:00
peerConnectAddress pca ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_copyip ( pca . addr , addr ) ;
sockaddr_storage_setport ( pca . addr , port ) ;
2011-07-10 14:19:46 -04:00
pca . type = RS_NET_CONN_TCP_EXTERNAL ;
//for the delay, we add a random time and some more time when the friend list is big
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2011-07-14 10:56:33 -04:00
pca . bandwidth = 0 ;
2011-07-10 14:19:46 -04:00
/* check address validity */
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( pca . addr , 0 ) )
2011-07-10 14:19:46 -04:00
{
2012-01-06 17:03:46 -05:00
addAddressIfUnique ( peer - > connAddrs , pca , true ) ;
2011-07-09 05:36:37 -04:00
}
}
2011-07-10 14:19:46 -04:00
else
{
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() DNSResolver hasn't found addr yet " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
}
}
else
{
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_AddDynDNS() Address( " < < dyndns < < " ) or Port( " < < port < < " ) NULL ignoring " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
}
}
2015-09-15 10:18:46 -04:00
void p3LinkMgrIMPL : : locked_ConnectAttempt_ProxyAddress ( peerConnectState * peer , const uint32_t type , const struct sockaddr_storage & proxy_addr , const std : : string & domain_addr , uint16_t domain_port )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2013-09-03 09:35:39 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() trying address: " < < domain_addr < < " : " < < domain_port < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
2013-09-03 09:35:39 -04:00
peerConnectAddress pca ;
2013-09-13 10:35:19 -04:00
pca . addr = proxy_addr ;
2011-07-09 05:36:37 -04:00
2015-09-15 10:18:46 -04:00
switch ( type ) {
2015-09-08 07:48:30 -04:00
case RS_HIDDEN_TYPE_TOR :
2015-09-07 05:22:15 -04:00
pca . type = RS_NET_CONN_TCP_HIDDEN_TOR ;
2015-09-07 17:40:52 -04:00
break ;
2015-09-15 11:10:48 -04:00
case RS_HIDDEN_TYPE_I2P :
pca . type = RS_NET_CONN_TCP_HIDDEN_I2P ;
break ;
2015-09-15 10:18:46 -04:00
case RS_HIDDEN_TYPE_UNKNOWN :
default :
2015-09-15 11:10:48 -04:00
/**** THIS CASE SHOULD NOT BE TRIGGERED - since this function is called with a valid hidden type only ****/
2015-09-15 10:18:46 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_ProxyAddress() hidden type of addr: " < < domain_addr < < " is unkown -> THIS SHOULD NEVER HAPPEN! " < < std : : endl ;
std : : cerr < < " - peer : " < < peer - > id < < " ( " < < peer - > name < < " ) " < < std : : endl ;
std : : cerr < < " - proxy: " < < sockaddr_storage_tostring ( proxy_addr ) < < std : : endl ;
std : : cerr < < " - addr : " < < domain_addr < < " : " < < domain_port < < std : : endl ;
pca . type = RS_NET_CONN_TCP_UNKNOW_TOPOLOGY ;
2015-09-07 17:40:52 -04:00
}
2011-07-09 05:36:37 -04:00
2013-09-03 09:35:39 -04:00
//for the delay, we add a random time and some more time when the friend list is big
pca . delay = P3CONNMGR_TCP_DEFAULT_DELAY ;
pca . ts = time ( NULL ) ;
pca . period = P3CONNMGR_TCP_DEFAULT_PERIOD ;
2013-09-13 10:35:19 -04:00
sockaddr_storage_clear ( pca . proxyaddr ) ;
sockaddr_storage_clear ( pca . srcaddr ) ;
2013-09-03 09:35:39 -04:00
pca . bandwidth = 0 ;
pca . domain_addr = domain_addr ;
pca . domain_port = domain_port ;
/* check address validity */
2013-09-13 10:35:19 -04:00
if ( locked_CheckPotentialAddr ( pca . addr , 0 ) )
2013-09-03 09:35:39 -04:00
{
addAddressIfUnique ( peer - > connAddrs , pca , true ) ;
2011-07-09 14:39:34 -04:00
}
2011-07-09 05:36:37 -04:00
}
2011-07-13 18:23:40 -04:00
bool p3LinkMgrIMPL : : addAddressIfUnique ( std : : list < peerConnectAddress > & addrList , peerConnectAddress & pca , bool pushFront )
2011-07-09 05:36:37 -04:00
{
/* iterate through the list, and make sure it isn't already
* in the list
*/
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2013-09-13 10:35:19 -04:00
std : : cerr < < " p3LinkMgrIMPL::addAddressIfUnique() Checking Address: " < < sockaddr_storage_iptostring ( pca . addr ) ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
std : : list < peerConnectAddress > : : iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = addrList . begin ( ) ; it ! = addrList . end ( ) ; + + it )
2011-07-09 05:36:37 -04:00
{
2013-09-13 10:35:19 -04:00
if ( sockaddr_storage_same ( pca . addr , it - > addr ) & &
2011-07-09 05:36:37 -04:00
( pca . type = = it - > type ) )
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::addAddressIfUnique() Discarding Duplicate Address " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-09 05:36:37 -04:00
/* already */
return false ;
}
}
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::addAddressIfUnique() Adding New Address " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2011-07-13 18:23:40 -04:00
if ( pushFront )
{
addrList . push_front ( pca ) ;
}
else
{
addrList . push_back ( pca ) ;
}
2011-07-09 05:36:37 -04:00
return true ;
}
2011-07-13 10:22:25 -04:00
bool p3LinkMgrIMPL : : locked_ConnectAttempt_Complete ( peerConnectState * peer )
2011-07-09 05:36:37 -04:00
{
/* flag as last attempt to prevent loop */
//add a random perturbation between 0 and 2 sec.
2011-07-09 14:39:34 -04:00
peer - > lastattempt = time ( NULL ) + rand ( ) % MAX_RANDOM_ATTEMPT_OFFSET ;
2011-07-09 05:36:37 -04:00
2011-07-09 14:39:34 -04:00
if ( peer - > inConnAttempt )
{
2011-07-09 05:36:37 -04:00
/* -> it'll automatically use the addresses we added */
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Already in CONNECT ATTEMPT " ;
2011-07-09 05:36:37 -04:00
std : : cerr < < std : : endl ;
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Remaining ConnAddr Count: " < < peer - > connAddrs . size ( ) ;
2011-07-09 14:39:34 -04:00
std : : cerr < < std : : endl ;
2011-07-09 05:36:37 -04:00
# endif
return true ;
}
/* start a connection attempt */
2011-07-09 14:39:34 -04:00
if ( peer - > connAddrs . size ( ) > 0 )
2011-07-09 05:36:37 -04:00
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2012-04-14 18:38:24 -04:00
std : : string out = " p3LinkMgrIMPL::locked_ConnectAttempt_Complete() Started CONNECT ATTEMPT! \n " ;
rs_sprintf_append ( out , " p3LinkMgrIMPL::locked_ConnectAttempt_Complete() ConnAddr Count: %u " , peer - > connAddrs . size ( ) ) ;
rslog ( RSL_DEBUG_ALERT , p3connectzone , out ) ;
std : : cerr < < out < < std : : endl ;
2011-07-09 14:39:34 -04:00
# endif
2011-07-09 05:36:37 -04:00
2011-07-09 14:39:34 -04:00
peer - > actions | = RS_PEER_CONNECT_REQ ;
mStatusChanged = true ;
2011-07-09 05:36:37 -04:00
return true ;
2011-07-09 14:39:34 -04:00
}
2011-07-09 05:36:37 -04:00
else
{
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2012-04-14 18:38:24 -04:00
std : : string out = " p3LinkMgrIMPL::locked_ConnectAttempt_Complete() No addr in the connect attempt list. Not suitable for CONNECT ATTEMPT! " ;
rslog ( RSL_DEBUG_ALERT , p3connectzone , out ) ;
std : : cerr < < out < < std : : endl ;
2011-07-09 14:39:34 -04:00
# endif
2011-07-09 05:36:37 -04:00
return false ;
}
return false ;
}
2011-07-09 20:41:39 -04:00
/***********************************************************************************************************
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Handling of Friends * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2014-03-17 16:56:06 -04:00
int p3LinkMgrIMPL : : addFriend ( const RsPeerId & id , bool isVisible )
2011-07-09 20:41:39 -04:00
{
2014-03-17 16:56:06 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgr::addFriend() id: " + id . toStdString ( ) ) ;
2011-08-06 08:27:23 -04:00
2011-07-09 20:41:39 -04:00
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2011-07-10 14:19:46 -04:00
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::addFriend( " < < id < < " , " < < isVisible < < " ) " ;
2011-07-10 14:19:46 -04:00
std : : cerr < < std : : endl ;
# endif
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-09 20:41:39 -04:00
it = mFriendList . find ( id ) ;
if ( it ! = mFriendList . end ( ) )
{
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::addFriend() ERROR, friend already exists : " < < id ;
2011-07-09 20:41:39 -04:00
std : : cerr < < std : : endl ;
return 0 ;
}
peerConnectState pcs ;
pcs . dhtVisible = isVisible ;
2011-07-10 14:19:46 -04:00
pcs . id = id ;
2011-07-11 08:11:58 -04:00
pcs . name = " NoName " ;
2011-07-10 14:19:46 -04:00
pcs . state = RS_PEER_S_FRIEND ;
pcs . actions = RS_PEER_NEW ;
2012-01-26 15:03:54 -05:00
pcs . linkType = RS_NET_CONN_SPEED_UNKNOWN ;
2011-07-09 20:41:39 -04:00
mFriendList [ id ] = pcs ;
2011-07-11 08:11:58 -04:00
mStatusChanged = true ;
2011-07-09 20:41:39 -04:00
}
mNetMgr - > netAssistFriend ( id , isVisible ) ;
return 1 ;
}
2014-03-17 16:56:06 -04:00
int p3LinkMgrIMPL : : removeFriend ( const RsPeerId & id )
2011-07-09 20:41:39 -04:00
{
2014-03-17 16:56:06 -04:00
rslog ( RSL_WARNING , p3connectzone , " p3LinkMgr::removeFriend() id: " + id . toStdString ( ) ) ;
2011-08-06 08:27:23 -04:00
2011-07-09 20:41:39 -04:00
{
2011-07-11 08:11:58 -04:00
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
# ifdef LINKMGR_DEBUG
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::removeFriend( " < < id < < " ) " ;
2011-07-09 20:41:39 -04:00
std : : cerr < < std : : endl ;
2011-07-11 08:11:58 -04:00
# endif
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2011-07-11 08:11:58 -04:00
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
2011-07-13 10:22:25 -04:00
std : : cerr < < " p3LinkMgrIMPL::removeFriend() ERROR, friend not there : " < < id ;
2011-07-11 08:11:58 -04:00
std : : cerr < < std : : endl ;
return 0 ;
}
/* Move to OthersList (so remove can be handled via the action) */
peerConnectState peer = it - > second ;
peer . state & = ( ~ RS_PEER_S_FRIEND ) ;
peer . state & = ( ~ RS_PEER_S_CONNECTED ) ;
peer . state & = ( ~ RS_PEER_S_ONLINE ) ;
peer . actions = RS_PEER_MOVED ;
peer . inConnAttempt = false ;
mOthersList [ id ] = peer ;
mStatusChanged = true ;
mFriendList . erase ( it ) ;
2011-07-09 20:41:39 -04:00
}
2011-07-11 08:11:58 -04:00
mNetMgr - > netAssistFriend ( id , false ) ;
2011-07-09 20:41:39 -04:00
return 1 ;
}
2015-06-01 18:03:07 -04:00
void p3LinkMgrIMPL : : disconnectFriend ( const RsPeerId & id )
{
std : : list < pqiMonitor * > disconnect_clients ;
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
disconnect_clients = clients ;
std : : cerr < < " Disconnecting friend " < < id < < std : : endl ;
std : : map < RsPeerId , peerConnectState > : : iterator it ;
it = mFriendList . find ( id ) ;
if ( it = = mFriendList . end ( ) )
{
std : : cerr < < " p3LinkMgrIMPL::removeFriend() ERROR, friend not there : " < < id ;
std : : cerr < < std : : endl ;
return ;
}
/* Move to OthersList (so remove can be handled via the action) */
peerConnectState peer = it - > second ;
peer . state & = ( ~ RS_PEER_S_CONNECTED ) ;
peer . state & = ( ~ RS_PEER_S_ONLINE ) ;
peer . actions = RS_PEER_DISCONNECTED ;
peer . inConnAttempt = false ;
}
for ( std : : list < pqiMonitor * > : : const_iterator it ( disconnect_clients . begin ( ) ) ; it ! = disconnect_clients . end ( ) ; + + it )
( * it ) - > disconnectPeer ( id ) ;
}
2011-07-09 20:41:39 -04:00
2011-07-13 10:22:25 -04:00
void p3LinkMgrIMPL : : printPeerLists ( std : : ostream & out )
2011-07-11 08:11:58 -04:00
{
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
2011-07-13 10:22:25 -04:00
out < < " p3LinkMgrIMPL::printPeerLists() Friend List " ;
2011-07-11 08:11:58 -04:00
out < < std : : endl ;
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , peerConnectState > : : iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = mFriendList . begin ( ) ; it ! = mFriendList . end ( ) ; + + it )
2011-07-11 08:11:58 -04:00
{
2014-03-17 16:56:06 -04:00
out < < " \t SSL ID: " < < it - > second . id . toStdString ( ) ;
2011-07-11 08:11:58 -04:00
out < < " \t State: " < < it - > second . state ;
out < < std : : endl ;
}
2011-07-13 10:22:25 -04:00
out < < " p3LinkMgrIMPL::printPeerLists() Others List " ;
2011-07-11 08:11:58 -04:00
out < < std : : endl ;
2014-10-24 18:07:26 -04:00
for ( it = mOthersList . begin ( ) ; it ! = mOthersList . end ( ) ; + + it )
2011-07-11 08:11:58 -04:00
{
2014-03-17 16:56:06 -04:00
out < < " \t SSL ID: " < < it - > second . id . toStdString ( ) ;
2011-07-11 08:11:58 -04:00
out < < " \t State: " < < it - > second . state ;
}
}
2015-05-25 10:02:45 -04:00
return ;
}
bool p3LinkMgrIMPL : : checkPotentialAddr ( const sockaddr_storage & addr , time_t age )
{
RsStackMutex stack ( mLinkMtx ) ; /****** STACK LOCK MUTEX *******/
return locked_CheckPotentialAddr ( addr , age ) ;
2011-07-11 08:11:58 -04:00
}
2011-07-09 20:41:39 -04:00
2011-07-10 14:19:46 -04:00
void printConnectState ( std : : ostream & out , peerConnectState & peer )
{
2014-03-17 16:56:06 -04:00
out < < " Friend: " < < peer . name < < " Id: " < < peer . id . toStdString ( ) < < " State: " < < peer . state ;
2011-07-10 14:19:46 -04:00
if ( peer . state & RS_PEER_S_FRIEND )
out < < " S:RS_PEER_S_FRIEND " ;
if ( peer . state & RS_PEER_S_ONLINE )
out < < " S:RS_PEER_S_ONLINE " ;
if ( peer . state & RS_PEER_S_CONNECTED )
out < < " S:RS_PEER_S_CONNECTED " ;
out < < " Actions: " < < peer . actions ;
if ( peer . actions & RS_PEER_NEW )
out < < " A:RS_PEER_NEW " ;
if ( peer . actions & RS_PEER_MOVED )
out < < " A:RS_PEER_MOVED " ;
if ( peer . actions & RS_PEER_CONNECTED )
out < < " A:RS_PEER_CONNECTED " ;
if ( peer . actions & RS_PEER_DISCONNECTED )
out < < " A:RS_PEER_DISCONNECTED " ;
if ( peer . actions & RS_PEER_CONNECT_REQ )
out < < " A:RS_PEER_CONNECT_REQ " ;
out < < std : : endl ;
return ;
}
2011-07-09 20:41:39 -04:00
2011-07-09 05:36:37 -04:00