diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b40ae0..e50de42 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,36 +15,35 @@ 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_HEADERS} +${PROJECT_SOURCES} ) # Set output name property (for consistency) @@ -52,24 +51,24 @@ set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAM # 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 # enable_testing() -# add_subdirectory(tests) \ No newline at end of file +# add_subdirectory(tests) diff --git a/PlayerTracker b/PlayerTracker new file mode 100755 index 0000000..558aff2 Binary files /dev/null and b/PlayerTracker differ diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/include/server_structs.h b/include/server_structs.h new file mode 100644 index 0000000..d3b5178 --- /dev/null +++ b/include/server_structs.h @@ -0,0 +1,32 @@ +#ifndef SERVER_STRUCTS_H +#define SERVER_STRUCTS_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. + // This will be used to adjust a specific behaviour for each platform. (eIPadDevice for now (1)) + int 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; + + // This is the type of operation required by the client. + // The following operations are now available: + // ---------------------------------------------------------------- + // HANDSHAKE = 0 : + // This operation identifier must be set when the client wants to start the comunication. + // + // SUBSCRIBE_UPDATE = 1 : + // This operation identifier must be set when the client wants to be updated from the specific ACServer. + // + // 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). + // + // DISMISS = 3 : + // This operation identifier must be set when the client wants to leave the comunication with ACServer. + int operationId; +} __attribute__((packed)); + + +#endif // SERVER_STRUCTS_H diff --git a/include/socket.c b/include/socket.c new file mode 100644 index 0000000..25ba240 --- /dev/null +++ b/include/socket.c @@ -0,0 +1,58 @@ +#include "socket.h" + +int connect_udp_socket(const char* ip, int 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 bind_udp_socket(int sockfd, const char* ip, int port) { + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = inet_addr(ip); + servaddr.sin_port = htons(port); + + if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { + perror("bind failed"); + close(sockfd); + return -1; + } + + return 0; +} + +int send_udp_message(int sockfd, const char *message, const char *dest_ip, int dest_port) { + 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); + + int n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr)); + if (n < 0) { + perror("sendto failed"); + return -1; + } + + return n; +} diff --git a/include/socket.h b/include/socket.h new file mode 100644 index 0000000..0d169ff --- /dev/null +++ b/include/socket.h @@ -0,0 +1,23 @@ +#ifndef SOCKET_H +#define SOCKET_H + +#include +#include +#include +#include +#include + +// ========================= +// UDP SOCKET FUCNTIONS +// ========================= +// 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 bind_udp_socket(int sockfd, const char* ip, int port); + +int send_udp_message(int sockfd, const char* message, const char* dest_ip, int dest_port); + +#endif // SOCKET_H + diff --git a/source/main.cpp b/source/main.cpp index df28b27..62aecc7 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,7 +1,36 @@ +#include #include +#include +#include +#include +#include + +#include "server_structs.h" +#include "socket.h" + +const int SERVER_OUT_PORT = 12000; +const int SERVER_IN_PORT = 11000; +const char *SERVER_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; + } + + buffer[n] = '\0'; // Null-terminate the received message + printf("Received message: %s\n", buffer); -int main() -{ - std::cout << "Hello, World!" << std::endl; return 0; -} \ No newline at end of file +}