Keep port information for local addresses

This commit is contained in:
Pieter Wuille 2012-05-10 20:35:13 +02:00
parent 457754d2c2
commit 7fa4443f77
3 changed files with 29 additions and 22 deletions

View File

@ -246,7 +246,7 @@ void ThreadIRCSeed2(void* parg)
return; return;
} }
CNetAddr addrLocal; CService addrLocal;
string strMyName; string strMyName;
if (GetLocal(addrLocal, &addrConnect)) if (GetLocal(addrLocal, &addrConnect))
strMyName = EncodeAddress(GetLocalAddress(&addrConnect)); strMyName = EncodeAddress(GetLocalAddress(&addrConnect));

View File

@ -46,7 +46,7 @@ bool fClient = false;
static bool fUseUPnP = false; static bool fUseUPnP = false;
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
static CCriticalSection cs_mapLocalHost; static CCriticalSection cs_mapLocalHost;
static map<CNetAddr, int> mapLocalHost; static map<CService, int> mapLocalHost;
static bool vfReachable[NET_MAX] = {}; static bool vfReachable[NET_MAX] = {};
static bool vfLimited[NET_MAX] = {}; static bool vfLimited[NET_MAX] = {};
static CNode* pnodeLocalHost = NULL; static CNode* pnodeLocalHost = NULL;
@ -96,7 +96,7 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
} }
// find 'best' local address for a particular peer // find 'best' local address for a particular peer
bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer) bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
{ {
if (fUseProxy || mapArgs.count("-connect") || fNoListen) if (fUseProxy || mapArgs.count("-connect") || fNoListen)
return false; return false;
@ -105,7 +105,7 @@ bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
int nBestReachability = -1; int nBestReachability = -1;
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
for (map<CNetAddr, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++) for (map<CService, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
{ {
int nCount = (*it).second; int nCount = (*it).second;
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer); int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
@ -124,11 +124,10 @@ bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
CAddress GetLocalAddress(const CNetAddr *paddrPeer) CAddress GetLocalAddress(const CNetAddr *paddrPeer)
{ {
CAddress ret(CService("0.0.0.0",0),0); CAddress ret(CService("0.0.0.0",0),0);
CNetAddr addr; CService addr;
if (GetLocal(addr, paddrPeer)) if (GetLocal(addr, paddrPeer))
{ {
ret.SetIP(addr); ret = CAddress(addr);
ret.SetPort(GetListenPort());
ret.nServices = nLocalServices; ret.nServices = nLocalServices;
ret.nTime = GetAdjustedTime(); ret.nTime = GetAdjustedTime();
} }
@ -196,7 +195,7 @@ void static AdvertizeLocal()
if (pnode->fSuccessfullyConnected) if (pnode->fSuccessfullyConnected)
{ {
CAddress addrLocal = GetLocalAddress(&pnode->addr); CAddress addrLocal = GetLocalAddress(&pnode->addr);
if (addrLocal.IsRoutable() && (CNetAddr)addrLocal != (CNetAddr)pnode->addrLocal) if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal)
{ {
pnode->PushAddress(addrLocal); pnode->PushAddress(addrLocal);
pnode->addrLocal = addrLocal; pnode->addrLocal = addrLocal;
@ -206,7 +205,7 @@ void static AdvertizeLocal()
} }
// learn a new local address // learn a new local address
bool AddLocal(const CNetAddr& addr, int nScore) bool AddLocal(const CService& addr, int nScore)
{ {
if (!addr.IsRoutable()) if (!addr.IsRoutable())
return false; return false;
@ -226,6 +225,13 @@ bool AddLocal(const CNetAddr& addr, int nScore)
return true; return true;
} }
bool AddLocal(const CNetAddr& addr, int nScore, int port)
{
if (port == -1)
port = GetListenPort();
return AddLocal(CService(addr, port), nScore);
}
/** Make a particular network entirely off-limits (no automatic connects to it) */ /** Make a particular network entirely off-limits (no automatic connects to it) */
void SetLimited(enum Network net, bool fLimited) void SetLimited(enum Network net, bool fLimited)
{ {
@ -240,7 +246,7 @@ bool IsLimited(const CNetAddr& addr)
} }
/** vote for a local address */ /** vote for a local address */
bool SeenLocal(const CNetAddr& addr) bool SeenLocal(const CService& addr)
{ {
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
@ -255,7 +261,7 @@ bool SeenLocal(const CNetAddr& addr)
} }
/** check whether a given address is potentially local */ /** check whether a given address is potentially local */
bool IsLocal(const CNetAddr& addr) bool IsLocal(const CService& addr)
{ {
LOCK(cs_mapLocalHost); LOCK(cs_mapLocalHost);
return mapLocalHost.count(addr) > 0; return mapLocalHost.count(addr) > 0;

View File

@ -44,22 +44,23 @@ bool StopNode();
enum enum
{ {
LOCAL_NONE, LOCAL_NONE, // unknown
LOCAL_IF, LOCAL_IF, // address a local interface listens on
LOCAL_UPNP, LOCAL_UPNP, // address reported by UPnP
LOCAL_IRC, LOCAL_IRC, // address reported by IRC (deprecated)
LOCAL_HTTP, LOCAL_HTTP, // address reported by whatismyip.com and similars
LOCAL_MANUAL, LOCAL_MANUAL, // address explicitly specified (-externalip=)
LOCAL_MAX LOCAL_MAX
}; };
void SetLimited(enum Network net, bool fLimited = true); void SetLimited(enum Network net, bool fLimited = true);
bool IsLimited(const CNetAddr& addr); bool IsLimited(const CNetAddr& addr);
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE); bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
bool SeenLocal(const CNetAddr& addr); bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE, int port = -1);
bool IsLocal(const CNetAddr& addr); bool SeenLocal(const CService& addr);
bool GetLocal(CNetAddr &addr, const CNetAddr *paddrPeer = NULL); bool IsLocal(const CService& addr);
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
bool IsReachable(const CNetAddr &addr); bool IsReachable(const CNetAddr &addr);
CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL); CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
@ -142,7 +143,7 @@ public:
unsigned int nMessageStart; unsigned int nMessageStart;
CAddress addr; CAddress addr;
std::string addrName; std::string addrName;
CNetAddr addrLocal; CService addrLocal;
int nVersion; int nVersion;
std::string strSubVer; std::string strSubVer;
bool fOneShot; bool fOneShot;