mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-13 02:22:55 +03:00
Make DNS resolution order selectable during runtime or compile time (#6865)
* Make DNS resolution order selectable during runtime or compile time (only in dual stack mode). * Extend IPv6 example to show usage of new hostByName function with selectable resolving order * Fix function definition of fqdn_rt in IPv6.ino. * Fix function call. * Fix missing bracket... * Only run if built with dual stack support * Make DNS resolution order selectable during runtime or compile time (only in dual stack mode). * Extend IPv6 example to show usage of new hostByName function with selectable resolving order * Fix function definition of fqdn_rt in IPv6 example. * Implement enum class for resolve type * Fix example IPv6.ino * Fix typedef in ESP8266WiFiGeneric.h function call * Change enum class definition to not depend on lwip/dns.h * Move err_t err definition outside switch. Fix typecast. * Always define DNSResolveType as pio test otherwise failes even if the enum class isn't used. Co-authored-by: altelch <heiko.krupp@rhrk.uni-kl.de> Co-authored-by: david gauchard <gauchard@laas.fr>
This commit is contained in:
@ -608,7 +608,7 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
|
||||
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms)
|
||||
{
|
||||
ip_addr_t addr;
|
||||
aResult = static_cast<uint32_t>(0);
|
||||
aResult = static_cast<uint32_t>(INADDR_NONE);
|
||||
|
||||
if(aResult.fromString(aHostname)) {
|
||||
// Host name is a IP address use it!
|
||||
@ -617,7 +617,11 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
|
||||
}
|
||||
|
||||
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
err_t err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult,LWIP_DNS_ADDRTYPE_DEFAULT);
|
||||
#else
|
||||
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
|
||||
#endif
|
||||
if(err == ERR_OK) {
|
||||
aResult = IPAddress(&addr);
|
||||
} else if(err == ERR_INPROGRESS) {
|
||||
@ -640,6 +644,57 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
|
||||
return (err == ERR_OK) ? 1 : 0;
|
||||
}
|
||||
|
||||
#if LWIP_IPV4 && LWIP_IPV6
|
||||
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms, DNSResolveType resolveType)
|
||||
{
|
||||
ip_addr_t addr;
|
||||
err_t err;
|
||||
aResult = static_cast<uint32_t>(INADDR_NONE);
|
||||
|
||||
if(aResult.fromString(aHostname)) {
|
||||
// Host name is a IP address use it!
|
||||
DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname);
|
||||
return 1;
|
||||
}
|
||||
|
||||
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
|
||||
switch(resolveType)
|
||||
{
|
||||
// Use selected addrtype
|
||||
case DNSResolveType::DNS_AddrType_IPv4:
|
||||
case DNSResolveType::DNS_AddrType_IPv6:
|
||||
case DNSResolveType::DNS_AddrType_IPv4_IPv6:
|
||||
case DNSResolveType::DNS_AddrType_IPv6_IPv4:
|
||||
err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult, (uint8_t) resolveType);
|
||||
break;
|
||||
default:
|
||||
err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult, LWIP_DNS_ADDRTYPE_DEFAULT); // If illegal type, use default.
|
||||
break;
|
||||
}
|
||||
|
||||
if(err == ERR_OK) {
|
||||
aResult = IPAddress(&addr);
|
||||
} else if(err == ERR_INPROGRESS) {
|
||||
_dns_lookup_pending = true;
|
||||
delay(timeout_ms);
|
||||
// will resume on timeout or when wifi_dns_found_callback fires
|
||||
_dns_lookup_pending = false;
|
||||
// will return here when dns_found_callback fires
|
||||
if(aResult.isSet()) {
|
||||
err = ERR_OK;
|
||||
}
|
||||
}
|
||||
|
||||
if(err != 0) {
|
||||
DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err);
|
||||
} else {
|
||||
DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str());
|
||||
}
|
||||
|
||||
return (err == ERR_OK) ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* DNS callback
|
||||
* @param name
|
||||
|
Reference in New Issue
Block a user