31
31
#include " PlatformMutex.h"
32
32
#include " SingletonPtr.h"
33
33
34
+ using namespace std ::chrono;
35
+ using rtos::Kernel::Clock;
36
+
34
37
#define CLASS_IN 1
35
38
36
39
#define RR_A 1
51
54
struct DNS_CACHE {
52
55
nsapi_addr_t *address;
53
56
char *host;
54
- uint64_t expires; /* !< time to live in milliseconds */
55
- uint64_t accessed; /* !< last accessed */
56
- uint8_t count; /* !< number of IP addresses */
57
+ Clock::time_point expires; /* !< time to live in milliseconds */
58
+ Clock::time_point accessed; /* !< last accessed */
59
+ uint8_t count; /* !< number of IP addresses */
57
60
};
58
61
59
62
struct SOCKET_CB_DATA {
@@ -80,7 +83,7 @@ struct DNS_QUERY {
80
83
UDPSocket *socket;
81
84
SOCKET_CB_DATA *socket_cb_data;
82
85
nsapi_addr_t *addrs;
83
- uint32_t ttl;
86
+ duration< uint32_t > ttl;
84
87
uint32_t total_timeout;
85
88
uint32_t socket_timeout;
86
89
uint16_t dns_message_id;
@@ -92,7 +95,7 @@ struct DNS_QUERY {
92
95
dns_state state;
93
96
};
94
97
95
- static void nsapi_dns_cache_add (const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count);
98
+ static void nsapi_dns_cache_add (const char *host, nsapi_addr_t *address, duration< uint32_t > ttl, uint8_t count);
96
99
static nsapi_size_or_error_t nsapi_dns_cache_find (const char *host, nsapi_version_t version, nsapi_addr_t *address);
97
100
static void nsapi_dns_cache_reset ();
98
101
@@ -227,7 +230,7 @@ static int dns_append_question(uint8_t *ptr, uint16_t id, const char *host, nsap
227
230
return *p - s_ptr;
228
231
}
229
232
230
- static int dns_scan_response (const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl, nsapi_addr_t *addr, unsigned addr_count)
233
+ static int dns_scan_response (const uint8_t *ptr, uint16_t exp_id, duration< uint32_t > *ttl, nsapi_addr_t *addr, unsigned addr_count)
231
234
{
232
235
const uint8_t **p = &ptr;
233
236
@@ -293,7 +296,7 @@ static int dns_scan_response(const uint8_t *ptr, uint16_t exp_id, uint32_t *ttl,
293
296
if (ttl_val > INT32_MAX) {
294
297
ttl_val = INT32_MAX;
295
298
}
296
- *ttl = ttl_val;
299
+ *ttl = duration< uint32_t >( ttl_val) ;
297
300
}
298
301
299
302
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,
323
326
return count;
324
327
}
325
328
326
- static void nsapi_dns_cache_add (const char *host, nsapi_addr_t *address, uint32_t ttl, uint8_t count)
329
+ static void nsapi_dns_cache_add (const char *host, nsapi_addr_t *address, duration< uint32_t > ttl, uint8_t count)
327
330
{
328
331
#if (MBED_CONF_NSAPI_DNS_CACHE_SIZE > 0)
329
332
// RFC 1034: if TTL is zero, entry is not added to cache
330
- if (ttl == 0 ) {
333
+ if (ttl == ttl. zero () ) {
331
334
return ;
332
335
}
333
336
@@ -339,7 +342,7 @@ static void nsapi_dns_cache_add(const char *host, nsapi_addr_t *address, uint32_
339
342
dns_cache_mutex->lock ();
340
343
341
344
int index = -1 ;
342
- uint64_t accessed = UINT64_MAX ;
345
+ Clock::time_point accessed = Clock::time_point::max () ;
343
346
344
347
// Finds free or last accessed entry
345
348
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_
373
376
dns_cache[index]->count = count;
374
377
dns_cache[index]->host = new (std::nothrow) char [strlen (host) + 1 ];
375
378
strcpy (dns_cache[index]->host , host);
376
- uint64_t ms_count = rtos::Kernel::get_ms_count ();
377
- dns_cache[index]->expires = ms_count + ( uint64_t ) ttl * 1000 ;
378
- dns_cache[index]->accessed = ms_count ;
379
+ auto now = Clock::now ();
380
+ dns_cache[index]->expires = now + ttl;
381
+ dns_cache[index]->accessed = now ;
379
382
}
380
383
381
384
dns_cache_mutex->unlock ();
@@ -391,9 +394,9 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
391
394
392
395
for (int i = 0 ; i < MBED_CONF_NSAPI_DNS_CACHE_SIZE; i++) {
393
396
if (dns_cache[i]) {
394
- uint64_t ms_count = rtos::Kernel::get_ms_count ();
397
+ auto now = Clock::now ();
395
398
// Checks all entries for expired entries
396
- if (ms_count > dns_cache[i]->expires ) {
399
+ if (now > dns_cache[i]->expires ) {
397
400
delete dns_cache[i]->host ;
398
401
delete dns_cache[i];
399
402
dns_cache[i] = NULL ;
@@ -406,7 +409,7 @@ static nsapi_size_or_error_t nsapi_dns_cache_find(const char *host, nsapi_versio
406
409
ret_val++;
407
410
}
408
411
}
409
- dns_cache[i]->accessed = ms_count ;
412
+ dns_cache[i]->accessed = now ;
410
413
411
414
}
412
415
}
@@ -566,7 +569,7 @@ static nsapi_size_or_error_t nsapi_dns_query_multiple(NetworkStack *stack, const
566
569
}
567
570
568
571
const uint8_t *response = packet;
569
- uint32_t ttl;
572
+ duration< uint32_t > ttl;
570
573
int resp = dns_scan_response (response, 1 , &ttl, addr, addr_count);
571
574
if (resp > 0 ) {
572
575
nsapi_dns_cache_add (host, addr, ttl, resp);
0 commit comments