diff --git a/src/main/hooklib/Module.mk b/src/main/hooklib/Module.mk index 1527390..261a31f 100644 --- a/src/main/hooklib/Module.mk +++ b/src/main/hooklib/Module.mk @@ -4,6 +4,7 @@ src_hooklib := \ acp.c \ app.c \ adapter.c \ + config-adapter.c \ rs232.c \ setupapi.c \ diff --git a/src/main/hooklib/adapter.c b/src/main/hooklib/adapter.c index 082d698..60b79e4 100644 --- a/src/main/hooklib/adapter.c +++ b/src/main/hooklib/adapter.c @@ -3,12 +3,16 @@ #include #include +#include +#include + #include "hook/table.h" #include "hooklib/adapter.h" #include "util/codepage.h" #include "util/defs.h" +#include "util/log.h" static DWORD WINAPI my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len); @@ -16,6 +20,9 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len); static DWORD(WINAPI *real_GetAdaptersInfo)( PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len); +static IP_ADDRESS_STRING override_address; +static bool use_address_override; + static const struct hook_symbol adapter_hook_syms[] = { {.name = "GetAdaptersInfo", .patch = my_GetAdaptersInfo, @@ -37,6 +44,24 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len) return ret; } + info = adapter_info; + + if (use_address_override) { + while (info) { + // this is well defined to be at most sizeof(IP_ADDRESS_STRING) + // and NULL filled if shorter (hence the memset in adapter_hook_override) + if (!memcmp(info->IpAddressList.IpAddress.String, override_address.String, sizeof(IP_ADDRESS_STRING))) { + log_info("%s: using [override] adapter: %s, %s, %s, %s", __FUNCTION__, info->AdapterName, info->Description, info->IpAddressList.IpAddress.String, info->IpAddressList.IpMask.String); + + // copy only this adapter over + memcpy(adapter_info, info, sizeof(*info)); + adapter_info->Next = 0; + return ret; + } + info = info->Next; + } + } + ip_fwd_table = (MIB_IPFORWARDTABLE *) malloc(sizeof(MIB_IPFORWARDTABLE)); table_size = 0; @@ -58,6 +83,9 @@ my_GetAdaptersInfo(PIP_ADAPTER_INFO adapter_info, PULONG out_buf_len) while (info) { if (info->Index == best_adapter) { + log_info("%s: using [best] adapter: %s, %s, %s, %s", __FUNCTION__, info->AdapterName, info->Description, info->IpAddressList.IpAddress.String, info->IpAddressList.IpMask.String); + + // copy only this adapter over memcpy(adapter_info, info, sizeof(*info)); adapter_info->Next = 0; return ret; @@ -73,3 +101,24 @@ void adapter_hook_init(void) hook_table_apply( NULL, "iphlpapi.dll", adapter_hook_syms, lengthof(adapter_hook_syms)); } + +void adapter_hook_override(const char* adapter_address) +{ + // starts off false anyways due to static + // but in case it gets called multiple times, set it anyways + use_address_override = false; + + if (adapter_address == NULL || *adapter_address == NULL) { + // empty, do nothing + return; + } + if (strlen(adapter_address) > sizeof(IP_ADDRESS_STRING)) { + log_warning("%s: %s is not an ipv4 address", __FUNCTION__, adapter_address); + return; + } + + memset(override_address.String, 0, sizeof(IP_ADDRESS_STRING)); + memcpy(override_address.String, adapter_address, sizeof(IP_ADDRESS_STRING)); + + use_address_override = true; +} \ No newline at end of file diff --git a/src/main/hooklib/adapter.h b/src/main/hooklib/adapter.h index b6a83ce..1f70931 100644 --- a/src/main/hooklib/adapter.h +++ b/src/main/hooklib/adapter.h @@ -3,4 +3,11 @@ void adapter_hook_init(void); +/** + * Uses the provided address to try and match a network adapter + * + * @param network ip to match + */ +void adapter_hook_override(const char* adapter_address); + #endif diff --git a/src/main/hooklib/config-adapter.c b/src/main/hooklib/config-adapter.c new file mode 100644 index 0000000..e8d2167 --- /dev/null +++ b/src/main/hooklib/config-adapter.c @@ -0,0 +1,34 @@ +#include "cconfig/cconfig-util.h" + +#include "hooklib/config-adapter.h" + +#include "util/log.h" + +#define HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY "adapter.override_ip" + +#define HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE "" + +void hooklib_config_adapter_init(struct cconfig *config) +{ + cconfig_util_set_str( + config, + HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY, + HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE, + "IP of adapter to force override with"); +} + +void hooklib_config_adapter_get(struct hooklib_config_adapter *config_adapter, struct cconfig *config) +{ + if (!cconfig_util_get_str( + config, + HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY, + config_adapter->override_ip, + sizeof(config_adapter->override_ip), + HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE)) { + log_warning( + "Invalid value for key '%s' specified, fallback " + "to default '%s'", + HOOKLIB_CONFIG_ADAPTER_OVERRIDE_IP_KEY, + HOOKLIB_CONFIG_ADAPTER_DEFAULT_OVERRIDE_IP_VALUE); + } +} diff --git a/src/main/hooklib/config-adapter.h b/src/main/hooklib/config-adapter.h new file mode 100644 index 0000000..f2013fe --- /dev/null +++ b/src/main/hooklib/config-adapter.h @@ -0,0 +1,17 @@ +#ifndef HOOKLIB_CONFIG_ADAPTER_H +#define HOOKLIB_CONFIG_ADAPTER_H + +#include + +#include "cconfig/cconfig.h" + +struct hooklib_config_adapter { + // this is larger on purpose, in case ppl enter the wrong stuff here + char override_ip[32]; +}; + +void hooklib_config_adapter_init(struct cconfig *config); + +void hooklib_config_adapter_get(struct hooklib_config_adapter *config_adapter, struct cconfig *config); + +#endif \ No newline at end of file