Feature: Better log printind and lots more of parsing

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-10-17 15:32:35 +01:00
parent 529469e2e3
commit 1fe0452ad6
5 changed files with 144 additions and 62 deletions

Binary file not shown.

View File

@ -49,6 +49,18 @@ int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok
return (int32_t)ntohl(v);
}
void read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, float *out, int *ok) {
if (!ensure(recv_len, *offset, sizeof(float))) {
*ok = 0;
return;
}
uint32_t v;
memcpy(&v, buf + *offset, sizeof(v));
*offset += sizeof(v);
v = ntohl(v);
memcpy(out, &v, sizeof(float));
}
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok) {
if (!ensure(recv_len, *offset, len)) {
*ok = 0;

View File

@ -33,6 +33,10 @@ u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int
// Advances the offset by 4 bytes.
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
// Reads a 32-bit float from the buffer in network byte order.
// Advances the offset by 4 bytes.
void read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, float *out, int *ok);
// Reads a fixed number of bytes into the output buffer.
// The output buffer must be at least 'len' bytes long.
void read_bytes(const u_int8_t *buf, size_t recv_len, size_t *offset, u_int8_t *out, size_t len, int *ok);

View File

@ -62,6 +62,8 @@ struct postion {
struct carAtributes {
// Related to ACSP_CAR_INFO
u_char isConnected; // 1 = connected, 0 = disconnected
u_char isLoading; // 1 = loading, 0 = not loading
char *car_model;
char *car_skin;
char *driver_name;
@ -74,6 +76,10 @@ struct carAtributes {
postion velocity;
u_int8_t carGear;
u_int16_t carRPM;
u_int32_t lap_time;
u_int32_t cuts;
u_int32_t total_cuts;
u_int32_t total_cuts_alltime;
float_t normalizedSplinePos;
} __attribute__((packed));
@ -104,7 +110,7 @@ struct trackAtributes {
u_int8_t road_temp;
char *weather_graphics;
int32_t elapsed_ms;
u_int32_t elapsed_ms;
} __attribute__((packed));
enum ACSP_MessageType {

View File

@ -12,6 +12,8 @@
#include "server_structs.h"
#include "socket.h"
#define DEBUG_CAR_INFO 0
const int SERVER_OUT_PORT = 12000;
const int SERVER_IN_PORT = 11000;
const char *SERVER_OUT_IP = "127.0.0.1";
@ -19,17 +21,8 @@ const char *SERVER_OUT_IP = "127.0.0.1";
const int MAX_PLAYERS = 64;
void init_carupdate(carAtributes *car) {
car->carID = 0;
car->position.x = 0.0f;
car->position.y = 0.0f;
car->position.z = 0.0f;
car->velocity.x = 0.0f;
car->velocity.y = 0.0f;
car->velocity.z = 0.0f;
car->carGear = 0;
car->carRPM = 0;
car->normalizedSplinePos = 0.0f;
car->isConnected = 0;
car->isLoading = 0;
car->car_model = (char *)malloc(64);
car->driver_GUID = (char *)malloc(64);
@ -42,6 +35,24 @@ void init_carupdate(carAtributes *car) {
strcpy(car->car_skin, "");
strcpy(car->driver_name, "");
strcpy(car->driver_team, "");
car->carID = 0;
car->position.x = 0.0f;
car->position.y = 0.0f;
car->position.z = 0.0f;
car->velocity.x = 0.0f;
car->velocity.y = 0.0f;
car->velocity.z = 0.0f;
car->carGear = 0;
car->carRPM = 0;
car->lap_time = 0;
car->cuts = 0;
car->total_cuts = 0;
car->total_cuts_alltime = 0; // TODO: SQL querry to get total cuts of all time;
car->normalizedSplinePos = 0.0f;
}
int main(void) {
@ -81,29 +92,31 @@ int main(void) {
char message[124] = {0}; // just a place to put info messages from the server itself
printf("\n");
while (1) {
offset = 0;
u_int8_t str_len_8 = 0;
memset(buffer, 0, sizeof(buffer));
// FIX: Its not garanted to receive a full packet in one recvfrom call, but the error -1 is not handled by a unsigned size_t
size_t recv_bytes = (size_t)recvfrom(recv_sock_fd, buffer, sizeof(buffer), 0, NULL, NULL);
if (recv_bytes < 0) {
perror("[!] recvfrom() failed");
close(send_sock_fd);
close(recv_sock_fd);
return EXIT_FAILURE;
}
// Convert buffer to utf-8 string and print it
#if DEBUG_CAR_INFO
printf("[+] Received %zd bytes from client\n", recv_bytes);
#endif
// The first byte of the messages is always an ACSP_MessageType
switch ((int)buffer[0]) {
// ============================
// SERVER → CLIENT MESSAGES
// ============================
// ============================
// SERVER → CLIENT MESSAGES
// ============================
case ACSP_SESSION_INFO:
printf("[+] Message Type: ACSP_SESSION_INFO\n");
goto skip_message;
case ACSP_NEW_SESSION: // DONE
printf("[+] Message Type: ACSP_NEW_SESSION\n");
skip_message:
offset = 1; // Reset offset to 1 to read after message types
trackInfo.protocol_version = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
@ -135,7 +148,8 @@ int main(void) {
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
read_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.weather_graphics, str_len_8, &ok);
trackInfo.elapsed_ms = read_int32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
// FIX: elapsed_ms was/is incorrectly read
trackInfo.elapsed_ms = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
if (!ok) {
fprintf(stderr, "[-] Error parsing NEW_SESSION packet (offset=%zu)\n", offset);
@ -153,11 +167,12 @@ int main(void) {
printf("\tType: %d\tTime: %d mins\tLaps: %d\tWait Time: %d secs\n", trackInfo.typ, trackInfo.time, trackInfo.laps, trackInfo.wait_time);
printf("\tAmbient Temp: %d C\tRoad Temp: %d C\n", trackInfo.ambient_temp, trackInfo.road_temp);
printf("\tWeather Graphics: %s\n", trackInfo.weather_graphics);
printf("\tElapsed Time: %d ms\n", trackInfo.elapsed_ms);
printf("\tElapsed Time: %d ms (%2d:%2d:%2d)\n\n", trackInfo.elapsed_ms, trackInfo.elapsed_ms / 60000, (trackInfo.elapsed_ms % 60000) / 1000,
(trackInfo.elapsed_ms % 60000) % 1000);
break;
case ACSP_NEW_CONNECTION: //DONE
case ACSP_NEW_CONNECTION: // DONE:
printf("[+] Message Type: ACSP_NEW_CONNECTION\n");
offset = 1; // Reset offset to 1 to read after message types
@ -181,12 +196,13 @@ int main(void) {
}
update.isConnected = 1; // Mark as connected
update.isLoading = 1; // Mark as loading
printf("\tDriver Name: \"%s\"\n", update.driver_name);
printf("\tDriver GUID: \"%s\"\n", update.driver_GUID);
printf("\tCar ID: %d\n", update.carID);
printf("\tCar Model: \"%s\"\n", update.car_model);
printf("\tCar Skin: \"%s\"\n", update.car_skin);
printf("\tCar Skin: \"%s\"\n\n", update.car_skin);
// Store player player into the respective Index
if (update.carID < MAX_PLAYERS) {
@ -195,7 +211,7 @@ int main(void) {
break;
case ACSP_CONNECTION_CLOSED: // DONE
case ACSP_CONNECTION_CLOSED: // DONE:
printf("[+] Message Type: ACSP_CONNECTION_CLOSED\n");
offset = 1; // Reset offset to 1 to read after message types
@ -224,7 +240,7 @@ int main(void) {
printf("\tDriver GUID: \"%s\"\n", update.driver_GUID);
printf("\tCar ID: %d\n", update.carID);
printf("\tCar Model: \"%s\"\n", update.car_model);
printf("\tCar Skin: \"%s\"\n", update.car_skin);
printf("\tCar Skin: \"%s\"\n\n", update.car_skin);
// Store player player into the respective Index
if (update.carID < MAX_PLAYERS) {
@ -233,10 +249,13 @@ int main(void) {
break;
case ACSP_CAR_UPDATE: // DONE
case ACSP_CAR_UPDATE: // DONE:
#if DEBUG_CAR_INFO
printf("[+] Message Type: ACSP_CAR_UPDATE\n");
#endif
offset = 1;
memcpy(&update.carID, buffer + 1, sizeof(uint8_t));
memcpy(&update.carID, buffer, sizeof(uint8_t));
offset = 1 + sizeof(uint8_t);
memcpy(&update.position, buffer + offset, sizeof(postion));
offset += sizeof(postion);
@ -249,30 +268,25 @@ int main(void) {
memcpy(&update.normalizedSplinePos, buffer + offset, sizeof(float_t));
offset += sizeof(float_t);
#if DEBUG_CAR_INFO
printf("\tCar %d Position: X: %.2f Y: %.2f Z: %.2f ", update.carID, update.position.x, update.position.y, update.position.z);
printf("Gear: %d RPM: %d\n", update.carGear, update.carRPM);
#endif
// Store player player into the respective index
if (update.carID < MAX_PLAYERS) {
memcpy(&players[update.carID].position, &update.position, sizeof(postion));
memcpy(&players[update.carID].velocity, &update.velocity, sizeof(postion));
players[update.carID].carGear = update.carGear;
players[update.carID].carRPM = update.carRPM;
players[update.carID].normalizedSplinePos = update.normalizedSplinePos;
}
// Store player player into the respective index
if (update.carID < MAX_PLAYERS) {
memcpy(&players[update.carID].position, &update.position, sizeof(postion));
memcpy(&players[update.carID].velocity, &update.velocity, sizeof(postion));
players[update.carID].carGear = update.carGear;
players[update.carID].carRPM = update.carRPM;
players[update.carID].normalizedSplinePos = update.normalizedSplinePos;
}
break;
case ACSP_CAR_INFO: // DONE
case ACSP_CAR_INFO: // DONE:
printf("[+] Message Type: ACSP_CAR_INFO\n");
offset = 1; // Reset offset to 1 to read after message types;
//
// is_connected = self.br.read_byte() != 0
// car_model = self.br.read_utf_string()
// car_skin = self.br.read_utf_string()
// driver_name = self.br.read_utf_string()
// driver_team = self.br.read_utf_string()
// driver_guid = self.br.read_utf_string()
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
update.isConnected = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
@ -298,7 +312,7 @@ int main(void) {
printf("\tCar Skin: \"%s\"\n", update.car_skin);
printf("\tDriver Name: \"%s\"\n", update.driver_name);
printf("\tDriver Team: \"%s\"\n", update.driver_team);
printf("\tDriver GUID: \"%s\"\n", update.driver_GUID);
printf("\tDriver GUID: \"%s\"\n\n", update.driver_GUID);
// Store player player into the respective Index
if (update.carID < MAX_PLAYERS) {
@ -307,44 +321,87 @@ int main(void) {
break;
case ACSP_END_SESSION: // DONE-ish (only session type cycling)
case ACSP_END_SESSION: // DONE: (only session type cycling)
printf("[+] Message Type: ACSP_END_SESSION\n");
// Advance session_type to the next in the enum;
trackInfo.session_type = (SessionType)((trackInfo.session_type + 1) % 3);
// Advance session_type to the next in the enum;
printf("\tNext Session Type: %d\n", trackInfo.session_type);
trackInfo.session_type = (SessionType)((trackInfo.session_type + 1) % 3);
if (trackInfo.session_type == 0) {
memcpy(trackInfo.session_name, "Practice", 9);
} else if (trackInfo.session_type == 1) {
memcpy(trackInfo.session_name, "Race", 5);
} else if (trackInfo.session_type == 2) {
memcpy(trackInfo.session_name, "Qualify", 8);
}
printf("\tNext Session Name: %s\n", trackInfo.session_name);
printf("\tNext Session Type: %d\n\n", trackInfo.session_type);
break;
case ACSP_VERSION: // TODO
case ACSP_VERSION: // DONE:
printf("[+] Message Type: ACSP_VERSION\n");
offset = 1; // Reset offset to 1 to read after message types
trackInfo.protocol_version = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
printf("\tProtocol Version: %d\n\n", trackInfo.protocol_version);
break;
case ACSP_CHAT: // TODO
case ACSP_CHAT: // DONE: Receive chat messages
printf("[+] Message Type: ACSP_CHAT\n");
offset = 1; // Reset offset to 1 to read after message types
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, message, str_len_8, &ok);
printf("\tCar ID: %d (%s)\n", update.carID, players[update.carID].driver_name);
printf("\tMessage: \"%s\"\n\n", message);
break;
case ACSP_CLIENT_LOADED: // TODO
case ACSP_CLIENT_LOADED: // DONE: Check for client loaded status
printf("[+] Message Type: ACSP_CLIENT_LOADED\n");
offset = 1; // Reset offset to 1 to read after message types
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
players[update.carID].isLoading = 0;
printf("\tCar ID: %d (%s) Finished Loading into the session\n\n", update.carID, players[update.carID].driver_name);
break;
case ACSP_SESSION_INFO: // TODO
printf("[+] Message Type: ACSP_SESSION_INFO\n");
break;
case ACSP_ERROR: // DONE
case ACSP_ERROR: // DONE: Simple error message from server
printf("[+] Message Type: ACSP_ERROR\n");
offset = 1; // Reset offset to 1 to read after message types
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
read_utf32le_string((const u_int8_t *)buffer, recv_bytes, &offset, message, str_len_8, &ok);
printf("\tServer Message: \"%s\"\n", message);
printf("\tServer Message: \"%s\"\n\n", message);
break;
case ACSP_LAP_COMPLETED: // TODO
case ACSP_LAP_COMPLETED: // TODO: Handle lap completed events
printf("[+] Message Type: ACSP_LAP_COMPLETED\n");
offset = 1; // Reset offset to 1 to read after message types
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
// TEST: Possiblity of lap_time not behing correct
// TODO: Verify with actual server
update.lap_time = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
update.cuts = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
update.total_cuts = read_uint32((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
printf("\tCar ID: %d (%s)\n", update.carID, players[update.carID].driver_name);
printf("\tLap Time: %5d ms\n", update.lap_time);
printf("\tCuts this lap: %d\n", update.cuts);
printf("\tTotal Cuts (this session): %d\n\n", update.total_cuts);
break;
// ============================
@ -365,6 +422,9 @@ int main(void) {
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_ENV\n");
break;
// TODO: Add for ranking system
// OPTIMIZE: Make sure DB queries are optimized for speed has there can be more that 30 players querying at the same time
//
// ============================
// CLIENT → SERVER COMMANDS
// ============================
@ -412,7 +472,7 @@ int main(void) {
// DEFAULT HANDLER
// ============================
default:
printf("[!] Unknown Message Type: %d\n", buffer[0]);
printf("[!] Unknown Message Type: %d\n\n", buffer[0]);
break;
}