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