aboutsummaryrefslogtreecommitdiff
path: root/src/netbase.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2012-04-19 17:38:03 +0200
committerPieter Wuille <pieter.wuille@gmail.com>2012-05-04 16:11:54 +0200
commit9bab521df895c149579b9e64931405c56b008afb (patch)
tree18177aabb6eb11265e3500a75abcdd6bf1150c86 /src/netbase.cpp
parenta012e2db59585bfa1978615aacade281053e0ae5 (diff)
Support connecting by hostnames passed to proxy (-proxydns)
Diffstat (limited to 'src/netbase.cpp')
-rw-r--r--src/netbase.cpp58
1 files changed, 51 insertions, 7 deletions
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);