From 7e1ba801d15bcb1434dec73312e1ef1a850d6d82 Mon Sep 17 00:00:00 2001 From: Johnathon Slightham Date: Thu, 19 Feb 2026 14:57:10 -0500 Subject: [PATCH] Fix UDP server when in softap mode --- components/rpc/wireless/UDPServer.cpp | 35 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/components/rpc/wireless/UDPServer.cpp b/components/rpc/wireless/UDPServer.cpp index d82e704..4d36370 100644 --- a/components/rpc/wireless/UDPServer.cpp +++ b/components/rpc/wireless/UDPServer.cpp @@ -119,9 +119,25 @@ void UDPServer::shutdown() { continue; } + // Resolve the active interface IP so multicast joins the correct interface + // (important in SoftAP mode where INADDR_ANY may pick the wrong/no interface) + esp_netif_ip_info_t active_ip_info = {}; + esp_netif_t *active_netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); + if (active_netif == nullptr || + esp_netif_get_ip_info(active_netif, &active_ip_info) != ESP_OK || + active_ip_info.ip.addr == 0) { + active_netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); + active_ip_info = {}; + if (active_netif != nullptr) { + esp_netif_get_ip_info(active_netif, &active_ip_info); + } + } + struct ip_mreq imreq = {}; imreq.imr_multiaddr.s_addr = inet_addr(RECV_MCAST); - imreq.imr_interface.s_addr = htonl(INADDR_ANY); + imreq.imr_interface.s_addr = active_ip_info.ip.addr != 0 + ? active_ip_info.ip.addr + : htonl(INADDR_ANY); if (setsockopt(that->m_rx_server_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imreq, sizeof(struct ip_mreq)) < 0) { ESP_LOGE(TAG, "Failed to set IP_ADD_MEMBERSHIP. Error %d", errno); @@ -164,24 +180,19 @@ void UDPServer::shutdown() { } bool UDPServer::is_network_connected() { - esp_netif_ip_info_t ip_info; + esp_netif_ip_info_t ip_info = {}; esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); - if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) { - return true; - } - - if (0 != ip_info.ip.addr) { + if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && + ip_info.ip.addr != 0) { return true; } netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"); + ip_info = {}; - if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) { - return true; - } - - if (0 != ip_info.ip.addr) { + if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && + ip_info.ip.addr != 0) { return true; }