Files
WT-Offline/native/wto-net/src/resolver_hooks.c
T
N0VAN0M4LY 2428b34497 feat(native): route client traffic to the loopback service
Build the socket library and client-local port configuration for redirecting
traffic to the offline service. Accept HTTPS and the existing matching and
HTTP protocols on the same port. Route requests by the original host.
2026-11-25 05:02:34 +08:00

76 lines
2.7 KiB
C

/*
* Host name resolution: every host name that is not a numeric address
* or localhost is resolved as the marker address 127.0.0.2, so no lookup leaves the machine
* and the connection hooks know the destination stood for a host name.
*/
#include "hooks.h"
#include "hook_config.h"
#include "redirect_rules.h"
typedef int (WSAAPI *getaddrinfo_fn)(PCSTR, PCSTR, const ADDRINFOA *, PADDRINFOA *);
typedef int (WSAAPI *GetAddrInfoW_fn)(PCWSTR, PCWSTR, const ADDRINFOW *, PADDRINFOW *);
typedef struct hostent *(WSAAPI *gethostbyname_fn)(const char *);
static getaddrinfo_fn real_getaddrinfo;
static GetAddrInfoW_fn real_GetAddrInfoW;
static gethostbyname_fn real_gethostbyname;
/* Only IPv4 answers: the marker is an IPv4 address. */
static int wants_only_ipv6(int family)
{
return family == AF_INET6;
}
static int WSAAPI hooked_getaddrinfo(PCSTR node, PCSTR service, const ADDRINFOA *hints, PADDRINFOA *result)
{
ADDRINFOA local_hints;
if (!is_redirected_host(node))
return real_getaddrinfo(node, service, hints, result);
log_line("resolve %s -> " MARKER_ADDRESS, node);
if (hints && wants_only_ipv6(hints->ai_family))
return WSAHOST_NOT_FOUND;
if (hints)
local_hints = *hints;
else {
ZeroMemory(&local_hints, sizeof(local_hints));
}
local_hints.ai_family = AF_INET;
local_hints.ai_flags |= AI_NUMERICHOST;
local_hints.ai_flags &= ~AI_CANONNAME;
return real_getaddrinfo(MARKER_ADDRESS, service, &local_hints, result);
}
static int WSAAPI hooked_GetAddrInfoW(PCWSTR node, PCWSTR service, const ADDRINFOW *hints, PADDRINFOW *result)
{
ADDRINFOW local_hints;
if (!is_redirected_host_w(node))
return real_GetAddrInfoW(node, service, hints, result);
log_line("resolve %ls -> " MARKER_ADDRESS, node);
if (hints && wants_only_ipv6(hints->ai_family))
return WSAHOST_NOT_FOUND;
if (hints)
local_hints = *hints;
else {
ZeroMemory(&local_hints, sizeof(local_hints));
}
local_hints.ai_family = AF_INET;
local_hints.ai_flags |= AI_NUMERICHOST;
local_hints.ai_flags &= ~AI_CANONNAME;
return real_GetAddrInfoW(MARKER_ADDRESS_W, service, &local_hints, result);
}
static struct hostent *WSAAPI hooked_gethostbyname(const char *name)
{
if (!is_redirected_host(name))
return real_gethostbyname(name);
log_line("resolve %s -> " MARKER_ADDRESS " (gethostbyname)", name);
return real_gethostbyname(MARKER_ADDRESS);
}
int install_resolver_hooks(void)
{
return hook_export("getaddrinfo", (void *)hooked_getaddrinfo, (void **)&real_getaddrinfo)
&& hook_export("GetAddrInfoW", (void *)hooked_GetAddrInfoW, (void **)&real_GetAddrInfoW)
&& hook_export("gethostbyname", (void *)hooked_gethostbyname, (void **)&real_gethostbyname);
}