Changeset View
Changeset View
Standalone View
Standalone View
src/netbase.cpp
Show First 20 Lines • Show All 407 Lines • ▼ Show 20 Lines | static bool Socks5(const std::string &strDest, int port, | ||||
if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket)) { | if (!InterruptibleRecv(pchRet3, 2, SOCKS5_RECV_TIMEOUT, hSocket)) { | ||||
CloseSocket(hSocket); | CloseSocket(hSocket); | ||||
return error("Error reading from proxy"); | return error("Error reading from proxy"); | ||||
} | } | ||||
LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest); | LogPrint(BCLog::NET, "SOCKS5 connected %s\n", strDest); | ||||
return true; | return true; | ||||
} | } | ||||
static bool ConnectSocketDirectly(const CService &addrConnect, | bool ConnectSocketDirectly(const CService &addrConnect, SOCKET &hSocketRet, | ||||
SOCKET &hSocketRet, int nTimeout) { | int nTimeout) { | ||||
hSocketRet = INVALID_SOCKET; | hSocketRet = INVALID_SOCKET; | ||||
struct sockaddr_storage sockaddr; | struct sockaddr_storage sockaddr; | ||||
socklen_t len = sizeof(sockaddr); | socklen_t len = sizeof(sockaddr); | ||||
if (!addrConnect.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | if (!addrConnect.GetSockAddr((struct sockaddr *)&sockaddr, &len)) { | ||||
LogPrintf("Cannot connect to %s: unsupported network\n", | LogPrintf("Cannot connect to %s: unsupported network\n", | ||||
addrConnect.ToString()); | addrConnect.ToString()); | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | |||||
bool IsProxy(const CNetAddr &addr) { | bool IsProxy(const CNetAddr &addr) { | ||||
LOCK(cs_proxyInfos); | LOCK(cs_proxyInfos); | ||||
for (int i = 0; i < NET_MAX; i++) { | for (int i = 0; i < NET_MAX; i++) { | ||||
if (addr == (CNetAddr)proxyInfo[i].proxy) return true; | if (addr == (CNetAddr)proxyInfo[i].proxy) return true; | ||||
} | } | ||||
return false; | return false; | ||||
} | } | ||||
static bool ConnectThroughProxy(const proxyType &proxy, | bool ConnectThroughProxy(const proxyType &proxy, const std::string &strDest, | ||||
const std::string &strDest, int port, | int port, SOCKET &hSocketRet, int nTimeout, | ||||
SOCKET &hSocketRet, int nTimeout, | |||||
bool *outProxyConnectionFailed) { | bool *outProxyConnectionFailed) { | ||||
SOCKET hSocket = INVALID_SOCKET; | SOCKET hSocket = INVALID_SOCKET; | ||||
// first connect to proxy server | // first connect to proxy server | ||||
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) { | if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) { | ||||
if (outProxyConnectionFailed) *outProxyConnectionFailed = true; | if (outProxyConnectionFailed) *outProxyConnectionFailed = true; | ||||
return false; | return false; | ||||
} | } | ||||
// do socks negotiation | // do socks negotiation | ||||
if (proxy.randomize_credentials) { | if (proxy.randomize_credentials) { | ||||
ProxyCredentials random_auth; | ProxyCredentials random_auth; | ||||
static std::atomic_int counter; | static std::atomic_int counter; | ||||
random_auth.username = random_auth.password = | random_auth.username = random_auth.password = | ||||
strprintf("%i", counter++); | strprintf("%i", counter++); | ||||
if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket)) | if (!Socks5(strDest, (unsigned short)port, &random_auth, hSocket)) { | ||||
return false; | |||||
} | |||||
} else if (!Socks5(strDest, (unsigned short)port, 0, hSocket)) { | |||||
return false; | return false; | ||||
} else { | |||||
if (!Socks5(strDest, (unsigned short)port, 0, hSocket)) return false; | |||||
} | } | ||||
hSocketRet = hSocket; | hSocketRet = hSocket; | ||||
return true; | return true; | ||||
} | } | ||||
bool ConnectSocket(const CService &addrDest, SOCKET &hSocketRet, int nTimeout, | |||||
bool *outProxyConnectionFailed) { | |||||
proxyType proxy; | |||||
if (outProxyConnectionFailed) *outProxyConnectionFailed = false; | |||||
if (GetProxy(addrDest.GetNetwork(), proxy)) { | |||||
return ConnectThroughProxy(proxy, addrDest.ToStringIP(), | |||||
addrDest.GetPort(), hSocketRet, nTimeout, | |||||
outProxyConnectionFailed); | |||||
} else { | |||||
// no proxy needed (none set for target network) | |||||
return ConnectSocketDirectly(addrDest, hSocketRet, nTimeout); | |||||
} | |||||
} | |||||
bool ConnectSocketByName(CService &addr, SOCKET &hSocketRet, | |||||
const char *pszDest, int portDefault, int nTimeout, | |||||
bool *outProxyConnectionFailed) { | |||||
std::string strDest; | |||||
int port = portDefault; | |||||
if (outProxyConnectionFailed) *outProxyConnectionFailed = false; | |||||
SplitHostPort(std::string(pszDest), port, strDest); | |||||
proxyType proxy; | |||||
GetNameProxy(proxy); | |||||
std::vector<CService> addrResolved; | |||||
if (Lookup(strDest.c_str(), addrResolved, port, | |||||
fNameLookup && !HaveNameProxy(), 256)) { | |||||
if (addrResolved.size() > 0) { | |||||
addr = addrResolved[GetRand(addrResolved.size())]; | |||||
return ConnectSocket(addr, hSocketRet, nTimeout); | |||||
} | |||||
} | |||||
addr = CService(); | |||||
if (!HaveNameProxy()) return false; | |||||
return ConnectThroughProxy(proxy, strDest, port, hSocketRet, nTimeout, | |||||
outProxyConnectionFailed); | |||||
} | |||||
bool LookupSubNet(const char *pszName, CSubNet &ret) { | bool LookupSubNet(const char *pszName, CSubNet &ret) { | ||||
std::string strSubnet(pszName); | std::string strSubnet(pszName); | ||||
size_t slash = strSubnet.find_last_of('/'); | size_t slash = strSubnet.find_last_of('/'); | ||||
std::vector<CNetAddr> vIP; | std::vector<CNetAddr> vIP; | ||||
std::string strAddress = strSubnet.substr(0, slash); | std::string strAddress = strSubnet.substr(0, slash); | ||||
if (LookupHost(strAddress.c_str(), vIP, 1, false)) { | if (LookupHost(strAddress.c_str(), vIP, 1, false)) { | ||||
CNetAddr network = vIP[0]; | CNetAddr network = vIP[0]; | ||||
▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines |