replace ProcessNetworkPacket

This commit is contained in:
yugecin 2020-04-04 05:10:14 +02:00
parent d7e2c32a9d
commit 3a3b4ab7e2
No known key found for this signature in database
GPG Key ID: BB3591E3D68964DE
12 changed files with 362 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#include "common.h" #include "common.h"
#include "rakpeer.h" #include "rakpeer.h"
#include "processnetworkpacket.h"
#include "uncompress.h" #include "uncompress.h"
#include <windows.h> #include <windows.h>
#include <stdlib.h> #include <stdlib.h>
@ -20,6 +21,7 @@ void simple_redir_call(void *address, void *newtarget)
void nethandler_init() void nethandler_init()
{ {
simple_redir_call(UNCOMPRESS_TARGET, uncompress_main); simple_redir_call(UNCOMPRESS_TARGET, uncompress_main);
simple_redir_call(RP_PARSE_NETWORK_PACKET, ProcessNetworkPacket);
simple_redir_call(RP_PARSE_CONNECTION_REQ, simple_redir_call(RP_PARSE_CONNECTION_REQ,
RakPeer__ParseConnectionRequestPacket); RakPeer__ParseConnectionRequestPacket);
} }

15
packet.c Normal file
View File

@ -0,0 +1,15 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#include "common.h"
#include "rakpeer.h"
#include "packet.h"
__declspec(naked)
struct CPacket* __cdecl Packet__AllocPacket(int bitSize)
{
_asm {
mov eax, 0x44FDE0
jmp eax
}
}

15
packet.h Normal file
View File

@ -0,0 +1,15 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#pragma pack(push,1)
struct CPacket {
short playerIndex;
struct PlayerID playerId;
char _pad[2];
int bitSize;
char *ptrData;
char deleteData;
};
#pragma pack(pop)
struct CPacket* __cdecl Packet__AllocPacket(int bitSize);

132
processnetworkpacket.c Normal file
View File

@ -0,0 +1,132 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#define PROCESSNETWORKPACKET_PRINT
#ifdef PROCESSNETWORKPACKET_PRINT
#define dprintf(X,...) printf(X,__VA_ARGS__)
#else
#define dprintf(X,...)
#endif
#include "common.h"
#include "rakpeer.h"
#include "processnetworkpacket.h"
#include "socketlayer.h"
#include "packet.h"
#include "reliability.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static
void ShortResponse(struct CRakPeer *rp, int host, int port, char id)
{
char buf[2];
buf[0] = id;
buf[1] = 0; /*padding cuz 1 byte packet may not get sent*/
SocketLayer__SendTo(rp->socket, buf, 2, host, port);
}
static
void HandleConnectedPlayerTryingToConnect(
struct CRakPeer *rp,
struct CRemoteSystem *remoteSystem,
int host,
int port)
{
if (remoteSystem->connectMode != CONNECTED &&
remoteSystem->connectMode != DISCONNECT_ASAP &&
remoteSystem->connectMode != DISCONNECT_ASAP_SILENTLY)
{
dprintf("is from connected player, ignore\n");
return;
}
dprintf("is from disconnected player, send that it failed\n");
ShortResponse(rp, host, port, ID_CONNECTION_ATTEMPT_FAILED);
}
static
void HandleConnectingPlayer(
struct CRakPeer *rp,
struct CRemoteSystem *rs,
int host,
short port,
char *data,
int length)
{
#define assignRemoteSystem RakPeer__AssignPlayerIDToRemoteSystemList
struct CPacket *packet;
struct PlayerID playerId;
short playerIndex;
/*connection cookies, but we don't do that*/
if (rs != NULL) {
dprintf("connection request is from already connected playa\n");
HandleConnectedPlayerTryingToConnect(rp, rs, host, port);
return;
}
dprintf("it's a new player\n");
playerId.binaryAddress = host;
playerId.port = port;
if (!assignRemoteSystem(rp, playerId, UNVERIFIED_SENDER)) {
dprintf("server is full\n");
ShortResponse(rp, host, port, ID_NO_FREE_INCOMING_CONNECTIONS);
return;
}
playerIndex = RakPeer__GetIndexFromPlayerID(rp, playerId, 1);
dprintf("assigned index %hd\n", playerIndex);
packet = Packet__AllocPacket(1);
packet->playerId = playerId;
packet->bitSize = 8;
packet->playerIndex = playerIndex;
packet->ptrData[0] = ID_OPEN_CONNECTION_REQUEST;
RakPeer__AddPacketToProducer(rp, packet);
ShortResponse(rp, host, port, ID_OPEN_CONNECTION_REPLY);
}
void __stdcall ProcessNetworkPacket(
int host,
short port,
char *data,
int length,
struct CRakPeer *rp)
{
struct CRemoteSystem *rs;
struct PlayerID playerId;
int isPacketFlood;
playerId.binaryAddress = host;
playerId.port = port;
if (data[0] == ID_OPEN_CONNECTION_REQUEST && length == 3) {
dprintf("got connection request\n");
rs = RakPeer__GetRemoteSystemFromPlayerID(rp, playerId, 1, 1);
HandleConnectingPlayer(rp, rs, host, port, data, length);
return;
}
rs = RakPeer__GetRemoteSystemFromPlayerID(rp, playerId, 1, 1);
if (!rs) {
dprintf("incoming from UNCONNECTED, ignoring\n");
return;
}
ReliabilityLayer__HandleSocketReceiveFromConnectedPlayer(
(void*) &rs->reliabilityLayer,
data,
length,
playerId,
&rp->messageHandlerList,
rp->MTUSize,
&isPacketFlood);
}

11
processnetworkpacket.h Normal file
View File

@ -0,0 +1,11 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#define RP_PARSE_NETWORK_PACKET ((void*) 0x462C21)
void __stdcall ProcessNetworkPacket(
int binaryAddress,
short port,
char *data,
int length,
struct CRakPeer *rakPeer);

View File

@ -9,10 +9,70 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
__declspec(naked)
int __stdcall RakPeer__GetIndexFromPlayerID(
struct CRakPeer *this,
struct PlayerID playerId,
char calledFromNetworkThread)
{
_asm {
pop eax
pop ecx
push eax
mov eax, 0x453A40
jmp eax
}
}
__declspec(naked)
int __stdcall RakPeer__AddPacketToProducer(
struct CRakPeer *this,
struct CPacket *packet)
{
_asm {
pop eax
pop ecx
push eax
mov eax, 0x4523D0
jmp eax
}
}
__declspec(naked)
int __stdcall RakPeer__AssignPlayerIDToRemoteSystemList(
struct CRakPeer *this,
struct PlayerID playerId,
int connectionMode)
{
_asm {
pop eax
pop ecx
push eax
mov eax, 0x455E00
jmp eax
}
}
__declspec(naked)
struct CRemoteSystem* __stdcall RakPeer__GetRemoteSystemFromPlayerID(
struct CRakPeer *this,
struct PlayerID playerId,
char calledFromNetworkThread,
char onlyActive)
{
_asm {
pop eax
pop ecx
push eax
mov eax, 0x453AF0
jmp eax
}
}
static static
int __declspec(naked) RakPeer__OnConnectionRequest( int __declspec(naked) RakPeer__OnConnectionRequest(
struct CRakPeer *this, struct CRakPeer *this,
struct CRakPeer__RemoteSystemStruct *remoteSystem, struct CRemoteSystem *remoteSystem,
char *AESKey, char *AESKey,
char setAESkey) char setAESkey)
{ {
@ -88,7 +148,7 @@ short __declspec(naked) RakPeer__AllowIncomingConnectionsInternal(
static static
void RakPeer__ParseConnectionRequestPacketInternal( void RakPeer__ParseConnectionRequestPacketInternal(
struct CRakPeer *this, struct CRakPeer *this,
struct CRakPeer__RemoteSystemStruct *remoteSystem, struct CRemoteSystem *remoteSystem,
struct PlayerID playerId, struct PlayerID playerId,
char *data, char *data,
char byteSize) char byteSize)

View File

@ -16,11 +16,12 @@
#define ID_CONNECTION_REQUEST 0x0B #define ID_CONNECTION_REQUEST 0x0B
#define ID_OPEN_CONNECTION_REQUEST 0x18 #define ID_OPEN_CONNECTION_REQUEST 0x18
#define ID_OPEN_CONNECTION_REPLY 0x19 #define ID_OPEN_CONNECTION_REPLY 0x19
#define ID_CONNECTION_ATTEMPT_FAILED 0x1D #define ID_CONNECTION_ATTEMPT_FAILED 0x1D /*results in server did not respond*/
#define ID_NEW_INCOMING_CONNECTION 0x1E #define ID_NEW_INCOMING_CONNECTION 0x1E
#define ID_NO_FREE_INCOMING_CONNECTIONS 0x1F #define ID_NO_FREE_INCOMING_CONNECTIONS 0x1F
#define ID_CONNECTION_BANNED 0x24 #define ID_CONNECTION_BANNED 0x24
#define ID_INVALID_PASSWORD 0x25 #define ID_INVALID_PASSWORD 0x25
#define ID_MODIFIED_PACKET 0x26
#define SYSTEM_PRIORITY 0 #define SYSTEM_PRIORITY 0
#define HIGH_PRIORITY 1 #define HIGH_PRIORITY 1
@ -40,9 +41,9 @@ struct PlayerID {
}; };
EXPECT_SIZE(struct PlayerID, 0x8); EXPECT_SIZE(struct PlayerID, 0x8);
struct CRakPeer__RemoteSystemStruct { struct CRemoteSystem {
char _pad0[0x10-0x0]; char _pad0[0x10-0x0];
void *reliabilityLayer; int reliabilityLayer;
char _pad14[0x778-0x14]; char _pad14[0x778-0x14];
int lowestPing; int lowestPing;
int nextPingTime; int nextPingTime;
@ -70,17 +71,41 @@ struct CRakPeer {
unsigned char incomingPasswordLength; unsigned char incomingPasswordLength;
char _pad33B[0x33C-0x33B]; char _pad33B[0x33C-0x33B];
struct CRakPeer__RemoteSystemStruct *remoteSystemList; struct CRakPeer__RemoteSystemStruct *remoteSystemList;
char _pad340[0x7DC-0x340]; char _pad340[0x3B8-0x340];
int messageHandlerList;
char _pad3BC[0x7DC-0x3BC];
void *inputTree; /*HuffmanEncodingTree**/ void *inputTree; /*HuffmanEncodingTree**/
void *outputTree; /*HuffmanEncodingTree**/ void *outputTree; /*HuffmanEncodingTree**/
char _pad7E4[0xC0C-0x7E4]; char _pad7E4[0xC0C-0x7E4];
int MTUSize; int MTUSize;
char _padC10[0xD95-0xC10]; char _padC18[0xC18-0xC10];
void *socket;
char _padC1C[0xD95-0xC1C];
char usingSecurity; char usingSecurity;
/*incomplete*/ /*incomplete*/
}; };
#pragma pack(pop) #pragma pack(pop)
int __stdcall RakPeer__GetIndexFromPlayerID(
struct CRakPeer *this,
struct PlayerID playerId,
char calledFromNetworkThread);
int __stdcall RakPeer__AddPacketToProducer(
struct CRakPeer *this,
struct CPacket *packet);
int __stdcall RakPeer__AssignPlayerIDToRemoteSystemList(
struct CRakPeer *this,
struct PlayerID playerId,
int connectionMode);
struct CRemoteSystem* __stdcall RakPeer__GetRemoteSystemFromPlayerID(
struct CRakPeer *this,
struct PlayerID playerId,
char calledFromNetworkThread,
char onlyActive);
/** /**
int __thiscall RakPeer__ParseConnectionRequestPacket( int __thiscall RakPeer__ParseConnectionRequestPacket(
CRakPeer *this, CRakPeer *this,

25
reliability.c Normal file
View File

@ -0,0 +1,25 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#include "common.h"
#include "rakpeer.h"
#include "reliability.h"
__declspec(naked)
int __stdcall ReliabilityLayer__HandleSocketReceiveFromConnectedPlayer(
struct CReliabilityLayer *this,
char *buffer,
int length,
struct PlayerID playerId,
void *messageHandlerList,
int MTUSize,
int *ptrOutIsPacketFlood)
{
_asm {
pop eax
pop ecx
push eax
mov eax, 0x45F7E0
jmp eax
}
}

11
reliability.h Normal file
View File

@ -0,0 +1,11 @@
/* vim: set filetype=c ts=8 noexpandtab: */
int __stdcall ReliabilityLayer__HandleSocketReceiveFromConnectedPlayer(
struct CReliabilityLayer *this,
char *buffer,
int length,
struct PlayerID playerId,
void *messageHandlerList,
int MTUSize,
int *ptrOutIsPacketFlood);

View File

@ -101,10 +101,26 @@
RelativePath=".\nethandler.c" RelativePath=".\nethandler.c"
> >
</File> </File>
<File
RelativePath=".\packet.c"
>
</File>
<File
RelativePath=".\packet.h"
>
</File>
<File <File
RelativePath="plugin.c" RelativePath="plugin.c"
> >
</File> </File>
<File
RelativePath=".\processnetworkpacket.c"
>
</File>
<File
RelativePath=".\processnetworkpacket.h"
>
</File>
<File <File
RelativePath=".\rakpeer.c" RelativePath=".\rakpeer.c"
> >
@ -113,6 +129,22 @@
RelativePath=".\rakpeer.h" RelativePath=".\rakpeer.h"
> >
</File> </File>
<File
RelativePath=".\reliability.c"
>
</File>
<File
RelativePath=".\reliability.h"
>
</File>
<File
RelativePath=".\socketlayer.c"
>
</File>
<File
RelativePath=".\socketlayer.h"
>
</File>
<File <File
RelativePath=".\uncompress.c" RelativePath=".\uncompress.c"
> >

18
socketlayer.c Normal file
View File

@ -0,0 +1,18 @@
/* vim: set filetype=c ts=8 noexpandtab: */
#include "socketlayer.h"
__declspec(naked)
int __stdcall SocketLayer__SendTo(
void *socket,
char *buf,
int len,
int host,
short port)
{
_asm {
mov eax, 0x462C50
jmp eax
}
}

9
socketlayer.h Normal file
View File

@ -0,0 +1,9 @@
/* vim: set filetype=c ts=8 noexpandtab: */
int __stdcall SocketLayer__SendTo(
void *socket,
char *buf,
int len,
int host,
short port);