Hello :), i'm trying to create a proxy in C that stands between the client and the nostale server and shows me the packets exchanged with the game. I see that every time notale is started on a local port which is dynamic and changes every time the game is opened. Below is my C code used to create the TCP proxy.
Should I need to implement in this code something that allows me to preload the port on the nostal hooks? And if so, can anyone tell me which solutions I could adapt?
Sorry for my english and ty to all
Should I need to implement in this code something that allows me to preload the port on the nostal hooks? And if so, can anyone tell me which solutions I could adapt?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define PORT LOCAL_PORT
int main() {
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c;
printf("\nInitializing Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("WSAStartup failed. Error Code : %d", WSAGetLastError());
return 1;
}
if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
printf("Could not create socket : %d", WSAGetLastError());
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
if (bind(s, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
printf("Bind failed with error code : %d", WSAGetLastError());
}
listen(s, 3);
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(s, (struct sockaddr*)&client, &c)) != INVALID_SOCKET) {
puts("Connection accepted");
SOCKET server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == INVALID_SOCKET) {
printf("Could not create socket : %d", WSAGetLastError());
return 1;
}
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("NOSTALE_IP_SERVER");
server_address.sin_port = htons(NOSTALE_PORT);
if (connect(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
printf("Connection error");
return 1;
}
char buffer[1024];
int bytes_received;
while ((bytes_received = recv(new_socket, buffer, sizeof(buffer), 0)) > 0) {
send(server_socket, buffer, bytes_received, 0);
memset(buffer, 0, sizeof(buffer));
bytes_received = recv(server_socket, buffer, sizeof(buffer), 0);
send(new_socket, buffer, bytes_received, 0);
memset(buffer, 0, sizeof(buffer));
}
closesocket(server_socket);
puts("Remote server connection closed");
closesocket(new_socket);
puts("Client connection closed");
}
WSACleanup();
return 0;
}