Fix UDP server when in softap mode

This commit is contained in:
2026-02-19 14:57:10 -05:00
parent 20b047bbfe
commit 7e1ba801d1

View File

@@ -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;
}