aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp111
1 files changed, 97 insertions, 14 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 2d896271f1..8b439efdca 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -56,11 +56,16 @@ map<CInv, int64> mapAlreadyAskedFor;
// Settings
int fUseProxy = false;
+int nConnectTimeout = 5000;
CAddress addrProxy("127.0.0.1",9050);
+unsigned short GetListenPort()
+{
+ return (unsigned short)(GetArg("-port", GetDefaultPort()));
+}
void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
{
@@ -77,7 +82,7 @@ void CNode::PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd)
-bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet)
+bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout)
{
hSocketRet = INVALID_SOCKET;
@@ -89,12 +94,90 @@ bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet)
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
#endif
- bool fRoutable = !(addrConnect.GetByte(3) == 10 || (addrConnect.GetByte(3) == 192 && addrConnect.GetByte(2) == 168));
- bool fProxy = (fUseProxy && fRoutable);
+ bool fProxy = (fUseProxy && addrConnect.IsRoutable());
struct sockaddr_in sockaddr = (fProxy ? addrProxy.GetSockAddr() : addrConnect.GetSockAddr());
+#ifdef __WXMSW__
+ u_long fNonblock = 1;
+ if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
+#else
+ int fFlags = fcntl(hSocket, F_GETFL, 0);
+ if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == -1)
+#endif
+ {
+ closesocket(hSocket);
+ return false;
+ }
+
+
if (connect(hSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
+ // WSAEINVAL is here because some legacy version of winsock uses it
+ if (WSAGetLastError() == WSAEINPROGRESS || WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAEINVAL)
+ {
+ struct timeval timeout;
+ timeout.tv_sec = nTimeout / 1000;
+ timeout.tv_usec = (nTimeout % 1000) * 1000;
+
+ fd_set fdset;
+ FD_ZERO(&fdset);
+ FD_SET(hSocket, &fdset);
+ int nRet = select(hSocket + 1, NULL, &fdset, NULL, &timeout);
+ if (nRet == 0)
+ {
+ printf("connection timeout\n");
+ closesocket(hSocket);
+ return false;
+ }
+ if (nRet == SOCKET_ERROR)
+ {
+ printf("select() for connection failed: %i\n",WSAGetLastError());
+ closesocket(hSocket);
+ return false;
+ }
+ socklen_t nRetSize = sizeof(nRet);
+#ifdef __WXMSW__
+ if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, (char*)(&nRet), &nRetSize) == SOCKET_ERROR)
+#else
+ if (getsockopt(hSocket, SOL_SOCKET, SO_ERROR, &nRet, &nRetSize) == SOCKET_ERROR)
+#endif
+ {
+ printf("getsockopt() for connection failed: %i\n",WSAGetLastError());
+ closesocket(hSocket);
+ return false;
+ }
+ if (nRet != 0)
+ {
+ printf("connect() failed after select(): %i\n",nRet);
+ closesocket(hSocket);
+ return false;
+ }
+ }
+#ifdef __WXMSW__
+ else if (WSAGetLastError() != WSAEISCONN)
+#else
+ else
+#endif
+ {
+ printf("connect() failed: %s\n",WSAGetLastError());
+ closesocket(hSocket);
+ return false;
+ }
+ }
+
+ /*
+ this isn't even strictly necessary
+ CNode::ConnectNode immediately turns the socket back to non-blocking
+ but we'll turn it back to blocking just in case
+ */
+#ifdef __WXMSW__
+ fNonblock = 0;
+ if (ioctlsocket(hSocket, FIONBIO, &fNonblock) == SOCKET_ERROR)
+#else
+ fFlags = fcntl(hSocket, F_GETFL, 0);
+ if (fcntl(hSocket, F_SETFL, fFlags & !O_NONBLOCK) == SOCKET_ERROR)
+#endif
+ {
closesocket(hSocket);
return false;
}
@@ -758,9 +841,12 @@ void ThreadSocketHandler2(void* parg)
if (nSelect == SOCKET_ERROR)
{
int nErr = WSAGetLastError();
- printf("socket select error %d\n", nErr);
- for (int i = 0; i <= hSocketMax; i++)
- FD_SET(i, &fdsetRecv);
+ if (hSocketMax > -1)
+ {
+ printf("socket select error %d\n", nErr);
+ for (int i = 0; i <= hSocketMax; i++)
+ FD_SET(i, &fdsetRecv);
+ }
FD_ZERO(&fdsetSend);
FD_ZERO(&fdsetError);
Sleep(timeout.tv_usec/1000);
@@ -970,7 +1056,7 @@ void ThreadMapPort2(void* parg)
printf("ThreadMapPort started\n");
char port[6];
- sprintf(port, "%d", GetDefaultPort());
+ sprintf(port, "%d", GetListenPort());
const char * rootdescurl = 0;
const char * multicastif = 0;
@@ -1063,7 +1149,7 @@ void DNSAddressSeed()
for (int seed_idx = 0; seed_idx < ARRAYLEN(strDNSSeed); seed_idx++) {
vector<CAddress> vaddr;
- if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, true))
+ if (Lookup(strDNSSeed[seed_idx], vaddr, NODE_NETWORK, -1, true))
{
BOOST_FOREACH (CAddress& addr, vaddr)
{
@@ -1440,14 +1526,11 @@ void ThreadMessageHandler2(void* parg)
-
-
-
bool BindListenPort(string& strError)
{
strError = "";
int nOne = 1;
- addrLocalHost.port = htons(GetDefaultPort());
+ addrLocalHost.port = htons(GetListenPort());
#ifdef __WXMSW__
// Initialize Windows Sockets
@@ -1499,7 +1582,7 @@ bool BindListenPort(string& strError)
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = INADDR_ANY; // bind to all IPs on this computer
- sockaddr.sin_port = htons(GetDefaultPort());
+ sockaddr.sin_port = htons(GetListenPort());
if (::bind(hListenSocket, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
int nErr = WSAGetLastError();
@@ -1561,7 +1644,7 @@ void StartNode(void* parg)
printf("ipv4 %s: %s\n", ifa->ifa_name, pszIP);
// Take the first IP that isn't loopback 127.x.x.x
- CAddress addr(*(unsigned int*)&s4->sin_addr, 0, nLocalServices);
+ CAddress addr(*(unsigned int*)&s4->sin_addr, GetListenPort(), nLocalServices);
if (addr.IsValid() && addr.GetByte(3) != 127)
{
addrLocalHost = addr;