generated from AfonsoCMSousa/CPP-Template
Feature: Finished testing connection. Works with Asstto Client (game).
This commit is contained in:
parent
881001d916
commit
20f2c3db79
BIN
PlayerTracker
BIN
PlayerTracker
Binary file not shown.
BIN
PlayerTracker.exe
Executable file
BIN
PlayerTracker.exe
Executable file
Binary file not shown.
@ -1,12 +1,20 @@
|
|||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// =========================
|
|
||||||
// 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 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);
|
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
if (sockfd < 0) {
|
if (sockfd < 0) {
|
||||||
perror("socket creation failed");
|
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) {
|
if (connect(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
||||||
perror("connection to the server failed");
|
perror("connection to the server failed");
|
||||||
close(sockfd);
|
CLOSESOCKET(sockfd);
|
||||||
return -1;
|
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) {
|
if (bind(sockfd, (const struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
|
||||||
perror("bind failed");
|
perror("bind failed");
|
||||||
close(sockfd);
|
CLOSESOCKET(sockfd);
|
||||||
return -1;
|
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_port = htons(dest_port);
|
||||||
destaddr.sin_addr.s_addr = inet_addr(dest_ip);
|
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));
|
ssize_t n = sendto(sockfd, message, strlen(message), 0, (const struct sockaddr*)&destaddr, sizeof(destaddr));
|
||||||
|
#endif
|
||||||
|
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
perror("sendto failed");
|
perror("sendto failed");
|
||||||
return -1;
|
return -1;
|
||||||
@ -62,3 +75,10 @@ ssize_t send_udp_message(int sockfd, const char *message, const char *dest_ip, u
|
|||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
void cleanup_sockets() {
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|||||||
@ -5,17 +5,24 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#pragma comment(lib, "ws2_32.lib") // link Winsock
|
#pragma comment(lib, "ws2_32.lib")
|
||||||
|
#define CLOSESOCKET closesocket
|
||||||
#else
|
#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 <arpa/inet.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#define CLOSESOCKET close
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// =========================
|
// =========================
|
||||||
|
|||||||
@ -4,21 +4,42 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
|
#pragma comment(lib, "ws2_32.lib") // only needed for MSVC
|
||||||
|
#else
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "server_structs.h"
|
#include "server_structs.h"
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
const int SERVER_OUT_PORT = 12000;
|
// Cross-platform close macro
|
||||||
const int SERVER_IN_PORT = 11000;
|
#ifdef _WIN32
|
||||||
const char *SERVER_OUT_IP = "127.0.0.1";
|
#define CLOSESOCKET closesocket
|
||||||
|
#else
|
||||||
|
#define CLOSESOCKET close
|
||||||
|
#endif
|
||||||
|
|
||||||
int main(void) {
|
const int SERVER_OUT_PORT = 9996;
|
||||||
|
const int SERVER_IN_PORT = 11000;
|
||||||
|
const char* SERVER_OUT_IP = "127.0.0.1";
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSADATA wsaData;
|
||||||
|
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
||||||
|
fprintf(stderr, "[-] WSAStartup failed\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
printf("[+] Starting server...\n");
|
printf("[+] Starting server...\n");
|
||||||
// Printf information about the server
|
|
||||||
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
printf("[+] Server listening on port %d\n", SERVER_IN_PORT);
|
||||||
printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
printf("[+] Server sending to %s:%d\n", SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||||
|
|
||||||
@ -26,6 +47,9 @@ int main(void) {
|
|||||||
int sock_FD = connect_udp_socket(SERVER_OUT_IP, SERVER_OUT_PORT);
|
int sock_FD = connect_udp_socket(SERVER_OUT_IP, SERVER_OUT_PORT);
|
||||||
if (sock_FD < 0) {
|
if (sock_FD < 0) {
|
||||||
fprintf(stderr, "[-] Failed to create UDP socket\n");
|
fprintf(stderr, "[-] Failed to create UDP socket\n");
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,39 +57,52 @@ int main(void) {
|
|||||||
hs.identifier = 1;
|
hs.identifier = 1;
|
||||||
hs.operationId = 1;
|
hs.operationId = 1;
|
||||||
hs.version = 0;
|
hs.version = 0;
|
||||||
// Send handshake message
|
|
||||||
|
|
||||||
printf("[+] Sending handshake message...\t");
|
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));
|
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) {
|
if (bytes_sent >= 0) {
|
||||||
printf("OK (%zd bytes)\n", bytes_sent);
|
printf("OK (%zd bytes)\n", bytes_sent);
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "ERROR. \n");
|
fprintf(stderr, "ERROR.\n");
|
||||||
close((int)sock_FD);
|
CLOSESOCKET(sock_FD);
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buffer[512]; // bigger than struct
|
uint8_t buffer[512]; // bigger than struct
|
||||||
ssize_t bytes_received = recv(sock_FD, buffer, sizeof(buffer), 0);
|
ssize_t bytes_received = recv(sock_FD, (char*)buffer, sizeof(buffer), 0);
|
||||||
|
|
||||||
if (bytes_received >= sizeof(handshackerResponse)) {
|
if (bytes_received >= sizeof(handshackerResponse)) {
|
||||||
|
|
||||||
|
// Convert strings from big endian to little endian if necessary
|
||||||
|
|
||||||
handshackerResponse resp;
|
handshackerResponse resp;
|
||||||
memcpy(&resp, buffer, sizeof(handshackerResponse));
|
memcpy(&resp, buffer, sizeof(handshackerResponse));
|
||||||
|
|
||||||
printf("[+] Received handshake response:\n");
|
printf("[+] Received handshake response:\n");
|
||||||
printf(" Car: %0x\n", resp.carName);
|
printf(" Car: ");
|
||||||
printf(" Driver: %0x\n", resp.driverName);
|
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(" Identifier: %d\n", resp.identifier);
|
||||||
printf(" Version: %0x\n", resp.version);
|
printf(" Version: %d\n", resp.version);
|
||||||
printf(" Track: %0x\n", resp.trackName);
|
printf(" Track: %s\n", resp.trackName);
|
||||||
printf(" Config: %0x\n", resp.trackConfig);
|
printf(" Config: %s\n", resp.trackConfig);
|
||||||
} else {
|
} else {
|
||||||
printf("[!] Packet too short for handshake response (%zd bytes)\n", bytes_received);
|
printf("[!] Packet too short for handshake response (%zd bytes)\n", bytes_received);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CLOSESOCKET(sock_FD);
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
WSACleanup();
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user