many changes
This commit is contained in:
69
net/DNS.h
Normal file
69
net/DNS.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user