bool
TestSanityChild::RecvPing(const int& zero, const float& zeroPtFive,
                          const int8_t&/*unused*/)
{
    if (0 != zero)
        fail("invalid argument `%d', should have been `0'", zero);

    if (0.5f != zeroPtFive)
        fail("invalid argument `%g', should have been `0.5'", zeroPtFive);

    if (!SendPong(1, 0.25f, 0))
        fail("sending Pong");
    return true;
}
Example #2
0
mozilla::ipc::IPCResult
TestAsyncReturnsChild::RecvPing(PingResolver&& aResolve)
{
  SendPong()->Then(MessageLoop::current()->SerialEventTarget(), __func__,
                   [aResolve](const Tuple<uint32_t, uint32_t>& aParam) {
                     if (Get<0>(aParam) == sMagic1 && Get<1>(aParam) == sMagic2) {
                       passed("take two arguments");
                     } else {
                       fail("get two argument but has wrong value");
                     }
                     aResolve(true);
                   },
                   [](PromiseRejectReason aReason) {
                     fail("sending Pong");
                   });
  return IPC_OK();
}
Example #3
0
void WebSocketWorker::ProcessFrames(QTcpSocket *socket)
{
    while (m_isRunning && socket && socket->bytesAvailable() >= 2) // No header? Return and wait for more
    {
        uint8_t headerSize = 2; // Smallest possible header size is 2 bytes, greatest is 14 bytes

        QByteArray header = socket->peek(headerSize); // Read header to establish validity and size of frame

        WebSocketFrame frame;
        // FIN
        frame.finalFrame = (bool)(header[0] & 0x80);
        // Reserved bits
        if (header.at(0) & 0x70)
        {
            LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker::ProcessFrames() "
                                     "- Invalid data in reserved fields");
            SendClose(kCloseProtocolError, "Invalid data in reserved fields");
            return;
        }
        // Operation code
        uint8_t opCode = (header.at(0) & 0xF);
        if ((opCode > WebSocketFrame::kOpBinaryFrame &&
             opCode < WebSocketFrame::kOpClose) ||
             (opCode > WebSocketFrame::kOpPong))
        {
            LOG(VB_GENERAL, LOG_ERR, QString("WebSocketWorker::ProcessFrames() "
                                             "- Invalid OpCode (%1)")
                                            .arg(QString::number(opCode, 16)));
            SendClose(kCloseProtocolError, "Invalid OpCode");
            return;
        }
        frame.opCode = (WebSocketFrame::OpCode)opCode;
        frame.isMasked = (header.at(1) >> 7) & 0xFE;

        if (frame.isMasked)
            headerSize += 4; // Add 4 bytes for the mask

        frame.payloadSize = (header.at(1) & 0x7F);
        // Handle 16 or 64bit payload size headers
        if (frame.payloadSize >= 126)
        {
            uint8_t payloadHeaderSize = 2; // 16bit payload size
            if (frame.payloadSize == 127)
                payloadHeaderSize = 8; // 64bit payload size

            headerSize += payloadHeaderSize; // Add bytes for extended payload size

            if (socket->bytesAvailable() < headerSize)
                return; // Return and wait for more

            header = socket->peek(headerSize); // Peek the entire header
            QByteArray payloadHeader = header.mid(2,payloadHeaderSize);
            frame.payloadSize = 0;
            for (int i = 0; i < payloadHeaderSize; i++)
            {
                frame.payloadSize |= ((uint8_t)payloadHeader.at(i) << ((payloadHeaderSize - (i + 1)) * 8));
            }
        }
        else
        {
            if (socket->bytesAvailable() < headerSize)
                return; // Return and wait for more
            header = socket->peek(headerSize); // Peek the entire header including mask
        }

        while ((uint64_t)socket->bytesAvailable() < (frame.payloadSize + header.length()))
        {
            if (!socket->waitForReadyRead(2000)) // Wait 2 seconds for the next chunk of the frame
            {

                m_errorCount++;

                if (m_errorCount == 5)
                {
                    LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker::ProcessFrames() - Timed out waiting for rest of frame to arrive.");
                    SendClose(kCloseBadData);
                }
                return;
            }
        }

        if (frame.opCode == WebSocketFrame::kOpContinuation)
             m_readFrame.payloadSize += frame.payloadSize;

        LOG(VB_HTTP, LOG_DEBUG, QString("Read Header: %1").arg(QString(header.toHex())));
        LOG(VB_HTTP, LOG_DEBUG, QString("Final Frame: %1").arg(frame.finalFrame ? "Yes" : "No"));
        LOG(VB_HTTP, LOG_DEBUG, QString("Op Code: %1").arg(QString::number(frame.opCode)));
        LOG(VB_HTTP, LOG_DEBUG, QString("Payload Size: %1 Bytes").arg(QString::number(frame.payloadSize)));
        LOG(VB_HTTP, LOG_DEBUG, QString("Total Payload Size: %1 Bytes").arg(QString::number( m_readFrame.payloadSize)));

        if (!m_fuzzTesting &&
            frame.payloadSize > qPow(2,20)) // Set 1MB limit on payload per frame
        {
            LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker::ProcessFrames() - Frame payload larger than limit of 1MB");
            SendClose(kCloseTooLarge, "Frame payload larger than limit of 1MB");
            return;
        }

        if (!m_fuzzTesting &&
            m_readFrame.payloadSize > qPow(2,22)) // Set 4MB limit on total payload
        {
            LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker::ProcessFrames() - Total payload larger than limit of 4MB");
            SendClose(kCloseTooLarge, "Total payload larger than limit of 4MB");
            return;
        }

        socket->read(headerSize); // Discard header from read buffer

        frame.payload = socket->read(frame.payloadSize);

        // Unmask payload
        if (frame.isMasked)
        {
            frame.mask = header.right(4);
            for (uint i = 0; i < frame.payloadSize; i++)
                frame.payload[i] = frame.payload.at(i) ^ frame.mask[i % 4];
        }

        if (m_readFrame.fragmented && frame.opCode > WebSocketFrame::kOpContinuation && frame.opCode < WebSocketFrame::kOpClose)
        {
            LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker - Incomplete multi-part frame? Expected continuation.");
            SendClose(kCloseProtocolError, "Incomplete multi-part frame? Expected continuation.");
            return;
        }

        // Check control frame validity
        if (frame.opCode >= 0x08)
        {
            if (!frame.finalFrame)
            {
                SendClose(kCloseProtocolError, "Control frames MUST NOT be fragmented");
                return;
            }
            else if (frame.payloadSize > 125)
            {
                SendClose(kCloseProtocolError, "Control frames MUST NOT have payload greater than 125 bytes");
                return;
            }
        }

        switch (frame.opCode)
        {
            case WebSocketFrame::kOpContinuation:
                if (!m_readFrame.fragmented)
                {
                    LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker - Received Continuation Frame out of sequence");
                    SendClose(kCloseProtocolError, "Received Continuation Frame out of sequence");
                    return;
                }

                m_readFrame.payload.append(frame.payload);

                if (m_readFrame.fragmented && frame.finalFrame)
                {
                    m_readFrame.finalFrame = true;
                    frame = m_readFrame;
                    // Fall through to appropriate handler for complete payload
                }
                else
                    break;
                [[clang::fallthrough]];
            case WebSocketFrame::kOpTextFrame:
            case WebSocketFrame::kOpBinaryFrame:
                HandleDataFrame(frame);
                break;
            case WebSocketFrame::kOpPing:
                SendPong(frame.payload);
                break;
            case WebSocketFrame::kOpPong:
                break;
            case WebSocketFrame::kOpClose:
                if (!frame.finalFrame)
                    SendClose(kCloseProtocolError, "Control frames MUST NOT be fragmented");
                else
                    HandleCloseConnection(frame.payload);
                break;
            default:
                LOG(VB_GENERAL, LOG_ERR, "WebSocketWorker - Received Unknown Frame Type");
                break;
        }

        frame.reset();
    }
}
Example #4
0
void kaillera_ProcessGeneralInstruction(k_instruction * ki) {
	switch (ki->type) {
	case USERJOIN: // user joins the server
		{
			unsigned short id = ki->load_short();
			int ping = ki->load_int();
			int conn = ki->load_char();
			kaillera_user_join_callback(ki->user, ping, id, conn);
			break;
		}
	case USERLEAV:
		{
			unsigned short id = ki->load_short();
			char quitmsg[128];
			ki->load_str(quitmsg, 128);
			kaillera_user_leave_callback(ki->user, quitmsg, id);
			break;
		}
	case GAMEMAKE:
		{
			//kaillera_core_debug("GAMEMAKE");

			char gname[128];
			ki->load_str(gname, 128);
			char emulator[128];
			ki->load_str(emulator, 128);
			unsigned int id = ki->load_int();
			
			kaillera_game_create_callback(gname, id, emulator, ki->user);
			
			if (KAILLERAC.game_id_requested) {
				KAILLERAC.game_id = id;
				if (strcmp(gname, KAILLERAC.GAME)==0 && strcmp(emulator, KAILLERAC.APP)==0 && strcmp(ki->user, KAILLERAC.USERNAME)==0) {
					KAILLERAC.game_id_requested = false;
					KAILLERAC.user_id_requested = true;
					
					KAILLERAC.USERSTAT = 3;
					KAILLERAC.PLAYERSTAT = 0;
					
					kaillera_user_game_create_callback();
				}
			}
			break;
		}
	case INSTRUCTION_GAMESTAT:
		{
			unsigned int id = ki->load_int();
			char status = ki->load_char();
			int players = ki->load_char();
			int maxplayers = ki->load_char();
			kaillera_game_status_change_callback(id, status, players, maxplayers);
			break;
		}
	case INSTRUCTION_GAMESHUT:
		{
			unsigned int id = ki->load_int();
			kaillera_game_close_callback(id);
			if (id==KAILLERAC.game_id){
				kaillera_user_game_closed_callback();
				kaillera_end_game_callback();
				KAILLERAC.USERSTAT = 2;
				KAILLERAC.PLAYERSTAT = -1;
			}
			break;
		}
	case GAMEBEGN:
		{
			//kaillera_core_debug("GAMEBEGN");
			//KAILLERAC.USERSTAT = 3;
			KAILLERAC.PLAYERSTAT = 1;
			KAILLERAC.throughput = ki->load_short();
			KAILLERAC.dframeno = (KAILLERAC.throughput + 1) * KAILLERAC.conset - 1;
			KAILLERAC.playerno = ki->load_char();					
			int players = ki->load_char();
			kaillera_game_callback(0, KAILLERAC.playerno, players);
			kaillera_core_debug("Server says: delay is %i frames", KAILLERAC.dframeno);
			break;
		}
	case GAMRSLST:
		{
			//kaillera_core_debug("GAMRSLST");

			if (!KAILLERAC.owner)
				kaillera_user_game_joined_callback();

			KAILLERAC.USERSTAT = 3;
			KAILLERAC.PLAYERSTAT = 0;
			int numplayers = ki->load_int();
			for (int x=0; x < numplayers; x++) {
				char name[32];
				ki->load_str(name, 32);
				int ping = ki->load_int();
				unsigned short id = ki->load_short();
				int conn = ki->load_char();
				kaillera_player_add_callback(name, ping, id, conn);
			}
			break;
		}
	case GAMRJOIN:
		{
			//kaillera_core_debug("GAMRJOIN");
			ki->load_int();
			char username[32];
			ki->load_str(username, 32);					
			int ping = ki->load_int();					
			unsigned short uid = ki->load_short();
			char connset = ki->load_char();

			kaillera_player_joined_callback(username, ping, uid, connset);

			if (KAILLERAC.user_id_requested) {
				KAILLERAC.user_id = uid;
				if (KAILLERAC.conset == connset && strcmp(username, KAILLERAC.USERNAME)==0) {
					KAILLERAC.user_id_requested = false;
					KAILLERAC.USERSTAT = 3;
					KAILLERAC.PLAYERSTAT = 0;
				}
			}
			break;
		}
	case GAMRLEAV:
		{
			unsigned short id;
			kaillera_player_left_callback(ki->user, id = ki->load_short());
			if (id==KAILLERAC.user_id && !KAILLERAC.leave_game_requested) {
				kaillera_user_kicked_callback();
				KAILLERAC.USERSTAT = 2;
				KAILLERAC.PLAYERSTAT = -1;
			}
			break;
		}
	case GAMRDROP:
		{
			int gdpl = ki->load_char();
			if (KAILLERAC.playerno == gdpl) {
				KAILLERAC.USERSTAT = 3;
				KAILLERAC.PLAYERSTAT = 0;
				kaillera_end_game_callback();
			}
			kaillera_player_dropped_callback(ki->user, gdpl);
			break;
		}
	case PARTCHAT:
		{
		kaillera_chat_callback(ki->user, ki->buffer);
		}
		break;
	case GAMECHAT:
		{
			kaillera_game_chat_callback(ki->user, ki->buffer);
		}
		break;
	case MOTDLINE:
		kaillera_motd_callback(ki->user, ki->buffer);
		break;				
	case LONGSUCC:
		{
			KAILLERAC.USERSTAT = KAILLERAC.USERSTAT == 1? 2:KAILLERAC.USERSTAT;
			int gameZ;
			int userZ;
			userZ = ki->load_int();
			gameZ = ki->load_int();
			int x;
			//users
			for(x=0;x<userZ; x++) {
				char name[32];
				ki->load_str(name, 32);
				int ping = ki->load_int();
				int status = ki->load_char();
				unsigned short id = ki->load_short();
				int conn = ki->load_char();
				kaillera_user_add_callback(name, ping, status, id, conn);
			}
			for(x=0;x<gameZ; x++) {
				char gname[128];
				ki->load_str(gname, 128);
				unsigned int id = ki->load_int();
				char emulator[128];
				ki->load_str(emulator, 128);
				char owner[32];
				ki->load_str(owner, 32);
				char users[20];
				ki->load_str(users, 20);
				int status = ki->load_char();
				kaillera_game_add_callback(gname, id, emulator, owner, users, status);
			}
			KAILLERAC.tmoutrsttime = p2p_GetTime();
			break;
		}
	case SERVPING:
		{
			if(!spoofing) // norma pong response only if not spoofing
				SendPong();
			break;
		}
	case LOGNSTAT:
		{
			ki->load_short();
			char lsmsg[128];
			ki->load_str(lsmsg, 128);
			kaillera_login_stat_callback(lsmsg);
			break;
		}
	default:
		return;
	}
}
Example #5
0
bool kaillera_core_connect(char * ip, int port){
	
	kaillera_core_debug("Connecting to %s:%i", ip, port);
	
	strncpy(KAILLERAC.IP, ip, 128);
	
	k_socket ksock;
	ksock.initialize(0, 2048);
	
	if (ksock.set_address(ip, port)){
		ksock.send("HELLO0.83", 10); // Client's hello message
		
		DWORD tout = p2p_GetTime();
		
		while ((!k_socket::check_sockets(0, 100) || !ksock.has_data()) && p2p_GetTime() - tout < KAILLERA_CONNECTION_RESP_MAX_DELAY);
		
		if (ksock.has_data()) {

			char srsp[256];
			int srspl = 256;
			sockaddr_in addr;
			
			kaillera_core_debug("server replied");
			
			if (ksock.check_recv(srsp, &srspl, false, &addr)) {
				if (strncmp("HELLOD00D", srsp, 9) == 0) { // Server's hello reply
					
					kaillera_core_debug("logging in");
					
					addr.sin_port = htons(atoi(srsp+9));
					KAILLERAC.connection->set_addr(&addr);
					KAILLERAC.USERSTAT = 1;
					KAILLERAC.PLAYERSTAT = -1;
					
					k_instruction ki;
					ki.type = USERLOGN;
					ki.store_string(KAILLERAC.APP);
					ki.store_char(KAILLERAC.conset);
					/* Old code to append "--FakePing" to the username
					if(spoofing)
					{
						//char tempbuff[32];
						//strcpy(tempbuff,KAILLERAC.USERNAME);
						//snprintf(KAILLERAC.USERNAME,32,"%s--FakePing",tempbuff);
					}*/
					ki.set_username(KAILLERAC.USERNAME);
					
					KAILLERAC.connection->send_instruction(&ki);

					// Credits to Fireblaster for showing me the code that taught me
					// I needed to pong 4 times.
					// If you take a minute to think about it, this ping spoofing code
					// is simple and makes perfect sense.
					if(spoofing)
					{
						Sleep(spoofPing);
						SendPong();
						Sleep(spoofPing);
						SendPong();
						Sleep(spoofPing);
						SendPong();
						Sleep(spoofPing);
						SendPong();
					}

					return true;
				}
			} else {
				if (strcmp("TOO", srsp)==0) { // Too many users
					kaillera_error_callback("Server is full");
				} else {
					kaillera_core_debug("server protocol mismatch or other error");
					//protocol different or unrecognized protocol
				}
			}

		}
		
		return false;
	}
	return false;
}
bool
TestLatencyChild::RecvPing()
{
    SendPong();
    return true;
}
void FSessionService::HandleSessionPingMessage(const FSessionServicePing& Message, const IMessageContextRef& Context)
{
	SendPong(Context, Message.UserName);
}
Example #8
0
void FEngineService::HandlePingMessage( const FEngineServicePing& Message, const IMessageContextRef& Context )
{
	SendPong(Context);
}