generated from AfonsoCMSousa/CPP-Template
Feature: hable to parse NEW_SESSION
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
#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);
|
||||
}
|
||||
|
||||
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,56 @@
|
||||
#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 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
|
||||
@@ -1,7 +1,10 @@
|
||||
#ifndef SERVER_STRUCTS_H
|
||||
#define SERVER_STRUCTS_H
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <sys/cdefs.h>
|
||||
#include <sys/types.h>
|
||||
struct handshake {
|
||||
// [not used in the current Remote Telemtry version by AC]
|
||||
// In future versions it will identify the platform type of the client.
|
||||
@@ -50,4 +53,102 @@ struct handshackerResponse{
|
||||
char trackConfig[50];
|
||||
} __attribute__((packed));
|
||||
|
||||
struct postion {
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct carAtributes {
|
||||
// Related to ACSP_CAR_INFO
|
||||
u_char isConnected; // 1 = connected, 0 = disconnected
|
||||
char *carModel;
|
||||
char *carSkin;
|
||||
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;
|
||||
float_t normalizedSplinePos;
|
||||
|
||||
} __attribute__((packed));
|
||||
|
||||
|
||||
struct trackAtributes {
|
||||
u_int8_t protocol_version;
|
||||
u_int8_t session_index;
|
||||
u_int8_t current_session_index;
|
||||
u_int8_t session_count;
|
||||
|
||||
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;
|
||||
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
|
||||
|
||||
+73
-47
@@ -3,62 +3,88 @@
|
||||
// =========================
|
||||
// UDP SOCKET FUCNTIONS
|
||||
// =========================
|
||||
// @param ip: IP address to connect to
|
||||
// @param port: Port number to connect to
|
||||
|
||||
// Create a UDP socket
|
||||
// @return: Socket file descriptor, or -1 on error
|
||||
int connect_udp_socket(const char* ip, uint16_t port) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("socket creation failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
|
||||
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");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return sockfd;
|
||||
int create_udp_socket(void) {
|
||||
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0) {
|
||||
perror("[!] socket() failed");
|
||||
return -1;
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
int bind_udp_socket(int sockfd, const char* ip, uint16_t port) {
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
// Create a UDP socket and connect it to a specific IP and port
|
||||
// Used for sending data (no bind needed)
|
||||
// @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;
|
||||
servaddr.sin_addr.s_addr = inet_addr(ip);
|
||||
servaddr.sin_port = htons(port);
|
||||
struct sockaddr_in servaddr;
|
||||
memset(&servaddr, 0, sizeof(servaddr));
|
||||
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) {
|
||||
perror("bind failed");
|
||||
close(sockfd);
|
||||
return -1;
|
||||
}
|
||||
if (connect(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
|
||||
perror("[!] connect() failed");
|
||||
close(sockfd);
|
||||
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) {
|
||||
struct sockaddr_in destaddr;
|
||||
memset(&destaddr, 0, sizeof(destaddr));
|
||||
struct sockaddr_in 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;
|
||||
destaddr.sin_port = htons(dest_port);
|
||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr *)&destaddr, sizeof(destaddr));
|
||||
if (n < 0) {
|
||||
perror("[!] sendto() failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
||||
if (n < 0) {
|
||||
perror("sendto failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return n;
|
||||
printf("[+] Sent %zd bytes to %s:%d\n", n, dest_ip, dest_port);
|
||||
return n;
|
||||
}
|
||||
|
||||
+12
-8
@@ -10,12 +10,15 @@ extern "C" {
|
||||
#include <ws2tcpip.h>
|
||||
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.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>
|
||||
#endif
|
||||
|
||||
// =========================
|
||||
@@ -24,12 +27,13 @@ extern "C" {
|
||||
// Server hosts a UDP socket at 127.0.0.1:12000
|
||||
// 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 create_bound_udp_socket(const char *ip, uint16_t port);
|
||||
|
||||
int bind_udp_socket(int sockfd, const char *ip, uint16_t port);
|
||||
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip,
|
||||
uint16_t dest_port);
|
||||
// UDP messaging
|
||||
ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, uint16_t dest_port);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user