generated from AfonsoCMSousa/CPP-Template
Feature: First handshakes and Information Tradding
This commit is contained in:
+53
-18
@@ -1,8 +1,11 @@
|
||||
#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"
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user