diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..22eb0c8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,7 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +UseTab: ForIndentation +TabWidth: 4 +ColumnLimit: 180 +BreakBeforeBraces: Attach +AllowShortFunctionsOnASingleLine: Empty diff --git a/CMakeLists.txt b/CMakeLists.txt index e50de42..bfb8296 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.20) -project(CPP_TEMPLATE VERSION 0.1.0 LANGUAGES CXX) +project(CPP_TEMPLATE VERSION 0.1.0 LANGUAGES C CXX) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) @@ -15,58 +15,57 @@ include_directories(${CMAKE_SOURCE_DIR}/libraries) # Gather all source files (.cpp, .c) file(GLOB_RECURSE PROJECT_SOURCES -${CMAKE_SOURCE_DIR}/include/*.cpp -${CMAKE_SOURCE_DIR}/include/*.c -${CMAKE_SOURCE_DIR}/libraries/*.cpp -${CMAKE_SOURCE_DIR}/libraries/*.c -${CMAKE_SOURCE_DIR}/source/*.cpp -${CMAKE_SOURCE_DIR}/source/*.c + ${CMAKE_SOURCE_DIR}/include/*.cpp + ${CMAKE_SOURCE_DIR}/include/*.c + ${CMAKE_SOURCE_DIR}/libraries/*.cpp + ${CMAKE_SOURCE_DIR}/libraries/*.c + ${CMAKE_SOURCE_DIR}/source/*.cpp + ${CMAKE_SOURCE_DIR}/source/*.c ) # Gather all header files (.hpp, .h) file(GLOB_RECURSE PROJECT_HEADERS -${CMAKE_SOURCE_DIR}/include/*.hpp -${CMAKE_SOURCE_DIR}/include/*.h -${CMAKE_SOURCE_DIR}/libraries/*.hpp -${CMAKE_SOURCE_DIR}/libraries/*.h -${CMAKE_SOURCE_DIR}/source/*.hpp -${CMAKE_SOURCE_DIR}/source/*.h + ${CMAKE_SOURCE_DIR}/include/*.hpp + ${CMAKE_SOURCE_DIR}/include/*.h + ${CMAKE_SOURCE_DIR}/libraries/*.hpp + ${CMAKE_SOURCE_DIR}/libraries/*.h + ${CMAKE_SOURCE_DIR}/source/*.hpp + ${CMAKE_SOURCE_DIR}/source/*.h ) # Allow user to set output program name option(OUTPUT_NAME "Name of the output executable" "") if(OUTPUT_NAME STREQUAL "") -set(EXECUTABLE_NAME ${PROJECT_NAME}) + set(EXECUTABLE_NAME ${PROJECT_NAME}) else() -set(EXECUTABLE_NAME ${OUTPUT_NAME}) + set(EXECUTABLE_NAME ${OUTPUT_NAME}) endif() # Add executable with all sources add_executable(${EXECUTABLE_NAME} -${PROJECT_SOURCES} + ${PROJECT_SOURCES} ) -# Set output name property (for consistency) set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME}) # Enable warnings and extra diagnostics if (MSVC) -target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze) + target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze) else() -target_compile_options(${EXECUTABLE_NAME} PRIVATE - -Wall - -Wextra - -Wpedantic - -Wshadow - -Wconversion - -Wsign-conversion - -Wuninitialized - -Wunused - -Werror=return-type - -fsanitize=address,undefined - -g -) -target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined) + target_compile_options(${EXECUTABLE_NAME} PRIVATE + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wconversion + -Wsign-conversion + -Wuninitialized + -Wunused + -Werror=return-type + -fsanitize=address,undefined + -g + ) + target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined) endif() # Optionally, enable testing diff --git a/PlayerTracker b/PlayerTracker index 558aff2..43c2e04 100755 Binary files a/PlayerTracker and b/PlayerTracker differ diff --git a/include/server_structs.h b/include/server_structs.h index d3b5178..101780a 100644 --- a/include/server_structs.h +++ b/include/server_structs.h @@ -1,15 +1,16 @@ #ifndef SERVER_STRUCTS_H #define SERVER_STRUCTS_H +#include struct handshake { // [not used in the current Remote Telemtry version by AC] // 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)) - int identifier; + int32_t identifier; // [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. - int version; + int32_t version; // This is the type of operation required by the client. // The following operations are now available: @@ -25,8 +26,28 @@ struct handshake { // // DISMISS = 3 : // This operation identifier must be set when the client wants to leave the comunication with ACServer. - int operationId; + int32_t operationId; } __attribute__((packed)); +struct handshackerResponse{ + // is the name of the car that the player is driving on the AC Server + char carName[50]; + + // is the name of the driver running on the AC Server + char driverName[50]; + + // for now is just 4242, this code will identify different status, + // as “NOT AVAILABLE” for connection + int32_t identifier; + + // for now is set to 1, this will identify the version running on the AC Server + int32_t version; + + // is the name of the track on the AC Server + char trackName[50]; + + // is the track configuration on the AC Server + char trackConfig[50]; +} __attribute__((packed)); #endif // SERVER_STRUCTS_H diff --git a/include/socket.c b/include/socket.c index 25ba240..ff14f1a 100644 --- a/include/socket.c +++ b/include/socket.c @@ -1,6 +1,12 @@ #include "socket.h" -int connect_udp_socket(const char* ip, int port) { +// ========================= +// UDP SOCKET FUCNTIONS +// ========================= +// @param ip: IP address to connect to +// @param port: Port number to connect to +// @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"); @@ -23,7 +29,7 @@ int connect_udp_socket(const char* ip, int port) { return sockfd; } -int bind_udp_socket(int sockfd, const char* ip, int port) { +int bind_udp_socket(int sockfd, const char* ip, uint16_t port) { struct sockaddr_in servaddr; memset(&servaddr, 0, sizeof(servaddr)); @@ -40,7 +46,7 @@ int bind_udp_socket(int sockfd, const char* ip, int port) { return 0; } -int send_udp_message(int sockfd, const char *message, const char *dest_ip, int dest_port) { +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)); @@ -48,7 +54,7 @@ int send_udp_message(int sockfd, const char *message, const char *dest_ip, int d destaddr.sin_port = htons(dest_port); destaddr.sin_addr.s_addr = inet_addr(dest_ip); - int n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr)); + ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr)); if (n < 0) { perror("sendto failed"); return -1; diff --git a/include/socket.h b/include/socket.h index 0d169ff..339feca 100644 --- a/include/socket.h +++ b/include/socket.h @@ -1,11 +1,22 @@ #ifndef SOCKET_H #define SOCKET_H +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef _WIN32 +#include +#include +#pragma comment(lib, "ws2_32.lib") // link Winsock +#else #include #include #include +#include #include -#include +#include +#endif // ========================= // UDP SOCKET FUCNTIONS @@ -13,11 +24,15 @@ // Server hosts a UDP socket at 127.0.0.1:12000 // Client sends a message to the server at 11000 -int connect_udp_socket(const char* ip, int port); +int connect_udp_socket(const char *ip, uint16_t port); -int bind_udp_socket(int sockfd, const char* ip, int port); +int bind_udp_socket(int sockfd, const char *ip, uint16_t port); -int send_udp_message(int sockfd, const char* message, const char* dest_ip, int dest_port); +ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, + uint16_t dest_port); + +#ifdef __cplusplus +} +#endif #endif // SOCKET_H - diff --git a/source/main.cpp b/source/main.cpp index 62aecc7..52abda1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,8 +1,11 @@ +#include #include #include #include #include #include +#include +#include #include #include "server_structs.h" @@ -10,27 +13,59 @@ const int SERVER_OUT_PORT = 12000; const int SERVER_IN_PORT = 11000; -const char *SERVER_IP = "127.0.0.1"; +const char *SERVER_OUT_IP = "127.0.0.1"; int main(void) { - - printf("[+] Starting server...\n"); - int n = connect_udp_socket(SERVER_IP, SERVER_OUT_PORT); - if (n < 0) { - fprintf(stderr, "[-] Failed to connect to UDP socket at %s:%d\n", SERVER_IP, SERVER_OUT_PORT); - return -1; - } - // TEST to see what server is sending: - char buffer[1024]; - n = recvfrom(0, buffer, sizeof(buffer)-1, 0, NULL, NULL); - if (n < 0) { - perror("recvfrom failed"); - return -1; - } + printf("[+] Starting server...\n"); + // Printf information about the server + printf("[+] Server listening on port %d\n", SERVER_IN_PORT); + printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT); - buffer[n] = '\0'; // Null-terminate the received message - printf("Received message: %s\n", buffer); + // Create UDP socket + int sock_FD = connect_udp_socket(SERVER_OUT_IP, SERVER_OUT_PORT); + if (sock_FD < 0) { + fprintf(stderr, "[-] Failed to create UDP socket\n"); + return -1; + } - return 0; + handshake hs; + hs.identifier = 1; + hs.operationId = 1; + hs.version = 0; + // Send handshake message + + printf("[+] Sending handshake message...\t"); + ssize_t bytes_sent = send_udp_message((int)sock_FD, (const char *)&hs, SERVER_OUT_IP, uint16_t(SERVER_OUT_PORT)); + if (bytes_sent >= 0) { + printf("OK (%zd bytes)\n", bytes_sent); + } else { + fprintf(stderr, "ERROR. \n"); + close((int)sock_FD); + return -2; + } + + uint8_t buffer[512]; // bigger than struct + ssize_t bytes_received = recv(sock_FD, buffer, sizeof(buffer), 0); + + if (bytes_received >= sizeof(handshackerResponse)) { + handshackerResponse resp; + memcpy(&resp, buffer, sizeof(handshackerResponse)); + + printf("[+] Received handshake response:\n"); + printf(" Car: %0x\n", resp.carName); + printf(" Driver: %0x\n", resp.driverName); + printf(" Identifier: %d\n", resp.identifier); + printf(" Version: %0x\n", resp.version); + printf(" Track: %0x\n", resp.trackName); + printf(" Config: %0x\n", resp.trackConfig); + } else { + printf("[!] Packet too short for handshake response (%zd bytes)\n", bytes_received); + } + + + + + + return 0; }