diff options
author | Philip Kaufmann <phil.kaufmann@t-online.de> | 2012-09-23 12:55:05 +0200 |
---|---|---|
committer | Philip Kaufmann <phil.kaufmann@t-online.de> | 2012-10-04 09:35:24 +0200 |
commit | 81bbef26099eb54f4dcc32e45b5e0416857a330d (patch) | |
tree | ebccf84ff2f56d1f6e02fbb410cc39b65c3fed6b /src/netbase.cpp | |
parent | 0547b02af78dcf2d84e4905b56c7f95d9582b2f9 (diff) |
add LOCK() for proxy related data-structures
- fix #1560 by properly locking proxy related data-structures
- update GetProxy() and introduce GetNameProxy() to be able to use a
thread-safe local copy from proxyInfo and nameproxyInfo
- update usage of GetProxy() all over the source to match the new
behaviour, as it now fills a full proxyType object
- rename GetNameProxy() into HaveNameProxy() to be more clear
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r-- | src/netbase.cpp | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/src/netbase.cpp b/src/netbase.cpp index daa8a8d07e..95d64939ba 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -5,6 +5,7 @@ #include "netbase.h" #include "util.h" +#include "sync.h" #ifndef WIN32 #include <sys/fcntl.h> @@ -16,9 +17,9 @@ using namespace std; // Settings -typedef std::pair<CService, int> proxyType; static proxyType proxyInfo[NET_MAX]; static proxyType nameproxyInfo; +static CCriticalSection cs_proxyInfos; int nConnectTimeout = 5000; bool fNameLookup = false; @@ -432,15 +433,17 @@ bool SetProxy(enum Network net, CService addrProxy, int nSocksVersion) { return false; if (nSocksVersion != 0 && !addrProxy.IsValid()) return false; + LOCK(cs_proxyInfos); proxyInfo[net] = std::make_pair(addrProxy, nSocksVersion); return true; } -bool GetProxy(enum Network net, CService &addrProxy) { +bool GetProxy(enum Network net, proxyType &proxyInfoOut) { assert(net >= 0 && net < NET_MAX); + LOCK(cs_proxyInfos); if (!proxyInfo[net].second) return false; - addrProxy = proxyInfo[net].first; + proxyInfoOut = proxyInfo[net]; return true; } @@ -449,16 +452,27 @@ bool SetNameProxy(CService addrProxy, int nSocksVersion) { return false; if (nSocksVersion != 0 && !addrProxy.IsValid()) return false; + LOCK(cs_proxyInfos); nameproxyInfo = std::make_pair(addrProxy, nSocksVersion); return true; } -bool GetNameProxy() { +bool GetNameProxy(proxyType &nameproxyInfoOut) { + LOCK(cs_proxyInfos); + if (!nameproxyInfo.second) + return false; + nameproxyInfoOut = nameproxyInfo; + return true; +} + +bool HaveNameProxy() { + LOCK(cs_proxyInfos); return nameproxyInfo.second != 0; } bool IsProxy(const CNetAddr &addr) { - for (int i=0; i<NET_MAX; i++) { + LOCK(cs_proxyInfos); + for (int i = 0; i < NET_MAX; i++) { if (proxyInfo[i].second && (addr == (CNetAddr)proxyInfo[i].first)) return true; } @@ -467,10 +481,10 @@ bool IsProxy(const CNetAddr &addr) { bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout) { - const proxyType &proxy = proxyInfo[addrDest.GetNetwork()]; + proxyType proxy; // no proxy needed - if (!proxy.second) + if (!GetProxy(addrDest.GetNetwork(), proxy)) return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout); SOCKET hSocket = INVALID_SOCKET; @@ -504,19 +518,22 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest SplitHostPort(string(pszDest), port, strDest); SOCKET hSocket = INVALID_SOCKET; - CService addrResolved(CNetAddr(strDest, fNameLookup && !nameproxyInfo.second), port); + + proxyType nameproxy; + GetNameProxy(nameproxy); + + CService addrResolved(CNetAddr(strDest, fNameLookup && !nameproxy.second), port); if (addrResolved.IsValid()) { addr = addrResolved; return ConnectSocket(addr, hSocketRet, nTimeout); } addr = CService("0.0.0.0:0"); - if (!nameproxyInfo.second) + if (!nameproxy.second) return false; - if (!ConnectSocketDirectly(nameproxyInfo.first, hSocket, nTimeout)) + if (!ConnectSocketDirectly(nameproxy.first, hSocket, nTimeout)) return false; - switch(nameproxyInfo.second) - { + switch(nameproxy.second) { default: case 4: return false; case 5: |