libcoap 4.3.2rc2

coap_session.c
Go to the documentation of this file.
1/* coap_session.c -- Session management for libcoap
2 *
3 * Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
4 * Copyright (C) 2022-2023 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
17#include "coap3/coap_internal.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 coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
31 coap_fixed_point_t res;
32 uint32_t fr = fp1.fractional_part * fp2.fractional_part;
33
34 res.integer_part = fp1.integer_part * fp2.integer_part + fr/1000;
35 res.fractional_part = fr % 1000;
36 return res;
37}
38
39 coap_fixed_point_t
40 coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
41 coap_fixed_point_t res;
42 uint32_t fr = fp1.fractional_part * u2;
43
44 res.integer_part = fp1.integer_part * u2 + fr/1000;
45 res.fractional_part = fr % 1000;
46 return res;
47}
48
49 coap_fixed_point_t
50 coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
51 coap_fixed_point_t res;
52 uint32_t fr = fp1.fractional_part + fp2.fractional_part;
53
54 res.integer_part = fp1.integer_part + fp2.integer_part + fr/1000;
55 res.fractional_part = fr % 1000;
56 return res;
57}
58
59 coap_fixed_point_t
60 coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
61 coap_fixed_point_t res = fp1;
62
63 res.integer_part += u2;
64 return res;
65}
66
67 coap_fixed_point_t
68 coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
69 coap_fixed_point_t res = fp1;
70
71 res.integer_part -= u2;
72 return res;
73}
74
75 coap_fixed_point_t
76 coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
77 coap_fixed_point_t res;
78 uint32_t num = (fp1.integer_part * 1000 + fp1.fractional_part) / u2;
79
80 res.integer_part = num / 1000;
81 res.fractional_part = num % 1000;
82 return res;
83}
84
85#if COAP_Q_BLOCK_SUPPORT
86 coap_fixed_point_t
87 coap_get_non_timeout_random(coap_session_t *session) {
88 coap_fixed_point_t res;
89 uint8_t ran;
90
91 coap_prng(&ran, sizeof(ran));
92 res = coap_sub_fixed_uint(COAP_ACK_RANDOM_FACTOR(session), 1);
93 res = coap_multi_fixed_uint(res, ran);
94 res = coap_div_fixed_uint(res, 0xff);
95 res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res);
96 return res;
97}
98
99 coap_tick_t
100 coap_get_non_timeout_random_ticks(coap_session_t *session) {
101 coap_fixed_point_t res = coap_get_non_timeout_random(session);
102 coap_tick_t ticks = (res.integer_part * COAP_TICKS_PER_SECOND +
103 res.fractional_part * COAP_TICKS_PER_SECOND / 1000);
104
105 return ticks;
106}
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
120coap_session_fix_non_probing_wait_base(coap_session_t *s) {
121 coap_fixed_point_t res;
122
123 res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
124 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
125 res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
126 res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
127 COAP_NON_PROBING_WAIT_BASE(s) = res;
128}
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
135coap_session_fix_non_partial_timeout(coap_session_t *s) {
136 coap_fixed_point_t res;
137
138 res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
139 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
140 res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
141 res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
142 res = coap_add_fixed_fixed(res, COAP_NON_TIMEOUT(s));
143 COAP_NON_PARTIAL_TIMEOUT(s) = res;
144}
145#endif /* COAP_Q_BLOCK_SUPPORT */
146
147 void
148 coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value) {
149 if (value.integer_part > 0 && value.fractional_part < 1000) {
150 session->ack_timeout = value;
151 coap_log_debug("***%s: session ack_timeout set to %u.%03u\n",
152 coap_session_str(session), session->ack_timeout.integer_part,
153 session->ack_timeout.fractional_part);
154 }
155}
156
157 void
158 coap_session_set_ack_random_factor(coap_session_t *session,
159 coap_fixed_point_t value) {
160 if (value.integer_part > 0 && value.fractional_part < 1000) {
161 session->ack_random_factor = value;
162 coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n",
163 coap_session_str(session), session->ack_random_factor.integer_part,
164 session->ack_random_factor.fractional_part);
165#if COAP_Q_BLOCK_SUPPORT
166 coap_session_fix_non_probing_wait_base(session);
167 coap_session_fix_non_partial_timeout(session);
168#endif /* COAP_Q_BLOCK_SUPPORT */
169 }
170 return;
171}
172
173 void
174 coap_session_set_max_retransmit(coap_session_t *session, uint16_t value) {
175 if (value > 0) {
176 session->max_retransmit = value;
177 coap_log_debug("***%s: session max_retransmit set to %u\n",
178 coap_session_str(session), session->max_retransmit);
179 }
180}
181
182 void
183 coap_session_set_nstart(coap_session_t *session, uint16_t value) {
184 if (value > 0) {
185 session->nstart = value;
186 coap_log_debug("***%s: session nstart set to %u\n",
187 coap_session_str(session), session->nstart);
188 }
189}
190
191 void
192 coap_session_set_default_leisure(coap_session_t *session,
193 coap_fixed_point_t value) {
194 if (value.integer_part > 0 && value.fractional_part < 1000) {
195 session->default_leisure = value;
196 coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
197 coap_session_str(session), session->default_leisure.integer_part,
198 session->default_leisure.fractional_part);
199 }
200}
201
202 void
203 coap_session_set_probing_rate(coap_session_t *session, uint32_t value) {
204 if (value > 0) {
205 session->probing_rate = value;
206 coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
207 coap_session_str(session), session->probing_rate);
208 }
209}
210
211 void
212 coap_session_set_max_payloads(coap_session_t *session, uint16_t value) {
213#if COAP_Q_BLOCK_SUPPORT
214 if (value > 0) {
215 session->max_payloads = value;
216 coap_log_debug("***%s: session max_payloads set to %u\n",
217 coap_session_str(session), session->max_payloads);
218 coap_session_fix_non_probing_wait_base(session);
219 coap_session_fix_non_partial_timeout(session);
220 }
221#else /* ! COAP_Q_BLOCK_SUPPORT */
222 (void)session;
223 (void)value;
224#endif /* ! COAP_Q_BLOCK_SUPPORT */
225}
226
227 void
228 coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value) {
229#if COAP_Q_BLOCK_SUPPORT
230 if (value > 0) {
231 session->non_max_retransmit = value;
232 coap_log_debug("***%s: session non_max_retransmit set to %u\n",
233 coap_session_str(session), session->non_max_retransmit);
234 coap_session_fix_non_probing_wait_base(session);
235 coap_session_fix_non_partial_timeout(session);
236 }
237#else /* ! COAP_Q_BLOCK_SUPPORT */
238 (void)session;
239 (void)value;
240#endif /* ! COAP_Q_BLOCK_SUPPORT */
241}
242
243 void
244 coap_session_set_non_timeout(coap_session_t *session,
245 coap_fixed_point_t value) {
246#if COAP_Q_BLOCK_SUPPORT
247 if (value.integer_part > 0 && value.fractional_part < 1000) {
248 session->non_timeout = value;
249 coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
250 coap_session_str(session), session->non_timeout.integer_part,
251 session->non_timeout.fractional_part);
252 coap_session_fix_non_probing_wait_base(session);
253 coap_session_fix_non_partial_timeout(session);
254 }
255#else /* ! COAP_Q_BLOCK_SUPPORT */
256 (void)session;
257 (void)value;
258#endif /* ! COAP_Q_BLOCK_SUPPORT */
259}
260
261 void
262 coap_session_set_non_receive_timeout(coap_session_t *session,
263 coap_fixed_point_t value) {
264#if COAP_Q_BLOCK_SUPPORT
265 if (value.integer_part > 0 && value.fractional_part < 1000)
266 session->non_receive_timeout = value;
267 coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
268 coap_session_str(session),
269 session->non_receive_timeout.integer_part,
270 session->non_receive_timeout.fractional_part);
271#else /* ! COAP_Q_BLOCK_SUPPORT */
272 (void)session;
273 (void)value;
274#endif /* ! COAP_Q_BLOCK_SUPPORT */
275}
276
277 coap_fixed_point_t
278 coap_session_get_ack_timeout(const coap_session_t *session) {
279 return session->ack_timeout;
280}
281
282 coap_fixed_point_t
283 coap_session_get_ack_random_factor(const coap_session_t *session) {
284 return session->ack_random_factor;
285}
286
287uint16_t
288 coap_session_get_max_retransmit(const coap_session_t *session) {
289 return session->max_retransmit;
290}
291
292uint16_t
293 coap_session_get_nstart(const coap_session_t *session) {
294 return session->nstart;
295}
296
297 coap_fixed_point_t
298 coap_session_get_default_leisure(const coap_session_t *session) {
299 return session->default_leisure;
300}
301
302uint32_t
303 coap_session_get_probing_rate(const coap_session_t *session) {
304 return session->probing_rate;
305}
306
307uint16_t
308 coap_session_get_max_payloads(const coap_session_t *session) {
309#if COAP_Q_BLOCK_SUPPORT
310 return session->max_payloads;
311#else /* ! COAP_Q_BLOCK_SUPPORT */
312 (void)session;
313 return COAP_DEFAULT_MAX_PAYLOADS;
314#endif /* ! COAP_Q_BLOCK_SUPPORT */
315}
316
317uint16_t
318 coap_session_get_non_max_retransmit(const coap_session_t *session) {
319#if COAP_Q_BLOCK_SUPPORT
320 return session->non_max_retransmit;
321#else /* ! COAP_Q_BLOCK_SUPPORT */
322 (void)session;
323 return COAP_DEFAULT_NON_MAX_RETRANSMIT;
324#endif /* ! COAP_Q_BLOCK_SUPPORT */
325}
326
327 coap_fixed_point_t
328 coap_session_get_non_timeout(const coap_session_t *session) {
329#if COAP_Q_BLOCK_SUPPORT
330 return session->non_timeout;
331#else /* ! COAP_Q_BLOCK_SUPPORT */
332 (void)session;
333 return COAP_DEFAULT_NON_TIMEOUT;
334#endif /* ! COAP_Q_BLOCK_SUPPORT */
335}
336
337 coap_fixed_point_t
338 coap_session_get_non_receive_timeout(const coap_session_t *session) {
339#if COAP_Q_BLOCK_SUPPORT
340 return session->non_receive_timeout;
341#else /* ! COAP_Q_BLOCK_SUPPORT */
342 (void)session;
343 return COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
344#endif /* ! COAP_Q_BLOCK_SUPPORT */
345}
346
347 coap_session_t *
348 coap_session_reference(coap_session_t *session) {
349 ++session->ref;
350 return session;
351}
352
353 void
354 coap_session_release(coap_session_t *session) {
355 if (session) {
356#ifndef __COVERITY__
357 assert(session->ref > 0);
358 if (session->ref > 0)
359 --session->ref;
360 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
361 coap_session_free(session);
362#else /* __COVERITY__ */
363 /* Coverity scan is fooled by the reference counter leading to
364 * false positives for USE_AFTER_FREE. */
365 --session->ref;
366 __coverity_negative_sink__(session->ref);
367 /* Indicate that resources are released properly. */
368 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
369 __coverity_free__(session);
370 }
371#endif /* __COVERITY__ */
372 }
373}
374
375 void
376 coap_session_set_app_data(coap_session_t *session, void *app_data) {
377 assert(session);
378 session->app = app_data;
379}
380
381 void *
382 coap_session_get_app_data(const coap_session_t *session) {
383 assert(session);
384 return session->app;
385}
386
387 static coap_session_t *
388 coap_make_session(coap_proto_t proto, coap_session_type_t type,
389 const coap_addr_hash_t *addr_hash,
390 const coap_address_t *local_addr,
391 const coap_address_t *remote_addr, int ifindex,
392 coap_context_t *context, coap_endpoint_t *endpoint) {
393 coap_session_t *session = (coap_session_t *)coap_malloc_type(COAP_SESSION,
394 sizeof(coap_session_t));
395#if ! COAP_SERVER_SUPPORT
396 (void)endpoint;
397#endif /* ! COAP_SERVER_SUPPORT */
398 if (!session)
399 return NULL;
400 memset(session, 0, sizeof(*session));
401 session->proto = proto;
402 session->type = type;
403 if (addr_hash)
404 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
405 else
406 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
407 if (local_addr)
408 coap_address_copy(&session->addr_info.local, local_addr);
409 else
410 coap_address_init(&session->addr_info.local);
411 if (remote_addr)
412 coap_address_copy(&session->addr_info.remote, remote_addr);
413 else
414 coap_address_init(&session->addr_info.remote);
415 session->ifindex = ifindex;
416 session->context = context;
417#if COAP_SERVER_SUPPORT
418 session->endpoint = endpoint;
419 if (endpoint)
420 session->mtu = endpoint->default_mtu;
421 else
422#endif /* COAP_SERVER_SUPPORT */
423 session->mtu = COAP_DEFAULT_MTU;
424 session->block_mode = context->block_mode;
425 if (proto == COAP_PROTO_DTLS) {
426 session->tls_overhead = 29;
427 if (session->tls_overhead >= session->mtu) {
428 session->tls_overhead = session->mtu;
429 coap_log_err("DTLS overhead exceeds MTU\n");
430 }
431 }
432 session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT;
433 session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR;
434 session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT;
435 session->nstart = COAP_DEFAULT_NSTART;
436 session->default_leisure = COAP_DEFAULT_DEFAULT_LEISURE;
437 session->probing_rate = COAP_DEFAULT_PROBING_RATE;
438#if COAP_Q_BLOCK_SUPPORT
439 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
440 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
441 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
442 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
443 coap_session_fix_non_probing_wait_base(session);
444 coap_session_fix_non_partial_timeout(session);
445#endif /* COAP_Q_BLOCK_SUPPORT */
446 session->dtls_event = -1;
447 session->last_ping_mid = COAP_INVALID_MID;
448 session->last_ack_mid = COAP_INVALID_MID;
449 session->last_con_mid = COAP_INVALID_MID;
450 session->max_token_size = context->max_token_size; /* RFC8974 */
451 if (session->type != COAP_SESSION_TYPE_CLIENT)
452 session->max_token_checked = COAP_EXT_T_CHECKED;
453
454 /* Randomly initialize */
455 /* TCP/TLS have no notion of mid */
456 if (COAP_PROTO_NOT_RELIABLE(session->proto))
457 coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
458 coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
459
460 return session;
461}
462
463 void
464 coap_session_mfree(coap_session_t *session) {
465 coap_queue_t *q, *tmp;
466 coap_lg_xmit_t *lq, *ltmp;
467
468#if COAP_CLIENT_SUPPORT
469 coap_lg_crcv_t *lg_crcv, *etmp;
470
471 /* Need to do this before (D)TLS and socket is closed down */
472 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
473 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
474 /* Need to close down observe */
475 if (coap_cancel_observe(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
476 /* Need to delete node we set up for NON */
477 coap_queue_t *queue = session->context->sendqueue;
478
479 while (queue) {
480 if (queue->session == session) {
481 coap_delete_node(queue);
482 break;
483 }
484 queue = queue->next;
485 }
486 }
487 }
488 LL_DELETE(session->lg_crcv, lg_crcv);
489 coap_block_delete_lg_crcv(session, lg_crcv);
490 }
491#endif /* COAP_CLIENT_SUPPORT */
492
493 if (session->partial_pdu)
494 coap_delete_pdu(session->partial_pdu);
495 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
496 if (session->psk_identity)
497 coap_delete_bin_const(session->psk_identity);
498 if (session->psk_key)
499 coap_delete_bin_const(session->psk_key);
500 if (session->psk_hint)
501 coap_delete_bin_const(session->psk_hint);
502
503#if COAP_SERVER_SUPPORT
504 coap_cache_entry_t *cp, *ctmp;
505 HASH_ITER(hh, session->context->cache, cp, ctmp) {
506 /* cp->session is NULL if not session based */
507 if (cp->session == session) {
508 coap_delete_cache_entry(session->context, cp);
509 }
510 }
511#endif /* COAP_SERVER_SUPPORT */
512 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
513 if (q->pdu->type==COAP_MESSAGE_CON && session->context &&
514 session->context->nack_handler) {
515 coap_check_update_token(session, q->pdu);
516 session->context->nack_handler(session, q->pdu,
517 session->proto == COAP_PROTO_DTLS ?
518 COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE,
519 q->id);
520 }
521 coap_delete_node(q);
522 }
523 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
524 LL_DELETE(session->lg_xmit, lq);
525 coap_block_delete_lg_xmit(session, lq);
526 }
527#if COAP_SERVER_SUPPORT
528 coap_lg_srcv_t *sq, *stmp;
529
530 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
531 LL_DELETE(session->lg_srcv, sq);
532 coap_block_delete_lg_srcv(session, sq);
533 }
534#endif /* COAP_SERVER_SUPPORT */
535#if COAP_OSCORE_SUPPORT
536 coap_delete_oscore_associations(session);
537#endif /* COAP_OSCORE_SUPPORT */
538#if COAP_WS_SUPPORT
539 coap_free_type(COAP_STRING, session->ws);
540 coap_delete_str_const(session->ws_host);
541#endif /* COAP_WS_SUPPORT */
542}
543
544 void
545 coap_session_free(coap_session_t *session) {
546 if (!session)
547 return;
548 assert(session->ref == 0);
549 if (session->ref)
550 return;
551 /* Make sure nothing gets deleted under our feet */
552 coap_session_reference(session);
553 coap_session_mfree(session);
554#if COAP_SERVER_SUPPORT
555 if (session->endpoint) {
556 if (session->endpoint->sessions)
557 SESSIONS_DELETE(session->endpoint->sessions, session);
558 } else
559#endif /* COAP_SERVER_SUPPORT */
560#if COAP_CLIENT_SUPPORT
561 if (session->context) {
562 if (session->context->sessions)
563 SESSIONS_DELETE(session->context->sessions, session);
564 }
565#endif /* COAP_CLIENT_SUPPORT */
566 coap_delete_bin_const(session->last_token);
567 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
568 (void *)session);
569
570 assert(session->ref == 1);
571 coap_free_type(COAP_SESSION, session);
572}
573
574 static size_t
575 coap_session_max_pdu_size_internal(const coap_session_t *session,
576 size_t max_with_header) {
577#if COAP_DISABLE_TCP
578 (void)session;
579 return max_with_header > 4 ? max_with_header - 4 : 0;
580#else /* !COAP_DISABLE_TCP */
581 if (COAP_PROTO_NOT_RELIABLE(session->proto))
582 return max_with_header > 4 ? max_with_header - 4 : 0;
583 /* we must assume there is no token to be on the safe side */
584 if (max_with_header <= 2)
585 return 0;
586 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
587 return max_with_header - 2;
588 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
589 return max_with_header - 3;
590 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
591 return max_with_header - 4;
592 else
593 return max_with_header - 6;
594#endif /* !COAP_DISABLE_TCP */
595}
596
597 size_t
598 coap_session_max_pdu_rcv_size(const coap_session_t *session) {
599 if (session->csm_rcv_mtu)
600 return coap_session_max_pdu_size_internal(session,
601 (size_t)(session->csm_rcv_mtu));
602
603 return coap_session_max_pdu_size_internal(session,
604 (size_t)(session->mtu - session->tls_overhead));
605}
606
607 size_t
608 coap_session_max_pdu_size(const coap_session_t *session) {
609 size_t max_with_header;
610
611#if COAP_CLIENT_SUPPORT
612 /*
613 * Delay if session->doing_first is set.
614 * E.g. Reliable and CSM not in yet for checking block support
615 */
616 coap_session_t *session_rw;
617
618 /*
619 * Need to do this to not get a compiler warning about const parameters
620 * but need to maintain source code backward compatibility
621 */
622 memcpy(&session_rw, &session, sizeof(session_rw));
623 if (coap_client_delay_first(session_rw) == 0) {
624 coap_log_debug("coap_client_delay_first: timeout\n");
625 /* Have to go with the defaults */
626 }
627#endif /* COAP_CLIENT_SUPPORT */
628
629 max_with_header = (size_t)(session->mtu - session->tls_overhead);
630
631 return coap_session_max_pdu_size_internal(session, max_with_header);
632}
633
634 void
635 coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
636#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
637 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
638 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
639#endif
640 if (mtu < 64)
641 mtu = 64;
642 session->mtu = mtu;
643 if (session->tls_overhead >= session->mtu) {
644 session->tls_overhead = session->mtu;
645 coap_log_err("DTLS overhead exceeds MTU\n");
646 }
647}
648
649ssize_t
650 coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
651 coap_queue_t *node) {
652 if (node) {
653 coap_queue_t *removed = NULL;
654 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
655 assert(removed == node);
656 coap_session_release(node->session);
657 node->session = NULL;
658 node->t = 0;
659 } else {
660 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
661 coap_queue_t *q = NULL;
662 /* Check same mid is not getting re-used in violation of RFC7252 */
663 LL_FOREACH(session->delayqueue, q) {
664 if (q->id == pdu->mid) {
665 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
666 coap_session_str(session), pdu->mid);
667 return COAP_INVALID_MID;
668 }
669 }
670 }
671 node = coap_new_node();
672 if (node == NULL)
673 return COAP_INVALID_MID;
674 node->id = pdu->mid;
675 node->pdu = pdu;
676 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
677 uint8_t r;
678 coap_prng(&r, sizeof(r));
679 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
680 node->timeout = coap_calc_timeout(session, r);
681 }
682 }
683 LL_APPEND(session->delayqueue, node);
684 coap_log_debug("** %s: mid=0x%04x: delayed\n",
685 coap_session_str(session), node->id);
686 return COAP_PDU_DELAYED;
687}
688
689#if !COAP_DISABLE_TCP
690 void
691 coap_session_send_csm(coap_session_t *session) {
692 coap_pdu_t *pdu;
693 uint8_t buf[4];
694 assert(COAP_PROTO_RELIABLE(session->proto));
695 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
696 session->state = COAP_SESSION_STATE_CSM;
697 session->partial_write = 0;
698 if (session->mtu == 0)
699 session->mtu = COAP_DEFAULT_MTU; /* base value */
700 pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_CSM, 0, 20);
701 if (pdu == NULL
702 || coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE,
703 coap_encode_var_safe(buf, sizeof(buf),
704 session->context->csm_max_message_size), buf) == 0
705 || coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER,
706 coap_encode_var_safe(buf, sizeof(buf),
707 0), buf) == 0
708 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
709 coap_add_option_internal(pdu,
710 COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH,
711 coap_encode_var_safe(buf, sizeof(buf),
712 session->max_token_size),
713 buf) == 0)
714 || coap_pdu_encode_header(pdu, session->proto) == 0
715 ) {
716 coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
717 } else {
718 ssize_t bytes_written;
719
720 pdu->session = session;
721 bytes_written = coap_session_send_pdu(session, pdu);
722 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
723 coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
724 } else {
725 session->csm_rcv_mtu = session->context->csm_max_message_size;
726 if (session->csm_rcv_mtu > COAP_BERT_BASE)
727 session->csm_bert_loc_support = 1;
728 else
729 session->csm_bert_loc_support = 0;
730 }
731 }
732 if (pdu)
733 coap_delete_pdu(pdu);
734}
735#endif /* !COAP_DISABLE_TCP */
736
737 coap_mid_t
738 coap_session_send_ping(coap_session_t *session) {
739 coap_pdu_t *ping = NULL;
740
741 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
742 session->con_active)
743 return COAP_INVALID_MID;
744 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
745 uint16_t mid = coap_new_message_id(session);
746 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
747 }
748#if !COAP_DISABLE_TCP
749 else {
750 ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, 0, 1);
751 }
752#endif /* !COAP_DISABLE_TCP */
753 if (!ping)
754 return COAP_INVALID_MID;
755 return coap_send_internal(session, ping);
756}
757
758 void
759 coap_session_connected(coap_session_t *session) {
760 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
761 coap_log_debug("***%s: session connected\n",
762 coap_session_str(session));
763 if (session->state == COAP_SESSION_STATE_CSM) {
764 coap_handle_event(session->context, COAP_EVENT_SESSION_CONNECTED, session);
765 if (session->doing_first)
766 session->doing_first = 0;
767 }
768 }
769
770 session->state = COAP_SESSION_STATE_ESTABLISHED;
771 session->partial_write = 0;
772
773 if (session->proto==COAP_PROTO_DTLS) {
774 session->tls_overhead = coap_dtls_get_overhead(session);
775 if (session->tls_overhead >= session->mtu) {
776 session->tls_overhead = session->mtu;
777 coap_log_err("DTLS overhead exceeds MTU\n");
778 }
779 }
780
781 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
782 ssize_t bytes_written;
783 coap_queue_t *q = session->delayqueue;
784 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
785 if (session->con_active >= COAP_NSTART(session))
786 break;
787 session->con_active++;
788 }
789 /* Take entry off the queue */
790 session->delayqueue = q->next;
791 q->next = NULL;
792
793 coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
794 coap_session_str(session), (int)q->pdu->mid);
795 bytes_written = coap_session_send_pdu(session, q->pdu);
796 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
797 if (coap_wait_ack(session->context, session, q) >= 0)
798 q = NULL;
799 }
800 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
801 if (q)
802 coap_delete_node(q);
803 if (bytes_written < 0)
804 break;
805 } else {
806 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
807 q->next = session->delayqueue;
808 session->delayqueue = q;
809 if (bytes_written > 0)
810 session->partial_write = (size_t)bytes_written;
811 break;
812 } else {
813 coap_delete_node(q);
814 }
815 }
816 }
817}
818
819#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
820 static const char *
821 coap_nack_name(coap_nack_reason_t reason) {
822 switch (reason) {
823 case COAP_NACK_TOO_MANY_RETRIES:
824 return "COAP_NACK_TOO_MANY_RETRIES";
825 case COAP_NACK_NOT_DELIVERABLE:
826 return "COAP_NACK_NOT_DELIVERABLE";
827 case COAP_NACK_RST:
828 return "COAP_NACK_RST";
829 case COAP_NACK_TLS_FAILED:
830 return "COAP_NACK_TLS_FAILED";
831 case COAP_NACK_ICMP_ISSUE:
832 return "COAP_NACK_ICMP_ISSUE";
833 case COAP_NACK_BAD_RESPONSE:
834 return "COAP_NACK_BAD_RESPONSE";
835 case COAP_NACK_TLS_LAYER_FAILED:
836 return "COAP_NACK_TLS_LAYER_FAILED";
837 case COAP_NACK_WS_LAYER_FAILED:
838 return "COAP_NACK_WS_LAYER_FAILED";
839 case COAP_NACK_WS_FAILED:
840 return "COAP_NACK_WS_FAILED";
841 default:
842 return "???";
843 }
844}
845#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
846
847 void
848 coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) {
849#if !COAP_DISABLE_TCP
850 coap_session_state_t state = session->state;
851#endif /* !COAP_DISABLE_TCP */
852 coap_lg_xmit_t *lq, *ltmp;
853#if COAP_SERVER_SUPPORT
854 coap_lg_srcv_t *sq, *stmp;
855#endif /* COAP_SERVER_SUPPORT */
856#if COAP_CLIENT_SUPPORT
857 coap_lg_crcv_t *cq, *etmp;
858#endif /* COAP_CLIENT_SUPPORT */
859
860 if (reason == COAP_NACK_ICMP_ISSUE) {
861 if (session->context->nack_handler) {
862 int sent_nack = 0;
863 coap_queue_t *q = session->context->sendqueue;
864 while (q) {
865 if (q->session == session) {
866 /* Take the first one */
867 coap_bin_const_t token = q->pdu->actual_token;
868
869 coap_check_update_token(session, q->pdu);
870 session->context->nack_handler(session, q->pdu, reason, q->id);
871 coap_update_token(q->pdu, token.length, token.s);
872 sent_nack = 1;
873 break;
874 }
875 q = q->next;
876 }
877#if COAP_CLIENT_SUPPORT
878 if (!sent_nack && session->lg_crcv) {
879 /* Take the first one */
880 session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
881 session->lg_crcv->pdu.mid);
882 sent_nack = 1;
883 }
884#endif /* COAP_CLIENT_SUPPORT */
885 if (!sent_nack) {
886 /* Unable to determine which request ICMP issue was for */
887 session->context->nack_handler(session, NULL, reason, 0);
888 }
889 }
890 coap_log_debug("***%s: session issue (%s)\n",
891 coap_session_str(session), coap_nack_name(reason));
892 return;
893 }
894 coap_log_debug("***%s: session disconnected (%s)\n",
895 coap_session_str(session), coap_nack_name(reason));
896#if COAP_SERVER_SUPPORT
897 coap_delete_observers(session->context, session);
898#endif /* COAP_SERVER_SUPPORT */
899
900 if (session->proto == COAP_PROTO_UDP)
901 session->state = COAP_SESSION_STATE_ESTABLISHED;
902 else
903 session->state = COAP_SESSION_STATE_NONE;
904
905 session->con_active = 0;
906
907 if (session->partial_pdu) {
908 coap_delete_pdu(session->partial_pdu);
909 session->partial_pdu = NULL;
910 }
911 session->partial_read = 0;
912
913 while (session->delayqueue) {
914 coap_queue_t *q = session->delayqueue;
915 session->delayqueue = q->next;
916 q->next = NULL;
917 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
918 coap_session_str(session), q->id);
919 if (q && q->pdu->type == COAP_MESSAGE_CON
920 && session->context->nack_handler) {
921 coap_check_update_token(session, q->pdu);
922 session->context->nack_handler(session, q->pdu, reason, q->id);
923 }
924 if (q)
925 coap_delete_node(q);
926 }
927
928#if COAP_CLIENT_SUPPORT
929 /* Need to do this before (D)TLS and socket is closed down */
930 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
931 LL_DELETE(session->lg_crcv, cq);
932 coap_block_delete_lg_crcv(session, cq);
933 }
934#endif /* COAP_CLIENT_SUPPORT */
935 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
936 LL_DELETE(session->lg_xmit, lq);
937 coap_block_delete_lg_xmit(session, lq);
938 }
939#if COAP_SERVER_SUPPORT
940 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
941 LL_DELETE(session->lg_srcv, sq);
942 coap_block_delete_lg_srcv(session, sq);
943 }
944#endif /* COAP_SERVER_SUPPORT */
945 coap_cancel_session_messages(session->context, session, reason);
946
947#if !COAP_DISABLE_TCP
948 if (COAP_PROTO_RELIABLE(session->proto)) {
949 if (coap_netif_available(session)) {
950 coap_handle_event(session->context,
951 state == COAP_SESSION_STATE_CONNECTING ?
952 COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session);
953 }
954 if (state != COAP_SESSION_STATE_NONE) {
955 coap_handle_event(session->context,
956 state == COAP_SESSION_STATE_ESTABLISHED ?
957 COAP_EVENT_SESSION_CLOSED : COAP_EVENT_SESSION_FAILED, session);
958 }
959 if (session->doing_first)
960 session->doing_first = 0;
961 }
962#endif /* !COAP_DISABLE_TCP */
963 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
964}
965
966#if COAP_SERVER_SUPPORT
967 static void
968coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
969 const coap_addr_tuple_t *addr_info) {
970 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
971 coap_address_copy(&addr_hash->remote, &addr_info->remote);
972 addr_hash->lport = coap_address_get_port(&addr_info->local);
973 addr_hash->proto = proto;
974}
975
976 coap_session_t *
977 coap_endpoint_get_session(coap_endpoint_t *endpoint,
978 const coap_packet_t *packet, coap_tick_t now) {
979 coap_session_t *session;
980 coap_session_t *rtmp;
981 unsigned int num_idle = 0;
982 unsigned int num_hs = 0;
983 coap_session_t *oldest = NULL;
984 coap_session_t *oldest_hs = NULL;
985 coap_addr_hash_t addr_hash;
986
987 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
988 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
989 if (session) {
990 /* Maybe mcast or unicast IP address which is not in the hash */
991 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
992 session->ifindex = packet->ifindex;
993 session->last_rx_tx = now;
994 return session;
995 }
996
997 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
998 if (session->ref == 0 && session->delayqueue == NULL) {
999 if (session->type == COAP_SESSION_TYPE_SERVER) {
1000 ++num_idle;
1001 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1002 oldest = session;
1003
1004 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1005 ++num_hs;
1006 /* See if this is a partial (D)TLS session set up
1007 which needs to be cleared down to prevent DOS */
1008 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1009 if (oldest_hs == NULL ||
1010 session->last_rx_tx < oldest_hs->last_rx_tx)
1011 oldest_hs = session;
1012 }
1013 }
1014 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1015 ++num_hs;
1016 /* See if this is a partial (D)TLS session set up for Client Hello
1017 which needs to be cleared down to prevent DOS */
1018 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1019 if (oldest_hs == NULL ||
1020 session->last_rx_tx < oldest_hs->last_rx_tx)
1021 oldest_hs = session;
1022 }
1023 }
1024 }
1025 }
1026
1027 if (endpoint->context->max_idle_sessions > 0 &&
1028 num_idle >= endpoint->context->max_idle_sessions) {
1029 coap_handle_event(oldest->context, COAP_EVENT_SERVER_SESSION_DEL, oldest);
1030 coap_session_free(oldest);
1031 } else if (oldest_hs) {
1032 coap_log_warn("***%s: Incomplete session timed out\n",
1033 coap_session_str(oldest_hs));
1034 coap_handle_event(oldest_hs->context, COAP_EVENT_SERVER_SESSION_DEL, oldest_hs);
1035 coap_session_free(oldest_hs);
1036 }
1037
1038 if (num_hs > (endpoint->context->max_handshake_sessions ?
1039 endpoint->context->max_handshake_sessions :
1040 COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS)) {
1041 /* Maxed out on number of sessions in (D)TLS negotiation state */
1042 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1043 "large. New request ignored\n");
1044 return NULL;
1045 }
1046
1047 if (endpoint->proto == COAP_PROTO_DTLS) {
1048 /*
1049 * Need to check that this actually is a Client Hello before wasting
1050 * time allocating and then freeing off session.
1051 */
1052
1053 /*
1054 * Generic header structure of the DTLS record layer.
1055 * typedef struct __attribute__((__packed__)) {
1056 * uint8_t content_type; content type of the included message
1057 * uint16_t version; Protocol version
1058 * uint16_t epoch; counter for cipher state changes
1059 * uint8_t sequence_number[6]; sequence number
1060 * uint16_t length; length of the following fragment
1061 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1062 * } dtls_record_handshake_t;
1063 */
1064#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1065#define DTLS_CT_ALERT 21 /* Content Type Alert */
1066#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1067#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1068#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1069
1070 const uint8_t *payload = (const uint8_t *)packet->payload;
1071 size_t length = packet->length;
1072 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1073 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1074 payload[OFF_CONTENT_TYPE], length,
1075 OFF_HANDSHAKE_TYPE + 1);
1076 return NULL;
1077 }
1078 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1079 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1080 /* only log if not a late alert */
1081 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1082 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1083 payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
1084 return NULL;
1085 }
1086 }
1087
1088 session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER,
1089 &addr_hash, &packet->addr_info.local,
1090 &packet->addr_info.remote,
1091 packet->ifindex, endpoint->context, endpoint);
1092 if (session) {
1093 session->last_rx_tx = now;
1094 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1095 sizeof(session->sock.lfunc));
1096 if (endpoint->proto == COAP_PROTO_UDP)
1097 session->state = COAP_SESSION_STATE_ESTABLISHED;
1098 else if (endpoint->proto == COAP_PROTO_DTLS) {
1099 session->type = COAP_SESSION_TYPE_HELLO;
1100 }
1101 SESSIONS_ADD(endpoint->sessions, session);
1102 coap_log_debug("***%s: session %p: new incoming session\n",
1103 coap_session_str(session), (void *)session);
1104 coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
1105 }
1106 return session;
1107}
1108
1109 coap_session_t *
1110 coap_session_new_dtls_session(coap_session_t *session,
1111 coap_tick_t now) {
1112 if (session) {
1113 session->last_rx_tx = now;
1114 session->type = COAP_SESSION_TYPE_SERVER;
1115 coap_dtls_establish(session);
1116 }
1117 return session;
1118}
1119#endif /* COAP_SERVER_SUPPORT */
1120
1121#if COAP_CLIENT_SUPPORT
1122 static coap_session_t *
1123coap_session_create_client(coap_context_t *ctx,
1124 const coap_address_t *local_if,
1125 const coap_address_t *server,
1126 coap_proto_t proto) {
1127 coap_session_t *session = NULL;
1128 int default_port = COAP_DEFAULT_PORT;
1129
1130 assert(server);
1131
1132 switch (proto) {
1133 case COAP_PROTO_UDP:
1134 default_port = COAP_DEFAULT_PORT;
1135 break;
1136 case COAP_PROTO_DTLS:
1137 if (!coap_dtls_is_supported()) {
1138 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1139 return NULL;
1140 }
1141 default_port = COAPS_DEFAULT_PORT;
1142 break;
1143 case COAP_PROTO_TCP:
1144 if (!coap_tcp_is_supported()) {
1145 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1146 return NULL;
1147 }
1148 default_port = COAP_DEFAULT_PORT;
1149 break;
1150 case COAP_PROTO_TLS:
1151 if (!coap_tls_is_supported()) {
1152 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1153 return NULL;
1154 }
1155 default_port = COAPS_DEFAULT_PORT;
1156 break;
1157 case COAP_PROTO_WS:
1158 if (!coap_ws_is_supported()) {
1159 coap_log_crit("coap_new_client_session*: WS not supported\n");
1160 return NULL;
1161 }
1162 default_port = 80;
1163 break;
1164 case COAP_PROTO_WSS:
1165 if (!coap_wss_is_supported()) {
1166 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1167 return NULL;
1168 }
1169 default_port = 443;
1170 break;
1171 case COAP_PROTO_NONE:
1172 case COAP_PROTO_LAST:
1173 default:
1174 assert(0);
1175 return NULL;
1176 }
1177 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1178 local_if, server, 0, ctx, NULL);
1179 if (!session)
1180 goto error;
1181
1182 coap_session_reference(session);
1183 session->sock.session = session;
1184 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1185 sizeof(session->sock.lfunc));
1186
1187 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1188 coap_session_t *s, *rtmp;
1189 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1190 goto error;
1191 }
1192 /* Check that this is not a duplicate 4-tuple */
1193 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1194 if (COAP_PROTO_NOT_RELIABLE(s->proto) &&
1195 coap_address_equals(&session->addr_info.local,
1196 &s->addr_info.local) &&
1197 coap_address_equals(&session->addr_info.remote,
1198 &s->addr_info.remote)) {
1199 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1200 coap_session_str(session), (void *)session);
1201 goto error;
1202 }
1203 }
1204#ifdef WITH_CONTIKI
1205 session->sock.context = ctx;
1206#endif /* WITH_CONTIKI */
1207#if !COAP_DISABLE_TCP
1208 } else if (COAP_PROTO_RELIABLE(proto)) {
1209 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1210 goto error;
1211 }
1212#endif /* !COAP_DISABLE_TCP */
1213 }
1214
1215#ifdef COAP_EPOLL_SUPPORT
1216 session->sock.session = session;
1217 coap_epoll_ctl_add(&session->sock,
1218 EPOLLIN |
1219 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1220 EPOLLOUT : 0),
1221 __func__);
1222#endif /* COAP_EPOLL_SUPPORT */
1223
1224 session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_WANT_READ;
1225 if (local_if)
1226 session->sock.flags |= COAP_SOCKET_BOUND;
1227#if COAP_SERVER_SUPPORT
1228 if (ctx->proxy_uri_resource)
1229 session->proxy_session = 1;
1230#endif /* COAP_SERVER_SUPPORT */
1231 SESSIONS_ADD(ctx->sessions, session);
1232 return session;
1233
1234error:
1235 /*
1236 * Need to add in the session as coap_session_release()
1237 * will call SESSIONS_DELETE in coap_session_free().
1238 */
1239 if (session)
1240 SESSIONS_ADD(ctx->sessions, session);
1241 coap_session_release(session);
1242 return NULL;
1243}
1244
1245 static void
1246coap_session_check_connect(coap_session_t *session) {
1247 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1248 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1249 }
1250#if !COAP_DISABLE_TCP
1251 if (COAP_PROTO_RELIABLE(session->proto)) {
1252 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1253 session->state = COAP_SESSION_STATE_CONNECTING;
1254 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1255 session->state != COAP_SESSION_STATE_NONE &&
1256 session->type == COAP_SESSION_TYPE_CLIENT) {
1257 session->doing_first = 1;
1258 }
1259 } else {
1260 /* Initial connect worked immediately */
1261 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1262 }
1263 }
1264#endif /* !COAP_DISABLE_TCP */
1265 coap_ticks(&session->last_rx_tx);
1266}
1267#endif /* COAP_CLIENT_SUPPORT */
1268
1269 void
1270 coap_session_establish(coap_session_t *session) {
1271 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1272 coap_session_connected(session);
1273#if !COAP_DISABLE_TCP
1274 if (COAP_PROTO_RELIABLE(session->proto))
1275 coap_session_send_csm(session);
1276#endif /* !COAP_DISABLE_TCP */
1277}
1278
1279#if COAP_CLIENT_SUPPORT
1280 coap_session_t *
1281 coap_new_client_session(coap_context_t *ctx,
1282 const coap_address_t *local_if,
1283 const coap_address_t *server,
1284 coap_proto_t proto) {
1285 coap_session_t *session = coap_session_create_client(ctx, local_if, server,
1286 proto);
1287 if (session) {
1288 coap_log_debug("***%s: session %p: created outgoing session\n",
1289 coap_session_str(session), (void *)session);
1290 coap_session_check_connect(session);
1291 }
1292 return session;
1293}
1294
1295 coap_session_t *
1296 coap_new_client_session_psk(coap_context_t *ctx,
1297 const coap_address_t *local_if,
1298 const coap_address_t *server,
1299 coap_proto_t proto, const char *identity,
1300 const uint8_t *key, unsigned key_len) {
1301 coap_dtls_cpsk_t setup_data;
1302
1303 memset(&setup_data, 0, sizeof(setup_data));
1304 setup_data.version = COAP_DTLS_CPSK_SETUP_VERSION;
1305
1306 if (identity) {
1307 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1308 setup_data.psk_info.identity.length = strlen(identity);
1309 }
1310
1311 if (key && key_len > 0) {
1312 setup_data.psk_info.key.s = key;
1313 setup_data.psk_info.key.length = key_len;
1314 }
1315
1316 return coap_new_client_session_psk2(ctx, local_if, server,
1317 proto, &setup_data);
1318}
1319
1320 coap_session_t *
1321 coap_new_client_session_psk2(coap_context_t *ctx,
1322 const coap_address_t *local_if,
1323 const coap_address_t *server,
1324 coap_proto_t proto,
1325 coap_dtls_cpsk_t *setup_data) {
1326 coap_session_t *session = coap_session_create_client(ctx, local_if,
1327 server, proto);
1328
1329 if (!session)
1330 return NULL;
1331
1332 session->cpsk_setup_data = *setup_data;
1333 if (setup_data->psk_info.identity.s) {
1334 session->psk_identity =
1335 coap_new_bin_const(setup_data->psk_info.identity.s,
1336 setup_data->psk_info.identity.length);
1337 if (!session->psk_identity) {
1338 coap_log_warn("Cannot store session Identity (PSK)\n");
1339 coap_session_release(session);
1340 return NULL;
1341 }
1342 } else if (coap_dtls_is_supported() || coap_tls_is_supported()) {
1343 coap_log_warn("Identity (PSK) not defined\n");
1344 coap_session_release(session);
1345 return NULL;
1346 }
1347
1348 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1349 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1350 setup_data->psk_info.key.length);
1351 if (!session->psk_key) {
1352 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1353 coap_session_release(session);
1354 return NULL;
1355 }
1356 } else if (coap_dtls_is_supported() || coap_tls_is_supported()) {
1357 coap_log_warn("Pre-shared key (PSK) not defined\n");
1358 coap_session_release(session);
1359 return NULL;
1360 }
1361
1362 if (coap_dtls_is_supported() || coap_tls_is_supported()) {
1363 if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
1364 coap_session_release(session);
1365 return NULL;
1366 }
1367 }
1368 coap_log_debug("***%s: new outgoing session\n",
1369 coap_session_str(session));
1370 coap_session_check_connect(session);
1371 return session;
1372}
1373#endif /* COAP_CLIENT_SUPPORT */
1374
1375 int
1376 coap_session_refresh_psk_hint(coap_session_t *session,
1377 const coap_bin_const_t *psk_hint
1378 ) {
1379 /* We may be refreshing the hint with the same hint */
1380 coap_bin_const_t *old_psk_hint = session->psk_hint;
1381
1382 if (psk_hint && psk_hint->s) {
1383 if (session->psk_hint) {
1384 if (coap_binary_equal(session->psk_hint, psk_hint))
1385 return 1;
1386 }
1387 session->psk_hint = coap_new_bin_const(psk_hint->s,
1388 psk_hint->length);
1389 if (!session->psk_hint) {
1390 coap_log_err("No memory to store identity hint (PSK)\n");
1391 if (old_psk_hint)
1392 coap_delete_bin_const(old_psk_hint);
1393 return 0;
1394 }
1395 } else {
1396 session->psk_hint = NULL;
1397 }
1398 if (old_psk_hint)
1399 coap_delete_bin_const(old_psk_hint);
1400
1401 return 1;
1402}
1403
1404 int
1405 coap_session_refresh_psk_key(coap_session_t *session,
1406 const coap_bin_const_t *psk_key
1407 ) {
1408 /* We may be refreshing the key with the same key */
1409 coap_bin_const_t *old_psk_key = session->psk_key;
1410
1411 if (psk_key && psk_key->s) {
1412 if (session->psk_key) {
1413 if (coap_binary_equal(session->psk_key, psk_key))
1414 return 1;
1415 }
1416 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1417 if (!session->psk_key) {
1418 coap_log_err("No memory to store pre-shared key (PSK)\n");
1419 if (old_psk_key)
1420 coap_delete_bin_const(old_psk_key);
1421 return 0;
1422 }
1423 } else {
1424 session->psk_key = NULL;
1425 }
1426 if (old_psk_key)
1427 coap_delete_bin_const(old_psk_key);
1428
1429 return 1;
1430}
1431
1432 int
1433 coap_session_refresh_psk_identity(coap_session_t *session,
1434 const coap_bin_const_t *psk_identity
1435 ) {
1436 /* We may be refreshing the identity with the same identity */
1437 coap_bin_const_t *old_psk_identity = session->psk_identity;
1438
1439 if (psk_identity && psk_identity->s) {
1440 if (session->psk_identity) {
1441 if (coap_binary_equal(session->psk_identity, psk_identity))
1442 return 1;
1443 }
1444 session->psk_identity = coap_new_bin_const(psk_identity->s,
1445 psk_identity->length);
1446 if (!session->psk_identity) {
1447 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1448 if (old_psk_identity)
1449 coap_delete_bin_const(old_psk_identity);
1450 return 0;
1451 }
1452 } else {
1453 session->psk_identity = NULL;
1454 }
1455 if (old_psk_identity)
1456 coap_delete_bin_const(old_psk_identity);
1457
1458 return 1;
1459}
1460
1461#if COAP_SERVER_SUPPORT
1462 const coap_bin_const_t *
1463 coap_session_get_psk_hint(const coap_session_t *session) {
1464 if (session)
1465 return session->psk_hint;
1466 return NULL;
1467}
1468#endif /* COAP_SERVER_SUPPORT */
1469
1470 const coap_bin_const_t *
1471 coap_session_get_psk_identity(const coap_session_t *session) {
1472 const coap_bin_const_t *psk_identity = NULL;
1473 if (session) {
1474 psk_identity = session->psk_identity;
1475 if (psk_identity == NULL) {
1476 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1477 }
1478 }
1479 return psk_identity;
1480}
1481
1482 const coap_bin_const_t *
1483 coap_session_get_psk_key(const coap_session_t *session) {
1484 if (session)
1485 return session->psk_key;
1486 return NULL;
1487}
1488
1489#if COAP_CLIENT_SUPPORT
1490 coap_session_t *
1491 coap_new_client_session_pki(coap_context_t *ctx,
1492 const coap_address_t *local_if,
1493 const coap_address_t *server,
1494 coap_proto_t proto,
1495 coap_dtls_pki_t *setup_data) {
1496 coap_session_t *session;
1497
1498 if (coap_dtls_is_supported() || coap_tls_is_supported()) {
1499 if (!setup_data) {
1500 return NULL;
1501 } else {
1502 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1503 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
1504 return NULL;
1505 }
1506 }
1507
1508 }
1509 session = coap_session_create_client(ctx, local_if, server, proto);
1510
1511 if (!session) {
1512 return NULL;
1513 }
1514
1515 if (coap_dtls_is_supported() || coap_tls_is_supported()) {
1516 /* we know that setup_data is not NULL */
1517 if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1518 coap_session_release(session);
1519 return NULL;
1520 }
1521 }
1522 coap_log_debug("***%s: new outgoing session\n",
1523 coap_session_str(session));
1524 coap_session_check_connect(session);
1525 return session;
1526}
1527#endif /* ! COAP_CLIENT_SUPPORT */
1528
1529#if COAP_SERVER_SUPPORT
1530#if !COAP_DISABLE_TCP
1531 coap_session_t *
1532 coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) {
1533 coap_session_t *session;
1534 session = coap_make_session(ep->proto, COAP_SESSION_TYPE_SERVER,
1535 NULL, NULL, NULL, 0, ctx, ep);
1536 if (!session)
1537 goto error;
1538
1539 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
1540 if (!coap_netif_strm_accept(ep, session))
1541 goto error;
1542
1543 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1544
1545#ifdef COAP_EPOLL_SUPPORT
1546 session->sock.session = session;
1547 coap_epoll_ctl_add(&session->sock,
1548 EPOLLIN,
1549 __func__);
1550#endif /* COAP_EPOLL_SUPPORT */
1551 SESSIONS_ADD(ep->sessions, session);
1552 if (session) {
1553 coap_log_debug("***%s: session %p: new incoming session\n",
1554 coap_session_str(session), (void *)session);
1555 coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
1556 coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
1557 session->state = COAP_SESSION_STATE_CONNECTING;
1558 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1559 }
1560 return session;
1561
1562error:
1563 /*
1564 * Need to add in the session as coap_session_release()
1565 * will call SESSIONS_DELETE in coap_session_free().
1566 */
1567 if (session) {
1568 SESSIONS_ADD(ep->sessions, session);
1569 coap_session_free(session);
1570 }
1571 return NULL;
1572}
1573#endif /* !COAP_DISABLE_TCP */
1574#endif /* COAP_SERVER_SUPPORT */
1575
1576 void
1577 coap_session_init_token(coap_session_t *session, size_t len,
1578 const uint8_t *data) {
1579 session->tx_token = coap_decode_var_bytes8(data, len);
1580}
1581
1582 void
1583 coap_session_new_token(coap_session_t *session, size_t *len,
1584 uint8_t *data) {
1585 *len = coap_encode_var_safe8(data,
1586 sizeof(session->tx_token), ++session->tx_token);
1587}
1588
1589uint16_t
1590 coap_new_message_id(coap_session_t *session) {
1591 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1592 return ++session->tx_mid;
1593 /* TCP/TLS have no notion of mid */
1594 return 0;
1595}
1596
1597 const coap_address_t *
1598 coap_session_get_addr_remote(const coap_session_t *session) {
1599 if (session)
1600 return &session->addr_info.remote;
1601 return NULL;
1602}
1603
1604 const coap_address_t *
1605 coap_session_get_addr_local(const coap_session_t *session) {
1606 if (session)
1607 return &session->addr_info.local;
1608 return NULL;
1609}
1610
1611 const coap_address_t *
1612 coap_session_get_addr_mcast(const coap_session_t *session) {
1613#if COAP_CLIENT_SUPPORT
1614 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
1615 session->sock.flags & COAP_SOCKET_MULTICAST)
1616 return &session->sock.mcast_addr;
1617#else /* ! COAP_CLIENT_SUPPORT */
1618 (void)session;
1619#endif /* ! COAP_CLIENT_SUPPORT */
1620 return NULL;
1621}
1622
1623 coap_context_t *
1624 coap_session_get_context(const coap_session_t *session) {
1625 if (session)
1626 return session->context;
1627 return NULL;
1628}
1629
1630 coap_proto_t
1631 coap_session_get_proto(const coap_session_t *session) {
1632 if (session)
1633 return session->proto;
1634 return 0;
1635}
1636
1637 coap_session_type_t
1638 coap_session_get_type(const coap_session_t *session) {
1639 if (session)
1640 return session->type;
1641 return 0;
1642}
1643
1644#if COAP_CLIENT_SUPPORT
1645 int
1646 coap_session_set_type_client(coap_session_t *session) {
1647#if COAP_SERVER_SUPPORT
1648 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1649 coap_session_reference(session);
1650 session->type = COAP_SESSION_TYPE_CLIENT;
1651 return 1;
1652 }
1653#else /* ! COAP_SERVER_SUPPORT */
1654 (void)session;
1655#endif /* ! COAP_SERVER_SUPPORT */
1656 return 0;
1657}
1658#endif /* COAP_CLIENT_SUPPORT */
1659
1660 coap_session_state_t
1661 coap_session_get_state(const coap_session_t *session) {
1662 if (session)
1663 return session->state;
1664 return 0;
1665}
1666
1667 int
1668 coap_session_get_ifindex(const coap_session_t *session) {
1669 if (session)
1670 return session->ifindex;
1671 return -1;
1672}
1673
1674 void *
1675 coap_session_get_tls(const coap_session_t *session,
1676 coap_tls_library_t *tls_lib) {
1677 if (session)
1678 return coap_dtls_get_tls(session, tls_lib);
1679 return NULL;
1680}
1681
1682 static const char *
1683 coap_proto_name(coap_proto_t proto) {
1684 switch (proto) {
1685 case COAP_PROTO_UDP:
1686 return "UDP ";
1687 case COAP_PROTO_DTLS:
1688 return "DTLS";
1689 case COAP_PROTO_TCP:
1690 return "TCP ";
1691 case COAP_PROTO_TLS:
1692 return "TLS ";
1693 case COAP_PROTO_WS:
1694 return "WS ";
1695 case COAP_PROTO_WSS:
1696 return "WSS ";
1697 case COAP_PROTO_NONE:
1698 case COAP_PROTO_LAST:
1699 default:
1700 return "????" ;
1701 break;
1702 }
1703 return NULL;
1704}
1705
1706#if COAP_SERVER_SUPPORT
1707 coap_endpoint_t *
1708 coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1709 coap_endpoint_t *ep = NULL;
1710
1711 assert(context);
1712 assert(listen_addr);
1713 assert(proto != COAP_PROTO_NONE);
1714
1715 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1716 coap_log_crit("coap_new_endpoint: DTLS not supported\n");
1717 goto error;
1718 }
1719
1720 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1721 coap_log_crit("coap_new_endpoint: TLS not supported\n");
1722 goto error;
1723 }
1724
1725 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1726 coap_log_crit("coap_new_endpoint: TCP not supported\n");
1727 goto error;
1728 }
1729
1730 if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
1731 coap_log_crit("coap_new_endpoint: WS not supported\n");
1732 goto error;
1733 }
1734
1735 if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
1736 coap_log_crit("coap_new_endpoint: WSS not supported\n");
1737 goto error;
1738 }
1739
1740 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
1741 proto == COAP_PROTO_WSS) {
1742 if (!coap_dtls_context_check_keys_enabled(context)) {
1743 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
1744 "coap_context_set_pki() not called\n");
1745 goto error;
1746 }
1747 }
1748
1749 ep = coap_malloc_endpoint();
1750 if (!ep) {
1751 coap_log_warn("coap_new_endpoint: malloc");
1752 goto error;
1753 }
1754
1755 memset(ep, 0, sizeof(coap_endpoint_t));
1756 ep->context = context;
1757 ep->proto = proto;
1758 ep->sock.endpoint = ep;
1759 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
1760
1761 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1762 if (!coap_netif_dgrm_listen(ep, listen_addr))
1763 goto error;
1764#ifdef WITH_CONTIKI
1765 ep->sock.context = context;
1766#endif /* WITH_CONTIKI */
1767#if !COAP_DISABLE_TCP
1768 } else if (COAP_PROTO_RELIABLE(proto)) {
1769 if (!coap_netif_strm_listen(ep, listen_addr))
1770 goto error;
1771#endif /* !COAP_DISABLE_TCP */
1772 } else {
1773 coap_log_crit("coap_new_endpoint: protocol not supported\n");
1774 goto error;
1775 }
1776
1777 if (COAP_LOG_DEBUG <= coap_get_log_level()) {
1778#ifndef INET6_ADDRSTRLEN
1779#define INET6_ADDRSTRLEN 40
1780#endif
1781 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1782
1783 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1784 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
1785 addr_str);
1786 }
1787 }
1788
1789 ep->default_mtu = COAP_DEFAULT_MTU;
1790
1791#ifdef COAP_EPOLL_SUPPORT
1792 ep->sock.endpoint = ep;
1793 coap_epoll_ctl_add(&ep->sock,
1794 EPOLLIN,
1795 __func__);
1796#endif /* COAP_EPOLL_SUPPORT */
1797
1798 LL_PREPEND(context->endpoint, ep);
1799 return ep;
1800
1801error:
1802 coap_free_endpoint(ep);
1803 return NULL;
1804}
1805
1806 void
1807 coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu) {
1808 ep->default_mtu = (uint16_t)mtu;
1809}
1810
1811 void
1812 coap_free_endpoint(coap_endpoint_t *ep) {
1813 if (ep) {
1814 coap_session_t *session, *rtmp;
1815
1816 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
1817 assert(session->ref == 0);
1818 if (session->ref == 0) {
1819 coap_session_free(session);
1820 }
1821 }
1822 if (coap_netif_available_ep(ep)) {
1823 /*
1824 * ep->sock.endpoint is set in coap_new_endpoint().
1825 * ep->sock.session is never set.
1826 *
1827 * session->sock.session is set for both clients and servers (when a
1828 * new session is accepted), but does not affect the endpoint.
1829 *
1830 * So, it is safe to call coap_netif_close_ep() after all the sessions
1831 * have been freed above as we are only working with the endpoint sock.
1832 */
1833#ifdef COAP_EPOLL_SUPPORT
1834 assert(ep->sock.session == NULL);
1835#endif /* COAP_EPOLL_SUPPORT */
1836 coap_netif_close_ep(ep);
1837 }
1838
1839 if (ep->context && ep->context->endpoint) {
1840 LL_DELETE(ep->context->endpoint, ep);
1841 }
1842 coap_mfree_endpoint(ep);
1843 }
1844}
1845#endif /* COAP_SERVER_SUPPORT */
1846
1847 coap_session_t *
1848 coap_session_get_by_peer(const coap_context_t *ctx,
1849 const coap_address_t *remote_addr,
1850 int ifindex) {
1851 coap_session_t *s, *rtmp;
1852#if COAP_CLIENT_SUPPORT
1853 SESSIONS_ITER(ctx->sessions, s, rtmp) {
1854 if (s->ifindex == ifindex) {
1855 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
1856 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
1857 return s;
1858 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
1859 return s;
1860 }
1861 }
1862#endif /* COAP_CLIENT_SUPPORT */
1863#if COAP_SERVER_SUPPORT
1864 coap_endpoint_t *ep;
1865
1866 LL_FOREACH(ctx->endpoint, ep) {
1867 SESSIONS_ITER(ep->sessions, s, rtmp) {
1868 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1869 remote_addr))
1870 return s;
1871 }
1872 }
1873#endif /* COAP_SERVER_SUPPORT */
1874 return NULL;
1875}
1876
1877#ifndef INET6_ADDRSTRLEN
1878#define INET6_ADDRSTRLEN 46
1879#endif
1880 const char *
1881 coap_session_str(const coap_session_t *session) {
1882 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
1883 char *p = szSession, *end = szSession + sizeof(szSession);
1884 if (coap_print_addr(&session->addr_info.local,
1885 (unsigned char *)p, end - p) > 0)
1886 p += strlen(p);
1887 if (p + 6 < end) {
1888 strcpy(p, " <-> ");
1889 p += 5;
1890 }
1891 if (p + 1 < end) {
1892 if (coap_print_addr(&session->addr_info.remote,
1893 (unsigned char *)p, end - p) > 0)
1894 p += strlen(p);
1895 }
1896 if (session->ifindex > 0 && p + 1 < end)
1897 p += snprintf(p, end - p, " (if%d)", session->ifindex);
1898 if (p + 6 < end) {
1899 strcpy(p, " ");
1900 p++;
1901 strcpy(p, coap_proto_name(session->proto));
1902 }
1903
1904 return szSession;
1905}
1906
1907#if COAP_SERVER_SUPPORT
1908 const char *
1909 coap_endpoint_str(const coap_endpoint_t *endpoint) {
1910 static char szEndpoint[128];
1911 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
1912 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
1913 p += strlen(p);
1914 if (p + 6 < end) {
1915 if (endpoint->proto == COAP_PROTO_UDP) {
1916 strcpy(p, " UDP");
1917 p += 4;
1918 } else if (endpoint->proto == COAP_PROTO_DTLS) {
1919 strcpy(p, " DTLS");
1920 p += 5;
1921 } else {
1922 strcpy(p, " NONE");
1923 p += 5;
1924 }
1925 }
1926
1927 return szEndpoint;
1928}
1929#endif /* COAP_SERVER_SUPPORT */
1930#if COAP_CLIENT_SUPPORT
1931 void
1932 coap_session_set_no_observe_cancel(coap_session_t *session) {
1933 session->no_observe_cancel = 1;
1934}
1935#endif /* COAP_CLIENT_SUPPORT */
1936#endif /* COAP_SESSION_C_ */
coap_address_init
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:248
coap_address_get_port
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:38
coap_address_copy
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.c:803
coap_address_equals
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:75
coap_internal.h
Pulls together all the internal only header files.
PRIu32
#define PRIu32
Definition: coap_internal.h:45
coap_nack_reason_t
coap_nack_reason_t
Definition: coap_io.h:69
COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:71
COAP_NACK_WS_FAILED
@ COAP_NACK_WS_FAILED
Definition: coap_io.h:78
COAP_NACK_TOO_MANY_RETRIES
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:70
COAP_NACK_TLS_FAILED
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:73
COAP_NACK_TLS_LAYER_FAILED
@ COAP_NACK_TLS_LAYER_FAILED
Definition: coap_io.h:76
COAP_NACK_ICMP_ISSUE
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:74
COAP_NACK_WS_LAYER_FAILED
@ COAP_NACK_WS_LAYER_FAILED
Definition: coap_io.h:77
COAP_NACK_RST
@ COAP_NACK_RST
Definition: coap_io.h:72
COAP_NACK_BAD_RESPONSE
@ COAP_NACK_BAD_RESPONSE
Definition: coap_io.h:75
COAP_SOCKET_MULTICAST
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Definition: coap_io_internal.h:73
coap_epoll_ctl_add
void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to add the state of events that epoll is to track for the appropriate file de...
COAP_SOCKET_NOT_EMPTY
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
Definition: coap_io_internal.h:62
COAP_SOCKET_BOUND
#define COAP_SOCKET_BOUND
the socket is bound
Definition: coap_io_internal.h:63
COAP_SOCKET_WANT_READ
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
Definition: coap_io_internal.h:65
coap_malloc_endpoint
coap_endpoint_t * coap_malloc_endpoint(void)
COAP_SOCKET_WANT_CONNECT
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
Definition: coap_io_internal.h:68
coap_mfree_endpoint
void coap_mfree_endpoint(coap_endpoint_t *ep)
coap_layers_coap
coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST]
Definition: coap_layers.c:39
COAP_LAYER_SESSION
@ COAP_LAYER_SESSION
Definition: coap_layers_internal.h:23
COAP_SESSION
@ COAP_SESSION
Definition: coap_mem.h:50
COAP_STRING
@ COAP_STRING
Definition: coap_mem.h:38
coap_malloc_type
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_dtls_context_set_pki
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition: coap_notls.c:77
coap_dtls_get_tls
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition: coap_notls.c:122
coap_dtls_get_overhead
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:224
coap_dtls_context_check_keys_enabled
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition: coap_notls.c:111
coap_session_send_ping
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:738
coap_proto_name
static const char * coap_proto_name(coap_proto_t proto)
Definition: coap_session.c:1683
coap_nack_name
static const char * coap_nack_name(coap_nack_reason_t reason)
Definition: coap_session.c:821
coap_make_session
static coap_session_t * coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addr_hash_t *addr_hash, const coap_address_t *local_addr, const coap_address_t *remote_addr, int ifindex, coap_context_t *context, coap_endpoint_t *endpoint)
Definition: coap_session.c:388
coap_session_max_pdu_size_internal
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
Definition: coap_session.c:575
INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN
Definition: coap_session.c:1878
coap_session_set_no_observe_cancel
void coap_session_set_no_observe_cancel(coap_session_t *session)
Disable client automatically sending observe cancel on session close.
SESSIONS_ADD
#define SESSIONS_ADD(e, obj)
Definition: coap_session_internal.h:529
SESSIONS_ITER_SAFE
#define SESSIONS_ITER_SAFE(e, el, rtmp)
Definition: coap_session_internal.h:538
COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
Definition: coap_session_internal.h:26
SESSIONS_DELETE
#define SESSIONS_DELETE(e, obj)
Definition: coap_session_internal.h:532
COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
Definition: coap_session_internal.h:27
SESSIONS_ITER
#define SESSIONS_ITER(e, el, rtmp)
Definition: coap_session_internal.h:535
SESSIONS_FIND
#define SESSIONS_FIND(e, k, res)
Definition: coap_session_internal.h:541
coap_tcp_is_supported
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:37
coap_block_delete_lg_srcv
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
coap_block_delete_lg_crcv
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
coap_check_update_token
void coap_check_update_token(coap_session_t *session, coap_pdu_t *pdu)
The function checks if the token needs to be updated before PDU is presented to the application (only...
coap_block_delete_lg_xmit
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: coap_block.c:2139
coap_cancel_observe
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
coap_delete_cache_entry
void coap_delete_cache_entry(coap_context_t *context, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
COAP_DEFAULT_NON_MAX_RETRANSMIT
#define COAP_DEFAULT_NON_MAX_RETRANSMIT
The number of times for requests for re-transmission of missing Q-Block1 when no response has been re...
Definition: coap_session.h:518
coap_session_get_non_max_retransmit
uint16_t coap_session_get_non_max_retransmit(const coap_session_t *session)
Get the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition: coap_session.c:318
coap_session_get_default_leisure
coap_fixed_point_t coap_session_get_default_leisure(const coap_session_t *session)
Get the CoAP default leisure time RFC7252 DEFAULT_LEISURE.
Definition: coap_session.c:298
coap_session_set_max_retransmit
void coap_session_set_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:174
COAP_DEFAULT_NON_TIMEOUT
#define COAP_DEFAULT_NON_TIMEOUT
The delay (+ ACK_RANDOM_FACTOR) to introduce once NON MAX_PAYLOADS Q-Block1 or Q-Block2 have been sen...
Definition: coap_session.h:527
coap_session_get_non_timeout
coap_fixed_point_t coap_session_get_non_timeout(const coap_session_t *session)
Get the CoAP MAX_PAYLOADS limit delay timeout.
Definition: coap_session.c:328
coap_session_set_ack_random_factor
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:158
COAP_DEFAULT_MAX_LATENCY
#define COAP_DEFAULT_MAX_LATENCY
The MAX_LATENCY definition.
Definition: coap_session.h:542
coap_session_get_ack_random_factor
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:283
COAP_DEFAULT_ACK_RANDOM_FACTOR
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
Definition: coap_session.h:467
coap_session_get_max_retransmit
uint16_t coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:288
coap_session_set_max_payloads
void coap_session_set_max_payloads(coap_session_t *session, uint16_t value)
Set the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition: coap_session.c:212
COAP_DEFAULT_MAX_RETRANSMIT
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
Definition: coap_session.h:475
coap_session_get_probing_rate
uint32_t coap_session_get_probing_rate(const coap_session_t *session)
Get the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
Definition: coap_session.c:303
COAP_DEFAULT_ACK_TIMEOUT
#define COAP_DEFAULT_ACK_TIMEOUT
Number of seconds when to expect an ACK or a response to an outstanding CON message.
Definition: coap_session.h:458
COAP_DEFAULT_DEFAULT_LEISURE
#define COAP_DEFAULT_DEFAULT_LEISURE
The number of seconds to use as bounds for multicast traffic RFC 7252, Section 4.8 Default value of D...
Definition: coap_session.h:492
coap_session_set_ack_timeout
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:148
COAP_DEFAULT_NSTART
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:484
coap_session_set_non_receive_timeout
void coap_session_set_non_receive_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non receive timeout delay timeout.
Definition: coap_session.c:262
coap_session_set_nstart
void coap_session_set_nstart(coap_session_t *session, uint16_t value)
Set the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:183
COAP_DEFAULT_PROBING_RATE
#define COAP_DEFAULT_PROBING_RATE
The number of bytes/second allowed when there is no response RFC 7252, Section 4.8 Default value of P...
Definition: coap_session.h:500
coap_session_set_non_max_retransmit
void coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition: coap_session.c:228
coap_session_get_max_payloads
uint16_t coap_session_get_max_payloads(const coap_session_t *session)
Get the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition: coap_session.c:308
COAP_DEFAULT_NON_RECEIVE_TIMEOUT
#define COAP_DEFAULT_NON_RECEIVE_TIMEOUT
The time to wait for any missing Q-Block1 or Q-Block2 packets before requesting re-transmission of mi...
Definition: coap_session.h:536
coap_session_set_probing_rate
void coap_session_set_probing_rate(coap_session_t *session, uint32_t value)
Set the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
Definition: coap_session.c:203
coap_session_set_default_leisure
void coap_session_set_default_leisure(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP default leisure time (for multicast) RFC7252 DEFAULT_LEISURE.
Definition: coap_session.c:192
coap_session_get_non_receive_timeout
coap_fixed_point_t coap_session_get_non_receive_timeout(const coap_session_t *session)
Get the CoAP non receive timeout delay timeout.
Definition: coap_session.c:338
coap_session_get_nstart
uint16_t coap_session_get_nstart(const coap_session_t *session)
Get the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:293
COAP_DEFAULT_MAX_PAYLOADS
#define COAP_DEFAULT_MAX_PAYLOADS
Number of Q-Block1 or Q-Block2 payloads that can be sent in a burst before a delay has to kick in.
Definition: coap_session.h:509
coap_session_get_ack_timeout
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:278
coap_session_set_non_timeout
void coap_session_set_non_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non timeout delay timeout.
Definition: coap_session.c:244
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:144
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:159
coap_prng
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:140
coap_remove_from_queue
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: coap_net.c:2237
coap_delete_node
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: coap_net.c:203
coap_client_delay_first
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition: coap_net.c:986
coap_send_internal
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1381
coap_calc_timeout
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout',...
Definition: coap_net.c:881
coap_wait_ack
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: coap_net.c:907
coap_new_node
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: coap_net.c:232
coap_cancel_session_messages
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
Definition: coap_net.c:2282
coap_new_message_id
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition: coap_session.c:1590
coap_handle_event
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition: coap_net.c:3917
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_dtls_establish
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
Definition: coap_dtls.c:21
coap_session_new_dtls_session
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
coap_dtls_context_set_cpsk
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
coap_tls_is_supported
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:28
COAP_DTLS_PKI_SETUP_VERSION
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition: coap_dtls.h:279
coap_dtls_is_supported
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:23
COAP_DTLS_CPSK_SETUP_VERSION
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition: coap_dtls.h:373
coap_tls_library_t
coap_tls_library_t
Definition: coap_dtls.h:92
COAP_DTLS_ROLE_CLIENT
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition: coap_dtls.h:44
coap_encode_var_safe
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: coap_encode.c:47
coap_decode_var_bytes8
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: coap_encode.c:67
coap_encode_var_safe8
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: coap_encode.c:77
COAP_EVENT_SESSION_CONNECTED
@ COAP_EVENT_SESSION_CONNECTED
Triggered when TCP layer completes exchange of CSM information.
Definition: coap_event.h:61
COAP_EVENT_TCP_FAILED
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition: coap_event.h:55
COAP_EVENT_SESSION_FAILED
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition: coap_event.h:65
COAP_EVENT_SERVER_SESSION_NEW
@ COAP_EVENT_SERVER_SESSION_NEW
Called in the CoAP IO loop if a new server-side session is created due to an incoming connection.
Definition: coap_event.h:85
COAP_EVENT_SESSION_CLOSED
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition: coap_event.h:63
COAP_EVENT_SERVER_SESSION_DEL
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
Definition: coap_event.h:94
COAP_EVENT_TCP_CLOSED
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition: coap_event.h:53
COAP_EVENT_TCP_CONNECTED
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition: coap_event.h:51
coap_log_debug
#define coap_log_debug(...)
Definition: coap_debug.h:120
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:91
coap_print_addr
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:218
coap_endpoint_str
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition: coap_session.c:1881
coap_log_info
#define coap_log_info(...)
Definition: coap_debug.h:108
coap_log_warn
#define coap_log_warn(...)
Definition: coap_debug.h:102
coap_log_err
#define coap_log_err(...)
Definition: coap_debug.h:96
coap_log_crit
#define coap_log_crit(...)
Definition: coap_debug.h:90
COAP_LOG_DEBUG
@ COAP_LOG_DEBUG
Definition: coap_debug.h:58
coap_netif_dgrm_listen
int coap_netif_dgrm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif datagram listem (udp).
coap_netif_close_ep
void coap_netif_close_ep(coap_endpoint_t *endpoint)
Layer function interface for Netif close for a endpoint.
coap_netif_strm_connect1
int coap_netif_strm_connect1(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif stream connect (tcp).
coap_netif_strm_listen
int coap_netif_strm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif stream listem (tcp).
coap_netif_strm_accept
int coap_netif_strm_accept(coap_endpoint_t *endpoint, coap_session_t *session)
Layer function interface for Netif stream accept.
coap_netif_dgrm_connect
int coap_netif_dgrm_connect(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif datagram connect (udp).
coap_netif_available
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition: coap_netif.c:25
coap_netif_available_ep
int coap_netif_available_ep(coap_endpoint_t *endpoint)
Function interface to check whether netif for endpoint is still available.
coap_delete_oscore_associations
void coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
coap_update_token
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition: coap_pdu.c:361
COAP_PDU_DELAYED
#define COAP_PDU_DELAYED
Definition: coap_pdu_internal.h:85
COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_MAX_MESSAGE_SIZE_TCP8
Definition: coap_pdu_internal.h:44
COAP_MAX_MESSAGE_SIZE_TCP0
#define COAP_MAX_MESSAGE_SIZE_TCP0
Definition: coap_pdu_internal.h:43
coap_pdu_encode_header
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: coap_pdu.c:1405
COAP_MAX_MESSAGE_SIZE_TCP16
#define COAP_MAX_MESSAGE_SIZE_TCP16
Definition: coap_pdu_internal.h:45
coap_add_option_internal
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition: coap_pdu.c:701
COAP_DEFAULT_PORT
#define COAP_DEFAULT_PORT
Definition: coap_pdu.h:37
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: coap_pdu.c:163
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:255
COAP_TOKEN_DEFAULT_MAX
#define COAP_TOKEN_DEFAULT_MAX
Definition: coap_pdu.h:56
COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
Definition: coap_pdu.h:191
coap_proto_t
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:304
COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: coap_pdu.h:190
COAPS_DEFAULT_PORT
#define COAPS_DEFAULT_PORT
Definition: coap_pdu.h:38
coap_pdu_init
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: coap_pdu.c:97
COAP_INVALID_MID
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: coap_pdu.h:258
COAP_DEFAULT_MTU
#define COAP_DEFAULT_MTU
Definition: coap_pdu.h:41
COAP_BERT_BASE
#define COAP_BERT_BASE
Definition: coap_pdu.h:44
COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: coap_pdu.h:189
COAP_PROTO_WS
@ COAP_PROTO_WS
Definition: coap_pdu.h:310
COAP_PROTO_DTLS
@ COAP_PROTO_DTLS
Definition: coap_pdu.h:307
COAP_PROTO_UDP
@ COAP_PROTO_UDP
Definition: coap_pdu.h:306
COAP_PROTO_NONE
@ COAP_PROTO_NONE
Definition: coap_pdu.h:305
COAP_PROTO_TLS
@ COAP_PROTO_TLS
Definition: coap_pdu.h:309
COAP_PROTO_WSS
@ COAP_PROTO_WSS
Definition: coap_pdu.h:311
COAP_PROTO_TCP
@ COAP_PROTO_TCP
Definition: coap_pdu.h:308
COAP_PROTO_LAST
@ COAP_PROTO_LAST
Definition: coap_pdu.h:312
COAP_SIGNALING_CODE_CSM
@ COAP_SIGNALING_CODE_CSM
Definition: coap_pdu.h:357
COAP_SIGNALING_CODE_PING
@ COAP_SIGNALING_CODE_PING
Definition: coap_pdu.h:358
COAP_MESSAGE_NON
@ COAP_MESSAGE_NON
Definition: coap_pdu.h:66
COAP_MESSAGE_CON
@ COAP_MESSAGE_CON
Definition: coap_pdu.h:65
COAP_NON_PROBING_WAIT_BASE
#define COAP_NON_PROBING_WAIT_BASE(s)
Definition: coap_session_internal.h:403
coap_new_server_session
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
coap_session_delay_pdu
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: coap_session.c:650
coap_session_refresh_psk_hint
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
Definition: coap_session.c:1376
coap_session_send_csm
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
Definition: coap_session.c:691
coap_add_fixed_uint
coap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:60
coap_session_max_pdu_rcv_size
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
Definition: coap_session.c:598
coap_sub_fixed_uint
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:68
coap_endpoint_get_session
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
COAP_ACK_RANDOM_FACTOR
#define COAP_ACK_RANDOM_FACTOR(s)
Definition: coap_session_internal.h:390
coap_session_establish
void coap_session_establish(coap_session_t *session)
Layer function interface for layer below session accept/connect being established.
Definition: coap_session.c:1270
coap_add_fixed_fixed
coap_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition: coap_session.c:50
coap_get_non_timeout_random_ticks
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
COAP_NSTART
#define COAP_NSTART(s)
Definition: coap_session_internal.h:392
coap_session_refresh_psk_key
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
Definition: coap_session.c:1405
coap_session_connected
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition: coap_session.c:759
coap_multi_fixed_fixed
coap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition: coap_session.c:30
coap_session_send_pdu
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition: coap_net.c:784
coap_div_fixed_uint
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:76
coap_session_free
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:545
coap_session_mfree
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:464
coap_session_refresh_psk_identity
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
Definition: coap_session.c:1433
coap_get_non_timeout_random
coap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session)
COAP_NON_MAX_RETRANSMIT
#define COAP_NON_MAX_RETRANSMIT(s)
Definition: coap_session_internal.h:397
COAP_NON_TIMEOUT
#define COAP_NON_TIMEOUT(s)
Definition: coap_session_internal.h:398
coap_multi_fixed_uint
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:40
COAP_NON_PARTIAL_TIMEOUT
#define COAP_NON_PARTIAL_TIMEOUT(s)
Definition: coap_session_internal.h:404
COAP_EXT_T_CHECKED
@ COAP_EXT_T_CHECKED
Token size valid.
Definition: coap_session_internal.h:60
coap_session_type_t
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:43
coap_session_set_mtu
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:635
coap_session_get_context
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
Definition: coap_session.c:1624
coap_session_get_addr_local
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address and port from the session.
Definition: coap_session.c:1605
coap_session_get_proto
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
Definition: coap_session.c:1631
coap_session_max_pdu_size
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:608
coap_session_get_psk_key
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
Definition: coap_session.c:1483
coap_session_get_tls
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
Definition: coap_session.c:1675
coap_session_get_state
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
Definition: coap_session.c:1661
coap_free_endpoint
void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
coap_session_state_t
coap_session_state_t
coap_session_state_t values
Definition: coap_session.h:54
coap_new_client_session_pki
coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
coap_new_endpoint
coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
COAP_PROTO_NOT_RELIABLE
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:36
coap_session_init_token
void coap_session_init_token(coap_session_t *session, size_t len, const uint8_t *data)
Initializes the token value to use as a starting point.
Definition: coap_session.c:1577
COAP_PROTO_RELIABLE
#define COAP_PROTO_RELIABLE(p)
Definition: coap_session.h:37
coap_new_client_session_psk
coap_session_t * coap_new_client_session_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
coap_session_new_token
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
Definition: coap_session.c:1583
coap_session_get_psk_identity
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
Definition: coap_session.c:1471
coap_session_set_app_data
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
Definition: coap_session.c:376
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:354
coap_session_get_by_peer
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
Get the session associated with the specified remote_addr and index.
Definition: coap_session.c:1848
coap_endpoint_set_default_mtu
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
coap_session_get_psk_hint
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
coap_session_get_addr_remote
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
Definition: coap_session.c:1598
coap_new_client_session
coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
coap_session_set_type_client
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
coap_new_client_session_psk2
coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
coap_session_disconnected
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:848
coap_session_get_addr_mcast
const coap_address_t * coap_session_get_addr_mcast(const coap_session_t *session)
Get the remote multicast IP address and port from the session if the original target IP was multicast...
Definition: coap_session.c:1612
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:348
coap_session_get_ifindex
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
Definition: coap_session.c:1668
coap_session_get_app_data
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:382
coap_session_get_type
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
Definition: coap_session.c:1638
COAP_SESSION_TYPE_HELLO
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition: coap_session.h:47
COAP_SESSION_TYPE_SERVER
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:46
COAP_SESSION_TYPE_CLIENT
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:45
COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:57
COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:58
COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:59
COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:55
COAP_SESSION_STATE_CONNECTING
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:56
coap_delete_bin_const
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: coap_str.c:120
coap_delete_str_const
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: coap_str.c:61
coap_new_bin_const
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition: coap_str.c:110
coap_binary_equal
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition: coap_str.h:203
coap_delete_observers
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
coap_ws_is_supported
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition: coap_ws.c:935
coap_wss_is_supported
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition: coap_ws.c:940
coap_addr_hash_t
Only used for servers for hashing incoming packets.
Definition: coap_session_internal.h:40
coap_addr_hash_t::lport
uint16_t lport
local port
Definition: coap_session_internal.h:42
coap_addr_hash_t::remote
coap_address_t remote
remote address and port
Definition: coap_session_internal.h:41
coap_addr_hash_t::proto
coap_proto_t proto
CoAP protocol.
Definition: coap_session_internal.h:43
coap_addr_tuple_t
Definition: coap_io.h:55
coap_addr_tuple_t::remote
coap_address_t remote
remote address and port
Definition: coap_io.h:56
coap_addr_tuple_t::local
coap_address_t local
local address and port
Definition: coap_io.h:57
coap_address_t
Multi-purpose address abstraction.
Definition: coap_address.h:109
coap_bin_const_t
CoAP binary data definition with const data.
Definition: coap_str.h:64
coap_bin_const_t::length
size_t length
length of binary data
Definition: coap_str.h:65
coap_bin_const_t::s
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
coap_cache_entry_t
Definition: coap_cache_internal.h:41
coap_cache_entry_t::session
coap_session_t * session
Definition: coap_cache_internal.h:44
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: coap_net_internal.h:49
coap_context_t::sessions
coap_session_t * sessions
client sessions
Definition: coap_net_internal.h:78
coap_context_t::nack_handler
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
Definition: coap_net_internal.h:102
coap_context_t::csm_max_message_size
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
Definition: coap_net_internal.h:161
coap_context_t::max_handshake_sessions
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
Definition: coap_net_internal.h:153
coap_context_t::sendqueue
coap_queue_t * sendqueue
Definition: coap_net_internal.h:73
coap_context_t::max_token_size
uint32_t max_token_size
Largest token size supported RFC8974.
Definition: coap_net_internal.h:172
coap_context_t::cache
coap_cache_entry_t * cache
CoAP cache-entry cache.
Definition: coap_net_internal.h:165
coap_context_t::endpoint
coap_endpoint_t * endpoint
the endpoints used for listening
Definition: coap_net_internal.h:75
coap_context_t::proxy_uri_resource
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
Definition: coap_net_internal.h:56
coap_context_t::block_mode
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
Definition: coap_net_internal.h:191
coap_context_t::max_idle_sessions
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
Definition: coap_net_internal.h:150
coap_dtls_cpsk_info_t::key
coap_bin_const_t key
Definition: coap_dtls.h:349
coap_dtls_cpsk_info_t::identity
coap_bin_const_t identity
Definition: coap_dtls.h:348
coap_dtls_cpsk_t
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:378
coap_dtls_cpsk_t::version
uint8_t version
Definition: coap_dtls.h:379
coap_dtls_cpsk_t::psk_info
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition: coap_dtls.h:407
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:284
coap_dtls_pki_t::version
uint8_t version
Definition: coap_dtls.h:285
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_session_internal.h:219
coap_endpoint_t::context
coap_context_t * context
endpoint's context
Definition: coap_session_internal.h:221
coap_endpoint_t::default_mtu
uint16_t default_mtu
default mtu for this interface
Definition: coap_session_internal.h:223
coap_endpoint_t::sessions
coap_session_t * sessions
hash table or list of active sessions
Definition: coap_session_internal.h:227
coap_endpoint_t::bind_addr
coap_address_t bind_addr
local interface address
Definition: coap_session_internal.h:226
coap_endpoint_t::sock
coap_socket_t sock
socket object for the interface, if any
Definition: coap_session_internal.h:224
coap_endpoint_t::proto
coap_proto_t proto
protocol used on this interface
Definition: coap_session_internal.h:222
coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition: coap_session.h:30
coap_fixed_point_t::fractional_part
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:32
coap_fixed_point_t::integer_part
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:31
coap_layer_func_t::l_establish
coap_layer_establish_t l_establish
Definition: coap_layers_internal.h:109
coap_layer_func_t::l_close
coap_layer_close_t l_close
Definition: coap_layers_internal.h:110
coap_lg_crcv_t
Structure to hold large body (many blocks) client receive information.
Definition: coap_block_internal.h:146
coap_lg_crcv_t::pdu
coap_pdu_t pdu
skeletal PDU
Definition: coap_block_internal.h:167
coap_lg_crcv_t::app_token
coap_binary_t * app_token
app requesting PDU token
Definition: coap_block_internal.h:162
coap_lg_crcv_t::observe_set
uint8_t observe_set
Set if this is an observe receive PDU.
Definition: coap_block_internal.h:150
coap_lg_srcv_t
Structure to hold large body (many blocks) server receive information.
Definition: coap_block_internal.h:177
coap_lg_xmit_t
Structure to hold large body (many blocks) transmission information.
Definition: coap_block_internal.h:117
coap_packet_t
Definition: coap_io_internal.h:207
coap_packet_t::length
size_t length
length of payload
Definition: coap_io_internal.h:210
coap_packet_t::addr_info
coap_addr_tuple_t addr_info
local and remote addresses
Definition: coap_io_internal.h:208
coap_packet_t::payload
unsigned char * payload
payload
Definition: coap_io_internal.h:211
coap_packet_t::ifindex
int ifindex
the interface index
Definition: coap_io_internal.h:209
coap_pdu_t
structure for CoAP PDUs
Definition: coap_pdu_internal.h:119
coap_pdu_t::hdr_size
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
Definition: coap_pdu_internal.h:126
coap_pdu_t::actual_token
coap_bin_const_t actual_token
Actual token in pdu.
Definition: coap_pdu_internal.h:132
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition: coap_pdu_internal.h:123
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition: coap_pdu_internal.h:135
coap_pdu_t::session
coap_session_t * session
Session responsible for PDU or NULL.
Definition: coap_pdu_internal.h:158
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition: coap_pdu_internal.h:120
coap_queue_t
Queue entry.
Definition: coap_net_internal.h:34
coap_queue_t::session
coap_session_t * session
the CoAP session
Definition: coap_net_internal.h:41
coap_queue_t::pdu
coap_pdu_t * pdu
the CoAP PDU to send
Definition: coap_net_internal.h:43
coap_queue_t::timeout
unsigned int timeout
the randomized timeout value
Definition: coap_net_internal.h:40
coap_queue_t::next
struct coap_queue_t * next
Definition: coap_net_internal.h:35
coap_queue_t::id
coap_mid_t id
CoAP message id.
Definition: coap_net_internal.h:42
coap_queue_t::t
coap_tick_t t
when to send PDU for the next time
Definition: coap_net_internal.h:36
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition: coap_session_internal.h:68
coap_session_t::lg_xmit
coap_lg_xmit_t * lg_xmit
list of large transmissions
Definition: coap_session_internal.h:96
coap_session_t::max_token_checked
volatile uint8_t max_token_checked
Check for max token size coap_ext_token_check_t.
Definition: coap_session_internal.h:200
coap_session_t::psk_key
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
Definition: coap_session_internal.h:127
coap_session_t::endpoint
coap_endpoint_t * endpoint
session's endpoint
Definition: coap_session_internal.h:84
coap_session_t::doing_first
uint8_t doing_first
Set if doing client's first request.
Definition: coap_session_internal.h:182
coap_session_t::last_rx_tx
coap_tick_t last_rx_tx
Definition: coap_session_internal.h:111
coap_session_t::sock
coap_socket_t sock
socket object for the session, if any
Definition: coap_session_internal.h:81
coap_session_t::partial_pdu
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition: coap_session_internal.h:110
coap_session_t::max_token_size
uint32_t max_token_size
Largest token size supported RFC8974.
Definition: coap_session_internal.h:204
coap_session_t::nstart
uint16_t nstart
maximum concurrent confirmable xmits (default 1)
Definition: coap_session_internal.h:152
coap_session_t::psk_identity
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
Definition: coap_session_internal.h:118
coap_session_t::state
coap_session_state_t state
current state of relationship with peer
Definition: coap_session_internal.h:71
coap_session_t::tx_token
uint64_t tx_token
Next token number to use.
Definition: coap_session_internal.h:205
coap_session_t::block_mode
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
Definition: coap_session_internal.h:181
coap_session_t::csm_bert_loc_support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
Definition: coap_session_internal.h:180
coap_session_t::addr_info
coap_addr_tuple_t addr_info
remote/local address info
Definition: coap_session_internal.h:79
coap_session_t::proto
coap_proto_t proto
protocol used
Definition: coap_session_internal.h:69
coap_session_t::tx_mid
uint16_t tx_mid
the last message id that was used in this session
Definition: coap_session_internal.h:88
coap_session_t::ref
unsigned ref
reference count from queues
Definition: coap_session_internal.h:73
coap_session_t::csm_rcv_mtu
size_t csm_rcv_mtu
CSM mtu (rcv)
Definition: coap_session_internal.h:76
coap_session_t::psk_hint
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
Definition: coap_session_internal.h:136
coap_session_t::last_token
coap_bin_const_t * last_token
Definition: coap_session_internal.h:206
coap_session_t::cpsk_setup_data
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
Definition: coap_session_internal.h:116
coap_session_t::mtu
size_t mtu
path or CSM mtu (xmt)
Definition: coap_session_internal.h:75
coap_session_t::no_observe_cancel
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
Definition: coap_session_internal.h:185
coap_session_t::partial_read
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition: coap_session_internal.h:108
coap_session_t::dtls_event
int dtls_event
Tracking any (D)TLS events on this session.
Definition: coap_session_internal.h:176
coap_session_t::max_retransmit
uint16_t max_retransmit
maximum re-transmit count (default 4)
Definition: coap_session_internal.h:150
coap_session_t::ack_random_factor
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition: coap_session_internal.h:148
coap_session_t::proxy_session
uint8_t proxy_session
Set if this is an ongoing proxy session.
Definition: coap_session_internal.h:183
coap_session_t::con_active
uint8_t con_active
Active CON request sent.
Definition: coap_session_internal.h:90
coap_session_t::delayqueue
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition: coap_session_internal.h:94
coap_session_t::tls_overhead
size_t tls_overhead
overhead of TLS layer
Definition: coap_session_internal.h:74
coap_session_t::app
void * app
application-specific data
Definition: coap_session_internal.h:145
coap_session_t::tx_rtag
uint32_t tx_rtag
Next Request-Tag number to use.
Definition: coap_session_internal.h:178
coap_session_t::last_ping_mid
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
Definition: coap_session_internal.h:92
coap_session_t::lg_srcv
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
Definition: coap_session_internal.h:101
coap_session_t::lg_crcv
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
Definition: coap_session_internal.h:98
coap_session_t::ack_timeout
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
Definition: coap_session_internal.h:146
coap_session_t::default_leisure
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
Definition: coap_session_internal.h:154
coap_session_t::last_con_mid
coap_mid_t last_con_mid
The last CON mid that has been been processed.
Definition: coap_session_internal.h:210
coap_session_t::type
coap_session_type_t type
client or server side socket
Definition: coap_session_internal.h:70
coap_session_t::probing_rate
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
Definition: coap_session_internal.h:156
coap_session_t::last_ack_mid
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
Definition: coap_session_internal.h:208
coap_session_t::context
coap_context_t * context
session's context
Definition: coap_session_internal.h:86
coap_session_t::partial_write
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
Definition: coap_session_internal.h:103
coap_session_t::addr_hash
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
Definition: coap_session_internal.h:77
coap_session_t::ifindex
int ifindex
interface index
Definition: coap_session_internal.h:80
coap_socket_t::lfunc
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
Definition: coap_io_internal.h:55
coap_socket_t::session
coap_session_t * session
Used to determine session owner.
Definition: coap_io_internal.h:47
coap_socket_t::endpoint
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
Definition: coap_io_internal.h:49
coap_socket_t::mcast_addr
coap_address_t mcast_addr
remote address and port (multicast track)
Definition: coap_io_internal.h:53
coap_socket_t::flags
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
Definition: coap_io_internal.h:46