Ejemplo n.º 1
0
bool CMasternode::IsValidNetAddr(CService addrIn)
{
    // TODO: regtest is fine with any addresses for now,
    // should probably be a bit smarter if one day we start to implement tests for this
    return Params().NetworkIDString() == CBaseChainParams::REGTEST ||
            (addrIn.IsIPv4() && IsReachable(addrIn) && addrIn.IsRoutable());
}
Ejemplo n.º 2
0
bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
{
    SOCKET hSocket = INVALID_SOCKET;
    bool fProxy = (fUseProxy && addrDest.IsRoutable() && !vfNoProxy[addrDest.GetNetwork()]);

    if (!ConnectSocketDirectly(fProxy ? addrProxy : addrDest, hSocket, nTimeout))
        return false;

    if (fProxy)
    {
        switch(nSocksVersion)
        {
            case 4:
                if (!Socks4(addrDest, hSocket))
                    return false;
                break;

            case 5:
            default:
                if (!Socks5(addrDest.ToStringIP(), addrDest.GetPort(), hSocket))
                    return false;
                break;
        }
    }

    hSocketRet = hSocket;
    return true;
}
Ejemplo n.º 3
0
// learn a new local address
bool AddLocal(const CService &addr, int nScore)
{
	if (!addr.IsRoutable())
		return false;

	if (!fDiscover && nScore < LOCAL_MANUAL)
		return false;

	if (IsLimited(addr))
		return false;

	printf("AddLocal(%s,%i)\n", addr.ToString().c_str(), nScore);

	{
		LOCK(cs_mapLocalHost);
		bool fAlready = mapLocalHost.count(addr) > 0;
		LocalServiceInfo &info = mapLocalHost[addr];
		if (!fAlready || nScore >= info.nScore) {
			info.nScore = nScore;
			info.nPort = addr.GetPort() + (fAlready ? 1 : 0);
		}
		SetReachable(addr.GetNetwork());
	}

	AdvertizeLocal();

	return true;
}
Ejemplo n.º 4
0
	bool ConnectSocket(const CService &addrDest, SOCKET& hSocketRet, int nTimeout)
	{
		hSocketRet = INVALID_SOCKET;

		SOCKET hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
		if (hSocket == INVALID_SOCKET)
			return false;
	#ifdef SO_NOSIGPIPE
		int set = 1;
		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);

	#ifdef WIN32
		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 WIN32
				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(): %s\n",strerror(nRet));
					closesocket(hSocket);
					return false;
				}
			}
	#ifdef WIN32
			else if (WSAGetLastError() != WSAEISCONN)
	#else
			else
	#endif
			{
				printf("connect() failed: %i\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 WIN32
		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;
		}

		if (fProxy)
		{
			printf("proxy connecting %s\n", addrDest.ToString().c_str());
			char pszSocks4IP[] = "\4\1\0\0\0\0\0\0user";
			struct sockaddr_in addr;
			addrDest.GetSockAddr(&addr);
			memcpy(pszSocks4IP + 2, &addr.sin_port, 2);
			memcpy(pszSocks4IP + 4, &addr.sin_addr, 4);
			char* pszSocks4 = pszSocks4IP;
			int nSize = sizeof(pszSocks4IP);

			int ret = send(hSocket, pszSocks4, nSize, MSG_NOSIGNAL);
			if (ret != nSize)
			{
				closesocket(hSocket);
				return error("Error sending to proxy");
			}
			char pchRet[8];
			if (recv(hSocket, pchRet, 8, 0) != 8)
			{
				closesocket(hSocket);
				return error("Error reading proxy response");
			}
			if (pchRet[1] != 0x5a)
			{
				closesocket(hSocket);
				if (pchRet[1] != 0x5b)
					printf("ERROR: Proxy returned error %d\n", pchRet[1]);
				return false;
			}
			printf("proxy connected %s\n", addrDest.ToString().c_str());
		}

		hSocketRet = hSocket;
		return true;
	}