72 lines
1.8 KiB
C++

#include <cstdint>
#include <ctype.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "server_structs.h"
#include "socket.h"
const int SERVER_OUT_PORT = 12000;
const int SERVER_IN_PORT = 11000;
const char *SERVER_OUT_IP = "127.0.0.1";
int main(void) {
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);
// 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;
}
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;
}