Skip to content

Commit fd77166

Browse files
committed
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 7b29bac commit fd77166

File tree

1 file changed

+34
-58
lines changed

1 file changed

+34
-58
lines changed

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

+34-58
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,43 +36,6 @@ 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
@@ -82,8 +45,9 @@ static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
8245
*/
8346
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
8447
{
48+
// IDEA: Rename to getAddressInfo() ?
49+
8550
err_t err = ERR_OK;
86-
gethostbynameParameters_t params;
8751

8852
// This should generally check if we have a global address assigned to one of the interfaces.
8953
// 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 +61,44 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool p
9761
}
9862

9963
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));
10464

65+
// First check if the host parses as a literal address
10566
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;
67+
const char *servname = "0";
68+
struct addrinfo *res;
69+
const struct addrinfo hints = {
70+
.ai_family = AF_UNSPEC,
71+
.ai_socktype = SOCK_STREAM,
72+
};
73+
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
74+
if (err == 0)
75+
{
76+
if (res->ai_family == AF_INET6)
77+
{
78+
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
79+
// As an array of u8_t
80+
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
81+
log_d("DNS found IPv6 %s", aResult.toString().c_str());
82+
}
83+
else if (res->ai_family == AF_INET)
84+
{
85+
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
86+
// As a single u32_t
87+
aResult = IPAddress(ipv4->sin_addr.s_addr);
88+
log_d("DNS found IPv4 %s", aResult.toString().c_str());
11889
}
90+
else
91+
{
92+
err = -1;
93+
}
94+
95+
lwip_freeaddrinfo(res);
11996
}
120-
Network.setStatusBits(NET_DNS_IDLE_BIT);
12197
}
12298
if (err == ERR_OK) {
12399
return 1;
124100
}
125-
log_e("DNS Failed for '%s' with error '%d' and result '%d'", aHostname, err, params.result);
101+
log_e("DNS Failed for '%s' with error '%d'", aHostname, err);
126102
return err;
127103
}
128104

0 commit comments

Comments
 (0)