Contiki-NG
dtls-support.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, RISE SICS AB.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * \file
33  * DTLS support for CoAP
34  * \author
35  * Niclas Finne <nfi@sics.se>
36  * Joakim Eriksson <joakime@sics.se>
37  */
38 
39 #include "dtls-support.h"
40 #include "lib/random.h"
41 #include <stdio.h>
42 #include <stdarg.h>
43 
44 /* Log configuration */
45 #define LOG_MODULE "dtls"
46 #define LOG_LEVEL LOG_LEVEL_DTLS
47 #include "dtls-log.h"
48 #include "coap-log.h"
49 
50 static dtls_context_t the_dtls_context;
51 static dtls_cipher_context_t cipher_context;
52 static uint8_t lock_context = 0;
53 /*---------------------------------------------------------------------------*/
54 dtls_context_t *
55 dtls_context_acquire(void)
56 {
57  if(lock_context) {
58  return NULL;
59  }
60  lock_context = 1;
61  LOG_DBG("Allocated context\n");
62  return &the_dtls_context;
63 }
64 /*---------------------------------------------------------------------------*/
65 void
66 dtls_context_release(dtls_context_t *context)
67 {
68  if(context == &the_dtls_context) {
69  lock_context = 0;
70  }
71 }
72 /*---------------------------------------------------------------------------*/
73 dtls_cipher_context_t *
74 dtls_cipher_context_acquire(void)
75 {
76  return &cipher_context;
77 }
78 /*---------------------------------------------------------------------------*/
79 void
80 dtls_cipher_context_release(dtls_cipher_context_t *c)
81 {
82 }
83 /*---------------------------------------------------------------------------*/
84 void
85 dtls_ticks(dtls_tick_t *t)
86 {
87  *t = clock_time();
88 }
89 /*---------------------------------------------------------------------------*/
90 int
91 dtls_fill_random(uint8_t *buf, size_t len)
92 {
93  int i;
94  if(buf) {
95  for(i = 0; i < len; i++) {
96  buf[i] = random_rand() & 0xff;
97  }
98  return 1;
99  }
100  return 0;
101 }
102 /*---------------------------------------------------------------------------*/
103 
104 /*---------------------------------------------------------------------------*/
105 /* message retransmission */
106 /*---------------------------------------------------------------------------*/
107 static void
108 dtls_retransmit_callback(void *ptr)
109 {
110  dtls_context_t *ctx;
111  clock_time_t now;
112  clock_time_t next;
113 
114  ctx = ptr;
115  now = clock_time();
116  /* Just one retransmission per timer scheduling */
117  dtls_check_retransmit(ctx, &next, 0);
118 
119  /* need to set timer to some value even if no nextpdu is available */
120  if(next != 0) {
121  ctimer_set(&ctx->support.retransmit_timer,
122  next <= now ? 1 : next - now,
123  dtls_retransmit_callback, ctx);
124  }
125 }
126 /*---------------------------------------------------------------------------*/
127 void
128 dtls_set_retransmit_timer(dtls_context_t *ctx, unsigned int timeout)
129 {
130  ctimer_set(&ctx->support.retransmit_timer, timeout,
131  dtls_retransmit_callback, ctx);
132 }
133 /*---------------------------------------------------------------------------*/
134 void
135 dtls_session_init(session_t *sess)
136 {
137  memset(sess, 0, sizeof(session_t));
138 }
139 /*---------------------------------------------------------------------------*/
140 int
141 dtls_session_equals(const session_t *a, const session_t *b)
142 {
143  coap_endpoint_t *e1 = (coap_endpoint_t *)a;
144  coap_endpoint_t *e2 = (coap_endpoint_t *)b;
145 
146  if(LOG_DBG_ENABLED) {
147  LOG_DBG(" **** EP:");
148  LOG_DBG_COAP_EP(e1);
149  LOG_DBG_(" =?= ");
150  LOG_DBG_COAP_EP(e2);
151  LOG_DBG_(" => %d\n", coap_endpoint_cmp(e1, e2));
152  }
153 
154  return coap_endpoint_cmp(e1, e2);
155 }
156 /*---------------------------------------------------------------------------*/
157 void *
158 dtls_session_get_address(const session_t *a)
159 {
160  /* improve this to only contain the addressing info */
161  return (void *)a;
162 }
163 /*---------------------------------------------------------------------------*/
164 int dtls_session_get_address_size(const session_t *a)
165 {
166  /* improve this to only contain the addressing info */
167  return sizeof(session_t);
168 }
169 /*---------------------------------------------------------------------------*/
170 void
171 dtls_session_print(const session_t *a)
172 {
173  coap_endpoint_print((const coap_endpoint_t *)a);
174 }
175 /*---------------------------------------------------------------------------*/
176 void
177 dtls_session_log(const session_t *a)
178 {
179  coap_endpoint_log((const coap_endpoint_t *)a);
180 }
181 /*---------------------------------------------------------------------------*/
182 void
183 dtls_support_init(void)
184 {
185  LOG_INFO("init\n");
186 }
187 /*---------------------------------------------------------------------------*/
Log support for CoAP
void coap_endpoint_print(const coap_endpoint_t *ep)
Print a CoAP endpoint.
Definition: coap-uip.c:110
void coap_endpoint_log(const coap_endpoint_t *ep)
Print a CoAP endpoint via the logging module.
Definition: coap-uip.c:94
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:118
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:58
int coap_endpoint_cmp(const coap_endpoint_t *e1, const coap_endpoint_t *e2)
Compare two CoAP endpoints.
Definition: coap-uip.c:163
static struct sicslowpan_addr_context * context
Addresses contexts for IPHC.
Definition: sicslowpan.c:516