Beispiel #1
0
RakString RakString::FormatForPOST(RakString &uri, RakString &contentType, unsigned int port, RakString &body)
{
	RakString out;
	RakString host;
	RakString remotePath;
	RakNet::RakString header;

	uri.SplitURI(header, host, remotePath);
	if (host.IsEmpty() || remotePath.IsEmpty())
		return out;
	
	out.Set("POST %s HTTP/1.0\r\n"
		"Host: %s:%i\r\n"
		"Content-Type: %s\r\n"
		"Content-Length: %u\r\n"
		"\r\n"
		"%s",
		remotePath.C_String(),
		host.C_String(),
		port,
		contentType.C_String(),
		body.GetLength(),
		body.C_String());

	return out;
}
Beispiel #2
0
RakNet::RakString RakString::FormatForPUTOrPost(const char* type, const char* uri, const char* contentType, const char* body, const char* extraHeaders)
{
	RakString out;
	RakString host;
	RakString remotePath;
	RakNet::RakString header;
	RakString uriRs;
	uriRs = uri;
	uriRs.SplitURI(header, host, remotePath);

	if (host.IsEmpty() || remotePath.IsEmpty())
		return out;

//	RakString bodyEncoded = body;
//	bodyEncoded.URLEncode();

	if (extraHeaders!=0 && extraHeaders[0])
	{
		out.Set("%s %s HTTP/1.1\r\n"
			"%s\r\n"
			"Host: %s\r\n"
			"Content-Type: %s\r\n"
			"Content-Length: %u\r\n"
			"\r\n"
			"%s",
			type,
			remotePath.C_String(),
			extraHeaders,
			host.C_String(),
			contentType,
			//bodyEncoded.GetLength(),
			//bodyEncoded.C_String());
			strlen(body),
			body);
	}
	else
	{
		out.Set("%s %s HTTP/1.1\r\n"
			"Host: %s\r\n"
			"Content-Type: %s\r\n"
			"Content-Length: %u\r\n"
			"\r\n"
			"%s",
			type,
			remotePath.C_String(),
			host.C_String(),
			contentType,
			//bodyEncoded.GetLength(),
			//bodyEncoded.C_String());
			strlen(body),
			body);
	}

	return out;
}
Beispiel #3
0
RakString RakString::FormatForGET(RakString &uri, unsigned int port)
{
	RakString out;
	RakString host;
	RakString remotePath;
	RakNet::RakString header;

	uri.SplitURI(header, host, remotePath);
	if (host.IsEmpty() || remotePath.IsEmpty())
		return out;

	out.Set("GET %s HTTP/1.0\r\n"
		"Host: %s:%i\r\n"
		"\r\n",
		remotePath.C_String(),
		host.C_String(),
		port);

	return out;
}
Beispiel #4
0
RakString RakString::FormatForDELETE(const char* uri, const char* extraHeaders)
{
	RakString out;
	RakString host;
	RakString remotePath;
	RakNet::RakString header;
	RakNet::RakString uriRs;
	uriRs = uri;

	uriRs.SplitURI(header, host, remotePath);
	if (host.IsEmpty() || remotePath.IsEmpty())
		return out;

	if (extraHeaders && extraHeaders[0])
	{
		out.Set("DELETE %s HTTP/1.1\r\n"
			"%s\r\n"
			"Content-Length: 0\r\n"
			"Host: %s\r\n"
			"Connection: close\r\n"
			"\r\n",
			remotePath.C_String(),
			extraHeaders,
			host.C_String());
	}
	else
	{
		out.Set("DELETE %s HTTP/1.1\r\n"
			"Content-Length: 0\r\n"
			"Host: %s\r\n"
			"Connection: close\r\n"
			"\r\n",
			remotePath.C_String(),
			host.C_String());
	}

	return out;
}
Beispiel #5
0
bool RPC3::SendCallOrSignal(RakString uniqueIdentifier, char parameterCount, RakNet::BitStream *serializedParameters, bool isCall)
{	
	SystemAddress systemAddr;
//	unsigned int outerIndex;
//	unsigned int innerIndex;

	if (uniqueIdentifier.IsEmpty())
		return false;

	RakNet::BitStream bs;
	if (outgoingTimestamp!=0)
	{
		bs.Write((MessageID)ID_TIMESTAMP);
		bs.Write(outgoingTimestamp);
	}
	bs.Write((MessageID)ID_AUTO_RPC_CALL);
	bs.Write(parameterCount);
	if (outgoingNetworkID!=UNASSIGNED_NETWORK_ID && isCall)
	{
		bs.Write(true);
		bs.Write(outgoingNetworkID);
	}
	else
	{
		bs.Write(false);
	}
	bs.Write(isCall);
	// This is so the call SetWriteOffset works
	bs.AlignWriteToByteBoundary();
	BitSize_t writeOffset = bs.GetWriteOffset();
	if (outgoingBroadcast)
	{
		unsigned systemIndex;
		for (systemIndex=0; systemIndex < rakPeerInterface->GetMaximumNumberOfPeers(); systemIndex++)
		{
			systemAddr=rakPeerInterface->GetSystemAddressFromIndex(systemIndex);
			if (systemAddr!=UNASSIGNED_SYSTEM_ADDRESS && systemAddr!=outgoingSystemAddress)
			{
// 				if (GetRemoteFunctionIndex(systemAddr, uniqueIdentifier, &outerIndex, &innerIndex, isCall))
// 				{
// 					// Write a number to identify the function if possible, for faster lookup and less bandwidth
// 					bs.Write(true);
// 					if (isCall)
// 						bs.WriteCompressed(remoteFunctions[outerIndex]->operator [](innerIndex).functionIndex);
// 					else
// 						bs.WriteCompressed(remoteSlots[outerIndex]->operator [](innerIndex).functionIndex);
// 				}
// 				else
// 				{
// 					bs.Write(false);
					stringCompressor->EncodeString(uniqueIdentifier, 512, &bs, 0);
//				}

				bs.WriteCompressed(serializedParameters->GetNumberOfBitsUsed());
				bs.WriteAlignedBytes((const unsigned char*) serializedParameters->GetData(), serializedParameters->GetNumberOfBytesUsed());
				SendUnified(&bs, outgoingPriority, outgoingReliability, outgoingOrderingChannel, systemAddr, false);

				// Start writing again after ID_AUTO_RPC_CALL
				bs.SetWriteOffset(writeOffset);
			}
		}
	}
	else
	{
		systemAddr = outgoingSystemAddress;
		if (systemAddr!=UNASSIGNED_SYSTEM_ADDRESS)
		{
// 			if (GetRemoteFunctionIndex(systemAddr, uniqueIdentifier, &outerIndex, &innerIndex, isCall))
// 			{
// 				// Write a number to identify the function if possible, for faster lookup and less bandwidth
// 				bs.Write(true);
// 				if (isCall)
// 					bs.WriteCompressed(remoteFunctions[outerIndex]->operator [](innerIndex).functionIndex);
// 				else
// 					bs.WriteCompressed(remoteSlots[outerIndex]->operator [](innerIndex).functionIndex);
// 			}
// 			else
// 			{
// 				bs.Write(false);
				stringCompressor->EncodeString(uniqueIdentifier, 512, &bs, 0);
//			}

			bs.WriteCompressed(serializedParameters->GetNumberOfBitsUsed());
			bs.WriteAlignedBytes((const unsigned char*) serializedParameters->GetData(), serializedParameters->GetNumberOfBytesUsed());
			SendUnified(&bs, outgoingPriority, outgoingReliability, outgoingOrderingChannel, systemAddr, false);
		}
		else
			return false;
	}
	return true;
}
void UDPProxyCoordinator::OnForwardingReplyFromServerToCoordinator(Packet *packet)
{
    RakNet::BitStream incomingBs(packet->data, packet->length, false);
    incomingBs.IgnoreBytes(2);
    SenderAndTargetAddress sata;
    incomingBs.Read(sata.senderClientAddress);
    incomingBs.Read(sata.targetClientAddress);
    bool objectExists;
    unsigned int index = forwardingRequestList.GetIndexFromKey(sata, &objectExists);
    if (objectExists==false)
    {
        // The guy disconnected before the request finished
        return;
    }
    ForwardingRequest *fw = forwardingRequestList[index];
    sata.senderClientGuid = fw->sata.senderClientGuid;
    sata.targetClientGuid = fw->sata.targetClientGuid;

    RakString serverPublicIp;
    incomingBs.Read(serverPublicIp);

    if (serverPublicIp.IsEmpty())
    {
        char serverIP[64];
        packet->systemAddress.ToString(false,serverIP);
        serverPublicIp=serverIP;
    }

    UDPForwarderResult success;
    unsigned char c;
    incomingBs.Read(c);
    success=(UDPForwarderResult)c;

    unsigned short forwardingPort;
    incomingBs.Read(forwardingPort);

    RakNet::BitStream outgoingBs;
    if (success==UDPFORWARDER_SUCCESS)
    {
        outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
        outgoingBs.Write((MessageID)ID_UDP_PROXY_FORWARDING_SUCCEEDED);
        outgoingBs.Write(sata.senderClientAddress);
        outgoingBs.Write(sata.targetClientAddress);
        outgoingBs.Write(sata.targetClientGuid);
        outgoingBs.Write(serverPublicIp);
        outgoingBs.Write(forwardingPort);
        rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, fw->requestingAddress, false);

        outgoingBs.Reset();
        outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
        outgoingBs.Write((MessageID)ID_UDP_PROXY_FORWARDING_NOTIFICATION);
        outgoingBs.Write(sata.senderClientAddress);
        outgoingBs.Write(sata.targetClientAddress);
        outgoingBs.Write(sata.targetClientGuid);
        outgoingBs.Write(serverPublicIp);
        outgoingBs.Write(forwardingPort);
        rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, sata.targetClientAddress, false);

        // 05/18/09 Keep the entry around for some time after success, so duplicates are reported if attempting forwarding from the target system before notification of success
        fw->timeoutAfterSuccess=RakNet::GetTimeMS()+fw->timeoutOnNoDataMS;
        // forwardingRequestList.RemoveAtIndex(index);
        // RakNet::OP_DELETE(fw,_FILE_AND_LINE_);

        return;
    }
    else if (success==UDPFORWARDER_NO_SOCKETS)
    {
        // Try next server
        TryNextServer(sata, fw);
    }
    else
    {
        RakAssert(success==UDPFORWARDER_FORWARDING_ALREADY_EXISTS);

        // Return in progress
        outgoingBs.Write((MessageID)ID_UDP_PROXY_GENERAL);
        outgoingBs.Write((MessageID)ID_UDP_PROXY_IN_PROGRESS);
        outgoingBs.Write(sata.senderClientAddress);
        outgoingBs.Write(sata.targetClientAddress);
        outgoingBs.Write(sata.targetClientGuid);
        outgoingBs.Write(serverPublicIp);
        outgoingBs.Write(forwardingPort);
        rakPeerInterface->Send(&outgoingBs, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, fw->requestingAddress, false);
        forwardingRequestList.RemoveAtIndex(index);
        RakNet::OP_DELETE(fw,_FILE_AND_LINE_);
    }
}
Beispiel #7
0
void Rackspace2::Update(void)
{
	RakString stringTransmitted;
	RakString hostTransmitted;
	RakString responseReceived;
	Packet *packet;
	SystemAddress sa;
	// This is kind of crappy, but for TCP plugins, always do HasCompletedConnectionAttempt, then Receive(), then HasFailedConnectionAttempt(),HasLostConnection()
	sa = tcp->HasCompletedConnectionAttempt();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		//printf("Rackspace2 TCP: Connected to %s\n", sa.ToString());
//		serverAddress = sa;
	}
	for (packet = tcp->Receive(); packet; tcp->DeallocatePacket(packet), packet = tcp->Receive())
		;
	sa = tcp->HasFailedConnectionAttempt();
	//if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	//	printf("Rackspace2 TCP: Failed connection attempt to %s\n", sa.ToString());
	sa = tcp->HasLostConnection();
	if (sa!=UNASSIGNED_SYSTEM_ADDRESS)
	{
		//printf("Rackspace2 TCP: Lost connection to %s\n", sa.ToString());
		//		serverAddress=UNASSIGNED_SYSTEM_ADDRESS;
	}

	SystemAddress hostReceived;
	int contentOffset;
	if (httpConnection2->GetResponse(stringTransmitted, hostTransmitted, responseReceived, hostReceived, contentOffset))
	{
		if (responseReceived.IsEmpty()==false)
		{
			static FILE *fp = fopen("responses.txt", "wt");
			fprintf(fp, responseReceived.C_String());
			fprintf(fp, "\n");
			if (contentOffset==-1)
			{
				if (eventCallback)
					eventCallback->OnResponse(R2RC_NO_CONTENT, responseReceived, contentOffset);
			}
			else
			{
				json_error_t error;

				json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
				if (!root)
				{
					if (eventCallback)
						eventCallback->OnResponse(R2RC_BAD_JSON, responseReceived, contentOffset);
				}
				else
				{
					void *iter = json_object_iter(root);
					const char *firstKey = json_object_iter_key(iter);
					if (stricmp(firstKey, "unauthorized")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_UNAUTHORIZED, responseReceived, contentOffset);					
					}
					else if (stricmp(firstKey, "itemNotFound")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_404_NOT_FOUND, responseReceived, contentOffset);					
					}
					else if (stricmp(firstKey, "access")==0)
					{
						json_t *valAuthToken = json_object_get(json_object_get(json_object_get(root, "access"), "token"), "id");
						strcpy(X_Auth_Token, json_string_value(valAuthToken));

						json_t *valAccountNumber = json_object_get(json_object_get(json_object_get(json_object_get(root, "access"), "token"), "tenant"), "id");
						cloudAccountNumber = atoi(json_string_value(valAccountNumber));	

						if (reexecuteLastRequestOnAuth)
						{
							reexecuteLastRequestOnAuth=false;

							json_t *root = json_loads(__addOpLast_dataAsStr.C_String(), 0, &error);							
							AddOperation(__addOpLast_URL, __addOpLast_isPost, root, true);							
						}
						else
						{
							if (eventCallback)
								eventCallback->OnResponse(R2RC_AUTHENTICATED, responseReceived, contentOffset);
						}

					}
					else if (stricmp(firstKey, "domains")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_GOT_DOMAINS, responseReceived, contentOffset);
					}
					else if (stricmp(firstKey, "records")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_GOT_RECORDS, responseReceived, contentOffset);
					}
					else if (stricmp(firstKey, "servers")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_GOT_SERVERS, responseReceived, contentOffset);
					}
					else if (stricmp(firstKey, "images")==0)
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_GOT_IMAGES, responseReceived, contentOffset);
					}
					else if (stricmp(firstKey, "message")==0)
					{
						const char *message = json_string_value(json_object_iter_value(iter));

						if (strcmp(message, "Invalid authentication token. Please renew.")==0)
						{
							// Sets reexecuteLastRequestOnAuth to true
							// After authenticate completes, will rerun the last run command
							Reauthenticate();
						}
						else
						{
							if (eventCallback)
								eventCallback->OnMessage(message, responseReceived, stringTransmitted, contentOffset);
						}
					}
					else
					{
						if (eventCallback)
							eventCallback->OnResponse(R2RC_UNKNOWN, responseReceived, contentOffset);
					}
				}

				json_decref(root);
			}
		}
		else
		{
			if (eventCallback)
				eventCallback->OnEmptyResponse(stringTransmitted);
		}
	}
}