Skip to content

Commit 6f7d45a

Browse files
committed
Merge pull request #1272 from sipa/ipv6fixes
A few IPv6 fixes
2 parents 22db3f2 + 5a3cb32 commit 6f7d45a

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

src/init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -611,14 +611,14 @@ bool AppInit2(int argc, char* argv[])
611611
std::string strError;
612612
if (mapArgs.count("-bind")) {
613613
BOOST_FOREACH(std::string strBind, mapMultiArgs["-bind"]) {
614-
fBound |= Bind(CService(strBind, GetDefaultPort(), false));
614+
fBound |= Bind(CService(strBind, GetListenPort(), false));
615615
}
616616
} else {
617617
struct in_addr inaddr_any;
618618
inaddr_any.s_addr = INADDR_ANY;
619-
fBound |= Bind(CService(inaddr_any, GetDefaultPort()));
619+
fBound |= Bind(CService(inaddr_any, GetListenPort()));
620620
#ifdef USE_IPV6
621-
fBound |= Bind(CService(in6addr_any, GetDefaultPort()));
621+
fBound |= Bind(CService(in6addr_any, GetListenPort()));
622622
#endif
623623
}
624624
if (!fBound)
@@ -628,7 +628,7 @@ bool AppInit2(int argc, char* argv[])
628628
if (mapArgs.count("-externalip"))
629629
{
630630
BOOST_FOREACH(string strAddr, mapMultiArgs["-externalip"])
631-
AddLocal(CNetAddr(strAddr, fNameLookup), LOCAL_MANUAL);
631+
AddLocal(CService(strAddr, GetListenPort(), fNameLookup), LOCAL_MANUAL);
632632
}
633633

634634
if (mapArgs.count("-paytxfee"))

src/net.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ void ThreadDNSAddressSeed2(void* parg);
3838
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
3939

4040

41+
struct LocalServiceInfo {
42+
int nScore;
43+
int nPort;
44+
};
4145

4246
//
4347
// Global state variables
@@ -46,7 +50,7 @@ bool fClient = false;
4650
static bool fUseUPnP = false;
4751
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
4852
static CCriticalSection cs_mapLocalHost;
49-
static map<CService, int> mapLocalHost;
53+
static map<CNetAddr, LocalServiceInfo> mapLocalHost;
5054
static bool vfReachable[NET_MAX] = {};
5155
static bool vfLimited[NET_MAX] = {};
5256
static CNode* pnodeLocalHost = NULL;
@@ -98,23 +102,23 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
98102
if (fUseProxy || mapArgs.count("-connect") || fNoListen)
99103
return false;
100104

101-
int nBestCount = -1;
105+
int nBestScore = -1;
102106
int nBestReachability = -1;
103107
{
104108
LOCK(cs_mapLocalHost);
105-
for (map<CService, int>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
109+
for (map<CNetAddr, LocalServiceInfo>::iterator it = mapLocalHost.begin(); it != mapLocalHost.end(); it++)
106110
{
107-
int nCount = (*it).second;
111+
int nScore = (*it).second.nScore;
108112
int nReachability = (*it).first.GetReachabilityFrom(paddrPeer);
109-
if (nReachability > nBestReachability || (nReachability == nBestReachability && nCount > nBestCount))
113+
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore))
110114
{
111-
addr = (*it).first;
115+
addr = CService((*it).first, (*it).second.nPort);
112116
nBestReachability = nReachability;
113-
nBestCount = nCount;
117+
nBestScore = nScore;
114118
}
115119
}
116120
}
117-
return nBestCount >= 0;
121+
return nBestScore >= 0;
118122
}
119123

120124
// get best local address for a particular peer as a CAddress
@@ -211,7 +215,12 @@ bool AddLocal(const CService& addr, int nScore)
211215

212216
{
213217
LOCK(cs_mapLocalHost);
214-
mapLocalHost[addr] = std::max(nScore, mapLocalHost[addr]) + (mapLocalHost.count(addr) ? 1 : 0);
218+
bool fAlready = mapLocalHost.count(addr) > 0;
219+
LocalServiceInfo &info = mapLocalHost[addr];
220+
if (!fAlready || nScore >= info.nScore) {
221+
info.nScore = nScore;
222+
info.nPort = addr.GetPort() + (fAlready ? 1 : 0);
223+
}
215224
enum Network net = addr.GetNetwork();
216225
vfReachable[net] = true;
217226
if (net == NET_IPV6) vfReachable[NET_IPV4] = true;
@@ -222,11 +231,9 @@ bool AddLocal(const CService& addr, int nScore)
222231
return true;
223232
}
224233

225-
bool AddLocal(const CNetAddr& addr, int nScore, int port)
234+
bool AddLocal(const CNetAddr &addr, int nScore)
226235
{
227-
if (port == -1)
228-
port = GetListenPort();
229-
return AddLocal(CService(addr, port), nScore);
236+
return AddLocal(CService(addr, GetListenPort()), nScore);
230237
}
231238

232239
/** Make a particular network entirely off-limits (no automatic connects to it) */
@@ -249,7 +256,7 @@ bool SeenLocal(const CService& addr)
249256
LOCK(cs_mapLocalHost);
250257
if (mapLocalHost.count(addr) == 0)
251258
return false;
252-
mapLocalHost[addr]++;
259+
mapLocalHost[addr].nScore++;
253260
}
254261

255262
AdvertizeLocal();
@@ -1887,8 +1894,9 @@ bool StopNode()
18871894
fShutdown = true;
18881895
nTransactionsUpdated++;
18891896
int64 nStart = GetTime();
1890-
for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1891-
semOutbound->post();
1897+
if (semOutbound)
1898+
for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
1899+
semOutbound->post();
18921900
do
18931901
{
18941902
int nThreadsRunning = 0;

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ CNode* FindNode(const CNetAddr& ip);
3838
CNode* FindNode(const CService& ip);
3939
CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL, int64 nTimeout=0);
4040
void MapPort(bool fMapPort);
41+
unsigned short GetListenPort();
4142
bool BindListenPort(const CService &bindAddr, std::string& strError=REF(std::string()));
4243
void StartNode(void* parg);
4344
bool StopNode();
@@ -58,7 +59,7 @@ enum
5859
void SetLimited(enum Network net, bool fLimited = true);
5960
bool IsLimited(const CNetAddr& addr);
6061
bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
61-
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE, int port = -1);
62+
bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
6263
bool SeenLocal(const CService& addr);
6364
bool IsLocal(const CService& addr);
6465
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);

src/netbase.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,14 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
464464
int port = portDefault;
465465

466466
size_t colon = strDest.find_last_of(':');
467-
char *endp = NULL;
468-
int n = strtol(pszDest + colon + 1, &endp, 10);
469-
if (endp && *endp == 0 && n >= 0) {
470-
strDest = strDest.substr(0, colon);
471-
if (n > 0 && n < 0x10000)
472-
port = n;
467+
if (colon != strDest.npos) {
468+
char *endp = NULL;
469+
int n = strtol(pszDest + colon + 1, &endp, 10);
470+
if (endp && *endp == 0 && n >= 0) {
471+
strDest = strDest.substr(0, colon);
472+
if (n > 0 && n < 0x10000)
473+
port = n;
474+
}
473475
}
474476
if (strDest[0] == '[' && strDest[strDest.size()-1] == ']')
475477
strDest = strDest.substr(1, strDest.size()-2);

0 commit comments

Comments
 (0)