diff --git a/PlayerTracker b/PlayerTracker index 43c2e04..b3ae3f1 100755 Binary files a/PlayerTracker and b/PlayerTracker differ diff --git a/PlayerTracker.exe b/PlayerTracker.exe new file mode 100755 index 0000000..6af1290 Binary files /dev/null and b/PlayerTracker.exe differ diff --git a/include/socket.c b/include/socket.c index ff14f1a..9ce9fc4 100644 --- a/include/socket.c +++ b/include/socket.c @@ -1,12 +1,20 @@ #include "socket.h" +#include +#include -// ========================= -// 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) { +#ifdef _WIN32 + 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); if (sockfd < 0) { perror("socket creation failed"); @@ -22,7 +30,7 @@ int connect_udp_socket(const char* ip, uint16_t port) { if (connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { perror("connection to the server failed"); - close(sockfd); + CLOSESOCKET(sockfd); return -1; } @@ -39,7 +47,7 @@ int bind_udp_socket(int sockfd, const char* ip, uint16_t port) { if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { perror("bind failed"); - close(sockfd); + CLOSESOCKET(sockfd); return -1; } @@ -54,7 +62,12 @@ ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, u destaddr.sin_port = htons(dest_port); destaddr.sin_addr.s_addr = inet_addr(dest_ip); +#ifdef _WIN32 + int n = sendto(sockfd, message, (int)strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr)); +#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; @@ -62,3 +75,10 @@ ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, u return n; } + +#ifdef _WIN32 +void cleanup_sockets() { + WSACleanup(); +} +#endif + diff --git a/include/socket.h b/include/socket.h index 339feca..c2542b0 100644 --- a/include/socket.h +++ b/include/socket.h @@ -5,17 +5,24 @@ extern "C" { #endif +#include +#include +#include +#include +#include + #ifdef _WIN32 #include #include -#pragma comment(lib, "ws2_32.lib") // link Winsock +#pragma comment(lib, "ws2_32.lib") +#define CLOSESOCKET closesocket #else #include #include #include #include #include -#include +#define CLOSESOCKET close #endif // ========================= diff --git a/source/main.cpp b/source/main.cpp index 52abda1..f585033 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -4,68 +4,105 @@ #include #include #include + +#ifdef _WIN32 +#include +#include +#pragma comment(lib, "ws2_32.lib") // only needed for MSVC +#else #include #include #include +#endif #include "server_structs.h" #include "socket.h" -const int SERVER_OUT_PORT = 12000; +// Cross-platform close macro +#ifdef _WIN32 +#define CLOSESOCKET closesocket +#else +#define CLOSESOCKET close +#endif + +const int SERVER_OUT_PORT = 9996; 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(void) { +int main() { - 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); +#ifdef _WIN32 + WSADATA wsaData; + if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { + fprintf(stderr, "[-] WSAStartup failed\n"); + return -1; + } +#endif - // 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; - } + printf("[+] Starting server...\n"); + printf("[+] Server listening on port %d\n", SERVER_IN_PORT); + printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT); - handshake hs; - hs.identifier = 1; - hs.operationId = 1; - hs.version = 0; - // Send handshake message + // 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"); +#ifdef _WIN32 + WSACleanup(); +#endif + return -1; + } - 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; - } + handshake hs; + hs.identifier = 1; + hs.operationId = 1; + hs.version = 0; - uint8_t buffer[512]; // bigger than struct - ssize_t bytes_received = recv(sock_FD, buffer, sizeof(buffer), 0); + 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"); + CLOSESOCKET(sock_FD); +#ifdef _WIN32 + WSACleanup(); +#endif + return -2; + } - if (bytes_received >= sizeof(handshackerResponse)) { - handshackerResponse resp; - memcpy(&resp, buffer, sizeof(handshackerResponse)); + uint8_t buffer[512]; // bigger than struct + ssize_t bytes_received = recv(sock_FD, (char*)buffer, sizeof(buffer), 0); - 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); - } + if (bytes_received >= sizeof(handshackerResponse)) { + // Convert strings from big endian to little endian if necessary + handshackerResponse resp; + 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); + } - return 0; + CLOSESOCKET(sock_FD); + +#ifdef _WIN32 + WSACleanup(); +#endif + + return 0; } +