From f4841280e78028b1119246167815ae756c2d9786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Mon, 13 Apr 2026 00:12:49 +1000 Subject: [PATCH 1/2] netenv: enhance broadcast logging to prevent log spamming by tracking unique source/destination pairs --- common/platform/netenv.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/common/platform/netenv.c b/common/platform/netenv.c index ecf2d7e..e99c4d4 100644 --- a/common/platform/netenv.c +++ b/common/platform/netenv.c @@ -165,6 +165,9 @@ static uint32_t netenv_ip_bcast; static uint32_t netenv_ip_iface; static uint32_t netenv_ip_router; static uint8_t netenv_mac_addr[6]; +static uint32_t netenv_last_logged_broadcast_src; +static uint32_t netenv_last_logged_broadcast_dest; +static bool netenv_broadcast_logged; HRESULT netenv_hook_init( const struct netenv_config *cfg, @@ -574,9 +577,19 @@ static int WINAPI hook_sendto( uint32_t src_addr = _byteswap_ulong(original_to->sin_addr.S_un.S_addr); uint32_t dest_addr = _byteswap_ulong(netenv_ip_bcast); - dprintf("Netenv: sendTo broadcast %u.%u.%u.%u -> %u.%u.%u.%u\n", - (src_addr >> 24) & 0xff, (src_addr >> 16) & 0xff, (src_addr >> 8) & 0xff, src_addr & 0xff, - (dest_addr >> 24) & 0xff, (dest_addr >> 16) & 0xff, (dest_addr >> 8) & 0xff, dest_addr & 0xff); + // Only log the first broadcast packet for each unique source/destination pair, + // to avoid spamming the log with ARP packets and such + if (!netenv_broadcast_logged || + netenv_last_logged_broadcast_src != src_addr || + netenv_last_logged_broadcast_dest != dest_addr) { + dprintf("Netenv: sendTo broadcast %u.%u.%u.%u -> %u.%u.%u.%u\n", + (src_addr >> 24) & 0xff, (src_addr >> 16) & 0xff, (src_addr >> 8) & 0xff, src_addr & 0xff, + (dest_addr >> 24) & 0xff, (dest_addr >> 16) & 0xff, (dest_addr >> 8) & 0xff, dest_addr & 0xff); + + netenv_last_logged_broadcast_src = src_addr; + netenv_last_logged_broadcast_dest = dest_addr; + netenv_broadcast_logged = true; + } struct sockaddr_in modified_to = {0}; memcpy(&modified_to, original_to, tolen); From d0d470c679772abf2705d6de23b71605175e25ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=B0=E3=83=AD=E3=83=BC=E3=83=A9=E3=83=B3=E3=83=97?= <130208311+Gl0w1amp@users.noreply.github.com> Date: Mon, 13 Apr 2026 00:18:32 +1000 Subject: [PATCH 2/2] netenv: add redirectBroadcast configuration option to control UDP broadcast redirection --- common/platform/config.c | 5 +++++ common/platform/netenv.c | 6 ++++++ common/platform/netenv.h | 1 + doc/config/common.md | 8 ++++++++ 4 files changed, 20 insertions(+) diff --git a/common/platform/config.c b/common/platform/config.c index 62cf167..75969bb 100644 --- a/common/platform/config.c +++ b/common/platform/config.c @@ -182,6 +182,11 @@ void netenv_config_load(struct netenv_config *cfg, const wchar_t *filename) memset(cfg, 0, sizeof(*cfg)); cfg->enable = GetPrivateProfileIntW(L"netenv", L"enable", 0, filename); + cfg->redirect_broadcast = GetPrivateProfileIntW( + L"netenv", + L"redirectBroadcast", + 1, + filename); cfg->addr_suffix = GetPrivateProfileIntW( L"netenv", diff --git a/common/platform/netenv.c b/common/platform/netenv.c index e99c4d4..be2e202 100644 --- a/common/platform/netenv.c +++ b/common/platform/netenv.c @@ -165,6 +165,7 @@ static uint32_t netenv_ip_bcast; static uint32_t netenv_ip_iface; static uint32_t netenv_ip_router; static uint8_t netenv_mac_addr[6]; +static bool netenv_redirect_broadcast; static uint32_t netenv_last_logged_broadcast_src; static uint32_t netenv_last_logged_broadcast_dest; static bool netenv_broadcast_logged; @@ -191,6 +192,7 @@ HRESULT netenv_hook_init( netenv_ip_iface = kc_cfg->subnet | cfg->addr_suffix; netenv_ip_router = kc_cfg->subnet | cfg->router_suffix; memcpy(netenv_mac_addr, cfg->mac_addr, sizeof(netenv_mac_addr)); + netenv_redirect_broadcast = cfg->redirect_broadcast; netenv_hook_apply_hooks(NULL); @@ -570,6 +572,10 @@ static int WINAPI hook_sendto( const struct sockaddr_in* original_to = (struct sockaddr_in*)to; + if (!netenv_redirect_broadcast) { + return next_sendto(s, buf, len, flags, to, tolen); + } + uint32_t bc_addr = _byteswap_ulong(netenv_ip_prefix | 0xFF); if (original_to->sin_addr.S_un.S_addr == bc_addr) { diff --git a/common/platform/netenv.h b/common/platform/netenv.h index 88cae15..2ed6c9c 100644 --- a/common/platform/netenv.h +++ b/common/platform/netenv.h @@ -9,6 +9,7 @@ struct netenv_config { bool enable; + bool redirect_broadcast; uint8_t addr_suffix; uint8_t router_suffix; uint8_t mac_addr[6]; diff --git a/doc/config/common.md b/doc/config/common.md index 3956a65..920905b 100644 --- a/doc/config/common.md +++ b/doc/config/common.md @@ -589,6 +589,14 @@ Default: `01:02:03:04:05:06` The MAC address of the virtualized Ethernet adapter. The exact value shouldn't ever matter. +### `redirectBroadcast` + +Default: `1` + +Redirect UDP packets sent to the virtual keychip subnet's broadcast address to +the address configured by `broadcast`. Disable this if you want to preserve the +original subnet-local broadcast destination. + ### `broadcast` Default: `255.255.255.255`