/src/libcoap/src/coap_session.c
Line | Count | Source |
1 | | /* coap_session.c -- Session management for libcoap |
2 | | * |
3 | | * Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com> |
4 | | * Copyright (C) 2022-2026 Jon Shallow <supjps-libcoap@jpshallow.com> |
5 | | * |
6 | | * SPDX-License-Identifier: BSD-2-Clause |
7 | | * |
8 | | * This file is part of the CoAP library libcoap. Please see |
9 | | * README for terms of use. |
10 | | */ |
11 | | |
12 | | /** |
13 | | * @file coap_session.c |
14 | | * @brief Session handling functions |
15 | | */ |
16 | | |
17 | | #include "coap3/coap_libcoap_build.h" |
18 | | |
19 | | #ifndef COAP_SESSION_C_ |
20 | | #define COAP_SESSION_C_ |
21 | | |
22 | | #include <stdio.h> |
23 | | |
24 | | #ifdef COAP_EPOLL_SUPPORT |
25 | | #include <sys/epoll.h> |
26 | | #include <sys/timerfd.h> |
27 | | #endif /* COAP_EPOLL_SUPPORT */ |
28 | | |
29 | | coap_fixed_point_t |
30 | 58 | coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) { |
31 | 58 | coap_fixed_point_t res; |
32 | 58 | uint32_t fr = fp1.fractional_part * fp2.fractional_part; |
33 | | |
34 | 58 | res.integer_part = fp1.integer_part * fp2.integer_part + fr/1000; |
35 | 58 | res.fractional_part = fr % 1000; |
36 | 58 | return res; |
37 | 58 | } |
38 | | |
39 | | coap_fixed_point_t |
40 | 58 | coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) { |
41 | 58 | coap_fixed_point_t res; |
42 | 58 | uint32_t fr = fp1.fractional_part * u2; |
43 | | |
44 | 58 | res.integer_part = fp1.integer_part * u2 + fr/1000; |
45 | 58 | res.fractional_part = fr % 1000; |
46 | 58 | return res; |
47 | 58 | } |
48 | | |
49 | | coap_fixed_point_t |
50 | 29 | coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) { |
51 | 29 | coap_fixed_point_t res; |
52 | 29 | uint32_t fr = fp1.fractional_part + fp2.fractional_part; |
53 | | |
54 | 29 | res.integer_part = fp1.integer_part + fp2.integer_part + fr/1000; |
55 | 29 | res.fractional_part = fr % 1000; |
56 | 29 | return res; |
57 | 29 | } |
58 | | |
59 | | coap_fixed_point_t |
60 | 58 | coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) { |
61 | 58 | coap_fixed_point_t res = fp1; |
62 | | |
63 | 58 | res.integer_part += u2; |
64 | 58 | return res; |
65 | 58 | } |
66 | | |
67 | | coap_fixed_point_t |
68 | 0 | coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) { |
69 | 0 | coap_fixed_point_t res = fp1; |
70 | |
|
71 | 0 | res.integer_part -= u2; |
72 | 0 | return res; |
73 | 0 | } |
74 | | |
75 | | coap_fixed_point_t |
76 | 0 | coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) { |
77 | 0 | coap_fixed_point_t res; |
78 | 0 | uint32_t num = (fp1.integer_part * 1000 + fp1.fractional_part) / u2; |
79 | |
|
80 | 0 | res.integer_part = num / 1000; |
81 | 0 | res.fractional_part = num % 1000; |
82 | 0 | return res; |
83 | 0 | } |
84 | | |
85 | | #if COAP_Q_BLOCK_SUPPORT |
86 | | coap_fixed_point_t |
87 | 0 | coap_get_non_timeout_random(coap_session_t *session) { |
88 | 0 | coap_fixed_point_t res; |
89 | 0 | uint8_t ran; |
90 | |
|
91 | 0 | coap_prng_lkd(&ran, sizeof(ran)); |
92 | 0 | res = coap_sub_fixed_uint(COAP_ACK_RANDOM_FACTOR(session), 1); |
93 | 0 | res = coap_multi_fixed_uint(res, ran); |
94 | 0 | res = coap_div_fixed_uint(res, 0xff); |
95 | 0 | res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res); |
96 | 0 | return res; |
97 | 0 | } |
98 | | |
99 | | coap_tick_t |
100 | 0 | coap_get_non_timeout_random_ticks(coap_session_t *session) { |
101 | 0 | coap_fixed_point_t res = coap_get_non_timeout_random(session); |
102 | 0 | coap_tick_t ticks = (res.integer_part * COAP_TICKS_PER_SECOND + |
103 | 0 | res.fractional_part * COAP_TICKS_PER_SECOND / 1000); |
104 | |
|
105 | 0 | return ticks; |
106 | 0 | } |
107 | | |
108 | | /* |
109 | | * Save away derived Congestion Control parameters for speed of access. |
110 | | * They will get updated whenever a component variable is updated. |
111 | | */ |
112 | | |
113 | | /* |
114 | | * NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) * |
115 | | * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM |
116 | | * |
117 | | * Do not include NON_TIMEOUT_RANDOM as that changes |
118 | | */ |
119 | | static void |
120 | 29 | coap_session_fix_non_probing_wait_base(coap_session_t *s) { |
121 | 29 | coap_fixed_point_t res; |
122 | | |
123 | 29 | res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s), |
124 | 29 | ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1)); |
125 | 29 | res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s)); |
126 | 29 | res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY); |
127 | 29 | COAP_NON_PROBING_WAIT_BASE(s) = res; |
128 | 29 | } |
129 | | |
130 | | /* |
131 | | * NON_PARTIAL_TIMEOUT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) * |
132 | | * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT |
133 | | */ |
134 | | static void |
135 | 29 | coap_session_fix_non_partial_timeout(coap_session_t *s) { |
136 | 29 | coap_fixed_point_t res; |
137 | | |
138 | 29 | res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s), |
139 | 29 | ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1)); |
140 | 29 | res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s)); |
141 | 29 | res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY); |
142 | 29 | res = coap_add_fixed_fixed(res, COAP_NON_TIMEOUT(s)); |
143 | 29 | COAP_NON_PARTIAL_TIMEOUT(s) = res; |
144 | 29 | } |
145 | | #endif /* COAP_Q_BLOCK_SUPPORT */ |
146 | | |
147 | | void |
148 | 0 | coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value) { |
149 | 0 | if (value.integer_part > 0 && value.fractional_part < 1000) { |
150 | 0 | session->ack_timeout = value; |
151 | 0 | coap_log_debug("***%s: session ack_timeout set to %u.%03u\n", |
152 | 0 | coap_session_str(session), session->ack_timeout.integer_part, |
153 | 0 | session->ack_timeout.fractional_part); |
154 | 0 | } |
155 | 0 | } |
156 | | |
157 | | void |
158 | | coap_session_set_ack_random_factor(coap_session_t *session, |
159 | 0 | coap_fixed_point_t value) { |
160 | 0 | if (value.integer_part > 0 && value.fractional_part < 1000) { |
161 | 0 | session->ack_random_factor = value; |
162 | 0 | coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n", |
163 | 0 | coap_session_str(session), session->ack_random_factor.integer_part, |
164 | 0 | session->ack_random_factor.fractional_part); |
165 | 0 | #if COAP_Q_BLOCK_SUPPORT |
166 | 0 | coap_session_fix_non_probing_wait_base(session); |
167 | 0 | coap_session_fix_non_partial_timeout(session); |
168 | 0 | #endif /* COAP_Q_BLOCK_SUPPORT */ |
169 | 0 | } |
170 | 0 | return; |
171 | 0 | } |
172 | | |
173 | | void |
174 | 0 | coap_session_set_max_retransmit(coap_session_t *session, uint16_t value) { |
175 | 0 | if (value > 0xff) |
176 | 0 | value = 0xff; |
177 | 0 | if (value > 0) { |
178 | 0 | session->max_retransmit = value; |
179 | 0 | coap_log_debug("***%s: session max_retransmit set to %u\n", |
180 | 0 | coap_session_str(session), session->max_retransmit); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | | void |
185 | 0 | coap_session_set_nstart(coap_session_t *session, uint16_t value) { |
186 | 0 | if (value > 0) { |
187 | 0 | session->nstart = value; |
188 | 0 | coap_log_debug("***%s: session nstart set to %u\n", |
189 | 0 | coap_session_str(session), session->nstart); |
190 | 0 | } |
191 | 0 | } |
192 | | |
193 | | void |
194 | | coap_session_set_default_leisure(coap_session_t *session, |
195 | 0 | coap_fixed_point_t value) { |
196 | 0 | if (value.integer_part > 0 && value.fractional_part < 1000) { |
197 | 0 | session->default_leisure = value; |
198 | 0 | coap_log_debug("***%s: session default_leisure set to %u.%03u\n", |
199 | 0 | coap_session_str(session), session->default_leisure.integer_part, |
200 | 0 | session->default_leisure.fractional_part); |
201 | 0 | } |
202 | 0 | } |
203 | | |
204 | | void |
205 | 0 | coap_session_set_probing_rate(coap_session_t *session, uint32_t value) { |
206 | 0 | if (value > 0) { |
207 | 0 | session->probing_rate = value; |
208 | 0 | coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n", |
209 | 0 | coap_session_str(session), session->probing_rate); |
210 | 0 | } |
211 | 0 | } |
212 | | |
213 | | void |
214 | 0 | coap_session_set_max_payloads(coap_session_t *session, uint16_t value) { |
215 | 0 | #if COAP_Q_BLOCK_SUPPORT |
216 | 0 | if (value > 0) { |
217 | 0 | session->max_payloads = value; |
218 | 0 | coap_log_debug("***%s: session max_payloads set to %u\n", |
219 | 0 | coap_session_str(session), session->max_payloads); |
220 | 0 | coap_session_fix_non_probing_wait_base(session); |
221 | 0 | coap_session_fix_non_partial_timeout(session); |
222 | 0 | } |
223 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
224 | | (void)session; |
225 | | (void)value; |
226 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
227 | 0 | } |
228 | | |
229 | | void |
230 | 0 | coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value) { |
231 | 0 | #if COAP_Q_BLOCK_SUPPORT |
232 | 0 | if (value > 0) { |
233 | 0 | session->non_max_retransmit = value; |
234 | 0 | coap_log_debug("***%s: session non_max_retransmit set to %u\n", |
235 | 0 | coap_session_str(session), session->non_max_retransmit); |
236 | 0 | coap_session_fix_non_probing_wait_base(session); |
237 | 0 | coap_session_fix_non_partial_timeout(session); |
238 | 0 | } |
239 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
240 | | (void)session; |
241 | | (void)value; |
242 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
243 | 0 | } |
244 | | |
245 | | void |
246 | | coap_session_set_non_timeout(coap_session_t *session, |
247 | 0 | coap_fixed_point_t value) { |
248 | 0 | #if COAP_Q_BLOCK_SUPPORT |
249 | 0 | if (value.integer_part > 0 && value.fractional_part < 1000) { |
250 | 0 | session->non_timeout = value; |
251 | 0 | coap_log_debug("***%s: session non_timeout set to %u.%03u\n", |
252 | 0 | coap_session_str(session), session->non_timeout.integer_part, |
253 | 0 | session->non_timeout.fractional_part); |
254 | 0 | coap_session_fix_non_probing_wait_base(session); |
255 | 0 | coap_session_fix_non_partial_timeout(session); |
256 | 0 | } |
257 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
258 | | (void)session; |
259 | | (void)value; |
260 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
261 | 0 | } |
262 | | |
263 | | void |
264 | | coap_session_set_non_receive_timeout(coap_session_t *session, |
265 | 0 | coap_fixed_point_t value) { |
266 | 0 | #if COAP_Q_BLOCK_SUPPORT |
267 | 0 | if (value.integer_part > 0 && value.fractional_part < 1000) |
268 | 0 | session->non_receive_timeout = value; |
269 | 0 | coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n", |
270 | 0 | coap_session_str(session), |
271 | 0 | session->non_receive_timeout.integer_part, |
272 | 0 | session->non_receive_timeout.fractional_part); |
273 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
274 | | (void)session; |
275 | | (void)value; |
276 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
277 | 0 | } |
278 | | |
279 | | coap_fixed_point_t |
280 | 0 | coap_session_get_ack_timeout(const coap_session_t *session) { |
281 | 0 | return session->ack_timeout; |
282 | 0 | } |
283 | | |
284 | | coap_fixed_point_t |
285 | 0 | coap_session_get_ack_random_factor(const coap_session_t *session) { |
286 | 0 | return session->ack_random_factor; |
287 | 0 | } |
288 | | |
289 | | uint16_t |
290 | 0 | coap_session_get_max_retransmit(const coap_session_t *session) { |
291 | 0 | return session->max_retransmit; |
292 | 0 | } |
293 | | |
294 | | uint16_t |
295 | 0 | coap_session_get_nstart(const coap_session_t *session) { |
296 | 0 | return session->nstart; |
297 | 0 | } |
298 | | |
299 | | coap_fixed_point_t |
300 | 0 | coap_session_get_default_leisure(const coap_session_t *session) { |
301 | 0 | return session->default_leisure; |
302 | 0 | } |
303 | | |
304 | | uint32_t |
305 | 0 | coap_session_get_probing_rate(const coap_session_t *session) { |
306 | 0 | return session->probing_rate; |
307 | 0 | } |
308 | | |
309 | | uint16_t |
310 | 0 | coap_session_get_max_payloads(const coap_session_t *session) { |
311 | 0 | #if COAP_Q_BLOCK_SUPPORT |
312 | 0 | return session->max_payloads; |
313 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
314 | | (void)session; |
315 | | return COAP_DEFAULT_MAX_PAYLOADS; |
316 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
317 | 0 | } |
318 | | |
319 | | uint16_t |
320 | 0 | coap_session_get_non_max_retransmit(const coap_session_t *session) { |
321 | 0 | #if COAP_Q_BLOCK_SUPPORT |
322 | 0 | return session->non_max_retransmit; |
323 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
324 | | (void)session; |
325 | | return COAP_DEFAULT_NON_MAX_RETRANSMIT; |
326 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
327 | 0 | } |
328 | | |
329 | | coap_fixed_point_t |
330 | 0 | coap_session_get_non_timeout(const coap_session_t *session) { |
331 | 0 | #if COAP_Q_BLOCK_SUPPORT |
332 | 0 | return session->non_timeout; |
333 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
334 | | (void)session; |
335 | | return COAP_DEFAULT_NON_TIMEOUT; |
336 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
337 | 0 | } |
338 | | |
339 | | coap_fixed_point_t |
340 | 0 | coap_session_get_non_receive_timeout(const coap_session_t *session) { |
341 | 0 | #if COAP_Q_BLOCK_SUPPORT |
342 | 0 | return session->non_receive_timeout; |
343 | | #else /* ! COAP_Q_BLOCK_SUPPORT */ |
344 | | (void)session; |
345 | | return COAP_DEFAULT_NON_RECEIVE_TIMEOUT; |
346 | | #endif /* ! COAP_Q_BLOCK_SUPPORT */ |
347 | 0 | } |
348 | | |
349 | | COAP_API coap_session_t * |
350 | 0 | coap_session_reference(coap_session_t *session) { |
351 | 0 | coap_lock_lock(return NULL); |
352 | 0 | coap_session_reference_lkd(session); |
353 | 0 | coap_lock_unlock(); |
354 | 0 | return session; |
355 | 0 | } |
356 | | |
357 | | coap_session_t * |
358 | 116 | coap_session_reference_lkd(coap_session_t *session) { |
359 | 116 | ++session->ref; |
360 | 116 | return session; |
361 | 116 | } |
362 | | |
363 | | COAP_API void |
364 | 29 | coap_session_release(coap_session_t *session) { |
365 | 29 | if (session) { |
366 | | #if COAP_THREAD_SAFE |
367 | | coap_context_t *context = session->context; |
368 | | (void)context; |
369 | | #endif /* COAP_THREAD_SAFE */ |
370 | | |
371 | 29 | coap_lock_lock(return); |
372 | 29 | coap_session_release_lkd(session); |
373 | 29 | coap_lock_unlock(); |
374 | 29 | } |
375 | 29 | } |
376 | | |
377 | | void |
378 | 87 | coap_session_release_lkd(coap_session_t *session) { |
379 | 87 | if (session) { |
380 | 87 | coap_lock_check_locked(); |
381 | 87 | #ifndef __COVERITY__ |
382 | 87 | assert(session->ref > 0); |
383 | 87 | if (session->ref > 0) |
384 | 87 | --session->ref; |
385 | 87 | if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) |
386 | 29 | coap_session_free(session); |
387 | | #else /* __COVERITY__ */ |
388 | | /* Coverity scan is fooled by the reference counter leading to |
389 | | * false positives for USE_AFTER_FREE. */ |
390 | | --session->ref; |
391 | | __coverity_negative_sink__(session->ref); |
392 | | /* Indicate that resources are released properly. */ |
393 | | if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) { |
394 | | __coverity_free__(session); |
395 | | } |
396 | | #endif /* __COVERITY__ */ |
397 | 87 | } |
398 | 87 | } |
399 | | |
400 | | COAP_API void |
401 | 0 | coap_session_set_app_data(coap_session_t *session, void *app_data) { |
402 | 0 | assert(session); |
403 | 0 | coap_lock_lock(return); |
404 | 0 | coap_session_set_app_data2_lkd(session, app_data, NULL); |
405 | 0 | coap_lock_unlock(); |
406 | 0 | } |
407 | | |
408 | | void * |
409 | 0 | coap_session_get_app_data(const coap_session_t *session) { |
410 | 0 | assert(session); |
411 | 0 | return session->app_data; |
412 | 0 | } |
413 | | |
414 | | COAP_API void * |
415 | | coap_session_set_app_data2(coap_session_t *session, void *app_data, |
416 | 0 | coap_app_data_free_callback_t callback) { |
417 | 0 | void *old_data; |
418 | |
|
419 | 0 | coap_lock_lock(return NULL); |
420 | 0 | old_data = coap_session_set_app_data2_lkd(session, app_data, callback); |
421 | 0 | coap_lock_unlock(); |
422 | 0 | return old_data; |
423 | 0 | } |
424 | | |
425 | | void * |
426 | | coap_session_set_app_data2_lkd(coap_session_t *session, void *app_data, |
427 | 0 | coap_app_data_free_callback_t callback) { |
428 | 0 | void *old_data = session->app_data; |
429 | |
|
430 | 0 | session->app_data = app_data; |
431 | 0 | session->app_cb = app_data ? callback : NULL; |
432 | 0 | return old_data; |
433 | 0 | } |
434 | | |
435 | | static coap_session_t * |
436 | | coap_make_session(coap_proto_t proto, coap_session_type_t type, |
437 | | const coap_addr_hash_t *addr_hash, |
438 | | const coap_address_t *local_addr, |
439 | | const coap_address_t *remote_addr, int ifindex, |
440 | 29 | coap_context_t *context, coap_endpoint_t *endpoint) { |
441 | 29 | coap_session_t *session = (coap_session_t *)coap_malloc_type(COAP_SESSION, |
442 | 29 | sizeof(coap_session_t)); |
443 | | #if ! COAP_SERVER_SUPPORT |
444 | | (void)endpoint; |
445 | | #endif /* ! COAP_SERVER_SUPPORT */ |
446 | 29 | if (!session) |
447 | 0 | return NULL; |
448 | 29 | memset(session, 0, sizeof(*session)); |
449 | 29 | session->proto = proto; |
450 | 29 | session->type = type; |
451 | 29 | if (addr_hash) |
452 | 0 | memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash)); |
453 | 29 | else |
454 | 29 | memset(&session->addr_hash, 0, sizeof(session->addr_hash)); |
455 | 29 | if (local_addr) { |
456 | 0 | coap_address_copy(&session->addr_info.local, local_addr); |
457 | 0 | #if COAP_CLIENT_SUPPORT |
458 | 0 | coap_address_copy(&session->local_reconnect, local_addr); |
459 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
460 | 29 | } else { |
461 | 29 | coap_address_init(&session->addr_info.local); |
462 | 29 | #if COAP_CLIENT_SUPPORT |
463 | 29 | coap_address_init(&session->local_reconnect); |
464 | 29 | #endif /* COAP_CLIENT_SUPPORT */ |
465 | 29 | } |
466 | 29 | if (remote_addr) |
467 | 29 | coap_address_copy(&session->addr_info.remote, remote_addr); |
468 | 0 | else |
469 | 0 | coap_address_init(&session->addr_info.remote); |
470 | 29 | session->ifindex = ifindex; |
471 | 29 | session->context = context; |
472 | 29 | #if COAP_CLIENT_SUPPORT |
473 | 29 | if (type == COAP_SESSION_TYPE_CLIENT) { |
474 | 29 | session->client_initiated = 1; |
475 | 29 | } |
476 | 29 | #endif /* COAP_CLIENT_SUPPORT */ |
477 | 29 | #if COAP_SERVER_SUPPORT |
478 | 29 | session->endpoint = endpoint; |
479 | 29 | if (endpoint) |
480 | 0 | session->mtu = endpoint->default_mtu; |
481 | 29 | else |
482 | 29 | #endif /* COAP_SERVER_SUPPORT */ |
483 | 29 | coap_session_set_mtu(session, COAP_DEFAULT_MTU); |
484 | 29 | session->block_mode = context->block_mode; |
485 | 29 | #if COAP_Q_BLOCK_SUPPORT |
486 | 29 | if (session->block_mode & COAP_BLOCK_FORCE_Q_BLOCK) { |
487 | 0 | set_block_mode_has_q(session->block_mode); |
488 | 0 | } |
489 | 29 | #endif |
490 | 29 | if (proto == COAP_PROTO_DTLS) { |
491 | 0 | session->tls_overhead = 29; |
492 | 0 | if (session->tls_overhead >= session->mtu) { |
493 | 0 | session->tls_overhead = session->mtu; |
494 | 0 | coap_log_err("DTLS overhead exceeds MTU\n"); |
495 | 0 | } |
496 | 0 | } |
497 | 29 | session->rl_ticks_per_packet = context->rl_ticks_per_packet; |
498 | 29 | session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT; |
499 | 29 | session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR; |
500 | 29 | session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT; |
501 | 29 | session->nstart = COAP_DEFAULT_NSTART; |
502 | 29 | session->default_leisure = COAP_DEFAULT_DEFAULT_LEISURE; |
503 | 29 | session->probing_rate = COAP_DEFAULT_PROBING_RATE; |
504 | 29 | #if COAP_Q_BLOCK_SUPPORT |
505 | 29 | session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS; |
506 | 29 | session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT; |
507 | 29 | session->non_timeout = COAP_DEFAULT_NON_TIMEOUT; |
508 | 29 | session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT; |
509 | 29 | coap_session_fix_non_probing_wait_base(session); |
510 | 29 | coap_session_fix_non_partial_timeout(session); |
511 | 29 | #endif /* COAP_Q_BLOCK_SUPPORT */ |
512 | 29 | session->dtls_event = -1; |
513 | 29 | session->last_ping_mid = COAP_INVALID_MID; |
514 | 29 | session->last_resp_mid = COAP_INVALID_MID; |
515 | 29 | session->last_con_handler_res = COAP_RESPONSE_OK; |
516 | 29 | session->max_token_size = context->max_token_size; /* RFC8974 */ |
517 | 29 | if (session->type != COAP_SESSION_TYPE_CLIENT) |
518 | 0 | session->max_token_checked = COAP_EXT_T_CHECKED; |
519 | | |
520 | | /* Randomly initialize */ |
521 | | /* TCP/TLS have no notion of mid */ |
522 | 29 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) |
523 | 29 | coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid)); |
524 | 29 | coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag)); |
525 | | |
526 | 29 | return session; |
527 | 29 | } |
528 | | |
529 | | void |
530 | 29 | coap_session_mfree(coap_session_t *session) { |
531 | 29 | coap_queue_t *q, *tmp; |
532 | 29 | coap_lg_xmit_t *lq, *ltmp; |
533 | | |
534 | 29 | #if COAP_PROXY_SUPPORT |
535 | 29 | if (session->ref_proxy_subs) |
536 | 0 | coap_delete_proxy_subscriber(session, NULL, 0, COAP_PROXY_SUBS_ALL); |
537 | 29 | #endif /* COAP_PROXY_SUPPORT */ |
538 | 29 | #if COAP_CLIENT_SUPPORT |
539 | 29 | coap_lg_crcv_t *lg_crcv, *etmp; |
540 | | |
541 | | /* Need to do this before (D)TLS and socket is closed down */ |
542 | 29 | LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) { |
543 | 0 | if (lg_crcv->observe_set && session->no_observe_cancel == 0) { |
544 | | /* Need to close down observe */ |
545 | 0 | if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) { |
546 | | /* Need to delete node we set up for NON */ |
547 | 0 | coap_queue_t *queue = session->context->sendqueue; |
548 | |
|
549 | 0 | while (queue) { |
550 | 0 | if (queue->session == session) { |
551 | 0 | coap_delete_node_lkd(queue); |
552 | 0 | break; |
553 | 0 | } |
554 | 0 | queue = queue->next; |
555 | 0 | } |
556 | 0 | } |
557 | 0 | } |
558 | | /* In case coap_cancel_observe_lkd() failure, which could clear down lg_crcv */ |
559 | 0 | if (!session->lg_crcv) |
560 | 0 | break; |
561 | 0 | LL_DELETE(session->lg_crcv, lg_crcv); |
562 | 0 | coap_block_delete_lg_crcv(session, lg_crcv); |
563 | 0 | } |
564 | 29 | #endif /* COAP_CLIENT_SUPPORT */ |
565 | | |
566 | 29 | LL_FOREACH_SAFE(session->delayqueue, q, tmp) { |
567 | 0 | if (q->pdu->type==COAP_MESSAGE_CON) { |
568 | 0 | coap_handle_nack(session, q->pdu, |
569 | 0 | session->proto == COAP_PROTO_DTLS ? |
570 | 0 | COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, |
571 | 0 | q->id); |
572 | 0 | } |
573 | 0 | coap_delete_node_lkd(q); |
574 | 0 | } |
575 | | |
576 | 29 | #if COAP_CLIENT_SUPPORT |
577 | 29 | coap_pdu_t *p, *ptmp; |
578 | | |
579 | 29 | LL_FOREACH_SAFE(session->doing_first_pdu, p, ptmp) { |
580 | 0 | if (p->type==COAP_MESSAGE_CON) { |
581 | 0 | coap_handle_nack(session, p, |
582 | 0 | session->proto == COAP_PROTO_DTLS ? |
583 | 0 | COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE, |
584 | 0 | p->mid); |
585 | 0 | } |
586 | 0 | coap_delete_pdu_lkd(p); |
587 | 0 | } |
588 | 29 | #endif /* COAP_CLIENT_SUPPORT */ |
589 | | |
590 | 29 | if (session->partial_pdu) |
591 | 0 | coap_delete_pdu_lkd(session->partial_pdu); |
592 | 29 | if (session->sock.lfunc[COAP_LAYER_SESSION].l_close) |
593 | 29 | session->sock.lfunc[COAP_LAYER_SESSION].l_close(session); |
594 | 29 | if (session->psk_identity) |
595 | 0 | coap_delete_bin_const(session->psk_identity); |
596 | 29 | if (session->psk_key) |
597 | 0 | coap_delete_bin_const(session->psk_key); |
598 | 29 | if (session->psk_hint) |
599 | 0 | coap_delete_bin_const(session->psk_hint); |
600 | | |
601 | 29 | #ifdef HAVE_LINUX_ERRQUEUE_H |
602 | 29 | coap_delete_str_const(session->last_data_write); |
603 | 29 | #endif /* HAVE_LINUX_ERRQUEUE_H */ |
604 | | |
605 | 29 | #if COAP_SERVER_SUPPORT |
606 | 29 | coap_cache_entry_t *cp, *ctmp; |
607 | 29 | HASH_ITER(hh, session->context->cache, cp, ctmp) { |
608 | | /* cp->session is NULL if not session based */ |
609 | 0 | if (cp->session == session) { |
610 | 0 | coap_delete_cache_entry(session->context, cp); |
611 | 0 | } |
612 | 0 | } |
613 | 29 | #endif /* COAP_PROXY_SUPPORT */ |
614 | 29 | LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) { |
615 | 0 | LL_DELETE(session->lg_xmit, lq); |
616 | 0 | coap_block_delete_lg_xmit(session, lq); |
617 | 0 | } |
618 | 29 | #if COAP_SERVER_SUPPORT |
619 | 29 | coap_lg_srcv_t *sq, *stmp; |
620 | | |
621 | 29 | LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) { |
622 | 0 | LL_DELETE(session->lg_srcv, sq); |
623 | 0 | coap_block_delete_lg_srcv(session, sq); |
624 | 0 | } |
625 | 29 | #endif /* COAP_SERVER_SUPPORT */ |
626 | 29 | #if COAP_OSCORE_SUPPORT |
627 | 29 | coap_delete_oscore_associations(session); |
628 | 29 | oscore_release_recipient_ctx(&session->recipient_ctx); |
629 | 29 | coap_delete_str_const(session->b_2_retransmit_token); |
630 | 29 | #endif /* COAP_OSCORE_SUPPORT */ |
631 | 29 | #if COAP_WS_SUPPORT |
632 | 29 | coap_free_type(COAP_STRING, session->ws); |
633 | 29 | coap_delete_str_const(session->ws_host); |
634 | 29 | #endif /* COAP_WS_SUPPORT */ |
635 | 29 | } |
636 | | |
637 | | void |
638 | 29 | coap_session_free(coap_session_t *session) { |
639 | 29 | if (!session) |
640 | 0 | return; |
641 | 29 | coap_lock_check_locked(); |
642 | 29 | assert(session->ref == 0); |
643 | 29 | if (session->ref) |
644 | 0 | return; |
645 | | /* Make sure nothing gets deleted under our feet */ |
646 | 29 | coap_session_reference_lkd(session); |
647 | 29 | coap_session_mfree(session); |
648 | 29 | #if COAP_SERVER_SUPPORT |
649 | 29 | coap_delete_bin_const(session->client_cid); |
650 | 29 | if (session->endpoint) { |
651 | 0 | if (session->endpoint->sessions) |
652 | 0 | SESSIONS_DELETE(session->endpoint->sessions, session); |
653 | 0 | } else |
654 | 29 | #endif /* COAP_SERVER_SUPPORT */ |
655 | 29 | #if COAP_CLIENT_SUPPORT |
656 | 29 | if (session->context) { |
657 | 29 | if (session->context->sessions) |
658 | 29 | SESSIONS_DELETE(session->context->sessions, session); |
659 | 29 | } |
660 | 29 | coap_delete_bin_const(session->req_token); |
661 | 29 | #endif /* COAP_CLIENT_SUPPORT */ |
662 | 29 | coap_delete_bin_const(session->last_token); |
663 | 29 | coap_delete_bin_const(session->echo); |
664 | 29 | #if COAP_SERVER_SUPPORT |
665 | 29 | coap_delete_pdu_lkd(session->last_resp_pdu); |
666 | 29 | #endif /* COAP_SERVER_SUPPORT */ |
667 | | |
668 | 29 | if (session->app_cb) { |
669 | 0 | coap_lock_callback(session->app_cb(session->app_data)); |
670 | 0 | } |
671 | 29 | coap_log_debug("***%s: session %p: closed\n", coap_session_str(session), |
672 | 29 | (void *)session); |
673 | 29 | assert(session->ref == 1); |
674 | 29 | coap_free_type(COAP_SESSION, session); |
675 | 29 | } |
676 | | |
677 | | #if COAP_SERVER_SUPPORT |
678 | | void |
679 | 0 | coap_session_server_keepalive_failed(coap_session_t *session) { |
680 | 0 | int i; |
681 | |
|
682 | 0 | coap_session_reference_lkd(session); |
683 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_KEEPALIVE_FAILURE, session); |
684 | 0 | coap_cancel_all_messages(session->context, session, NULL); |
685 | 0 | RESOURCES_ITER(session->context->resources, r) { |
686 | | /* In case code is broken somewhere */ |
687 | 0 | for (i = 0; i < 1000; i++) { |
688 | 0 | if (!coap_delete_observer(r, session, NULL)) |
689 | 0 | break; |
690 | 0 | } |
691 | 0 | } |
692 | 0 | if (session->context->unknown_resource) { |
693 | | /* In case code is broken somewhere */ |
694 | 0 | for (i = 0; i < 1000; i++) { |
695 | 0 | if (!coap_delete_observer(session->context->unknown_resource, session, NULL)) |
696 | 0 | break; |
697 | 0 | } |
698 | 0 | } |
699 | 0 | if (session->context->proxy_uri_resource) { |
700 | | /* In case code is broken somewhere */ |
701 | 0 | for (i = 0; i < 1000; i++) { |
702 | 0 | if (!coap_delete_observer(session->context->proxy_uri_resource, session, NULL)) |
703 | 0 | break; |
704 | 0 | } |
705 | 0 | } |
706 | 0 | while (session->delayqueue) { |
707 | 0 | coap_queue_t *q = session->delayqueue; |
708 | |
|
709 | 0 | session->delayqueue = q->next; |
710 | 0 | coap_delete_node_lkd(q); |
711 | 0 | } |
712 | | /* Force session to go away */ |
713 | 0 | coap_session_set_type_client_lkd(session, 0); |
714 | 0 | coap_session_release_lkd(session); |
715 | |
|
716 | 0 | coap_session_release_lkd(session); |
717 | 0 | } |
718 | | #endif /* COAP_SERVER_SUPPORT */ |
719 | | |
720 | | static size_t |
721 | | coap_session_max_pdu_size_internal(const coap_session_t *session, |
722 | 230 | size_t max_with_header) { |
723 | | #if COAP_DISABLE_TCP |
724 | | (void)session; |
725 | | return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE |
726 | | ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE |
727 | | : 0; |
728 | | #else /* !COAP_DISABLE_TCP */ |
729 | 230 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) |
730 | 230 | return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE |
731 | 230 | ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE |
732 | 230 | : 0; |
733 | | /* we must assume there is no token to be on the safe side */ |
734 | 0 | if (max_with_header <= 2) |
735 | 0 | return 0; |
736 | 0 | else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2) |
737 | 0 | return max_with_header - 2; |
738 | 0 | else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3) |
739 | 0 | return max_with_header - 3; |
740 | 0 | else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4) |
741 | 0 | return max_with_header - 4; |
742 | 0 | else |
743 | 0 | return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE; |
744 | 0 | #endif /* !COAP_DISABLE_TCP */ |
745 | 0 | } |
746 | | |
747 | | size_t |
748 | 24 | coap_session_max_pdu_rcv_size(const coap_session_t *session) { |
749 | 24 | if (session->csm_rcv_mtu) |
750 | 0 | return coap_session_max_pdu_size_internal(session, |
751 | 0 | (size_t)(session->csm_rcv_mtu)); |
752 | | |
753 | 24 | return coap_session_max_pdu_size_internal(session, |
754 | 24 | (size_t)(session->mtu - session->tls_overhead)); |
755 | 24 | } |
756 | | |
757 | | COAP_API size_t |
758 | 57 | coap_session_max_pdu_size(const coap_session_t *session) { |
759 | 57 | size_t size; |
760 | 57 | coap_session_t *session_rw; |
761 | | |
762 | | /* |
763 | | * Need to do this to not get a compiler warning about const parameters |
764 | | * but need to maintain source code backward compatibility |
765 | | */ |
766 | 57 | memcpy(&session_rw, &session, sizeof(session_rw)); |
767 | 57 | coap_lock_lock(return 0); |
768 | 57 | size = coap_session_max_pdu_size_lkd(session_rw); |
769 | 57 | coap_lock_unlock(); |
770 | 57 | return size; |
771 | 57 | } |
772 | | |
773 | | size_t |
774 | 206 | coap_session_max_pdu_size_lkd(const coap_session_t *session) { |
775 | 206 | size_t max_with_header; |
776 | | |
777 | 206 | coap_lock_check_locked(); |
778 | 206 | #if COAP_CLIENT_SUPPORT |
779 | 206 | if (COAP_PROTO_RELIABLE(session->proto) && |
780 | 0 | session->type == COAP_SESSION_TYPE_CLIENT && |
781 | 0 | session->doing_first) { |
782 | | /* |
783 | | * Delay if session->doing_first is set. |
784 | | * E.g. Reliable and CSM not in yet for checking block support |
785 | | */ |
786 | 0 | coap_session_t *session_rw; |
787 | | |
788 | | /* |
789 | | * Need to do this to not get a compiler warning about const parameters |
790 | | * but need to maintain source code backward compatibility |
791 | | */ |
792 | 0 | memcpy(&session_rw, &session, sizeof(session_rw)); |
793 | 0 | if (coap_client_delay_first(session_rw) == 0) { |
794 | 0 | coap_log_debug("coap_client_delay_first: timeout\n"); |
795 | | /* Have to go with the defaults */ |
796 | 0 | } |
797 | 0 | } |
798 | 206 | #endif /* COAP_CLIENT_SUPPORT */ |
799 | | |
800 | 206 | max_with_header = (size_t)(session->mtu - session->tls_overhead); |
801 | | |
802 | 206 | return coap_session_max_pdu_size_internal(session, max_with_header); |
803 | 206 | } |
804 | | |
805 | | void |
806 | 29 | coap_session_set_mtu(coap_session_t *session, unsigned mtu) { |
807 | 29 | if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE) { |
808 | 0 | mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE; |
809 | 0 | coap_log_debug("* %s: Restricting MTU size to %u\n", |
810 | 0 | coap_session_str(session), mtu); |
811 | 0 | } |
812 | 29 | if (mtu < 64) |
813 | 0 | mtu = 64; |
814 | 29 | session->mtu = mtu; |
815 | 29 | if (session->tls_overhead >= session->mtu) { |
816 | 0 | session->tls_overhead = session->mtu; |
817 | 0 | coap_log_err("DTLS overhead exceeds MTU\n"); |
818 | 0 | } |
819 | 29 | } |
820 | | |
821 | | ssize_t |
822 | | coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, |
823 | 0 | coap_queue_t *node) { |
824 | 0 | if (node) { |
825 | 0 | coap_queue_t *removed = NULL; |
826 | | /* Nodes are per ID, no notion of token */ |
827 | 0 | coap_remove_from_queue(&session->context->sendqueue, session, node->id, |
828 | 0 | NULL, &removed); |
829 | 0 | assert(removed == node); |
830 | | /* reuse node */ |
831 | 0 | coap_session_release_lkd(node->session); |
832 | 0 | node->session = NULL; |
833 | 0 | node->t = 0; |
834 | 0 | } else { |
835 | 0 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
836 | 0 | coap_queue_t *q = NULL; |
837 | | /* Check same mid is not getting re-used in violation of RFC7252 */ |
838 | 0 | LL_FOREACH(session->delayqueue, q) { |
839 | 0 | if (q->id == pdu->mid) { |
840 | 0 | coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n", |
841 | 0 | coap_session_str(session), pdu->mid); |
842 | 0 | return COAP_INVALID_MID; |
843 | 0 | } |
844 | 0 | } |
845 | 0 | } |
846 | 0 | node = coap_new_node(); |
847 | 0 | if (node == NULL) |
848 | 0 | return COAP_INVALID_MID; |
849 | 0 | node->id = pdu->mid; |
850 | 0 | node->pdu = pdu; |
851 | 0 | if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) { |
852 | 0 | uint8_t r; |
853 | 0 | coap_prng_lkd(&r, sizeof(r)); |
854 | | /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */ |
855 | 0 | node->timeout = coap_calc_timeout(session, r); |
856 | 0 | } |
857 | 0 | coap_address_copy(&node->remote, &session->addr_info.remote); |
858 | 0 | } |
859 | 0 | LL_APPEND(session->delayqueue, node); |
860 | 0 | coap_show_pdu(COAP_LOG_DEBUG, node->pdu); |
861 | 0 | coap_log_debug("** %s: mid=0x%04x: delayed\n", |
862 | 0 | coap_session_str(session), node->id); |
863 | 0 | return COAP_PDU_DELAYED; |
864 | 0 | } |
865 | | |
866 | | #if !COAP_DISABLE_TCP |
867 | | void |
868 | 0 | coap_session_send_csm(coap_session_t *session) { |
869 | 0 | coap_pdu_t *pdu; |
870 | 0 | uint8_t buf[4]; |
871 | 0 | assert(COAP_PROTO_RELIABLE(session->proto)); |
872 | 0 | coap_log_debug("***%s: sending CSM\n", coap_session_str(session)); |
873 | 0 | session->state = COAP_SESSION_STATE_CSM; |
874 | 0 | session->partial_write = 0; |
875 | 0 | if (session->mtu == 0) |
876 | 0 | coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */ |
877 | 0 | pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_CSM, 0, 20); |
878 | 0 | if (pdu == NULL |
879 | 0 | || coap_add_option_internal(pdu, (coap_option_num_t)COAP_SIG_OPT_MAX_MESSAGE_SIZE, |
880 | 0 | coap_encode_var_safe(buf, sizeof(buf), |
881 | 0 | session->context->csm_max_message_size), buf) == 0 |
882 | 0 | || coap_add_option_internal(pdu, (coap_option_num_t)COAP_SIG_OPT_BLOCK_WISE_TRANSFER, |
883 | 0 | coap_encode_var_safe(buf, sizeof(buf), |
884 | 0 | 0), buf) == 0 |
885 | 0 | || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX && |
886 | 0 | coap_add_option_internal(pdu, |
887 | 0 | (coap_option_num_t)COAP_SIG_OPT_EXTENDED_TOKEN_LENGTH, |
888 | 0 | coap_encode_var_safe(buf, sizeof(buf), |
889 | 0 | session->max_token_size), |
890 | 0 | buf) == 0) |
891 | 0 | || coap_pdu_encode_header(pdu, session->proto) == 0 |
892 | 0 | ) { |
893 | 0 | coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE); |
894 | 0 | } else { |
895 | 0 | ssize_t bytes_written; |
896 | |
|
897 | 0 | pdu->session = session; |
898 | 0 | bytes_written = coap_session_send_pdu(session, pdu); |
899 | 0 | if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) { |
900 | 0 | coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE); |
901 | 0 | } else { |
902 | 0 | session->csm_rcv_mtu = session->context->csm_max_message_size; |
903 | 0 | if (session->csm_rcv_mtu > COAP_BERT_BASE) |
904 | 0 | session->csm_bert_loc_support = 1; |
905 | 0 | else |
906 | 0 | session->csm_bert_loc_support = 0; |
907 | 0 | } |
908 | 0 | } |
909 | 0 | if (pdu) |
910 | 0 | coap_delete_pdu_lkd(pdu); |
911 | 0 | } |
912 | | #endif /* !COAP_DISABLE_TCP */ |
913 | | |
914 | | COAP_API coap_mid_t |
915 | 0 | coap_session_send_ping(coap_session_t *session) { |
916 | 0 | coap_mid_t mid; |
917 | |
|
918 | 0 | coap_lock_lock(return COAP_INVALID_MID); |
919 | 0 | mid = coap_session_send_ping_lkd(session); |
920 | 0 | if (mid != COAP_INVALID_MID) { |
921 | 0 | coap_tick_t now; |
922 | |
|
923 | 0 | coap_ticks(&now); |
924 | 0 | session->last_ping_mid = mid; |
925 | 0 | session->last_rx_tx = now; |
926 | 0 | session->last_ping = now; |
927 | 0 | } |
928 | 0 | coap_lock_unlock(); |
929 | 0 | return mid; |
930 | 0 | } |
931 | | |
932 | | coap_mid_t |
933 | 0 | coap_session_send_ping_lkd(coap_session_t *session) { |
934 | 0 | coap_pdu_t *ping = NULL; |
935 | |
|
936 | 0 | coap_lock_check_locked(); |
937 | 0 | if (session->state != COAP_SESSION_STATE_ESTABLISHED || |
938 | 0 | session->con_active) |
939 | 0 | return COAP_INVALID_MID; |
940 | 0 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
941 | 0 | uint16_t mid = coap_new_message_id_lkd(session); |
942 | 0 | ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0); |
943 | 0 | } |
944 | 0 | #if !COAP_DISABLE_TCP |
945 | 0 | else { |
946 | 0 | ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, 0, 1); |
947 | 0 | } |
948 | 0 | #endif /* !COAP_DISABLE_TCP */ |
949 | 0 | if (!ping) |
950 | 0 | return COAP_INVALID_MID; |
951 | 0 | return coap_send_internal(session, ping, NULL); |
952 | 0 | } |
953 | | |
954 | | void |
955 | 29 | coap_session_connected(coap_session_t *session) { |
956 | 29 | if (session->state != COAP_SESSION_STATE_ESTABLISHED) { |
957 | 29 | coap_log_debug("***%s: session connected\n", |
958 | 29 | coap_session_str(session)); |
959 | 29 | if (session->state == COAP_SESSION_STATE_CSM) { |
960 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_SESSION_CONNECTED, session); |
961 | 0 | #if COAP_CLIENT_SUPPORT |
962 | 0 | coap_session_reestablished(session); |
963 | 0 | coap_reset_doing_first(session); |
964 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
965 | 0 | } |
966 | 29 | } |
967 | | |
968 | 29 | session->state = COAP_SESSION_STATE_ESTABLISHED; |
969 | 29 | session->partial_write = 0; |
970 | | |
971 | 29 | if (session->proto==COAP_PROTO_DTLS) { |
972 | 0 | session->tls_overhead = coap_dtls_get_overhead(session); |
973 | 0 | if (session->tls_overhead >= session->mtu) { |
974 | 0 | session->tls_overhead = session->mtu; |
975 | 0 | coap_log_err("DTLS overhead exceeds MTU\n"); |
976 | 0 | } |
977 | 0 | } |
978 | | |
979 | 29 | while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) { |
980 | 0 | ssize_t bytes_written; |
981 | 0 | coap_queue_t *q = session->delayqueue; |
982 | 0 | coap_address_t remote; |
983 | |
|
984 | 0 | if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) { |
985 | 0 | if (session->con_active >= COAP_NSTART(session)) |
986 | 0 | break; |
987 | 0 | session->con_active++; |
988 | 0 | } |
989 | | /* Take entry off the queue */ |
990 | 0 | session->delayqueue = q->next; |
991 | 0 | q->next = NULL; |
992 | |
|
993 | 0 | coap_address_copy(&remote, &session->addr_info.remote); |
994 | 0 | coap_address_copy(&session->addr_info.remote, &q->remote); |
995 | 0 | coap_log_debug("** %s: mid=0x%04x: transmitted after delay (2)\n", |
996 | 0 | coap_session_str(session), (int)q->id); |
997 | 0 | bytes_written = coap_session_send_pdu(session, q->pdu); |
998 | 0 | if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) { |
999 | 0 | if (coap_wait_ack(session->context, session, q) >= 0) |
1000 | 0 | q = NULL; |
1001 | 0 | } |
1002 | 0 | coap_address_copy(&session->addr_info.remote, &remote); |
1003 | 0 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
1004 | 0 | if (q) |
1005 | 0 | coap_delete_node_lkd(q); |
1006 | 0 | if (bytes_written < 0) |
1007 | 0 | break; |
1008 | 0 | } else if (q) { |
1009 | 0 | if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) { |
1010 | 0 | q->next = session->delayqueue; |
1011 | 0 | session->delayqueue = q; |
1012 | 0 | if (bytes_written > 0) |
1013 | 0 | session->partial_write = (size_t)bytes_written; |
1014 | 0 | break; |
1015 | 0 | } else { |
1016 | 0 | coap_delete_node_lkd(q); |
1017 | 0 | } |
1018 | 0 | } |
1019 | 0 | } |
1020 | 29 | } |
1021 | | |
1022 | | #if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG |
1023 | | static const char * |
1024 | 0 | coap_nack_name(coap_nack_reason_t reason) { |
1025 | 0 | switch (reason) { |
1026 | 0 | case COAP_NACK_TOO_MANY_RETRIES: |
1027 | 0 | return "COAP_NACK_TOO_MANY_RETRIES"; |
1028 | 0 | case COAP_NACK_NOT_DELIVERABLE: |
1029 | 0 | return "COAP_NACK_NOT_DELIVERABLE"; |
1030 | 0 | case COAP_NACK_RST: |
1031 | 0 | return "COAP_NACK_RST"; |
1032 | 0 | case COAP_NACK_TLS_FAILED: |
1033 | 0 | return "COAP_NACK_TLS_FAILED"; |
1034 | 0 | case COAP_NACK_ICMP_ISSUE: |
1035 | 0 | return "COAP_NACK_ICMP_ISSUE"; |
1036 | 0 | case COAP_NACK_BAD_RESPONSE: |
1037 | 0 | return "COAP_NACK_BAD_RESPONSE"; |
1038 | 0 | case COAP_NACK_TLS_LAYER_FAILED: |
1039 | 0 | return "COAP_NACK_TLS_LAYER_FAILED"; |
1040 | 0 | case COAP_NACK_WS_LAYER_FAILED: |
1041 | 0 | return "COAP_NACK_WS_LAYER_FAILED"; |
1042 | 0 | case COAP_NACK_WS_FAILED: |
1043 | 0 | return "COAP_NACK_WS_FAILED"; |
1044 | 0 | default: |
1045 | 0 | return "???"; |
1046 | 0 | } |
1047 | 0 | } |
1048 | | #endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */ |
1049 | | |
1050 | | void |
1051 | | coap_handle_nack(coap_session_t *session, |
1052 | | coap_pdu_t *sent, |
1053 | | const coap_nack_reason_t reason, |
1054 | 11 | const coap_mid_t mid) { |
1055 | 11 | if (session->context->nack_cb |
1056 | 0 | #if COAP_CLIENT_SUPPORT |
1057 | 0 | && !session->doing_first_pdu |
1058 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1059 | 11 | ) { |
1060 | 0 | coap_bin_const_t token = {0, NULL}; |
1061 | |
|
1062 | 0 | if (sent) { |
1063 | 0 | coap_check_update_token(session, sent); |
1064 | 0 | token = sent->actual_token; |
1065 | 0 | } |
1066 | 0 | coap_lock_callback(session->context->nack_cb(session, sent, reason, mid)); |
1067 | 0 | if (sent) { |
1068 | 0 | coap_update_token(sent, token.length, token.s); |
1069 | 0 | } |
1070 | 0 | } |
1071 | 11 | #if COAP_CLIENT_SUPPORT |
1072 | 11 | if (reason != COAP_NACK_ICMP_ISSUE) { |
1073 | 11 | session->doing_send_recv = 0; |
1074 | 11 | } |
1075 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1076 | 11 | } |
1077 | | |
1078 | | COAP_API void |
1079 | 0 | coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) { |
1080 | 0 | coap_lock_lock(return); |
1081 | 0 | coap_session_disconnected_lkd(session, reason); |
1082 | 0 | coap_lock_unlock(); |
1083 | 0 | } |
1084 | | |
1085 | | void |
1086 | 11 | coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason) { |
1087 | 11 | #if !COAP_DISABLE_TCP |
1088 | 11 | coap_session_state_t state = session->state; |
1089 | 11 | #endif /* !COAP_DISABLE_TCP */ |
1090 | 11 | coap_lg_xmit_t *lq, *ltmp; |
1091 | 11 | #if COAP_SERVER_SUPPORT |
1092 | 11 | coap_lg_srcv_t *sq, *stmp; |
1093 | 11 | #endif /* COAP_SERVER_SUPPORT */ |
1094 | 11 | #if COAP_CLIENT_SUPPORT |
1095 | 11 | coap_lg_crcv_t *cq, *etmp; |
1096 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1097 | 11 | int sent_nack = 0; |
1098 | 11 | coap_queue_t *q; |
1099 | | |
1100 | 11 | coap_lock_check_locked(); |
1101 | 11 | #if COAP_CLIENT_SUPPORT |
1102 | 11 | coap_session_failed(session); |
1103 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1104 | | |
1105 | 11 | q = session->context->sendqueue; |
1106 | 11 | while (q) { |
1107 | 0 | if (q->session == session) { |
1108 | | /* Take the first one */ |
1109 | 0 | coap_handle_nack(session, q->pdu, reason, q->id); |
1110 | 0 | sent_nack = 1; |
1111 | 0 | break; |
1112 | 0 | } |
1113 | 0 | q = q->next; |
1114 | 0 | } |
1115 | | |
1116 | 11 | while (session->delayqueue) { |
1117 | 0 | q = session->delayqueue; |
1118 | 0 | session->delayqueue = q->next; |
1119 | 0 | q->next = NULL; |
1120 | 0 | coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n", |
1121 | 0 | coap_session_str(session), q->id); |
1122 | 0 | if (q->pdu->type == COAP_MESSAGE_CON) { |
1123 | 0 | coap_handle_nack(session, q->pdu, reason, q->id); |
1124 | 0 | sent_nack = 1; |
1125 | 0 | } |
1126 | |
|
1127 | 0 | coap_delete_node_lkd(q); |
1128 | 0 | } |
1129 | 11 | #if COAP_CLIENT_SUPPORT |
1130 | 11 | session->doing_send_recv = 0; |
1131 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1132 | 11 | #if COAP_CLIENT_SUPPORT |
1133 | 11 | if (!sent_nack && session->lg_crcv) { |
1134 | | /* Take the first one */ |
1135 | 0 | coap_handle_nack(session, session->lg_crcv->sent_pdu, reason, |
1136 | 0 | session->lg_crcv->sent_pdu->mid); |
1137 | 0 | sent_nack = 1; |
1138 | 0 | } |
1139 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1140 | 11 | if (!sent_nack) { |
1141 | | /* Unable to determine which request disconnection was for */ |
1142 | 11 | coap_handle_nack(session, NULL, reason, 0); |
1143 | 11 | } |
1144 | 11 | coap_log_debug("***%s: session disconnected (%s)\n", |
1145 | 11 | coap_session_str(session), coap_nack_name(reason)); |
1146 | 11 | #if COAP_SERVER_SUPPORT |
1147 | 11 | coap_delete_observers(session->context, session); |
1148 | 11 | #endif /* COAP_SERVER_SUPPORT */ |
1149 | | |
1150 | 11 | if (session->proto == COAP_PROTO_UDP) |
1151 | 11 | session->state = COAP_SESSION_STATE_ESTABLISHED; |
1152 | 0 | else |
1153 | 0 | session->state = COAP_SESSION_STATE_NONE; |
1154 | | |
1155 | 11 | session->con_active = 0; |
1156 | | |
1157 | 11 | if (session->partial_pdu) { |
1158 | 0 | coap_delete_pdu_lkd(session->partial_pdu); |
1159 | 0 | session->partial_pdu = NULL; |
1160 | 0 | } |
1161 | 11 | session->partial_read = 0; |
1162 | | |
1163 | | /* Not done if nack handler called above */ |
1164 | 11 | while (session->delayqueue) { |
1165 | 0 | q = session->delayqueue; |
1166 | 0 | session->delayqueue = q->next; |
1167 | 0 | q->next = NULL; |
1168 | 0 | coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n", |
1169 | 0 | coap_session_str(session), q->id); |
1170 | 0 | #if COAP_CLIENT_SUPPORT |
1171 | 0 | session->doing_send_recv = 0; |
1172 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
1173 | 0 | coap_delete_node_lkd(q); |
1174 | 0 | } |
1175 | | |
1176 | 11 | #if COAP_CLIENT_SUPPORT |
1177 | 11 | if (!session->session_failed) { |
1178 | | /* Need to do this before (D)TLS and socket is closed down */ |
1179 | 11 | LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) { |
1180 | 0 | LL_DELETE(session->lg_crcv, cq); |
1181 | 0 | coap_block_delete_lg_crcv(session, cq); |
1182 | 0 | } |
1183 | 11 | } |
1184 | 11 | #endif /* COAP_CLIENT_SUPPORT */ |
1185 | 11 | LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) { |
1186 | 0 | LL_DELETE(session->lg_xmit, lq); |
1187 | 0 | coap_block_delete_lg_xmit(session, lq); |
1188 | 0 | } |
1189 | 11 | #if COAP_SERVER_SUPPORT |
1190 | 11 | LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) { |
1191 | 0 | LL_DELETE(session->lg_srcv, sq); |
1192 | 0 | coap_block_delete_lg_srcv(session, sq); |
1193 | 0 | } |
1194 | 11 | #endif /* COAP_SERVER_SUPPORT */ |
1195 | 11 | coap_cancel_session_messages(session->context, session, reason); |
1196 | | |
1197 | 11 | #if !COAP_DISABLE_TCP |
1198 | 11 | if (COAP_PROTO_RELIABLE(session->proto)) { |
1199 | 0 | if (coap_netif_available(session)) { |
1200 | 0 | coap_handle_event_lkd(session->context, |
1201 | 0 | state == COAP_SESSION_STATE_CONNECTING ? |
1202 | 0 | COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session); |
1203 | 0 | } |
1204 | 0 | #if COAP_CLIENT_SUPPORT |
1205 | 0 | if (state != COAP_SESSION_STATE_NONE && !session->session_failed) { |
1206 | 0 | coap_handle_event_lkd(session->context, |
1207 | 0 | state == COAP_SESSION_STATE_ESTABLISHED ? |
1208 | 0 | COAP_EVENT_SESSION_CLOSED : COAP_EVENT_SESSION_FAILED, session); |
1209 | 0 | } |
1210 | 0 | coap_reset_doing_first(session); |
1211 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
1212 | 0 | } |
1213 | 11 | #endif /* !COAP_DISABLE_TCP */ |
1214 | 11 | if (session->sock.lfunc[COAP_LAYER_SESSION].l_close) |
1215 | 11 | session->sock.lfunc[COAP_LAYER_SESSION].l_close(session); |
1216 | 11 | } |
1217 | | |
1218 | | #if COAP_CLIENT_SUPPORT |
1219 | | void |
1220 | 11 | coap_session_failed(coap_session_t *session) { |
1221 | 11 | if (session->context->reconnect_time && session->client_initiated) { |
1222 | 0 | if (session->sock.lfunc[COAP_LAYER_SESSION].l_close) |
1223 | 0 | session->sock.lfunc[COAP_LAYER_SESSION].l_close(session); |
1224 | 0 | session->session_failed = 1; |
1225 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_RECONNECT_FAILED, session); |
1226 | 0 | session->retry_count++; |
1227 | 0 | if (session->context->retry_count && session->retry_count >= session->context->retry_count) { |
1228 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_RECONNECT_NO_MORE, session); |
1229 | 0 | if (session->type != COAP_SESSION_TYPE_CLIENT) { |
1230 | 0 | coap_session_set_type_client_lkd(session, 0); |
1231 | 0 | coap_session_release_lkd(session); |
1232 | 0 | } |
1233 | 0 | } |
1234 | 0 | coap_ticks(&session->last_rx_tx); |
1235 | 0 | } |
1236 | 11 | } |
1237 | | #endif /* COAP_CLIENT_SUPPORT */ |
1238 | | |
1239 | | #if COAP_SERVER_SUPPORT |
1240 | | static void |
1241 | | coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto, |
1242 | 0 | const coap_addr_tuple_t *addr_info) { |
1243 | 0 | memset(addr_hash, 0, sizeof(coap_addr_hash_t)); |
1244 | 0 | coap_address_copy(&addr_hash->remote, &addr_info->remote); |
1245 | 0 | addr_hash->lport = coap_address_get_port(&addr_info->local); |
1246 | 0 | addr_hash->proto = proto; |
1247 | 0 | } |
1248 | | |
1249 | | /* Aids debugging when session is created */ |
1250 | | static coap_session_t *debug_session; |
1251 | | |
1252 | | coap_session_t * |
1253 | | coap_endpoint_get_session(coap_endpoint_t *endpoint, |
1254 | 0 | const coap_packet_t *packet, coap_tick_t now) { |
1255 | 0 | coap_session_t *session; |
1256 | 0 | coap_session_t *rtmp; |
1257 | 0 | unsigned int num_idle = 0; |
1258 | 0 | unsigned int num_hs = 0; |
1259 | 0 | coap_session_t *oldest = NULL; |
1260 | 0 | coap_session_t *oldest_hs = NULL; |
1261 | 0 | coap_addr_hash_t addr_hash; |
1262 | |
|
1263 | 0 | coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info); |
1264 | 0 | SESSIONS_FIND(endpoint->sessions, addr_hash, session); |
1265 | 0 | if (session) { |
1266 | | /* Maybe mcast or unicast IP address which is not in the hash */ |
1267 | 0 | coap_address_copy(&session->addr_info.local, &packet->addr_info.local); |
1268 | 0 | session->ifindex = packet->ifindex; |
1269 | 0 | session->last_rx_tx = now; |
1270 | 0 | return session; |
1271 | 0 | } |
1272 | | |
1273 | 0 | #if COAP_CLIENT_SUPPORT |
1274 | 0 | SESSIONS_FIND(endpoint->context->sessions, addr_hash, session); |
1275 | 0 | if (session) { |
1276 | | /* Maybe mcast or unicast IP address which is not in the hash */ |
1277 | 0 | coap_address_copy(&session->addr_info.local, &packet->addr_info.local); |
1278 | 0 | session->ifindex = packet->ifindex; |
1279 | 0 | session->last_rx_tx = now; |
1280 | 0 | return session; |
1281 | 0 | } |
1282 | | |
1283 | 0 | if (coap_is_mcast(&packet->addr_info.local)) { |
1284 | | /* Check if this a proxy client packet we sent on another socket */ |
1285 | 0 | SESSIONS_ITER(endpoint->context->sessions, session, rtmp) { |
1286 | 0 | if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) && |
1287 | 0 | coap_address_get_port(&session->addr_info.local) == |
1288 | 0 | coap_address_get_port(&packet->addr_info.remote)) { |
1289 | | /* Drop looped back packet to stop recursion / confusion */ |
1290 | 0 | return NULL; |
1291 | 0 | } |
1292 | 0 | } |
1293 | 0 | } |
1294 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
1295 | 0 | SESSIONS_ITER(endpoint->sessions, session, rtmp) { |
1296 | 0 | if (session->ref == 0 && session->delayqueue == NULL && |
1297 | 0 | session->lg_srcv == NULL && session->lg_xmit == NULL) { |
1298 | 0 | if ( |
1299 | 0 | #if COAP_CLIENT_SUPPORT |
1300 | 0 | !(session->client_initiated && endpoint->context->reconnect_time) && |
1301 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
1302 | 0 | session->type == COAP_SESSION_TYPE_SERVER) { |
1303 | 0 | ++num_idle; |
1304 | 0 | if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx) |
1305 | 0 | oldest = session; |
1306 | |
|
1307 | 0 | if (session->state == COAP_SESSION_STATE_HANDSHAKE) { |
1308 | 0 | ++num_hs; |
1309 | | /* See if this is a partial (D)TLS session set up |
1310 | | which needs to be cleared down to prevent DOS */ |
1311 | 0 | if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) { |
1312 | 0 | if (oldest_hs == NULL || |
1313 | 0 | session->last_rx_tx < oldest_hs->last_rx_tx) |
1314 | 0 | oldest_hs = session; |
1315 | 0 | } |
1316 | 0 | } |
1317 | 0 | } else if (session->type == COAP_SESSION_TYPE_HELLO) { |
1318 | 0 | ++num_hs; |
1319 | | /* See if this is a partial (D)TLS session set up for Client Hello |
1320 | | which needs to be cleared down to prevent DOS */ |
1321 | 0 | if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) { |
1322 | 0 | if (oldest_hs == NULL || |
1323 | 0 | session->last_rx_tx < oldest_hs->last_rx_tx) |
1324 | 0 | oldest_hs = session; |
1325 | 0 | } |
1326 | 0 | } |
1327 | 0 | } |
1328 | 0 | } |
1329 | |
|
1330 | 0 | if (endpoint->context->max_idle_sessions > 0 && |
1331 | 0 | num_idle >= endpoint->context->max_idle_sessions) { |
1332 | 0 | coap_handle_event_lkd(oldest->context, COAP_EVENT_SERVER_SESSION_DEL, oldest); |
1333 | 0 | coap_session_free(oldest); |
1334 | 0 | } else if (oldest_hs) { |
1335 | 0 | coap_log_warn("***%s: Incomplete session timed out\n", |
1336 | 0 | coap_session_str(oldest_hs)); |
1337 | 0 | coap_handle_event_lkd(oldest_hs->context, COAP_EVENT_SERVER_SESSION_DEL, oldest_hs); |
1338 | 0 | coap_session_free(oldest_hs); |
1339 | 0 | } |
1340 | |
|
1341 | 0 | if (num_hs > (endpoint->context->max_handshake_sessions ? |
1342 | 0 | endpoint->context->max_handshake_sessions : |
1343 | 0 | COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS)) { |
1344 | | /* Maxed out on number of sessions in (D)TLS negotiation state */ |
1345 | 0 | coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too " |
1346 | 0 | "large. New request ignored\n"); |
1347 | 0 | return NULL; |
1348 | 0 | } |
1349 | | |
1350 | 0 | if (endpoint->proto == COAP_PROTO_DTLS) { |
1351 | | /* |
1352 | | * Need to check that this actually is a Client Hello before wasting |
1353 | | * time allocating and then freeing off session. |
1354 | | */ |
1355 | | |
1356 | | /* |
1357 | | * Generic header structure of the DTLS record layer. |
1358 | | * typedef struct __attribute__((__packed__)) { |
1359 | | * uint8_t content_type; content type of the included message |
1360 | | * uint16_t version; Protocol version |
1361 | | * uint16_t epoch; counter for cipher state changes |
1362 | | * uint8_t sequence_number[6]; sequence number |
1363 | | * uint16_t length; length of the following fragment |
1364 | | * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE |
1365 | | * } dtls_record_handshake_t; |
1366 | | */ |
1367 | 0 | #define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */ |
1368 | 0 | #define DTLS_CT_ALERT 21 /* Content Type Alert */ |
1369 | 0 | #define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */ |
1370 | 0 | #define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */ |
1371 | 0 | #define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */ |
1372 | 0 | #define DTLS_CT_CID 25 /* Content Type Connection ID */ |
1373 | 0 | #define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */ |
1374 | 0 | #define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */ |
1375 | |
|
1376 | 0 | const uint8_t *payload = (const uint8_t *)packet->payload; |
1377 | 0 | size_t length = packet->length; |
1378 | 0 | if (length < (OFF_HANDSHAKE_TYPE + 1)) { |
1379 | 0 | coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%" PRIuS " < %d) dropped\n", |
1380 | 0 | payload[OFF_CONTENT_TYPE], length, |
1381 | 0 | OFF_HANDSHAKE_TYPE + 1); |
1382 | 0 | return NULL; |
1383 | 0 | } |
1384 | 0 | if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 || |
1385 | 0 | payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) { |
1386 | | /* Client may have changed its IP address */ |
1387 | 0 | int changed = 0; |
1388 | |
|
1389 | 0 | SESSIONS_ITER(endpoint->sessions, session, rtmp) { |
1390 | 0 | if (session->client_cid) { |
1391 | 0 | if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 && |
1392 | 0 | length > (OFF_CID_DTLS13 + session->client_cid->length) && |
1393 | 0 | memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13], |
1394 | 0 | session->client_cid->length) == 0) || |
1395 | 0 | (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID && |
1396 | 0 | length > (OFF_CID + session->client_cid->length) && |
1397 | 0 | memcmp(session->client_cid->s, &payload[OFF_CID], |
1398 | 0 | session->client_cid->length) == 0)) { |
1399 | | /* Updating IP address */ |
1400 | 0 | coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session)); |
1401 | 0 | SESSIONS_DELETE(endpoint->sessions, session); |
1402 | 0 | session->addr_info = packet->addr_info; |
1403 | 0 | memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash)); |
1404 | 0 | SESSIONS_ADD(endpoint->sessions, session); |
1405 | 0 | coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session)); |
1406 | 0 | return session; |
1407 | 0 | } |
1408 | 0 | } |
1409 | 0 | } |
1410 | 0 | if (!changed) { |
1411 | 0 | coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n"); |
1412 | 0 | return NULL; |
1413 | 0 | } |
1414 | 0 | } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE || |
1415 | 0 | payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) { |
1416 | | /* only log if not a late alert */ |
1417 | 0 | if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT) |
1418 | 0 | coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n", |
1419 | 0 | payload[OFF_CONTENT_TYPE], |
1420 | 0 | payload[OFF_HANDSHAKE_TYPE]); |
1421 | 0 | return NULL; |
1422 | 0 | } |
1423 | 0 | } |
1424 | | |
1425 | 0 | session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER, |
1426 | 0 | &addr_hash, &packet->addr_info.local, |
1427 | 0 | &packet->addr_info.remote, |
1428 | 0 | packet->ifindex, endpoint->context, endpoint); |
1429 | | /* Aids debugging endpoint created session */ |
1430 | 0 | debug_session = session; |
1431 | 0 | if (session) { |
1432 | 0 | session->last_rx_tx = now; |
1433 | 0 | memcpy(session->sock.lfunc, endpoint->sock.lfunc, |
1434 | 0 | sizeof(session->sock.lfunc)); |
1435 | 0 | if (endpoint->proto == COAP_PROTO_UDP) |
1436 | 0 | session->state = COAP_SESSION_STATE_ESTABLISHED; |
1437 | 0 | else if (endpoint->proto == COAP_PROTO_DTLS) { |
1438 | 0 | session->type = COAP_SESSION_TYPE_HELLO; |
1439 | 0 | } |
1440 | 0 | SESSIONS_ADD(endpoint->sessions, session); |
1441 | 0 | coap_log_debug("***%s: session %p: new incoming session\n", |
1442 | 0 | coap_session_str(session), (void *)session); |
1443 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); |
1444 | 0 | } |
1445 | 0 | return session; |
1446 | 0 | } |
1447 | | |
1448 | | coap_session_t * |
1449 | | coap_session_new_dtls_session(coap_session_t *session, |
1450 | 0 | coap_tick_t now) { |
1451 | 0 | if (session) { |
1452 | 0 | session->last_rx_tx = now; |
1453 | 0 | session->type = COAP_SESSION_TYPE_SERVER; |
1454 | 0 | coap_dtls_establish(session); |
1455 | 0 | } |
1456 | 0 | return session; |
1457 | 0 | } |
1458 | | #endif /* COAP_SERVER_SUPPORT */ |
1459 | | |
1460 | | #if COAP_CLIENT_SUPPORT |
1461 | | static coap_session_t * |
1462 | | coap_session_create_client(coap_context_t *ctx, |
1463 | | const coap_address_t *local_if, |
1464 | | const coap_address_t *server, |
1465 | | coap_proto_t proto, |
1466 | | void *app_data, |
1467 | | coap_app_data_free_callback_t callback, |
1468 | 29 | coap_str_const_t *ws_host) { |
1469 | 29 | coap_session_t *session = NULL; |
1470 | 29 | int default_port = COAP_DEFAULT_PORT; |
1471 | | |
1472 | 29 | assert(server); |
1473 | | |
1474 | 29 | switch (proto) { |
1475 | 29 | case COAP_PROTO_UDP: |
1476 | 29 | default_port = COAP_DEFAULT_PORT; |
1477 | 29 | break; |
1478 | 0 | case COAP_PROTO_DTLS: |
1479 | 0 | if (!coap_dtls_is_supported()) { |
1480 | 0 | coap_log_crit("coap_new_client_session*: DTLS not supported\n"); |
1481 | 0 | return NULL; |
1482 | 0 | } |
1483 | 0 | default_port = COAPS_DEFAULT_PORT; |
1484 | 0 | break; |
1485 | 0 | case COAP_PROTO_TCP: |
1486 | 0 | if (!coap_tcp_is_supported()) { |
1487 | 0 | coap_log_crit("coap_new_client_session*: TCP not supported\n"); |
1488 | 0 | return NULL; |
1489 | 0 | } |
1490 | 0 | default_port = COAP_DEFAULT_PORT; |
1491 | 0 | break; |
1492 | 0 | case COAP_PROTO_TLS: |
1493 | 0 | if (!coap_tls_is_supported()) { |
1494 | 0 | coap_log_crit("coap_new_client_session*: TLS not supported\n"); |
1495 | 0 | return NULL; |
1496 | 0 | } |
1497 | 0 | default_port = COAPS_DEFAULT_PORT; |
1498 | 0 | break; |
1499 | 0 | case COAP_PROTO_WS: |
1500 | 0 | if (!coap_ws_is_supported()) { |
1501 | 0 | coap_log_crit("coap_new_client_session*: WS not supported\n"); |
1502 | 0 | return NULL; |
1503 | 0 | } |
1504 | 0 | default_port = 80; |
1505 | 0 | break; |
1506 | 0 | case COAP_PROTO_WSS: |
1507 | 0 | if (!coap_wss_is_supported()) { |
1508 | 0 | coap_log_crit("coap_new_client_session*: WSS not supported\n"); |
1509 | 0 | return NULL; |
1510 | 0 | } |
1511 | 0 | default_port = 443; |
1512 | 0 | break; |
1513 | 0 | case COAP_PROTO_NONE: |
1514 | 0 | case COAP_PROTO_LAST: |
1515 | 0 | default: |
1516 | 0 | assert(0); |
1517 | 0 | return NULL; |
1518 | 29 | } |
1519 | 29 | session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL, |
1520 | 29 | local_if, server, 0, ctx, NULL); |
1521 | 29 | if (!session) |
1522 | 0 | goto error; |
1523 | | |
1524 | 29 | coap_session_reference_lkd(session); |
1525 | 29 | session->sock.session = session; |
1526 | 29 | memcpy(&session->sock.lfunc, coap_layers_coap[proto], |
1527 | 29 | sizeof(session->sock.lfunc)); |
1528 | | |
1529 | 29 | session->app_data = app_data; |
1530 | 29 | session->app_cb = app_data ? callback : NULL; |
1531 | 29 | #if COAP_WS_SUPPORT |
1532 | 29 | if (ws_host) { |
1533 | 0 | session->ws_host = coap_new_str_const(ws_host->s, ws_host->length); |
1534 | 0 | } |
1535 | | #else /* ! COAP_WS_SUPPORT */ |
1536 | | (void)ws_host; |
1537 | | #endif /* ! COAP_WS_SUPPORT */ |
1538 | | |
1539 | 29 | if (COAP_PROTO_NOT_RELIABLE(proto)) { |
1540 | 29 | coap_session_t *s, *rtmp; |
1541 | 29 | if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) { |
1542 | 0 | goto error; |
1543 | 0 | } |
1544 | | /* Check that this is not a duplicate 4-tuple */ |
1545 | 29 | SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) { |
1546 | 0 | if (COAP_PROTO_NOT_RELIABLE(s->proto) && |
1547 | 0 | coap_address_equals(&session->addr_info.local, |
1548 | 0 | &s->addr_info.local) && |
1549 | 0 | coap_address_equals(&session->addr_info.remote, |
1550 | 0 | &s->addr_info.remote)) { |
1551 | 0 | coap_log_warn("***%s: session %p: duplicate - already exists\n", |
1552 | 0 | coap_session_str(session), (void *)session); |
1553 | 0 | goto error; |
1554 | 0 | } |
1555 | 0 | } |
1556 | | #ifdef WITH_CONTIKI |
1557 | | session->sock.context = ctx; |
1558 | | #endif /* WITH_CONTIKI */ |
1559 | 29 | #if !COAP_DISABLE_TCP |
1560 | 29 | } else if (COAP_PROTO_RELIABLE(proto)) { |
1561 | 0 | if (!coap_netif_strm_connect1(session, local_if, server, default_port)) { |
1562 | 0 | goto error; |
1563 | 0 | } |
1564 | 0 | #endif /* !COAP_DISABLE_TCP */ |
1565 | 0 | } |
1566 | | |
1567 | 29 | session->sock.session = session; |
1568 | 29 | #ifdef COAP_EPOLL_SUPPORT |
1569 | 29 | coap_epoll_ctl_add(&session->sock, |
1570 | 29 | EPOLLIN | |
1571 | 29 | ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ? |
1572 | 29 | EPOLLOUT : 0), |
1573 | 29 | __func__); |
1574 | 29 | #endif /* COAP_EPOLL_SUPPORT */ |
1575 | | |
1576 | 29 | session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_WANT_READ; |
1577 | 29 | if (local_if) |
1578 | 0 | session->sock.flags |= COAP_SOCKET_BOUND; |
1579 | 29 | #if COAP_SERVER_SUPPORT |
1580 | 29 | if (ctx->proxy_uri_resource) |
1581 | 0 | session->proxy_session = 1; |
1582 | 29 | #endif /* COAP_SERVER_SUPPORT */ |
1583 | 29 | SESSIONS_ADD(ctx->sessions, session); |
1584 | 29 | return session; |
1585 | | |
1586 | 0 | error: |
1587 | | /* |
1588 | | * Need to add in the session as coap_session_release_lkd() |
1589 | | * will call SESSIONS_DELETE in coap_session_free(). |
1590 | | */ |
1591 | 0 | if (session) |
1592 | 0 | SESSIONS_ADD(ctx->sessions, session); |
1593 | 0 | coap_session_release_lkd(session); |
1594 | 0 | return NULL; |
1595 | 0 | } |
1596 | | |
1597 | | static void |
1598 | 29 | coap_session_check_connect(coap_session_t *session) { |
1599 | 29 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
1600 | 29 | session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session); |
1601 | 29 | } |
1602 | 29 | #if !COAP_DISABLE_TCP |
1603 | 29 | if (COAP_PROTO_RELIABLE(session->proto)) { |
1604 | 0 | if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) { |
1605 | 0 | session->state = COAP_SESSION_STATE_CONNECTING; |
1606 | 0 | if (session->client_initiated) { |
1607 | 0 | session->doing_first = 1; |
1608 | 0 | } |
1609 | 0 | } else { |
1610 | | /* Initial connect worked immediately */ |
1611 | 0 | session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session); |
1612 | 0 | } |
1613 | 0 | } |
1614 | 29 | #endif /* !COAP_DISABLE_TCP */ |
1615 | 29 | coap_ticks(&session->last_rx_tx); |
1616 | 29 | } |
1617 | | #endif /* COAP_CLIENT_SUPPORT */ |
1618 | | |
1619 | | void |
1620 | 29 | coap_session_establish(coap_session_t *session) { |
1621 | 29 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) |
1622 | 29 | coap_session_connected(session); |
1623 | 29 | #if !COAP_DISABLE_TCP |
1624 | 29 | if (COAP_PROTO_RELIABLE(session->proto)) |
1625 | 0 | coap_session_send_csm(session); |
1626 | 29 | #endif /* !COAP_DISABLE_TCP */ |
1627 | 29 | } |
1628 | | |
1629 | | #if COAP_CLIENT_SUPPORT |
1630 | | int |
1631 | 0 | coap_session_reconnect(coap_session_t *session) { |
1632 | 0 | int default_port = COAP_DEFAULT_PORT; |
1633 | |
|
1634 | 0 | if (session->sock.lfunc[COAP_LAYER_SESSION].l_close && !session->session_failed) |
1635 | 0 | session->sock.lfunc[COAP_LAYER_SESSION].l_close(session); |
1636 | |
|
1637 | 0 | switch (session->proto) { |
1638 | 0 | case COAP_PROTO_UDP: |
1639 | 0 | default_port = COAP_DEFAULT_PORT; |
1640 | 0 | break; |
1641 | 0 | case COAP_PROTO_DTLS: |
1642 | 0 | default_port = COAPS_DEFAULT_PORT; |
1643 | 0 | break; |
1644 | 0 | case COAP_PROTO_TCP: |
1645 | 0 | default_port = COAP_DEFAULT_PORT; |
1646 | 0 | break; |
1647 | 0 | case COAP_PROTO_TLS: |
1648 | 0 | default_port = COAPS_DEFAULT_PORT; |
1649 | 0 | break; |
1650 | 0 | case COAP_PROTO_WS: |
1651 | 0 | default_port = 80; |
1652 | 0 | break; |
1653 | 0 | case COAP_PROTO_WSS: |
1654 | 0 | default_port = 443; |
1655 | 0 | break; |
1656 | 0 | case COAP_PROTO_NONE: |
1657 | 0 | case COAP_PROTO_LAST: |
1658 | 0 | default: |
1659 | 0 | assert(0); |
1660 | 0 | return 0; |
1661 | 0 | } |
1662 | 0 | session->sock.session = session; |
1663 | 0 | coap_log_debug("***%s: trying to reconnect\n", coap_session_str(session)); |
1664 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_RECONNECT_STARTED, session); |
1665 | 0 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
1666 | 0 | session->state = COAP_SESSION_STATE_NONE; |
1667 | 0 | if (!coap_netif_dgrm_connect(session, &session->local_reconnect, &session->addr_info.remote, |
1668 | 0 | default_port)) { |
1669 | 0 | goto error; |
1670 | 0 | } |
1671 | 0 | coap_session_reestablished(session); |
1672 | | #ifdef WITH_CONTIKI |
1673 | | session->sock.context = session->context; |
1674 | | #endif /* WITH_CONTIKI */ |
1675 | 0 | #if !COAP_DISABLE_TCP |
1676 | 0 | } else if (COAP_PROTO_RELIABLE(session->proto)) { |
1677 | 0 | if (!coap_netif_strm_connect1(session, &session->local_reconnect, &session->addr_info.remote, |
1678 | 0 | default_port)) { |
1679 | 0 | goto error; |
1680 | 0 | } |
1681 | 0 | #endif /* !COAP_DISABLE_TCP */ |
1682 | 0 | } else { |
1683 | 0 | goto error; |
1684 | 0 | } |
1685 | 0 | #ifdef COAP_EPOLL_SUPPORT |
1686 | 0 | coap_epoll_ctl_add(&session->sock, |
1687 | 0 | EPOLLIN | |
1688 | 0 | ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ? |
1689 | 0 | EPOLLOUT : 0), |
1690 | 0 | __func__); |
1691 | 0 | #endif /* COAP_EPOLL_SUPPORT */ |
1692 | |
|
1693 | 0 | session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_WANT_READ; |
1694 | 0 | session->sock.flags |= COAP_SOCKET_BOUND; |
1695 | 0 | coap_session_check_connect(session); |
1696 | 0 | return 1; |
1697 | 0 | error: |
1698 | 0 | return 0; |
1699 | 0 | } |
1700 | | |
1701 | | void |
1702 | 0 | coap_session_reestablished(coap_session_t *session) { |
1703 | 0 | coap_lg_crcv_t *lg_crcv, *etmp; |
1704 | |
|
1705 | 0 | if (!session->session_failed) |
1706 | 0 | return; |
1707 | 0 | coap_log_debug("***%s: session re-established\n", |
1708 | 0 | coap_session_str(session)); |
1709 | 0 | session->session_failed = 0; |
1710 | 0 | session->retry_count = 0; |
1711 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_RECONNECT_SUCCESS, session); |
1712 | 0 | LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) { |
1713 | 0 | coap_pdu_reference_lkd(lg_crcv->sent_pdu); |
1714 | 0 | coap_send_internal(session, lg_crcv->sent_pdu, NULL); |
1715 | 0 | } |
1716 | 0 | } |
1717 | | |
1718 | | COAP_API coap_session_t * |
1719 | | coap_new_client_session(coap_context_t *ctx, |
1720 | | const coap_address_t *local_if, |
1721 | | const coap_address_t *server, |
1722 | 29 | coap_proto_t proto) { |
1723 | 29 | coap_session_t *session; |
1724 | | |
1725 | 29 | coap_lock_lock(return NULL); |
1726 | 29 | session = coap_new_client_session3_lkd(ctx, local_if, server, proto, NULL, NULL, NULL); |
1727 | 29 | coap_lock_unlock(); |
1728 | 29 | return session; |
1729 | 29 | } |
1730 | | |
1731 | | COAP_API coap_session_t * |
1732 | | coap_new_client_session3(coap_context_t *ctx, |
1733 | | const coap_address_t *local_if, |
1734 | | const coap_address_t *server, |
1735 | | coap_proto_t proto, |
1736 | | void *app_data, |
1737 | | coap_app_data_free_callback_t callback, |
1738 | 0 | coap_str_const_t *ws_host) { |
1739 | 0 | coap_session_t *session; |
1740 | |
|
1741 | 0 | coap_lock_lock(return NULL); |
1742 | 0 | session = coap_new_client_session3_lkd(ctx, local_if, server, proto, app_data, callback, ws_host); |
1743 | 0 | coap_lock_unlock(); |
1744 | 0 | return session; |
1745 | 0 | } |
1746 | | |
1747 | | coap_session_t * |
1748 | | coap_new_client_session3_lkd(coap_context_t *ctx, |
1749 | | const coap_address_t *local_if, |
1750 | | const coap_address_t *server, |
1751 | | coap_proto_t proto, |
1752 | | void *app_data, |
1753 | | coap_app_data_free_callback_t callback, |
1754 | 29 | coap_str_const_t *ws_host) { |
1755 | 29 | coap_session_t *session; |
1756 | | |
1757 | 29 | coap_lock_check_locked(); |
1758 | 29 | session = coap_session_create_client(ctx, local_if, server, |
1759 | 29 | proto, app_data, callback, ws_host); |
1760 | 29 | if (session) { |
1761 | 29 | coap_log_debug("***%s: session %p: created outgoing session\n", |
1762 | 29 | coap_session_str(session), (void *)session); |
1763 | 29 | coap_session_check_connect(session); |
1764 | 29 | } |
1765 | 29 | return session; |
1766 | 29 | } |
1767 | | |
1768 | | COAP_API coap_session_t * |
1769 | | coap_new_client_session_psk(coap_context_t *ctx, |
1770 | | const coap_address_t *local_if, |
1771 | | const coap_address_t *server, |
1772 | | coap_proto_t proto, const char *identity, |
1773 | 0 | const uint8_t *key, unsigned key_len) { |
1774 | 0 | coap_session_t *session; |
1775 | |
|
1776 | 0 | coap_lock_lock(return NULL); |
1777 | 0 | session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len); |
1778 | 0 | coap_lock_unlock(); |
1779 | 0 | return session; |
1780 | 0 | } |
1781 | | |
1782 | | coap_session_t * |
1783 | | coap_new_client_session_psk_lkd(coap_context_t *ctx, |
1784 | | const coap_address_t *local_if, |
1785 | | const coap_address_t *server, |
1786 | | coap_proto_t proto, const char *identity, |
1787 | 0 | const uint8_t *key, unsigned key_len) { |
1788 | 0 | coap_dtls_cpsk_t setup_data; |
1789 | |
|
1790 | 0 | coap_lock_check_locked(); |
1791 | 0 | memset(&setup_data, 0, sizeof(setup_data)); |
1792 | 0 | setup_data.version = COAP_DTLS_CPSK_SETUP_VERSION; |
1793 | |
|
1794 | 0 | if (identity) { |
1795 | 0 | setup_data.psk_info.identity.s = (const uint8_t *)identity; |
1796 | 0 | setup_data.psk_info.identity.length = strlen(identity); |
1797 | 0 | } |
1798 | |
|
1799 | 0 | if (key && key_len > 0) { |
1800 | 0 | setup_data.psk_info.key.s = key; |
1801 | 0 | setup_data.psk_info.key.length = key_len; |
1802 | 0 | } |
1803 | |
|
1804 | 0 | return coap_new_client_session_psk3_lkd(ctx, local_if, server, |
1805 | 0 | proto, &setup_data, NULL, NULL, NULL); |
1806 | 0 | } |
1807 | | |
1808 | | /* |
1809 | | * Check the validity of the SNI to send to the server. |
1810 | | * |
1811 | | * https://datatracker.ietf.org/doc/html/rfc6066#section-3 |
1812 | | * Literal IPv4 and IPv6 addresses are not permitted in "HostName". |
1813 | | */ |
1814 | | static void |
1815 | 0 | coap_sanitize_client_sni(char **client_sni) { |
1816 | 0 | char *cp; |
1817 | |
|
1818 | 0 | if (*client_sni == NULL) |
1819 | 0 | return; |
1820 | | |
1821 | 0 | cp = *client_sni; |
1822 | 0 | switch (*cp) { |
1823 | 0 | case '0': |
1824 | 0 | case '1': |
1825 | 0 | case '2': |
1826 | 0 | case '3': |
1827 | 0 | case '4': |
1828 | 0 | case '5': |
1829 | 0 | case '6': |
1830 | 0 | case '7': |
1831 | 0 | case '8': |
1832 | 0 | case '9': |
1833 | 0 | case 'a': |
1834 | 0 | case 'b': |
1835 | 0 | case 'c': |
1836 | 0 | case 'd': |
1837 | 0 | case 'e': |
1838 | 0 | case 'f': |
1839 | 0 | case 'A': |
1840 | 0 | case 'B': |
1841 | 0 | case 'C': |
1842 | 0 | case 'D': |
1843 | 0 | case 'E': |
1844 | 0 | case 'F': |
1845 | 0 | case ':': |
1846 | 0 | break; |
1847 | 0 | case '\000': |
1848 | | /* Empty entry invalid */ |
1849 | 0 | *client_sni = NULL; |
1850 | 0 | return; |
1851 | 0 | default: |
1852 | | /* Does not start with a hex digit or : - not literal IP. */ |
1853 | 0 | return; |
1854 | 0 | } |
1855 | | /* Check for IPv4 */ |
1856 | 0 | while (*cp) { |
1857 | 0 | switch (*cp) { |
1858 | 0 | case '0': |
1859 | 0 | case '1': |
1860 | 0 | case '2': |
1861 | 0 | case '3': |
1862 | 0 | case '4': |
1863 | 0 | case '5': |
1864 | 0 | case '6': |
1865 | 0 | case '7': |
1866 | 0 | case '8': |
1867 | 0 | case '9': |
1868 | 0 | case '.': |
1869 | 0 | break; |
1870 | 0 | default: |
1871 | | /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */ |
1872 | 0 | goto check_ipv6; |
1873 | 0 | } |
1874 | 0 | cp++; |
1875 | 0 | } |
1876 | | /* IPv4 address - not allowed. */ |
1877 | 0 | *client_sni = NULL; |
1878 | 0 | return; |
1879 | | |
1880 | 0 | check_ipv6: |
1881 | | /* Check for IPv6 */ |
1882 | 0 | cp = *client_sni; |
1883 | 0 | while (*cp) { |
1884 | 0 | switch (*cp) { |
1885 | 0 | case '0': |
1886 | 0 | case '1': |
1887 | 0 | case '2': |
1888 | 0 | case '3': |
1889 | 0 | case '4': |
1890 | 0 | case '5': |
1891 | 0 | case '6': |
1892 | 0 | case '7': |
1893 | 0 | case '8': |
1894 | 0 | case '9': |
1895 | 0 | case 'a': |
1896 | 0 | case 'b': |
1897 | 0 | case 'c': |
1898 | 0 | case 'd': |
1899 | 0 | case 'e': |
1900 | 0 | case 'f': |
1901 | 0 | case 'A': |
1902 | 0 | case 'B': |
1903 | 0 | case 'C': |
1904 | 0 | case 'D': |
1905 | 0 | case 'E': |
1906 | 0 | case 'F': |
1907 | 0 | case ':': |
1908 | 0 | break; |
1909 | 0 | case '%': |
1910 | | /* Start of i/f specification, Previous is IPv6 */ |
1911 | 0 | *client_sni = NULL; |
1912 | 0 | return; |
1913 | 0 | default: |
1914 | | /* Not of format xx:xx::xx. */ |
1915 | 0 | return; |
1916 | 0 | } |
1917 | 0 | cp++; |
1918 | 0 | } |
1919 | 0 | *client_sni = NULL; |
1920 | 0 | return; |
1921 | 0 | } |
1922 | | |
1923 | | COAP_API coap_session_t * |
1924 | | coap_new_client_session_psk2(coap_context_t *ctx, |
1925 | | const coap_address_t *local_if, |
1926 | | const coap_address_t *server, |
1927 | | coap_proto_t proto, |
1928 | 0 | coap_dtls_cpsk_t *setup_data) { |
1929 | 0 | coap_session_t *session; |
1930 | |
|
1931 | 0 | coap_lock_lock(return NULL); |
1932 | 0 | session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, setup_data, |
1933 | 0 | NULL, NULL, NULL); |
1934 | 0 | coap_lock_unlock(); |
1935 | 0 | return session; |
1936 | 0 | } |
1937 | | |
1938 | | COAP_API coap_session_t * |
1939 | | coap_new_client_session_psk3(coap_context_t *ctx, |
1940 | | const coap_address_t *local_if, |
1941 | | const coap_address_t *server, |
1942 | | coap_proto_t proto, |
1943 | | coap_dtls_cpsk_t *setup_data, |
1944 | | void *app_data, |
1945 | | coap_app_data_free_callback_t callback, |
1946 | 0 | coap_str_const_t *ws_host) { |
1947 | 0 | coap_session_t *session; |
1948 | |
|
1949 | 0 | coap_lock_lock(return NULL); |
1950 | 0 | session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, |
1951 | 0 | setup_data, app_data, callback, ws_host); |
1952 | 0 | coap_lock_unlock(); |
1953 | 0 | return session; |
1954 | 0 | } |
1955 | | |
1956 | | coap_session_t * |
1957 | | coap_new_client_session_psk3_lkd(coap_context_t *ctx, |
1958 | | const coap_address_t *local_if, |
1959 | | const coap_address_t *server, |
1960 | | coap_proto_t proto, |
1961 | | coap_dtls_cpsk_t *setup_data, |
1962 | | void *app_data, |
1963 | | coap_app_data_free_callback_t callback, |
1964 | 0 | coap_str_const_t *ws_host) { |
1965 | 0 | coap_session_t *session; |
1966 | |
|
1967 | 0 | coap_lock_check_locked(); |
1968 | 0 | session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host); |
1969 | |
|
1970 | 0 | if (!session || !setup_data) |
1971 | 0 | return NULL; |
1972 | | |
1973 | 0 | session->cpsk_setup_data = *setup_data; |
1974 | 0 | if (setup_data->psk_info.identity.s) { |
1975 | 0 | session->psk_identity = |
1976 | 0 | coap_new_bin_const(setup_data->psk_info.identity.s, |
1977 | 0 | setup_data->psk_info.identity.length); |
1978 | 0 | if (!session->psk_identity) { |
1979 | 0 | coap_log_warn("Cannot store session Identity (PSK)\n"); |
1980 | 0 | coap_session_release_lkd(session); |
1981 | 0 | return NULL; |
1982 | 0 | } |
1983 | 0 | } else if (coap_dtls_is_supported() || coap_tls_is_supported()) { |
1984 | 0 | coap_log_warn("Identity (PSK) not defined\n"); |
1985 | 0 | coap_session_release_lkd(session); |
1986 | 0 | return NULL; |
1987 | 0 | } |
1988 | | |
1989 | 0 | if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) { |
1990 | 0 | session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s, |
1991 | 0 | setup_data->psk_info.key.length); |
1992 | 0 | if (!session->psk_key) { |
1993 | 0 | coap_log_warn("Cannot store session pre-shared key (PSK)\n"); |
1994 | 0 | coap_session_release_lkd(session); |
1995 | 0 | return NULL; |
1996 | 0 | } |
1997 | 0 | } else if (coap_dtls_is_supported() || coap_tls_is_supported()) { |
1998 | 0 | coap_log_warn("Pre-shared key (PSK) not defined\n"); |
1999 | 0 | coap_session_release_lkd(session); |
2000 | 0 | return NULL; |
2001 | 0 | } |
2002 | | |
2003 | 0 | coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni); |
2004 | |
|
2005 | 0 | if (coap_dtls_is_supported() || coap_tls_is_supported()) { |
2006 | 0 | if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) { |
2007 | 0 | coap_session_release_lkd(session); |
2008 | 0 | return NULL; |
2009 | 0 | } |
2010 | 0 | } |
2011 | 0 | coap_log_debug("***%s: new outgoing session\n", |
2012 | 0 | coap_session_str(session)); |
2013 | 0 | coap_session_check_connect(session); |
2014 | 0 | return session; |
2015 | 0 | } |
2016 | | #endif /* COAP_CLIENT_SUPPORT */ |
2017 | | |
2018 | | int |
2019 | | coap_session_refresh_psk_hint(coap_session_t *session, |
2020 | | const coap_bin_const_t *psk_hint |
2021 | 0 | ) { |
2022 | | /* We may be refreshing the hint with the same hint */ |
2023 | 0 | coap_bin_const_t *old_psk_hint = session->psk_hint; |
2024 | |
|
2025 | 0 | if (psk_hint && psk_hint->s) { |
2026 | 0 | if (session->psk_hint) { |
2027 | 0 | if (coap_binary_equal(session->psk_hint, psk_hint)) |
2028 | 0 | return 1; |
2029 | 0 | } |
2030 | 0 | session->psk_hint = coap_new_bin_const(psk_hint->s, |
2031 | 0 | psk_hint->length); |
2032 | 0 | if (!session->psk_hint) { |
2033 | 0 | coap_log_err("No memory to store identity hint (PSK)\n"); |
2034 | 0 | if (old_psk_hint) |
2035 | 0 | coap_delete_bin_const(old_psk_hint); |
2036 | 0 | return 0; |
2037 | 0 | } |
2038 | 0 | } else { |
2039 | 0 | session->psk_hint = NULL; |
2040 | 0 | } |
2041 | 0 | if (old_psk_hint) |
2042 | 0 | coap_delete_bin_const(old_psk_hint); |
2043 | |
|
2044 | 0 | return 1; |
2045 | 0 | } |
2046 | | |
2047 | | int |
2048 | | coap_session_refresh_psk_key(coap_session_t *session, |
2049 | | const coap_bin_const_t *psk_key |
2050 | 0 | ) { |
2051 | | /* We may be refreshing the key with the same key */ |
2052 | 0 | coap_bin_const_t *old_psk_key = session->psk_key; |
2053 | |
|
2054 | 0 | if (psk_key && psk_key->s) { |
2055 | 0 | if (session->psk_key) { |
2056 | 0 | if (coap_binary_equal(session->psk_key, psk_key)) |
2057 | 0 | return 1; |
2058 | 0 | } |
2059 | 0 | session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length); |
2060 | 0 | if (!session->psk_key) { |
2061 | 0 | coap_log_err("No memory to store pre-shared key (PSK)\n"); |
2062 | 0 | if (old_psk_key) |
2063 | 0 | coap_delete_bin_const(old_psk_key); |
2064 | 0 | return 0; |
2065 | 0 | } |
2066 | 0 | } else { |
2067 | 0 | session->psk_key = NULL; |
2068 | 0 | } |
2069 | 0 | if (old_psk_key) |
2070 | 0 | coap_delete_bin_const(old_psk_key); |
2071 | |
|
2072 | 0 | return 1; |
2073 | 0 | } |
2074 | | |
2075 | | int |
2076 | | coap_session_refresh_psk_identity(coap_session_t *session, |
2077 | | const coap_bin_const_t *psk_identity |
2078 | 0 | ) { |
2079 | | /* We may be refreshing the identity with the same identity */ |
2080 | 0 | coap_bin_const_t *old_psk_identity = session->psk_identity; |
2081 | |
|
2082 | 0 | if (psk_identity && psk_identity->s) { |
2083 | 0 | if (session->psk_identity) { |
2084 | 0 | if (coap_binary_equal(session->psk_identity, psk_identity)) |
2085 | 0 | return 1; |
2086 | 0 | } |
2087 | 0 | session->psk_identity = coap_new_bin_const(psk_identity->s, |
2088 | 0 | psk_identity->length); |
2089 | 0 | if (!session->psk_identity) { |
2090 | 0 | coap_log_err("No memory to store pre-shared key identity (PSK)\n"); |
2091 | 0 | if (old_psk_identity) |
2092 | 0 | coap_delete_bin_const(old_psk_identity); |
2093 | 0 | return 0; |
2094 | 0 | } |
2095 | 0 | } else { |
2096 | 0 | session->psk_identity = NULL; |
2097 | 0 | } |
2098 | 0 | if (old_psk_identity) |
2099 | 0 | coap_delete_bin_const(old_psk_identity); |
2100 | |
|
2101 | 0 | return 1; |
2102 | 0 | } |
2103 | | |
2104 | | #if COAP_SERVER_SUPPORT |
2105 | | const coap_bin_const_t * |
2106 | 0 | coap_session_get_psk_hint(const coap_session_t *session) { |
2107 | 0 | if (session) |
2108 | 0 | return session->psk_hint; |
2109 | 0 | return NULL; |
2110 | 0 | } |
2111 | | #endif /* COAP_SERVER_SUPPORT */ |
2112 | | |
2113 | | const coap_bin_const_t * |
2114 | 0 | coap_session_get_psk_identity(const coap_session_t *session) { |
2115 | 0 | const coap_bin_const_t *psk_identity = NULL; |
2116 | 0 | if (session) { |
2117 | 0 | psk_identity = session->psk_identity; |
2118 | 0 | if (psk_identity == NULL) { |
2119 | 0 | psk_identity = &session->cpsk_setup_data.psk_info.identity; |
2120 | 0 | } |
2121 | 0 | } |
2122 | 0 | return psk_identity; |
2123 | 0 | } |
2124 | | |
2125 | | const coap_bin_const_t * |
2126 | 0 | coap_session_get_psk_key(const coap_session_t *session) { |
2127 | 0 | if (session) |
2128 | 0 | return session->psk_key; |
2129 | 0 | return NULL; |
2130 | 0 | } |
2131 | | |
2132 | | #if COAP_CLIENT_SUPPORT |
2133 | | COAP_API coap_session_t * |
2134 | | coap_new_client_session_pki(coap_context_t *ctx, |
2135 | | const coap_address_t *local_if, |
2136 | | const coap_address_t *server, |
2137 | | coap_proto_t proto, |
2138 | 0 | coap_dtls_pki_t *setup_data) { |
2139 | 0 | coap_session_t *session; |
2140 | |
|
2141 | 0 | coap_lock_lock(return NULL); |
2142 | 0 | session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data, |
2143 | 0 | NULL, NULL, NULL); |
2144 | 0 | coap_lock_unlock(); |
2145 | 0 | return session; |
2146 | 0 | } |
2147 | | |
2148 | | COAP_API coap_session_t * |
2149 | | coap_new_client_session_pki3(coap_context_t *ctx, |
2150 | | const coap_address_t *local_if, |
2151 | | const coap_address_t *server, |
2152 | | coap_proto_t proto, |
2153 | | coap_dtls_pki_t *setup_data, |
2154 | | void *app_data, |
2155 | | coap_app_data_free_callback_t callback, |
2156 | 0 | coap_str_const_t *ws_host) { |
2157 | 0 | coap_session_t *session; |
2158 | |
|
2159 | 0 | coap_lock_lock(return NULL); |
2160 | 0 | session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data, |
2161 | 0 | app_data, callback, ws_host); |
2162 | 0 | coap_lock_unlock(); |
2163 | 0 | return session; |
2164 | 0 | } |
2165 | | |
2166 | | coap_session_t * |
2167 | | coap_new_client_session_pki3_lkd(coap_context_t *ctx, |
2168 | | const coap_address_t *local_if, |
2169 | | const coap_address_t *server, |
2170 | | coap_proto_t proto, |
2171 | | coap_dtls_pki_t *setup_data, |
2172 | | void *app_data, |
2173 | | coap_app_data_free_callback_t callback, |
2174 | 0 | coap_str_const_t *ws_host) { |
2175 | 0 | coap_session_t *session; |
2176 | 0 | coap_dtls_pki_t l_setup_data; |
2177 | |
|
2178 | 0 | if (!setup_data) |
2179 | 0 | return NULL; |
2180 | 0 | if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) { |
2181 | 0 | coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n"); |
2182 | 0 | return NULL; |
2183 | 0 | } |
2184 | | |
2185 | 0 | coap_lock_check_locked(); |
2186 | 0 | l_setup_data = *setup_data; |
2187 | 0 | coap_sanitize_client_sni(&l_setup_data.client_sni); |
2188 | |
|
2189 | 0 | session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host); |
2190 | |
|
2191 | 0 | if (!session) { |
2192 | 0 | return NULL; |
2193 | 0 | } |
2194 | | |
2195 | 0 | if (coap_dtls_is_supported() || coap_tls_is_supported()) { |
2196 | | /* we know that setup_data is not NULL */ |
2197 | 0 | if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) { |
2198 | 0 | coap_session_release_lkd(session); |
2199 | 0 | return NULL; |
2200 | 0 | } |
2201 | 0 | } |
2202 | 0 | coap_log_debug("***%s: new outgoing session\n", |
2203 | 0 | coap_session_str(session)); |
2204 | 0 | coap_session_check_connect(session); |
2205 | 0 | return session; |
2206 | 0 | } |
2207 | | #endif /* ! COAP_CLIENT_SUPPORT */ |
2208 | | |
2209 | | #if COAP_SERVER_SUPPORT |
2210 | | #if !COAP_DISABLE_TCP |
2211 | | coap_session_t * |
2212 | 0 | coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra) { |
2213 | 0 | coap_session_t *session; |
2214 | 0 | coap_tick_t now; |
2215 | |
|
2216 | 0 | session = coap_make_session(ep->proto, COAP_SESSION_TYPE_SERVER, |
2217 | 0 | NULL, NULL, NULL, 0, ctx, ep); |
2218 | 0 | if (!session) |
2219 | 0 | goto error; |
2220 | | |
2221 | 0 | memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc)); |
2222 | 0 | if (!coap_netif_strm_accept(ep, session, extra)) |
2223 | 0 | goto error; |
2224 | | |
2225 | 0 | coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info); |
2226 | |
|
2227 | 0 | session->sock.session = session; |
2228 | 0 | #ifdef COAP_EPOLL_SUPPORT |
2229 | 0 | coap_epoll_ctl_add(&session->sock, |
2230 | 0 | EPOLLIN, |
2231 | 0 | __func__); |
2232 | 0 | #endif /* COAP_EPOLL_SUPPORT */ |
2233 | 0 | SESSIONS_ADD(ep->sessions, session); |
2234 | 0 | if (session) { |
2235 | 0 | coap_ticks(&now); |
2236 | 0 | session->last_rx_tx = now; |
2237 | 0 | coap_log_debug("***%s: session %p: new incoming session\n", |
2238 | 0 | coap_session_str(session), (void *)session); |
2239 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_TCP_CONNECTED, session); |
2240 | 0 | coap_handle_event_lkd(session->context, COAP_EVENT_SERVER_SESSION_NEW, session); |
2241 | 0 | session->state = COAP_SESSION_STATE_CONNECTING; |
2242 | 0 | session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session); |
2243 | 0 | } |
2244 | 0 | return session; |
2245 | | |
2246 | 0 | error: |
2247 | | /* |
2248 | | * Need to add in the session as coap_session_release_lkd() |
2249 | | * will call SESSIONS_DELETE in coap_session_free(). |
2250 | | */ |
2251 | 0 | if (session) { |
2252 | 0 | SESSIONS_ADD(ep->sessions, session); |
2253 | 0 | coap_session_free(session); |
2254 | 0 | } |
2255 | 0 | return NULL; |
2256 | 0 | } |
2257 | | #endif /* !COAP_DISABLE_TCP */ |
2258 | | #endif /* COAP_SERVER_SUPPORT */ |
2259 | | |
2260 | | void |
2261 | | coap_session_init_token(coap_session_t *session, size_t len, |
2262 | 0 | const uint8_t *data) { |
2263 | 0 | session->tx_token = coap_decode_var_bytes8(data, len); |
2264 | | /* |
2265 | | * Decrement as when first used by coap_session_new_token() it will |
2266 | | * get incremented |
2267 | | */ |
2268 | 0 | session->tx_token--; |
2269 | 0 | } |
2270 | | |
2271 | | void |
2272 | | coap_session_new_token(coap_session_t *session, size_t *len, |
2273 | 0 | uint8_t *data) { |
2274 | 0 | *len = coap_encode_var_safe8(data, |
2275 | 0 | sizeof(session->tx_token), ++session->tx_token); |
2276 | 0 | } |
2277 | | |
2278 | | COAP_API uint16_t |
2279 | 76 | coap_new_message_id(coap_session_t *session) { |
2280 | 76 | uint16_t mid; |
2281 | | |
2282 | 76 | coap_lock_lock(return 0); |
2283 | 76 | mid = coap_new_message_id_lkd(session); |
2284 | 76 | coap_lock_unlock(); |
2285 | 76 | return mid; |
2286 | 76 | } |
2287 | | |
2288 | | uint16_t |
2289 | 142 | coap_new_message_id_lkd(coap_session_t *session) { |
2290 | 142 | coap_lock_check_locked(); |
2291 | 142 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) |
2292 | 142 | return ++session->tx_mid; |
2293 | | /* TCP/TLS have no notion of mid */ |
2294 | 0 | return 0; |
2295 | 142 | } |
2296 | | |
2297 | | const coap_address_t * |
2298 | 0 | coap_session_get_addr_remote(const coap_session_t *session) { |
2299 | 0 | if (session) |
2300 | 0 | return &session->addr_info.remote; |
2301 | 0 | return NULL; |
2302 | 0 | } |
2303 | | |
2304 | | const coap_address_t * |
2305 | 0 | coap_session_get_addr_local(const coap_session_t *session) { |
2306 | 0 | if (session) |
2307 | 0 | return &session->addr_info.local; |
2308 | 0 | return NULL; |
2309 | 0 | } |
2310 | | |
2311 | | const coap_address_t * |
2312 | 0 | coap_session_get_addr_mcast(const coap_session_t *session) { |
2313 | 0 | #if COAP_CLIENT_SUPPORT |
2314 | 0 | if (session && session->type == COAP_SESSION_TYPE_CLIENT && |
2315 | 0 | session->sock.flags & COAP_SOCKET_MULTICAST) |
2316 | 0 | return &session->sock.mcast_addr; |
2317 | | #else /* ! COAP_CLIENT_SUPPORT */ |
2318 | | (void)session; |
2319 | | #endif /* ! COAP_CLIENT_SUPPORT */ |
2320 | 0 | return NULL; |
2321 | 0 | } |
2322 | | |
2323 | | coap_context_t * |
2324 | 0 | coap_session_get_context(const coap_session_t *session) { |
2325 | 0 | if (session) |
2326 | 0 | return session->context; |
2327 | 0 | return NULL; |
2328 | 0 | } |
2329 | | |
2330 | | coap_proto_t |
2331 | 0 | coap_session_get_proto(const coap_session_t *session) { |
2332 | 0 | if (session) |
2333 | 0 | return session->proto; |
2334 | 0 | return 0; |
2335 | 0 | } |
2336 | | |
2337 | | coap_session_type_t |
2338 | 0 | coap_session_get_type(const coap_session_t *session) { |
2339 | 0 | if (session) |
2340 | 0 | return session->type; |
2341 | 0 | return 0; |
2342 | 0 | } |
2343 | | |
2344 | | int |
2345 | 0 | coap_session_is_oscore(const coap_session_t *session) { |
2346 | 0 | #if COAP_OSCORE_SUPPORT |
2347 | 0 | return (session && session->oscore_encryption != 0) ? 1 : 0; |
2348 | | #else |
2349 | | (void)session; |
2350 | | return 0; |
2351 | | #endif |
2352 | 0 | } |
2353 | | |
2354 | | COAP_API int |
2355 | 0 | coap_session_set_type_client(coap_session_t *session) { |
2356 | 0 | int ret; |
2357 | |
|
2358 | 0 | coap_lock_lock(return 0); |
2359 | 0 | ret = coap_session_set_type_client_lkd(session, 1); |
2360 | 0 | coap_lock_unlock(); |
2361 | 0 | return ret; |
2362 | 0 | } |
2363 | | |
2364 | | int |
2365 | 0 | coap_session_set_type_client_lkd(coap_session_t *session, int report_changed) { |
2366 | 0 | #if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT |
2367 | 0 | if (session && session->type == COAP_SESSION_TYPE_SERVER) { |
2368 | 0 | coap_session_reference_lkd(session); |
2369 | 0 | session->type = COAP_SESSION_TYPE_CLIENT; |
2370 | 0 | if (session->endpoint) { |
2371 | 0 | SESSIONS_DELETE(session->endpoint->sessions, session); |
2372 | 0 | SESSIONS_ADD(session->context->sessions, session); |
2373 | 0 | if (COAP_PROTO_NOT_RELIABLE(session->proto)) { |
2374 | 0 | session->sock = session->endpoint->sock; |
2375 | 0 | session->sock.flags |= COAP_SOCKET_SLAVE; |
2376 | 0 | session->sock.flags &= ~COAP_SOCKET_CAN_READ; |
2377 | 0 | session->sock.flags &= ~COAP_SOCKET_WANT_READ; |
2378 | 0 | } else { |
2379 | 0 | session->sock.endpoint = NULL; |
2380 | 0 | } |
2381 | 0 | session->endpoint = NULL; |
2382 | 0 | } |
2383 | 0 | if (report_changed) { |
2384 | 0 | coap_log_debug("***%s: session now is type Client\n", |
2385 | 0 | coap_session_str(session)); |
2386 | 0 | } |
2387 | 0 | return 1; |
2388 | 0 | } |
2389 | | #else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */ |
2390 | | (void)session; |
2391 | | (void)report_changed; |
2392 | | #endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */ |
2393 | 0 | return 0; |
2394 | 0 | } |
2395 | | |
2396 | | COAP_API int |
2397 | 0 | coap_session_set_type_server(coap_session_t *session) { |
2398 | 0 | int ret; |
2399 | |
|
2400 | 0 | coap_lock_lock(return 0); |
2401 | 0 | ret = coap_session_set_type_server_lkd(session); |
2402 | 0 | coap_lock_unlock(); |
2403 | 0 | return ret; |
2404 | 0 | } |
2405 | | |
2406 | | int |
2407 | 0 | coap_session_set_type_server_lkd(coap_session_t *session) { |
2408 | 0 | #if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT |
2409 | 0 | if (session && session->type == COAP_SESSION_TYPE_CLIENT) { |
2410 | 0 | coap_endpoint_t *ep; |
2411 | 0 | coap_mid_t mid; |
2412 | 0 | coap_pdu_t *ping = NULL; |
2413 | |
|
2414 | 0 | LL_FOREACH(session->context->endpoint, ep) { |
2415 | 0 | if (session->proto == ep->proto) |
2416 | 0 | break; |
2417 | 0 | } |
2418 | 0 | if (!ep) { |
2419 | | /* Need a dummy endpoint to 'hang' off as this is now a server */ |
2420 | 0 | ep = coap_malloc_endpoint(); |
2421 | 0 | if (!ep) { |
2422 | 0 | coap_log_warn("coap_new_endpoint: malloc"); |
2423 | 0 | return 0; |
2424 | 0 | } |
2425 | | |
2426 | 0 | memset(ep, 0, sizeof(coap_endpoint_t)); |
2427 | 0 | ep->context = session->context; |
2428 | 0 | ep->proto = session->proto; |
2429 | 0 | ep->sock.endpoint = ep; |
2430 | 0 | memcpy(&ep->sock.lfunc, coap_layers_coap[session->proto], sizeof(ep->sock.lfunc)); |
2431 | |
|
2432 | 0 | ep->default_mtu = COAP_DEFAULT_MTU; |
2433 | 0 | LL_PREPEND(session->context->endpoint, ep); |
2434 | 0 | } |
2435 | | /* Need to use coap_send_lkd() here to get traffic flowing */ |
2436 | 0 | if (session->proto == COAP_PROTO_UDP) { |
2437 | 0 | ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_EMPTY_CODE, |
2438 | 0 | coap_new_message_id_lkd(session), 0); |
2439 | 0 | if (!ping) { |
2440 | 0 | session->type = COAP_SESSION_TYPE_SERVER; |
2441 | 0 | return 0; |
2442 | 0 | } |
2443 | 0 | mid = coap_send_lkd(session, ping); |
2444 | 0 | if (mid != COAP_INVALID_MID) { |
2445 | 0 | coap_tick_t now; |
2446 | |
|
2447 | 0 | coap_ticks(&now); |
2448 | 0 | session->last_ping_mid = mid; |
2449 | 0 | session->last_rx_tx = now; |
2450 | 0 | session->last_ping = now; |
2451 | 0 | } |
2452 | 0 | } |
2453 | | /* |
2454 | | * (D)TLS will initiate traffic with handshake. |
2455 | | * Reliable protocols will be sending a CSM, so no nead for Ping. |
2456 | | */ |
2457 | 0 | if (session->context) { |
2458 | 0 | if (session->context->sessions) { |
2459 | 0 | SESSIONS_DELETE(session->context->sessions, session); |
2460 | 0 | } |
2461 | 0 | } |
2462 | 0 | SESSIONS_ADD(ep->sessions, session); |
2463 | 0 | session->endpoint = ep; |
2464 | 0 | session->type = COAP_SESSION_TYPE_SERVER; |
2465 | 0 | coap_session_release_lkd(session); |
2466 | 0 | coap_log_debug("***%s: session now is type Server\n", |
2467 | 0 | coap_session_str(session)); |
2468 | 0 | return 1; |
2469 | 0 | } |
2470 | | #else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */ |
2471 | | (void)session; |
2472 | | #endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */ |
2473 | 0 | return 0; |
2474 | 0 | } |
2475 | | |
2476 | | coap_session_state_t |
2477 | 0 | coap_session_get_state(const coap_session_t *session) { |
2478 | 0 | if (session) |
2479 | 0 | return session->state; |
2480 | 0 | return 0; |
2481 | 0 | } |
2482 | | |
2483 | | coap_endpoint_t * |
2484 | 0 | coap_session_get_endpoint(const coap_session_t *session) { |
2485 | 0 | #if COAP_SERVER_SUPPORT |
2486 | 0 | if (session) |
2487 | 0 | return session->endpoint; |
2488 | | #else /* ! COAP_SERVER_SUPPORT */ |
2489 | | (void)session; |
2490 | | #endif /* ! COAP_SERVER_SUPPORT */ |
2491 | 0 | return 0; |
2492 | 0 | } |
2493 | | |
2494 | | int |
2495 | 0 | coap_session_get_ifindex(const coap_session_t *session) { |
2496 | 0 | if (session) |
2497 | 0 | return session->ifindex; |
2498 | 0 | return -1; |
2499 | 0 | } |
2500 | | |
2501 | | void * |
2502 | | coap_session_get_tls(const coap_session_t *session, |
2503 | 0 | coap_tls_library_t *tls_lib) { |
2504 | 0 | if (session) |
2505 | 0 | return coap_dtls_get_tls(session, tls_lib); |
2506 | 0 | return NULL; |
2507 | 0 | } |
2508 | | |
2509 | | static const char * |
2510 | 0 | coap_proto_name(coap_proto_t proto) { |
2511 | 0 | switch (proto) { |
2512 | 0 | case COAP_PROTO_UDP: |
2513 | 0 | return "UDP "; |
2514 | 0 | case COAP_PROTO_DTLS: |
2515 | 0 | return "DTLS"; |
2516 | 0 | case COAP_PROTO_TCP: |
2517 | 0 | return "TCP "; |
2518 | 0 | case COAP_PROTO_TLS: |
2519 | 0 | return "TLS "; |
2520 | 0 | case COAP_PROTO_WS: |
2521 | 0 | return "WS "; |
2522 | 0 | case COAP_PROTO_WSS: |
2523 | 0 | return "WSS "; |
2524 | 0 | case COAP_PROTO_NONE: |
2525 | 0 | case COAP_PROTO_LAST: |
2526 | 0 | default: |
2527 | 0 | return "????" ; |
2528 | 0 | break; |
2529 | 0 | } |
2530 | 0 | return NULL; |
2531 | 0 | } |
2532 | | |
2533 | | #if COAP_SERVER_SUPPORT |
2534 | | COAP_API coap_endpoint_t * |
2535 | 0 | coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) { |
2536 | 0 | coap_endpoint_t *endpoint; |
2537 | |
|
2538 | 0 | coap_lock_lock(return NULL); |
2539 | 0 | endpoint = coap_new_endpoint_lkd(context, listen_addr, proto); |
2540 | 0 | coap_lock_unlock(); |
2541 | 0 | return endpoint; |
2542 | 0 | } |
2543 | | |
2544 | | coap_endpoint_t * |
2545 | | coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr, |
2546 | 0 | coap_proto_t proto) { |
2547 | 0 | coap_endpoint_t *ep = NULL; |
2548 | |
|
2549 | 0 | if (!context || !listen_addr) { |
2550 | 0 | coap_log_debug("coap_new_endpoint: Both context and listen_addr need to be defined\n"); |
2551 | 0 | return NULL; |
2552 | 0 | } |
2553 | | |
2554 | 0 | coap_lock_check_locked(); |
2555 | |
|
2556 | 0 | switch (proto) { |
2557 | 0 | case COAP_PROTO_UDP: |
2558 | 0 | break; |
2559 | 0 | case COAP_PROTO_DTLS: |
2560 | 0 | if (!coap_dtls_is_supported()) { |
2561 | 0 | coap_log_warn("coap_new_endpoint: DTLS not supported\n"); |
2562 | 0 | return NULL; |
2563 | 0 | } |
2564 | 0 | break; |
2565 | 0 | case COAP_PROTO_TLS: |
2566 | 0 | if (!coap_tls_is_supported()) { |
2567 | 0 | coap_log_warn("coap_new_endpoint: TLS not supported\n"); |
2568 | 0 | return NULL; |
2569 | 0 | } |
2570 | 0 | break; |
2571 | 0 | case COAP_PROTO_TCP: |
2572 | 0 | if (!coap_tcp_is_supported()) { |
2573 | 0 | coap_log_warn("coap_new_endpoint: TCP not supported\n"); |
2574 | 0 | return NULL; |
2575 | 0 | } |
2576 | 0 | break; |
2577 | 0 | case COAP_PROTO_WS: |
2578 | 0 | if (!coap_ws_is_supported()) { |
2579 | 0 | coap_log_warn("coap_new_endpoint: WS not supported\n"); |
2580 | 0 | return NULL; |
2581 | 0 | } |
2582 | 0 | break; |
2583 | 0 | case COAP_PROTO_WSS: |
2584 | 0 | if (!coap_wss_is_supported()) { |
2585 | 0 | coap_log_warn("coap_new_endpoint: WSS not supported\n"); |
2586 | 0 | return NULL; |
2587 | 0 | } |
2588 | 0 | break; |
2589 | 0 | case COAP_PROTO_NONE: |
2590 | 0 | case COAP_PROTO_LAST: |
2591 | 0 | default: |
2592 | 0 | coap_log_crit("coap_new_endpoint: Unsupported protocol %d\n", proto); |
2593 | 0 | return NULL; |
2594 | 0 | } |
2595 | | |
2596 | 0 | if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS || |
2597 | 0 | proto == COAP_PROTO_WSS) { |
2598 | 0 | if (!coap_dtls_context_check_keys_enabled(context)) { |
2599 | 0 | coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or " |
2600 | 0 | "coap_context_set_pki() not called\n"); |
2601 | 0 | return NULL; |
2602 | 0 | } |
2603 | 0 | } |
2604 | 0 | ep = coap_malloc_endpoint(); |
2605 | 0 | if (!ep) { |
2606 | 0 | coap_log_warn("coap_new_endpoint: malloc"); |
2607 | 0 | return NULL; |
2608 | 0 | } |
2609 | | |
2610 | 0 | memset(ep, 0, sizeof(coap_endpoint_t)); |
2611 | 0 | ep->context = context; |
2612 | 0 | ep->proto = proto; |
2613 | 0 | ep->sock.endpoint = ep; |
2614 | 0 | if (proto < COAP_PROTO_LAST) { |
2615 | | /* |
2616 | | * Should be caught above, but come compilers realize that proto |
2617 | | * includes COAP_PROTO_LAST, but there is no table entry for that |
2618 | | */ |
2619 | 0 | memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc)); |
2620 | 0 | } |
2621 | |
|
2622 | 0 | if (COAP_PROTO_NOT_RELIABLE(proto)) { |
2623 | 0 | if (!coap_netif_dgrm_listen(ep, listen_addr)) |
2624 | 0 | goto error; |
2625 | | #ifdef WITH_CONTIKI |
2626 | | ep->sock.context = context; |
2627 | | #endif /* WITH_CONTIKI */ |
2628 | 0 | #if !COAP_DISABLE_TCP |
2629 | 0 | } else if (COAP_PROTO_RELIABLE(proto)) { |
2630 | 0 | if (!coap_netif_strm_listen(ep, listen_addr)) |
2631 | 0 | goto error; |
2632 | 0 | #endif /* !COAP_DISABLE_TCP */ |
2633 | 0 | } else { |
2634 | 0 | coap_log_crit("coap_new_endpoint: Protocol type not supported %d\n", proto); |
2635 | 0 | goto error; |
2636 | 0 | } |
2637 | | |
2638 | 0 | if (COAP_LOG_DEBUG <= coap_get_log_level()) { |
2639 | | #ifndef INET6_ADDRSTRLEN |
2640 | | #define INET6_ADDRSTRLEN 40 |
2641 | | #endif |
2642 | 0 | unsigned char addr_str[INET6_ADDRSTRLEN + 8]; |
2643 | |
|
2644 | 0 | if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) { |
2645 | 0 | coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto), |
2646 | 0 | addr_str); |
2647 | 0 | } |
2648 | 0 | } |
2649 | |
|
2650 | 0 | ep->default_mtu = COAP_DEFAULT_MTU; |
2651 | |
|
2652 | 0 | #ifdef COAP_EPOLL_SUPPORT |
2653 | 0 | ep->sock.endpoint = ep; |
2654 | 0 | coap_epoll_ctl_add(&ep->sock, |
2655 | 0 | EPOLLIN, |
2656 | 0 | __func__); |
2657 | 0 | #endif /* COAP_EPOLL_SUPPORT */ |
2658 | |
|
2659 | 0 | LL_PREPEND(context->endpoint, ep); |
2660 | 0 | return ep; |
2661 | | |
2662 | 0 | error: |
2663 | 0 | coap_free_endpoint_lkd(ep); |
2664 | 0 | return NULL; |
2665 | 0 | } |
2666 | | |
2667 | | void |
2668 | 0 | coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu) { |
2669 | 0 | ep->default_mtu = (uint16_t)mtu; |
2670 | 0 | } |
2671 | | |
2672 | | COAP_API void |
2673 | 0 | coap_free_endpoint(coap_endpoint_t *ep) { |
2674 | 0 | if (ep) { |
2675 | 0 | coap_context_t *context = ep->context; |
2676 | 0 | if (context) { |
2677 | 0 | coap_lock_lock(return); |
2678 | 0 | } |
2679 | 0 | coap_free_endpoint_lkd(ep); |
2680 | 0 | if (context) { |
2681 | 0 | coap_lock_unlock(); |
2682 | 0 | } |
2683 | 0 | } |
2684 | 0 | } |
2685 | | |
2686 | | void |
2687 | 0 | coap_free_endpoint_lkd(coap_endpoint_t *ep) { |
2688 | 0 | if (ep) { |
2689 | 0 | coap_session_t *session, *rtmp; |
2690 | |
|
2691 | 0 | if (ep->context) { |
2692 | | /* If fully allocated and inserted */ |
2693 | 0 | coap_lock_check_locked(); |
2694 | 0 | SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) { |
2695 | 0 | if (session->ref > 0) { |
2696 | 0 | coap_session_disconnected_lkd(session, COAP_NACK_NOT_DELIVERABLE); |
2697 | 0 | #if COAP_CLIENT_SUPPORT |
2698 | 0 | if (session->client_initiated) { |
2699 | 0 | coap_session_release_lkd(session); |
2700 | 0 | } |
2701 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
2702 | 0 | } |
2703 | 0 | assert(session->ref == 0); |
2704 | 0 | if (session->ref == 0) { |
2705 | 0 | coap_handle_event_lkd(ep->context, COAP_EVENT_SERVER_SESSION_DEL, session); |
2706 | 0 | coap_session_free(session); |
2707 | 0 | } |
2708 | 0 | } |
2709 | 0 | if (coap_netif_available_ep(ep)) { |
2710 | | /* |
2711 | | * ep->sock.endpoint is set in coap_new_endpoint(). |
2712 | | * ep->sock.session is never set. |
2713 | | * |
2714 | | * session->sock.session is set for both clients and servers (when a |
2715 | | * new session is accepted), but does not affect the endpoint. |
2716 | | * |
2717 | | * So, it is safe to call coap_netif_close_ep() after all the sessions |
2718 | | * have been freed above as we are only working with the endpoint sock. |
2719 | | */ |
2720 | 0 | #ifdef COAP_EPOLL_SUPPORT |
2721 | 0 | assert(ep->sock.session == NULL); |
2722 | 0 | #endif /* COAP_EPOLL_SUPPORT */ |
2723 | 0 | coap_netif_close_ep(ep); |
2724 | 0 | } |
2725 | | |
2726 | 0 | if (ep->context->endpoint) { |
2727 | 0 | LL_DELETE(ep->context->endpoint, ep); |
2728 | 0 | } |
2729 | 0 | } |
2730 | | |
2731 | 0 | if (ep->app_cb) { |
2732 | 0 | coap_lock_callback(ep->app_cb(ep->app_data)); |
2733 | 0 | } |
2734 | |
|
2735 | 0 | coap_mfree_endpoint(ep); |
2736 | 0 | } |
2737 | 0 | } |
2738 | | |
2739 | | void * |
2740 | 0 | coap_endpoint_get_app_data(const coap_endpoint_t *endpoint) { |
2741 | 0 | assert(endpoint); |
2742 | 0 | return endpoint->app_data; |
2743 | 0 | } |
2744 | | |
2745 | | COAP_API void * |
2746 | | coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *app_data, |
2747 | 0 | coap_app_data_free_callback_t callback) { |
2748 | 0 | void *old_data; |
2749 | |
|
2750 | 0 | coap_lock_lock(return NULL); |
2751 | 0 | old_data = coap_endpoint_set_app_data_lkd(endpoint, app_data, callback); |
2752 | 0 | coap_lock_unlock(); |
2753 | 0 | return old_data; |
2754 | 0 | } |
2755 | | |
2756 | | void * |
2757 | | coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *app_data, |
2758 | 0 | coap_app_data_free_callback_t callback) { |
2759 | 0 | void *old_data = endpoint->app_data; |
2760 | |
|
2761 | 0 | endpoint->app_data = app_data; |
2762 | 0 | endpoint->app_cb = app_data ? callback : NULL; |
2763 | 0 | return old_data; |
2764 | 0 | } |
2765 | | |
2766 | | #endif /* COAP_SERVER_SUPPORT */ |
2767 | | |
2768 | | coap_session_t * |
2769 | | coap_session_get_by_peer(const coap_context_t *ctx, |
2770 | | const coap_address_t *remote_addr, |
2771 | 0 | int ifindex) { |
2772 | 0 | coap_session_t *s, *rtmp; |
2773 | 0 | #if COAP_CLIENT_SUPPORT |
2774 | 0 | SESSIONS_ITER(ctx->sessions, s, rtmp) { |
2775 | 0 | if (s->ifindex == ifindex) { |
2776 | 0 | if (s->sock.flags & COAP_SOCKET_MULTICAST) { |
2777 | 0 | if (coap_address_equals(&s->sock.mcast_addr, remote_addr)) |
2778 | 0 | return s; |
2779 | 0 | } else if (coap_address_equals(&s->addr_info.remote, remote_addr)) |
2780 | 0 | return s; |
2781 | 0 | } |
2782 | 0 | } |
2783 | 0 | #endif /* COAP_CLIENT_SUPPORT */ |
2784 | 0 | #if COAP_SERVER_SUPPORT |
2785 | 0 | coap_endpoint_t *ep; |
2786 | |
|
2787 | 0 | LL_FOREACH(ctx->endpoint, ep) { |
2788 | 0 | SESSIONS_ITER(ep->sessions, s, rtmp) { |
2789 | 0 | if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote, |
2790 | 0 | remote_addr)) |
2791 | 0 | return s; |
2792 | 0 | } |
2793 | 0 | } |
2794 | 0 | #endif /* COAP_SERVER_SUPPORT */ |
2795 | 0 | return NULL; |
2796 | 0 | } |
2797 | | |
2798 | | #ifndef INET6_ADDRSTRLEN |
2799 | | #define INET6_ADDRSTRLEN 46 |
2800 | | #endif |
2801 | | const char * |
2802 | 0 | coap_session_str(const coap_session_t *session) { |
2803 | 0 | static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24]; |
2804 | 0 | char *p = szSession, *end = szSession + sizeof(szSession); |
2805 | |
|
2806 | 0 | if (!session) { |
2807 | 0 | return "Session not defined"; |
2808 | 0 | } |
2809 | 0 | if (coap_print_addr(&session->addr_info.local, |
2810 | 0 | (unsigned char *)p, end - p) > 0) |
2811 | 0 | p += strlen(p); |
2812 | 0 | if (p + 6 < end) { |
2813 | 0 | strcpy(p, " <-> "); |
2814 | 0 | p += 5; |
2815 | 0 | } |
2816 | 0 | if (p + 1 < end) { |
2817 | 0 | if (coap_print_addr(&session->addr_info.remote, |
2818 | 0 | (unsigned char *)p, end - p) > 0) |
2819 | 0 | p += strlen(p); |
2820 | 0 | } |
2821 | 0 | if (session->ifindex > 0 && p + 1 < end) |
2822 | 0 | p += snprintf(p, end - p, " (if%d)", session->ifindex); |
2823 | 0 | if (p + 6 < end) { |
2824 | 0 | strcpy(p, " "); |
2825 | 0 | p++; |
2826 | 0 | strcpy(p, coap_proto_name(session->proto)); |
2827 | 0 | } |
2828 | |
|
2829 | 0 | return szSession; |
2830 | 0 | } |
2831 | | |
2832 | | #if COAP_SERVER_SUPPORT |
2833 | | const char * |
2834 | 0 | coap_endpoint_str(const coap_endpoint_t *endpoint) { |
2835 | 0 | static char szEndpoint[128]; |
2836 | 0 | char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint); |
2837 | 0 | if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0) |
2838 | 0 | p += strlen(p); |
2839 | 0 | if (p + 6 < end) { |
2840 | 0 | if (endpoint->proto == COAP_PROTO_UDP) { |
2841 | 0 | strcpy(p, " UDP"); |
2842 | 0 | } else if (endpoint->proto == COAP_PROTO_DTLS) { |
2843 | 0 | strcpy(p, " DTLS"); |
2844 | 0 | } else { |
2845 | 0 | strcpy(p, " NONE"); |
2846 | 0 | } |
2847 | 0 | } |
2848 | |
|
2849 | 0 | return szEndpoint; |
2850 | 0 | } |
2851 | | #endif /* COAP_SERVER_SUPPORT */ |
2852 | | #if COAP_CLIENT_SUPPORT |
2853 | | void |
2854 | 0 | coap_session_set_no_observe_cancel(coap_session_t *session) { |
2855 | 0 | session->no_observe_cancel = 1; |
2856 | 0 | } |
2857 | | |
2858 | | void |
2859 | 0 | coap_call_home_stop_reconnecting(coap_session_t *session) { |
2860 | 0 | if (session->type != COAP_SESSION_TYPE_CLIENT) { |
2861 | 0 | coap_lock_lock(return); |
2862 | 0 | coap_session_set_type_client_lkd(session, 0); |
2863 | 0 | coap_session_release_lkd(session); |
2864 | 0 | coap_lock_unlock(); |
2865 | 0 | } |
2866 | 0 | } |
2867 | | #endif /* COAP_CLIENT_SUPPORT */ |
2868 | | #endif /* COAP_SESSION_C_ */ |