generated from AfonsoCMSousa/CPP-Template
Feature: First handshakes and Information Tradding
This commit is contained in:
parent
6485986a5c
commit
881001d916
7
.clang-format
Normal file
7
.clang-format
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
BasedOnStyle: LLVM
|
||||||
|
IndentWidth: 4
|
||||||
|
UseTab: ForIndentation
|
||||||
|
TabWidth: 4
|
||||||
|
ColumnLimit: 180
|
||||||
|
BreakBeforeBraces: Attach
|
||||||
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
@ -1,5 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.20)
|
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 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
@ -15,58 +15,57 @@ include_directories(${CMAKE_SOURCE_DIR}/libraries)
|
|||||||
|
|
||||||
# Gather all source files (.cpp, .c)
|
# Gather all source files (.cpp, .c)
|
||||||
file(GLOB_RECURSE PROJECT_SOURCES
|
file(GLOB_RECURSE PROJECT_SOURCES
|
||||||
${CMAKE_SOURCE_DIR}/include/*.cpp
|
${CMAKE_SOURCE_DIR}/include/*.cpp
|
||||||
${CMAKE_SOURCE_DIR}/include/*.c
|
${CMAKE_SOURCE_DIR}/include/*.c
|
||||||
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||||
${CMAKE_SOURCE_DIR}/libraries/*.c
|
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||||
${CMAKE_SOURCE_DIR}/source/*.cpp
|
${CMAKE_SOURCE_DIR}/source/*.cpp
|
||||||
${CMAKE_SOURCE_DIR}/source/*.c
|
${CMAKE_SOURCE_DIR}/source/*.c
|
||||||
)
|
)
|
||||||
|
|
||||||
# Gather all header files (.hpp, .h)
|
# Gather all header files (.hpp, .h)
|
||||||
file(GLOB_RECURSE PROJECT_HEADERS
|
file(GLOB_RECURSE PROJECT_HEADERS
|
||||||
${CMAKE_SOURCE_DIR}/include/*.hpp
|
${CMAKE_SOURCE_DIR}/include/*.hpp
|
||||||
${CMAKE_SOURCE_DIR}/include/*.h
|
${CMAKE_SOURCE_DIR}/include/*.h
|
||||||
${CMAKE_SOURCE_DIR}/libraries/*.hpp
|
${CMAKE_SOURCE_DIR}/libraries/*.hpp
|
||||||
${CMAKE_SOURCE_DIR}/libraries/*.h
|
${CMAKE_SOURCE_DIR}/libraries/*.h
|
||||||
${CMAKE_SOURCE_DIR}/source/*.hpp
|
${CMAKE_SOURCE_DIR}/source/*.hpp
|
||||||
${CMAKE_SOURCE_DIR}/source/*.h
|
${CMAKE_SOURCE_DIR}/source/*.h
|
||||||
)
|
)
|
||||||
|
|
||||||
# Allow user to set output program name
|
# Allow user to set output program name
|
||||||
option(OUTPUT_NAME "Name of the output executable" "")
|
option(OUTPUT_NAME "Name of the output executable" "")
|
||||||
if(OUTPUT_NAME STREQUAL "")
|
if(OUTPUT_NAME STREQUAL "")
|
||||||
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||||
else()
|
else()
|
||||||
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Add executable with all sources
|
# Add executable with all sources
|
||||||
add_executable(${EXECUTABLE_NAME}
|
add_executable(${EXECUTABLE_NAME}
|
||||||
${PROJECT_SOURCES}
|
${PROJECT_SOURCES}
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set output name property (for consistency)
|
|
||||||
set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME})
|
set_target_properties(${EXECUTABLE_NAME} PROPERTIES OUTPUT_NAME ${EXECUTABLE_NAME})
|
||||||
|
|
||||||
# Enable warnings and extra diagnostics
|
# Enable warnings and extra diagnostics
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze)
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE /W4 /permissive- /analyze)
|
||||||
else()
|
else()
|
||||||
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
-Wall
|
-Wall
|
||||||
-Wextra
|
-Wextra
|
||||||
-Wpedantic
|
-Wpedantic
|
||||||
-Wshadow
|
-Wshadow
|
||||||
-Wconversion
|
-Wconversion
|
||||||
-Wsign-conversion
|
-Wsign-conversion
|
||||||
-Wuninitialized
|
-Wuninitialized
|
||||||
-Wunused
|
-Wunused
|
||||||
-Werror=return-type
|
-Werror=return-type
|
||||||
-fsanitize=address,undefined
|
-fsanitize=address,undefined
|
||||||
-g
|
-g
|
||||||
)
|
)
|
||||||
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
|
target_link_options(${EXECUTABLE_NAME} PRIVATE -fsanitize=address,undefined)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Optionally, enable testing
|
# Optionally, enable testing
|
||||||
|
|||||||
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
@ -1,15 +1,16 @@
|
|||||||
#ifndef SERVER_STRUCTS_H
|
#ifndef SERVER_STRUCTS_H
|
||||||
#define SERVER_STRUCTS_H
|
#define SERVER_STRUCTS_H
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
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))
|
||||||
int 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.
|
||||||
int 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:
|
||||||
@ -25,8 +26,28 @@ struct handshake {
|
|||||||
//
|
//
|
||||||
// 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.
|
||||||
int operationId;
|
int32_t operationId;
|
||||||
} __attribute__((packed));
|
} __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
|
#endif // SERVER_STRUCTS_H
|
||||||
|
|||||||
@ -1,6 +1,12 @@
|
|||||||
#include "socket.h"
|
#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);
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sockfd < 0) {
|
if (sockfd < 0) {
|
||||||
perror("socket creation failed");
|
perror("socket creation failed");
|
||||||
@ -23,7 +29,7 @@ int connect_udp_socket(const char* ip, int port) {
|
|||||||
return sockfd;
|
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;
|
struct sockaddr_in servaddr;
|
||||||
memset(&servaddr, 0, sizeof(servaddr));
|
memset(&servaddr, 0, sizeof(servaddr));
|
||||||
|
|
||||||
@ -40,7 +46,7 @@ int bind_udp_socket(int sockfd, const char* ip, int port) {
|
|||||||
return 0;
|
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;
|
struct sockaddr_in destaddr;
|
||||||
memset(&destaddr, 0, sizeof(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_port = htons(dest_port);
|
||||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
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) {
|
if (n < 0) {
|
||||||
perror("sendto failed");
|
perror("sendto failed");
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@ -1,11 +1,22 @@
|
|||||||
#ifndef SOCKET_H
|
#ifndef SOCKET_H
|
||||||
#define SOCKET_H
|
#define SOCKET_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
||||||
|
#else
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <cstring>
|
#include <string.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
// UDP SOCKET FUCNTIONS
|
// UDP SOCKET FUCNTIONS
|
||||||
@ -13,11 +24,15 @@
|
|||||||
// 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
|
||||||
|
|
||||||
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
|
#endif // SOCKET_H
|
||||||
|
|
||||||
|
|||||||
@ -1,8 +1,11 @@
|
|||||||
|
#include <cstdint>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "server_structs.h"
|
#include "server_structs.h"
|
||||||
@ -10,27 +13,59 @@
|
|||||||
|
|
||||||
const int SERVER_OUT_PORT = 12000;
|
const int SERVER_OUT_PORT = 12000;
|
||||||
const int SERVER_IN_PORT = 11000;
|
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) {
|
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:
|
printf("[+] Starting server...\n");
|
||||||
char buffer[1024];
|
// Printf information about the server
|
||||||
n = recvfrom(0, buffer, sizeof(buffer)-1, 0, NULL, NULL);
|
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||||
if (n < 0) {
|
printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||||
perror("recvfrom failed");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[n] = '\0'; // Null-terminate the received message
|
// Create UDP socket
|
||||||
printf("Received message: %s\n", buffer);
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user