make packets big endian

This commit is contained in:
Lorenzo Torres 2026-01-08 19:18:11 +01:00
parent 238eff5bb3
commit 6055103cdc

View file

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <arpa/inet.h>
#include "network.h" #include "network.h"
#include "client.h" #include "client.h"
#include "password.h" #include "password.h"
@ -95,7 +96,7 @@ static void send_packet(struct client *client, uint8_t type, const void *data, s
if (!buf) return; if (!buf) return;
struct packet_header *hdr = (struct packet_header *)buf; struct packet_header *hdr = (struct packet_header *)buf;
hdr->size = (uint16_t)total_len; hdr->size = htons((uint16_t)total_len);
hdr->type = type; hdr->type = type;
if (data && data_len > 0) { if (data && data_len > 0) {
@ -633,7 +634,9 @@ void network_handle_data(struct client *client, const char *data, size_t len)
return; return;
} }
const struct packet_header *hdr = (const struct packet_header *)data;
struct packet_header *hdr = (struct packet_header *)data;
hdr->size = ntohs(hdr->size);
if (hdr->size > len || hdr->size < sizeof(struct packet_header)) { if (hdr->size > len || hdr->size < sizeof(struct packet_header)) {
send_error(client, ERR_INVALID_PACKET); send_error(client, ERR_INVALID_PACKET);
@ -643,6 +646,7 @@ void network_handle_data(struct client *client, const char *data, size_t len)
const char *payload = data + sizeof(struct packet_header); const char *payload = data + sizeof(struct packet_header);
size_t payload_len = hdr->size - sizeof(struct packet_header); size_t payload_len = hdr->size - sizeof(struct packet_header);
switch (hdr->type) { switch (hdr->type) {
case PACKET_REGISTER: case PACKET_REGISTER:
handle_register(client, payload, payload_len); handle_register(client, payload, payload_len);