14#if DETWS_ENABLE_DNS_RESOLVER
16DetwsIpClass detws_dns_classify(uint32_t ip)
19 return DetwsIpClass::DETWS_IP_UNSPECIFIED;
20 if (ip == 0xFFFFFFFFu)
21 return DetwsIpClass::DETWS_IP_BROADCAST;
22 uint8_t a = (uint8_t)((ip >> 24) & 0xFF);
23 uint8_t b = (uint8_t)((ip >> 16) & 0xFF);
25 return DetwsIpClass::DETWS_IP_LOOPBACK;
27 return DetwsIpClass::DETWS_IP_PRIVATE;
28 if (a == 172 && b >= 16 && b <= 31)
29 return DetwsIpClass::DETWS_IP_PRIVATE;
30 if (a == 192 && b == 168)
31 return DetwsIpClass::DETWS_IP_PRIVATE;
32 if (a == 169 && b == 254)
33 return DetwsIpClass::DETWS_IP_LINKLOCAL;
34 if (a >= 224 && a <= 239)
35 return DetwsIpClass::DETWS_IP_MULTICAST;
36 return DetwsIpClass::DETWS_IP_PUBLIC;
39bool detws_dns_verify(uint32_t ip)
41 switch (detws_dns_classify(ip))
43 case DetwsIpClass::DETWS_IP_UNSPECIFIED:
44 case DetwsIpClass::DETWS_IP_BROADCAST:
45 case DetwsIpClass::DETWS_IP_LOOPBACK:
46 case DetwsIpClass::DETWS_IP_MULTICAST:
57#include "lwip/ip_addr.h"
58#include "lwip/priv/tcpip_priv.h"
71 volatile bool done =
false;
72 volatile bool ok =
false;
78 struct tcpip_api_call_data base;
82void dns_cb(
const char *name,
const ip_addr_t *addr,
void *arg)
94err_t do_dns(
struct tcpip_api_call_data *c)
96 const char *host = ((DnsCall *)c)->host;
97 err_t e = dns_gethostbyname(host, &s_dr.addr, dns_cb,
nullptr);
103 else if (e != ERR_INPROGRESS)
108uint32_t to_host_order(
const ip_addr_t *a)
110 return lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(a)));
114bool detws_dns_resolve(
const char *host, uint32_t *out_ip)
116 if (!host || !out_ip)
120 if (ipaddr_aton(host, &literal))
122 *out_ip = to_host_order(&literal);
129 memset(&k, 0,
sizeof(k));
131 tcpip_api_call(do_dns, &k.base);
134 while (!s_dr.done && (int32_t)(deadline -
detws_millis()) > 0)
139 *out_ip = to_host_order(&s_dr.addr);
155DnsTestCtx s_dns_test;
158void detws_dns_test_set_resolve(
bool ok, uint32_t ip)
163bool detws_dns_resolve(
const char *, uint32_t *out_ip)
168 *out_ip = s_dns_test.ip;
174bool detws_dns_resolve_verified(
const char *host, uint32_t *out_ip)
177 if (!detws_dns_resolve(host, &ip))
179 if (!detws_dns_verify(ip))
#define DETWS_DNS_TIMEOUT_MS
DNS resolve timeout in milliseconds.
Pluggable monotonic clock for all library timing.
uint32_t detws_millis(void)
The library's monotonic time at 1000 Hz (milliseconds).
void dwsdelay(uint32_t ms)
Block for at least ms milliseconds - the library's single delay primitive.
DNS resolver with answer verification (DETWS_ENABLE_DNS_RESOLVER).