diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/init.cpp | 21 | ||||
-rw-r--r-- | src/net.cpp | 124 | ||||
-rw-r--r-- | src/net.h | 7 | ||||
-rw-r--r-- | src/netbase.cpp | 58 | ||||
-rw-r--r-- | src/netbase.h | 4 |
5 files changed, 145 insertions, 69 deletions
diff --git a/src/init.cpp b/src/init.cpp index aa1399f813..35da20336c 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -181,6 +181,7 @@ bool AppInit2(int argc, char* argv[]) " -proxy=<ip:port> \t " + _("Connect through socks proxy") + "\n" + " -socks=<n> \t " + _("Select the version of socks proxy to use (4 or 5, 5 is default)") + "\n" + " -dns \t " + _("Allow DNS lookups for addnode and connect") + "\n" + + " -proxydns \t " + _("Pass DNS requests to (SOCKS5) proxy") + "\n" + " -port=<port> \t\t " + _("Listen for connections on <port> (default: 8333 or testnet: 18333)") + "\n" + " -maxconnections=<n>\t " + _("Maintain at most <n> connections to peers (default: 125)") + "\n" + " -addnode=<ip> \t " + _("Add a node to connect to and attempt to keep the connection open") + "\n" + @@ -524,13 +525,16 @@ bool AppInit2(int argc, char* argv[]) // Note: the GetBoolArg() calls for all of these must happen later. SoftSetBoolArg("-listen", false); SoftSetBoolArg("-irc", false); - SoftSetBoolArg("-dnsseed", false); + SoftSetBoolArg("-proxydns", true); SoftSetBoolArg("-upnp", false); - SoftSetBoolArg("-dns", false); } - fAllowDNS = GetBoolArg("-dns"); + fNameLookup = GetBoolArg("-dns"); + fProxyNameLookup = GetBoolArg("-proxydns"); + if (fProxyNameLookup) + fNameLookup = true; fNoListen = !GetBoolArg("-listen", true); + nSocksVersion = GetArg("-socks", 5); // Continue to put "/P2SH/" in the coinbase to monitor // BIP16 support. @@ -548,17 +552,6 @@ bool AppInit2(int argc, char* argv[]) } } - if (mapArgs.count("-addnode")) - { - BOOST_FOREACH(string strAddr, mapMultiArgs["-addnode"]) - { - CAddress addr(CService(strAddr, GetDefaultPort(), fAllowDNS)); - addr.nTime = 0; // so it won't relay unless successfully connected - if (addr.IsValid()) - addrman.Add(addr, CNetAddr("127.0.0.1")); - } - } - if (mapArgs.count("-paytxfee")) { if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee)) diff --git a/src/net.cpp b/src/net.cpp index 92b4a3173f..ca137b70fa 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -35,7 +35,7 @@ void ThreadOpenAddedConnections2(void* parg); void ThreadMapPort2(void* parg); #endif void ThreadDNSAddressSeed2(void* parg); -bool OpenNetworkConnection(const CAddress& addrConnect); +bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest = NULL); @@ -43,7 +43,6 @@ bool OpenNetworkConnection(const CAddress& addrConnect); // Global state variables // bool fClient = false; -bool fAllowDNS = false; static bool fUseUPnP = false; uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); CAddress addrLocalHost(CService("0.0.0.0", 0), nLocalServices); @@ -307,6 +306,15 @@ CNode* FindNode(const CNetAddr& ip) return NULL; } +CNode* FindNode(std::string addrName) +{ + LOCK(cs_vNodes); + BOOST_FOREACH(CNode* pnode, vNodes) + if (pnode->addrName == addrName) + return (pnode); + return NULL; +} + CNode* FindNode(const CService& addr) { { @@ -318,35 +326,38 @@ CNode* FindNode(const CService& addr) return NULL; } -CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) +CNode* ConnectNode(CAddress addrConnect, const char *pszDest, int64 nTimeout) { - if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) - return NULL; + if (pszDest != NULL) { + if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost) + return NULL; - // Look for an existing connection - CNode* pnode = FindNode((CService)addrConnect); - if (pnode) - { - if (nTimeout != 0) - pnode->AddRef(nTimeout); - else - pnode->AddRef(); - return pnode; + // Look for an existing connection + CNode* pnode = FindNode((CService)addrConnect); + if (pnode) + { + if (nTimeout != 0) + pnode->AddRef(nTimeout); + else + pnode->AddRef(); + return pnode; + } } + /// debug print printf("trying connection %s lastseen=%.1fhrs\n", - addrConnect.ToString().c_str(), - (double)(addrConnect.nTime - GetAdjustedTime())/3600.0); - - addrman.Attempt(addrConnect); + pszDest ? pszDest : addrConnect.ToString().c_str(), + pszDest ? 0 : (double)(addrConnect.nTime - GetAdjustedTime())/3600.0); // Connect SOCKET hSocket; - if (ConnectSocket(addrConnect, hSocket)) + if (pszDest ? ConnectSocketByName(addrConnect, hSocket, pszDest, GetDefaultPort()) : ConnectSocket(addrConnect, hSocket)) { + addrman.Attempt(addrConnect); + /// debug print - printf("connected %s\n", addrConnect.ToString().c_str()); + printf("connected %s\n", pszDest ? pszDest : addrConnect.ToString().c_str()); // Set to nonblocking #ifdef WIN32 @@ -359,11 +370,12 @@ CNode* ConnectNode(CAddress addrConnect, int64 nTimeout) #endif // Add node - CNode* pnode = new CNode(hSocket, addrConnect, false); + CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false); if (nTimeout != 0) pnode->AddRef(nTimeout); else pnode->AddRef(); + { LOCK(cs_vNodes); vNodes.push_back(pnode); @@ -389,7 +401,7 @@ void CNode::CloseSocketDisconnect() { if (fDebug) printf("%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); - printf("disconnecting node %s\n", addr.ToString().c_str()); + printf("disconnecting node %s\n", addrName.c_str()); closesocket(hSocket); hSocket = INVALID_SOCKET; vRecv.clear(); @@ -444,7 +456,7 @@ bool CNode::Misbehaving(int howmuch) { if (addr.IsLocal()) { - printf("Warning: local node %s misbehaving\n", addr.ToString().c_str()); + printf("Warning: local node %s misbehaving\n", addrName.c_str()); return false; } @@ -458,7 +470,7 @@ bool CNode::Misbehaving(int howmuch) setBanned[addr] = banTime; } CloseSocketDisconnect(); - printf("Disconnected %s for misbehavior (score=%d)\n", addr.ToString().c_str(), nMisbehavior); + printf("Disconnected %s for misbehavior (score=%d)\n", addrName.c_str(), nMisbehavior); return true; } return false; @@ -675,7 +687,7 @@ void ThreadSocketHandler2(void* parg) else { printf("accepted connection %s\n", addr.ToString().c_str()); - CNode* pnode = new CNode(hSocket, addr, true); + CNode* pnode = new CNode(hSocket, addr, "", true); pnode->AddRef(); { LOCK(cs_vNodes); @@ -1025,20 +1037,25 @@ void ThreadDNSAddressSeed2(void* parg) printf("Loading addresses from DNS seeds (could take a while)\n"); for (unsigned int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) { - vector<CNetAddr> vaddr; - vector<CAddress> vAdd; - if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) - { - BOOST_FOREACH(CNetAddr& ip, vaddr) + if (fProxyNameLookup) { + CAddress addr; + OpenNetworkConnection(addr, strDNSSeed[seed_idx][1]); + } else { + vector<CNetAddr> vaddr; + vector<CAddress> vAdd; + if (LookupHost(strDNSSeed[seed_idx][1], vaddr)) { - int nOneDay = 24*3600; - CAddress addr = CAddress(CService(ip, GetDefaultPort())); - addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old - vAdd.push_back(addr); - found++; + BOOST_FOREACH(CNetAddr& ip, vaddr) + { + int nOneDay = 24*3600; + CAddress addr = CAddress(CService(ip, GetDefaultPort())); + addr.nTime = GetTime() - 3*nOneDay - GetRand(4*nOneDay); // use a random age between 3 and 7 days old + vAdd.push_back(addr); + found++; + } } + addrman.Add(vAdd, CNetAddr(strDNSSeed[seed_idx][0], true)); } - addrman.Add(vAdd, CNetAddr(strDNSSeed[seed_idx][0], true)); } } @@ -1199,9 +1216,8 @@ void ThreadOpenConnections2(void* parg) { BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"]) { - CAddress addr(CService(strAddr, GetDefaultPort(), fAllowDNS)); - if (addr.IsValid()) - OpenNetworkConnection(addr); + CAddress addr(CService("0.0.0.0:0")); + OpenNetworkConnection(addr, strAddr.c_str()); for (int i = 0; i < 10 && i < nLoop; i++) { Sleep(500); @@ -1325,11 +1341,25 @@ void ThreadOpenAddedConnections2(void* parg) if (mapArgs.count("-addnode") == 0) return; + if (fProxyNameLookup) { + while(!fShutdown) { + BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) { + CAddress addr; + OpenNetworkConnection(addr, strAddNode.c_str()); + Sleep(500); + } + vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; + Sleep(120000); // Retry every 2 minutes + vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; + } + return; + } + vector<vector<CService> > vservAddressesToAdd(0); BOOST_FOREACH(string& strAddNode, mapMultiArgs["-addnode"]) { vector<CService> vservNode(0); - if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fAllowDNS, 0)) + if(Lookup(strAddNode.c_str(), vservNode, GetDefaultPort(), fNameLookup, 0)) { vservAddressesToAdd.push_back(vservNode); { @@ -1343,7 +1373,7 @@ void ThreadOpenAddedConnections2(void* parg) { vector<vector<CService> > vservConnectAddresses = vservAddressesToAdd; // Attempt to connect to each IP for each addnode entry until at least one is successful per addnode entry - // (keeping in mind that addnode entries can have many IPs if fAllowDNS) + // (keeping in mind that addnode entries can have many IPs if fNameLookup) { LOCK(cs_vNodes); BOOST_FOREACH(CNode* pnode, vNodes) @@ -1373,19 +1403,23 @@ void ThreadOpenAddedConnections2(void* parg) } } -bool OpenNetworkConnection(const CAddress& addrConnect) +bool OpenNetworkConnection(const CAddress& addrConnect, const char *strDest) { // // Initiate outbound network connection // if (fShutdown) return false; - if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || - FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect)) + if (!strDest) + if ((CNetAddr)addrConnect == (CNetAddr)addrLocalHost || !addrConnect.IsIPv4() || + FindNode((CNetAddr)addrConnect) || CNode::IsBanned(addrConnect) || + FindNode(addrConnect.ToStringIPPort().c_str())) + return false; + if (strDest && FindNode(strDest)) return false; vnThreadsRunning[THREAD_OPENCONNECTIONS]--; - CNode* pnode = ConnectNode(addrConnect); + CNode* pnode = ConnectNode(addrConnect, strDest); vnThreadsRunning[THREAD_OPENCONNECTIONS]++; if (fShutdown) return false; @@ -35,7 +35,7 @@ bool GetMyExternalIP(CNetAddr& ipRet); void AddressCurrentlyConnected(const CService& addr); CNode* FindNode(const CNetAddr& ip); CNode* FindNode(const CService& ip); -CNode* ConnectNode(CAddress addrConnect, int64 nTimeout=0); +CNode* ConnectNode(CAddress addrConnect, const char *strDest = NULL, int64 nTimeout=0); void MapPort(bool fMapPort); bool BindListenPort(std::string& strError=REF(std::string())); void StartNode(void* parg); @@ -83,7 +83,6 @@ enum threadId }; extern bool fClient; -extern bool fAllowDNS; extern uint64 nLocalServices; extern CAddress addrLocalHost; extern uint64 nLocalHostNonce; @@ -120,6 +119,7 @@ public: int nHeaderStart; unsigned int nMessageStart; CAddress addr; + std::string addrName; int nVersion; std::string strSubVer; bool fClient; @@ -157,7 +157,7 @@ public: CCriticalSection cs_inventory; std::multimap<int64, CInv> mapAskFor; - CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false) : vSend(SER_NETWORK, MIN_PROTO_VERSION), vRecv(SER_NETWORK, MIN_PROTO_VERSION) + CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false) : vSend(SER_NETWORK, MIN_PROTO_VERSION), vRecv(SER_NETWORK, MIN_PROTO_VERSION) { nServices = 0; hSocket = hSocketIn; @@ -168,6 +168,7 @@ public: nHeaderStart = -1; nMessageStart = -1; addr = addrIn; + addrName = addrNameIn == "" ? addr.ToStringIPPort() : addrNameIn; nVersion = 0; strSubVer = ""; fClient = false; // set by version message diff --git a/src/netbase.cpp b/src/netbase.cpp index 60f34bbd30..a9cc0cf4d3 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -15,7 +15,10 @@ using namespace std; // Settings +int nSocksVersion = 5; int fUseProxy = false; +bool fProxyNameLookup = false; +bool fNameLookup = false; CService addrProxy("127.0.0.1",9050); int nConnectTimeout = 5000; @@ -310,12 +313,12 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int)); #endif - bool fProxy = (fUseProxy && addrDest.IsRoutable()); struct sockaddr_in sockaddr; - if (fProxy) - addrProxy.GetSockAddr(&sockaddr); - else - addrDest.GetSockAddr(&sockaddr); + if (!addrConnect.GetSockAddr(&sockaddr)) + { + closesocket(hSocket); + return false; + } #ifdef WIN32 u_long fNonblock = 1; @@ -329,7 +332,6 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe return false; } - if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR) { // WSAEINVAL is here because some legacy version of winsock uses it @@ -414,7 +416,7 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout) if (fProxy) { - switch(GetArg("-socks", 5)) + switch(nSocksVersion) { case 4: if (!Socks4(addrDest, hSocket)) @@ -433,6 +435,48 @@ bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout) return true; } +bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout) +{ + string strDest(pszDest); + int port = portDefault; + + size_t colon = strDest.find_last_of(':'); + char *endp = NULL; + int n = strtol(pszDest + colon + 1, &endp, 10); + if (endp && *endp == 0 && n >= 0) { + strDest = strDest.substr(0, colon); + if (n > 0 && n < 0x10000) + port = n; + } + if (strDest[0] == '[' && strDest[strDest.size()-1] == ']') + strDest = strDest.substr(1, strDest.size()-2); + + SOCKET hSocket = INVALID_SOCKET; + CService addrResolved(CNetAddr(strDest, fNameLookup && !fProxyNameLookup), port); + if (addrResolved.IsValid()) { + addr = addrResolved; + return ConnectSocket(addr, hSocketRet, nTimeout); + } + addr = CService("0.0.0.0:0"); + if (!fNameLookup) + return false; + if (!ConnectSocketDirectly(addrProxy, hSocket, nTimeout)) + return false; + + switch(nSocksVersion) + { + case 4: return false; + case 5: + default: + if (!Socks5(strDest, port, hSocket)) + return false; + break; + } + + hSocketRet = hSocket; + return true; +} + void CNetAddr::Init() { memset(ip, 0, 16); diff --git a/src/netbase.h b/src/netbase.h index 00b6850b2a..3d29569069 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -119,9 +119,13 @@ bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllo bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout); +bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault = 0, int nTimeout = nConnectTimeout); // Settings +extern int nSocksVersion; extern int fUseProxy; +extern bool fProxyNameLookup; +extern bool fNameLookup; extern CService addrProxy; #endif |