Compressed all game packets

This commit is contained in:
MysterD 2023-04-16 18:18:18 -07:00
parent 4dec136324
commit 9b71c30661
4 changed files with 54 additions and 4 deletions

Binary file not shown.

View File

@ -309,8 +309,15 @@ void network_send_to(u8 localIndex, struct Packet* p) {
if (p->keepSendingAfterDisconnect) {
localIndex = 0; // Force this type of packet to use the saved addr
}
int rc = gNetworkSystem->send(localIndex, p->addr, p->buffer, p->cursor + sizeof(u32));
if (rc == SOCKET_ERROR) { LOG_ERROR("send error %d", rc); return; }
u8* buffer = NULL;
u32 len = 0;
packet_compress(p, &buffer, &len);
if (!buffer || len == 0) {
LOG_ERROR("Failed to compress!");
} else {
int rc = gNetworkSystem->send(localIndex, p->addr, buffer, len);
if (rc == SOCKET_ERROR) { LOG_ERROR("send error %d", rc); return; }
}
}
p->sent = true;
@ -375,14 +382,16 @@ void network_receive(u8 localIndex, void* addr, u8* data, u16 dataLength) {
.buffer = { 0 },
.dataLength = dataLength,
};
memcpy(p.buffer, data, dataLength);
if (!packet_decompress(&p, data, dataLength)) {
LOG_ERROR("Failed to decompress!");
return;
}
if (localIndex != UNKNOWN_LOCAL_INDEX && localIndex != 0) {
gNetworkPlayers[localIndex].lastReceived = clock_elapsed();
}
// subtract and check hash
p.dataLength -= sizeof(u32);
if (!packet_check_hash(&p)) {
LOG_ERROR("invalid packet hash!");
return;

View File

@ -1,8 +1,47 @@
#include <stdio.h>
#include <zlib.h>
#include "../network.h"
#include "pc/network/ban_list.h"
#include "pc/debuglog.h"
static u32 sCompBufferLen = 0;
static Bytef* sCompBuffer = NULL;
static void increase_comp_buffer(u32 compressedLen) {
if (compressedLen <= sCompBufferLen && sCompBuffer) { return; }
if (sCompBuffer != NULL) { free(sCompBuffer); }
sCompBufferLen = compressedLen;
sCompBuffer = (Bytef*)malloc(sCompBufferLen);
}
void packet_compress(struct Packet* p, u8** compBuffer, u32* compSize) {
uLong sourceSize = p->dataLength + sizeof(u32);
uLongf compressedLen = compressBound(sourceSize);
increase_comp_buffer(PACKET_LENGTH);
if (sCompBuffer && compress2((Bytef*)sCompBuffer, &compressedLen, (Bytef*)p->buffer, sourceSize, Z_BEST_COMPRESSION) == Z_OK) {
*compBuffer = sCompBuffer;
*compSize = compressedLen;
} else {
*compBuffer = NULL;
*compSize = 0;
}
}
bool packet_decompress(struct Packet* p, u8* compBuffer, u32 compSize) {
increase_comp_buffer(PACKET_LENGTH);
if (!sCompBuffer) { return false; }
uLong decompSize = PACKET_LENGTH;
if (uncompress((Bytef*)p->buffer, &decompSize, (Bytef*)compBuffer, compSize) == Z_OK) {
p->dataLength = decompSize - sizeof(u32);
return true;
} else {
return false;
}
}
void packet_process(struct Packet* p) {
if (gNetworkType == NT_NONE) { return; }
if (p->levelAreaMustMatch) {

View File

@ -148,6 +148,8 @@ struct LSTNetworkType {
};
// packet.c
void packet_compress(struct Packet* p, u8** compBuffer, u32* compSize);
bool packet_decompress(struct Packet* p, u8* compBuffer, u32 compSize);
void packet_process(struct Packet* p);
void packet_receive(struct Packet* packet);
bool packet_spoofed(struct Packet* p, u8 globalIndex);