1
1
#include "dtls-support.h"
2
2
#include "lib/random.h"
3
+ #include <stdarg.h>
4
+
5
+ #define DEBUG DEBUG_NONE
6
+ #include "dtls_debug.h"
3
7
4
8
static dtls_context_t the_dtls_context ;
5
9
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 ;
8
19
return & the_dtls_context ;
9
20
}
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
+ }
12
28
}
13
-
14
- PROCESS (dtls_retransmit_process , "DTLS retransmit process" );
15
-
29
+ /*---------------------------------------------------------------------------*/
16
30
/* In Contiki we know that there should be no threads accessing the
17
31
functions at the same time which means there is no need for locking */
18
32
dtls_cipher_context_t *
19
- dtls_cipher_context_get (void )
33
+ dtls_cipher_context_acquire (void )
20
34
{
21
35
return & cipher_context ;
22
36
}
23
-
37
+ /*---------------------------------------------------------------------------*/
24
38
void
25
39
dtls_cipher_context_release (dtls_cipher_context_t * c )
26
40
{
27
41
}
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]" );
75
77
}
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 ;
100
88
}
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 */
118
89
return 0 ;
119
- #endif
120
90
}
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
125
105
void
126
- dsrv_log (log_t level , char * format , ...) {
127
- static char timebuf [ 32 ];
106
+ dsrv_log (log_t level , char * format , ...)
107
+ {
128
108
va_list ap ;
129
109
130
- if ( maxlog < level )
110
+ if ( dtls_get_log_level () < level ) {
131
111
return ;
112
+ }
132
113
133
- if (print_timestamp (timebuf ,sizeof (timebuf ), clock_time ()))
134
- PRINTF ("%s " , timebuf );
114
+ print_timestamp (level );
135
115
136
116
if (level <= DTLS_LOG_DEBUG ) {
137
117
PRINTF ("%s " , loglevels [level ]);
@@ -141,54 +121,46 @@ dsrv_log(log_t level, char *format, ...) {
141
121
vprintf (format , ap );
142
122
va_end (ap );
143
123
}
144
-
124
+ #endif /* HAVE_VPRINTF */
125
+ /*---------------------------------------------------------------------------*/
145
126
void
146
127
dtls_dsrv_hexdump_log (log_t level , const char * name , const unsigned char * buf , size_t length , int extend ) {
147
- static char timebuf [32 ];
148
128
int n = 0 ;
149
129
150
- if (dtls_get_log_level () < level )
130
+ if (dtls_get_log_level () < level ) {
151
131
return ;
132
+ }
152
133
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 );
158
135
159
- if (extend ) {
136
+ if (extend ) {
160
137
PRINTF ("%s: (%zu bytes):\n" , name , length );
161
138
162
- while (length -- ) {
163
- if (n % 16 == 0 )
139
+ while (length -- ) {
140
+ if (n % 16 == 0 ) {
164
141
PRINTF ("%08X " , n );
142
+ }
165
143
166
144
PRINTF ("%02X " , * buf ++ );
167
145
168
146
n ++ ;
169
- if (n % 8 == 0 ) {
170
- if (n % 16 == 0 )
147
+ if (n % 8 == 0 ) {
148
+ if (n % 16 == 0 ) {
171
149
PRINTF ("\n" );
172
- else
173
- PRINTF (" " );
150
+ } else {
151
+ PRINTF (" " );
152
+ }
174
153
}
175
154
}
176
155
} else {
177
156
PRINTF ("%s: (%zu bytes): " , name , length );
178
- while (length -- )
157
+ while (length -- ) {
179
158
PRINTF ("%02X" , * buf ++ );
159
+ }
180
160
}
181
161
PRINTF ("\n" );
182
162
}
183
-
184
- /* --------- time support --------- */
185
-
186
- void
187
- dtls_ticks (dtls_tick_t * t )
188
- {
189
- * t = clock_time ();
190
- }
191
-
163
+ /*---------------------------------------------------------------------------*/
192
164
int
193
165
dtls_fill_random (uint8_t * buf , size_t len )
194
166
{
@@ -202,58 +174,50 @@ dtls_fill_random(uint8_t *buf, size_t len)
202
174
return 0 ;
203
175
}
204
176
205
- static void dtls_retransmit_callback (void * ptr );
206
-
177
+ /*---------------------------------------------------------------------------*/
178
+ /* time support */
179
+ /*---------------------------------------------------------------------------*/
207
180
void
208
- dtls_support_init ( void )
181
+ dtls_ticks ( dtls_tick_t * t )
209
182
{
210
- /* Start the ctimer */
211
- ctimer_set (& the_dtls_context .support .retransmit_timer , 0xFFFF ,
212
- dtls_retransmit_callback , NULL );
183
+ * t = clock_time ();
213
184
}
214
185
215
186
/*---------------------------------------------------------------------------*/
216
187
/* message retransmission */
217
188
/*---------------------------------------------------------------------------*/
218
- static void dtls_retransmit_callback ( (void * ) ptr )
189
+ static void
190
+ dtls_retransmit_callback (void * ptr )
219
191
{
192
+ dtls_context_t * context ;
220
193
clock_time_t now ;
221
194
clock_time_t next ;
222
195
196
+ context = ptr ;
197
+
223
198
now = clock_time ();
199
+
224
200
/* Just one retransmission per timer scheduling */
225
- dtls_check_retransmit (& the_dtls_context , & next , 0 );
201
+ dtls_check_retransmit (context , & next , 0 );
226
202
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 ,
230
205
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 );
235
207
}
236
208
}
237
-
209
+ /*---------------------------------------------------------------------------*/
238
210
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 );
252
216
}
253
-
254
-
255
-
256
- void dtls_support_init (void )
217
+ /*---------------------------------------------------------------------------*/
218
+ void
219
+ dtls_support_init (void )
257
220
{
258
221
/* setup whatever */
259
222
}
223
+ /*---------------------------------------------------------------------------*/
0 commit comments