mDNS hosts fail to resolve with getaddrinfo when in APSTA mode
Posted: Thu May 01, 2025 10:01 pm
I'm trying to use getaddrinfo to resolve an mdns hostname. This seems to work fine when I'm running in AP mode but when I start using APSTA, getaddrinfo occasionally fails. I'm wondering if I'm initializing something incorrectly or if there's a timing issue here.
I have two ESP32-S3s that are both in APSTA mode. One of the devices (n2kwg-xxx) is a STA to the other device's soft AP (tcm-xxx). On the AP device, I'm calling getaddrinfo with the hostname of the n2kwg device:
In my logs, with mdns debugging enabled, I'm seeing that the mdns packets are arriving to the mdns service just fine.
I can also see the mdns packets in Wireshark and it seems to be correct, but getaddrinfo is still failing. Any ideas at what's wrong here?
I have two ESP32-S3s that are both in APSTA mode. One of the devices (n2kwg-xxx) is a STA to the other device's soft AP (tcm-xxx). On the AP device, I'm calling getaddrinfo with the hostname of the n2kwg device:
Code: Select all
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_ADDRCONFIG,
};
struct addrinfo* resolved = NULL;
// [...]
struct sockaddr_in dest_addr = {
.sin_len = 0,
.sin_family = AF_INET,
.sin_port = (in_port_t)htons(s_settings->port),
.sin_addr = {0},
};
int32_t error = getaddrinfo(s_settings->address, NULL, &hints, &resolved);
if (error != 0 || resolved == NULL) {
ESP_LOGE(TAG, "getaddrinfo failed for '%s', returned %" PRId32, s_settings->address, error);
// TODO: freeaddrinfo if not null
vTaskDelay(pdMS_TO_TICKS(10000));
continue;
}
In my logs, with mdns debugging enabled, I'm seeing that the mdns packets are arriving to the mdns service just fine.
Code: Select all
RX[1][0]: From: 192.168.5.2:5353, To: 224.0.0.251, Packet[9950]: AUTHORITATIVE
A: _services._dns-sd._udp.local. PTR IN 4500[17] ._xxx._tcp.local.
A: _xxx._tcp.local. PTR IN 4500[21] n2kwg-348518454ac0._xxx._tcp.local.
A: n2kwg-348518454ac0._xxx._tcp.local. SRV IN FLUSH 120[27] 0 0 9 n2kwg-348518454ac0.local.
A: n2kwg-348518454ac0._xxx._tcp.local. TXT IN FLUSH 4500[174] device_name=xxxxxx; services=_http._tcp, _n2k._tcp; device_type=n2kwg
A: n2kwg-348518454ac0.local. A IN FLUSH 120[4] 192.168.5.2
A: _services._dns-sd._udp.local. PTR IN 4500[7] ._n2k._tcp.local.
A: _n2k._tcp.local. PTR IN 4500[21] n2kwg-348518454ac0._n2k._tcp.local.
A: n2kwg-348518454ac0._n2k._tcp.local. SRV IN FLUSH 120[8] 0 0 2000 n2kwg-348518454ac0.local.
A: n2kwg-348518454ac0._n2k._tcp.local. TXT IN FLUSH 4500[1]
A: _services._dns-sd._udp.local. PTR IN 4500[8] ._http._tcp.local.
A: _http._tcp.local. PTR IN 4500[21] n2kwg-348518454ac0._http._tcp.local.
A: n2kwg-348518454ac0._http._tcp.local. SRV IN FLUSH 120[8] 0 0 80 n2kwg-348518454ac0.local.
A: n2kwg-348518454ac0._http._tcp.local. TXT IN FLUSH 4500[29] api_path=/api/v1/; web_path=/
E (26355) n2kwg: getaddrinfo failed for 'n2kwg-348518454ac0.local', returned 202