Support for multiple local addresses

This commit is contained in:
Pieter Wuille 2012-02-12 13:45:24 +01:00
parent 478b01d9a7
commit 39857190de
8 changed files with 187 additions and 88 deletions

View file

@ -12,7 +12,6 @@ using namespace std;
using namespace boost; using namespace boost;
int nGotIRCAddresses = 0; int nGotIRCAddresses = 0;
bool fGotExternalIP = false;
void ThreadIRCSeed2(void* parg); void ThreadIRCSeed2(void* parg);
@ -216,7 +215,6 @@ void ThreadIRCSeed2(void* parg)
printf("ThreadIRCSeed started\n"); printf("ThreadIRCSeed started\n");
int nErrorWait = 10; int nErrorWait = 10;
int nRetryWait = 10; int nRetryWait = 10;
bool fNameInUse = false;
while (!fShutdown) while (!fShutdown)
{ {
@ -248,9 +246,10 @@ void ThreadIRCSeed2(void* parg)
return; return;
} }
CNetAddr addrLocal;
string strMyName; string strMyName;
if (addrLocalHost.IsRoutable() && !fUseProxy && !fNameInUse) if (GetLocal(addrLocal, &addrConnect))
strMyName = EncodeAddress(addrLocalHost); strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
else else
strMyName = strprintf("x%u", GetRand(1000000000)); strMyName = strprintf("x%u", GetRand(1000000000));
@ -265,7 +264,6 @@ void ThreadIRCSeed2(void* parg)
if (nRet == 2) if (nRet == 2)
{ {
printf("IRC name already in use\n"); printf("IRC name already in use\n");
fNameInUse = true;
Wait(10); Wait(10);
continue; continue;
} }
@ -285,9 +283,8 @@ void ThreadIRCSeed2(void* parg)
if (!fUseProxy && addrFromIRC.IsRoutable()) if (!fUseProxy && addrFromIRC.IsRoutable())
{ {
// IRC lets you to re-nick // IRC lets you to re-nick
fGotExternalIP = true; AddLocal(addrFromIRC, LOCAL_IRC);
addrLocalHost.SetIP(addrFromIRC); strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
strMyName = EncodeAddress(addrLocalHost);
Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str()); Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
} }
} }

View file

@ -8,6 +8,5 @@
void ThreadIRCSeed(void* parg); void ThreadIRCSeed(void* parg);
extern int nGotIRCAddresses; extern int nGotIRCAddresses;
extern bool fGotExternalIP;
#endif #endif

View file

@ -2232,6 +2232,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (!vRecv.empty()) if (!vRecv.empty())
vRecv >> pfrom->nStartingHeight; vRecv >> pfrom->nStartingHeight;
if (pfrom->fInbound && addrMe.IsRoutable())
{
pfrom->addrLocal = addrMe;
SeenLocal(addrMe);
}
// Disconnect if we connected to ourself // Disconnect if we connected to ourself
if (nNonce == nLocalHostNonce && nNonce > 1) if (nNonce == nLocalHostNonce && nNonce > 1)
{ {
@ -2255,11 +2261,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (!pfrom->fInbound) if (!pfrom->fInbound)
{ {
// Advertise our address // Advertise our address
if (!fNoListen && !fUseProxy && addrLocalHost.IsRoutable() && if (!fNoListen && !fUseProxy && !IsInitialBlockDownload())
!IsInitialBlockDownload())
{ {
CAddress addr(addrLocalHost); CAddress addr = GetLocalAddress(&pfrom->addr);
addr.nTime = GetAdjustedTime(); if (addr.IsRoutable())
pfrom->PushAddress(addr); pfrom->PushAddress(addr);
} }
@ -2889,10 +2894,10 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
pnode->setAddrKnown.clear(); pnode->setAddrKnown.clear();
// Rebroadcast our address // Rebroadcast our address
if (!fNoListen && !fUseProxy && addrLocalHost.IsRoutable()) if (!fNoListen && !fUseProxy)
{ {
CAddress addr(addrLocalHost); CAddress addr = GetLocalAddress(&pnode->addr);
addr.nTime = GetAdjustedTime(); if (addr.IsRoutable())
pnode->PushAddress(addr); pnode->PushAddress(addr);
} }
} }

View file

@ -45,7 +45,8 @@ bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NU
bool fClient = false; bool fClient = false;
static bool fUseUPnP = false; static bool fUseUPnP = false;
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
CAddress addrLocalHost(CService("0.0.0.0", 0), nLocalServices); CCriticalSection cs_mapLocalHost;
map<CNetAddr, int> mapLocalHost;
static CNode* pnodeLocalHost = NULL; static CNode* pnodeLocalHost = NULL;
uint64 nLocalHostNonce = 0; uint64 nLocalHostNonce = 0;
array<int, THREAD_MAX> vnThreadsRunning; array<int, THREAD_MAX> vnThreadsRunning;
@ -92,7 +93,45 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd); PushMessage("getblocks", CBlockLocator(pindexBegin), hashEnd);
} }
// find 'best' local address for a particular peer
bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
{
if (fUseProxy || mapArgs.count("-connect") || fNoListen)
return false;
int nBestCount = -1;
int nBestReachability = -1;
{
LOCK(cs_mapLocalHost);
for (map<CNetAddr, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
{
int nCount = (*it).second;
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
if (nReachability > nBestReachability || (nReachability == nBestReachability && nCount > nBestCount))
{
addr = (*it).first;
nBestReachability = nReachability;
nBestCount = nCount;
}
}
}
return nBestCount >= 0;
}
// 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);
CNetAddr addr;
if (GetLocal(addr, paddrPeer))
{
ret.SetIP(addr);
ret.SetPort(GetListenPort());
ret.nServices = nLocalServices;
ret.nTime = GetAdjustedTime();
}
return ret;
}
bool RecvLine(SOCKET hSocket, string& strLine) bool RecvLine(SOCKET hSocket, string& strLine)
{ {
@ -145,6 +184,64 @@ bool RecvLine(SOCKET hSocket, string& strLine)
} }
} }
// 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);
if (addrLocal.IsRoutable() && (CNetAddr)addrLocal != (CNetAddr)pnode->addrLocal)
{
pnode->PushAddress(addrLocal);
pnode->addrLocal = addrLocal;
}
}
}
}
// learn a new local address
bool AddLocal(const CNetAddr& addr, int nScore)
{
if (!addr.IsRoutable())
return false;
printf("AddLocal(%s,%i)\n", addr.ToString().c_str(), nScore);
{
LOCK(cs_mapLocalHost);
mapLocalHost[addr] = std::max(nScore, mapLocalHost[addr]) + (mapLocalHost.count(addr) ? 1 : 0);
}
AdvertizeLocal();
return true;
}
// vote for a local address
bool SeenLocal(const CNetAddr& addr)
{
{
LOCK(cs_mapLocalHost);
if (mapLocalHost.count(addr) == 0)
return false;
mapLocalHost[addr]++;
}
AdvertizeLocal();
return true;
}
// check whether a given address is potentially local
bool IsLocal(const CNetAddr& addr)
{
LOCK(cs_mapLocalHost);
return mapLocalHost.count(addr) > 0;
}
bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet) bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
@ -258,33 +355,11 @@ bool GetMyExternalIP(CNetAddr& ipRet)
void ThreadGetMyExternalIP(void* parg) void ThreadGetMyExternalIP(void* parg)
{ {
// Wait for IRC to get it first CNetAddr addrLocalHost;
if (GetBoolArg("-irc", false))
{
for (int i = 0; i < 2 * 60; i++)
{
Sleep(1000);
if (fGotExternalIP || fShutdown)
return;
}
}
// Fallback in case IRC fails to get it
if (GetMyExternalIP(addrLocalHost)) if (GetMyExternalIP(addrLocalHost))
{ {
printf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP().c_str()); printf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP().c_str());
if (addrLocalHost.IsRoutable()) AddLocal(addrLocalHost, LOCAL_HTTP);
{
// If we already connected to a few before we had our IP, go back and addr them.
// setAddrKnown automatically filters any duplicate sends.
CAddress addr(addrLocalHost);
addr.nTime = GetAdjustedTime();
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
pnode->PushAddress(addr);
}
}
} }
} }
@ -337,7 +412,7 @@ CNode* FindNode(const CService& addr)
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout) CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout)
{ {
if (pszDest == NULL) { if (pszDest == NULL) {
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) if (IsLocal(addrConnect))
return NULL; return NULL;
// Look for an existing connection // Look for an existing connection
@ -426,7 +501,7 @@ void CNode::PushVersion()
/// when NTP implemented, change to just nTime = GetAdjustedTime() /// when NTP implemented, change to just nTime = GetAdjustedTime()
int64 nTime = (fInbound ? GetAdjustedTime() : GetTime()); int64 nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (fUseProxy ? CAddress(CService("0.0.0.0",0)) : addr); CAddress addrYou = (fUseProxy ? CAddress(CService("0.0.0.0",0)) : addr);
CAddress addrMe = (fUseProxy || !addrLocalHost.IsRoutable() ? CAddress(CService("0.0.0.0",0)) : addrLocalHost); CAddress addrMe = GetLocalAddress(&addr);
RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); RAND_bytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe, PushMessage("version", PROTOCOL_VERSION, nLocalServices, nTime, addrYou, addrMe,
nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight); nLocalHostNonce, FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, std::vector<string>()), nBestHeight);
@ -897,8 +972,6 @@ void ThreadMapPort2(void* parg)
r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr)); r = UPNP_GetValidIGD(devlist, &urls, &data, lanaddr, sizeof(lanaddr));
if (r == 1) if (r == 1)
{
if (!addrLocalHost.IsRoutable())
{ {
char externalIPAddress[40]; char externalIPAddress[40];
r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress); r = UPNP_GetExternalIPAddress(urls.controlURL, data.first.servicetype, externalIPAddress);
@ -909,14 +982,11 @@ void ThreadMapPort2(void* parg)
if(externalIPAddress[0]) if(externalIPAddress[0])
{ {
printf("UPnP: ExternalIPAddress = %s\n", externalIPAddress); printf("UPnP: ExternalIPAddress = %s\n", externalIPAddress);
CAddress addrExternalFromUPnP(CService(externalIPAddress, 0), nLocalServices); AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP);
if (addrExternalFromUPnP.IsRoutable())
addrLocalHost = addrExternalFromUPnP;
} }
else else
printf("UPnP: GetExternalIPAddress failed.\n"); printf("UPnP: GetExternalIPAddress failed.\n");
} }
}
string strDesc = "Bitcoin " + FormatFullVersion(); string strDesc = "Bitcoin " + FormatFullVersion();
#ifndef UPNPDISCOVER_SUCCESS #ifndef UPNPDISCOVER_SUCCESS
@ -1318,7 +1388,7 @@ void ThreadOpenConnections2(void* parg)
CAddress addr = addrman.Select(10 + min(nOutbound,8)*10); CAddress addr = addrman.Select(10 + min(nOutbound,8)*10);
// if we selected an invalid address, restart // if we selected an invalid address, restart
if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.GetGroup()) || addr == addrLocalHost) if (!addr.IsIPv4() || !addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
break; break;
nTries++; nTries++;
@ -1436,7 +1506,7 @@ bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest, boo
if (fShutdown) if (fShutdown)
return false; return false;
if (!strDest) if (!strDest)
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || if (IsLocal(addrConnect) ||
FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) || FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) ||
FindNode(addrConnect.ToStringIPPort().c_str())) FindNode(addrConnect.ToStringIPPort().c_str()))
return false; return false;
@ -1550,7 +1620,6 @@ bool BindListenPort(string& strError)
{ {
strError = ""; strError = "";
int nOne = 1; int nOne = 1;
addrLocalHost.SetPort(GetListenPort());
#ifdef WIN32 #ifdef WIN32
// Initialize Windows Sockets // Initialize Windows Sockets
@ -1649,11 +1718,7 @@ void StartNode(void* parg)
{ {
BOOST_FOREACH (const CNetAddr &addr, vaddr) BOOST_FOREACH (const CNetAddr &addr, vaddr)
{ {
if (!addr.IsLocal()) AddLocal(addr, LOCAL_IF);
{
addrLocalHost.SetIP(addr);
break;
}
} }
} }
} }
@ -1676,32 +1741,26 @@ void StartNode(void* parg)
printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP); printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
// Take the first IP that isn't loopback 127.x.x.x // Take the first IP that isn't loopback 127.x.x.x
CAddress addr(CService(s4->sin_addr, GetListenPort()), nLocalServices); CNetAddr addr(s4->sin_addr);
if (addr.IsValid() && !addr.IsLocal()) AddLocal(addr, LOCAL_IF);
{
addrLocalHost = addr;
break;
}
} }
else if (ifa->ifa_addr->sa_family == AF_INET6) else if (ifa->ifa_addr->sa_family == AF_INET6)
{ {
struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr); struct sockaddr_in6* s6 = (struct sockaddr_in6*)(ifa->ifa_addr);
if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s6->sin6_addr), pszIP, sizeof(pszIP)) != NULL) if (inet_ntop(ifa->ifa_addr->sa_family, (void*)&(s6->sin6_addr), pszIP, sizeof(pszIP)) != NULL)
printf("ipv6 %s: %s\n", ifa->ifa_name, pszIP); printf("ipv6 %s: %s\n", ifa->ifa_name, pszIP);
#ifdef USE_IPV6
CNetAddr addr(s6->sin6_addr);
AddLocal(addr, LOCAL_IF);
#endif
} }
} }
freeifaddrs(myaddrs); freeifaddrs(myaddrs);
} }
#endif #endif
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
if (fUseProxy || mapArgs.count("-connect") || fNoListen) if (!fUseProxy && !mapArgs.count("-connect") && !fNoListen)
{
// Proxies can't take incoming connections
addrLocalHost.SetIP(CNetAddr("0.0.0.0"));
printf("addrLocalHost = %s\n", addrLocalHost.ToString().c_str());
}
else
{ {
CreateThread(ThreadGetMyExternalIP, NULL); CreateThread(ThreadGetMyExternalIP, NULL);
} }

View file

@ -42,6 +42,21 @@ bool BindListenPort(std::string& strError=REF(std::string()));
void StartNode(void* parg); void StartNode(void* parg);
bool StopNode(); bool StopNode();
enum
{
LOCAL_NONE,
LOCAL_IF,
LOCAL_UPNP,
LOCAL_IRC,
LOCAL_HTTP,
};
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
bool SeenLocal(const CNetAddr& addr);
bool IsLocal(const CNetAddr& addr);
bool GetLocal(CNetAddr &addr, const CNetAddr *paddrPeer = NULL);
CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
enum enum
{ {
MSG_TX = 1, MSG_TX = 1,
@ -85,7 +100,6 @@ enum threadId
extern bool fClient; extern bool fClient;
extern uint64 nLocalServices; extern uint64 nLocalServices;
extern CAddress addrLocalHost;
extern uint64 nLocalHostNonce; extern uint64 nLocalHostNonce;
extern boost::array<int, THREAD_MAX> vnThreadsRunning; extern boost::array<int, THREAD_MAX> vnThreadsRunning;
extern CAddrMan addrman; extern CAddrMan addrman;
@ -121,6 +135,7 @@ public:
unsigned int nMessageStart; unsigned int nMessageStart;
CAddress addr; CAddress addr;
std::string addrName; std::string addrName;
CNetAddr addrLocal;
int nVersion; int nVersion;
std::string strSubVer; std::string strSubVer;
bool fOneShot; bool fOneShot;

View file

@ -772,6 +772,29 @@ void CNetAddr::print() const
printf("CNetAddr(%s)\n", ToString().c_str()); printf("CNetAddr(%s)\n", ToString().c_str());
} }
// for IPv6 partners: for unknown/Teredo partners: for IPv4 partners:
// 0 - unroutable // 0 - unroutable // 0 - unroutable
// 1 - teredo // 1 - teredo // 1 - ipv4
// 2 - tunneled ipv6 // 2 - tunneled ipv6
// 3 - ipv4 // 3 - ipv6
// 4 - ipv6 // 4 - ipv4
int CNetAddr::GetReachabilityFrom(const CNetAddr *paddrPartner) const
{
if (!IsValid() || !IsRoutable())
return 0;
if (paddrPartner && paddrPartner->IsIPv4())
return IsIPv4() ? 1 : 0;
if (IsRFC4380())
return 1;
if (IsRFC3964() || IsRFC6052())
return 2;
bool fRealIPv6 = paddrPartner && !paddrPartner->IsRFC4380() && paddrPartner->IsValid() && paddrPartner->IsRoutable();
if (fRealIPv6)
return IsIPv4() ? 3 : 4;
else
return IsIPv4() ? 4 : 3;
}
void CService::Init() void CService::Init()
{ {
port = 0; port = 0;

View file

@ -51,6 +51,7 @@ class CNetAddr
int64 GetHash() const; int64 GetHash() const;
bool GetInAddr(struct in_addr* pipv4Addr) const; bool GetInAddr(struct in_addr* pipv4Addr) const;
std::vector<unsigned char> GetGroup() const; std::vector<unsigned char> GetGroup() const;
int GetReachabilityFrom(const CNetAddr *paddrPartner = NULL) const;
void print() const; void print() const;
#ifdef USE_IPV6 #ifdef USE_IPV6

View file

@ -31,13 +31,13 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
{ {
CNode::ClearBanned(); CNode::ClearBanned();
CAddress addr1(ip(0xa0b0c001)); CAddress addr1(ip(0xa0b0c001));
CNode dummyNode1(INVALID_SOCKET, addr1, true); CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
dummyNode1.Misbehaving(100); // Should get banned dummyNode1.Misbehaving(100); // Should get banned
BOOST_CHECK(CNode::IsBanned(addr1)); BOOST_CHECK(CNode::IsBanned(addr1));
BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different ip, not banned BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different ip, not banned
CAddress addr2(ip(0xa0b0c002)); CAddress addr2(ip(0xa0b0c002));
CNode dummyNode2(INVALID_SOCKET, addr2, true); CNode dummyNode2(INVALID_SOCKET, addr2, "", true);
dummyNode2.Misbehaving(50); dummyNode2.Misbehaving(50);
BOOST_CHECK(!CNode::IsBanned(addr2)); // 2 not banned yet... BOOST_CHECK(!CNode::IsBanned(addr2)); // 2 not banned yet...
BOOST_CHECK(CNode::IsBanned(addr1)); // ... but 1 still should be BOOST_CHECK(CNode::IsBanned(addr1)); // ... but 1 still should be
@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
CNode::ClearBanned(); CNode::ClearBanned();
mapArgs["-banscore"] = "111"; // because 11 is my favorite number mapArgs["-banscore"] = "111"; // because 11 is my favorite number
CAddress addr1(ip(0xa0b0c001)); CAddress addr1(ip(0xa0b0c001));
CNode dummyNode1(INVALID_SOCKET, addr1, true); CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
dummyNode1.Misbehaving(100); dummyNode1.Misbehaving(100);
BOOST_CHECK(!CNode::IsBanned(addr1)); BOOST_CHECK(!CNode::IsBanned(addr1));
dummyNode1.Misbehaving(10); dummyNode1.Misbehaving(10);
@ -67,7 +67,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
SetMockTime(nStartTime); // Overrides future calls to GetTime() SetMockTime(nStartTime); // Overrides future calls to GetTime()
CAddress addr(ip(0xa0b0c001)); CAddress addr(ip(0xa0b0c001));
CNode dummyNode(INVALID_SOCKET, addr, true); CNode dummyNode(INVALID_SOCKET, addr, "", true);
dummyNode.Misbehaving(100); dummyNode.Misbehaving(100);
BOOST_CHECK(CNode::IsBanned(addr)); BOOST_CHECK(CNode::IsBanned(addr));