Skip to content

Commit 64235dc

Browse files
authored
Network refactoring - fix some IPv6 DNS issues (espressif#9439)
* 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. * fix(dns): Use getaddrinfo for DNS, to fix some IPv6 issues Replace hostbyname with getaddrinfo for better IPv6 support. The API is also simpler, as it has no callbacks (they are handled internally). Allows dual-stack networks to connect to IPv6-only destinations. Still does not work for IPv6-only networks, as IPv6 DNS is not enabled in the pre-built ESP-IDF libraries.
1 parent 1a80829 commit 64235dc

File tree

7 files changed

+69
-72
lines changed

7 files changed

+69
-72
lines changed

libraries/Network/src/NetworkEvents.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ NetworkEvents::~NetworkEvents(){
5555
}
5656
}
5757

58-
static uint32_t _initial_bits = NET_DNS_IDLE_BIT;
58+
static uint32_t _initial_bits = 0;
5959

6060
bool NetworkEvents::initNetworkEvents(){
6161
if(!_arduino_event_group){

libraries/Network/src/NetworkEvents.h

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ static const int WIFI_SCANNING_BIT = BIT0;
2828
static const int WIFI_SCAN_DONE_BIT= BIT1;
2929
#endif
3030

31-
static const int NET_DNS_IDLE_BIT = BIT2;
32-
static const int NET_DNS_DONE_BIT = BIT3;
33-
3431
#define NET_HAS_IP6_GLOBAL_BIT 0
3532

3633
ESP_EVENT_DECLARE_BASE(ARDUINO_EVENTS);

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

libraries/Network/src/NetworkManager.cpp

+35-64
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
*/
66
#include "NetworkManager.h"
77
#include "NetworkInterface.h"
8+
#include "IPAddress.h"
89
#include "esp_netif.h"
9-
#include "lwip/ip_addr.h"
1010
#include "lwip/dns.h"
11-
#include "esp32-hal-log.h"
1211
#include "esp_mac.h"
12+
#include "netdb.h"
1313

1414
NetworkManager::NetworkManager(){
1515

@@ -36,54 +36,16 @@ bool NetworkManager::begin(){
3636
return initialized;
3737
}
3838

39-
typedef struct gethostbynameParameters {
40-
const char *hostname;
41-
ip_addr_t addr;
42-
uint8_t addr_type;
43-
int result;
44-
} gethostbynameParameters_t;
45-
46-
/**
47-
* DNS callback
48-
* @param name
49-
* @param ipaddr
50-
* @param callback_arg
51-
*/
52-
static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
53-
{
54-
gethostbynameParameters_t *parameters = static_cast<gethostbynameParameters_t *>(callback_arg);
55-
if(ipaddr) {
56-
if(parameters->result == 0){
57-
memcpy(&(parameters->addr), ipaddr, sizeof(ip_addr_t));
58-
parameters->result = 1;
59-
}
60-
} else {
61-
parameters->result = -1;
62-
}
63-
Network.setStatusBits(NET_DNS_DONE_BIT);
64-
}
65-
66-
/**
67-
* Callback to execute dns_gethostbyname in lwIP's TCP/IP context
68-
* @param param Parameters for dns_gethostbyname call
69-
*/
70-
static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
71-
{
72-
gethostbynameParameters_t *parameters = static_cast<gethostbynameParameters_t *>(param);
73-
return dns_gethostbyname_addrtype(parameters->hostname, &parameters->addr, &wifi_dns_found_callback, parameters, parameters->addr_type);
74-
}
75-
7639
/**
7740
* Resolve the given hostname to an IP address.
7841
* @param aHostname Name to be resolved
7942
* @param aResult IPAddress structure to store the returned IP address
8043
* @return 1 if aIPAddrString was successfully converted to an IP address,
8144
* else error code
8245
*/
83-
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
46+
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
8447
{
8548
err_t err = ERR_OK;
86-
gethostbynameParameters_t params;
8749

8850
// This should generally check if we have a global address assigned to one of the interfaces.
8951
// If such address is not assigned, there is no point in trying to get V6 from DNS as we will not be able to reach it.
@@ -97,32 +59,41 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool p
9759
}
9860

9961
aResult = static_cast<uint32_t>(0);
100-
params.hostname = aHostname;
101-
params.addr_type = (preferV6 || hasGlobalV6)?LWIP_DNS_ADDRTYPE_IPV6_IPV4:LWIP_DNS_ADDRTYPE_IPV4;
102-
params.result = 0;
103-
aResult.to_ip_addr_t(&(params.addr));
104-
105-
if (!aResult.fromString(aHostname)) {
106-
Network.waitStatusBits(NET_DNS_IDLE_BIT, 16000);
107-
Network.clearStatusBits(NET_DNS_IDLE_BIT | NET_DNS_DONE_BIT);
108-
109-
err = esp_netif_tcpip_exec(wifi_gethostbyname_tcpip_ctx, &params);
110-
if (err == ERR_OK) {
111-
aResult.from_ip_addr_t(&(params.addr));
112-
} else if (err == ERR_INPROGRESS) {
113-
Network.waitStatusBits(NET_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
114-
Network.clearStatusBits(NET_DNS_DONE_BIT);
115-
if (params.result == 1) {
116-
aResult.from_ip_addr_t(&(params.addr));
117-
err = ERR_OK;
118-
}
119-
}
120-
Network.setStatusBits(NET_DNS_IDLE_BIT);
62+
63+
// First check if the host parses as a literal address
64+
if (aResult.fromString(aHostname)) {
65+
return 1;
12166
}
122-
if (err == ERR_OK) {
67+
68+
const char *servname = "0";
69+
struct addrinfo *res;
70+
const struct addrinfo hints = {
71+
.ai_family = AF_UNSPEC,
72+
.ai_socktype = SOCK_STREAM,
73+
};
74+
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
75+
if (err == ERR_OK)
76+
{
77+
if (res->ai_family == AF_INET6)
78+
{
79+
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
80+
// As an array of u8_t
81+
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
82+
log_d("DNS found IPv6 %s", aResult.toString().c_str());
83+
}
84+
else
85+
{
86+
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
87+
// As a single u32_t
88+
aResult = IPAddress(ipv4->sin_addr.s_addr);
89+
log_d("DNS found IPv4 %s", aResult.toString().c_str());
90+
}
91+
92+
lwip_freeaddrinfo(res);
12393
return 1;
12494
}
125-
log_e("DNS Failed for '%s' with error '%d' and result '%d'", aHostname, err, params.result);
95+
96+
log_e("DNS Failed for '%s' with error '%d'", aHostname, err);
12697
return err;
12798
}
12899

libraries/Network/src/NetworkManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class NetworkManager : public NetworkEvents, public Printable {
1414
NetworkManager();
1515

1616
bool begin();
17-
int hostByName(const char *aHostname, IPAddress &aResult, bool preferV6=false);
17+
int hostByName(const char *aHostname, IPAddress &aResult);
1818
uint8_t * macAddress(uint8_t * mac);
1919
String macAddress();
2020

libraries/WiFi/src/WiFiGeneric.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -794,9 +794,9 @@ bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2
794794
/*
795795
* Deprecated Methods
796796
*/
797-
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
797+
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
798798
{
799-
return Network.hostByName(aHostname, aResult, preferV6);
799+
return Network.hostByName(aHostname, aResult);
800800
}
801801

802802
IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {

libraries/WiFi/src/WiFiGeneric.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class WiFiGenericClass
115115
static void useStaticBuffers(bool bufferMode);
116116
static bool useStaticBuffers();
117117

118-
static int hostByName(const char *aHostname, IPAddress &aResult, bool preferV6=false);
118+
static int hostByName(const char *aHostname, IPAddress &aResult);
119119

120120
static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
121121
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);

0 commit comments

Comments
 (0)