/* * Settings and log next to the DLL. * * wto_ws.cfg, written by the build into the game copy (@wto/patcher network-hook.ts): * port= without a valid port the hooks still answer every host name * locally but refuse every connection that would leave the machine * exe.time_date_stamp=0x... the executable the native values were resolved for * exe.size_of_image=0x... * native.= what the LocalMP modules need from the executable: RVAs of * functions and globals, structure offsets (local_mp_values.c) * * wto_ws.log: which host names were answered locally and which connections were redirected or * refused, so a run shows every address the game tried (the audit handover 6.2 asks for). */ #include "hook_config.h" #include "redirect_rules.h" #include #include #include #include #include #define NATIVE_PREFIX "native." #define NATIVE_NAME_MAX 80 #define NATIVE_VALUES_MAX 512 typedef struct NativeValue { char name[NATIVE_NAME_MAX]; int64_t value; } NativeValue; static wchar_t g_folder[MAX_PATH]; static CRITICAL_SECTION g_log_lock; static FILE *g_log; static NativeValue g_native[NATIVE_VALUES_MAX]; static size_t g_native_count; static int64_t g_exe_time_date_stamp = -1; static int64_t g_exe_size_of_image = -1; static void find_folder(HMODULE self) { DWORD length = GetModuleFileNameW(self, g_folder, MAX_PATH); if (length == 0 || length >= MAX_PATH) { g_folder[0] = L'\0'; return; } wchar_t *slash = wcsrchr(g_folder, L'\\'); if (slash) slash[1] = L'\0'; } static void read_port(const char *text) { char *end; unsigned long port = strtoul(text, &end, 10); if (end != text && port > 0 && port < 65536) g_service_port = (unsigned short)port; } /* "" as written by the build: hexadecimal 0x..., negative as -0x... */ static int parse_value(const char *text, int64_t *value) { char *end; long long parsed = strtoll(text, &end, 0); if (end == text) return 0; *value = (int64_t)parsed; return 1; } static void read_native_value(const char *line) { const char *name = line + sizeof(NATIVE_PREFIX) - 1; const char *equals = strchr(name, '='); size_t length = equals ? (size_t)(equals - name) : 0; NativeValue *slot; if (length == 0 || length >= NATIVE_NAME_MAX || g_native_count >= NATIVE_VALUES_MAX) return; slot = &g_native[g_native_count]; memcpy(slot->name, name, length); slot->name[length] = '\0'; if (parse_value(equals + 1, &slot->value)) g_native_count++; } static void read_settings(void) { wchar_t path[MAX_PATH]; char line[160]; FILE *file; if (_snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.cfg", g_folder) < 0) return; if (_wfopen_s(&file, path, L"r") != 0 || file == NULL) return; while (fgets(line, sizeof(line), file)) { line[strcspn(line, "\r\n")] = '\0'; if (strncmp(line, "port=", 5) == 0) read_port(line + 5); else if (strncmp(line, "exe.time_date_stamp=", 20) == 0) parse_value(line + 20, &g_exe_time_date_stamp); else if (strncmp(line, "exe.size_of_image=", 18) == 0) parse_value(line + 18, &g_exe_size_of_image); else if (strncmp(line, NATIVE_PREFIX, sizeof(NATIVE_PREFIX) - 1) == 0) read_native_value(line); } fclose(file); } int config_native_value(const char *name, int64_t *value) { size_t i; for (i = 0; i < g_native_count; i++) { if (strcmp(g_native[i].name, name) == 0) { *value = g_native[i].value; return 1; } } return 0; } int config_native_matches_executable(void) { const uint8_t *module = (const uint8_t *)GetModuleHandleW(NULL); const IMAGE_DOS_HEADER *dos = (const IMAGE_DOS_HEADER *)module; const IMAGE_NT_HEADERS64 *nt; if (module == NULL || dos->e_magic != IMAGE_DOS_SIGNATURE) return 0; nt = (const IMAGE_NT_HEADERS64 *)(module + dos->e_lfanew); return nt->Signature == IMAGE_NT_SIGNATURE && (int64_t)nt->FileHeader.TimeDateStamp == g_exe_time_date_stamp && (int64_t)nt->OptionalHeader.SizeOfImage == g_exe_size_of_image; } void config_load(HMODULE self) { wchar_t path[MAX_PATH]; InitializeCriticalSection(&g_log_lock); find_folder(self); read_settings(); if (_snwprintf_s(path, MAX_PATH, _TRUNCATE, L"%swto_ws.log", g_folder) >= 0) g_log = _wfsopen(path, L"w", _SH_DENYWR); log_line("wto_ws: local server port %u%s", g_service_port, g_service_port ? "" : " (none: outside connections are refused)"); log_line("wto_ws: %u native value(s) for the LocalMP modules", (unsigned)g_native_count); } void log_line(const char *format, ...) { va_list args; SYSTEMTIME now; if (g_log == NULL) return; GetLocalTime(&now); EnterCriticalSection(&g_log_lock); fprintf(g_log, "%02u:%02u:%02u.%03u ", now.wHour, now.wMinute, now.wSecond, now.wMilliseconds); va_start(args, format); vfprintf(g_log, format, args); va_end(args); fputc('\n', g_log); fflush(g_log); LeaveCriticalSection(&g_log_lock); } void describe_address(const struct sockaddr *name, int length, char *text, size_t size) { char host[INET6_ADDRSTRLEN] = "?"; unsigned short port = 0; if (name && name->sa_family == AF_INET && length >= (int)sizeof(struct sockaddr_in)) { const struct sockaddr_in *v4 = (const struct sockaddr_in *)name; inet_ntop(AF_INET, &v4->sin_addr, host, sizeof(host)); port = ntohs(v4->sin_port); } else if (name && name->sa_family == AF_INET6 && length >= (int)sizeof(struct sockaddr_in6)) { const struct sockaddr_in6 *v6 = (const struct sockaddr_in6 *)name; inet_ntop(AF_INET6, &v6->sin6_addr, host, sizeof(host)); port = ntohs(v6->sin6_port); } _snprintf_s(text, size, _TRUNCATE, "%s:%u", host, port); }