mirror of https://github.com/ARMmbed/mbed-os.git
nsapi_dns: Convert to Chrono
parent
d1ae0d570c
commit
08fa26bdda
|
|
@ -31,6 +31,9 @@
|
||||||
#include "PlatformMutex.h"
|
#include "PlatformMutex.h"
|
||||||
#include "SingletonPtr.h"
|
#include "SingletonPtr.h"
|
||||||
|
|
||||||
|
using namespace std::chrono;
|
||||||
|
using rtos::Kernel::Clock;
|
||||||
|
|
||||||
#define CLASS_IN 1
|
#define CLASS_IN 1
|
||||||
|
|
||||||
#define RR_A 1
|
#define RR_A 1
|
||||||
|
|
@ -51,9 +54,9 @@
|
||||||
struct DNS_CACHE {
|
struct DNS_CACHE {
|
||||||
nsapi_addr_t *address;
|
nsapi_addr_t *address;
|
||||||
char *host;
|
char *host;
|
||||||
uint64_t expires; /*!< time to live in milliseconds */
|
Clock::time_point expires; /*!< time to live in milliseconds */
|
||||||
uint64_t accessed; /*!< last accessed */
|
Clock::time_point accessed; /*!< last accessed */
|
||||||
uint8_t count; /*!< number of IP addresses */
|
uint8_t count; /*!< number of IP addresses */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SOCKET_CB_DATA {
|
struct SOCKET_CB_DATA {
|
||||||
|
|
@ -80,7 +83,7 @@ struct DNS_QUERY {
|
||||||
UDPSocket *socket;
|
UDPSocket *socket;
|
||||||
SOCKET_CB_DATA *socket_cb_data;
|
SOCKET_CB_DATA *socket_cb_data;
|
||||||
nsapi_addr_t *addrs;
|
nsapi_addr_t *addrs;
|
||||||
uint32_t ttl;
|
duration<uint32_t> ttl;
|
||||||
uint32_t total_timeout;
|
uint32_t total_timeout;
|
||||||
uint32_t socket_timeout;
|
uint32_t socket_timeout;
|
||||||
uint16_t dns_message_id;
|
uint16_t dns_message_id;
|
||||||
|
|
@ -92,7 +95,7 @@ struct DNS_QUERY {
|
||||||
dns_state state;
|
dns_state state;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count);
|
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, duration<uint32_t> ttl, uint8_t count);
|
||||||
static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address);
|
static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_version_t version, nsapi_addr_t *address);
|
||||||
static void nsapi_dns_cache_reset();
|
static void nsapi_dns_cache_reset();
|
||||||
|
|
||||||
|
|
@ -227,7 +230,7 @@ static int dns_append_question(uint8_t *ptr, uint16_t id, const char *host, nsap
|
||||||
return *p - s_ptr;
|
return *p - s_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl, nsapi_addr_t *addr, unsigned addr_count)
|
static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, duration<uint32_t> *ttl, nsapi_addr_t *addr, unsigned addr_count)
|
||||||
{
|
{
|
||||||
const uint8_t **p = &ptr;
|
const uint8_t **p = &ptr;
|
||||||
|
|
||||||
|
|
@ -293,7 +296,7 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
|
||||||
if (ttl_val > INT32_MAX) {
|
if (ttl_val > INT32_MAX) {
|
||||||
ttl_val = INT32_MAX;
|
ttl_val = INT32_MAX;
|
||||||
}
|
}
|
||||||
*ttl = ttl_val;
|
*ttl = duration<uint32_t>(ttl_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) {
|
if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) {
|
||||||
|
|
@ -323,11 +326,11 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count)
|
static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, duration<uint32_t> ttl, uint8_t count)
|
||||||
{
|
{
|
||||||
#if (MBED_CONF_NSAPI_DNS_CACHE_SIZE > 0)
|
#if (MBED_CONF_NSAPI_DNS_CACHE_SIZE > 0)
|
||||||
// RFC 1034: if TTL is zero, entry is not added to cache
|
// RFC 1034: if TTL is zero, entry is not added to cache
|
||||||
if (ttl == 0) {
|
if (ttl == ttl.zero()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -339,7 +342,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
|
||||||
dns_cache_mutex->lock();
|
dns_cache_mutex->lock();
|
||||||
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
uint64_t accessed = UINT64_MAX;
|
Clock::time_point accessed = Clock::time_point::max();
|
||||||
|
|
||||||
// Finds free or last accessed entry
|
// Finds free or last accessed entry
|
||||||
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
|
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
|
||||||
|
|
@ -373,9 +376,9 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
|
||||||
dns_cache[index]->count = count;
|
dns_cache[index]->count = count;
|
||||||
dns_cache[index]->host = new (std::nothrow) char[strlen(host) + 1];
|
dns_cache[index]->host = new (std::nothrow) char[strlen(host) + 1];
|
||||||
strcpy(dns_cache[index]->host, host);
|
strcpy(dns_cache[index]->host, host);
|
||||||
uint64_t ms_count = rtos::Kernel::get_ms_count();
|
auto now = Clock::now();
|
||||||
dns_cache[index]->expires = ms_count + (uint64_t) ttl * 1000;
|
dns_cache[index]->expires = now + ttl;
|
||||||
dns_cache[index]->accessed = ms_count;
|
dns_cache[index]->accessed = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
dns_cache_mutex->unlock();
|
dns_cache_mutex->unlock();
|
||||||
|
|
@ -391,9 +394,9 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
|
||||||
|
|
||||||
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
|
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
|
||||||
if (dns_cache[i]) {
|
if (dns_cache[i]) {
|
||||||
uint64_t ms_count = rtos::Kernel::get_ms_count();
|
auto now = Clock::now();
|
||||||
// Checks all entries for expired entries
|
// Checks all entries for expired entries
|
||||||
if (ms_count > dns_cache[i]->expires) {
|
if (now > dns_cache[i]->expires) {
|
||||||
delete dns_cache[i]->host;
|
delete dns_cache[i]->host;
|
||||||
delete dns_cache[i];
|
delete dns_cache[i];
|
||||||
dns_cache[i] = NULL;
|
dns_cache[i] = NULL;
|
||||||
|
|
@ -406,7 +409,7 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
|
||||||
ret_val++;
|
ret_val++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dns_cache[i]->accessed = ms_count;
|
dns_cache[i]->accessed = now;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -566,7 +569,7 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint8_t *response = packet;
|
const uint8_t *response = packet;
|
||||||
uint32_t ttl;
|
duration<uint32_t> ttl;
|
||||||
int resp = dns_scan_response(response, 1, &ttl, addr, addr_count);
|
int resp = dns_scan_response(response, 1, &ttl, addr, addr_count);
|
||||||
if (resp > 0) {
|
if (resp > 0) {
|
||||||
nsapi_dns_cache_add(host, addr, ttl, resp);
|
nsapi_dns_cache_add(host, addr, ttl, resp);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue