Skip to content

Commit 478b01d

Browse files
committed
Add -seednode connections, and use this for -dnsseed + -proxydns
1 parent 9bab521 commit 478b01d

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/init.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ bool AppInit2(int argc, char* argv[])
180180
" -timeout=<n> \t " + _("Specify connection timeout (in milliseconds)") + "\n" +
181181
" -proxy=<ip:port> \t " + _("Connect through socks proxy") + "\n" +
182182
" -socks=<n> \t " + _("Select the version of socks proxy to use (4 or 5, 5 is default)") + "\n" +
183-
" -dns \t " + _("Allow DNS lookups for addnode and connect") + "\n" +
183+
" -dns \t " + _("Allow DNS lookups for -addnode, -seednode and -connect") + "\n" +
184184
" -proxydns \t " + _("Pass DNS requests to (SOCKS5) proxy") + "\n" +
185185
" -port=<port> \t\t " + _("Listen for connections on <port> (default: 8333 or testnet: 18333)") + "\n" +
186186
" -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" +
187187
" -addnode=<ip> \t " + _("Add a node to connect to and attempt to keep the connection open") + "\n" +
188188
" -connect=<ip> \t\t " + _("Connect only to the specified node") + "\n" +
189+
" -seednode=<ip> \t\t " + _("Connect to a node to retrieve peer addresses, and disconnect") + "\n" +
189190
" -irc \t " + _("Find peers using internet relay chat (default: 0)") + "\n" +
190191
" -listen \t " + _("Accept connections from outside (default: 1)") + "\n" +
191192
#ifdef QT_GUI
@@ -536,6 +537,9 @@ bool AppInit2(int argc, char* argv[])
536537
fNoListen = !GetBoolArg("-listen", true);
537538
nSocksVersion = GetArg("-socks", 5);
538539

540+
BOOST_FOREACH(string strDest, mapMultiArgs["-seednode"])
541+
AddOneShot(strDest);
542+
539543
// Continue to put "/P2SH/" in the coinbase to monitor
540544
// BIP16 support.
541545
// This can be removed eventually...

src/main.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,7 +2264,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
22642264
}
22652265

22662266
// Get recent addresses
2267-
if (pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
2267+
if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
22682268
{
22692269
pfrom->PushMessage("getaddr");
22702270
pfrom->fGetAddr = true;
@@ -2280,7 +2280,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
22802280

22812281
// Ask the first connected node for block updates
22822282
static int nAskedForBlocks = 0;
2283-
if (!pfrom->fClient &&
2283+
if (!pfrom->fClient && !pfrom->fOneShot &&
22842284
(pfrom->nVersion < NOBLKS_VERSION_START ||
22852285
pfrom->nVersion >= NOBLKS_VERSION_END) &&
22862286
(nAskedForBlocks < 1 || vNodes.size() <= 1))
@@ -2378,6 +2378,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
23782378
addrman.Add(vAddr, pfrom->addr, 2 * 60 * 60);
23792379
if (vAddr.size() < 1000)
23802380
pfrom->fGetAddr = false;
2381+
if (pfrom->fOneShot)
2382+
pfrom->fDisconnect = true;
23812383
}
23822384

23832385

src/net.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg);
3535
void ThreadMapPort2(void* parg);
3636
#endif
3737
void ThreadDNSAddressSeed2(void* parg);
38-
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NULL);
38+
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NULL, bool fOneShot = false);
3939

4040

4141

@@ -59,6 +59,8 @@ deque<pair<int64, CInv> > vRelayExpiration;
5959
CCriticalSection cs_mapRelay;
6060
map<CInv, int64> mapAlreadyAskedFor;
6161

62+
static deque<string> vOneShots;
63+
CCriticalSection cs_vOneShots;
6264

6365
set<CNetAddr> setservAddNodeAddresses;
6466
CCriticalSection cs_setservAddNodeAddresses;
@@ -68,6 +70,12 @@ static int nOutbound = 0;
6870
static CConditionVariable condOutbound;
6971

7072

73+
void AddOneShot(string strDest)
74+
{
75+
LOCK(cs_vOneShots);
76+
vOneShots.push_back(strDest);
77+
}
78+
7179
unsigned short GetListenPort()
7280
{
7381
return (unsigned short)(GetArg("-port", GetDefaultPort()));
@@ -328,7 +336,7 @@ CNode* FindNode(const CService& addr)
328336

329337
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout)
330338
{
331-
if (pszDest != NULL) {
339+
if (pszDest == NULL) {
332340
if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost)
333341
return NULL;
334342

@@ -1038,8 +1046,7 @@ void ThreadDNSAddressSeed2(void* parg)
10381046

10391047
for (unsigned int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
10401048
if (fProxyNameLookup) {
1041-
CAddress addr;
1042-
OpenNetworkConnection(addr, strDNSSeed[seed_idx][1]);
1049+
AddOneShot(strDNSSeed[seed_idx][1]);
10431050
} else {
10441051
vector<CNetAddr> vaddr;
10451052
vector<CAddress> vAdd;
@@ -1205,6 +1212,21 @@ void ThreadOpenConnections(void* parg)
12051212
printf("ThreadOpenConnections exiting\n");
12061213
}
12071214

1215+
void static ProcessOneShot()
1216+
{
1217+
string strDest;
1218+
{
1219+
LOCK(cs_vOneShots);
1220+
if (vOneShots.empty())
1221+
return;
1222+
strDest = vOneShots.front();
1223+
vOneShots.pop_front();
1224+
}
1225+
CAddress addr;
1226+
if (!OpenNetworkConnection(addr, strDest.c_str(), true))
1227+
AddOneShot(strDest);
1228+
}
1229+
12081230
void ThreadOpenConnections2(void* parg)
12091231
{
12101232
printf("ThreadOpenConnections started\n");
@@ -1214,9 +1236,10 @@ void ThreadOpenConnections2(void* parg)
12141236
{
12151237
for (int64 nLoop = 0;; nLoop++)
12161238
{
1239+
ProcessOneShot();
12171240
BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
12181241
{
1219-
CAddress addr(CService("0.0.0.0:0"));
1242+
CAddress addr;
12201243
OpenNetworkConnection(addr, strAddr.c_str());
12211244
for (int i = 0; i < 10 && i < nLoop; i++)
12221245
{
@@ -1232,6 +1255,8 @@ void ThreadOpenConnections2(void* parg)
12321255
int64 nStart = GetTime();
12331256
loop
12341257
{
1258+
ProcessOneShot();
1259+
12351260
vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
12361261
Sleep(500);
12371262
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
@@ -1403,7 +1428,7 @@ void ThreadOpenAddedConnections2(void* parg)
14031428
}
14041429
}
14051430

1406-
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest)
1431+
bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest, bool fOneShot)
14071432
{
14081433
//
14091434
// Initiate outbound network connection
@@ -1426,6 +1451,8 @@ bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest)
14261451
if (!pnode)
14271452
return false;
14281453
pnode->fNetworkNode = true;
1454+
if (fOneShot)
1455+
pnode->fOneShot = true;
14291456

14301457
return true;
14311458
}

src/net.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern int nBestHeight;
3030
inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); }
3131
inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); }
3232

33+
void AddOneShot(std::string strDest);
3334
bool RecvLine(SOCKET hSocket, std::string& strLine);
3435
bool GetMyExternalIP(CNetAddr& ipRet);
3536
void AddressCurrentlyConnected(const CService& addr);
@@ -122,6 +123,7 @@ class CNode
122123
std::string addrName;
123124
int nVersion;
124125
std::string strSubVer;
126+
bool fOneShot;
125127
bool fClient;
126128
bool fInbound;
127129
bool fNetworkNode;
@@ -171,6 +173,7 @@ class CNode
171173
addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn;
172174
nVersion = 0;
173175
strSubVer = "";
176+
fOneShot = false;
174177
fClient = false; // set by version message
175178
fInbound = fInboundIn;
176179
fNetworkNode = false;

0 commit comments

Comments
 (0)