示例#1
0
/*
 * Verify a PDU is what we expected
 */
bool MockDMPInflator::HandlePDUData(uint32_t vector,
                                    HeaderSet &headers,
                                    const uint8_t *data,
                                    unsigned int pdu_len) {
  DMPHeader header = headers.GetDMPHeader();
  CPPUNIT_ASSERT_EQUAL(expected_vector, vector);
  CPPUNIT_ASSERT_EQUAL(expected_virtual, header.IsVirtual());
  CPPUNIT_ASSERT_EQUAL(expected_relative, header.IsRelative());
  CPPUNIT_ASSERT(expected_type == header.Type());
  CPPUNIT_ASSERT(expected_size == header.Size());

  if (vector == DMP_GET_PROPERTY_VECTOR ||
      vector == DMP_SET_PROPERTY_VECTOR) {
    unsigned int length = pdu_len;
    const BaseDMPAddress *addr = DecodeAddress(header.Size(), header.Type(),
                                               data, length);
    CPPUNIT_ASSERT(addr);
    CPPUNIT_ASSERT_EQUAL(expected_start, addr->Start());
    CPPUNIT_ASSERT_EQUAL(expected_increment, addr->Increment());
    CPPUNIT_ASSERT_EQUAL(expected_number, addr->Number());
    delete addr;
  }
  return true;
}
示例#2
0
文件: irc.cpp 项目: StealthCash/STX
void ThreadIRCSeed()
{
    // Don't connect to IRC if we won't use IPv4 connections.
    if (IsLimited(NET_IPV4))
        return;

    // ... or if we won't make outbound connections and won't accept inbound ones.
    if (mapArgs.count("-connect") && fNoListen)
        return;

    // ... or if IRC is not enabled.
    if (GetBoolArg("-irc", false))
        return;
    
    int nErrorWait = 10;
    int nRetryWait = 10;
    int nNameRetry = 0;

    for(;;)
    {
        boost::this_thread::interruption_point();
        CService addrConnect("92.243.23.21", 6667); // irc.lfnet.org

        CService addrIRC("irc.lfnet.org", 6667, true);
        if (addrIRC.IsValid())
            addrConnect = addrIRC;

        SOCKET hSocket;
        if (!ConnectSocket(addrConnect, hSocket, nConnectTimeout))
        {
            LogPrintf("IRC connect failed\n");
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        };

        if (!RecvUntil(hSocket, "Found your hostname", "using your IP address instead", "Couldn't look up your hostname", "ignoring hostname"))
        {
            closesocket(hSocket);
            hSocket = INVALID_SOCKET;
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        };

        CNetAddr addrIPv4("1.2.3.4"); // arbitrary IPv4 address to make GetLocal prefer IPv4 addresses
        CService addrLocal;
        std::string strMyName;
        // Don't use our IP as our nick if we're not listening
        // or if it keeps failing because the nick is already in use.
        if (!fNoListen && GetLocal(addrLocal, &addrIPv4) && nNameRetry<3)
            strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
        if (strMyName == "")
            strMyName = strprintf("x%u", GetRand(1000000000));

        Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
        Send(hSocket, strprintf("USER %s 8 * : %s\r", strMyName.c_str(), strMyName.c_str()).c_str());

        int nRet = RecvUntil(hSocket, " 004 ", " 433 ");
        if (nRet != 1)
        {
            closesocket(hSocket);
            hSocket = INVALID_SOCKET;
            if (nRet == 2)
            {
                LogPrintf("IRC name already in use\n");
                nNameRetry++;
                Wait(10);
                continue;
            };
            nErrorWait = nErrorWait * 11 / 10;
            if (Wait(nErrorWait += 60))
                continue;
            else
                return;
        };
        
        nNameRetry = 0;
        MilliSleep(500);

        // Get our external IP from the IRC server and re-nick before joining the channel
        CNetAddr addrFromIRC;
        if (GetIPFromIRC(hSocket, strMyName, addrFromIRC))
        {
            LogPrintf("GetIPFromIRC() returned %s\n", addrFromIRC.ToString().c_str());
            // Don't use our IP as our nick if we're not listening
            if (!fNoListen && addrFromIRC.IsRoutable())
            {
                // IRC lets you to re-nick
                AddLocal(addrFromIRC, LOCAL_IRC);
                strMyName = EncodeAddress(GetLocalAddress(&addrConnect));
                Send(hSocket, strprintf("NICK %s\r", strMyName.c_str()).c_str());
            };
        };

        if (fTestNet)
        {
            Send(hSocket, "JOIN #sdcoinTEST\r");
            Send(hSocket, "WHO #sdcoinTEST\r");
        } else
        {
            // randomly join #StealthCash00-#StealthCash05
            //int channel_number = GetRandInt(5);

            // Channel number is always 0 for initial release
            int channel_number = 0;
            Send(hSocket, strprintf("JOIN #sdcoin%02d\r", channel_number).c_str());
            Send(hSocket, strprintf("WHO #sdcoin%02d\r", channel_number).c_str());
        };

        int64_t nStart = GetTime();
        std::string strLine;
        strLine.reserve(10000);
        while (RecvLineIRC(hSocket, strLine))
        {
            boost::this_thread::interruption_point();
            if (strLine.empty() || strLine.size() > 900 || strLine[0] != ':')
                continue;

            std::vector<std::string> vWords;
            ParseString(strLine, ' ', vWords);
            if (vWords.size() < 2)
                continue;

            char pszName[10000];
            pszName[0] = '\0';

            if (vWords[1] == "352" && vWords.size() >= 8)
            {
                // index 7 is limited to 16 characters
                // could get full length name at index 10, but would be different from join messages
                strlcpy(pszName, vWords[7].c_str(), sizeof(pszName));
                LogPrintf("IRC got who\n");
            };

            if (vWords[1] == "JOIN" && vWords[0].size() > 1)
            {
                // :[email protected] JOIN :#channelname
                strlcpy(pszName, vWords[0].c_str() + 1, sizeof(pszName));
                if (strchr(pszName, '!'))
                    *strchr(pszName, '!') = '\0';
                LogPrintf("IRC got join\n");
            };

            if (pszName[0] == 'u')
            {
                CAddress addr;
                if (DecodeAddress(pszName, addr))
                {
                    addr.nTime = GetAdjustedTime();
                    if (addrman.Add(addr, addrConnect, 51 * 60))
                        LogPrintf("IRC got new address: %s\n", addr.ToString().c_str());
                    nGotIRCAddresses++;
                } else
                {
                    LogPrintf("IRC decode failed\n");
                };
            };
        };
        closesocket(hSocket);
        hSocket = INVALID_SOCKET;

        if (GetTime() - nStart > 20 * 60)
        {
            nErrorWait /= 3;
            nRetryWait /= 3;
        };

        nRetryWait = nRetryWait * 11 / 10;
        if (!Wait(nRetryWait += 60))
            return;
    };
}
示例#3
0
	bool GetKeyElement(const std::string &encode_key, PrivateKeyPrefix &prefix, SignatureType &sign_type, std::string &raw_data) {
		PrivateKeyPrefix prefix_tmp;
		SignatureType sign_type_tmp = SIGNTYPE_NONE;
		std::string buff = DecodeAddress(encode_key);
		if (buff.size() == 27 && (uint8_t)buff.at(0) == 0X01 && (uint8_t)buff.at(1) == 0X56){// address
			prefix_tmp = ADDRESS_PREFIX;
		}
		else if (buff.size() == 41 && (uint8_t)buff.at(0) == 0XDA && (uint8_t)buff.at(1) == 0X37 && (uint8_t)buff.at(2) == 0X9F){//private key
			prefix_tmp = PRIVATEKEY_PREFIX;
		}
		else{
			return false;
		}     
		
	   

		bool ret = true;
		if (prefix_tmp == ADDRESS_PREFIX) {
			uint8_t a = (uint8_t)buff.at(2); 
			sign_type_tmp = (SignatureType)a;   
			size_t datalen = buff.size() - 7;
			switch (sign_type_tmp) {
			case SIGNTYPE_ED25519:{
				ret = (ED25519_ADDRESS_LENGTH == datalen);
				break;
			}
			case SIGNTYPE_CFCASM2:{
				ret = (SM2_ADDRESS_LENGTH == datalen);
				break;
			}
			default:
				ret = false;
			}
		}
		else if (prefix_tmp == PRIVATEKEY_PREFIX) {
			uint8_t a = (uint8_t)buff.at(3);  
			sign_type_tmp = (SignatureType)a;
			size_t datalen = buff.size() - 9;
			switch (sign_type_tmp) {
			case SIGNTYPE_ED25519:{
				ret = (ED25519_PRIVATEKEY_LENGTH == datalen);
				break;
			}
			case SIGNTYPE_CFCASM2:{
				ret = (SM2_PRIVATEKEY_LENGTH == datalen);
				break;
			}
			default:
				ret = false;
			}
		}
		else {
			ret = false;
		}

		if (ret){
			//checksum
			std::string checksum = buff.substr(buff.size() - 4);
			std::string hash1 = CalcHash(buff.substr(0, buff.size() - 4), sign_type_tmp);
			std::string hash2 = CalcHash(hash1, sign_type_tmp);
			if (checksum.compare(hash2.substr(0, 4)))
				return false;

			prefix = prefix_tmp;
			sign_type = sign_type_tmp;
			if (prefix_tmp == ADDRESS_PREFIX) {
				raw_data = buff.substr(3, buff.size() - 7);
			}
			else if (prefix_tmp == PRIVATEKEY_PREFIX) {
				raw_data = buff.substr(4, buff.size() - 9);
			}
		} 

		return ret;
	}
示例#4
0
/**
 * This function implements jdwpTransportEnv::StartListening
 */
static jdwpTransportError JNICALL 
TCPIPSocketTran_StartListening(jdwpTransportEnv* env, const char* address, 
        char** actualAddress)
{
    SOCKET envClientSocket = ((internalEnv*)env->functions->reserved1)->envClientSocket;
    if (envClientSocket != INVALID_SOCKET) {
        SetLastTranError(env, "there is already an open connection to the debugger", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    SOCKET envServerSocket = ((internalEnv*)env->functions->reserved1)->envServerSocket;
    if (envServerSocket != INVALID_SOCKET) {
        SetLastTranError(env, "transport is currently in listen mode", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    jdwpTransportError res;
    struct sockaddr_in serverSockAddr;
    res = DecodeAddress(env, address, &serverSockAddr, true);
    if (res != JDWPTRANSPORT_ERROR_NONE) {
        return res;
    }

    SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (serverSocket == INVALID_SOCKET) {
        SetLastTranError(env, "unable to create socket", GetLastErrorStatus());
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }

    if (!SetSocketOptions(env, serverSocket)) {
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }

    int err;

    err = bind(serverSocket, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr));
    if (err == SOCKET_ERROR) {
        SetLastTranError(env, "binding to port failed", GetLastErrorStatus());
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    err = listen(serverSocket, SOMAXCONN);
    if (err == SOCKET_ERROR) {
        SetLastTranError(env, "listen start failed", GetLastErrorStatus());
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    if (!SetSocketBlockingMode(env, serverSocket, false)) {
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }

    ((internalEnv*)env->functions->reserved1)->envServerSocket = serverSocket;

    socklen_t len = sizeof(serverSockAddr);
    err = getsockname(serverSocket, (struct sockaddr *)&serverSockAddr, &len);
    if (err == SOCKET_ERROR) {
        SetLastTranError(env, "socket error", GetLastErrorStatus());
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    char* retAddress = 0;

    // RI always returns only port number in listening mode
/*
    char portName[6];
    sprintf(portName, "%d", ntohs(serverSockAddr.sin_port)); //instead of itoa()

    char hostName[NI_MAXHOST];
    if (getnameinfo((struct sockaddr *)&serverSockAddr, len, hostName, sizeof(hostName), NULL, 0, 0)) {
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }
    if (strcmp(hostName, "0.0.0.0") == 0) {
        gethostname(hostName, sizeof(hostName));
    }
    retAddress = (char*)(((internalEnv*)env->functions->reserved1)
        ->alloc)((jint)(strlen(hostName) + strlen(portName) + 2)); 
    if (retAddress == 0) {
        SetLastTranError(env, "out of memory", 0);
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
    }
    sprintf(retAddress, "%s:%s", hostName, portName);
*/
    retAddress = (char*)(((internalEnv*)env->functions->reserved1)->alloc)(6 + 1); 
    if (retAddress == 0) {
        SetLastTranError(env, "out of memory", 0);
        return JDWPTRANSPORT_ERROR_OUT_OF_MEMORY;
    }
    sprintf(retAddress, "%d", ntohs(serverSockAddr.sin_port));

    *actualAddress = retAddress;

    return JDWPTRANSPORT_ERROR_NONE;
} //TCPIPSocketTran_StartListening
示例#5
0
/**
 * This function implements jdwpTransportEnv::Attach
 */
static jdwpTransportError JNICALL 
TCPIPSocketTran_Attach(jdwpTransportEnv* env, const char* address,
        jlong attachTimeout, jlong handshakeTimeout)
{
    if ((address == 0) || (*address == 0)) {
        SetLastTranError(env, "address is missing", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
    }

    if (attachTimeout < 0) {
        SetLastTranError(env, "attachTimeout timeout is negative", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
    }

    if (handshakeTimeout < 0) {
        SetLastTranError(env, "handshakeTimeout timeout is negative", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_ARGUMENT;
    }

    SOCKET envClientSocket = ((internalEnv*)env->functions->reserved1)->envClientSocket;
    if (envClientSocket != INVALID_SOCKET) {
        SetLastTranError(env, "there is already an open connection to the debugger", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    SOCKET envServerSocket = ((internalEnv*)env->functions->reserved1)->envServerSocket;
    if (envServerSocket != INVALID_SOCKET) {
        SetLastTranError(env, "transport is currently in listen mode", 0);
        return JDWPTRANSPORT_ERROR_ILLEGAL_STATE ;
    }

    struct sockaddr_in serverSockAddr;
    jdwpTransportError res = DecodeAddress(env, address, &serverSockAddr, false);
    if (res != JDWPTRANSPORT_ERROR_NONE) {
        return res;
    }

    SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
    if (clientSocket == INVALID_SOCKET) {
        SetLastTranError(env, "unable to create socket", GetLastErrorStatus());
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }
    
    if (!SetSocketOptions(env, clientSocket)) {
        return JDWPTRANSPORT_ERROR_IO_ERROR;
    }

    if (attachTimeout == 0) {
        if (!SetSocketBlockingMode(env, clientSocket, true)) {
            return JDWPTRANSPORT_ERROR_IO_ERROR;
        }
        int err = connect(clientSocket, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr));
        if (err == SOCKET_ERROR) {
            SetLastTranError(env, "connection failed", GetLastErrorStatus());
            SetSocketBlockingMode(env, clientSocket, false);
            return JDWPTRANSPORT_ERROR_IO_ERROR;
        }
        if (!SetSocketBlockingMode(env, clientSocket, false)) {
            return JDWPTRANSPORT_ERROR_IO_ERROR;
        }
    } else {
        if (!SetSocketBlockingMode(env, clientSocket, false)) {
            return JDWPTRANSPORT_ERROR_IO_ERROR;
        }
        int err = connect(clientSocket, (struct sockaddr *)&serverSockAddr, sizeof(serverSockAddr));
        if (err == SOCKET_ERROR) {
            if (GetLastErrorStatus() != SOCKETWOULDBLOCK) {
                SetLastTranError(env, "connection failed", GetLastErrorStatus());
                return JDWPTRANSPORT_ERROR_IO_ERROR;
            } else {  
                fd_set fdwrite;
                FD_ZERO(&fdwrite);
                FD_SET(clientSocket, &fdwrite);
                TIMEVAL tv = {(long)(attachTimeout / 1000), (long)(attachTimeout % 1000)};

                int ret = select((int)clientSocket + 1, NULL, &fdwrite, NULL, &tv);
                if (ret == SOCKET_ERROR) {
                    SetLastTranError(env, "socket error", GetLastErrorStatus());
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
                }
                if ((ret != 1) || !(FD_ISSET(clientSocket, &fdwrite))) {
                    SetLastTranError(env, "timeout occurred", 0);
                    return JDWPTRANSPORT_ERROR_IO_ERROR;
                }
            }
        }
    }

    EnterCriticalSendSection(env);
    EnterCriticalReadSection(env);
    ((internalEnv*)env->functions->reserved1)->envClientSocket = clientSocket;
    res = CheckHandshaking(env, clientSocket, (long)handshakeTimeout);
    LeaveCriticalReadSection(env);
    LeaveCriticalSendSection(env);
    if (res != JDWPTRANSPORT_ERROR_NONE) {
        TCPIPSocketTran_Close(env);
        return res;
    }

    return JDWPTRANSPORT_ERROR_NONE;
} //TCPIPSocketTran_Attach