Skip to content

Commit 4d51978

Browse files
author
altelch
committed
Make DNS resolution order selectable during runtime or compile time (only in dual stack mode).
1 parent cc6d346 commit 4d51978

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,47 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
617617
}
618618

619619
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
620+
#if LWIP_IPV4 && LWIP_IPV6
621+
err_t err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult,LWIP_DNS_ADDRTYPE_DEFAULT);
622+
#else
620623
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
624+
#endif
625+
if(err == ERR_OK) {
626+
aResult = IPAddress(&addr);
627+
} else if(err == ERR_INPROGRESS) {
628+
_dns_lookup_pending = true;
629+
delay(timeout_ms);
630+
// will resume on timeout or when wifi_dns_found_callback fires
631+
_dns_lookup_pending = false;
632+
// will return here when dns_found_callback fires
633+
if(aResult.isSet()) {
634+
err = ERR_OK;
635+
}
636+
}
637+
638+
if(err != 0) {
639+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err);
640+
} else {
641+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str());
642+
}
643+
644+
return (err == ERR_OK) ? 1 : 0;
645+
}
646+
647+
#if LWIP_IPV4 && LWIP_IPV6
648+
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms, uint8_t resolveType)
649+
{
650+
ip_addr_t addr;
651+
aResult = static_cast<uint32_t>(0);
652+
653+
if(aResult.fromString(aHostname)) {
654+
// Host name is a IP address use it!
655+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname);
656+
return 1;
657+
}
658+
659+
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
660+
err_t err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult,resolveType & 3); // limit to defined types 0 -3
621661
if(err == ERR_OK) {
622662
aResult = IPAddress(&addr);
623663
} else if(err == ERR_INPROGRESS) {
@@ -639,6 +679,7 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
639679

640680
return (err == ERR_OK) ? 1 : 0;
641681
}
682+
#endif
642683

643684
/**
644685
* DNS callback

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ class ESP8266WiFiGenericClass {
113113
public:
114114
int hostByName(const char* aHostname, IPAddress& aResult);
115115
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms);
116+
#if LWIP_IPV4 && LWIP_IPV6
117+
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms, uint8_t resolveType);
118+
#endif
116119
bool getPersistent();
117120

118121
protected:

0 commit comments

Comments
 (0)