Skip to content

Commit 34a891b

Browse files
committed
Moved clock handling into dtls-support
1 parent d7157bb commit 34a891b

File tree

10 files changed

+46
-94
lines changed

10 files changed

+46
-94
lines changed

Diff for: contiki-support/dtls-support-conf.h

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ typedef struct {
1818
int ifindex;
1919
} session_t;
2020

21+
#define DTLS_TICKS_PER_SECOND CLOCK_SECOND
22+
23+
typedef clock_time_t dtls_tick_t;
24+
2125
#define WITH_CONTIKI 1
2226

2327
#define HAVE_ASSERT_H 1

Diff for: contiki-support/dtls-support.c

-5
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s
183183

184184
/* --------- time support --------- */
185185

186-
void
187-
dtls_clock_init(void)
188-
{
189-
}
190-
191186
void
192187
dtls_ticks(dtls_tick_t *t)
193188
{

Diff for: dtls-support.h

+8
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,13 @@ void dtls_session_print(const session_t *a);
5151
*/
5252
void dtls_session_snprint(const session_t *a, char *buf, size_t size);
5353

54+
/**
55+
* Clock handling
56+
*/
57+
void dtls_ticks(dtls_tick_t *t);
58+
59+
#ifndef DTLS_TICKS_PER_SECOND
60+
#error DTLS_TICKS_PER_SECOND is not defined
61+
#endif /* DTLS_TICKS_PER_SECOND */
5462

5563
#endif /* DTLS_SUPPORT_H_ */

Diff for: dtls.c

+6-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*******************************************************************************/
1919

2020
#include "tinydtls.h"
21-
#include "dtls_time.h"
2221

2322
#include <stdio.h>
2423
#include <stdlib.h>
@@ -33,7 +32,6 @@
3332
#include "dtls-numeric.h"
3433
#include "netq.h"
3534
#include "dtls.h"
36-
#include "dtls-hmac.h"
3735

3836
#include "dtls-alert.h"
3937
#include "dtls-prng.h"
@@ -149,12 +147,11 @@ static const unsigned char cert_asn1_header[] = {
149147

150148
void
151149
dtls_init() {
152-
dtls_clock_init();
150+
dtls_support_init();
153151
dtls_crypto_init();
154152
dtls_hmac_storage_init();
155153
netq_init();
156154
dtls_peer_init();
157-
dtls_support_init();
158155
dtls_hmac_storage_init();
159156
}
160157

@@ -1464,9 +1461,9 @@ dtls_send_multi(dtls_context_t *ctx, dtls_peer_t *peer,
14641461
if (n) {
14651462
dtls_tick_t now;
14661463
dtls_ticks(&now);
1467-
n->t = now + 2 * CLOCK_SECOND;
1464+
n->t = now + 2 * DTLS_TICKS_PER_SECOND;
14681465
n->retransmit_cnt = 0;
1469-
n->timeout = 2 * CLOCK_SECOND;
1466+
n->timeout = 2 * DTLS_TICKS_PER_SECOND;
14701467
n->peer = peer;
14711468
n->epoch = (security) ? security->epoch : 0;
14721469
n->type = type;
@@ -1774,7 +1771,7 @@ dtls_send_server_hello(dtls_context_t *ctx, dtls_peer_t *peer)
17741771
/* Set server random: First 4 bytes are the server's Unix timestamp,
17751772
* followed by 28 bytes of generate random data. */
17761773
dtls_ticks(&now);
1777-
dtls_int_to_uint32(handshake->tmp.random.server, now / CLOCK_SECOND);
1774+
dtls_int_to_uint32(handshake->tmp.random.server, now / DTLS_TICKS_PER_SECOND);
17781775
dtls_prng(handshake->tmp.random.server + 4, 28);
17791776

17801777
memcpy(p, handshake->tmp.random.server, DTLS_RANDOM_LENGTH);
@@ -2353,7 +2350,7 @@ dtls_send_client_hello(dtls_context_t *ctx, dtls_peer_t *peer,
23532350
/* Set client random: First 4 bytes are the client's Unix timestamp,
23542351
* followed by 28 bytes of generate random data. */
23552352
dtls_ticks(&now);
2356-
dtls_int_to_uint32(handshake->tmp.random.client, now / CLOCK_SECOND);
2353+
dtls_int_to_uint32(handshake->tmp.random.client, now / DTLS_TICKS_PER_SECOND);
23572354
dtls_prng(handshake->tmp.random.client + sizeof(uint32_t),
23582355
DTLS_RANDOM_LENGTH - sizeof(uint32_t));
23592356
}
@@ -3969,7 +3966,7 @@ dtls_stop_retransmission(dtls_context_t *context, dtls_peer_t *peer) {
39693966
}
39703967

39713968
void
3972-
dtls_check_retransmit(dtls_context_t *context, clock_time_t *next, int all) {
3969+
dtls_check_retransmit(dtls_context_t *context, dtls_tick_t *next, int all) {
39733970
dtls_tick_t now;
39743971
netq_t *node = netq_head(&context->sendqueue);
39753972

Diff for: dtls.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "dtls-hmac.h"
3535

3636
#include "tinydtls.h"
37-
#include "dtls_time.h"
3837

3938
#ifndef DTLSv12
4039
#define DTLS_VERSION 0xfeff /* DTLS v1.1 */
@@ -210,7 +209,7 @@ struct netq_t;
210209
/** Holds global information of the DTLS engine. */
211210
typedef struct dtls_context_t {
212211
unsigned char cookie_secret[DTLS_COOKIE_SECRET_LENGTH];
213-
clock_time_t cookie_secret_age; /**< the time the secret has been generated */
212+
dtls_tick_t cookie_secret_age; /**< the time the secret has been generated */
214213

215214
dtls_peer_t *peers; /**< peer hash map */
216215

@@ -306,7 +305,7 @@ int dtls_write(struct dtls_context_t *ctx, session_t *session,
306305
* of the next scheduled retransmission, or @c 0 when no packets are
307306
* waiting.
308307
*/
309-
void dtls_check_retransmit(dtls_context_t *context, clock_time_t *next,
308+
void dtls_check_retransmit(dtls_context_t *context, dtls_tick_t *next,
310309
int all);
311310

312311
#define DTLS_COOKIE_LENGTH 16

Diff for: dtls_time.h

-60
This file was deleted.

Diff for: netq.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "tinydtls.h"
2222
#include "dtls.h"
23-
#include "dtls_time.h"
2423

2524
/**
2625
* \defgroup netq Network Packet Queue
@@ -50,7 +49,7 @@ typedef unsigned char netq_packet_t[DTLS_MAX_BUF];
5049
typedef struct netq_t {
5150
struct netq_t *next;
5251

53-
clock_time_t t; /**< when to send PDU for the next time */
52+
dtls_tick_t t; /**< when to send PDU for the next time */
5453
unsigned int timeout; /**< randomized timeout value */
5554

5655
dtls_peer_t *peer; /**< remote address */

Diff for: posix/dtls-support-conf.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ typedef struct {
1717
uint8_t ifindex;
1818
} session_t;
1919

20+
#define DTLS_TICKS_PER_SECOND 1000
21+
22+
typedef uint64_t dtls_tick_t;
23+
2024
#endif /* DTLS_SUPPORT_CONF_H_ */

Diff for: posix/dtls-support.c

+19-13
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
#include "tinydtls.h"
55
#include "lib/memb.h"
6-
#include <stdlib.h>
76
#include "dtls-support.h"
87
#include "dtls_debug.h"
9-
#include "dtls_time.h"
8+
#include <stdlib.h>
9+
#include <sys/time.h>
1010
#ifdef HAVE_ASSERT_H
1111
#include <assert.h>
1212
#endif
@@ -214,19 +214,15 @@ dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, s
214214

215215
/* --------- time support ----------- */
216216

217-
time_t dtls_clock_offset;
217+
static time_t dtls_clock_offset;
218218

219219
void
220-
dtls_clock_init(void) {
221-
dtls_clock_offset = time(NULL);
222-
dtls_clock_offset = 0;
223-
}
224-
225-
void dtls_ticks(dtls_tick_t *t) {
220+
dtls_ticks(dtls_tick_t *t)
221+
{
226222
struct timeval tv;
227223
gettimeofday(&tv, NULL);
228-
*t = (tv.tv_sec - dtls_clock_offset) * DTLS_TICKS_PER_SECOND
229-
+ (tv.tv_usec * DTLS_TICKS_PER_SECOND / 1000000);
224+
*t = (tv.tv_sec - dtls_clock_offset) * (dtls_tick_t)DTLS_TICKS_PER_SECOND
225+
+ (tv.tv_usec * (dtls_tick_t)DTLS_TICKS_PER_SECOND / 1000000);
230226
}
231227

232228
int
@@ -292,7 +288,17 @@ dtls_session_equals(const session_t *a, const session_t *b) {
292288

293289

294290
/* The init */
295-
void dtls_support_init(void)
291+
void
292+
dtls_support_init(void)
296293
{
297-
/* setup whatever */
294+
#ifdef HAVE_TIME_H
295+
dtls_clock_offset = time(NULL);
296+
#else
297+
# ifdef __GNUC__
298+
/* Issue a warning when using gcc. Other prepropressors do
299+
* not seem to have a similar feature. */
300+
# warning "cannot initialize clock"
301+
# endif
302+
dtls_clock_offset = 0;
303+
#endif
298304
}

Diff for: tests/netq-test.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ int main(int argc, char **argv) {
2222
struct netq_t *nq = NULL, *node;
2323
int i;
2424

25-
clock_time_t timestamps[] = { 300, 100, 200, 400, 500 };
25+
dtls_tick_t timestamps[] = { 300, 100, 200, 400, 500 };
2626

27-
for (i = 0; i < sizeof(timestamps)/sizeof(clock_time_t); i++) {
27+
for (i = 0; i < sizeof(timestamps)/sizeof(dtls_tick_t); i++) {
2828
node = netq_node_new(0);
2929

3030
if (!node) {

0 commit comments

Comments
 (0)