Skip to content

Commit 1825372

Browse files
committed
fix(dns): Handle IPv6 DNS server address results
If the result from esp_netif_get_dns_info is an IPv6 address, then parse to an IPAddress.
1 parent 6c6666a commit 1825372

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Diff for: libraries/Network/src/NetworkInterface.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,35 @@ IPAddress NetworkInterface::dnsIP(uint8_t dns_no) const
606606
if(esp_netif_get_dns_info(_esp_netif, dns_no?ESP_NETIF_DNS_BACKUP:ESP_NETIF_DNS_MAIN, &d) != ESP_OK){
607607
return IPAddress();
608608
}
609+
if (d.ip.type == ESP_IPADDR_TYPE_V6){
610+
// IPv6 from 4x uint32_t; byte order based on IPV62STR() in esp_netif_ip_addr.h
611+
log_d("DNS got IPv6: " IPV6STR, IPV62STR(d.ip.u_addr.ip6));
612+
uint32_t addr0 esp_netif_htonl(d.ip.u_addr.ip6.addr[0]);
613+
uint32_t addr1 esp_netif_htonl(d.ip.u_addr.ip6.addr[1]);
614+
uint32_t addr2 esp_netif_htonl(d.ip.u_addr.ip6.addr[2]);
615+
uint32_t addr3 esp_netif_htonl(d.ip.u_addr.ip6.addr[3]);
616+
return IPAddress(
617+
(uint8_t)(addr0 >> 24) & 0xFF,
618+
(uint8_t)(addr0 >> 16) & 0xFF,
619+
(uint8_t)(addr0 >> 8) & 0xFF,
620+
(uint8_t)addr0 & 0xFF,
621+
(uint8_t)(addr1 >> 24) & 0xFF,
622+
(uint8_t)(addr1 >> 16) & 0xFF,
623+
(uint8_t)(addr1 >> 8) & 0xFF,
624+
(uint8_t)addr1 & 0xFF,
625+
(uint8_t)(addr2 >> 24) & 0xFF,
626+
(uint8_t)(addr2 >> 16) & 0xFF,
627+
(uint8_t)(addr2 >> 8) & 0xFF,
628+
(uint8_t)addr2 & 0xFF,
629+
(uint8_t)(addr3 >> 24) & 0xFF,
630+
(uint8_t)(addr3 >> 16) & 0xFF,
631+
(uint8_t)(addr3 >> 8) & 0xFF,
632+
(uint8_t)addr3 & 0xFF,
633+
d.ip.u_addr.ip6.zone
634+
);
635+
}
636+
// IPv4 from single uint32_t
637+
log_d("DNS IPv4: " IPSTR, IP2STR(&d.ip.u_addr.ip4));
609638
return IPAddress(d.ip.u_addr.ip4.addr);
610639
}
611640

0 commit comments

Comments
 (0)