70 lines
1.5 KiB
C++
70 lines
1.5 KiB
C++
#ifndef DNS_H
|
|
#define DNS_H
|
|
|
|
#include "../Platforms.h"
|
|
#include "../Debug.h"
|
|
|
|
typedef void (*DNSResolveCallback)(const char* name, const ip_addr_t* ip, void* arg);
|
|
|
|
#if ESP8266
|
|
class DNS {
|
|
|
|
static constexpr const char* NAME = "DNS";
|
|
|
|
espconn dns;
|
|
ip_addr_t ip;
|
|
|
|
DNSResolveCallback callback;
|
|
void* arg;
|
|
|
|
public:
|
|
|
|
/** resolve hostname. creates callback */
|
|
void resolveHost(const char* c, DNSResolveCallback callback, void* arg) {
|
|
debugMod1(NAME, "dns lookup: %s", c);
|
|
this->callback = callback;
|
|
this->arg = arg;
|
|
dns.reverse = (void*) this;
|
|
espconn_gethostbyname(&dns, c, &ip, _onHostResolved);
|
|
}
|
|
|
|
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg) {
|
|
debugMod1(NAME, "dns resolved: %s", c);
|
|
DNS* dns = (DNS*) arg;
|
|
dns->callback(name, ipaddr, dns->arg);
|
|
}
|
|
|
|
};
|
|
#elif ESP32
|
|
|
|
#include <lwip/dns.h>
|
|
|
|
class DNS {
|
|
|
|
static constexpr const char* NAME = "DNS";
|
|
ip_addr_t ip;
|
|
DNSResolveCallback callback;
|
|
void* arg;
|
|
|
|
public:
|
|
|
|
/** resolve hostname. creates callback */
|
|
void resolveHost(const char* host, DNSResolveCallback callback, void* arg) {
|
|
debugMod1(NAME, "dns lookup: %s", host);
|
|
this->callback = callback;
|
|
this->arg = arg;
|
|
dns_gethostbyname(host, &ip, _onHostResolved, this);
|
|
}
|
|
|
|
static void _onHostResolved(const char* name, const ip_addr_t* ipaddr, void* arg) {
|
|
debugMod1(NAME, "dns resolved: %s", name);
|
|
DNS* dns = (DNS*) arg;
|
|
dns->callback(name, ipaddr, dns->arg);
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
#endif // DNS_H
|