Skip to content

nsapi_dns: Convert to Chrono #12428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UNITTESTS/stubs/Kernel_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
namespace rtos {

uint64_t Kernel::get_ms_count()
{
return impl::get_tick_count();

}
uint64_t Kernel::impl::get_tick_count()
{
return 20;
}
Expand Down
37 changes: 20 additions & 17 deletions features/netsocket/nsapi_dns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "PlatformMutex.h"
#include "SingletonPtr.h"

using namespace std::chrono;
using rtos::Kernel::Clock;
Copy link
Contributor

@michalpasztamobica michalpasztamobica Feb 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary? (I mean using rtos::Kernel::Clock;)

Copy link
Contributor Author

@kjbracey kjbracey Feb 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but it saves typing rtos::Kernel:: a fair few times in the file. Could just go halfway with the more conventional using namespace rtos.


#define CLASS_IN 1

#define RR_A 1
Expand All @@ -51,9 +54,9 @@
struct DNS_CACHE {
nsapi_addr_t *address;
char *host;
uint64_t expires; /*!< time to live in milliseconds */
uint64_t accessed; /*!< last accessed */
uint8_t count; /*!< number of IP addresses */
Clock::time_point expires; /*!< time to live in milliseconds */
Clock::time_point accessed; /*!< last accessed */
uint8_t count; /*!< number of IP addresses */
};

struct SOCKET_CB_DATA {
Expand All @@ -80,7 +83,7 @@ struct DNS_QUERY {
UDPSocket *socket;
SOCKET_CB_DATA *socket_cb_data;
nsapi_addr_t *addrs;
uint32_t ttl;
duration<uint32_t> ttl;
uint32_t total_timeout;
uint32_t socket_timeout;
uint16_t dns_message_id;
Expand All @@ -92,7 +95,7 @@ struct DNS_QUERY {
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 void nsapi_dns_cache_reset();

Expand Down Expand Up @@ -227,7 +230,7 @@ static int dns_append_question(uint8_t *ptr, uint16_t id, const char *host, nsap
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;

Expand Down Expand Up @@ -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) {
ttl_val = INT32_MAX;
}
*ttl = ttl_val;
*ttl = duration<uint32_t>(ttl_val);
}

if (rtype == RR_A && rclass == CLASS_IN && rdlength == NSAPI_IPv4_BYTES) {
Expand Down Expand Up @@ -323,11 +326,11 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
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)
// RFC 1034: if TTL is zero, entry is not added to cache
if (ttl == 0) {
if (ttl == ttl.zero()) {
return;
}

Expand All @@ -339,7 +342,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
dns_cache_mutex->lock();

int index = -1;
uint64_t accessed = UINT64_MAX;
Clock::time_point accessed = Clock::time_point::max();

// Finds free or last accessed entry
for (int i = 0; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
Expand Down Expand Up @@ -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]->host = new (std::nothrow) char[strlen(host) + 1];
strcpy(dns_cache[index]->host, host);
uint64_t ms_count = rtos::Kernel::get_ms_count();
dns_cache[index]->expires = ms_count + (uint64_t) ttl * 1000;
dns_cache[index]->accessed = ms_count;
auto now = Clock::now();
dns_cache[index]->expires = now + ttl;
dns_cache[index]->accessed = now;
}

dns_cache_mutex->unlock();
Expand All @@ -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++) {
if (dns_cache[i]) {
uint64_t ms_count = rtos::Kernel::get_ms_count();
auto now = Clock::now();
// 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];
dns_cache[i] = NULL;
Expand All @@ -406,7 +409,7 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
ret_val++;
}
}
dns_cache[i]->accessed = ms_count;
dns_cache[i]->accessed = now;

}
}
Expand Down Expand Up @@ -566,7 +569,7 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
}

const uint8_t *response = packet;
uint32_t ttl;
duration<uint32_t> ttl;
int resp = dns_scan_response(response, 1, &ttl, addr, addr_count);
if (resp > 0) {
nsapi_dns_cache_add(host, addr, ttl, resp);
Expand Down