From 9bab521df895c149579b9e64931405c56b008afb Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 19 Apr 2012 17:38:03 +0200 Subject: Support connecting by hostnames passed to proxy (-proxydns) --- src/netbase.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) (limited to 'src/netbase.cpp') 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); -- cgit v1.2.3