2018-05-28 16:03:39 -04:00
/*******************************************************************************
* libretroshare / src / pqi : pqipersongrp . cc *
* *
* libretroshare : retroshare core library *
* *
* Copyright ( C ) 2004 - 2008 Robert Fernie < retroshare @ lunamutt . com > *
* *
* 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/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-11-14 22:18:48 -05:00
# include "pqi/pqipersongrp.h"
2011-07-09 14:39:34 -04:00
# include "pqi/p3linkmgr.h"
2008-07-10 12:29:18 -04:00
# include "util/rsdebug.h"
2015-04-23 15:31:41 -04:00
# include "util/rsprint.h"
2017-04-30 14:08:30 -04:00
# include "serialiser/rsserializer.h"
2008-01-25 01:36:40 -05:00
2009-05-11 10:30:53 -04:00
# include <stdio.h>
2007-11-14 22:18:48 -05:00
2016-06-18 07:00:15 -04:00
static struct RsLog : : logInfo pqipersongrpzoneInfo = { RsLog : : Default , " pqipersongrp " } ;
# define pqipersongrpzone &pqipersongrpzoneInfo
2007-11-14 22:18:48 -05:00
2010-10-07 07:17:42 -04:00
# ifdef WINDOWS_SYS
///////////////////////////////////////////////////////////
// hack for too many connections
# include "retroshare/rsinit.h"
2014-03-17 16:56:06 -04:00
static std : : list < RsPeerId > waitingIds ;
2011-08-07 14:48:48 -04:00
# define MAX_CONNECT_COUNT 3
2010-10-07 07:17:42 -04:00
///////////////////////////////////////////////////////////
# endif
2008-04-03 08:51:28 -04:00
/****
2008-07-09 05:55:09 -04:00
* # define PGRP_DEBUG 1
2018-04-01 10:26:48 -04:00
* # define PGRP_DEBUG_LOG 1
2008-07-09 05:55:09 -04:00
* * * */
2007-11-14 22:18:48 -05:00
2012-01-27 08:03:59 -05:00
# define DEFAULT_DOWNLOAD_KB_RATE (200.0)
# define DEFAULT_UPLOAD_KB_RATE (50.0)
2008-11-22 08:15:07 -05:00
/* MUTEX NOTES:
* Functions like GetRsRawItem ( ) lock itself ( pqihandler ) and
* likewise ServiceServer and ConfigMgr mutex themselves .
* This means the only things we need to worry about are :
* pqilistener and when accessing pqihandlers data .
*/
2013-10-01 23:21:04 -04:00
// New speedy recv.
bool pqipersongrp : : RecvRsRawItem ( RsRawItem * item )
{
std : : cerr < < " pqipersongrp::RecvRsRawItem() " ;
std : : cerr < < std : : endl ;
p3ServiceServer : : recvItem ( item ) ;
return true ;
}
2015-04-23 17:37:16 -04:00
# ifdef TO_BE_REMOVED
2007-11-14 22:18:48 -05:00
// handle the tunnel services.
2007-12-11 20:29:14 -05:00
int pqipersongrp : : tickServiceRecv ( )
2007-11-14 22:18:48 -05:00
{
2012-04-13 20:30:23 -04:00
RsRawItem * pqi = NULL ;
2007-11-14 22:18:48 -05:00
int i = 0 ;
2012-04-13 20:30:23 -04:00
2013-10-01 06:11:34 -04:00
pqioutput ( PQL_DEBUG_ALL , pqipersongrpzone , " pqipersongrp::tickServiceRecv() " ) ;
2007-11-14 22:18:48 -05:00
2007-12-11 20:29:14 -05:00
//p3ServiceServer::tick();
2007-11-14 22:18:48 -05:00
2007-12-11 20:29:14 -05:00
while ( NULL ! = ( pqi = GetRsRawItem ( ) ) )
2007-11-14 22:18:48 -05:00
{
2014-12-23 08:13:02 -05:00
static int ntimes = 0 ;
if ( + + ntimes < 20 )
{
2014-03-21 23:53:44 -04:00
std : : cerr < < " pqipersongrp::tickServiceRecv() GetRsRawItem() " ;
2015-04-23 15:31:41 -04:00
std : : cerr < < " should never happen anymore! item data= " < < RsUtil : : BinToHex ( ( char * ) pqi - > getRawData ( ) , pqi - > getRawLength ( ) ) ;
std : : cerr < < std : : endl ;
2014-12-23 08:13:02 -05:00
}
2014-03-21 23:53:44 -04:00
2007-11-14 22:18:48 -05:00
+ + i ;
pqioutput ( PQL_DEBUG_BASIC , pqipersongrpzone ,
2013-10-01 06:11:34 -04:00
" pqipersongrp::tickServiceRecv() Incoming TunnelItem " ) ;
recvItem ( pqi ) ;
2007-11-14 22:18:48 -05:00
}
2007-12-11 20:29:14 -05:00
if ( 0 < i )
2007-11-14 22:18:48 -05:00
{
2007-12-11 20:29:14 -05:00
return 1 ;
2007-11-14 22:18:48 -05:00
}
2007-12-11 20:29:14 -05:00
return 0 ;
}
2015-04-23 17:37:16 -04:00
# endif
2007-12-11 20:29:14 -05:00
// handle the tunnel services.
2013-10-01 06:11:34 -04:00
// Improvements:
// This function is no longer necessary, and data is pushed directly to pqihandler.
2015-04-23 17:37:16 -04:00
# ifdef TO_BE_REMOVED
2007-12-11 20:29:14 -05:00
int pqipersongrp : : tickServiceSend ( )
{
RsRawItem * pqi = NULL ;
int i = 0 ;
2012-04-13 20:30:23 -04:00
pqioutput ( PQL_DEBUG_ALL , pqipersongrpzone , " pqipersongrp::tickServiceSend() " ) ;
2007-12-11 20:29:14 -05:00
p3ServiceServer : : tick ( ) ;
2007-11-14 22:18:48 -05:00
2008-11-22 08:15:07 -05:00
while ( NULL ! = ( pqi = outgoing ( ) ) ) /* outgoing has own locking */
2007-11-14 22:18:48 -05:00
{
+ + i ;
pqioutput ( PQL_DEBUG_BASIC , pqipersongrpzone ,
2007-12-11 20:29:14 -05:00
" pqipersongrp::tickTunnelServer() OutGoing RsItem " ) ;
2007-11-14 22:18:48 -05:00
2008-11-22 08:15:07 -05:00
SendRsRawItem ( pqi ) ; /* Locked by pqihandler */
2007-11-14 22:18:48 -05:00
}
if ( 0 < i )
{
return 1 ;
}
return 0 ;
}
2013-10-01 06:11:34 -04:00
# endif
2007-11-14 22:18:48 -05:00
2008-01-25 01:36:40 -05:00
// init
2015-06-17 04:49:43 -04:00
pqipersongrp : : pqipersongrp ( p3ServiceControl * ctrl , unsigned long flags )
2016-05-18 13:15:37 -04:00
: pqihandler ( ) , p3ServiceServer ( this , ctrl ) , pqil ( NULL ) , pqilMtx ( " pqipersongrp " ) , initFlags ( flags )
2007-11-14 22:18:48 -05:00
{
}
2008-01-25 01:36:40 -05:00
2007-11-14 22:18:48 -05:00
int pqipersongrp : : tick ( )
{
/* could limit the ticking of listener / tunnels to 1/sec...
* but not to important .
*/
2012-11-25 11:36:55 -05:00
{
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2012-11-25 11:36:55 -05:00
if ( pqil )
{
pqil - > tick ( ) ;
}
} /* UNLOCKED */
2008-11-22 08:15:07 -05:00
2007-12-11 20:29:14 -05:00
int i = 0 ;
2015-04-23 17:37:16 -04:00
# ifdef TO_BE_REMOVED
if ( tickServiceSend ( ) )
2008-04-03 08:51:28 -04:00
{
i = 1 ;
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-04-03 08:51:28 -04:00
std : : cerr < < " pqipersongrp::tick() moreToTick from tickServiceSend() " < < std : : endl ;
# endif
}
2013-10-01 06:11:34 -04:00
if ( pqihandler : : tick ( ) ) /* does Send/Recv */
2008-04-03 08:51:28 -04:00
{
i = 1 ;
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-04-03 08:51:28 -04:00
std : : cerr < < " pqipersongrp::tick() moreToTick from pqihandler::tick() " < < std : : endl ;
# endif
}
if ( tickServiceRecv ( ) )
{
i = 1 ;
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-04-03 08:51:28 -04:00
std : : cerr < < " pqipersongrp::tick() moreToTick from tickServiceRecv() " < < std : : endl ;
# endif
2015-04-23 17:37:16 -04:00
}
2013-10-01 06:11:34 -04:00
# endif
2015-04-24 05:29:40 -04:00
if ( pqihandler : : tick ( ) )
i = 1 ;
2015-04-23 17:37:16 -04:00
p3ServiceServer : : tick ( ) ;
2013-10-01 06:11:34 -04:00
2008-04-03 08:51:28 -04:00
return i ;
2007-11-14 22:18:48 -05:00
}
int pqipersongrp : : status ( )
{
2012-11-25 11:36:55 -05:00
{
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2012-11-25 11:36:55 -05:00
if ( pqil )
{
pqil - > status ( ) ;
}
} /* UNLOCKED */
2008-11-22 08:15:07 -05:00
2007-11-14 22:18:48 -05:00
return pqihandler : : status ( ) ;
}
2008-01-25 01:36:40 -05:00
/* Initialise pqilistener */
int pqipersongrp : : init_listener ( )
2007-11-14 22:18:48 -05:00
{
2008-01-25 01:36:40 -05:00
/* extract our information from the p3ConnectMgr */
if ( initFlags & PQIPERSON_NO_LISTENER )
2007-11-14 22:18:48 -05:00
{
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2008-01-25 01:36:40 -05:00
pqil = NULL ;
}
else
{
/* extract details from
*/
2013-09-13 10:35:19 -04:00
struct sockaddr_storage laddr ;
mLinkMgr - > getLocalAddress ( laddr ) ;
2011-07-09 14:39:34 -04:00
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2012-11-25 11:36:55 -05:00
pqil = locked_createListener ( laddr ) ;
2007-11-14 22:18:48 -05:00
}
2008-01-25 01:36:40 -05:00
return 1 ;
}
2007-11-14 22:18:48 -05:00
2013-09-13 10:35:19 -04:00
bool pqipersongrp : : resetListener ( const struct sockaddr_storage & local )
2008-01-25 01:36:40 -05:00
{
2010-02-07 16:28:40 -05:00
# ifdef PGRP_DEBUG
2010-07-31 14:14:10 -04:00
std : : cerr < < " pqipersongrp::resetListener() " < < std : : endl ;
2010-02-07 16:28:40 -05:00
# endif
2009-08-04 19:22:44 -04:00
2008-01-25 01:36:40 -05:00
// stop it,
// change the address.
// restart.
2008-11-22 08:15:07 -05:00
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2008-11-22 08:15:07 -05:00
2010-07-31 14:14:10 -04:00
if ( pqil ! = NULL )
2007-12-11 20:29:14 -05:00
{
2010-02-07 16:28:40 -05:00
# ifdef PGRP_DEBUG
2010-07-31 14:14:10 -04:00
std : : cerr < < " pqipersongrp::resetListener() haveListener " < < std : : endl ;
2010-02-07 16:28:40 -05:00
# endif
2009-08-04 19:22:44 -04:00
2008-01-25 01:36:40 -05:00
pqil - > resetlisten ( ) ;
2010-07-31 14:14:10 -04:00
pqil - > setListenAddr ( local ) ;
2008-01-25 01:36:40 -05:00
pqil - > setuplisten ( ) ;
2009-08-04 19:22:44 -04:00
2010-02-07 16:28:40 -05:00
# ifdef PGRP_DEBUG
2010-07-31 14:14:10 -04:00
std : : cerr < < " pqipersongrp::resetListener() done! " < < std : : endl ;
2010-02-07 16:28:40 -05:00
# endif
2009-08-04 19:22:44 -04:00
2007-12-11 20:29:14 -05:00
}
2008-01-25 01:36:40 -05:00
return 1 ;
}
2007-12-11 20:29:14 -05:00
2008-01-25 01:36:40 -05:00
void pqipersongrp : : statusChange ( const std : : list < pqipeer > & plist )
{
/* iterate through, only worry about the friends */
std : : list < pqipeer > : : const_iterator it ;
2014-10-24 18:07:26 -04:00
for ( it = plist . begin ( ) ; it ! = plist . end ( ) ; + + it )
2008-01-25 01:36:40 -05:00
{
if ( it - > state & RS_PEER_S_FRIEND )
{
/* now handle add/remove */
if ( ( it - > actions & RS_PEER_NEW )
| | ( it - > actions & RS_PEER_MOVED ) )
{
addPeer ( it - > id ) ;
}
if ( it - > actions & RS_PEER_CONNECT_REQ )
{
connectPeer ( it - > id ) ;
}
}
else /* Not Friend */
{
if ( it - > actions & RS_PEER_MOVED )
{
removePeer ( it - > id ) ;
}
}
}
}
2007-11-14 22:18:48 -05:00
2010-10-07 07:17:42 -04:00
# ifdef WINDOWS_SYS
///////////////////////////////////////////////////////////
// hack for too many connections
void pqipersongrp : : statusChanged ( )
{
# warning "Windows connection limited hacked together - please fix"
if ( RsInit : : isWindowsXP ( ) = = false ) {
/* the problem only exist in Windows XP */
2012-11-25 15:52:10 -05:00
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
2010-10-07 07:17:42 -04:00
waitingIds . clear ( ) ;
return ;
}
2012-11-25 15:52:10 -05:00
{
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
if ( waitingIds . empty ( ) )
{
/* nothing to do */
return ;
}
2011-08-04 19:27:58 -04:00
}
2010-10-07 07:17:42 -04:00
/* check for active connections and start waiting id's */
long connect_count = 0 ;
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > toConnect ;
2010-10-07 07:17:42 -04:00
{
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
/* get address from p3connmgr */
2011-07-09 14:39:34 -04:00
if ( ! mLinkMgr ) {
2010-10-07 07:17:42 -04:00
return ;
}
/* check for active connections and start waiting id's */
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > peers ;
2011-07-09 14:39:34 -04:00
mLinkMgr - > getFriendList ( peers ) ;
2010-10-07 07:17:42 -04:00
/* count connection attempts */
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > : : iterator peer ;
2014-10-24 18:07:26 -04:00
for ( peer = peers . begin ( ) ; peer ! = peers . end ( ) ; + + peer ) {
2010-10-07 07:17:42 -04:00
peerConnectState state ;
2011-07-09 14:39:34 -04:00
if ( mLinkMgr - > getFriendNetStatus ( * peer , state ) = = false ) {
2010-10-07 07:17:42 -04:00
continue ;
}
if ( state . inConnAttempt ) {
connect_count + + ;
if ( connect_count > = MAX_CONNECT_COUNT ) {
# ifdef PGRP_DEBUG
2011-08-07 14:48:48 -04:00
std : : cerr < < " pqipersongrp::statusChanged() Too many connections due to windows limitations. There are " < < waitingIds . size ( ) < < " waiting connections. " < < std : : endl ;
2010-10-07 07:17:42 -04:00
# endif
return ;
}
}
}
# ifdef PGRP_DEBUG
2012-11-25 15:52:10 -05:00
std : : cerr < < " pqipersongrp::statusChanged() There are " ;
std : : cerr < < connect_count < < " connection attempts and " < < waitingIds . size ( ) ;
std : : cerr < < " waiting connections. Can start " ;
std : : cerr < < ( MAX_CONNECT_COUNT - connect_count ) < < " connection attempts. " ;
std : : cerr < < std : : endl ;
2010-10-07 07:17:42 -04:00
# endif
2012-11-25 15:52:10 -05:00
/* start some waiting id's */
for ( int i = connect_count ; i < MAX_CONNECT_COUNT ; i + + )
{
if ( waitingIds . empty ( ) ) {
break ;
}
2014-03-17 16:56:06 -04:00
RsPeerId waitingId = waitingIds . front ( ) ;
2012-11-25 15:52:10 -05:00
waitingIds . pop_front ( ) ;
2012-11-25 16:48:00 -05:00
# ifdef PGRP_DEBUG
2012-11-25 15:52:10 -05:00
std : : cerr < < " pqipersongrp::statusChanged() id: " < < waitingId < < " connect peer " ;
std : : cerr < < std : : endl ;
2012-11-25 16:48:00 -05:00
# endif
2012-11-25 15:52:10 -05:00
toConnect . push_back ( waitingId ) ;
2010-10-07 07:17:42 -04:00
}
2012-11-25 15:52:10 -05:00
} /* UNLOCKED */
2011-08-07 14:48:48 -04:00
2014-03-17 16:56:06 -04:00
std : : list < RsPeerId > : : iterator cit ;
2014-10-24 18:07:26 -04:00
for ( cit = toConnect . begin ( ) ; cit ! = toConnect . end ( ) ; + + cit )
2012-11-25 15:52:10 -05:00
{
connectPeer ( * cit , true ) ;
2010-10-07 07:17:42 -04:00
}
}
///////////////////////////////////////////////////////////
# endif
2007-11-14 22:18:48 -05:00
2014-03-17 16:56:06 -04:00
bool pqipersongrp : : getCryptoParams ( const RsPeerId & id , RsPeerCryptoParams & params )
2013-06-28 17:47:25 -04:00
{
RsStackMutex stack ( coreMtx ) ; /******* LOCKED MUTEX **********/
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , SearchModule * > : : iterator it = mods . find ( id ) ;
2013-09-10 15:31:44 -04:00
if ( it = = mods . end ( ) )
return false ;
return it - > second - > pqi - > getCryptoParams ( params ) ;
//return locked_getCryptoParams(id,params) ;
2013-06-28 17:47:25 -04:00
}
2007-11-14 22:18:48 -05:00
2014-03-17 16:56:06 -04:00
int pqipersongrp : : addPeer ( const RsPeerId & id )
2008-01-25 01:36:40 -05:00
{
2014-03-17 16:56:06 -04:00
pqioutput ( PQL_DEBUG_BASIC , pqipersongrpzone , " pqipersongrp::addPeer() PeerId: " + id . toStdString ( ) ) ;
2008-01-25 01:36:40 -05:00
2011-07-11 08:11:58 -04:00
std : : cerr < < " pqipersongrp::addPeer() id: " < < id ;
2008-01-25 01:36:40 -05:00
std : : cerr < < std : : endl ;
2011-07-11 08:11:58 -04:00
# ifdef PGRP_DEBUG
2008-07-09 05:55:09 -04:00
# endif
2008-01-25 01:36:40 -05:00
SearchModule * sm = NULL ;
2012-11-25 11:36:55 -05:00
{
// The Mutex is required here as pqiListener is not thread-safe.
2016-05-18 13:15:37 -04:00
RsStackMutex stack ( pqilMtx ) ; /******* LOCKED MUTEX **********/
2012-11-25 11:36:55 -05:00
pqiperson * pqip = locked_createPerson ( id , pqil ) ;
// attach to pqihandler
sm = new SearchModule ( ) ;
sm - > peerid = id ;
sm - > pqi = pqip ;
// reset it to start it working.
2018-04-01 10:26:48 -04:00
# ifdef PGRP_DEBUG_LOG
2012-11-25 11:36:55 -05:00
pqioutput ( PQL_WARNING , pqipersongrpzone , " pqipersongrp::addPeer() => reset() called to initialise new person " ) ;
2018-04-01 10:26:48 -04:00
# endif
2012-11-25 11:36:55 -05:00
pqip - > reset ( ) ;
pqip - > listen ( ) ;
2007-11-14 22:18:48 -05:00
2012-11-25 11:36:55 -05:00
} /* UNLOCKED */
2007-11-14 22:18:48 -05:00
return AddSearchModule ( sm ) ;
}
2008-01-25 01:36:40 -05:00
2014-03-17 16:56:06 -04:00
int pqipersongrp : : removePeer ( const RsPeerId & id )
2007-11-14 22:18:48 -05:00
{
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , SearchModule * > : : iterator it ;
2007-11-14 22:18:48 -05:00
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2011-07-11 08:11:58 -04:00
std : : cerr < < " pqipersongrp::removePeer() id: " < < id ;
std : : cerr < < std : : endl ;
2016-01-26 23:00:10 -05:00
# endif
2008-01-25 01:36:40 -05:00
2008-11-22 08:15:07 -05:00
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
2008-01-25 01:36:40 -05:00
it = mods . find ( id ) ;
if ( it ! = mods . end ( ) )
2007-11-14 22:18:48 -05:00
{
2008-01-25 01:36:40 -05:00
SearchModule * mod = it - > second ;
pqiperson * p = ( pqiperson * ) mod - > pqi ;
2010-06-25 18:00:38 -04:00
p - > stoplistening ( ) ;
2011-08-06 08:27:23 -04:00
pqioutput ( PQL_WARNING , pqipersongrpzone , " pqipersongrp::removePeer() => reset() called before deleting person " ) ;
2008-01-25 01:36:40 -05:00
p - > reset ( ) ;
2013-10-01 23:21:04 -04:00
p - > fullstopthreads ( ) ;
2008-01-25 01:36:40 -05:00
delete p ;
mods . erase ( it ) ;
2007-11-14 22:18:48 -05:00
}
2011-07-11 08:11:58 -04:00
else
{
std : : cerr < < " pqipersongrp::removePeer() ERROR doesn't exist! id: " < < id ;
std : : cerr < < std : : endl ;
}
2007-11-14 22:18:48 -05:00
return 1 ;
}
2014-03-17 16:56:06 -04:00
int pqipersongrp : : tagHeartbeatRecvd ( const RsPeerId & id )
2009-12-13 16:59:26 -05:00
{
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , SearchModule * > : : iterator it ;
2009-12-13 16:59:26 -05:00
# ifdef PGRP_DEBUG
2010-06-25 18:00:38 -04:00
std : : cerr < < " pqipersongrp::tagHeartbeatRecvd() id: " < < id ;
std : : cerr < < std : : endl ;
2009-12-13 16:59:26 -05:00
# endif
2010-06-25 18:00:38 -04:00
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
2009-12-13 16:59:26 -05:00
2010-06-25 18:00:38 -04:00
it = mods . find ( id ) ;
if ( it ! = mods . end ( ) )
{
SearchModule * mod = it - > second ;
pqiperson * p = ( pqiperson * ) mod - > pqi ;
p - > receiveHeartbeat ( ) ;
return 1 ;
}
return 0 ;
2009-12-13 16:59:26 -05:00
}
2010-06-25 18:00:38 -04:00
2014-03-17 16:56:06 -04:00
int pqipersongrp : : connectPeer ( const RsPeerId & id
2010-10-07 07:17:42 -04:00
# ifdef WINDOWS_SYS
///////////////////////////////////////////////////////////
// hack for too many connections
, bool bConnect /*= false*/
///////////////////////////////////////////////////////////
# endif
)
2011-08-07 14:48:48 -04:00
{
2011-07-13 18:23:40 -04:00
RsStackMutex stack ( coreMtx ) ; /**************** LOCKED MUTEX ****************/
2012-11-25 11:36:55 -05:00
if ( ! mLinkMgr )
return 0 ;
2011-08-07 14:48:48 -04:00
if ( id = = mLinkMgr - > getOwnId ( ) )
2011-07-13 18:23:40 -04:00
{
2011-08-07 14:48:48 -04:00
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() ERROR Failed, connecting to own id. " < < std : : endl ;
# endif
return 0 ;
}
2014-03-17 16:56:06 -04:00
std : : map < RsPeerId , SearchModule * > : : iterator it ;
2008-01-25 01:36:40 -05:00
it = mods . find ( id ) ;
if ( it = = mods . end ( ) )
2007-11-14 22:18:48 -05:00
{
2008-01-25 01:36:40 -05:00
return 0 ;
2007-11-14 22:18:48 -05:00
}
2008-01-25 01:36:40 -05:00
/* get the connect attempt details from the p3connmgr... */
SearchModule * mod = it - > second ;
pqiperson * p = ( pqiperson * ) mod - > pqi ;
2010-10-07 07:17:42 -04:00
# ifdef WINDOWS_SYS
///////////////////////////////////////////////////////////
// hack for too many connections
if ( RsInit : : isWindowsXP ( ) ) {
/* the problem only exist in Windows XP */
if ( bConnect = = false ) {
/* check for id is waiting */
if ( std : : find ( waitingIds . begin ( ) , waitingIds . end ( ) , id ) ! = waitingIds . end ( ) ) {
/* id is waiting for a connection */
2011-08-07 14:48:48 -04:00
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() id: " < < id < < " is already waiting " ;
std : : cerr < < std : : endl ;
# endif
return 0 ;
}
/* check for connection type of the next connect attempt */
peerConnectState state ;
if ( mLinkMgr - > getFriendNetStatus ( id , state ) = = false ) {
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() id: " < < id < < " No friend net status " ;
std : : cerr < < std : : endl ;
# endif
return 0 ;
}
if ( state . connAddrs . size ( ) < 1 ) {
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() id: " < < id < < " No existing connect addresses " ;
std : : cerr < < std : : endl ;
# endif
return 0 ;
}
const peerConnectAddress & currentConnAddrAttempt = state . connAddrs . front ( ) ;
if ( currentConnAddrAttempt . type & RS_NET_CONN_TCP_ALL ) {
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() id: " < < id < < " added to the waiting list " ;
std : : cerr < < std : : endl ;
# endif
/* TCP connect, add id to waiting */
waitingIds . push_back ( id ) ;
/* wait for call to connectPeer with empty id */
2010-10-07 07:17:42 -04:00
return 0 ;
}
2011-08-07 14:48:48 -04:00
/* try all other types of connect directly */
2010-10-07 07:17:42 -04:00
2011-08-07 14:48:48 -04:00
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() id: " < < id < < " connect directly without wait " ;
std : : cerr < < std : : endl ;
# endif
2010-10-07 07:17:42 -04:00
}
/* remove id from waiting */
waitingIds . remove ( id ) ;
}
///////////////////////////////////////////////////////////
# endif
2013-09-13 10:35:19 -04:00
struct sockaddr_storage addr ;
2008-02-26 21:32:20 -05:00
uint32_t delay ;
uint32_t period ;
2008-03-26 11:35:09 -04:00
uint32_t timeout ;
2008-01-25 01:36:40 -05:00
uint32_t type ;
2011-07-27 12:56:37 -04:00
uint32_t flags = 0 ;
2008-01-25 01:36:40 -05:00
2013-09-13 10:35:19 -04:00
struct sockaddr_storage proxyaddr ;
struct sockaddr_storage srcaddr ;
2011-07-14 10:56:33 -04:00
uint32_t bandwidth ;
2013-09-03 09:35:39 -04:00
std : : string domain_addr ;
uint16_t domain_port ;
2011-07-14 10:56:33 -04:00
2013-09-03 09:35:39 -04:00
if ( ! mLinkMgr - > connectAttempt ( id , addr , proxyaddr , srcaddr , delay , period , type , flags , bandwidth , domain_addr , domain_port ) )
2008-01-28 00:40:32 -05:00
{
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-01-28 00:40:32 -05:00
std : : cerr < < " pqipersongrp::connectPeer() No Net Address " ;
std : : cerr < < std : : endl ;
2008-07-09 05:55:09 -04:00
# endif
2008-01-28 00:40:32 -05:00
return 0 ;
}
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-01-28 00:40:32 -05:00
std : : cerr < < " pqipersongrp::connectPeer() connectAttempt data id: " < < id ;
2013-09-13 10:35:19 -04:00
std : : cerr < < " addr: " < < sockaddr_storage_tostring ( addr ) ;
2008-02-26 21:32:20 -05:00
std : : cerr < < " delay: " < < delay ;
std : : cerr < < " period: " < < period ;
2008-01-28 00:40:32 -05:00
std : : cerr < < " type: " < < type ;
2011-07-13 18:23:40 -04:00
std : : cerr < < " flags: " < < flags ;
2013-09-03 09:35:39 -04:00
std : : cerr < < " domain_addr: " < < domain_addr ;
std : : cerr < < " domain_port: " < < domain_port ;
2008-01-28 00:40:32 -05:00
std : : cerr < < std : : endl ;
2008-07-09 05:55:09 -04:00
# endif
2008-01-28 00:40:32 -05:00
2008-01-25 01:36:40 -05:00
uint32_t ptype ;
if ( type & RS_NET_CONN_TCP_ALL )
{
2015-09-15 11:10:48 -04:00
switch ( type ) {
case RS_NET_CONN_TCP_HIDDEN_TOR :
2015-09-07 05:22:15 -04:00
ptype = PQI_CONNECT_HIDDEN_TOR_TCP ;
timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD ;
2015-09-15 11:10:48 -04:00
break ;
case RS_NET_CONN_TCP_HIDDEN_I2P :
2015-09-07 05:22:15 -04:00
ptype = PQI_CONNECT_HIDDEN_I2P_TCP ;
timeout = RS_TCP_HIDDEN_TIMEOUT_PERIOD ;
2015-09-15 11:10:48 -04:00
break ;
default :
2013-09-03 09:35:39 -04:00
ptype = PQI_CONNECT_TCP ;
2015-09-15 11:10:48 -04:00
timeout = RS_TCP_STD_TIMEOUT_PERIOD ;
break ;
2013-09-03 09:35:39 -04:00
}
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-03-26 11:35:09 -04:00
std : : cerr < < " pqipersongrp::connectPeer() connecting with TCP: Timeout : " < < timeout ;
std : : cerr < < std : : endl ;
2008-07-09 05:55:09 -04:00
# endif
2008-01-25 01:36:40 -05:00
}
else if ( type & RS_NET_CONN_UDP_ALL )
2007-11-14 22:18:48 -05:00
{
2008-01-25 01:36:40 -05:00
ptype = PQI_CONNECT_UDP ;
2011-07-18 20:06:09 -04:00
timeout = period + RS_UDP_STD_TIMEOUT_PERIOD ; // Split of UNCERTAINTY + TIME FOR TTL to RISE to Connection.
2008-07-09 05:55:09 -04:00
# ifdef PGRP_DEBUG
2008-03-26 11:35:09 -04:00
std : : cerr < < " pqipersongrp::connectPeer() connecting with UDP: Timeout : " < < timeout ;
std : : cerr < < std : : endl ;
2008-07-09 05:55:09 -04:00
# endif
2007-11-14 22:18:48 -05:00
}
2008-01-25 01:36:40 -05:00
else
2010-07-04 06:35:38 -04:00
{
# ifdef PGRP_DEBUG
std : : cerr < < " pqipersongrp::connectPeer() Ignoring Unknown Type: " < < type ;
std : : cerr < < std : : endl ;
# endif
2008-01-25 01:36:40 -05:00
return 0 ;
2010-07-04 06:35:38 -04:00
}
2008-01-25 01:36:40 -05:00
2013-09-03 09:35:39 -04:00
p - > connect ( ptype , addr , proxyaddr , srcaddr , delay , period , timeout , flags , bandwidth , domain_addr , domain_port ) ;
2008-01-25 01:36:40 -05:00
2007-11-14 22:18:48 -05:00
return 1 ;
}
2015-05-16 07:19:53 -04:00
bool pqipersongrp : : notifyConnect ( const RsPeerId & id , uint32_t ptype , bool success , bool isIncomingConnection , const struct sockaddr_storage & raddr )
2007-11-14 22:18:48 -05:00
{
2010-06-25 18:00:38 -04:00
uint32_t type = 0 ;
2008-03-26 11:35:09 -04:00
if ( ptype = = PQI_CONNECT_TCP )
{
type = RS_NET_CONN_TCP_ALL ;
}
2009-12-13 16:59:26 -05:00
else if ( ptype = = PQI_CONNECT_UDP )
2008-03-26 11:35:09 -04:00
{
type = RS_NET_CONN_UDP_ALL ;
}
2010-06-25 18:00:38 -04:00
2011-07-09 14:39:34 -04:00
if ( mLinkMgr )
2015-05-16 07:19:53 -04:00
mLinkMgr - > connectResult ( id , success , isIncomingConnection , type , raddr ) ;
2008-01-25 01:36:40 -05:00
2011-07-09 14:39:34 -04:00
return ( NULL ! = mLinkMgr ) ;
2007-11-14 22:18:48 -05:00
}
2008-01-25 01:36:40 -05:00
/******************************** DUMMY Specific features ***************************/
2007-11-14 22:18:48 -05:00
2008-01-25 01:36:40 -05:00
# include "pqi/pqibin.h"
2007-11-14 22:18:48 -05:00
2013-09-13 10:35:19 -04:00
pqilistener * pqipersongrpDummy : : locked_createListener ( const struct sockaddr_storage & /*laddr*/ )
2007-11-14 22:18:48 -05:00
{
2008-01-25 01:36:40 -05:00
pqilistener * listener = new pqilistener ( ) ;
return listener ;
2007-11-14 22:18:48 -05:00
}
2014-03-17 16:56:06 -04:00
pqiperson * pqipersongrpDummy : : locked_createPerson ( const RsPeerId & id , pqilistener * /*listener*/ )
2007-11-14 22:18:48 -05:00
{
2014-03-17 16:56:06 -04:00
pqioutput ( PQL_DEBUG_BASIC , pqipersongrpzone , " pqipersongrpDummy::createPerson() PeerId: " + id . toStdString ( ) ) ;
2007-11-14 22:18:48 -05:00
2008-01-25 01:36:40 -05:00
pqiperson * pqip = new pqiperson ( id , this ) ;
2007-11-14 22:18:48 -05:00
2008-01-25 01:36:40 -05:00
// TCP
NetBinDummy * d1 = new NetBinDummy ( pqip , id , PQI_CONNECT_TCP ) ;
RsSerialiser * rss = new RsSerialiser ( ) ;
2017-04-30 13:17:44 -04:00
rss - > addSerialType ( new RsRawSerialiser ( ) ) ;
2008-01-25 01:36:40 -05:00
2013-10-01 23:21:04 -04:00
pqiconnect * pqic = new pqiconnect ( pqip , rss , d1 ) ;
2008-01-25 01:36:40 -05:00
pqip - > addChildInterface ( PQI_CONNECT_TCP , pqic ) ;
// UDP.
NetBinDummy * d2 = new NetBinDummy ( pqip , id , PQI_CONNECT_UDP ) ;
RsSerialiser * rss2 = new RsSerialiser ( ) ;
2017-04-30 13:17:44 -04:00
rss2 - > addSerialType ( new RsRawSerialiser ( ) ) ;
2008-01-25 01:36:40 -05:00
2013-10-01 23:21:04 -04:00
pqiconnect * pqic2 = new pqiconnect ( pqip , rss2 , d2 ) ;
2008-01-25 01:36:40 -05:00
pqip - > addChildInterface ( PQI_CONNECT_UDP , pqic2 ) ;
return pqip ;
2007-11-14 22:18:48 -05:00
}
2008-01-25 01:36:40 -05:00
/******************************** DUMMY Specific features ***************************/
2007-11-14 22:18:48 -05:00