Skip to content

Commit 212095b

Browse files
committed
Merge pull request #1650 from Links2004/master
speed up WiFi.hostByName when the hostname is actually a IP.
2 parents 103b581 + 3e9dede commit 212095b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,32 @@ void wifi_dns_found_callback(const char *name, ip_addr_t *ipaddr, void *callback
316316
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult) {
317317
ip_addr_t addr;
318318
aResult = static_cast<uint32_t>(0);
319+
320+
if(aResult.fromString(aHostname)) {
321+
// Host name is a IP address use it!
322+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname);
323+
return 1;
324+
}
325+
326+
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
319327
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
320328
if(err == ERR_OK) {
321329
aResult = addr.addr;
322330
} else if(err == ERR_INPROGRESS) {
323331
esp_yield();
324332
// will return here when dns_found_callback fires
333+
if(aResult != 0) {
334+
err = ERR_OK;
335+
}
336+
}
337+
338+
if(err != 0) {
339+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, err);
340+
} else {
341+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str());
325342
}
326343

327-
return (aResult != 0) ? 1 : 0;
344+
return (err == ERR_OK) ? 1 : 0;
328345
}
329346

330347
/**

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525

2626
#include "ESP8266WiFiType.h"
2727

28+
#ifdef DEBUG_ESP_WIFI
29+
#ifdef DEBUG_ESP_PORT
30+
#define DEBUG_WIFI_GENERIC(...) DEBUG_ESP_PORT.printf( __VA_ARGS__ )
31+
#endif
32+
#endif
33+
34+
#ifndef DEBUG_WIFI_GENERIC
35+
#define DEBUG_WIFI_GENERIC(...)
36+
#endif
37+
2838
typedef void (*WiFiEventCb)(WiFiEvent_t event);
2939

3040
typedef struct {

0 commit comments

Comments
 (0)