From 3e9dede14ef980880387d504512a338d52051e69 Mon Sep 17 00:00:00 2001 From: Markus Sattler Date: Thu, 18 Feb 2016 23:00:25 +0100 Subject: [PATCH] speed up WiFi.hostByName when the hostname is actually a IP. --- .../ESP8266WiFi/src/ESP8266WiFiGeneric.cpp | 19 ++++++++++++++++++- .../ESP8266WiFi/src/ESP8266WiFiGeneric.h | 10 ++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 0a06fce85a..c73ec9385d 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -316,15 +316,32 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) { ip_addr_t addr; aResult = static_cast(0); + + 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); err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult); if(err == ERR_OK) { aResult = addr.addr; } else if(err == ERR_INPROGRESS) { esp_yield(); // will return here when dns_found_callback fires + if(aResult != 0) { + err = ERR_OK; + } + } + + if(err != 0) { + DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, err); + } else { + DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); } - return (aResult != 0) ? 1 : 0; + return (err == ERR_OK) ? 1 : 0; } /** diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h index 80a7c946fc..7519605231 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h @@ -25,6 +25,16 @@ #include "ESP8266WiFiType.h" +#ifdef DEBUG_ESP_WIFI +#ifdef DEBUG_ESP_PORT +#define DEBUG_WIFI_GENERIC(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ ) +#endif +#endif + +#ifndef DEBUG_WIFI_GENERIC +#define DEBUG_WIFI_GENERIC(...) +#endif + typedef void (*WiFiEventCb)(WiFiEvent_t event); typedef struct {