Skip to content

Commit

Permalink
Network refactoring - fix some IPv6 DNS issues (#9439)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sgryphon committed Apr 3, 2024
1 parent 1a80829 commit 64235dc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 72 deletions.
2 changes: 1 addition & 1 deletion libraries/Network/src/NetworkEvents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ NetworkEvents::~NetworkEvents(){
}
}

static uint32_t _initial_bits = NET_DNS_IDLE_BIT;
static uint32_t _initial_bits = 0;

bool NetworkEvents::initNetworkEvents(){
if(!_arduino_event_group){
Expand Down
3 changes: 0 additions & 3 deletions libraries/Network/src/NetworkEvents.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ static const int WIFI_SCANNING_BIT = BIT0;
static const int WIFI_SCAN_DONE_BIT= BIT1;
#endif

static const int NET_DNS_IDLE_BIT = BIT2;
static const int NET_DNS_DONE_BIT = BIT3;

#define NET_HAS_IP6_GLOBAL_BIT 0

ESP_EVENT_DECLARE_BASE(ARDUINO_EVENTS);
Expand Down
29 changes: 29 additions & 0 deletions libraries/Network/src/NetworkInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,35 @@ IPAddress NetworkInterface::dnsIP(uint8_t dns_no) const
if(esp_netif_get_dns_info(_esp_netif, dns_no?ESP_NETIF_DNS_BACKUP:ESP_NETIF_DNS_MAIN, &d) != ESP_OK){
return IPAddress();
}
if (d.ip.type == ESP_IPADDR_TYPE_V6){
// IPv6 from 4x uint32_t; byte order based on IPV62STR() in esp_netif_ip_addr.h
log_d("DNS got IPv6: " IPV6STR, IPV62STR(d.ip.u_addr.ip6));
uint32_t addr0 esp_netif_htonl(d.ip.u_addr.ip6.addr[0]);
uint32_t addr1 esp_netif_htonl(d.ip.u_addr.ip6.addr[1]);
uint32_t addr2 esp_netif_htonl(d.ip.u_addr.ip6.addr[2]);
uint32_t addr3 esp_netif_htonl(d.ip.u_addr.ip6.addr[3]);
return IPAddress(
(uint8_t)(addr0 >> 24) & 0xFF,
(uint8_t)(addr0 >> 16) & 0xFF,
(uint8_t)(addr0 >> 8) & 0xFF,
(uint8_t)addr0 & 0xFF,
(uint8_t)(addr1 >> 24) & 0xFF,
(uint8_t)(addr1 >> 16) & 0xFF,
(uint8_t)(addr1 >> 8) & 0xFF,
(uint8_t)addr1 & 0xFF,
(uint8_t)(addr2 >> 24) & 0xFF,
(uint8_t)(addr2 >> 16) & 0xFF,
(uint8_t)(addr2 >> 8) & 0xFF,
(uint8_t)addr2 & 0xFF,
(uint8_t)(addr3 >> 24) & 0xFF,
(uint8_t)(addr3 >> 16) & 0xFF,
(uint8_t)(addr3 >> 8) & 0xFF,
(uint8_t)addr3 & 0xFF,
d.ip.u_addr.ip6.zone
);
}
// IPv4 from single uint32_t
log_d("DNS IPv4: " IPSTR, IP2STR(&d.ip.u_addr.ip4));
return IPAddress(d.ip.u_addr.ip4.addr);
}

Expand Down
99 changes: 35 additions & 64 deletions libraries/Network/src/NetworkManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/
#include "NetworkManager.h"
#include "NetworkInterface.h"
#include "IPAddress.h"
#include "esp_netif.h"
#include "lwip/ip_addr.h"
#include "lwip/dns.h"
#include "esp32-hal-log.h"
#include "esp_mac.h"
#include "netdb.h"

NetworkManager::NetworkManager(){

Expand All @@ -36,54 +36,16 @@ bool NetworkManager::begin(){
return initialized;
}

typedef struct gethostbynameParameters {
const char *hostname;
ip_addr_t addr;
uint8_t addr_type;
int result;
} gethostbynameParameters_t;

/**
* DNS callback
* @param name
* @param ipaddr
* @param callback_arg
*/
static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
{
gethostbynameParameters_t *parameters = static_cast<gethostbynameParameters_t *>(callback_arg);
if(ipaddr) {
if(parameters->result == 0){
memcpy(&(parameters->addr), ipaddr, sizeof(ip_addr_t));
parameters->result = 1;
}
} else {
parameters->result = -1;
}
Network.setStatusBits(NET_DNS_DONE_BIT);
}

/**
* Callback to execute dns_gethostbyname in lwIP's TCP/IP context
* @param param Parameters for dns_gethostbyname call
*/
static esp_err_t wifi_gethostbyname_tcpip_ctx(void *param)
{
gethostbynameParameters_t *parameters = static_cast<gethostbynameParameters_t *>(param);
return dns_gethostbyname_addrtype(parameters->hostname, &parameters->addr, &wifi_dns_found_callback, parameters, parameters->addr_type);
}

/**
* Resolve the given hostname to an IP address.
* @param aHostname Name to be resolved
* @param aResult IPAddress structure to store the returned IP address
* @return 1 if aIPAddrString was successfully converted to an IP address,
* else error code
*/
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult)
{
err_t err = ERR_OK;
gethostbynameParameters_t params;

// This should generally check if we have a global address assigned to one of the interfaces.
// 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.
Expand All @@ -97,32 +59,41 @@ int NetworkManager::hostByName(const char* aHostname, IPAddress& aResult, bool p
}

aResult = static_cast<uint32_t>(0);
params.hostname = aHostname;
params.addr_type = (preferV6 || hasGlobalV6)?LWIP_DNS_ADDRTYPE_IPV6_IPV4:LWIP_DNS_ADDRTYPE_IPV4;
params.result = 0;
aResult.to_ip_addr_t(&(params.addr));

if (!aResult.fromString(aHostname)) {
Network.waitStatusBits(NET_DNS_IDLE_BIT, 16000);
Network.clearStatusBits(NET_DNS_IDLE_BIT | NET_DNS_DONE_BIT);

err = esp_netif_tcpip_exec(wifi_gethostbyname_tcpip_ctx, &params);
if (err == ERR_OK) {
aResult.from_ip_addr_t(&(params.addr));
} else if (err == ERR_INPROGRESS) {
Network.waitStatusBits(NET_DNS_DONE_BIT, 15000); //real internal timeout in lwip library is 14[s]
Network.clearStatusBits(NET_DNS_DONE_BIT);
if (params.result == 1) {
aResult.from_ip_addr_t(&(params.addr));
err = ERR_OK;
}
}
Network.setStatusBits(NET_DNS_IDLE_BIT);

// First check if the host parses as a literal address
if (aResult.fromString(aHostname)) {
return 1;
}
if (err == ERR_OK) {

const char *servname = "0";
struct addrinfo *res;
const struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
};
err = lwip_getaddrinfo(aHostname, servname, &hints, &res);
if (err == ERR_OK)
{
if (res->ai_family == AF_INET6)
{
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)res->ai_addr;
// As an array of u8_t
aResult = IPAddress(IPv6, ipv6->sin6_addr.s6_addr);
log_d("DNS found IPv6 %s", aResult.toString().c_str());
}
else
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
// As a single u32_t
aResult = IPAddress(ipv4->sin_addr.s_addr);
log_d("DNS found IPv4 %s", aResult.toString().c_str());
}

lwip_freeaddrinfo(res);
return 1;
}
log_e("DNS Failed for '%s' with error '%d' and result '%d'", aHostname, err, params.result);

log_e("DNS Failed for '%s' with error '%d'", aHostname, err);
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/Network/src/NetworkManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NetworkManager : public NetworkEvents, public Printable {
NetworkManager();

bool begin();
int hostByName(const char *aHostname, IPAddress &aResult, bool preferV6=false);
int hostByName(const char *aHostname, IPAddress &aResult);
uint8_t * macAddress(uint8_t * mac);
String macAddress();

Expand Down
4 changes: 2 additions & 2 deletions libraries/WiFi/src/WiFiGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,9 +794,9 @@ bool WiFiGenericClass::setDualAntennaConfig(uint8_t gpio_ant1, uint8_t gpio_ant2
/*
* Deprecated Methods
*/
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, bool preferV6)
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
{
return Network.hostByName(aHostname, aResult, preferV6);
return Network.hostByName(aHostname, aResult);
}

IPAddress WiFiGenericClass::calculateNetworkID(IPAddress ip, IPAddress subnet) {
Expand Down
2 changes: 1 addition & 1 deletion libraries/WiFi/src/WiFiGeneric.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class WiFiGenericClass
static void useStaticBuffers(bool bufferMode);
static bool useStaticBuffers();

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

static IPAddress calculateNetworkID(IPAddress ip, IPAddress subnet);
static IPAddress calculateBroadcast(IPAddress ip, IPAddress subnet);
Expand Down

0 comments on commit 64235dc

Please sign in to comment.