generated from AfonsoCMSousa/CPP-Template
Compare commits
10
Commits
WindowsBuild
..
v1.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e94afdfd9b | ||
|
|
650977cfb9 | ||
|
|
67d51489f0 | ||
|
|
8bf502a237 | ||
|
|
f5bf01b1f8 | ||
|
|
d38dbac8f4 | ||
|
|
2df0838be8 | ||
|
|
1fe0452ad6 | ||
|
|
529469e2e3 | ||
|
|
42df9f79fe |
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,141 @@
|
|||||||
|
#include "parcer.h"
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int ensure(size_t recv_len, size_t offset, size_t need) {
|
||||||
|
return (offset + need <= recv_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, sizeof(uint8_t))) {
|
||||||
|
*ok = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
u_int8_t v;
|
||||||
|
memcpy(&v, buf + *offset, sizeof(v));
|
||||||
|
*offset += sizeof(v);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, sizeof(uint16_t))) {
|
||||||
|
*ok = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
u_int16_t v;
|
||||||
|
memcpy(&v, buf + *offset, sizeof(v));
|
||||||
|
*offset += sizeof(v);
|
||||||
|
return (u_int16_t)ntohs(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, sizeof(uint32_t))) {
|
||||||
|
*ok = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
u_int32_t v;
|
||||||
|
memcpy(&v, buf + *offset, sizeof(v));
|
||||||
|
*offset += sizeof(v);
|
||||||
|
return (u_int32_t)ntohl(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t read_int32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, sizeof(int32_t))) {
|
||||||
|
*ok = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int32_t v;
|
||||||
|
memcpy(&v, buf + *offset, sizeof(v));
|
||||||
|
*offset += sizeof(v);
|
||||||
|
return (int32_t)ntohl(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, sizeof(float))) {
|
||||||
|
*ok = 0;
|
||||||
|
return 0.0f;
|
||||||
|
}
|
||||||
|
u_int32_t v_int;
|
||||||
|
memcpy(&v_int, buf + *offset, sizeof(v_int));
|
||||||
|
*offset += sizeof(v_int);
|
||||||
|
v_int = ntohl(v_int);
|
||||||
|
float v_float;
|
||||||
|
memcpy(&v_float, &v_int, sizeof(v_float));
|
||||||
|
return v_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;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(out, buf + *offset, len);
|
||||||
|
*offset += len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
|
||||||
|
size_t i = *offset;
|
||||||
|
size_t j = 0;
|
||||||
|
|
||||||
|
while (i + 3 < buf_size && j < max_len) {
|
||||||
|
uint32_t codeunit = buffer[i] | (buffer[i + 1] << 8) | (buffer[i + 2] << 16) | (buffer[i + 3] << 24);
|
||||||
|
|
||||||
|
if (codeunit == 0) {
|
||||||
|
i += 4; // termina a string
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (codeunit < 0x80) {
|
||||||
|
dest[j++] = (char)codeunit;
|
||||||
|
} else {
|
||||||
|
dest[j++] = '?'; // substitui caracteres fora de ASCII
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest[j] = '\0';
|
||||||
|
*offset = i;
|
||||||
|
*ok = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok) {
|
||||||
|
size_t i = *offset;
|
||||||
|
size_t j = 0;
|
||||||
|
|
||||||
|
while (i + 1 < buf_size && j < max_len - 1) {
|
||||||
|
uint16_t codeunit = buffer[i] | (buffer[i + 1] << 8);
|
||||||
|
|
||||||
|
if (codeunit == 0) {
|
||||||
|
i += 2; // termina a string
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (codeunit < 0x80) {
|
||||||
|
dest[j++] = (char)codeunit;
|
||||||
|
} else {
|
||||||
|
dest[j++] = '?'; // substitui caracteres fora de ASCII
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
dest[j] = '\0';
|
||||||
|
*offset = i;
|
||||||
|
*ok = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t len, int *ok) {
|
||||||
|
if (!ensure(recv_len, *offset, len)) {
|
||||||
|
*ok = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i <= len; i++) {
|
||||||
|
out[i] = (char)buf[*offset + i];
|
||||||
|
}
|
||||||
|
out[len] = '\0'; // Ensure null termination
|
||||||
|
*offset += len;
|
||||||
|
*ok = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#ifndef PARCER_H
|
||||||
|
#define PARCER_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
// Ensures that there are at least 'need' bytes available in the buffer
|
||||||
|
// starting from 'offset'. Returns 1 if enough bytes are available, 0 otherwise.
|
||||||
|
int ensure(size_t recv_len, size_t offset, size_t need);
|
||||||
|
|
||||||
|
// Reads an 8-bit unsigned integer from the buffer.
|
||||||
|
// Advances the offset by 1 byte.
|
||||||
|
u_int8_t read_uint8(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||||
|
|
||||||
|
// Reads a 16-bit unsigned integer from the buffer in network byte order.
|
||||||
|
// Advances the offset by 2 bytes.
|
||||||
|
u_int16_t read_uint16(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||||
|
|
||||||
|
// Reads a 32-bit unsigned integer from the buffer in network byte order.
|
||||||
|
// Advances the offset by 4 bytes.
|
||||||
|
u_int32_t read_uint32(const u_int8_t *buf, size_t recv_len, size_t *offset, int *ok);
|
||||||
|
|
||||||
|
// Reads a 32-bit signed integer from the buffer in network byte order.
|
||||||
|
// 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.
|
||||||
|
float read_float(const u_int8_t *buf, size_t recv_len, size_t *offset, 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);
|
||||||
|
|
||||||
|
// Reads a length-prefixed string. The length is a single byte.
|
||||||
|
// The string is not null-terminated in the buffer, but the function
|
||||||
|
void read_utf32le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
|
||||||
|
|
||||||
|
// reads up to max_len - 1 characters and null-terminates the destination buffer.
|
||||||
|
// The string is encoded in UTF-32LE.
|
||||||
|
void read_utf16le_string(const uint8_t *buffer, size_t buf_size, size_t *offset, char *dest, size_t max_len, int *ok);
|
||||||
|
|
||||||
|
// Reads a null-terminated string from the buffer.
|
||||||
|
// The string is read into the output buffer, which must be at least max_len bytes long
|
||||||
|
void read_string(const u_int8_t *buf, size_t recv_len, size_t *offset, char *out, size_t max_len, int *ok);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // PARCER_H
|
||||||
+163
-36
@@ -1,53 +1,180 @@
|
|||||||
#ifndef SERVER_STRUCTS_H
|
#ifndef SERVER_STRUCTS_H
|
||||||
#define SERVER_STRUCTS_H
|
#define SERVER_STRUCTS_H
|
||||||
|
|
||||||
|
#include <cmath>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <sys/cdefs.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
struct handshake {
|
struct handshake {
|
||||||
// [not used in the current Remote Telemtry version by AC]
|
// [not used in the current Remote Telemtry version by AC]
|
||||||
// In future versions it will identify the platform type of the client.
|
// In future versions it will identify the platform type of the client.
|
||||||
// This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1))
|
// This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1))
|
||||||
int32_t identifier;
|
int32_t identifier;
|
||||||
|
|
||||||
// [not used in the current Remote Telemtry version by AC]
|
// [not used in the current Remote Telemtry version by AC]
|
||||||
// In future version this field will identify the AC Remote Telemetry version that the device expects to speak with.
|
// In future version this field will identify the AC Remote Telemetry version that the device expects to speak with.
|
||||||
int32_t version;
|
int32_t version;
|
||||||
|
|
||||||
// This is the type of operation required by the client.
|
// This is the type of operation required by the client.
|
||||||
// The following operations are now available:
|
// The following operations are now available:
|
||||||
// ----------------------------------------------------------------
|
// ----------------------------------------------------------------
|
||||||
// HANDSHAKE = 0 :
|
// HANDSHAKE = 0 :
|
||||||
// This operation identifier must be set when the client wants to start the comunication.
|
// This operation identifier must be set when the client wants to start the comunication.
|
||||||
//
|
//
|
||||||
// SUBSCRIBE_UPDATE = 1 :
|
// SUBSCRIBE_UPDATE = 1 :
|
||||||
// This operation identifier must be set when the client wants to be updated from the specific ACServer.
|
// This operation identifier must be set when the client wants to be updated from the specific ACServer.
|
||||||
//
|
//
|
||||||
// SUBSCRIBE_SPOT = 2 :
|
// SUBSCRIBE_SPOT = 2 :
|
||||||
// This operation identifier must be set when the client wants to be updated from the specific ACServer just for SPOT Events (e.g.: the end of a lap).
|
// This operation identifier must be set when the client wants to be updated from the specific ACServer just for SPOT Events (e.g.: the end of a lap).
|
||||||
//
|
//
|
||||||
// DISMISS = 3 :
|
// DISMISS = 3 :
|
||||||
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
// This operation identifier must be set when the client wants to leave the comunication with ACServer.
|
||||||
int32_t operationId;
|
int32_t operationId;
|
||||||
} __attribute__((packed));
|
} __attribute__((packed));
|
||||||
|
|
||||||
struct handshackerResponse{
|
struct handshackerResponse {
|
||||||
// is the name of the car that the player is driving on the AC Server
|
// is the name of the car that the player is driving on the AC Server
|
||||||
char carName[50];
|
char carName[50];
|
||||||
|
|
||||||
// is the name of the driver running on the AC Server
|
// is the name of the driver running on the AC Server
|
||||||
char driverName[50];
|
char driverName[50];
|
||||||
|
|
||||||
// for now is just 4242, this code will identify different status,
|
// for now is just 4242, this code will identify different status,
|
||||||
// as “NOT AVAILABLE” for connection
|
// as “NOT AVAILABLE” for connection
|
||||||
int32_t identifier;
|
int32_t identifier;
|
||||||
|
|
||||||
// for now is set to 1, this will identify the version running on the AC Server
|
// for now is set to 1, this will identify the version running on the AC Server
|
||||||
int32_t version;
|
int32_t version;
|
||||||
|
|
||||||
// is the name of the track on the AC Server
|
// is the name of the track on the AC Server
|
||||||
char trackName[50];
|
char trackName[50];
|
||||||
|
|
||||||
// is the track configuration on the AC Server
|
// is the track configuration on the AC Server
|
||||||
char trackConfig[50];
|
char trackConfig[50];
|
||||||
} __attribute__((packed));
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct postion {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
enum flag {
|
||||||
|
NO_FLAG = 0,
|
||||||
|
YELLOW_FLAG = 1,
|
||||||
|
BLUE_FLAG = 2,
|
||||||
|
BLACK_FLAG = 3,
|
||||||
|
CHECKERED_FLAG = 4,
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
struct carAtributes {
|
||||||
|
// Related to ACSP_CAR_INFO
|
||||||
|
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||||
|
u_char isLoading; // 1 = loading, 0 = not isLoading
|
||||||
|
|
||||||
|
char *car_model;
|
||||||
|
char *car_skin;
|
||||||
|
char *driver_name;
|
||||||
|
char *driver_team;
|
||||||
|
char *driver_GUID;
|
||||||
|
|
||||||
|
// Related to ACSP_CAR_UPDATE
|
||||||
|
u_int8_t carID;
|
||||||
|
postion position;
|
||||||
|
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;
|
||||||
|
u_int16_t total_laps_completed;
|
||||||
|
u_int16_t contacts;
|
||||||
|
u_int16_t total_contacts;
|
||||||
|
|
||||||
|
flag current_flag; // TODO: implement flag status updates
|
||||||
|
// TAG:3
|
||||||
|
|
||||||
|
float_t normalizedSplinePos;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
enum SessionType {
|
||||||
|
PRACTICE = 0,
|
||||||
|
RACE = 1,
|
||||||
|
QUALIFYING = 2,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct trackAtributes {
|
||||||
|
u_int8_t protocol_version;
|
||||||
|
|
||||||
|
u_int8_t session_index;
|
||||||
|
u_int8_t current_session_index;
|
||||||
|
u_int8_t session_count;
|
||||||
|
SessionType session_type;
|
||||||
|
|
||||||
|
char *server_name;
|
||||||
|
char *track;
|
||||||
|
char *track_config;
|
||||||
|
char *session_name;
|
||||||
|
|
||||||
|
u_int8_t typ;
|
||||||
|
u_int16_t time;
|
||||||
|
u_int16_t laps;
|
||||||
|
u_int16_t wait_time;
|
||||||
|
u_int8_t ambient_temp;
|
||||||
|
u_int8_t road_temp;
|
||||||
|
|
||||||
|
char *weather_graphics;
|
||||||
|
u_int32_t elapsed_ms;
|
||||||
|
} __attribute__((packed));
|
||||||
|
|
||||||
|
enum ACSP_MessageType {
|
||||||
|
// ============================
|
||||||
|
// PROTOCOL VERSION
|
||||||
|
// ============================
|
||||||
|
PROTOCOL_VERSION = 4,
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// SERVER → CLIENT MESSAGES
|
||||||
|
// ============================
|
||||||
|
ACSP_NEW_SESSION = 50,
|
||||||
|
ACSP_NEW_CONNECTION = 51,
|
||||||
|
ACSP_CONNECTION_CLOSED = 52,
|
||||||
|
ACSP_CAR_UPDATE = 53,
|
||||||
|
ACSP_CAR_INFO = 54, // Response to ACSP_GET_CAR_INFO
|
||||||
|
ACSP_END_SESSION = 55,
|
||||||
|
ACSP_VERSION = 56,
|
||||||
|
ACSP_CHAT = 57,
|
||||||
|
ACSP_CLIENT_LOADED = 58,
|
||||||
|
ACSP_SESSION_INFO = 59,
|
||||||
|
ACSP_ERROR = 60,
|
||||||
|
ACSP_LAP_COMPLETED = 73,
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// EVENTS
|
||||||
|
// ============================
|
||||||
|
ACSP_CLIENT_EVENT = 130,
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// EVENT TYPES
|
||||||
|
// ============================
|
||||||
|
ACSP_CE_COLLISION_WITH_CAR = 10,
|
||||||
|
ACSP_CE_COLLISION_WITH_ENV = 11,
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// CLIENT → SERVER COMMANDS
|
||||||
|
// ============================
|
||||||
|
ACSP_REALTIMEPOS_INTERVAL = 200,
|
||||||
|
ACSP_GET_CAR_INFO = 201,
|
||||||
|
ACSP_SEND_CHAT = 202, // Sends chat to one car
|
||||||
|
ACSP_BROADCAST_CHAT = 203, // Sends chat to everybody
|
||||||
|
ACSP_GET_SESSION_INFO = 204,
|
||||||
|
ACSP_SET_SESSION_INFO = 205,
|
||||||
|
ACSP_KICK_USER = 206,
|
||||||
|
ACSP_NEXT_SESSION = 207,
|
||||||
|
ACSP_RESTART_SESSION = 208,
|
||||||
|
ACSP_ADMIN_COMMAND = 209 // Send message plus a string
|
||||||
|
};
|
||||||
|
|
||||||
#endif // SERVER_STRUCTS_H
|
#endif // SERVER_STRUCTS_H
|
||||||
|
|||||||
+76
-70
@@ -1,84 +1,90 @@
|
|||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int connect_udp_socket(const char* ip, uint16_t port) {
|
// =========================
|
||||||
#ifdef _WIN32
|
// UDP SOCKET FUCNTIONS
|
||||||
static int wsa_initialized = 0;
|
// =========================
|
||||||
if (!wsa_initialized) {
|
|
||||||
WSADATA wsaData;
|
|
||||||
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
|
||||||
fprintf(stderr, "WSAStartup failed\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
wsa_initialized = 1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
// Create a UDP socket
|
||||||
if (sockfd < 0) {
|
// @return: Socket file descriptor, or -1 on error
|
||||||
perror("socket creation failed");
|
int create_udp_socket(void) {
|
||||||
return -1;
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
}
|
if (sockfd < 0) {
|
||||||
|
perror("[!] socket() failed");
|
||||||
struct sockaddr_in servaddr;
|
return -1;
|
||||||
memset(&servaddr, 0, sizeof(servaddr));
|
}
|
||||||
|
return sockfd;
|
||||||
servaddr.sin_family = AF_INET;
|
|
||||||
servaddr.sin_port = htons(port);
|
|
||||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
|
||||||
|
|
||||||
if (connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
|
||||||
perror("connection to the server failed");
|
|
||||||
CLOSESOCKET(sockfd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sockfd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int bind_udp_socket(int sockfd, const char* ip, uint16_t port) {
|
// Create a UDP socket and connect it to a specific IP and port
|
||||||
struct sockaddr_in servaddr;
|
// Used for sending data (no bind needed)
|
||||||
memset(&servaddr, 0, sizeof(servaddr));
|
// @param ip: Destination IP address as a string
|
||||||
|
// @return: Socket file descriptor, or -1 on error
|
||||||
|
int connect_udp_socket(const char *ip, uint16_t port) {
|
||||||
|
int sockfd = create_udp_socket();
|
||||||
|
if (sockfd < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
servaddr.sin_family = AF_INET;
|
struct sockaddr_in servaddr;
|
||||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
memset(&servaddr, 0, sizeof(servaddr));
|
||||||
servaddr.sin_port = htons(port);
|
servaddr.sin_family = AF_INET;
|
||||||
|
servaddr.sin_port = htons(port);
|
||||||
|
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||||
|
|
||||||
if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
||||||
perror("bind failed");
|
perror("[!] connect() failed");
|
||||||
CLOSESOCKET(sockfd);
|
close(sockfd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
printf("[+] Connected UDP socket to %s:%d\n", ip, port);
|
||||||
|
return sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create and bind a UDP socket to a local IP and port
|
||||||
|
// Used for listening for incoming data
|
||||||
|
// @param ip: Local IP address to bind to
|
||||||
|
// @param port: Local port to bind to
|
||||||
|
// @return: Socket file descriptor, or -1 on error
|
||||||
|
int create_bound_udp_socket(const char *ip, uint16_t port) {
|
||||||
|
int sockfd = create_udp_socket();
|
||||||
|
if (sockfd < 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
addr.sin_addr.s_addr = inet_addr(ip);
|
||||||
|
|
||||||
|
if (bind(sockfd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
|
||||||
|
perror("[!] bind() failed");
|
||||||
|
close(sockfd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("[+] Bound UDP socket on %s:%d\n", ip, port);
|
||||||
|
return sockfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a UDP message to the specified IP and port
|
||||||
|
// @param sockfd: Socket file descriptor
|
||||||
|
// @param message: Message to send
|
||||||
|
// @param dest_ip: Destination IP address as a string
|
||||||
|
// @param dest_port: Destination port
|
||||||
|
// @return: Number of bytes sent, or -1 on error
|
||||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
|
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port) {
|
||||||
struct sockaddr_in destaddr;
|
struct sockaddr_in destaddr;
|
||||||
memset(&destaddr, 0, sizeof(destaddr));
|
memset(&destaddr, 0, sizeof(destaddr));
|
||||||
|
destaddr.sin_family = AF_INET;
|
||||||
|
destaddr.sin_port = htons(dest_port);
|
||||||
|
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
||||||
|
|
||||||
destaddr.sin_family = AF_INET;
|
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&destaddr, sizeof(destaddr));
|
||||||
destaddr.sin_port = htons(dest_port);
|
if (n < 0) {
|
||||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
perror("[!] sendto() failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
printf("[+] Sent %zd bytes to %s:%d\n", n, dest_ip, dest_port);
|
||||||
int n = sendto(sockfd, message, (int)strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
return n;
|
||||||
#else
|
|
||||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (n < 0) {
|
|
||||||
perror("sendto failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return n;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
void cleanup_sockets() {
|
|
||||||
WSACleanup();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|||||||
+13
-16
@@ -5,24 +5,20 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <ctype.h>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#pragma comment(lib, "ws2_32.lib")
|
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||||
#define CLOSESOCKET closesocket
|
|
||||||
#else
|
#else
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#define CLOSESOCKET close
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
@@ -31,12 +27,13 @@ extern "C" {
|
|||||||
// Server hosts a UDP socket at 127.0.0.1:12000
|
// Server hosts a UDP socket at 127.0.0.1:12000
|
||||||
// Client sends a message to the server at 11000
|
// Client sends a message to the server at 11000
|
||||||
|
|
||||||
|
// UDP socket creation & management
|
||||||
|
int create_udp_socket(void);
|
||||||
int connect_udp_socket(const char *ip, uint16_t port);
|
int connect_udp_socket(const char *ip, uint16_t port);
|
||||||
|
int create_bound_udp_socket(const char *ip, uint16_t port);
|
||||||
|
|
||||||
int bind_udp_socket(int sockfd, const char *ip, uint16_t port);
|
// UDP messaging
|
||||||
|
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port);
|
||||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip,
|
|
||||||
uint16_t dest_port);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
+545
-84
@@ -1,108 +1,569 @@
|
|||||||
#include <cstdint>
|
#include <cmath>
|
||||||
#include <ctype.h>
|
#include <cstddef>
|
||||||
#include <iostream>
|
#include <cstdlib>
|
||||||
|
#include <iterator>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
#pragma comment(lib, "ws2_32.lib") // only needed for MSVC
|
|
||||||
#else
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
|
||||||
|
|
||||||
|
#include "parcer.h"
|
||||||
#include "server_structs.h"
|
#include "server_structs.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
// Cross-platform close macro
|
#define DEBUG_CAR_INFO 0
|
||||||
#ifdef _WIN32
|
|
||||||
#define CLOSESOCKET closesocket
|
|
||||||
#else
|
|
||||||
#define CLOSESOCKET close
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const int SERVER_OUT_PORT = 9996;
|
const int SERVER_OUT_PORT = 12000;
|
||||||
const int SERVER_IN_PORT = 11000;
|
const int SERVER_IN_PORT = 11000;
|
||||||
const char* SERVER_OUT_IP = "127.0.0.1";
|
const char *SERVER_OUT_IP = "127.0.0.1";
|
||||||
|
|
||||||
int main() {
|
const int MAX_PLAYERS = 64;
|
||||||
|
|
||||||
#ifdef _WIN32
|
void init_carupdate(carAtributes *car) {
|
||||||
WSADATA wsaData;
|
car->isConnected = 0;
|
||||||
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
car->isLoading = 0;
|
||||||
fprintf(stderr, "[-] WSAStartup failed\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
printf("[+] Starting server...\n");
|
car->car_model = (char *)malloc(64);
|
||||||
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
car->driver_GUID = (char *)malloc(64);
|
||||||
printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
car->car_skin = (char *)malloc(64);
|
||||||
|
car->driver_name = (char *)malloc(64);
|
||||||
|
car->driver_team = (char *)malloc(64);
|
||||||
|
|
||||||
// Create UDP socket
|
strcpy(car->car_model, "");
|
||||||
int sock_FD = connect_udp_socket(SERVER_OUT_IP, SERVER_OUT_PORT);
|
strcpy(car->driver_GUID, "");
|
||||||
if (sock_FD < 0) {
|
strcpy(car->car_skin, "");
|
||||||
fprintf(stderr, "[-] Failed to create UDP socket\n");
|
strcpy(car->driver_name, "");
|
||||||
#ifdef _WIN32
|
strcpy(car->driver_team, "");
|
||||||
WSACleanup();
|
|
||||||
#endif
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
handshake hs;
|
car->carID = 0;
|
||||||
hs.identifier = 1;
|
|
||||||
hs.operationId = 1;
|
|
||||||
hs.version = 0;
|
|
||||||
|
|
||||||
printf("[+] Sending handshake message...\t");
|
car->position.x = 0.0f;
|
||||||
ssize_t bytes_sent = send_udp_message((int)sock_FD, (const char*)&hs, SERVER_OUT_IP, uint16_t(SERVER_OUT_PORT));
|
car->position.y = 0.0f;
|
||||||
if (bytes_sent >= 0) {
|
car->position.z = 0.0f;
|
||||||
printf("OK (%zd bytes)\n", bytes_sent);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr, "ERROR.\n");
|
|
||||||
CLOSESOCKET(sock_FD);
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSACleanup();
|
|
||||||
#endif
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t buffer[512]; // bigger than struct
|
car->velocity.x = 0.0f;
|
||||||
ssize_t bytes_received = recv(sock_FD, (char*)buffer, sizeof(buffer), 0);
|
car->velocity.y = 0.0f;
|
||||||
|
car->velocity.z = 0.0f;
|
||||||
|
|
||||||
if (bytes_received >= sizeof(handshackerResponse)) {
|
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; TAG:1
|
||||||
|
|
||||||
// Convert strings from big endian to little endian if necessary
|
car->total_laps_completed = 0;
|
||||||
|
car->contacts = 0;
|
||||||
|
car->total_contacts = 0; // TODO: SQL querry to get total contacts of all time; TAG:2
|
||||||
|
|
||||||
handshackerResponse resp;
|
car->normalizedSplinePos = 0.0f;
|
||||||
memcpy(&resp, buffer, sizeof(handshackerResponse));
|
|
||||||
|
|
||||||
printf("[+] Received handshake response:\n");
|
|
||||||
printf(" Car: ");
|
|
||||||
for (int i = 0; i < 50; i++) {
|
|
||||||
printf("%c", isprint(resp.carName[i]) ? resp.carName[i] : '.');
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf(" Driver: %s\n", resp.driverName);
|
|
||||||
printf(" Identifier: %d\n", resp.identifier);
|
|
||||||
printf(" Version: %d\n", resp.version);
|
|
||||||
printf(" Track: %s\n", resp.trackName);
|
|
||||||
printf(" Config: %s\n", resp.trackConfig);
|
|
||||||
} else {
|
|
||||||
printf("[!] Packet too short for handshake response (%zd bytes)\n", bytes_received);
|
|
||||||
}
|
|
||||||
|
|
||||||
CLOSESOCKET(sock_FD);
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
WSACleanup();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
printf("[+] Starting server...\n");
|
||||||
|
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||||
|
printf("[+] Server sending to %s:%d\n\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||||
|
|
||||||
|
int send_sock_fd = connect_udp_socket("127.0.0.1", 11000); // SEND requests to Server
|
||||||
|
int recv_sock_fd = create_bound_udp_socket("0.0.0.0", 12000); // LISTEN to server updates
|
||||||
|
|
||||||
|
if (send_sock_fd < 0 || recv_sock_fd < 0) {
|
||||||
|
fprintf(stderr, "[!] Failed to create sockets (%d)(%d)\n", send_sock_fd, recv_sock_fd);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long offset = 0;
|
||||||
|
int ok = 1;
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
|
trackAtributes trackInfo;
|
||||||
|
carAtributes players[MAX_PLAYERS];
|
||||||
|
|
||||||
|
carAtributes update;
|
||||||
|
|
||||||
|
// Basically a contrutor for carAtributes (because all strings in carAtributes are pointers)
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||||
|
init_carupdate(&players[i]);
|
||||||
|
}
|
||||||
|
init_carupdate(&update);
|
||||||
|
|
||||||
|
trackInfo.server_name = (char *)malloc(128);
|
||||||
|
trackInfo.track = (char *)malloc(128);
|
||||||
|
trackInfo.track_config = (char *)malloc(64);
|
||||||
|
trackInfo.weather_graphics = (char *)malloc(64);
|
||||||
|
trackInfo.session_name = (char *)malloc(64);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// ============================
|
||||||
|
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);
|
||||||
|
trackInfo.session_index = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.current_session_index = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.session_count = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
|
||||||
|
trackInfo.session_type = (SessionType)trackInfo.session_index; // FOR BACKWARD COMPATIBILITY
|
||||||
|
|
||||||
|
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, trackInfo.server_name, str_len_8, &ok);
|
||||||
|
|
||||||
|
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.track, str_len_8, &ok);
|
||||||
|
|
||||||
|
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.track_config, str_len_8, &ok);
|
||||||
|
|
||||||
|
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.session_name, str_len_8, &ok);
|
||||||
|
|
||||||
|
trackInfo.typ = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.laps = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.wait_time = read_uint16((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.ambient_temp = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
trackInfo.road_temp = 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_string((const u_int8_t *)buffer, recv_bytes, &offset, trackInfo.weather_graphics, str_len_8, &ok);
|
||||||
|
|
||||||
|
// FIX: elapsed_ms was/is incorrectly read
|
||||||
|
// TEST: Verify with actual server
|
||||||
|
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);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\tServer Name: \"%s\"\n", trackInfo.server_name);
|
||||||
|
printf("\tTrack: \"%s\"\n", trackInfo.track);
|
||||||
|
printf("\tTrack Config: \"%s\"\n", trackInfo.track_config);
|
||||||
|
printf("\tSession Name: \"%s\"\n", trackInfo.session_name);
|
||||||
|
|
||||||
|
printf("\tProtocol Version: %d\tSession Index: %d/%d\tCurrent Session Index: %d\n", trackInfo.protocol_version, trackInfo.session_index, trackInfo.session_count,
|
||||||
|
trackInfo.current_session_index);
|
||||||
|
|
||||||
|
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 (%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:
|
||||||
|
printf("[+] Message Type: ACSP_NEW_CONNECTION\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, update.driver_name, str_len_8, &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, update.driver_GUID, str_len_8, &ok);
|
||||||
|
|
||||||
|
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_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_model, str_len_8, &ok);
|
||||||
|
|
||||||
|
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_skin, str_len_8, &ok);
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "[-] Error parsing NEW_CONNECTION packet (offset=%zu)\n", offset);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
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\n", update.car_skin);
|
||||||
|
|
||||||
|
// Store player player into the respective Index
|
||||||
|
if (update.carID < MAX_PLAYERS) {
|
||||||
|
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_CONNECTION_CLOSED: // DONE:
|
||||||
|
printf("[+] Message Type: ACSP_CONNECTION_CLOSED\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, update.driver_name, str_len_8, &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, update.driver_GUID, str_len_8, &ok);
|
||||||
|
|
||||||
|
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_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_model, str_len_8, &ok);
|
||||||
|
|
||||||
|
str_len_8 = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
read_string((const u_int8_t *)buffer, recv_bytes, &offset, update.car_skin, str_len_8, &ok);
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "[-] Error parsing NEW_CONNECTION packet (offset=%zu)\n", offset);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
update.isConnected = 0; // Mark as disconnected
|
||||||
|
|
||||||
|
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\n", update.car_skin);
|
||||||
|
|
||||||
|
// Store player player into the respective Index
|
||||||
|
if (update.carID < MAX_PLAYERS) {
|
||||||
|
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_CAR_UPDATE: // DONE:
|
||||||
|
#if DEBUG_CAR_INFO
|
||||||
|
printf("[+] Message Type: ACSP_CAR_UPDATE\n");
|
||||||
|
#endif
|
||||||
|
offset = 1;
|
||||||
|
|
||||||
|
memcpy(&update.carID, buffer, sizeof(uint8_t));
|
||||||
|
offset = 1 + sizeof(uint8_t);
|
||||||
|
memcpy(&update.position, buffer + offset, sizeof(postion));
|
||||||
|
offset += sizeof(postion);
|
||||||
|
memcpy(&update.velocity, buffer + offset, sizeof(postion));
|
||||||
|
offset += sizeof(postion);
|
||||||
|
memcpy(&update.carGear, buffer + offset, sizeof(u_int8_t));
|
||||||
|
offset += sizeof(u_int8_t);
|
||||||
|
memcpy(&update.carRPM, buffer + offset, sizeof(uint16_t));
|
||||||
|
offset += sizeof(uint16_t);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_CAR_INFO: // DONE:
|
||||||
|
printf("[+] Message Type: ACSP_CAR_INFO\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);
|
||||||
|
update.isConnected = 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, update.car_model, str_len_8, &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, update.car_skin, str_len_8, &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, update.driver_name, str_len_8, &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, update.driver_team, str_len_8, &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, update.driver_GUID, str_len_8, &ok);
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "[-] Error parsing CAR_INFO packet (offset=%zu)\n", offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\tCar ID: %d\n", update.carID);
|
||||||
|
printf("\tIs Connected: %d\n", update.isConnected);
|
||||||
|
printf("\tCar Model: \"%s\"\n", update.car_model);
|
||||||
|
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\n", update.driver_GUID);
|
||||||
|
|
||||||
|
// Store player player into the respective Index
|
||||||
|
if (update.carID < MAX_PLAYERS) {
|
||||||
|
memcpy(&players[update.carID], &update, sizeof(carAtributes));
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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: // 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: // 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: // 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_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\n", message);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_LAP_COMPLETED: // DONE: 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;
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// EVENTS
|
||||||
|
// ============================
|
||||||
|
case ACSP_CLIENT_EVENT: {
|
||||||
|
printf("[+] Message Type: ACSP_CLIENT_EVENT\n");
|
||||||
|
offset = 1; // Reset offset to 1 to read after message types
|
||||||
|
|
||||||
|
u_int8_t event_type = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
|
||||||
|
update.carID = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
u_int8_t event_car_id = 255; // If event doesnt involve another car, set to 255
|
||||||
|
|
||||||
|
switch (event_type) {
|
||||||
|
// ============================
|
||||||
|
// EVENT TYPES
|
||||||
|
// ============================
|
||||||
|
case ACSP_CE_COLLISION_WITH_CAR: {
|
||||||
|
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_CAR\n");
|
||||||
|
event_car_id = read_uint8((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
|
||||||
|
printf("Car ID: %d (%s) collided with Car ID: %d (%s)\n", update.carID, players[update.carID].driver_name, event_car_id, players[event_car_id].driver_name);
|
||||||
|
|
||||||
|
players[update.carID].contacts++;
|
||||||
|
players[event_car_id].contacts++;
|
||||||
|
|
||||||
|
// TODO: Update total contacts for both players in the database
|
||||||
|
// TAG:2 Update total contacts for both players
|
||||||
|
|
||||||
|
} break;
|
||||||
|
|
||||||
|
case ACSP_CE_COLLISION_WITH_ENV: {
|
||||||
|
printf("[+] Event Type: ACSP_CE_COLLISION_WITH_ENV\n");
|
||||||
|
|
||||||
|
printf("Car ID: %d (%s) collided with the environment\n", update.carID, players[update.carID].driver_name);
|
||||||
|
|
||||||
|
players[update.carID].contacts++;
|
||||||
|
|
||||||
|
// TODO: Update total contacts for the player in the database
|
||||||
|
// TAG:2 Update total contacts for the player
|
||||||
|
} break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: possible iRacing style impact severity system
|
||||||
|
// With X velues and (e.g >5 m/s = 0x, >10 m/s = 2x, >15 m/s = 4x) && Blackflag limits (e.g x17 = DQ)
|
||||||
|
|
||||||
|
float impact_speed = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
postion world_pos = {0};
|
||||||
|
world_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
world_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
world_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
|
||||||
|
postion rel_pos = {0};
|
||||||
|
rel_pos.x = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
rel_pos.y = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
rel_pos.z = read_float((const u_int8_t *)buffer, recv_bytes, &offset, &ok);
|
||||||
|
|
||||||
|
printf("\tImpact Speed: %.2f m/s\n", impact_speed);
|
||||||
|
printf("\tWorld Position: X: %.2f Y: %.2f Z: %.2f\n", world_pos.x, world_pos.y, world_pos.z);
|
||||||
|
printf("\tRelative Position: X: %.2f Y: %.2f Z: %.2f\n\n", rel_pos.x, rel_pos.y, rel_pos.z);
|
||||||
|
|
||||||
|
} 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
|
||||||
|
// NOTE: These are not meant to be here, these are comands to SEND to the server
|
||||||
|
// ============================
|
||||||
|
case ACSP_REALTIMEPOS_INTERVAL:
|
||||||
|
printf("[+] Command: ACSP_REALTIMEPOS_INTERVAL\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_GET_CAR_INFO:
|
||||||
|
printf("[+] Command: ACSP_GET_CAR_INFO\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_SEND_CHAT:
|
||||||
|
printf("[+] Command: ACSP_SEND_CHAT\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_BROADCAST_CHAT:
|
||||||
|
printf("[+] Command: ACSP_BROADCAST_CHAT\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_GET_SESSION_INFO:
|
||||||
|
printf("[+] Command: ACSP_GET_SESSION_INFO\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_SET_SESSION_INFO:
|
||||||
|
printf("[+] Command: ACSP_SET_SESSION_INFO\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_KICK_USER:
|
||||||
|
printf("[+] Command: ACSP_KICK_USER\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_NEXT_SESSION:
|
||||||
|
printf("[+] Command: ACSP_NEXT_SESSION\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_RESTART_SESSION:
|
||||||
|
printf("[+] Command: ACSP_RESTART_SESSION\n");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ACSP_ADMIN_COMMAND: { // DONE:
|
||||||
|
printf("[+] Command: ACSP_ADMIN_COMMAND\n");
|
||||||
|
char *buffer = (char *)malloc(256);
|
||||||
|
char command[256] = "/settime 19:00"; // TEST:
|
||||||
|
|
||||||
|
buffer[0] = (char)ACSP_ADMIN_COMMAND;
|
||||||
|
for (int i = 1; i <= strlen(command); i++) {
|
||||||
|
buffer[i] = command[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
sendto(send_sock_fd, buffer, strlen(command) + 1, 0, NULL, 0);
|
||||||
|
printf("\tSent Admin Command: %s\n\n", command);
|
||||||
|
free(buffer);
|
||||||
|
} break;
|
||||||
|
|
||||||
|
// ============================
|
||||||
|
// DEFAULT HANDLER
|
||||||
|
// ============================
|
||||||
|
default:
|
||||||
|
printf("[!] Unknown Message Type: %d\n\n", buffer[0]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
usleep(10000); // Sleep for 10ms
|
||||||
|
}
|
||||||
|
close(send_sock_fd);
|
||||||
|
close(recv_sock_fd);
|
||||||
|
|
||||||
|
// Free allocated memory
|
||||||
|
free(update.car_model);
|
||||||
|
free(update.driver_GUID);
|
||||||
|
free(update.car_skin);
|
||||||
|
free(update.driver_name);
|
||||||
|
free(update.driver_team);
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_PLAYERS; i++) {
|
||||||
|
free(players[i].car_model);
|
||||||
|
free(players[i].driver_GUID);
|
||||||
|
free(players[i].car_skin);
|
||||||
|
free(players[i].driver_name);
|
||||||
|
free(players[i].driver_team);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(trackInfo.server_name);
|
||||||
|
free(trackInfo.track);
|
||||||
|
free(trackInfo.track_config);
|
||||||
|
free(trackInfo.weather_graphics);
|
||||||
|
free(trackInfo.session_name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user