Skip to content

Commit 22034bf

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 1825372 commit 22034bf

File tree

3 files changed

+36
-68
lines changed

3 files changed

+36
-68
lines changed

Diff for: 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){

Diff for: 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);

Diff for: 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

0 commit comments

Comments
 (0)