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
2011-05-14 21:30:15 +02:00
# include "irc.h"
2011-05-14 22:57:34 +02:00
# include "db.h"
2011-05-14 23:20:30 +02:00
# include "net.h"
2011-05-15 23:52:31 +02:00
# include "init.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
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
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 ;
void ThreadMessageHandler2 ( void * parg ) ;
void ThreadSocketHandler2 ( void * parg ) ;
void ThreadOpenConnections2 ( void * parg ) ;
2011-12-17 01:48:03 +01:00
void ThreadOpenAddedConnections2 ( void * parg ) ;
2011-03-26 13:01:27 +01:00
# ifdef USE_UPNP
void ThreadMapPort2 ( void * parg ) ;
# endif
2011-11-21 18:25:00 +01:00
void ThreadDNSAddressSeed2 ( void * parg ) ;
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 ;
bool fUseUPnP = false ;
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 ;
2011-12-21 22:33:19 +01:00
uint64 nLocalHostNonce = 0 ;
2012-02-07 19:42:29 +01:00
array < int , THREAD_MAX > vnThreadsRunning ;
2012-05-11 15:28:59 +02:00
static std : : vector < SOCKET > vhListenSocket ;
2012-01-04 23:39:45 +01:00
CAddrMan addrman ;
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 ;
2011-12-21 22:33:19 +01:00
map < CInv , int64 > mapAlreadyAskedFor ;
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
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 ( )
{
return ( unsigned short ) ( GetArg ( " -port " , GetDefaultPort ( ) ) ) ;
}
2010-08-29 18:58:15 +02:00
void CNode : : PushGetBlocks ( CBlockIndex * pindexBegin , uint256 hashEnd )
{
// Filter out duplicate requests
if ( pindexBegin = = pindexLastGetBlocksBegin & & hashEnd = = hashLastGetBlocksEnd )
return ;
pindexLastGetBlocksBegin = pindexBegin ;
hashLastGetBlocksEnd = hashEnd ;
PushMessage ( " getblocks " , CBlockLocator ( pindexBegin ) , hashEnd ) ;
}
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 = " " ;
loop
{
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 )
{
if ( fShutdown )
return false ;
if ( nBytes < 0 )
{
int nErr = WSAGetLastError ( ) ;
if ( nErr = = WSAEMSGSIZE )
continue ;
if ( nErr = = WSAEWOULDBLOCK | | nErr = = WSAEINTR | | nErr = = WSAEINPROGRESS )
{
Sleep ( 10 ) ;
continue ;
}
}
if ( ! strLine . empty ( ) )
return true ;
if ( nBytes = = 0 )
{
// socket closed
printf ( " socket closed \n " ) ;
return false ;
}
else
{
// socket error
int nErr = WSAGetLastError ( ) ;
printf ( " recv failed: %d \n " , nErr ) ;
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 ;
2012-02-12 13:45:24 +01:00
printf ( " AddLocal(%s,%i) \n " , addr . ToString ( ) . c_str ( ) , nScore ) ;
{
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
{
loop
{
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 ) ;
2010-08-29 18:58:15 +02:00
printf ( " 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 " ) ;
}
2010-12-15 23:43:51 +01:00
// We now get our external IP from the IRC server first and only use this as a backup
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 + + )
{
2010-12-15 23:43:51 +01:00
// We should be phasing out our use of sites like these. If we need
// 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 )
{
2012-01-17 02:12:35 +01: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 ;
}
2010-12-15 23:43:51 +01:00
void ThreadGetMyExternalIP ( void * parg )
{
2012-07-17 09:53:53 +02:00
// Make this thread recognisable as the external IP detection thread
2012-06-24 17:03:57 +02:00
RenameThread ( " bitcoin-ext-ip " ) ;
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
{
printf ( " 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
{
{
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-01-03 23:33:31 +01:00
if ( ( CNetAddr ) pnode - > addr = = ip )
2010-08-29 18:58:15 +02:00
return ( pnode ) ;
}
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
{
{
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-01-03 23:33:31 +01:00
if ( ( CService ) pnode - > addr = = addr )
2010-08-29 18:58:15 +02:00
return ( pnode ) ;
}
return NULL ;
}
2012-04-19 17:38:03 +02:00
CNode * ConnectNode ( CAddress addrConnect , const char * pszDest , int64 nTimeout )
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 )
{
if ( nTimeout ! = 0 )
pnode - > AddRef ( nTimeout ) ;
else
pnode - > AddRef ( ) ;
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
2012-01-04 23:39:45 +01:00
printf ( " 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 ;
2012-04-19 17:38:03 +02:00
if ( pszDest ? ConnectSocketByName ( addrConnect , hSocket , pszDest , GetDefaultPort ( ) ) : ConnectSocket ( addrConnect , hSocket ) )
2010-08-29 18:58:15 +02:00
{
2012-04-19 17:38:03 +02:00
addrman . Attempt ( addrConnect ) ;
2010-08-29 18:58:15 +02:00
/// debug print
2012-04-19 17:38:03 +02:00
printf ( " 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 )
2012-07-26 02:48:39 +02:00
printf ( " 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 )
2012-07-26 02:48:39 +02:00
printf ( " 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 ) ;
2010-08-29 18:58:15 +02:00
if ( nTimeout ! = 0 )
pnode - > AddRef ( nTimeout ) ;
else
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 )
{
2012-04-19 17:38:03 +02:00
printf ( " disconnecting node %s \n " , addrName . c_str ( ) ) ;
2010-08-29 18:58:15 +02:00
closesocket ( hSocket ) ;
hSocket = INVALID_SOCKET ;
2012-03-18 04:03:24 +01:00
vRecv . clear ( ) ;
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 ) ) ;
2012-05-01 22:03:51 +02:00
printf ( " 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 ,
nLocalHostNonce , FormatSubVersion ( CLIENT_NAME , CLIENT_VERSION , std : : vector < string > ( ) ) , nBestHeight ) ;
}
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 ( ) )
{
2012-07-27 08:36:43 +02:00
printf ( " 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
2012-05-17 23:05:36 +02:00
printf ( " 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
printf ( " 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 ( nReleaseTime ) ;
X ( nStartingHeight ) ;
X ( nMisbehavior ) ;
}
# undef X
2010-08-29 18:58:15 +02:00
void ThreadSocketHandler ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the networking thread
2012-06-30 17:14:28 +02:00
RenameThread ( " bitcoin-net " ) ;
2012-06-24 17:03:57 +02:00
2010-08-29 18:58:15 +02:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] + + ;
2010-08-29 18:58:15 +02:00
ThreadSocketHandler2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
PrintException ( & e , " ThreadSocketHandler() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
throw ; // support pthread_cancel()
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadSocketHandler exited \n " ) ;
2010-08-29 18:58:15 +02:00
}
void ThreadSocketHandler2 ( void * parg )
{
printf ( " ThreadSocketHandler started \n " ) ;
list < CNode * > vNodesDisconnected ;
2012-04-22 20:01:25 +02:00
unsigned int nPrevNodeCount = 0 ;
2010-08-29 18:58:15 +02:00
loop
{
//
// 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 | |
( pnode - > GetRefCount ( ) < = 0 & & pnode - > vRecv . empty ( ) & & pnode - > vSend . empty ( ) ) )
{
// 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
pnode - > nReleaseTime = max ( pnode - > nReleaseTime , GetTime ( ) + 15 * 60 ) ;
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 )
{
TRY_LOCK ( pnode - > cs_vRecv , lockRecv ) ;
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 ;
FD_SET ( pnode - > hSocket , & fdsetRecv ) ;
FD_SET ( pnode - > hSocket , & fdsetError ) ;
hSocketMax = max ( hSocketMax , pnode - > hSocket ) ;
2012-09-05 22:01:28 +02:00
have_fds = true ;
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
if ( lockSend & & ! pnode - > vSend . empty ( ) )
2010-08-29 18:58:15 +02:00
FD_SET ( pnode - > hSocket , & fdsetSend ) ;
2012-04-06 18:39:12 +02:00
}
2010-08-29 18:58:15 +02:00
}
}
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] - - ;
2012-09-05 22:01:28 +02:00
int nSelect = select ( have_fds ? hSocketMax + 1 : 0 ,
& fdsetRecv , & fdsetSend , & fdsetError , & timeout ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_SOCKETHANDLER ] + + ;
2010-08-29 18:58:15 +02:00
if ( fShutdown )
return ;
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 ( ) ;
2011-06-07 00:48:37 +02:00
printf ( " 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 ) ;
Sleep ( timeout . tv_usec / 1000 ) ;
}
//
// 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 ) )
2012-07-27 08:36:43 +02:00
printf ( " 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 )
printf ( " socket error accept failed: %d \n " , nErr ) ;
2010-08-29 18:58:15 +02:00
}
2011-02-16 20:43:04 +01:00
else if ( nInbound > = GetArg ( " -maxconnections " , 125 ) - 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
{
2012-03-05 19:33:24 +01:00
printf ( " 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
{
2011-05-02 15:34:42 +02:00
printf ( " 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
{
if ( fShutdown )
return ;
//
// Receive
//
if ( pnode - > hSocket = = INVALID_SOCKET )
continue ;
if ( FD_ISSET ( pnode - > hSocket , & fdsetRecv ) | | FD_ISSET ( pnode - > hSocket , & fdsetError ) )
{
2012-04-06 18:39:12 +02:00
TRY_LOCK ( pnode - > cs_vRecv , lockRecv ) ;
if ( lockRecv )
2010-08-29 18:58:15 +02:00
{
CDataStream & vRecv = pnode - > vRecv ;
unsigned int nPos = vRecv . size ( ) ;
2011-06-30 23:29:44 +02:00
if ( nPos > ReceiveBufferSize ( ) ) {
2010-08-29 18:58:15 +02:00
if ( ! pnode - > fDisconnect )
2012-09-29 11:57:44 +02:00
printf ( " socket recv flood control disconnect (% " PRIszu " bytes) \n " , vRecv . size ( ) ) ;
2010-08-29 18:58:15 +02:00
pnode - > CloseSocketDisconnect ( ) ;
}
2011-02-16 19:18:11 +01:00
else {
// 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
{
2011-02-16 19:18:11 +01:00
vRecv . resize ( nPos + nBytes ) ;
memcpy ( & vRecv [ nPos ] , pchBuf , nBytes ) ;
pnode - > nLastRecv = GetTime ( ) ;
}
else if ( nBytes = = 0 )
{
// socket closed gracefully
2010-08-29 18:58:15 +02:00
if ( ! pnode - > fDisconnect )
2011-02-16 19:18:11 +01:00
printf ( " 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 )
printf ( " socket recv error %d \n " , nErr ) ;
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 )
2010-08-29 18:58:15 +02:00
{
CDataStream & vSend = pnode - > vSend ;
if ( ! vSend . empty ( ) )
{
int nBytes = send ( pnode - > hSocket , & vSend [ 0 ] , vSend . size ( ) , MSG_NOSIGNAL | MSG_DONTWAIT ) ;
if ( nBytes > 0 )
{
vSend . erase ( vSend . begin ( ) , vSend . begin ( ) + nBytes ) ;
pnode - > nLastSend = GetTime ( ) ;
}
else if ( nBytes < 0 )
{
// error
int nErr = WSAGetLastError ( ) ;
if ( nErr ! = WSAEWOULDBLOCK & & nErr ! = WSAEMSGSIZE & & nErr ! = WSAEINTR & & nErr ! = WSAEINPROGRESS )
{
printf ( " socket send error %d \n " , nErr ) ;
pnode - > CloseSocketDisconnect ( ) ;
}
}
}
}
}
//
// Inactivity checking
//
if ( pnode - > vSend . empty ( ) )
pnode - > nLastSendEmpty = GetTime ( ) ;
if ( GetTime ( ) - pnode - > nTimeConnected > 60 )
{
if ( pnode - > nLastRecv = = 0 | | pnode - > nLastSend = = 0 )
{
printf ( " socket no message in first 60 seconds, %d %d \n " , pnode - > nLastRecv ! = 0 , pnode - > nLastSend ! = 0 ) ;
pnode - > fDisconnect = true ;
}
else if ( GetTime ( ) - pnode - > nLastSend > 90 * 60 & & GetTime ( ) - pnode - > nLastSendEmpty > 90 * 60 )
{
printf ( " socket not sending \n " ) ;
pnode - > fDisconnect = true ;
}
else if ( GetTime ( ) - pnode - > nLastRecv > 90 * 60 )
{
printf ( " socket inactivity timeout \n " ) ;
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 ( ) ;
}
Sleep ( 10 ) ;
}
}
2011-03-26 13:01:27 +01:00
# ifdef USE_UPNP
void ThreadMapPort ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the UPnP thread
2012-06-30 17:14:28 +02:00
RenameThread ( " bitcoin-UPnP " ) ;
2012-06-24 17:03:57 +02:00
2011-03-26 13:01:27 +01:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_UPNP ] + + ;
2011-03-26 13:01:27 +01:00
ThreadMapPort2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_UPNP ] - - ;
2011-03-26 13:01:27 +01:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_UPNP ] - - ;
2011-03-26 13:01:27 +01:00
PrintException ( & e , " ThreadMapPort() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_UPNP ] - - ;
2011-03-26 13:01:27 +01:00
PrintException ( NULL , " ThreadMapPort() " ) ;
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadMapPort exited \n " ) ;
2011-03-26 13:01:27 +01:00
}
void ThreadMapPort2 ( void * parg )
{
printf ( " ThreadMapPort started \n " ) ;
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 )
printf ( " UPnP: GetExternalIPAddress() returned %d \n " , r ) ;
else
{
if ( externalIPAddress [ 0 ] )
{
printf ( " 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
printf ( " UPnP: GetExternalIPAddress failed. \n " ) ;
}
}
2011-08-01 16:34:59 +02:00
string strDesc = " Bitcoin " + FormatFullVersion ( ) ;
2011-12-10 17:52:50 +01:00
# ifndef UPNPDISCOVER_SUCCESS
2012-01-31 23:36:25 +01:00
/* miniupnpc 1.5 */
2011-12-10 17:52:50 +01:00
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 ) ;
2011-12-10 17:52:50 +01:00
# else
2012-01-31 23:36:25 +01:00
/* miniupnpc 1.6 */
2011-03-26 13:01:27 +01:00
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 " ) ;
2011-12-10 17:52:50 +01:00
# endif
2011-08-12 00:20:07 +02:00
2011-03-26 13:01:27 +01:00
if ( r ! = UPNPCOMMAND_SUCCESS )
printf ( " 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 ) ) ;
2011-03-26 13:01:27 +01:00
else
printf ( " UPnP Port Mapping successful. \n " ) ;
2012-01-31 23:36:25 +01:00
int i = 1 ;
2011-03-26 13:01:27 +01:00
loop {
if ( fShutdown | | ! fUseUPnP )
{
2012-09-03 08:23:34 +02:00
r = UPNP_DeletePortMapping ( urls . controlURL , data . first . servicetype , port . c_str ( ) , " TCP " , 0 ) ;
2011-03-26 13:01:27 +01:00
printf ( " UPNP_DeletePortMapping() returned : %d \n " , r ) ;
freeUPNPDevlist ( devlist ) ; devlist = 0 ;
FreeUPNPUrls ( & urls ) ;
return ;
}
2012-01-31 23:36:25 +01:00
if ( i % 600 = = 0 ) // Refresh every 20 minutes
{
# 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 )
printf ( " 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
printf ( " UPnP Port Mapping successful. \n " ) ; ;
}
2011-03-26 13:01:27 +01:00
Sleep ( 2000 ) ;
2012-01-31 23:36:25 +01:00
i + + ;
2011-03-26 13:01:27 +01:00
}
} else {
printf ( " No valid UPnP IGDs found \n " ) ;
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
loop {
2011-04-15 00:37:50 +02:00
if ( fShutdown | | ! fUseUPnP )
2011-03-26 13:01:27 +01:00
return ;
Sleep ( 2000 ) ;
}
}
}
2012-05-24 19:02:21 +02:00
void MapPort ( )
2011-03-26 13:01:27 +01:00
{
2012-02-07 19:42:29 +01:00
if ( fUseUPnP & & vnThreadsRunning [ THREAD_UPNP ] < 1 )
2011-03-26 13:01:27 +01:00
{
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadMapPort , NULL ) )
2011-03-26 13:01:27 +01:00
printf ( " Error: ThreadMapPort(ThreadMapPort) failed \n " ) ;
}
}
2011-08-09 18:38:17 +02:00
# else
2012-05-24 19:02:21 +02:00
void MapPort ( )
2011-08-09 18:38:17 +02:00
{
// Intentionally left blank.
}
2011-03-26 13:01:27 +01:00
# endif
2012-01-04 23:39:45 +01:00
// DNS seeds
// Each pair gives a source name and a seed name.
// The first name is used as information source for addrman.
// The second name should resolve to a list of seed addresses.
2013-01-30 05:13:17 +01:00
static const char * strMainNetDNSSeed [ ] [ 2 ] = {
2012-01-04 23:39:45 +01:00
{ " bitcoin.sipa.be " , " seed.bitcoin.sipa.be " } ,
2012-07-11 16:55:30 +02:00
{ " bluematt.me " , " dnsseed.bluematt.me " } ,
2012-01-04 23:39:45 +01:00
{ " dashjr.org " , " dnsseed.bitcoin.dashjr.org " } ,
2012-07-11 16:55:30 +02:00
{ " xf2.org " , " bitseed.xf2.org " } ,
2013-01-30 05:13:17 +01:00
{ NULL , NULL }
} ;
static const char * strTestNetDNSSeed [ ] [ 2 ] = {
{ " bitcoin.petertodd.org " , " testnet-seed.bitcoin.petertodd.org " } ,
{ NULL , NULL }
2011-03-09 04:40:50 +01:00
} ;
2010-08-29 18:58:15 +02:00
2011-11-21 18:25:00 +01:00
void ThreadDNSAddressSeed ( void * parg )
2011-03-09 04:40:50 +01:00
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the DNS seeding thread
RenameThread ( " bitcoin-dnsseed " ) ;
2011-11-21 18:25:00 +01:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_DNSSEED ] + + ;
2011-11-21 18:25:00 +01:00
ThreadDNSAddressSeed2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_DNSSEED ] - - ;
2011-11-21 18:25:00 +01:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_DNSSEED ] - - ;
2011-11-21 18:25:00 +01:00
PrintException ( & e , " ThreadDNSAddressSeed() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_DNSSEED ] - - ;
2011-11-21 18:25:00 +01:00
throw ; // support pthread_cancel()
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadDNSAddressSeed exited \n " ) ;
2011-11-21 18:25:00 +01:00
}
void ThreadDNSAddressSeed2 ( void * parg )
{
2013-01-30 05:13:17 +01:00
static const char * ( * strDNSSeed ) [ 2 ] = fTestNet ? strTestNetDNSSeed : strMainNetDNSSeed ;
2011-11-21 18:25:00 +01:00
printf ( " ThreadDNSAddressSeed started \n " ) ;
2011-03-09 04:40:50 +01:00
int found = 0 ;
2013-01-30 05:13:17 +01:00
printf ( " Loading addresses from DNS seeds (could take a while) \n " ) ;
for ( unsigned int seed_idx = 0 ; strDNSSeed [ seed_idx ] [ 0 ] ! = NULL ; seed_idx + + ) {
if ( HaveNameProxy ( ) ) {
AddOneShot ( strDNSSeed [ seed_idx ] [ 1 ] ) ;
} else {
vector < CNetAddr > vaddr ;
vector < CAddress > vAdd ;
if ( LookupHost ( strDNSSeed [ seed_idx ] [ 1 ] , vaddr ) )
{
BOOST_FOREACH ( CNetAddr & ip , vaddr )
2011-05-02 15:34:42 +02:00
{
2013-01-30 05:13:17 +01:00
int nOneDay = 24 * 3600 ;
CAddress addr = CAddress ( CService ( ip , GetDefaultPort ( ) ) ) ;
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-01-30 05:13:17 +01:00
addrman . Add ( vAdd , CNetAddr ( strDNSSeed [ seed_idx ] [ 0 ] , true ) ) ;
2011-03-09 04:40:50 +01:00
}
}
2011-03-09 04:55:29 +01:00
printf ( " %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
2010-08-29 18:58:15 +02:00
unsigned int pnSeed [ ] =
{
2013-01-29 23:53:02 +01:00
0xe473042e , 0xb177f2ad , 0xd63f3fb2 , 0xf864f736 , 0x44a23ac7 , 0xcf6d9650 , 0xd648042e , 0x0536f447 ,
0x3c654ed0 , 0x3e16a5bc , 0xa38e09b0 , 0xdfae795b , 0xabfeca5b , 0x94ad7840 , 0xf3b9f1c7 , 0xbe70e0ad ,
0x3bbd09b0 , 0x8d0c7dd5 , 0x3b2a7332 , 0x1a06175e , 0x581f175e , 0xca0d2dcc , 0x0fdbc658 , 0xcf591ec7 ,
0x295a12b2 , 0xb4707bce , 0x68bb09b0 , 0x4e735747 , 0x89709553 , 0x05a7814e , 0x5b8ec658 , 0x402c5512 ,
0xe80d0905 , 0x17681a5e , 0xc02aa748 , 0x9f811741 , 0x5f321cb0 , 0x23e1ee47 , 0xaf7f170c , 0xaa240ab0 ,
0xedea6257 , 0x76106bc1 , 0x2cf310cc , 0x08612acb , 0x9c682e4e , 0x8e963c6c , 0x443c795b , 0x22e246b8 ,
0xfa1f2dcc , 0x90118140 , 0x3821042e , 0x33c3fd2e , 0x10046d5b , 0x40d14b3e , 0x7fb8f8ce , 0x67696550 ,
0xeeecbe58 , 0x4f341745 , 0x46b8fbd5 , 0xc8463932 , 0x6b73e862 , 0x4c715932 , 0x4a6785d5 , 0xce3a64c2 ,
0xde9604c7 , 0x9b06884f , 0x18002a45 , 0xea9bc345 , 0xc4f1c658 , 0xe475c1c7 , 0xdd3e795b , 0x9722175e ,
0x34562f4e , 0x66c46e4e , 0x40bb1243 , 0x7d9171d0 , 0x17b8dbd5 , 0x63cbfd2e , 0x1a08b8d8 , 0x6175a73b ,
0x228d2660 , 0x8627c658 , 0x9c566644 , 0x38cca5bc , 0x3089de5b , 0x92e25f5d , 0xa393f73f , 0xcc92dc3e ,
0x27487446 , 0x62cbfd2e , 0x9d983b45 , 0xf72a09b0 , 0xf75f042e , 0x6434bb6a , 0xb29e77d8 , 0x19be4fd9 ,
0x76443243 , 0x9dd72645 , 0x694cef43 , 0x89c2efd5 , 0x5f1c5058 , 0x46c6e45b , 0xe1391b40 , 0x77ccefd5 ,
0x472e5a6d , 0x85709553 , 0xdd4f5d4c , 0x64ef5a46 , 0x7f0ae502 , 0xcf08d850 , 0x3460042e , 0xeafa2d42 ,
0x793c9044 , 0x9d094746 , 0x1ab9b153 , 0xbfe9a5bc , 0x34771fb0 , 0xb7722e32 , 0x1168964b , 0x19b06ab8 ,
0x19243b25 , 0x13188045 , 0xb4070905 , 0x728ebb5d , 0x44f24ac8 , 0xa317fead , 0x642f6a57 , 0x3d951f32 ,
0x3d312e4e , 0xfac4d048 , 0xefc4dd50 , 0x52b9f1c7 , 0xc14d3cc3 , 0x0219ea44 , 0x3b79d058 , 0xfa217242 ,
0x39c80647 , 0xfb697252 , 0x1d495a42 , 0x0aa81f4e , 0x58249ab8 , 0xe6a8e6c3 , 0x2bc4dad8 , 0x85963c6c ,
0xa4ce09b0 , 0x2005f536 , 0x5cc2703e , 0x1992de43 , 0x74e86b4c , 0xe7085653 , 0xf5e15a51 , 0xb4872b60 ,
0x29e2b162 , 0xa07ea053 , 0x8229fd18 , 0x4562ec4d , 0x8dec814e , 0x36cfa4cf , 0x96461032 , 0x3c8770de ,
0xd10a1f5f , 0x95934641 , 0x97cd65d0 , 0x2e35324a , 0x2566ba1f , 0x1ca1a9d1 , 0xb808b8d5 , 0xf9a24a5d ,
0xafc8d431 , 0xe4b8d9b2 , 0x0f5321b2 , 0x330bc658 , 0x74b347ce , 0x972babd5 , 0x044f7d4f , 0x06562f4e ,
0x8b8d3c6c , 0x3507c658 , 0xe4174e4d , 0xf1c009b0 , 0x52249ab8 , 0x27211772 , 0xf6a9ba59 , 0x7a391b40 ,
0x855dc6c0 , 0x291f20b2 , 0xe29bc345 , 0x90963c6c , 0x0af70732 , 0x4242a91f , 0x4c531d48 , 0xa32df948 ,
0x627e3044 , 0x65be1f54 , 0x1a0cbf83 , 0x6a443532 , 0x8d5f1955 , 0xbafa8132 , 0x3534bdd5 , 0xca019dd9 ,
0x8a0d9332 , 0x5584e7d8 , 0x7cd1f25e , 0xeabe3fb2 , 0x2945d0d1 , 0x46415718 , 0x70d6042e , 0x99eb76d0 ,
0x9ece09b0 , 0xb3777418 , 0x5e5e91d9 , 0x237a3ab0 , 0xf512b62e , 0x45dec347 , 0x59b7f862 , 0x4c443b25 ,
0x3cc6484b , 0x9a8ec6d1 , 0x021eea44 , 0xc9483944 , 0xfd567e32 , 0xfd204bb2 , 0xc5330bcc , 0x5202894e ,
0xf9e309b0 , 0x4cc17557 , 0xdb9064ae , 0xe19e77d8 , 0x25857f60 , 0xeb4a15ad , 0x1f47f554 , 0xea4472d9 ,
0xd20de593 , 0xf5733b25 , 0x11892b54 , 0x5729d35f , 0xe6188cd1 , 0x488b132e , 0x541c534a , 0xa8e854ae ,
0xa255a66c , 0x33688763 , 0xc6629ac6 , 0xc20a6265 , 0xcd92a059 , 0x72029d3b , 0x4c298f5e , 0x51452e4e ,
0xbb065058 , 0x15fd2dcc , 0xf40c135e , 0x615a0bad , 0x0c6a6805 , 0x4971a7ad , 0x17f2a5d5 , 0xf8babf47 ,
0xb61f50ad , 0x4e1451b1 , 0xf72d9252 , 0x5c2abe58 , 0xbd987c61 , 0x084ae5cf , 0x20781fb0 , 0x38b0f160 ,
0x18aac705 , 0x14f86dc1 , 0x5556f481 , 0x0a36c144 , 0xeb446e4c , 0x2c1c0d6c , 0xbd0ff860 , 0x869f92db ,
0x36c94f4c , 0x05502444 , 0x148fe55b , 0xd5301e59 , 0xd57a8f45 , 0x110dc04a , 0x8670fc36 , 0xee733b25 ,
0xca56f481 , 0x2a5c3bae , 0x844b0905 , 0x1e51fe53 , 0x0241c244 , 0x59c0614e , 0x94e70a55 , 0x7312fead ,
0xb735be44 , 0xa55d0905 , 0x2f63962e , 0x14a4e15b , 0x63f8f05c , 0x62d0d262 , 0x3cab41ad , 0x87f1b1cb ,
0x018da6b8 , 0xb3967dd5 , 0xcb56f481 , 0x685ad718 , 0x3b4aeeca , 0x8d106bc1 , 0x51180905 , 0x72660f48 ,
0x1521a243 , 0x5b56f481 , 0x6390e560 , 0xdd61464e , 0x58353b25 , 0x553fc062 , 0x27c45d59 , 0xacc62e4e ,
0x0d5a1cd9 , 0x7f65f442 , 0xbdeef660 , 0xf1bd1855 , 0xf8473cae , 0x13b120b2 , 0x442440d0 , 0x53fd4352 ,
0xa305fc57 , 0x458be84d , 0x639ce1c3 , 0xebaaee47 , 0x95e2c247 , 0xf056f481 , 0x6256f481 , 0x1d87c65e ,
0x0a453418 , 0x5beb175e , 0xd64f1618 , 0xc360795b , 0x2fbf5753 , 0xa8c58e53 , 0x651cec52 , 0x9d37b043 ,
0x124a9758 , 0x5242e4a9 , 0x89913c6c , 0x880efe2e , 0x2f2f2f0c , 0x72b26751 , 0x2896e46d , 0x80f4166c ,
0x320d59ad , 0xc50151d0 , 0x11a8aa43 , 0xccf56057 , 0x5fbad118 , 0x4719b151 , 0x2b5f4bc0 , 0x4d7a4a50 ,
0xad06e047 , 0x62ef5a46 , 0x5aebde58 , 0xdf7aa66c , 0x851acb50 , 0x66b9a559 , 0x3e9bb153 , 0xcc512f2e ,
0xc073b08e , 0xd519be58 , 0xe981ea4d , 0x12fd50cb , 0x378739ad , 0x06683cae , 0xa22310b2 , 0xc185c705 ,
0x8741b545 , 0xa26c8318 , 0x22d5bc43 , 0x39201ec0 , 0x68581e3e , 0xdc9bcf62 , 0xd508cc82 , 0xb149675b ,
0x4c9609b0 , 0x84feb84c , 0x08291e2e , 0xfd2253b2 , 0x1fd269c1 , 0xc9483932 , 0x4d641fb0 , 0x7d37c918 ,
0xa9de20ad , 0x77e2d655 , 0x6d421b59 , 0xd7668f80 , 0xced09b62 , 0xa9e5a5bc , 0xa4074e18 , 0x60fc5ecc ,
0x01300148 , 0x68062444 , 0xb4224847 , 0xed3aa443 , 0xb772fb43 , 0x9f56f481 , 0x220dfd18 , 0x8e1c3d6c ,
0xc44f09b0 , 0x7df2bb73 , 0xe22fb844 , 0xea534242 , 0xb6a755d4 , 0xa036654b , 0x138ece5b , 0xda65d3c3 ,
0x955871bc , 0x792124b0 , 0xfc82594c , 0x851d494b , 0x2c7aee47 , 0x26af46b8 , 0x1416252e , 0xa8abb944 ,
0x36c49d25 , 0x674f645d , 0x363646b8 , 0x9e1a2942 , 0x66d0c154 , 0xc6c2a545 , 0x3570f2ad , 0xe7d547c7 ,
0x7d104932 , 0x18cb9c18 , 0x1dcfa4cf , 0xd156f481 , 0x2a02b91f , 0x3eeb3fa8 , 0xcac4175e , 0x34146d42 ,
0x994c4d46 , 0x5666f440 , 0x85d6713e , 0x5ecb296c , 0x0ea0ae46 , 0x87e69f42 , 0xc58409b0 , 0x1f3436ae ,
0x21dc6a57 , 0x4ad1cd42 , 0xfb8c1a4c , 0x52d3dab2 , 0x3769894b , 0xb52f1c62 , 0x3677916d , 0x82b3fe57 ,
0x493d4ac6 , 0x9f963c6c , 0x5d91ff60 , 0x458e0dad , 0xa49d0947 , 0x491a3e18 , 0x4aadcd5b , 0x0e46494b ,
0x1d1610ad , 0x1a10af5d , 0x4956f481 , 0x207a3eae , 0x77e73244 , 0xfa3b8742 , 0x3261fc36 , 0xfcebf536 ,
0x1662e836 , 0xf655f636 , 0xa2dbd0ad , 0x23036693 , 0x30448432 , 0xa2b03463 , 0x30730344 , 0x8e4a6882 ,
0x0c50a1cb , 0xc8d8c06b , 0xc9cd6191 , 0xf443db50 , 0xa9553c50 , 0x23145847 , 0xc35da66c , 0x29c12a60 ,
0x55c2b447 , 0x7434f75c , 0x61660640 , 0xde2a7018 , 0xc639494c , 0x1c306fce , 0x19b89244 , 0xd29a6462 ,
0x462cd1b2 , 0x29902f44 , 0x2817fa53 , 0x21a30905 , 0x7777ae46 , 0x288443a1 , 0x7bee5148 , 0xc2a8b043 ,
0xf5c3d35f , 0x2311ef84 , 0x57de08a4 , 0x6b221bb2 , 0xf2625846 , 0x4b9e09b0 , 0xa24f880e , 0x22b11447 ,
0xb3a0c744 , 0x919e77d8 , 0xec8b64ae , 0xff5c8d45 , 0x7b15b484 , 0x32679a5f , 0xba80b62e , 0x05c25c61 ,
0x60014746 , 0x5e8fb04c , 0xe67c0905 , 0x4329c658 , 0xac8fe555 , 0xf875e647 , 0x67406386 , 0x35ceea18 ,
0xbb79484b , 0xd7b9fa62 , 0x238209b0 , 0x208a1d32 , 0x9630995e , 0x039c1318 , 0x6e48006c , 0x60582344 ,
0xadbb0150 , 0x853fd462 , 0x03772e4e , 0x652ce960 , 0x49b630ad , 0x9993af43 , 0x3735b34b , 0x548a07d9 ,
0x55a44aad , 0xa23d1bcc , 0xfdbb2f4e , 0x530b24a0 , 0x0a44b451 , 0x6827c657 , 0x1f66494b , 0x4e680a47 ,
0x77e7b747 , 0xa5eb3fa8 , 0x6649764a , 0xd4e76c4b , 0x2c691fb0 , 0xf1292e44 , 0xc6d6c774 , 0x85d23775 ,
0x28275f4d , 0x259ae46d , 0x02424e81 , 0x5f16be58 , 0xe707c658 , 0x49eae5c7 , 0xd5d147ad , 0x9a7abdc3 ,
0xe8ac7fc7 , 0x84ec3aae , 0xc24942d0 , 0x294aa318 , 0x08ac3d18 , 0x8894042e , 0xb24609b0 , 0x9bcaab58 ,
0xc400f712 , 0xd5c512b8 , 0x2c02cc62 , 0x25080fd8 , 0xed74a847 , 0x18a5ec5e , 0x9850ec6d , 0xf8909758 ,
0x7f56f481 , 0x4496f23c , 0xae27784f , 0xcb7cd93e , 0x06e32860 , 0x50b9a84f , 0x3660434a , 0x09161f5f ,
0x900486bc , 0x08055459 , 0xe7ec1017 , 0x7e39494c , 0x4f443b25 , 0x14751a8a , 0x717d03d4 , 0xbd0e24d8 ,
0x054b6f56 , 0x854c496c , 0xd92a454a , 0xc39bd054 , 0x6093614b , 0x9dbad754 , 0x5bf0604a , 0x99f22305
2010-08-29 18:58:15 +02: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 ) ;
printf ( " Flushed %d addresses to peers.dat % " PRI64d " ms \n " ,
addrman . size ( ) , GetTimeMillis ( ) - nStart ) ;
2012-01-04 23:39:45 +01:00
}
2010-08-29 18:58:15 +02:00
2012-01-04 23:39:45 +01:00
void ThreadDumpAddress2 ( void * parg )
{
2012-10-14 13:43:42 +02:00
printf ( " ThreadDumpAddress started \n " ) ;
2012-01-04 23:39:45 +01:00
vnThreadsRunning [ THREAD_DUMPADDRESS ] + + ;
while ( ! fShutdown )
{
DumpAddresses ( ) ;
vnThreadsRunning [ THREAD_DUMPADDRESS ] - - ;
Sleep ( 100000 ) ;
vnThreadsRunning [ THREAD_DUMPADDRESS ] + + ;
}
vnThreadsRunning [ THREAD_DUMPADDRESS ] - - ;
}
void ThreadDumpAddress ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the address dumping thread
RenameThread ( " bitcoin-adrdump " ) ;
2012-01-04 23:39:45 +01:00
try
{
ThreadDumpAddress2 ( parg ) ;
}
catch ( std : : exception & e ) {
PrintException ( & e , " ThreadDumpAddress() " ) ;
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadDumpAddress exited \n " ) ;
2012-01-04 23:39:45 +01:00
}
2010-08-29 18:58:15 +02:00
void ThreadOpenConnections ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the connection opening thread
RenameThread ( " bitcoin-opencon " ) ;
2010-08-29 18:58:15 +02:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] + + ;
2010-08-29 18:58:15 +02:00
ThreadOpenConnections2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2010-08-29 18:58:15 +02:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2010-08-29 18:58:15 +02:00
PrintException ( & e , " ThreadOpenConnections() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2010-08-29 18:58:15 +02:00
PrintException ( NULL , " ThreadOpenConnections() " ) ;
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadOpenConnections exited \n " ) ;
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
}
2010-08-29 18:58:15 +02:00
void ThreadOpenConnections2 ( void * parg )
{
printf ( " ThreadOpenConnections started \n " ) ;
// 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 + + )
{
Sleep ( 500 ) ;
if ( fShutdown )
return ;
}
}
2012-08-21 17:32:04 +02:00
Sleep ( 500 ) ;
2010-08-29 18:58:15 +02:00
}
}
// Initiate network connections
2011-12-21 22:33:19 +01:00
int64 nStart = GetTime ( ) ;
2010-08-29 18:58:15 +02:00
loop
{
2012-04-24 02:15:00 +02:00
ProcessOneShot ( ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2010-08-29 18:58:15 +02:00
Sleep ( 500 ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] + + ;
2012-02-15 21:17:15 +01:00
if ( fShutdown )
return ;
2012-05-10 18:44:07 +02:00
2012-04-04 16:01:57 +02:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2012-05-10 18:44:07 +02:00
CSemaphoreGrant grant ( * semOutbound ) ;
2012-04-04 16:01:57 +02:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] + + ;
if ( fShutdown )
return ;
2010-08-29 18:58:15 +02:00
2012-01-04 23:39:45 +01:00
// Add seed nodes if IRC isn't working
2012-05-24 19:02:21 +02:00
if ( addrman . size ( ) = = 0 & & ( GetTime ( ) - nStart > 60 ) & & ! fTestNet )
2012-01-03 01:28:15 +01:00
{
2012-01-04 23:39:45 +01:00
std : : vector < CAddress > vAdd ;
2012-04-15 22:52:09 +02:00
for ( unsigned int i = 0 ; i < ARRAYLEN ( pnSeed ) ; i + + )
2010-08-29 18:58:15 +02:00
{
2012-01-03 01:28:15 +01:00
// It'll only connect to one or two seed nodes because once it connects,
// it'll get a pile of addresses with newer timestamps.
// Seed nodes are given a random 'last seen time' of between one and two
// weeks ago.
const int64 nOneWeek = 7 * 24 * 60 * 60 ;
2012-01-03 23:33:31 +01:00
struct in_addr ip ;
memcpy ( & ip , & pnSeed [ i ] , sizeof ( ip ) ) ;
CAddress addr ( CService ( ip , GetDefaultPort ( ) ) ) ;
2012-01-03 01:28:15 +01:00
addr . nTime = GetTime ( ) - GetRand ( nOneWeek ) - nOneWeek ;
2012-01-04 23:39:45 +01:00
vAdd . push_back ( addr ) ;
2010-08-29 18:58:15 +02:00
}
2012-01-04 23:39:45 +01:00
addrman . Add ( vAdd , CNetAddr ( " 127.0.0.1 " ) ) ;
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 ;
loop
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
if ( addr . GetPort ( ) ! = GetDefaultPort ( ) & & nTries < 50 )
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
}
}
2011-12-17 01:48:03 +01:00
void ThreadOpenAddedConnections ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the connection opening thread
RenameThread ( " bitcoin-opencon " ) ;
2011-12-17 01:48:03 +01:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] + + ;
2011-12-17 01:48:03 +01:00
ThreadOpenAddedConnections2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] - - ;
2011-12-17 01:48:03 +01:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] - - ;
2011-12-17 01:48:03 +01:00
PrintException ( & e , " ThreadOpenAddedConnections() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] - - ;
2011-12-17 01:48:03 +01:00
PrintException ( NULL , " ThreadOpenAddedConnections() " ) ;
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadOpenAddedConnections exited \n " ) ;
2011-12-17 01:48:03 +01:00
}
void ThreadOpenAddedConnections2 ( void * parg )
{
printf ( " ThreadOpenAddedConnections started \n " ) ;
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 ( ) ) {
2012-04-19 17:38:03 +02:00
while ( ! fShutdown ) {
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 ( ) ) ;
2012-04-19 17:38:03 +02:00
Sleep ( 500 ) ;
2012-07-02 20:14:02 +02:00
if ( fShutdown )
return ;
2012-04-19 17:38:03 +02:00
}
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] - - ;
Sleep ( 120000 ) ; // Retry every 2 minutes
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] + + ;
}
return ;
}
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 ) ;
if ( Lookup ( strAddNode . c_str ( ) , vservNode , 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 ) ;
2011-12-17 01:48:03 +01:00
Sleep ( 500 ) ;
if ( fShutdown )
return ;
}
if ( fShutdown )
return ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] - - ;
2011-12-17 01:48:03 +01:00
Sleep ( 120000 ) ; // Retry every 2 minutes
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] + + ;
2011-12-17 01:48:03 +01:00
if ( fShutdown )
return ;
}
}
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
//
if ( fShutdown )
return false ;
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-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] - - ;
2012-04-19 17:38:03 +02:00
CNode * pnode = ConnectNode ( addrConnect , strDest ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_OPENCONNECTIONS ] + + ;
2010-08-29 18:58:15 +02:00
if ( fShutdown )
return false ;
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 ;
}
void ThreadMessageHandler ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the message handling thread
RenameThread ( " bitcoin-msghand " ) ;
2010-08-29 18:58:15 +02:00
try
{
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] + + ;
2010-08-29 18:58:15 +02:00
ThreadMessageHandler2 ( parg ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
}
catch ( std : : exception & e ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
PrintException ( & e , " ThreadMessageHandler() " ) ;
} catch ( . . . ) {
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
PrintException ( NULL , " ThreadMessageHandler() " ) ;
}
2012-05-17 19:52:38 +02:00
printf ( " ThreadMessageHandler exited \n " ) ;
2010-08-29 18:58:15 +02:00
}
void ThreadMessageHandler2 ( void * parg )
{
printf ( " ThreadMessageHandler started \n " ) ;
SetThreadPriority ( THREAD_PRIORITY_BELOW_NORMAL ) ;
while ( ! fShutdown )
{
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 ( ) ;
}
// 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
{
// Receive messages
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vRecv , lockRecv ) ;
if ( lockRecv )
ProcessMessages ( pnode ) ;
}
2010-08-29 18:58:15 +02:00
if ( fShutdown )
return ;
// Send messages
2012-04-06 18:39:12 +02:00
{
TRY_LOCK ( pnode - > cs_vSend , lockSend ) ;
if ( lockSend )
SendMessages ( pnode , pnode = = pnodeTrickle ) ;
}
2010-08-29 18:58:15 +02:00
if ( fShutdown )
return ;
}
{
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 ( ) ;
}
2010-09-09 03:00:40 +02:00
// Wait and allow messages to bunch up.
// Reduce vnThreadsRunning so StopNode has permission to exit while
// we're sleeping, but we must always check fShutdown after doing this.
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] - - ;
2010-08-29 18:58:15 +02:00
Sleep ( 100 ) ;
2010-09-08 21:24:32 +02:00
if ( fRequestShutdown )
2012-06-11 07:40:14 +02:00
StartShutdown ( ) ;
2012-02-07 19:42:29 +01:00
vnThreadsRunning [ THREAD_MESSAGEHANDLER ] + + ;
2010-08-29 18:58:15 +02:00
if ( fShutdown )
return ;
}
}
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 ;
2011-10-07 17:02:21 +02:00
# ifdef WIN32
2010-08-29 18:58:15 +02:00
// Initialize Windows Sockets
WSADATA wsadata ;
int ret = WSAStartup ( MAKEWORD ( 2 , 2 ) , & wsadata ) ;
if ( ret ! = NO_ERROR )
{
strError = strprintf ( " Error: TCP/IP socket library failed to start (WSAStartup returned error %d) " , ret ) ;
printf ( " %s \n " , strError . c_str ( ) ) ;
return false ;
}
# endif
// 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 ( ) ) ;
printf ( " %s \n " , strError . c_str ( ) ) ;
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 ( ) ) ;
printf ( " %s \n " , strError . c_str ( ) ) ;
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 ( ) ) ;
printf ( " %s \n " , strError . c_str ( ) ) ;
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
setsockopt ( hListenSocket , IPPROTO_IPV6 , IPV6_V6ONLY , ( void * ) & nOne , sizeof ( int ) ) ;
2012-03-31 17:58:25 +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 ) ) ;
2010-08-29 18:58:15 +02:00
printf ( " %s \n " , strError . c_str ( ) ) ;
return false ;
}
2012-05-11 15:28:59 +02:00
printf ( " 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 ( ) ) ;
printf ( " %s \n " , strError . c_str ( ) ) ;
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 ) )
2012-05-13 00:40:30 +02:00
printf ( " 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 ) )
2012-05-13 00:40:30 +02:00
printf ( " 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 ) )
2012-08-29 20:25:37 +02:00
NewThread ( ThreadGetMyExternalIP , NULL ) ;
2012-02-19 20:44:35 +01:00
}
void StartNode ( void * parg )
{
2012-06-24 17:03:57 +02:00
// Make this thread recognisable as the startup thread
2012-06-30 17:14:28 +02:00
RenameThread ( " bitcoin-start " ) ;
2012-06-24 17:03:57 +02:00
2012-05-10 18:44:07 +02:00
if ( semOutbound = = NULL ) {
// initialize semaphore
int nMaxOutbound = min ( MAX_OUTBOUND_CONNECTIONS , ( int ) GetArg ( " -maxconnections " , 125 ) ) ;
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 ) )
2011-11-21 18:25:00 +01:00
printf ( " DNS seeding disabled \n " ) ;
else
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadDNSAddressSeed , NULL ) )
printf ( " Error: NewThread(ThreadDNSAddressSeed) failed \n " ) ;
2011-11-21 18:25:00 +01:00
2011-03-26 13:01:27 +01:00
// Map ports with UPnP
2012-05-24 19:02:21 +02:00
if ( fUseUPnP )
MapPort ( ) ;
2011-03-26 13:01:27 +01:00
2010-08-29 18:58:15 +02:00
// Get addresses from IRC and advertise ours
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadIRCSeed , NULL ) )
printf ( " Error: NewThread(ThreadIRCSeed) failed \n " ) ;
2010-08-29 18:58:15 +02:00
// Send and receive from sockets, accept connections
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadSocketHandler , NULL ) )
printf ( " Error: NewThread(ThreadSocketHandler) failed \n " ) ;
2010-08-29 18:58:15 +02:00
2011-12-17 01:48:03 +01:00
// Initiate outbound connections from -addnode
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadOpenAddedConnections , NULL ) )
printf ( " Error: NewThread(ThreadOpenAddedConnections) failed \n " ) ;
2011-12-17 01:48:03 +01:00
2010-08-29 18:58:15 +02:00
// Initiate outbound connections
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadOpenConnections , NULL ) )
printf ( " Error: NewThread(ThreadOpenConnections) failed \n " ) ;
2010-08-29 18:58:15 +02:00
// Process messages
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadMessageHandler , NULL ) )
printf ( " Error: NewThread(ThreadMessageHandler) failed \n " ) ;
2010-08-29 18:58:15 +02:00
2012-01-04 23:39:45 +01:00
// Dump network addresses
2012-08-29 20:25:37 +02:00
if ( ! NewThread ( ThreadDumpAddress , NULL ) )
printf ( " Error; NewThread(ThreadDumpAddress) failed \n " ) ;
2012-01-04 23:39:45 +01:00
2010-08-29 18:58:15 +02:00
// Generate coins in the background
2012-02-16 21:00:16 +01:00
GenerateBitcoins ( GetBoolArg ( " -gen " , false ) , pwalletMain ) ;
2010-08-29 18:58:15 +02:00
}
bool StopNode ( )
{
printf ( " StopNode() \n " ) ;
fShutdown = true ;
nTransactionsUpdated + + ;
2011-12-21 22:33:19 +01:00
int64 nStart = GetTime ( ) ;
2012-05-12 17:44:14 +02:00
if ( semOutbound )
for ( int i = 0 ; i < MAX_OUTBOUND_CONNECTIONS ; i + + )
semOutbound - > post ( ) ;
2012-02-07 19:42:29 +01:00
do
2010-08-29 18:58:15 +02:00
{
2012-02-07 19:42:29 +01:00
int nThreadsRunning = 0 ;
for ( int n = 0 ; n < THREAD_MAX ; n + + )
nThreadsRunning + = vnThreadsRunning [ n ] ;
if ( nThreadsRunning = = 0 )
break ;
2010-08-29 18:58:15 +02:00
if ( GetTime ( ) - nStart > 20 )
break ;
Sleep ( 20 ) ;
2012-02-07 19:42:29 +01:00
} while ( true ) ;
if ( vnThreadsRunning [ THREAD_SOCKETHANDLER ] > 0 ) printf ( " ThreadSocketHandler still running \n " ) ;
if ( vnThreadsRunning [ THREAD_OPENCONNECTIONS ] > 0 ) printf ( " ThreadOpenConnections still running \n " ) ;
if ( vnThreadsRunning [ THREAD_MESSAGEHANDLER ] > 0 ) printf ( " ThreadMessageHandler still running \n " ) ;
if ( vnThreadsRunning [ THREAD_MINER ] > 0 ) printf ( " ThreadBitcoinMiner still running \n " ) ;
2012-04-15 02:35:58 +02:00
if ( vnThreadsRunning [ THREAD_RPCLISTENER ] > 0 ) printf ( " ThreadRPCListener still running \n " ) ;
if ( vnThreadsRunning [ THREAD_RPCHANDLER ] > 0 ) printf ( " ThreadsRPCServer still running \n " ) ;
2012-05-24 19:02:21 +02:00
# ifdef USE_UPNP
if ( vnThreadsRunning [ THREAD_UPNP ] > 0 ) printf ( " ThreadMapPort still running \n " ) ;
# endif
2012-02-07 19:42:29 +01:00
if ( vnThreadsRunning [ THREAD_DNSSEED ] > 0 ) printf ( " ThreadDNSAddressSeed still running \n " ) ;
if ( vnThreadsRunning [ THREAD_ADDEDCONNECTIONS ] > 0 ) printf ( " ThreadOpenAddedConnections still running \n " ) ;
2012-01-04 23:39:45 +01:00
if ( vnThreadsRunning [ THREAD_DUMPADDRESS ] > 0 ) printf ( " ThreadDumpAddresses still running \n " ) ;
2012-04-15 02:35:58 +02:00
while ( vnThreadsRunning [ THREAD_MESSAGEHANDLER ] > 0 | | vnThreadsRunning [ THREAD_RPCHANDLER ] > 0 )
2010-08-29 18:58:15 +02:00
Sleep ( 20 ) ;
Sleep ( 50 ) ;
2012-01-04 23:39:45 +01:00
DumpAddresses ( ) ;
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 )
printf ( " closesocket(hListenSocket) failed with error %d \n " , WSAGetLastError ( ) ) ;
2010-08-29 18:58:15 +02:00
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 ) ;
}
}