Skip to content

Commit 7fa4443

Browse files
committed
Keep port information for local addresses
1 parent 457754d commit 7fa4443

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

src/irc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void ThreadIRCSeed2(void* parg)
246246
return;
247247
}
248248

249-
CNetAddr addrLocal;
249+
CService addrLocal;
250250
string strMyName;
251251
if (GetLocal(addrLocal, &addrConnect))
252252
strMyName = EncodeAddress(GetLocalAddress(&addrConnect));

src/net.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ bool fClient = false;
4646
static bool fUseUPnP = false;
4747
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
4848
static CCriticalSection cs_mapLocalHost;
49-
static map<CNetAddr, int> mapLocalHost;
49+
static map<CService, int> mapLocalHost;
5050
static bool vfReachable[NET_MAX] = {};
5151
static bool vfLimited[NET_MAX] = {};
5252
static CNode* pnodeLocalHost = NULL;
@@ -96,7 +96,7 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
9696
}
9797

9898
// find 'best' local address for a particular peer
99-
bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
99+
bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
100100
{
101101
if (fUseProxy || mapArgs.count("-connect") || fNoListen)
102102
return false;
@@ -105,7 +105,7 @@ bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
105105
int nBestReachability = -1;
106106
{
107107
LOCK(cs_mapLocalHost);
108-
for (map<CNetAddr, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
108+
for (map<CService, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
109109
{
110110
int nCount = (*it).second;
111111
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
@@ -124,11 +124,10 @@ bool GetLocal(CNetAddr& addr, const CNetAddr *paddrPeer)
124124
CAddress GetLocalAddress(const CNetAddr *paddrPeer)
125125
{
126126
CAddress ret(CService("0.0.0.0",0),0);
127-
CNetAddr addr;
127+
CService addr;
128128
if (GetLocal(addr, paddrPeer))
129129
{
130-
ret.SetIP(addr);
131-
ret.SetPort(GetListenPort());
130+
ret = CAddress(addr);
132131
ret.nServices = nLocalServices;
133132
ret.nTime = GetAdjustedTime();
134133
}
@@ -196,7 +195,7 @@ void static AdvertizeLocal()
196195
if (pnode->fSuccessfullyConnected)
197196
{
198197
CAddress addrLocal = GetLocalAddress(&pnode->addr);
199-
if (addrLocal.IsRoutable() && (CNetAddr)addrLocal != (CNetAddr)pnode->addrLocal)
198+
if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal)
200199
{
201200
pnode->PushAddress(addrLocal);
202201
pnode->addrLocal = addrLocal;
@@ -206,7 +205,7 @@ void static AdvertizeLocal()
206205
}
207206

208207
// learn a new local address
209-
bool AddLocal(const CNetAddr& addr, int nScore)
208+
bool AddLocal(const CService& addr, int nScore)
210209
{
211210
if (!addr.IsRoutable())
212211
return false;
@@ -226,6 +225,13 @@ bool AddLocal(const CNetAddr& addr, int nScore)
226225
return true;
227226
}
228227

228+
bool AddLocal(const CNetAddr& addr, int nScore, int port)
229+
{
230+
if (port == -1)
231+
port = GetListenPort();
232+
return AddLocal(CService(addr, port), nScore);
233+
}
234+
229235
/** Make a particular network entirely off-limits (no automatic connects to it) */
230236
void SetLimited(enum Network net, bool fLimited)
231237
{
@@ -240,7 +246,7 @@ bool IsLimited(const CNetAddr& addr)
240246
}
241247

242248
/** vote for a local address */
243-
bool SeenLocal(const CNetAddr& addr)
249+
bool SeenLocal(const CService& addr)
244250
{
245251
{
246252
LOCK(cs_mapLocalHost);
@@ -255,7 +261,7 @@ bool SeenLocal(const CNetAddr& addr)
255261
}
256262

257263
/** check whether a given address is potentially local */
258-
bool IsLocal(const CNetAddr& addr)
264+
bool IsLocal(const CService& addr)
259265
{
260266
LOCK(cs_mapLocalHost);
261267
return mapLocalHost.count(addr) > 0;

src/net.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,23 @@ bool StopNode();
4444

4545
enum
4646
{
47-
LOCAL_NONE,
48-
LOCAL_IF,
49-
LOCAL_UPNP,
50-
LOCAL_IRC,
51-
LOCAL_HTTP,
52-
LOCAL_MANUAL,
47+
LOCAL_NONE, // unknown
48+
LOCAL_IF, // address a local interface listens on
49+
LOCAL_UPNP, // address reported by UPnP
50+
LOCAL_IRC, // address reported by IRC (deprecated)
51+
LOCAL_HTTP, // address reported by whatismyip.com and similars
52+
LOCAL_MANUAL, // address explicitly specified (-externalip=)
5353

5454
LOCAL_MAX
5555
};
5656

5757
void SetLimited(enum Network net, bool fLimited = true);
5858
bool IsLimited(const CNetAddr& addr);
59-
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
60-
bool SeenLocal(const CNetAddr& addr);
61-
bool IsLocal(const CNetAddr& addr);
62-
bool GetLocal(CNetAddr &addr, const CNetAddr *paddrPeer = NULL);
59+
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
60+
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE, int port = -1);
61+
bool SeenLocal(const CService& addr);
62+
bool IsLocal(const CService& addr);
63+
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
6364
bool IsReachable(const CNetAddr &addr);
6465
CAddress GetLocalAddress(const CNetAddr *paddrPeer = NULL);
6566

@@ -142,7 +143,7 @@ class CNode
142143
unsigned int nMessageStart;
143144
CAddress addr;
144145
std::string addrName;
145-
CNetAddr addrLocal;
146+
CService addrLocal;
146147
int nVersion;
147148
std::string strSubVer;
148149
bool fOneShot;

0 commit comments

Comments
 (0)