2010-08-29 18:58:15 +02:00
// Copyright (c) 2009-2010 Satoshi Nakamoto
2012-02-07 17:28:30 +01:00
// Copyright (c) 2009-2012 The Bitcoin developers
2010-08-29 18:58:15 +02:00
// Distributed under the MIT/X11 software license, see the accompanying
2012-05-18 16:02:28 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2010-08-29 18:58:15 +02:00
2013-05-28 01:55:01 +02:00
# if defined(HAVE_CONFIG_H)
# include "bitcoin-config.h"
# endif
2013-05-07 15:16:25 +02:00
# include "chainparams.h"
2011-05-14 22:57:34 +02:00
# include "db.h"
2011-05-14 23:20:30 +02:00
# include "net.h"
2013-06-06 09:04:33 +02:00
# include "core.h"
2012-01-04 23:39:45 +01:00
# include "addrman.h"
2012-04-15 22:10:54 +02:00
# include "ui_interface.h"
2012-08-13 05:26:30 +02:00
# include "script.h"
2010-08-29 18:58:15 +02:00
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2011-07-02 03:59:37 +02:00
# include <string.h>
# endif
2013-07-17 10:51:40 +02:00
# ifndef WIN32
# include <fcntl.h>
# endif
2011-03-26 13:01:27 +01:00
# ifdef USE_UPNP
# include <miniupnpc/miniwget.h>
# include <miniupnpc/miniupnpc.h>
# include <miniupnpc/upnpcommands.h>
# include <miniupnpc/upnperrors.h>
# endif
2013-06-24 00:23:28 +02:00
// Dump addresses to peers.dat every 15 minutes (900s)
# define DUMP_ADDRESSES_INTERVAL 900
2013-05-28 01:55:01 +02:00
# if !defined(HAVE_MSG_NOSIGNAL)
# define MSG_NOSIGNAL 0
# endif
2013-06-24 00:23:28 +02:00
2011-05-15 09:11:04 +02:00
using namespace std ;
using namespace boost ;
2010-08-29 18:58:15 +02:00
static const int MAX_OUTBOUND_CONNECTIONS = 8 ;
2012-05-10 18:44:07 +02:00
bool OpenNetworkConnection ( const CAddress & addrConnect , CSemaphoreGrant * grantOutbound = NULL , const char * strDest = NULL , bool fOneShot = false ) ;
2010-08-29 18:58:15 +02:00
2012-05-13 00:41:24 +02:00
struct LocalServiceInfo {
int nScore ;
int nPort ;
} ;
2010-08-29 18:58:15 +02:00
//
// Global state variables
//
2012-05-24 19:02:21 +02:00
bool fDiscover = true ;
2013-01-09 19:39:52 +01:00
uint64 nLocalServices = NODE_NETWORK ;
2012-04-10 20:22:04 +02:00
static CCriticalSection cs_mapLocalHost ;
2012-05-13 00:41:24 +02:00
static map < CNetAddr , LocalServiceInfo > mapLocalHost ;
2012-04-10 20:22:04 +02:00
static bool vfReachable [ NET_MAX ] = { } ;
2012-05-04 16:46:22 +02:00
static bool vfLimited [ NET_MAX ] = { } ;
2011-08-11 13:41:01 +02:00
static CNode * pnodeLocalHost = NULL ;
2013-04-05 00:43:04 +02:00
static CNode * pnodeSync = NULL ;
2011-12-21 22:33:19 +01:00
uint64 nLocalHostNonce = 0 ;
2012-05-11 15:28:59 +02:00
static std : : vector < SOCKET > vhListenSocket ;
2012-01-04 23:39:45 +01:00
CAddrMan addrman ;
2013-04-26 00:46:47 +02:00
int nMaxConnections = 125 ;
2010-08-29 18:58:15 +02:00
vector < CNode * > vNodes ;
CCriticalSection cs_vNodes ;
map < CInv , CDataStream > mapRelay ;
2011-12-21 22:33:19 +01:00
deque < pair < int64 , CInv > > vRelayExpiration ;
2010-08-29 18:58:15 +02:00
CCriticalSection cs_mapRelay ;
2013-03-28 00:45:43 +01:00
limitedmap < CInv , int64 > mapAlreadyAskedFor ( MAX_INV_SZ ) ;
2010-08-29 18:58:15 +02:00
2012-04-24 02:15:00 +02:00
static deque < string > vOneShots ;
CCriticalSection cs_vOneShots ;
2010-08-29 18:58:15 +02:00
2011-12-17 01:48:03 +01:00
set < CNetAddr > setservAddNodeAddresses ;
CCriticalSection cs_setservAddNodeAddresses ;
2012-07-02 19:55:16 +02:00
vector < std : : string > vAddedNodes ;
CCriticalSection cs_vAddedNodes ;
2012-05-10 18:44:07 +02:00
static CSemaphore * semOutbound = NULL ;
2010-08-29 18:58:15 +02:00
2013-06-06 05:21:41 +02:00
// Signals for message handling
static CNodeSignals g_signals ;
CNodeSignals & GetNodeSignals ( ) { return g_signals ; }
2013-01-07 17:07:51 +01:00
2012-04-24 02:15:00 +02:00
void AddOneShot ( string strDest )
{
LOCK ( cs_vOneShots ) ;
vOneShots . push_back ( strDest ) ;
}
2011-04-21 16:45:08 +02:00
unsigned short GetListenPort ( )
{
2013-05-07 15:16:25 +02:00
return ( unsigned short ) ( GetArg ( " -port " , Params ( ) . GetDefaultPort ( ) ) ) ;
2011-04-21 16:45:08 +02:00
}
2010-08-29 18:58:15 +02:00
2012-02-12 13:45:24 +01:00
// find 'best' local address for a particular peer
2012-05-10 20:35:13 +02:00
bool GetLocal ( CService & addr , const CNetAddr * paddrPeer )
2012-02-12 13:45:24 +01:00
{
2012-05-24 19:02:21 +02:00
if ( fNoListen )
2012-02-12 13:45:24 +01:00
return false ;
2010-08-29 18:58:15 +02:00
2012-05-13 00:41:24 +02:00
int nBestScore = - 1 ;
2012-02-12 13:45:24 +01:00
int nBestReachability = - 1 ;
{
LOCK ( cs_mapLocalHost ) ;
2012-05-13 00:41:24 +02:00
for ( map < CNetAddr , LocalServiceInfo > : : iterator it = mapLocalHost . begin ( ) ; it ! = mapLocalHost . end ( ) ; it + + )
2012-02-12 13:45:24 +01:00
{
2012-05-13 00:41:24 +02:00
int nScore = ( * it ) . second . nScore ;
2012-02-12 13:45:24 +01:00
int nReachability = ( * it ) . first . GetReachabilityFrom ( paddrPeer ) ;
2012-05-13 00:41:24 +02:00
if ( nReachability > nBestReachability | | ( nReachability = = nBestReachability & & nScore > nBestScore ) )
2012-02-12 13:45:24 +01:00
{
2012-05-13 00:41:24 +02:00
addr = CService ( ( * it ) . first , ( * it ) . second . nPort ) ;
2012-02-12 13:45:24 +01:00
nBestReachability = nReachability ;
2012-05-13 00:41:24 +02:00
nBestScore = nScore ;
2012-02-12 13:45:24 +01:00
}
}
}
2012-05-13 00:41:24 +02:00
return nBestScore > = 0 ;
2012-02-12 13:45:24 +01:00
}
2010-08-29 18:58:15 +02:00
2012-02-12 13:45:24 +01:00
// get best local address for a particular peer as a CAddress
CAddress GetLocalAddress ( const CNetAddr * paddrPeer )
{
CAddress ret ( CService ( " 0.0.0.0 " , 0 ) , 0 ) ;
2012-05-10 20:35:13 +02:00
CService addr ;
2012-02-12 13:45:24 +01:00
if ( GetLocal ( addr , paddrPeer ) )
{
2012-05-10 20:35:13 +02:00
ret = CAddress ( addr ) ;
2012-02-12 13:45:24 +01:00
ret . nServices = nLocalServices ;
ret . nTime = GetAdjustedTime ( ) ;
}
return ret ;
}
2010-08-29 18:58:15 +02:00
2012-02-19 19:05:41 +01:00
bool RecvLine ( SOCKET hSocket , string & strLine )
{
strLine = " " ;
2013-07-31 06:06:44 +02:00
while ( true )
2012-02-19 19:05:41 +01:00
{
char c ;
int nBytes = recv ( hSocket , & c , 1 , 0 ) ;
if ( nBytes > 0 )
{
if ( c = = ' \n ' )
continue ;
if ( c = = ' \r ' )
return true ;
strLine + = c ;
if ( strLine . size ( ) > = 9000 )
return true ;
}
else if ( nBytes < = 0 )
{
2013-03-09 18:02:57 +01:00
boost : : this_thread : : interruption_point ( ) ;
2012-02-19 19:05:41 +01:00
if ( nBytes < 0 )
{
int nErr = WSAGetLastError ( ) ;
if ( nErr = = WSAEMSGSIZE )
continue ;
if ( nErr = = WSAEWOULDBLOCK | | nErr = = WSAEINTR | | nErr = = WSAEINPROGRESS )
{
2013-03-07 20:25:21 +01:00
MilliSleep ( 10 ) ;
2012-02-19 19:05:41 +01:00
continue ;
}
}
if ( ! strLine . empty ( ) )
return true ;
if ( nBytes = = 0 )
{
// socket closed
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " socket closed \n " ) ;
2012-02-19 19:05:41 +01:00
return false ;
}
else
{
// socket error
int nErr = WSAGetLastError ( ) ;
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " recv failed: %d \n " , nErr ) ;
2012-02-19 19:05:41 +01:00
return false ;
}
}
}
}
2012-02-12 13:45:24 +01:00
// used when scores of local addresses may have changed
// pushes better local address to peers
void static AdvertizeLocal ( )
{
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
{
if ( pnode - > fSuccessfullyConnected )
{
CAddress addrLocal = GetLocalAddress ( & pnode - > addr ) ;
2012-05-10 20:35:13 +02:00
if ( addrLocal . IsRoutable ( ) & & ( CService ) addrLocal ! = ( CService ) pnode - > addrLocal )
2012-02-12 13:45:24 +01:00
{
pnode - > PushAddress ( addrLocal ) ;
pnode - > addrLocal = addrLocal ;
}
}
}
}
2012-05-01 21:04:07 +02:00
void SetReachable ( enum Network net , bool fFlag )
{
LOCK ( cs_mapLocalHost ) ;
vfReachable [ net ] = fFlag ;
if ( net = = NET_IPV6 & & fFlag )
vfReachable [ NET_IPV4 ] = true ;
}
2012-02-12 13:45:24 +01:00
// learn a new local address
2012-05-10 20:35:13 +02:00
bool AddLocal ( const CService & addr , int nScore )
2012-02-12 13:45:24 +01:00
{
if ( ! addr . IsRoutable ( ) )
return false ;
2012-05-24 19:02:21 +02:00
if ( ! fDiscover & & nScore < LOCAL_MANUAL )
2012-05-13 14:11:53 +02:00
return false ;
2012-05-13 23:50:49 +02:00
if ( IsLimited ( addr ) )
2012-05-13 15:11:51 +02:00
return false ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " AddLocal(%s,%i) \n " , addr . ToString ( ) . c_str ( ) , nScore ) ;
2012-02-12 13:45:24 +01:00
{
LOCK ( cs_mapLocalHost ) ;
2012-05-13 00:41:24 +02:00
bool fAlready = mapLocalHost . count ( addr ) > 0 ;
LocalServiceInfo & info = mapLocalHost [ addr ] ;
if ( ! fAlready | | nScore > = info . nScore ) {
2012-08-29 02:33:25 +02:00
info . nScore = nScore + ( fAlready ? 1 : 0 ) ;
info . nPort = addr . GetPort ( ) ;
2012-05-13 00:41:24 +02:00
}
2012-05-01 21:04:07 +02:00
SetReachable ( addr . GetNetwork ( ) ) ;
2012-02-12 13:45:24 +01:00
}
AdvertizeLocal ( ) ;
return true ;
}
2012-05-13 01:26:14 +02:00
bool AddLocal ( const CNetAddr & addr , int nScore )
2012-05-10 20:35:13 +02:00
{
2012-05-13 01:26:14 +02:00
return AddLocal ( CService ( addr , GetListenPort ( ) ) , nScore ) ;
2012-05-10 20:35:13 +02:00
}
2012-05-04 16:46:22 +02:00
/** Make a particular network entirely off-limits (no automatic connects to it) */
void SetLimited ( enum Network net , bool fLimited )
{
2012-05-14 17:15:58 +02:00
if ( net = = NET_UNROUTABLE )
return ;
2012-05-04 16:46:22 +02:00
LOCK ( cs_mapLocalHost ) ;
vfLimited [ net ] = fLimited ;
}
2012-05-14 17:15:58 +02:00
bool IsLimited ( enum Network net )
2012-05-04 16:46:22 +02:00
{
LOCK ( cs_mapLocalHost ) ;
2012-05-14 17:15:58 +02:00
return vfLimited [ net ] ;
}
bool IsLimited ( const CNetAddr & addr )
{
return IsLimited ( addr . GetNetwork ( ) ) ;
2012-05-04 16:46:22 +02:00
}
/** vote for a local address */
2012-05-10 20:35:13 +02:00
bool SeenLocal ( const CService & addr )
2012-02-12 13:45:24 +01:00
{
{
LOCK ( cs_mapLocalHost ) ;
if ( mapLocalHost . count ( addr ) = = 0 )
return false ;
2012-05-13 00:41:24 +02:00
mapLocalHost [ addr ] . nScore + + ;
2012-02-12 13:45:24 +01:00
}
AdvertizeLocal ( ) ;
return true ;
}
2012-05-04 16:46:22 +02:00
/** check whether a given address is potentially local */
2012-05-10 20:35:13 +02:00
bool IsLocal ( const CService & addr )
2012-02-12 13:45:24 +01:00
{
LOCK ( cs_mapLocalHost ) ;
return mapLocalHost . count ( addr ) > 0 ;
}
2010-08-29 18:58:15 +02:00
2012-05-04 16:46:22 +02:00
/** check whether a given address is in a network we can probably connect to */
2012-04-10 20:22:04 +02:00
bool IsReachable ( const CNetAddr & addr )
{
LOCK ( cs_mapLocalHost ) ;
2012-05-04 16:46:22 +02:00
enum Network net = addr . GetNetwork ( ) ;
return vfReachable [ net ] & & ! vfLimited [ net ] ;
2012-04-10 20:22:04 +02:00
}
2010-08-29 18:58:15 +02:00
2012-01-03 23:33:31 +01:00
bool GetMyExternalIP2 ( const CService & addrConnect , const char * pszGet , const char * pszKeyword , CNetAddr & ipRet )
2010-08-29 18:58:15 +02:00
{
SOCKET hSocket ;
if ( ! ConnectSocket ( addrConnect , hSocket ) )
return error ( " GetMyExternalIP() : connection to % s failed " , addrConnect.ToString().c_str()) ;
send ( hSocket , pszGet , strlen ( pszGet ) , MSG_NOSIGNAL ) ;
string strLine ;
while ( RecvLine ( hSocket , strLine ) )
{
2010-11-15 22:37:33 +01:00
if ( strLine . empty ( ) ) // HTTP response is separated from headers by blank line
2010-08-29 18:58:15 +02:00
{
2013-07-31 06:06:44 +02:00
while ( true )
2010-08-29 18:58:15 +02:00
{
if ( ! RecvLine ( hSocket , strLine ) )
{
closesocket ( hSocket ) ;
return false ;
}
2010-11-15 22:37:33 +01:00
if ( pszKeyword = = NULL )
break ;
2012-04-15 22:47:24 +02:00
if ( strLine . find ( pszKeyword ) ! = string : : npos )
2010-08-29 18:58:15 +02:00
{
strLine = strLine . substr ( strLine . find ( pszKeyword ) + strlen ( pszKeyword ) ) ;
break ;
}
}
closesocket ( hSocket ) ;
2012-04-15 22:47:24 +02:00
if ( strLine . find ( " < " ) ! = string : : npos )
2010-08-29 18:58:15 +02:00
strLine = strLine . substr ( 0 , strLine . find ( " < " ) ) ;
strLine = strLine . substr ( strspn ( strLine . c_str ( ) , " \t \n \r " ) ) ;
while ( strLine . size ( ) > 0 & & isspace ( strLine [ strLine . size ( ) - 1 ] ) )
strLine . resize ( strLine . size ( ) - 1 ) ;
2012-01-03 23:33:31 +01:00
CService addr ( strLine , 0 , true ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " GetMyExternalIP() received [%s] %s \n " , strLine . c_str ( ) , addr . ToString ( ) . c_str ( ) ) ;
2012-01-03 23:33:31 +01:00
if ( ! addr . IsValid ( ) | | ! addr . IsRoutable ( ) )
2010-08-29 18:58:15 +02:00
return false ;
2012-01-03 23:33:31 +01:00
ipRet . SetIP ( addr ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
}
closesocket ( hSocket ) ;
return error ( " GetMyExternalIP() : connection closed " ) ;
}
2012-01-03 23:33:31 +01:00
bool GetMyExternalIP ( CNetAddr & ipRet )
2010-08-29 18:58:15 +02:00
{
2012-01-17 02:12:35 +01:00
CService addrConnect ;
2010-08-29 18:58:15 +02:00
const char * pszGet ;
const char * pszKeyword ;
for ( int nLookup = 0 ; nLookup < = 1 ; nLookup + + )
for ( int nHost = 1 ; nHost < = 2 ; nHost + + )
{
2013-04-04 11:30:55 +02:00
// We should be phasing out our use of sites like these. If we need
2010-12-15 23:43:51 +01:00
// replacements, we should ask for volunteers to put this simple
2012-07-26 02:48:39 +02:00
// php file on their web server that prints the client IP:
2010-12-15 23:43:51 +01:00
// <?php echo $_SERVER["REMOTE_ADDR"]; ?>
2010-08-29 18:58:15 +02:00
if ( nHost = = 1 )
{
2013-04-04 11:30:55 +02:00
addrConnect = CService ( " 91.198.22.70 " , 80 ) ; // checkip.dyndns.org
2010-08-29 18:58:15 +02:00
if ( nLookup = = 1 )
{
2012-01-03 23:33:31 +01:00
CService addrIP ( " checkip.dyndns.org " , 80 , true ) ;
2011-05-02 15:34:42 +02:00
if ( addrIP . IsValid ( ) )
addrConnect = addrIP ;
2010-08-29 18:58:15 +02:00
}
2010-11-19 21:22:46 +01:00
pszGet = " GET / HTTP/1.1 \r \n "
" Host: checkip.dyndns.org \r \n "
" User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1) \r \n "
2010-08-29 18:58:15 +02:00
" Connection: close \r \n "
" \r \n " ;
2010-11-19 21:22:46 +01:00
pszKeyword = " Address: " ;
2010-08-29 18:58:15 +02:00
}
else if ( nHost = = 2 )
{
2012-01-17 02:12:35 +01:00
addrConnect = CService ( " 74.208.43.192 " , 80 ) ; // www.showmyip.com
2010-08-29 18:58:15 +02:00
if ( nLookup = = 1 )
{
2012-01-03 23:33:31 +01:00
CService addrIP ( " www.showmyip.com " , 80 , true ) ;
2011-05-02 15:34:42 +02:00
if ( addrIP . IsValid ( ) )
addrConnect = addrIP ;
2010-08-29 18:58:15 +02:00
}
2010-11-19 21:22:46 +01:00
pszGet = " GET /simple/ HTTP/1.1 \r \n "
" Host: www.showmyip.com \r \n "
2010-08-29 18:58:15 +02:00
" User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1) \r \n "
" Connection: close \r \n "
" \r \n " ;
2010-11-19 21:22:46 +01:00
pszKeyword = NULL ; // Returns just IP address
2010-08-29 18:58:15 +02:00
}
if ( GetMyExternalIP2 ( addrConnect , pszGet , pszKeyword , ipRet ) )
return true ;
}
return false ;
}
2013-04-23 11:36:54 +02:00
void ThreadGetMyExternalIP ( )
2010-12-15 23:43:51 +01:00
{
2012-02-12 13:45:24 +01:00
CNetAddr addrLocalHost ;
2012-01-03 23:33:31 +01:00
if ( GetMyExternalIP ( addrLocalHost ) )
2010-12-15 23:43:51 +01:00
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " GetMyExternalIP() returned %s \n " , addrLocalHost . ToStringIP ( ) . c_str ( ) ) ;
2012-02-12 13:45:24 +01:00
AddLocal ( addrLocalHost , LOCAL_HTTP ) ;
2010-12-15 23:43:51 +01:00
}
}
2010-08-29 18:58:15 +02:00
2012-01-03 23:33:31 +01:00
void AddressCurrentlyConnected ( const CService & addr )
2010-08-29 18:58:15 +02:00
{
2012-01-04 23:39:45 +01:00
addrman . Connected ( addr ) ;
2010-08-29 18:58:15 +02:00
}
2012-01-03 23:33:31 +01:00
CNode * FindNode ( const CNetAddr & ip )
2010-08-29 18:58:15 +02:00
{
2013-04-04 11:30:55 +02:00
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
if ( ( CNetAddr ) pnode - > addr = = ip )
return ( pnode ) ;
2010-08-29 18:58:15 +02:00
return NULL ;
}
2012-04-19 17:38:03 +02:00
CNode * FindNode ( std : : string addrName )
{
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
if ( pnode - > addrName = = addrName )
return ( pnode ) ;
return NULL ;
}
2012-01-03 23:33:31 +01:00
CNode * FindNode ( const CService & addr )
2010-08-29 18:58:15 +02:00
{
2013-04-04 11:30:55 +02:00
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
if ( ( CService ) pnode - > addr = = addr )
return ( pnode ) ;
2010-08-29 18:58:15 +02:00
return NULL ;
}
2013-03-29 00:43:31 +01:00
CNode * ConnectNode ( CAddress addrConnect , const char * pszDest )
2010-08-29 18:58:15 +02:00
{
2012-04-24 02:15:00 +02:00
if ( pszDest = = NULL ) {
2012-02-12 13:45:24 +01:00
if ( IsLocal ( addrConnect ) )
2012-04-19 17:38:03 +02:00
return NULL ;
2010-08-29 18:58:15 +02:00
2012-04-19 17:38:03 +02:00
// Look for an existing connection
CNode * pnode = FindNode ( ( CService ) addrConnect ) ;
if ( pnode )
{
2013-03-29 00:43:31 +01:00
pnode - > AddRef ( ) ;
2012-04-19 17:38:03 +02:00
return pnode ;
}
2010-08-29 18:58:15 +02:00
}
2012-04-19 17:38:03 +02:00
2010-08-29 18:58:15 +02:00
/// debug print
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " trying connection %s lastseen=%.1fhrs \n " ,
2012-04-19 17:38:03 +02:00
pszDest ? pszDest : addrConnect . ToString ( ) . c_str ( ) ,
2012-05-17 22:58:05 +02:00
pszDest ? 0 : ( double ) ( GetAdjustedTime ( ) - addrConnect . nTime ) / 3600.0 ) ;
2010-08-29 18:58:15 +02:00
// Connect
SOCKET hSocket ;
2013-05-07 15:16:25 +02:00
if ( pszDest ? ConnectSocketByName ( addrConnect , hSocket , pszDest , Params ( ) . GetDefaultPort ( ) ) : ConnectSocket ( addrConnect , hSocket ) )
2010-08-29 18:58:15 +02:00
{
2012-04-19 17:38:03 +02:00
addrman . Attempt ( addrConnect ) ;
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " connected %s \n " , pszDest ? pszDest : addrConnect . ToString ( ) . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
2012-07-26 02:48:39 +02:00
// Set to non-blocking
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2010-08-29 18:58:15 +02:00
u_long nOne = 1 ;
if ( ioctlsocket ( hSocket , FIONBIO , & nOne ) = = SOCKET_ERROR )
2013-09-18 12:38:08 +02:00
LogPrintf ( " ConnectSocket() : ioctlsocket non-blocking setting failed, error %d \n " , WSAGetLastError ( ) ) ;
2010-08-29 18:58:15 +02:00
# else
if ( fcntl ( hSocket , F_SETFL , O_NONBLOCK ) = = SOCKET_ERROR )
2013-09-18 12:38:08 +02:00
LogPrintf ( " ConnectSocket() : fcntl non-blocking setting failed, error %d \n " , errno ) ;
2010-08-29 18:58:15 +02:00
# endif
// Add node
2012-04-19 17:38:03 +02:00
CNode * pnode = new CNode ( hSocket , addrConnect , pszDest ? pszDest : " " , false ) ;
2013-03-29 00:43:31 +01:00
pnode - > AddRef ( ) ;
2012-04-19 17:38:03 +02:00
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
2010-08-29 18:58:15 +02:00
vNodes . push_back ( pnode ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
pnode - > nTimeConnected = GetTime ( ) ;
return pnode ;
}
else
{
return NULL ;
}
}
void CNode : : CloseSocketDisconnect ( )
{
fDisconnect = true ;
if ( hSocket ! = INVALID_SOCKET )
{
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " disconnecting node %s \n " , addrName . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
closesocket ( hSocket ) ;
hSocket = INVALID_SOCKET ;
}
2013-04-05 00:43:04 +02:00
// in case this fails, we'll empty the recv buffer when the CNode is deleted
TRY_LOCK ( cs_vRecvMsg , lockRecv ) ;
if ( lockRecv )
vRecvMsg . clear ( ) ;
// if this was the sync node, we'll need a new one
if ( this = = pnodeSync )
pnodeSync = NULL ;
2010-08-29 18:58:15 +02:00
}
void CNode : : Cleanup ( )
{
}
2011-12-16 22:26:14 +01:00
void CNode : : PushVersion ( )
{
/// when NTP implemented, change to just nTime = GetAdjustedTime()
2011-12-21 22:33:19 +01:00
int64 nTime = ( fInbound ? GetAdjustedTime ( ) : GetTime ( ) ) ;
2012-05-24 19:02:21 +02:00
CAddress addrYou = ( addr . IsRoutable ( ) & & ! IsProxy ( addr ) ? addr : CAddress ( CService ( " 0.0.0.0 " , 0 ) ) ) ;
2012-02-12 13:45:24 +01:00
CAddress addrMe = GetLocalAddress ( & addr ) ;
2011-12-16 22:26:14 +01:00
RAND_bytes ( ( unsigned char * ) & nLocalHostNonce , sizeof ( nLocalHostNonce ) ) ;
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " send version message: version %d, blocks=%d, us=%s, them=%s, peer=%s \n " , PROTOCOL_VERSION , nBestHeight , addrMe . ToString ( ) . c_str ( ) , addrYou . ToString ( ) . c_str ( ) , addr . ToString ( ) . c_str ( ) ) ;
2011-12-16 22:26:14 +01:00
PushMessage ( " version " , PROTOCOL_VERSION , nLocalServices , nTime , addrYou , addrMe ,
2013-06-11 23:10:22 +02:00
nLocalHostNonce , FormatSubVersion ( CLIENT_NAME , CLIENT_VERSION , std : : vector < string > ( ) ) , nBestHeight , true ) ;
2011-12-16 22:26:14 +01:00
}
2012-01-03 23:33:31 +01:00
std : : map < CNetAddr , int64 > CNode : : setBanned ;
2011-09-06 22:09:04 +02:00
CCriticalSection CNode : : cs_setBanned ;
void CNode : : ClearBanned ( )
{
setBanned . clear ( ) ;
}
2012-01-03 23:33:31 +01:00
bool CNode : : IsBanned ( CNetAddr ip )
2011-09-06 22:09:04 +02:00
{
bool fResult = false ;
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_setBanned ) ;
2012-01-03 23:33:31 +01:00
std : : map < CNetAddr , int64 > : : iterator i = setBanned . find ( ip ) ;
2011-09-06 22:09:04 +02:00
if ( i ! = setBanned . end ( ) )
{
2011-12-21 22:33:19 +01:00
int64 t = ( * i ) . second ;
2011-09-06 22:09:04 +02:00
if ( GetTime ( ) < t )
fResult = true ;
}
}
return fResult ;
}
bool CNode : : Misbehaving ( int howmuch )
{
if ( addr . IsLocal ( ) )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " Warning: Local node %s misbehaving (delta: %d)! \n " , addrName . c_str ( ) , howmuch ) ;
2011-09-06 22:09:04 +02:00
return false ;
}
nMisbehavior + = howmuch ;
if ( nMisbehavior > = GetArg ( " -banscore " , 100 ) )
{
2011-12-21 22:33:19 +01:00
int64 banTime = GetTime ( ) + GetArg ( " -bantime " , 60 * 60 * 24 ) ; // Default 24-hour ban
2013-09-18 12:38:08 +02:00
LogPrintf ( " Misbehaving: %s (%d -> %d) DISCONNECTING \n " , addr . ToString ( ) . c_str ( ) , nMisbehavior - howmuch , nMisbehavior ) ;
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_setBanned ) ;
2012-01-03 23:33:31 +01:00
if ( setBanned [ addr ] < banTime )
setBanned [ addr ] = banTime ;
2012-04-06 18:39:12 +02:00
}
2011-09-06 22:09:04 +02:00
CloseSocketDisconnect ( ) ;
return true ;
2012-05-17 23:05:36 +02:00
} else
2013-09-18 12:38:08 +02:00
LogPrintf ( " Misbehaving: %s (%d -> %d) \n " , addr . ToString ( ) . c_str ( ) , nMisbehavior - howmuch , nMisbehavior ) ;
2011-09-06 22:09:04 +02:00
return false ;
}
2012-06-29 23:24:53 +02:00
# undef X
# define X(name) stats.name = name
void CNode : : copyStats ( CNodeStats & stats )
{
X ( nServices ) ;
X ( nLastSend ) ;
X ( nLastRecv ) ;
X ( nTimeConnected ) ;
X ( addrName ) ;
X ( nVersion ) ;
X ( strSubVer ) ;
X ( fInbound ) ;
X ( nStartingHeight ) ;
X ( nMisbehavior ) ;
2013-04-07 19:31:13 +02:00
X ( nSendBytes ) ;
X ( nRecvBytes ) ;
stats . fSyncNode = ( this = = pnodeSync ) ;
2013-08-22 13:34:33 +02:00
// It is common for nodes with good ping times to suddenly become lagged,
// due to a new block arriving or other large transfer.
// Merely reporting pingtime might fool the caller into thinking the node was still responsive,
// since pingtime does not update until the ping is complete, which might take a while.
// So, if a ping is taking an unusually long time in flight,
// the caller can immediately detect that this is happening.
int64 nPingUsecWait = 0 ;
if ( ( 0 ! = nPingNonceSent ) & & ( 0 ! = nPingUsecStart ) ) {
nPingUsecWait = GetTimeMicros ( ) - nPingUsecStart ;
}
// Raw ping time is in microseconds, but show it to user as whole seconds (Bitcoin users should be well used to small numbers with many decimal places by now :)
stats . dPingTime = ( ( ( double ) nPingUsecTime ) / 1e6 ) ;
stats . dPingWait = ( ( ( double ) nPingUsecWait ) / 1e6 ) ;
2012-06-29 23:24:53 +02:00
}
# undef X
2010-08-29 18:58:15 +02:00
2012-11-16 01:41:12 +01:00
// requires LOCK(cs_vRecvMsg)
bool CNode : : ReceiveMsgBytes ( const char * pch , unsigned int nBytes )
{
while ( nBytes > 0 ) {
// get current incomplete message, or create a new one
2013-03-01 01:41:28 +01:00
if ( vRecvMsg . empty ( ) | |
2012-11-16 01:41:12 +01:00
vRecvMsg . back ( ) . complete ( ) )
vRecvMsg . push_back ( CNetMessage ( SER_NETWORK , nRecvVersion ) ) ;
CNetMessage & msg = vRecvMsg . back ( ) ;
// absorb network data
int handled ;
if ( ! msg . in_data )
handled = msg . readHeader ( pch , nBytes ) ;
else
handled = msg . readData ( pch , nBytes ) ;
if ( handled < 0 )
return false ;
pch + = handled ;
nBytes - = handled ;
}
return true ;
}
int CNetMessage : : readHeader ( const char * pch , unsigned int nBytes )
{
// copy data to temporary parsing buffer
unsigned int nRemaining = 24 - nHdrPos ;
unsigned int nCopy = std : : min ( nRemaining , nBytes ) ;
memcpy ( & hdrbuf [ nHdrPos ] , pch , nCopy ) ;
nHdrPos + = nCopy ;
// if header incomplete, exit
if ( nHdrPos < 24 )
return nCopy ;
// deserialize to CMessageHeader
try {
hdrbuf > > hdr ;
}
catch ( std : : exception & e ) {
return - 1 ;
}
// reject messages larger than MAX_SIZE
if ( hdr . nMessageSize > MAX_SIZE )
return - 1 ;
// switch state to reading message data
in_data = true ;
vRecv . resize ( hdr . nMessageSize ) ;
return nCopy ;
}
int CNetMessage : : readData ( const char * pch , unsigned int nBytes )
{
unsigned int nRemaining = hdr . nMessageSize - nDataPos ;
unsigned int nCopy = std : : min ( nRemaining , nBytes ) ;
memcpy ( & vRecv [ nDataPos ] , pch , nCopy ) ;
nDataPos + = nCopy ;
return nCopy ;
}
2010-08-29 18:58:15 +02:00
2012-11-16 00:04:52 +01:00
// requires LOCK(cs_vSend)
void SocketSendData ( CNode * pnode )
{
2013-03-24 16:52:24 +01:00
std : : deque < CSerializeData > : : iterator it = pnode - > vSendMsg . begin ( ) ;
while ( it ! = pnode - > vSendMsg . end ( ) ) {
const CSerializeData & data = * it ;
assert ( data . size ( ) > pnode - > nSendOffset ) ;
int nBytes = send ( pnode - > hSocket , & data [ pnode - > nSendOffset ] , data . size ( ) - pnode - > nSendOffset , MSG_NOSIGNAL | MSG_DONTWAIT ) ;
if ( nBytes > 0 ) {
pnode - > nLastSend = GetTime ( ) ;
2013-04-07 19:31:13 +02:00
pnode - > nSendBytes + = nBytes ;
2013-03-24 16:52:24 +01:00
pnode - > nSendOffset + = nBytes ;
if ( pnode - > nSendOffset = = data . size ( ) ) {
pnode - > nSendOffset = 0 ;
pnode - > nSendSize - = data . size ( ) ;
it + + ;
} else {
// could not send full message; stop sending more
break ;
}
} else {
if ( nBytes < 0 ) {
// error
int nErr = WSAGetLastError ( ) ;
if ( nErr ! = WSAEWOULDBLOCK & & nErr ! = WSAEMSGSIZE & & nErr ! = WSAEINTR & & nErr ! = WSAEINPROGRESS )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket send error %d \n " , nErr ) ;
2013-03-24 16:52:24 +01:00
pnode - > CloseSocketDisconnect ( ) ;
}
}
// couldn't send anything at all
break ;
2012-11-16 00:04:52 +01:00
}
}
2013-03-24 16:52:24 +01:00
if ( it = = pnode - > vSendMsg . end ( ) ) {
assert ( pnode - > nSendOffset = = 0 ) ;
assert ( pnode - > nSendSize = = 0 ) ;
}
pnode - > vSendMsg . erase ( pnode - > vSendMsg . begin ( ) , it ) ;
2012-11-16 00:04:52 +01:00
}
2010-08-29 18:58:15 +02:00
2013-03-29 02:17:10 +01:00
static list < CNode * > vNodesDisconnected ;
2013-03-07 04:31:26 +01:00
void ThreadSocketHandler ( )
2010-08-29 18:58:15 +02:00
{
2012-04-22 20:01:25 +02:00
unsigned int nPrevNodeCount = 0 ;
2013-07-31 06:06:44 +02:00
while ( true )
2010-08-29 18:58:15 +02:00
{
//
// Disconnect nodes
//
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2010-08-29 18:58:15 +02:00
// Disconnect unused nodes
vector < CNode * > vNodesCopy = vNodes ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
{
if ( pnode - > fDisconnect | |
2013-03-24 16:52:24 +01:00
( pnode - > GetRefCount ( ) < = 0 & & pnode - > vRecvMsg . empty ( ) & & pnode - > nSendSize = = 0 & & pnode - > ssSend . empty ( ) ) )
2010-08-29 18:58:15 +02:00
{
// remove from vNodes
vNodes . erase ( remove ( vNodes . begin ( ) , vNodes . end ( ) , pnode ) , vNodes . end ( ) ) ;
2012-05-10 18:44:07 +02:00
// release outbound grant (if any)
pnode - > grantOutbound . Release ( ) ;
2012-04-04 16:01:57 +02:00
2010-08-29 18:58:15 +02:00
// close socket and cleanup
pnode - > CloseSocketDisconnect ( ) ;
pnode - > Cleanup ( ) ;
// hold in disconnected pool until all refs are released
if ( pnode - > fNetworkNode | | pnode - > fInbound )
pnode - > Release ( ) ;
vNodesDisconnected . push_back ( pnode ) ;
}
}
// Delete disconnected nodes
list < CNode * > vNodesDisconnectedCopy = vNodesDisconnected ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesDisconnectedCopy )
2010-08-29 18:58:15 +02:00
{
// wait until threads are done using it
if ( pnode - > GetRefCount ( ) < = 0 )
{
bool fDelete = false ;
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
if ( lockSend )
{
2012-11-16 01:41:12 +01:00
TRY_LOCK ( pnode - > cs_vRecvMsg , lockRecv ) ;
2012-04-06 18:39:12 +02:00
if ( lockRecv )
{
2012-10-03 19:03:43 +02:00
TRY_LOCK ( pnode - > cs_inventory , lockInv ) ;
if ( lockInv )
fDelete = true ;
2012-04-06 18:39:12 +02:00
}
}
}
2010-08-29 18:58:15 +02:00
if ( fDelete )
{
vNodesDisconnected . remove ( pnode ) ;
delete pnode ;
}
}
}
}
if ( vNodes . size ( ) ! = nPrevNodeCount )
{
nPrevNodeCount = vNodes . size ( ) ;
2012-05-06 19:40:58 +02:00
uiInterface . NotifyNumConnectionsChanged ( vNodes . size ( ) ) ;
2010-08-29 18:58:15 +02:00
}
//
// Find which sockets have data to receive
//
struct timeval timeout ;
timeout . tv_sec = 0 ;
timeout . tv_usec = 50000 ; // frequency to poll pnode->vSend
fd_set fdsetRecv ;
fd_set fdsetSend ;
fd_set fdsetError ;
FD_ZERO ( & fdsetRecv ) ;
FD_ZERO ( & fdsetSend ) ;
FD_ZERO ( & fdsetError ) ;
SOCKET hSocketMax = 0 ;
2012-09-05 22:01:28 +02:00
bool have_fds = false ;
2010-12-22 14:08:00 +01:00
2012-05-11 15:28:59 +02:00
BOOST_FOREACH ( SOCKET hListenSocket , vhListenSocket ) {
2010-12-22 14:08:00 +01:00
FD_SET ( hListenSocket , & fdsetRecv ) ;
2012-05-11 15:28:59 +02:00
hSocketMax = max ( hSocketMax , hListenSocket ) ;
2012-09-05 22:01:28 +02:00
have_fds = true ;
2012-05-11 15:28:59 +02:00
}
2010-08-29 18:58:15 +02:00
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2010-08-29 18:58:15 +02:00
{
2011-06-24 20:09:24 +02:00
if ( pnode - > hSocket = = INVALID_SOCKET )
2010-08-29 18:58:15 +02:00
continue ;
2013-04-30 18:42:01 +02:00
FD_SET ( pnode - > hSocket , & fdsetError ) ;
hSocketMax = max ( hSocketMax , pnode - > hSocket ) ;
have_fds = true ;
// Implement the following logic:
// * If there is data to send, select() for sending data. As this only
// happens when optimistic write failed, we choose to first drain the
// write buffer in this case before receiving more. This avoids
// needlessly queueing received data, if the remote peer is not themselves
// receiving data. This means properly utilizing TCP flow control signalling.
// * Otherwise, if there is no (complete) message in the receive buffer,
// or there is space left in the buffer, select() for receiving data.
// * (if neither of the above applies, there is certainly one message
// in the receiver buffer ready to be processed).
// Together, that means that at least one of the following is always possible,
// so we don't deadlock:
// * We send some data.
// * We wait for data to be received (and disconnect after timeout).
// * We process a message in the buffer (message handler thread).
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
2013-04-30 18:42:01 +02:00
if ( lockSend & & ! pnode - > vSendMsg . empty ( ) ) {
FD_SET ( pnode - > hSocket , & fdsetSend ) ;
continue ;
2012-11-16 00:20:26 +01:00
}
2012-04-06 18:39:12 +02:00
}
2013-04-30 18:42:01 +02:00
{
TRY_LOCK ( pnode - > cs_vRecvMsg , lockRecv ) ;
if ( lockRecv & & (
pnode - > vRecvMsg . empty ( ) | | ! pnode - > vRecvMsg . front ( ) . complete ( ) | |
pnode - > GetTotalRecvSize ( ) < = ReceiveFloodSize ( ) ) )
FD_SET ( pnode - > hSocket , & fdsetRecv ) ;
}
2010-08-29 18:58:15 +02:00
}
}
2012-09-05 22:01:28 +02:00
int nSelect = select ( have_fds ? hSocketMax + 1 : 0 ,
& fdsetRecv , & fdsetSend , & fdsetError , & timeout ) ;
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
if ( nSelect = = SOCKET_ERROR )
{
2012-09-05 22:01:28 +02:00
if ( have_fds )
2011-06-07 00:48:37 +02:00
{
2012-09-05 22:01:28 +02:00
int nErr = WSAGetLastError ( ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket select error %d \n " , nErr ) ;
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < = hSocketMax ; i + + )
2011-06-07 00:48:37 +02:00
FD_SET ( i , & fdsetRecv ) ;
}
2010-08-29 18:58:15 +02:00
FD_ZERO ( & fdsetSend ) ;
FD_ZERO ( & fdsetError ) ;
2013-03-07 20:25:21 +01:00
MilliSleep ( timeout . tv_usec / 1000 ) ;
2010-08-29 18:58:15 +02:00
}
//
// Accept new connections
//
2012-05-11 15:28:59 +02:00
BOOST_FOREACH ( SOCKET hListenSocket , vhListenSocket )
2010-12-22 14:08:00 +01:00
if ( hListenSocket ! = INVALID_SOCKET & & FD_ISSET ( hListenSocket , & fdsetRecv ) )
2010-08-29 18:58:15 +02:00
{
2012-03-31 17:58:25 +02:00
# ifdef USE_IPV6
2012-05-11 15:28:59 +02:00
struct sockaddr_storage sockaddr ;
2012-03-31 17:58:25 +02:00
# else
2012-05-11 15:28:59 +02:00
struct sockaddr sockaddr ;
2012-03-31 17:58:25 +02:00
# endif
2010-08-29 18:58:15 +02:00
socklen_t len = sizeof ( sockaddr ) ;
SOCKET hSocket = accept ( hListenSocket , ( struct sockaddr * ) & sockaddr , & len ) ;
2012-02-15 20:56:29 +01:00
CAddress addr ;
2011-01-23 09:08:09 +01:00
int nInbound = 0 ;
2012-02-15 20:56:29 +01:00
if ( hSocket ! = INVALID_SOCKET )
2012-05-11 15:28:59 +02:00
if ( ! addr . SetSockAddr ( ( const struct sockaddr * ) & sockaddr ) )
2013-09-18 12:38:08 +02:00
LogPrintf ( " Warning: Unknown socket family \n " ) ;
2012-02-15 20:56:29 +01:00
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2012-04-06 18:39:12 +02:00
if ( pnode - > fInbound )
nInbound + + ;
}
2012-02-15 20:56:29 +01:00
2010-08-29 18:58:15 +02:00
if ( hSocket = = INVALID_SOCKET )
{
2012-09-05 22:20:26 +02:00
int nErr = WSAGetLastError ( ) ;
if ( nErr ! = WSAEWOULDBLOCK )
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket error accept failed: %d \n " , nErr ) ;
2010-08-29 18:58:15 +02:00
}
2013-04-26 00:46:47 +02:00
else if ( nInbound > = nMaxConnections - MAX_OUTBOUND_CONNECTIONS )
2010-08-29 18:58:15 +02:00
{
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_setservAddNodeAddresses ) ;
2011-12-17 01:48:03 +01:00
if ( ! setservAddNodeAddresses . count ( addr ) )
closesocket ( hSocket ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
}
2012-01-03 23:33:31 +01:00
else if ( CNode : : IsBanned ( addr ) )
2011-09-06 22:09:04 +02:00
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " connection from %s dropped (banned) \n " , addr . ToString ( ) . c_str ( ) ) ;
2011-09-06 22:09:04 +02:00
closesocket ( hSocket ) ;
}
2010-08-29 18:58:15 +02:00
else
{
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " accepted connection %s \n " , addr . ToString ( ) . c_str ( ) ) ;
2012-04-19 17:38:03 +02:00
CNode * pnode = new CNode ( hSocket , addr , " " , true ) ;
2010-08-29 18:58:15 +02:00
pnode - > AddRef ( ) ;
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
2010-08-29 18:58:15 +02:00
vNodes . push_back ( pnode ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
}
}
//
// Service each socket
//
vector < CNode * > vNodesCopy ;
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2010-08-29 18:58:15 +02:00
vNodesCopy = vNodes ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
pnode - > AddRef ( ) ;
}
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
{
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
//
// Receive
//
if ( pnode - > hSocket = = INVALID_SOCKET )
continue ;
if ( FD_ISSET ( pnode - > hSocket , & fdsetRecv ) | | FD_ISSET ( pnode - > hSocket , & fdsetError ) )
{
2012-11-16 01:41:12 +01:00
TRY_LOCK ( pnode - > cs_vRecvMsg , lockRecv ) ;
2012-04-06 18:39:12 +02:00
if ( lockRecv )
2010-08-29 18:58:15 +02:00
{
2013-04-30 18:42:01 +02:00
{
2011-02-16 19:18:11 +01:00
// typical socket buffer is 8K-64K
char pchBuf [ 0x10000 ] ;
int nBytes = recv ( pnode - > hSocket , pchBuf , sizeof ( pchBuf ) , MSG_DONTWAIT ) ;
if ( nBytes > 0 )
2010-08-29 18:58:15 +02:00
{
2012-11-16 01:41:12 +01:00
if ( ! pnode - > ReceiveMsgBytes ( pchBuf , nBytes ) )
pnode - > CloseSocketDisconnect ( ) ;
2011-02-16 19:18:11 +01:00
pnode - > nLastRecv = GetTime ( ) ;
2013-04-07 19:31:13 +02:00
pnode - > nRecvBytes + = nBytes ;
2011-02-16 19:18:11 +01:00
}
else if ( nBytes = = 0 )
{
// socket closed gracefully
2010-08-29 18:58:15 +02:00
if ( ! pnode - > fDisconnect )
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " socket closed \n " ) ;
2010-08-29 18:58:15 +02:00
pnode - > CloseSocketDisconnect ( ) ;
}
2011-02-16 19:18:11 +01:00
else if ( nBytes < 0 )
{
// error
int nErr = WSAGetLastError ( ) ;
if ( nErr ! = WSAEWOULDBLOCK & & nErr ! = WSAEMSGSIZE & & nErr ! = WSAEINTR & & nErr ! = WSAEINPROGRESS )
{
if ( ! pnode - > fDisconnect )
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket recv error %d \n " , nErr ) ;
2011-02-16 19:18:11 +01:00
pnode - > CloseSocketDisconnect ( ) ;
}
}
2010-08-29 18:58:15 +02:00
}
}
}
//
// Send
//
if ( pnode - > hSocket = = INVALID_SOCKET )
continue ;
if ( FD_ISSET ( pnode - > hSocket , & fdsetSend ) )
{
2012-04-06 18:39:12 +02:00
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
if ( lockSend )
2012-11-16 00:04:52 +01:00
SocketSendData ( pnode ) ;
2010-08-29 18:58:15 +02:00
}
//
// Inactivity checking
//
2013-03-24 16:52:24 +01:00
if ( pnode - > vSendMsg . empty ( ) )
2010-08-29 18:58:15 +02:00
pnode - > nLastSendEmpty = GetTime ( ) ;
if ( GetTime ( ) - pnode - > nTimeConnected > 60 )
{
if ( pnode - > nLastRecv = = 0 | | pnode - > nLastSend = = 0 )
{
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " socket no message in first 60 seconds, %d %d \n " , pnode - > nLastRecv ! = 0 , pnode - > nLastSend ! = 0 ) ;
2010-08-29 18:58:15 +02:00
pnode - > fDisconnect = true ;
}
else if ( GetTime ( ) - pnode - > nLastSend > 90 * 60 & & GetTime ( ) - pnode - > nLastSendEmpty > 90 * 60 )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket not sending \n " ) ;
2010-08-29 18:58:15 +02:00
pnode - > fDisconnect = true ;
}
else if ( GetTime ( ) - pnode - > nLastRecv > 90 * 60 )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " socket inactivity timeout \n " ) ;
2010-08-29 18:58:15 +02:00
pnode - > fDisconnect = true ;
}
}
}
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
pnode - > Release ( ) ;
}
2013-03-07 20:25:21 +01:00
MilliSleep ( 10 ) ;
2010-08-29 18:58:15 +02:00
}
}
2011-03-26 13:01:27 +01:00
# ifdef USE_UPNP
2013-03-07 04:31:26 +01:00
void ThreadMapPort ( )
2011-03-26 13:01:27 +01:00
{
2012-09-05 23:36:19 +02:00
std : : string port = strprintf ( " %u " , GetListenPort ( ) ) ;
2011-03-26 13:01:27 +01:00
const char * multicastif = 0 ;
const char * minissdpdpath = 0 ;
struct UPNPDev * devlist = 0 ;
char lanaddr [ 64 ] ;
2011-12-10 17:52:50 +01:00
# ifndef UPNPDISCOVER_SUCCESS
/* miniupnpc 1.5 */
devlist = upnpDiscover ( 2000 , multicastif , minissdpdpath , 0 ) ;
# else
/* miniupnpc 1.6 */
int error = 0 ;
2011-08-12 00:20:07 +02:00
devlist = upnpDiscover ( 2000 , multicastif , minissdpdpath , 0 , 0 , & error ) ;
2011-12-10 17:52:50 +01:00
# endif
2011-03-26 13:01:27 +01:00
struct UPNPUrls urls ;
struct IGDdatas data ;
int r ;
2011-04-16 20:35:45 +02:00
r = UPNP_GetValidIGD ( devlist , & urls , & data , lanaddr , sizeof ( lanaddr ) ) ;
if ( r = = 1 )
2011-03-26 13:01:27 +01:00
{
2012-05-24 19:02:21 +02:00
if ( fDiscover ) {
2012-02-10 04:41:42 +01:00
char externalIPAddress [ 40 ] ;
r = UPNP_GetExternalIPAddress ( urls . controlURL , data . first . servicetype , externalIPAddress ) ;
if ( r ! = UPNPCOMMAND_SUCCESS )
2013-09-18 12:38:08 +02:00
LogPrintf ( " UPnP: GetExternalIPAddress() returned %d \n " , r ) ;
2012-02-10 04:41:42 +01:00
else
{
if ( externalIPAddress [ 0 ] )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " UPnP: ExternalIPAddress = %s \n " , externalIPAddress ) ;
2012-02-19 20:44:35 +01:00
AddLocal ( CNetAddr ( externalIPAddress ) , LOCAL_UPNP ) ;
2012-02-10 04:41:42 +01:00
}
else
2013-09-18 12:38:08 +02:00
LogPrintf ( " UPnP: GetExternalIPAddress failed. \n " ) ;
2012-02-10 04:41:42 +01:00
}
}
2011-08-01 16:34:59 +02:00
string strDesc = " Bitcoin " + FormatFullVersion ( ) ;
2011-08-12 00:20:07 +02:00
2013-03-07 04:31:26 +01:00
try {
2013-07-31 06:06:44 +02:00
while ( true ) {
2012-01-31 23:36:25 +01:00
# ifndef UPNPDISCOVER_SUCCESS
/* miniupnpc 1.5 */
r = UPNP_AddPortMapping ( urls . controlURL , data . first . servicetype ,
2012-09-03 08:23:34 +02:00
port . c_str ( ) , port . c_str ( ) , lanaddr , strDesc . c_str ( ) , " TCP " , 0 ) ;
2012-01-31 23:36:25 +01:00
# else
/* miniupnpc 1.6 */
r = UPNP_AddPortMapping ( urls . controlURL , data . first . servicetype ,
2012-09-03 08:23:34 +02:00
port . c_str ( ) , port . c_str ( ) , lanaddr , strDesc . c_str ( ) , " TCP " , 0 , " 0 " ) ;
2012-01-31 23:36:25 +01:00
# endif
if ( r ! = UPNPCOMMAND_SUCCESS )
2013-09-18 12:38:08 +02:00
LogPrintf ( " AddPortMapping(%s, %s, %s) failed with code %d (%s) \n " ,
2012-09-03 08:23:34 +02:00
port . c_str ( ) , port . c_str ( ) , lanaddr , r , strupnperror ( r ) ) ;
2012-01-31 23:36:25 +01:00
else
2013-09-18 12:38:08 +02:00
LogPrintf ( " UPnP Port Mapping successful. \n " ) ; ;
2013-03-07 04:31:26 +01:00
MilliSleep ( 20 * 60 * 1000 ) ; // Refresh every 20 minutes
2012-01-31 23:36:25 +01:00
}
2013-03-07 04:31:26 +01:00
}
catch ( boost : : thread_interrupted )
{
r = UPNP_DeletePortMapping ( urls . controlURL , data . first . servicetype , port . c_str ( ) , " TCP " , 0 ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " UPNP_DeletePortMapping() returned : %d \n " , r ) ;
2013-03-07 04:31:26 +01:00
freeUPNPDevlist ( devlist ) ; devlist = 0 ;
FreeUPNPUrls ( & urls ) ;
throw ;
2011-03-26 13:01:27 +01:00
}
} else {
2013-09-18 12:38:08 +02:00
LogPrintf ( " No valid UPnP IGDs found \n " ) ;
2011-03-26 13:01:27 +01:00
freeUPNPDevlist ( devlist ) ; devlist = 0 ;
2011-04-16 20:35:45 +02:00
if ( r ! = 0 )
FreeUPNPUrls ( & urls ) ;
2011-03-26 13:01:27 +01:00
}
}
2013-03-07 04:31:26 +01:00
void MapPort ( bool fUseUPnP )
2011-03-26 13:01:27 +01:00
{
2013-03-07 04:31:26 +01:00
static boost : : thread * upnp_thread = NULL ;
if ( fUseUPnP )
2011-03-26 13:01:27 +01:00
{
2013-03-07 04:31:26 +01:00
if ( upnp_thread ) {
upnp_thread - > interrupt ( ) ;
upnp_thread - > join ( ) ;
delete upnp_thread ;
}
2013-04-23 11:36:54 +02:00
upnp_thread = new boost : : thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " upnp " , & ThreadMapPort ) ) ;
2013-03-07 04:31:26 +01:00
}
else if ( upnp_thread ) {
upnp_thread - > interrupt ( ) ;
upnp_thread - > join ( ) ;
delete upnp_thread ;
upnp_thread = NULL ;
2011-03-26 13:01:27 +01:00
}
}
2013-03-07 04:31:26 +01:00
2011-08-09 18:38:17 +02:00
# else
2013-03-07 04:31:26 +01:00
void MapPort ( bool )
2011-08-09 18:38:17 +02:00
{
// Intentionally left blank.
}
2011-03-26 13:01:27 +01:00
# endif
2013-03-07 04:31:26 +01:00
void ThreadDNSAddressSeed ( )
2011-11-21 18:25:00 +01:00
{
2013-05-07 15:16:25 +02:00
const vector < CDNSSeedData > & vSeeds = Params ( ) . DNSSeeds ( ) ;
2011-03-09 04:40:50 +01:00
int found = 0 ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " Loading addresses from DNS seeds (could take a while) \n " ) ;
2013-01-30 05:13:17 +01:00
2013-05-07 15:16:25 +02:00
BOOST_FOREACH ( const CDNSSeedData & seed , vSeeds ) {
2013-01-30 05:13:17 +01:00
if ( HaveNameProxy ( ) ) {
2013-05-07 15:16:25 +02:00
AddOneShot ( seed . host ) ;
2013-01-30 05:13:17 +01:00
} else {
2013-05-07 15:16:25 +02:00
vector < CNetAddr > vIPs ;
2013-01-30 05:13:17 +01:00
vector < CAddress > vAdd ;
2013-05-07 15:16:25 +02:00
if ( LookupHost ( seed . host . c_str ( ) , vIPs ) )
2013-01-30 05:13:17 +01:00
{
2013-05-07 15:16:25 +02:00
BOOST_FOREACH ( CNetAddr & ip , vIPs )
2011-05-02 15:34:42 +02:00
{
2013-01-30 05:13:17 +01:00
int nOneDay = 24 * 3600 ;
2013-05-07 15:16:25 +02:00
CAddress addr = CAddress ( CService ( ip , Params ( ) . GetDefaultPort ( ) ) ) ;
2013-01-30 05:13:17 +01:00
addr . nTime = GetTime ( ) - 3 * nOneDay - GetRand ( 4 * nOneDay ) ; // use a random age between 3 and 7 days old
vAdd . push_back ( addr ) ;
found + + ;
2011-05-02 15:34:42 +02:00
}
2011-03-09 04:40:50 +01:00
}
2013-05-07 15:16:25 +02:00
addrman . Add ( vAdd , CNetAddr ( seed . name , true ) ) ;
2011-03-09 04:40:50 +01:00
}
}
2013-09-18 12:38:08 +02:00
LogPrintf ( " %d addresses found from DNS seeds \n " , found ) ;
2011-03-09 04:40:50 +01:00
}
2010-08-29 18:58:15 +02:00
2011-11-21 18:25:00 +01:00
2012-01-04 23:39:45 +01:00
void DumpAddresses ( )
{
2012-05-17 04:11:19 +02:00
int64 nStart = GetTimeMillis ( ) ;
2012-01-04 23:39:45 +01:00
CAddrDB adb ;
2012-05-17 04:11:19 +02:00
adb . Write ( addrman ) ;
2013-09-18 12:38:08 +02:00
LogPrint ( " net " , " Flushed %d addresses to peers.dat % " PRI64d " ms \n " ,
2012-05-17 04:11:19 +02:00
addrman . size ( ) , GetTimeMillis ( ) - nStart ) ;
2012-01-04 23:39:45 +01:00
}
2010-08-29 18:58:15 +02:00
2012-04-24 02:15:00 +02:00
void static ProcessOneShot ( )
{
string strDest ;
{
LOCK ( cs_vOneShots ) ;
if ( vOneShots . empty ( ) )
return ;
strDest = vOneShots . front ( ) ;
vOneShots . pop_front ( ) ;
}
CAddress addr ;
2012-05-10 18:44:07 +02:00
CSemaphoreGrant grant ( * semOutbound , true ) ;
if ( grant ) {
if ( ! OpenNetworkConnection ( addr , & grant , strDest . c_str ( ) , true ) )
AddOneShot ( strDest ) ;
}
2012-04-24 02:15:00 +02:00
}
2013-03-07 04:31:26 +01:00
void ThreadOpenConnections ( )
2010-08-29 18:58:15 +02:00
{
// Connect to specific addresses
2012-08-21 17:32:04 +02:00
if ( mapArgs . count ( " -connect " ) & & mapMultiArgs [ " -connect " ] . size ( ) > 0 )
2010-08-29 18:58:15 +02:00
{
2011-12-21 22:33:19 +01:00
for ( int64 nLoop = 0 ; ; nLoop + + )
2010-08-29 18:58:15 +02:00
{
2012-04-24 02:15:00 +02:00
ProcessOneShot ( ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( string strAddr , mapMultiArgs [ " -connect " ] )
2010-08-29 18:58:15 +02:00
{
2012-04-24 02:15:00 +02:00
CAddress addr ;
2012-05-10 18:44:07 +02:00
OpenNetworkConnection ( addr , NULL , strAddr . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
for ( int i = 0 ; i < 10 & & i < nLoop ; i + + )
{
2013-03-07 20:25:21 +01:00
MilliSleep ( 500 ) ;
2010-08-29 18:58:15 +02:00
}
}
2013-03-07 20:25:21 +01:00
MilliSleep ( 500 ) ;
2010-08-29 18:58:15 +02:00
}
}
// Initiate network connections
2011-12-21 22:33:19 +01:00
int64 nStart = GetTime ( ) ;
2013-07-31 06:06:44 +02:00
while ( true )
2010-08-29 18:58:15 +02:00
{
2012-04-24 02:15:00 +02:00
ProcessOneShot ( ) ;
2013-03-07 20:25:21 +01:00
MilliSleep ( 500 ) ;
2012-02-15 21:17:15 +01:00
2012-05-10 18:44:07 +02:00
CSemaphoreGrant grant ( * semOutbound ) ;
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
2013-05-07 15:16:25 +02:00
// Add seed nodes if DNS seeds are all down (an infrastructure attack?).
if ( addrman . size ( ) = = 0 & & ( GetTime ( ) - nStart > 60 ) ) {
static bool done = false ;
if ( ! done ) {
2013-09-18 12:38:08 +02:00
LogPrintf ( " Adding fixed seed nodes as DNS doesn't seem to be available. \n " ) ;
2013-05-07 15:16:25 +02:00
addrman . Add ( Params ( ) . FixedSeeds ( ) , CNetAddr ( " 127.0.0.1 " ) ) ;
done = true ;
2010-08-29 18:58:15 +02:00
}
}
//
// Choose an address to connect to based on most recently seen
//
CAddress addrConnect ;
2012-07-02 02:23:26 +02:00
// Only connect out to one peer per network group (/16 for IPv4).
2010-08-29 18:58:15 +02:00
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
2012-05-10 18:44:07 +02:00
int nOutbound = 0 ;
2012-01-03 23:33:31 +01:00
set < vector < unsigned char > > setConnected ;
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
2012-05-10 18:44:07 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes ) {
2012-07-02 02:23:26 +02:00
if ( ! pnode - > fInbound ) {
setConnected . insert ( pnode - > addr . GetGroup ( ) ) ;
2012-05-10 18:44:07 +02:00
nOutbound + + ;
2012-07-02 02:23:26 +02:00
}
2012-05-10 18:44:07 +02:00
}
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
2011-12-21 22:33:19 +01:00
int64 nANow = GetAdjustedTime ( ) ;
2011-10-04 05:41:47 +02:00
2012-01-04 23:39:45 +01:00
int nTries = 0 ;
2013-07-31 06:06:44 +02:00
while ( true )
2010-08-29 18:58:15 +02:00
{
2012-01-04 23:39:45 +01:00
// use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections)
CAddress addr = addrman . Select ( 10 + min ( nOutbound , 8 ) * 10 ) ;
2010-08-29 18:58:15 +02:00
2012-01-04 23:39:45 +01:00
// if we selected an invalid address, restart
2012-03-31 17:58:25 +02:00
if ( ! addr . IsValid ( ) | | setConnected . count ( addr . GetGroup ( ) ) | | IsLocal ( addr ) )
2012-01-04 23:39:45 +01:00
break ;
2010-08-29 18:58:15 +02:00
2012-08-21 17:32:04 +02:00
// If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
// stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
// already-connected network ranges, ...) before trying new addrman addresses.
2012-01-04 23:39:45 +01:00
nTries + + ;
2012-08-21 17:32:04 +02:00
if ( nTries > 100 )
break ;
2010-08-29 18:58:15 +02:00
2012-05-04 16:46:22 +02:00
if ( IsLimited ( addr ) )
continue ;
2012-01-04 23:39:45 +01:00
// only consider very recently tried nodes after 30 failed attempts
if ( nANow - addr . nLastTry < 600 & & nTries < 30 )
continue ;
// do not allow non-default ports, unless after 50 invalid addresses selected already
2013-05-07 15:16:25 +02:00
if ( addr . GetPort ( ) ! = Params ( ) . GetDefaultPort ( ) & & nTries < 50 )
2012-01-04 23:39:45 +01:00
continue ;
addrConnect = addr ;
break ;
2010-08-29 18:58:15 +02:00
}
if ( addrConnect . IsValid ( ) )
2012-05-10 18:44:07 +02:00
OpenNetworkConnection ( addrConnect , & grant ) ;
2010-08-29 18:58:15 +02:00
}
}
2013-03-07 04:31:26 +01:00
void ThreadOpenAddedConnections ( )
2011-12-17 01:48:03 +01:00
{
2012-07-02 19:55:16 +02:00
{
LOCK ( cs_vAddedNodes ) ;
vAddedNodes = mapMultiArgs [ " -addnode " ] ;
}
2011-12-17 01:48:03 +01:00
2012-09-23 12:55:05 +02:00
if ( HaveNameProxy ( ) ) {
2013-03-07 04:31:26 +01:00
while ( true ) {
2012-07-02 19:55:16 +02:00
list < string > lAddresses ( 0 ) ;
{
LOCK ( cs_vAddedNodes ) ;
BOOST_FOREACH ( string & strAddNode , vAddedNodes )
lAddresses . push_back ( strAddNode ) ;
}
BOOST_FOREACH ( string & strAddNode , lAddresses ) {
2012-04-19 17:38:03 +02:00
CAddress addr ;
2012-05-10 18:44:07 +02:00
CSemaphoreGrant grant ( * semOutbound ) ;
OpenNetworkConnection ( addr , & grant , strAddNode . c_str ( ) ) ;
2013-03-07 20:25:21 +01:00
MilliSleep ( 500 ) ;
2012-04-19 17:38:03 +02:00
}
2013-03-07 20:25:21 +01:00
MilliSleep ( 120000 ) ; // Retry every 2 minutes
2012-04-19 17:38:03 +02:00
}
}
2012-07-02 21:11:57 +02:00
for ( unsigned int i = 0 ; true ; i + + )
2011-12-17 01:48:03 +01:00
{
2012-07-02 19:55:16 +02:00
list < string > lAddresses ( 0 ) ;
{
LOCK ( cs_vAddedNodes ) ;
BOOST_FOREACH ( string & strAddNode , vAddedNodes )
lAddresses . push_back ( strAddNode ) ;
}
list < vector < CService > > lservAddressesToAdd ( 0 ) ;
BOOST_FOREACH ( string & strAddNode , lAddresses )
2011-12-17 01:48:03 +01:00
{
2012-07-02 19:55:16 +02:00
vector < CService > vservNode ( 0 ) ;
2013-05-07 15:16:25 +02:00
if ( Lookup ( strAddNode . c_str ( ) , vservNode , Params ( ) . GetDefaultPort ( ) , fNameLookup , 0 ) )
2012-04-06 18:39:12 +02:00
{
2012-07-02 19:55:16 +02:00
lservAddressesToAdd . push_back ( vservNode ) ;
{
LOCK ( cs_setservAddNodeAddresses ) ;
BOOST_FOREACH ( CService & serv , vservNode )
setservAddNodeAddresses . insert ( serv ) ;
}
2012-04-06 18:39:12 +02:00
}
2011-12-17 01:48:03 +01:00
}
// Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry
2012-04-19 17:38:03 +02:00
// (keeping in mind that addnode entries can have many IPs if fNameLookup)
2012-04-06 18:39:12 +02:00
{
LOCK ( cs_vNodes ) ;
2011-12-17 01:48:03 +01:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2012-07-02 19:55:16 +02:00
for ( list < vector < CService > > : : iterator it = lservAddressesToAdd . begin ( ) ; it ! = lservAddressesToAdd . end ( ) ; it + + )
2011-12-17 01:48:03 +01:00
BOOST_FOREACH ( CService & addrNode , * ( it ) )
if ( pnode - > addr = = addrNode )
{
2012-07-02 19:55:16 +02:00
it = lservAddressesToAdd . erase ( it ) ;
2011-12-17 01:48:03 +01:00
it - - ;
break ;
}
2012-04-06 18:39:12 +02:00
}
2012-07-02 19:55:16 +02:00
BOOST_FOREACH ( vector < CService > & vserv , lservAddressesToAdd )
2011-12-17 01:48:03 +01:00
{
2012-05-10 18:44:07 +02:00
CSemaphoreGrant grant ( * semOutbound ) ;
2012-07-02 21:11:57 +02:00
OpenNetworkConnection ( CAddress ( vserv [ i % vserv . size ( ) ] ) , & grant ) ;
2013-03-07 20:25:21 +01:00
MilliSleep ( 500 ) ;
2011-12-17 01:48:03 +01:00
}
2013-03-07 20:25:21 +01:00
MilliSleep ( 120000 ) ; // Retry every 2 minutes
2011-12-17 01:48:03 +01:00
}
}
2012-07-26 02:48:39 +02:00
// if successful, this moves the passed grant to the constructed node
2012-05-10 18:44:07 +02:00
bool OpenNetworkConnection ( const CAddress & addrConnect , CSemaphoreGrant * grantOutbound , const char * strDest , bool fOneShot )
2010-08-29 18:58:15 +02:00
{
//
// Initiate outbound network connection
//
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2012-04-19 17:38:03 +02:00
if ( ! strDest )
2012-02-12 13:45:24 +01:00
if ( IsLocal ( addrConnect ) | |
FindNode ( ( CNetAddr ) addrConnect ) | | CNode : : IsBanned ( addrConnect ) | |
2012-04-19 17:38:03 +02:00
FindNode ( addrConnect . ToStringIPPort ( ) . c_str ( ) ) )
return false ;
if ( strDest & & FindNode ( strDest ) )
2010-08-29 18:58:15 +02:00
return false ;
2012-04-19 17:38:03 +02:00
CNode * pnode = ConnectNode ( addrConnect , strDest ) ;
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
if ( ! pnode )
return false ;
2012-05-10 18:44:07 +02:00
if ( grantOutbound )
grantOutbound - > MoveTo ( pnode - > grantOutbound ) ;
2010-08-29 18:58:15 +02:00
pnode - > fNetworkNode = true ;
2012-04-24 02:15:00 +02:00
if ( fOneShot )
pnode - > fOneShot = true ;
2010-08-29 18:58:15 +02:00
return true ;
}
2013-04-05 00:43:04 +02:00
// for now, use a very simple selection metric: the node from which we received
// most recently
double static NodeSyncScore ( const CNode * pnode ) {
return - pnode - > nLastRecv ;
}
2010-08-29 18:58:15 +02:00
2013-04-05 00:43:04 +02:00
void static StartSync ( const vector < CNode * > & vNodes ) {
CNode * pnodeNewSync = NULL ;
double dBestScore = 0 ;
2010-08-29 18:58:15 +02:00
2013-04-05 00:43:04 +02:00
// Iterate over all nodes
BOOST_FOREACH ( CNode * pnode , vNodes ) {
// check preconditions for allowing a sync
if ( ! pnode - > fClient & & ! pnode - > fOneShot & &
! pnode - > fDisconnect & & pnode - > fSuccessfullyConnected & &
( pnode - > nStartingHeight > ( nBestHeight - 144 ) ) & &
( pnode - > nVersion < NOBLKS_VERSION_START | | pnode - > nVersion > = NOBLKS_VERSION_END ) ) {
// if ok, compare node's score with the best so far
double dScore = NodeSyncScore ( pnode ) ;
if ( pnodeNewSync = = NULL | | dScore > dBestScore ) {
pnodeNewSync = pnode ;
dBestScore = dScore ;
}
}
}
// if a new sync candidate was found, start sync!
if ( pnodeNewSync ) {
pnodeNewSync - > fStartSync = true ;
pnodeSync = pnodeNewSync ;
}
}
2010-08-29 18:58:15 +02:00
2013-03-07 04:31:26 +01:00
void ThreadMessageHandler ( )
2010-08-29 18:58:15 +02:00
{
SetThreadPriority ( THREAD_PRIORITY_BELOW_NORMAL ) ;
2013-03-07 04:31:26 +01:00
while ( true )
2010-08-29 18:58:15 +02:00
{
2013-04-05 00:43:04 +02:00
bool fHaveSyncNode = false ;
2010-08-29 18:58:15 +02:00
vector < CNode * > vNodesCopy ;
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2010-08-29 18:58:15 +02:00
vNodesCopy = vNodes ;
2013-04-05 00:43:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy ) {
2010-08-29 18:58:15 +02:00
pnode - > AddRef ( ) ;
2013-04-05 00:43:04 +02:00
if ( pnode = = pnodeSync )
fHaveSyncNode = true ;
}
2010-08-29 18:58:15 +02:00
}
2013-04-05 00:43:04 +02:00
if ( ! fHaveSyncNode )
StartSync ( vNodesCopy ) ;
2010-08-29 18:58:15 +02:00
// Poll the connected nodes for messages
CNode * pnodeTrickle = NULL ;
if ( ! vNodesCopy . empty ( ) )
pnodeTrickle = vNodesCopy [ GetRand ( vNodesCopy . size ( ) ) ] ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
{
2013-03-01 01:41:28 +01:00
if ( pnode - > fDisconnect )
continue ;
2010-08-29 18:58:15 +02:00
// Receive messages
2012-04-06 18:39:12 +02:00
{
2012-11-16 01:41:12 +01:00
TRY_LOCK ( pnode - > cs_vRecvMsg , lockRecv ) ;
2012-04-06 18:39:12 +02:00
if ( lockRecv )
2013-06-06 05:21:41 +02:00
if ( ! g_signals . ProcessMessages ( pnode ) )
2012-11-16 01:41:12 +01:00
pnode - > CloseSocketDisconnect ( ) ;
2012-04-06 18:39:12 +02:00
}
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
// Send messages
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
2013-06-06 05:21:41 +02:00
if ( lockSend )
g_signals . SendMessages ( pnode , pnode = = pnodeTrickle ) ;
2012-04-06 18:39:12 +02:00
}
2013-03-07 04:31:26 +01:00
boost : : this_thread : : interruption_point ( ) ;
2010-08-29 18:58:15 +02:00
}
{
2012-04-06 18:39:12 +02:00
LOCK ( cs_vNodes ) ;
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodesCopy )
2010-08-29 18:58:15 +02:00
pnode - > Release ( ) ;
}
2013-03-07 20:25:21 +01:00
MilliSleep ( 100 ) ;
2010-08-29 18:58:15 +02:00
}
}
2012-05-11 15:28:59 +02:00
bool BindListenPort ( const CService & addrBind , string & strError )
2010-08-29 18:58:15 +02:00
{
strError = " " ;
int nOne = 1 ;
// Create socket for listening for incoming connections
2012-03-31 17:58:25 +02:00
# ifdef USE_IPV6
2012-05-11 15:28:59 +02:00
struct sockaddr_storage sockaddr ;
2012-03-31 17:58:25 +02:00
# else
2012-05-11 15:28:59 +02:00
struct sockaddr sockaddr ;
2012-03-31 17:58:25 +02:00
# endif
2012-05-11 15:28:59 +02:00
socklen_t len = sizeof ( sockaddr ) ;
if ( ! addrBind . GetSockAddr ( ( struct sockaddr * ) & sockaddr , & len ) )
{
strError = strprintf ( " Error: bind address family for %s not supported " , addrBind . ToString ( ) . c_str ( ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %s \n " , strError . c_str ( ) ) ;
2012-05-11 15:28:59 +02:00
return false ;
}
SOCKET hListenSocket = socket ( ( ( struct sockaddr * ) & sockaddr ) - > sa_family , SOCK_STREAM , IPPROTO_TCP ) ;
2010-08-29 18:58:15 +02:00
if ( hListenSocket = = INVALID_SOCKET )
{
strError = strprintf ( " Error: Couldn't open socket for incoming connections (socket returned error %d) " , WSAGetLastError ( ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %s \n " , strError . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
return false ;
}
2011-08-07 18:18:05 +02:00
# ifdef SO_NOSIGPIPE
2010-08-29 18:58:15 +02:00
// Different way of disabling SIGPIPE on BSD
setsockopt ( hListenSocket , SOL_SOCKET , SO_NOSIGPIPE , ( void * ) & nOne , sizeof ( int ) ) ;
# endif
2011-10-07 17:02:21 +02:00
# ifndef WIN32
2010-08-29 18:58:15 +02:00
// Allow binding if the port is still in TIME_WAIT state after
// the program was closed and restarted. Not an issue on windows.
setsockopt ( hListenSocket , SOL_SOCKET , SO_REUSEADDR , ( void * ) & nOne , sizeof ( int ) ) ;
# endif
2012-05-11 15:28:59 +02:00
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2012-07-26 02:48:39 +02:00
// Set to non-blocking, incoming connections will also inherit this
2010-08-29 18:58:15 +02:00
if ( ioctlsocket ( hListenSocket , FIONBIO , ( u_long * ) & nOne ) = = SOCKET_ERROR )
# else
if ( fcntl ( hListenSocket , F_SETFL , O_NONBLOCK ) = = SOCKET_ERROR )
# endif
{
strError = strprintf ( " Error: Couldn't set properties on socket for incoming connections (error %d) " , WSAGetLastError ( ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %s \n " , strError . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
return false ;
}
2012-03-31 17:58:25 +02:00
# ifdef USE_IPV6
2012-05-11 15:28:59 +02:00
// some systems don't have IPV6_V6ONLY but are always v6only; others do have the option
// and enable it by default or not. Try to enable it, if possible.
if ( addrBind . IsIPv6 ( ) ) {
# ifdef IPV6_V6ONLY
2013-07-13 13:05:04 +02:00
# ifdef WIN32
setsockopt ( hListenSocket , IPPROTO_IPV6 , IPV6_V6ONLY , ( const char * ) & nOne , sizeof ( int ) ) ;
# else
2012-05-11 15:28:59 +02:00
setsockopt ( hListenSocket , IPPROTO_IPV6 , IPV6_V6ONLY , ( void * ) & nOne , sizeof ( int ) ) ;
2012-03-31 17:58:25 +02:00
# endif
2013-07-13 13:05:04 +02:00
# endif
2012-05-11 15:28:59 +02:00
# ifdef WIN32
int nProtLevel = 10 /* PROTECTION_LEVEL_UNRESTRICTED */ ;
int nParameterId = 23 /* IPV6_PROTECTION_LEVEl */ ;
// this call is allowed to fail
setsockopt ( hListenSocket , IPPROTO_IPV6 , nParameterId , ( const char * ) & nProtLevel , sizeof ( int ) ) ;
# endif
}
# endif
if ( : : bind ( hListenSocket , ( struct sockaddr * ) & sockaddr , len ) = = SOCKET_ERROR )
2010-08-29 18:58:15 +02:00
{
int nErr = WSAGetLastError ( ) ;
if ( nErr = = WSAEADDRINUSE )
2012-05-16 15:34:29 +02:00
strError = strprintf ( _ ( " Unable to bind to %s on this computer. Bitcoin is probably already running. " ) , addrBind . ToString ( ) . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
else
2012-05-11 15:28:59 +02:00
strError = strprintf ( _ ( " Unable to bind to %s on this computer (bind returned error %d, %s) " ) , addrBind . ToString ( ) . c_str ( ) , nErr , strerror ( nErr ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %s \n " , strError . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
return false ;
}
2013-09-18 12:38:08 +02:00
LogPrintf ( " Bound to %s \n " , addrBind . ToString ( ) . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
// Listen for incoming connections
if ( listen ( hListenSocket , SOMAXCONN ) = = SOCKET_ERROR )
{
strError = strprintf ( " Error: Listening for incoming connections failed (listen returned error %d) " , WSAGetLastError ( ) ) ;
2013-09-18 12:38:08 +02:00
LogPrintf ( " %s \n " , strError . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
return false ;
}
2012-05-11 15:28:59 +02:00
vhListenSocket . push_back ( hListenSocket ) ;
2012-05-24 19:02:21 +02:00
if ( addrBind . IsRoutable ( ) & & fDiscover )
2012-05-11 15:28:59 +02:00
AddLocal ( addrBind , LOCAL_BIND ) ;
2010-08-29 18:58:15 +02:00
return true ;
}
2012-02-19 20:44:35 +01:00
void static Discover ( )
2010-08-29 18:58:15 +02:00
{
2012-05-24 19:02:21 +02:00
if ( ! fDiscover )
2012-02-19 20:44:35 +01:00
return ;
2010-08-29 18:58:15 +02:00
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2012-07-26 02:48:39 +02:00
// Get local host IP
2010-08-29 18:58:15 +02:00
char pszHostName [ 1000 ] = " " ;
if ( gethostname ( pszHostName , sizeof ( pszHostName ) ) ! = SOCKET_ERROR )
{
2012-01-03 23:33:31 +01:00
vector < CNetAddr > vaddr ;
if ( LookupHost ( pszHostName , vaddr ) )
2012-05-01 01:44:59 +02:00
{
2012-01-03 23:33:31 +01:00
BOOST_FOREACH ( const CNetAddr & addr , vaddr )
2012-05-01 01:44:59 +02:00
{
2012-02-12 13:45:24 +01:00
AddLocal ( addr , LOCAL_IF ) ;
2012-05-01 01:44:59 +02:00
}
}
2010-08-29 18:58:15 +02:00
}
# else
// Get local host ip
struct ifaddrs * myaddrs ;
if ( getifaddrs ( & myaddrs ) = = 0 )
{
for ( struct ifaddrs * ifa = myaddrs ; ifa ! = NULL ; ifa = ifa - > ifa_next )
{
if ( ifa - > ifa_addr = = NULL ) continue ;
if ( ( ifa - > ifa_flags & IFF_UP ) = = 0 ) continue ;
if ( strcmp ( ifa - > ifa_name , " lo " ) = = 0 ) continue ;
if ( strcmp ( ifa - > ifa_name , " lo0 " ) = = 0 ) continue ;
if ( ifa - > ifa_addr - > sa_family = = AF_INET )
{
struct sockaddr_in * s4 = ( struct sockaddr_in * ) ( ifa - > ifa_addr ) ;
2012-02-12 13:45:24 +01:00
CNetAddr addr ( s4 - > sin_addr ) ;
2012-03-31 17:58:25 +02:00
if ( AddLocal ( addr , LOCAL_IF ) )
2013-09-18 12:38:08 +02:00
LogPrintf ( " IPv4 %s: %s \n " , ifa - > ifa_name , addr . ToString ( ) . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
}
2012-03-31 17:58:25 +02:00
# ifdef USE_IPV6
2010-08-29 18:58:15 +02:00
else if ( ifa - > ifa_addr - > sa_family = = AF_INET6 )
{
struct sockaddr_in6 * s6 = ( struct sockaddr_in6 * ) ( ifa - > ifa_addr ) ;
2012-02-12 13:45:24 +01:00
CNetAddr addr ( s6 - > sin6_addr ) ;
2012-03-31 17:58:25 +02:00
if ( AddLocal ( addr , LOCAL_IF ) )
2013-09-18 12:38:08 +02:00
LogPrintf ( " IPv6 %s: %s \n " , ifa - > ifa_name , addr . ToString ( ) . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
}
2012-03-31 17:58:25 +02:00
# endif
2010-08-29 18:58:15 +02:00
}
freeifaddrs ( myaddrs ) ;
}
# endif
2012-07-20 12:20:37 +02:00
// Don't use external IPv4 discovery, when -onlynet="IPv6"
if ( ! IsLimited ( NET_IPV4 ) )
2013-04-23 11:36:54 +02:00
boost : : thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " ext-ip " , & ThreadGetMyExternalIP ) ) ;
2012-02-19 20:44:35 +01:00
}
2013-03-09 18:02:57 +01:00
void StartNode ( boost : : thread_group & threadGroup )
2012-02-19 20:44:35 +01:00
{
2012-05-10 18:44:07 +02:00
if ( semOutbound = = NULL ) {
// initialize semaphore
2013-04-26 00:46:47 +02:00
int nMaxOutbound = min ( MAX_OUTBOUND_CONNECTIONS , nMaxConnections ) ;
2012-05-10 18:44:07 +02:00
semOutbound = new CSemaphore ( nMaxOutbound ) ;
}
2012-02-19 20:44:35 +01:00
if ( pnodeLocalHost = = NULL )
pnodeLocalHost = new CNode ( INVALID_SOCKET , CAddress ( CService ( " 127.0.0.1 " , 0 ) , nLocalServices ) ) ;
Discover ( ) ;
2010-08-29 18:58:15 +02:00
//
// Start threads
//
2012-02-06 20:35:57 +01:00
if ( ! GetBoolArg ( " -dnsseed " , true ) )
2013-09-18 12:38:08 +02:00
LogPrintf ( " DNS seeding disabled \n " ) ;
2011-11-21 18:25:00 +01:00
else
2013-04-23 11:36:54 +02:00
threadGroup . create_thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " dnsseed " , & ThreadDNSAddressSeed ) ) ;
2011-11-21 18:25:00 +01:00
2013-03-09 18:02:57 +01:00
# ifdef USE_UPNP
2011-03-26 13:01:27 +01:00
// Map ports with UPnP
2013-03-07 04:31:26 +01:00
MapPort ( GetBoolArg ( " -upnp " , USE_UPNP ) ) ;
2013-03-09 18:02:57 +01:00
# endif
2011-03-26 13:01:27 +01:00
2010-08-29 18:58:15 +02:00
// Send and receive from sockets, accept connections
2013-03-09 18:02:57 +01:00
threadGroup . create_thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " net " , & ThreadSocketHandler ) ) ;
2010-08-29 18:58:15 +02:00
2011-12-17 01:48:03 +01:00
// Initiate outbound connections from -addnode
2013-03-09 18:02:57 +01:00
threadGroup . create_thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " addcon " , & ThreadOpenAddedConnections ) ) ;
2011-12-17 01:48:03 +01:00
2010-08-29 18:58:15 +02:00
// Initiate outbound connections
2013-03-09 18:02:57 +01:00
threadGroup . create_thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " opencon " , & ThreadOpenConnections ) ) ;
2010-08-29 18:58:15 +02:00
// Process messages
2013-03-09 18:02:57 +01:00
threadGroup . create_thread ( boost : : bind ( & TraceThread < void ( * ) ( ) > , " msghand " , & ThreadMessageHandler ) ) ;
2010-08-29 18:58:15 +02:00
2012-01-04 23:39:45 +01:00
// Dump network addresses
2013-06-24 00:23:28 +02:00
threadGroup . create_thread ( boost : : bind ( & LoopForever < void ( * ) ( ) > , " dumpaddr " , & DumpAddresses , DUMP_ADDRESSES_INTERVAL * 1000 ) ) ;
2010-08-29 18:58:15 +02:00
}
bool StopNode ( )
{
2013-09-18 12:38:08 +02:00
LogPrintf ( " StopNode() \n " ) ;
2013-03-07 04:31:26 +01:00
MapPort ( false ) ;
2012-05-12 17:44:14 +02:00
if ( semOutbound )
for ( int i = 0 ; i < MAX_OUTBOUND_CONNECTIONS ; i + + )
semOutbound - > post ( ) ;
2013-03-07 20:25:21 +01:00
MilliSleep ( 50 ) ;
2012-01-04 23:39:45 +01:00
DumpAddresses ( ) ;
2013-03-29 02:17:10 +01:00
2010-08-29 18:58:15 +02:00
return true ;
}
class CNetCleanup
{
public :
CNetCleanup ( )
{
}
~ CNetCleanup ( )
{
// Close sockets
2011-05-15 09:11:04 +02:00
BOOST_FOREACH ( CNode * pnode , vNodes )
2010-08-29 18:58:15 +02:00
if ( pnode - > hSocket ! = INVALID_SOCKET )
closesocket ( pnode - > hSocket ) ;
2012-05-11 15:28:59 +02:00
BOOST_FOREACH ( SOCKET hListenSocket , vhListenSocket )
if ( hListenSocket ! = INVALID_SOCKET )
if ( closesocket ( hListenSocket ) = = SOCKET_ERROR )
2013-09-18 12:38:08 +02:00
LogPrintf ( " closesocket(hListenSocket) failed with error %d \n " , WSAGetLastError ( ) ) ;
2010-08-29 18:58:15 +02:00
2013-03-29 02:17:10 +01:00
// clean up some globals (to help leak detection)
BOOST_FOREACH ( CNode * pnode , vNodes )
delete pnode ;
BOOST_FOREACH ( CNode * pnode , vNodesDisconnected )
delete pnode ;
vNodes . clear ( ) ;
vNodesDisconnected . clear ( ) ;
delete semOutbound ;
semOutbound = NULL ;
delete pnodeLocalHost ;
pnodeLocalHost = NULL ;
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2010-08-29 18:58:15 +02:00
// Shutdown Windows Sockets
WSACleanup ( ) ;
# endif
}
}
instance_of_cnetcleanup ;
2012-08-13 05:26:30 +02:00
void RelayTransaction ( const CTransaction & tx , const uint256 & hash )
{
CDataStream ss ( SER_NETWORK , PROTOCOL_VERSION ) ;
ss . reserve ( 10000 ) ;
ss < < tx ;
RelayTransaction ( tx , hash , ss ) ;
}
void RelayTransaction ( const CTransaction & tx , const uint256 & hash , const CDataStream & ss )
{
CInv inv ( MSG_TX , hash ) ;
{
LOCK ( cs_mapRelay ) ;
// Expire old relay messages
while ( ! vRelayExpiration . empty ( ) & & vRelayExpiration . front ( ) . first < GetTime ( ) )
{
mapRelay . erase ( vRelayExpiration . front ( ) . second ) ;
vRelayExpiration . pop_front ( ) ;
}
// Save original serialized message so newer versions are preserved
mapRelay . insert ( std : : make_pair ( inv , ss ) ) ;
vRelayExpiration . push_back ( std : : make_pair ( GetTime ( ) + 15 * 60 , inv ) ) ;
}
LOCK ( cs_vNodes ) ;
BOOST_FOREACH ( CNode * pnode , vNodes )
{
2012-08-21 03:10:25 +02:00
if ( ! pnode - > fRelayTxes )
continue ;
2012-08-13 05:26:30 +02:00
LOCK ( pnode - > cs_filter ) ;
if ( pnode - > pfilter )
{
2012-08-19 05:38:28 +02:00
if ( pnode - > pfilter - > IsRelevantAndUpdate ( tx , hash ) )
2012-08-13 05:26:30 +02:00
pnode - > PushInventory ( inv ) ;
} else
pnode - > PushInventory ( inv ) ;
}
}