Skip to content

Commit e670f60

Browse files
committed
Renamed DTLS support malloc_context/free_context to dtls_context_acquire/
dtls_context_release. Cleanup of Contiki support example.
1 parent d1895b6 commit e670f60

File tree

6 files changed

+141
-174
lines changed

6 files changed

+141
-174
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ typedef struct {
1212
#define DTLS_SUPPORT_CONF_CONTEXT_STATE dtls_support_context_state_t
1313

1414
typedef struct {
15-
unsigned char size;
1615
uip_ipaddr_t addr;
1716
uint16_t port;
18-
int ifindex;
1917
} session_t;
2018

2119
#define DTLS_TICKS_PER_SECOND CLOCK_SECOND

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

+129-165
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,117 @@
11
#include "dtls-support.h"
22
#include "lib/random.h"
3+
#include <stdarg.h>
4+
5+
#define DEBUG DEBUG_NONE
6+
#include "dtls_debug.h"
37

48
static dtls_context_t the_dtls_context;
59
static dtls_cipher_context_t cipher_context;
6-
7-
dtls_context_t *malloc_context() {
10+
static uint8_t lock_context = 0;
11+
/*---------------------------------------------------------------------------*/
12+
dtls_context_t *
13+
dtls_context_acquire(void)
14+
{
15+
if(lock_context) {
16+
return NULL;
17+
}
18+
lock_context = 1;
819
return &the_dtls_context;
920
}
10-
11-
void free_context(dtls_context_t *context) {
21+
/*---------------------------------------------------------------------------*/
22+
void
23+
dtls_context_release(dtls_context_t *context)
24+
{
25+
if(context == &the_dtls_context) {
26+
lock_context = 0;
27+
}
1228
}
13-
14-
PROCESS(dtls_retransmit_process, "DTLS retransmit process");
15-
29+
/*---------------------------------------------------------------------------*/
1630
/* In Contiki we know that there should be no threads accessing the
1731
functions at the same time which means there is no need for locking */
1832
dtls_cipher_context_t *
19-
dtls_cipher_context_get(void)
33+
dtls_cipher_context_acquire(void)
2034
{
2135
return &cipher_context;
2236
}
23-
37+
/*---------------------------------------------------------------------------*/
2438
void
2539
dtls_cipher_context_release(dtls_cipher_context_t *c)
2640
{
2741
}
28-
29-
30-
31-
#ifndef NDEBUG
32-
33-
static size_t
34-
dsrv_print_addr(const session_t *addr, char *buf, size_t len) {
35-
#ifdef HAVE_ARPA_INET_H
36-
const void *addrptr = NULL;
37-
in_port_t port;
38-
char *p = buf;
39-
40-
switch (addr->addr.sa.sa_family) {
41-
case AF_INET:
42-
if (len < INET_ADDRSTRLEN)
43-
return 0;
44-
45-
addrptr = &addr->addr.sin.sin_addr;
46-
port = ntohs(addr->addr.sin.sin_port);
47-
break;
48-
case AF_INET6:
49-
if (len < INET6_ADDRSTRLEN + 2)
50-
return 0;
51-
52-
*p++ = '[';
53-
54-
addrptr = &addr->addr.sin6.sin6_addr;
55-
port = ntohs(addr->addr.sin6.sin6_port);
56-
57-
break;
58-
default:
59-
memcpy(buf, "(unknown address type)", min(22, len));
60-
return min(22, len);
61-
}
62-
63-
if (inet_ntop(addr->addr.sa.sa_family, addrptr, p, len) == 0) {
64-
perror("dsrv_print_addr");
65-
return 0;
66-
}
67-
68-
p += dtls_strnlen(p, len);
69-
70-
if (addr->addr.sa.sa_family == AF_INET6) {
71-
if (p < buf + len) {
72-
*p++ = ']';
73-
} else
74-
return 0;
42+
/*---------------------------------------------------------------------------*/
43+
void
44+
dtls_session_init(session_t *sess)
45+
{
46+
memset(sess, 0, sizeof(session_t));
47+
}
48+
/*---------------------------------------------------------------------------*/
49+
int
50+
dtls_session_equals(const session_t *a, const session_t *b)
51+
{
52+
return a->port == b->port
53+
&& uip_ipaddr_cmp(&((a)->addr),&(b->addr));
54+
}
55+
/*---------------------------------------------------------------------------*/
56+
void *
57+
dtls_session_get_address(const session_t *a)
58+
{
59+
return (void *)a;
60+
}
61+
/*---------------------------------------------------------------------------*/
62+
int
63+
dtls_session_get_address_size(const session_t *a)
64+
{
65+
return sizeof(session_t);
66+
}
67+
/*---------------------------------------------------------------------------*/
68+
void
69+
dtls_session_print(const session_t *a)
70+
{
71+
printf("[");
72+
if(a) {
73+
uip_debug_ipaddr_print(&a->addr);
74+
printf("]:%u", a->port);
75+
} else {
76+
printf("NULL]");
7577
}
76-
77-
p += snprintf(p, buf + len - p + 1, ":%d", port);
78-
79-
return p - buf;
80-
#else /* HAVE_ARPA_INET_H */
81-
# if WITH_CONTIKI
82-
char *p = buf;
83-
# ifdef UIP_CONF_IPV6
84-
uint8_t i;
85-
const char hex[] = "0123456789ABCDEF";
86-
87-
if (len < 41)
88-
return 0;
89-
90-
*p++ = '[';
91-
92-
for (i=0; i < 16; i += 2) {
93-
if (i) {
94-
*p++ = ':';
95-
}
96-
*p++ = hex[(addr->addr.u8[i] & 0xf0) >> 4];
97-
*p++ = hex[(addr->addr.u8[i] & 0x0f)];
98-
*p++ = hex[(addr->addr.u8[i+1] & 0xf0) >> 4];
99-
*p++ = hex[(addr->addr.u8[i+1] & 0x0f)];
78+
}
79+
/*---------------------------------------------------------------------------*/
80+
size_t
81+
dsrv_print_addr(const session_t *addr, char *buf, size_t len)
82+
{
83+
if(len > 1) {
84+
/* TODO print IP address and port */
85+
buf[0] = '[';
86+
buf[1] = ']';
87+
return 2;
10088
}
101-
*p++ = ']';
102-
# else /* UIP_CONF_IPV6 */
103-
# warning "IPv4 network addresses will not be included in debug output"
104-
105-
if (len < 21)
106-
return 0;
107-
# endif /* UIP_CONF_IPV6 */
108-
if (buf + len - p < 6)
109-
return 0;
110-
111-
p += sprintf(p, ":%d", uip_htons(addr->port));
112-
113-
return p - buf;
114-
# else /* WITH_CONTIKI */
115-
/* TODO: output addresses manually */
116-
# warning "inet_ntop() not available, network addresses will not be included in debug output"
117-
# endif /* WITH_CONTIKI */
11889
return 0;
119-
#endif
12090
}
121-
122-
#endif /* NDEBUG */
123-
124-
91+
/*---------------------------------------------------------------------------*/
92+
extern char *loglevels[];
93+
static inline size_t
94+
print_timestamp(log_t level)
95+
{
96+
dtls_tick_t now;
97+
dtls_ticks(&now);
98+
if(level <= DTLS_LOG_DEBUG) {
99+
printf("%s ", loglevels[level]);
100+
}
101+
return printf("%5lu ", (unsigned long)now);
102+
}
103+
/*---------------------------------------------------------------------------*/
104+
#ifdef HAVE_VPRINTF
125105
void
126-
dsrv_log(log_t level, char *format, ...) {
127-
static char timebuf[32];
106+
dsrv_log(log_t level, char *format, ...)
107+
{
128108
va_list ap;
129109

130-
if (maxlog < level)
110+
if(dtls_get_log_level() < level) {
131111
return;
112+
}
132113

133-
if (print_timestamp(timebuf,sizeof(timebuf), clock_time()))
134-
PRINTF("%s ", timebuf);
114+
print_timestamp(level);
135115

136116
if(level <= DTLS_LOG_DEBUG) {
137117
PRINTF("%s ", loglevels[level]);
@@ -141,54 +121,46 @@ dsrv_log(log_t level, char *format, ...) {
141121
vprintf(format, ap);
142122
va_end(ap);
143123
}
144-
124+
#endif /* HAVE_VPRINTF */
125+
/*---------------------------------------------------------------------------*/
145126
void
146127
dtls_dsrv_hexdump_log(log_t level, const char *name, const unsigned char *buf, size_t length, int extend) {
147-
static char timebuf[32];
148128
int n = 0;
149129

150-
if (dtls_get_log_level() < level)
130+
if(dtls_get_log_level() < level) {
151131
return;
132+
}
152133

153-
if (print_timestamp(timebuf,sizeof(timebuf), clock_time()))
154-
PRINTF("%s ", timebuf);
155-
156-
if (level >= 0 && level <= DTLS_LOG_DEBUG)
157-
PRINTF("%s ", loglevels[level]);
134+
print_timestamp(level);
158135

159-
if (extend) {
136+
if(extend) {
160137
PRINTF("%s: (%zu bytes):\n", name, length);
161138

162-
while (length--) {
163-
if (n % 16 == 0)
139+
while(length--) {
140+
if (n % 16 == 0) {
164141
PRINTF("%08X ", n);
142+
}
165143

166144
PRINTF("%02X ", *buf++);
167145

168146
n++;
169-
if (n % 8 == 0) {
170-
if (n % 16 == 0)
147+
if(n % 8 == 0) {
148+
if(n % 16 == 0) {
171149
PRINTF("\n");
172-
else
173-
PRINTF(" ");
150+
} else {
151+
PRINTF(" ");
152+
}
174153
}
175154
}
176155
} else {
177156
PRINTF("%s: (%zu bytes): ", name, length);
178-
while (length--)
157+
while (length--) {
179158
PRINTF("%02X", *buf++);
159+
}
180160
}
181161
PRINTF("\n");
182162
}
183-
184-
/* --------- time support --------- */
185-
186-
void
187-
dtls_ticks(dtls_tick_t *t)
188-
{
189-
*t = clock_time();
190-
}
191-
163+
/*---------------------------------------------------------------------------*/
192164
int
193165
dtls_fill_random(uint8_t *buf, size_t len)
194166
{
@@ -202,58 +174,50 @@ dtls_fill_random(uint8_t *buf, size_t len)
202174
return 0;
203175
}
204176

205-
static void dtls_retransmit_callback(void *ptr);
206-
177+
/*---------------------------------------------------------------------------*/
178+
/* time support */
179+
/*---------------------------------------------------------------------------*/
207180
void
208-
dtls_support_init(void)
181+
dtls_ticks(dtls_tick_t *t)
209182
{
210-
/* Start the ctimer */
211-
ctimer_set(&the_dtls_context.support.retransmit_timer, 0xFFFF,
212-
dtls_retransmit_callback, NULL);
183+
*t = clock_time();
213184
}
214185

215186
/*---------------------------------------------------------------------------*/
216187
/* message retransmission */
217188
/*---------------------------------------------------------------------------*/
218-
static void dtls_retransmit_callback((void *) ptr)
189+
static void
190+
dtls_retransmit_callback(void *ptr)
219191
{
192+
dtls_context_t *context;
220193
clock_time_t now;
221194
clock_time_t next;
222195

196+
context = ptr;
197+
223198
now = clock_time();
199+
224200
/* Just one retransmission per timer scheduling */
225-
dtls_check_retransmit(&the_dtls_context, &next, 0);
201+
dtls_check_retransmit(context, &next, 0);
226202

227-
/* need to set timer to some value even if no nextpdu is available */
228-
if (next != 0) {
229-
ctimer_set(&the_dtls_context.retransmit_timer,
203+
if(next != 0) {
204+
ctimer_set(&context->support.retransmit_timer,
230205
next <= now ? 1 : next - now,
231-
dtls_retransmit_callback, NULL);
232-
} else {
233-
ctimer_set(&the_dtls_context.retransmit_timer, 0xFFFF,
234-
dtls_retransmit_callback, NULL);
206+
dtls_retransmit_callback, context);
235207
}
236208
}
237-
209+
/*---------------------------------------------------------------------------*/
238210
void
239-
dtls_session_init(session_t *sess) {
240-
assert(sess);
241-
memset(sess, 0, sizeof(session_t));
242-
sess->size = sizeof(sess->addr);
243-
}
244-
245-
int
246-
dtls_session_equals(const session_t *a, const session_t *b) {
247-
assert(a); assert(b);
248-
return (a->size == b->size
249-
&& a->port == b->port
250-
&& uip_ipaddr_cmp(&((a)->addr),&(b->addr))
251-
&& a->ifindex == b->ifindex);
211+
dtls_set_retransmit_timer(dtls_context_t *context, unsigned int time)
212+
{
213+
/* Start the ctimer for this context */
214+
ctimer_set(&context->support.retransmit_timer, time,
215+
dtls_retransmit_callback, context);
252216
}
253-
254-
255-
256-
void dtls_support_init(void)
217+
/*---------------------------------------------------------------------------*/
218+
void
219+
dtls_support_init(void)
257220
{
258221
/* setup whatever */
259222
}
223+
/*---------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)