libcoap 4.3.5-develop-18bb1ff
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-2025 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
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 }
500 session->nstart = COAP_DEFAULT_NSTART;
503#if COAP_Q_BLOCK_SUPPORT
504 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
505 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
506 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
507 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
508 coap_session_fix_non_probing_wait_base(session);
509 coap_session_fix_non_partial_timeout(session);
510#endif /* COAP_Q_BLOCK_SUPPORT */
511 session->dtls_event = -1;
516 session->max_token_size = context->max_token_size; /* RFC8974 */
517 if (session->type != COAP_SESSION_TYPE_CLIENT)
519
520 /* Randomly initialize */
521 /* TCP/TLS have no notion of mid */
522 if (COAP_PROTO_NOT_RELIABLE(session->proto))
523 coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
524 coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
525
526 return session;
527}
528
529void
531 coap_queue_t *q, *tmp;
532 coap_lg_xmit_t *lq, *ltmp;
533
534#if COAP_PROXY_SUPPORT
535 if (session->ref_proxy_subs)
536 coap_delete_proxy_subscriber(session, NULL, 0, COAP_PROXY_SUBS_ALL);
537#endif /* COAP_PROXY_SUPPORT */
538#if COAP_CLIENT_SUPPORT
539 coap_lg_crcv_t *lg_crcv, *etmp;
540
541 /* Need to do this before (D)TLS and socket is closed down */
542 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
543 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
544 /* Need to close down observe */
545 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
546 /* Need to delete node we set up for NON */
547 coap_queue_t *queue = session->context->sendqueue;
548
549 while (queue) {
550 if (queue->session == session) {
552 break;
553 }
554 queue = queue->next;
555 }
556 }
557 }
558 /* In case coap_cancel_observe_lkd() failure, which could clear down lg_crcv */
559 if (!session->lg_crcv)
560 break;
561 LL_DELETE(session->lg_crcv, lg_crcv);
562 coap_block_delete_lg_crcv(session, lg_crcv);
563 }
564#endif /* COAP_CLIENT_SUPPORT */
565
566 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
567 if (q->pdu->type==COAP_MESSAGE_CON) {
568 coap_handle_nack(session, q->pdu,
569 session->proto == COAP_PROTO_DTLS ?
571 q->id);
572 }
574 }
575 if (session->partial_pdu)
577 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
578 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
579 if (session->psk_identity)
581 if (session->psk_key)
583 if (session->psk_hint)
585
586#if COAP_SERVER_SUPPORT
587 coap_cache_entry_t *cp, *ctmp;
588 HASH_ITER(hh, session->context->cache, cp, ctmp) {
589 /* cp->session is NULL if not session based */
590 if (cp->session == session) {
591 coap_delete_cache_entry(session->context, cp);
592 }
593 }
594#endif /* COAP_PROXY_SUPPORT */
595 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
596 LL_DELETE(session->lg_xmit, lq);
597 coap_block_delete_lg_xmit(session, lq);
598 }
599#if COAP_SERVER_SUPPORT
600 coap_lg_srcv_t *sq, *stmp;
601
602 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
603 LL_DELETE(session->lg_srcv, sq);
604 coap_block_delete_lg_srcv(session, sq);
605 }
606#endif /* COAP_SERVER_SUPPORT */
607#if COAP_OSCORE_SUPPORT
609#endif /* COAP_OSCORE_SUPPORT */
610#if COAP_WS_SUPPORT
611 coap_free_type(COAP_STRING, session->ws);
612 coap_delete_str_const(session->ws_host);
613#endif /* COAP_WS_SUPPORT */
614}
615
616void
618 if (!session)
619 return;
621 assert(session->ref == 0);
622 if (session->ref)
623 return;
624 /* Make sure nothing gets deleted under our feet */
626 coap_session_mfree(session);
627#if COAP_SERVER_SUPPORT
629 if (session->endpoint) {
630 if (session->endpoint->sessions)
631 SESSIONS_DELETE(session->endpoint->sessions, session);
632 } else
633#endif /* COAP_SERVER_SUPPORT */
634#if COAP_CLIENT_SUPPORT
635 if (session->context) {
636 if (session->context->sessions)
637 SESSIONS_DELETE(session->context->sessions, session);
638 }
640#endif /* COAP_CLIENT_SUPPORT */
642 coap_delete_bin_const(session->echo);
643#if COAP_SERVER_SUPPORT
645#endif /* COAP_SERVER_SUPPORT */
646
647 if (session->app_cb) {
648 session->app_cb(session->app_data);
649 }
650 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
651 (void *)session);
652 assert(session->ref == 1);
654}
655
656#if COAP_SERVER_SUPPORT
657void
659 int i;
660
663 coap_cancel_all_messages(session->context, session, NULL);
664 RESOURCES_ITER(session->context->resources, r) {
665 /* In case code is broken somewhere */
666 for (i = 0; i < 1000; i++) {
667 if (!coap_delete_observer(r, session, NULL))
668 break;
669 }
670 }
671 if (session->context->unknown_resource) {
672 /* In case code is broken somewhere */
673 for (i = 0; i < 1000; i++) {
674 if (!coap_delete_observer(session->context->unknown_resource, session, NULL))
675 break;
676 }
677 }
678 if (session->context->proxy_uri_resource) {
679 /* In case code is broken somewhere */
680 for (i = 0; i < 1000; i++) {
681 if (!coap_delete_observer(session->context->proxy_uri_resource, session, NULL))
682 break;
683 }
684 }
685 while (session->delayqueue) {
686 coap_queue_t *q = session->delayqueue;
687
688 session->delayqueue = q->next;
690 }
691 /* Force session to go away */
694
696}
697#endif /* COAP_SERVER_SUPPORT */
698
699static size_t
701 size_t max_with_header) {
702#if COAP_DISABLE_TCP
703 (void)session;
704 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
705 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
706 : 0;
707#else /* !COAP_DISABLE_TCP */
708 if (COAP_PROTO_NOT_RELIABLE(session->proto))
709 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
710 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
711 : 0;
712 /* we must assume there is no token to be on the safe side */
713 if (max_with_header <= 2)
714 return 0;
715 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
716 return max_with_header - 2;
717 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
718 return max_with_header - 3;
719 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
720 return max_with_header - 4;
721 else
722 return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE;
723#endif /* !COAP_DISABLE_TCP */
724}
725
726size_t
728 if (session->csm_rcv_mtu)
730 (size_t)(session->csm_rcv_mtu));
731
733 (size_t)(session->mtu - session->tls_overhead));
734}
735
736COAP_API size_t
738 size_t size;
739 coap_session_t *session_rw;
740
741 /*
742 * Need to do this to not get a compiler warning about const parameters
743 * but need to maintain source code backward compatibility
744 */
745 memcpy(&session_rw, &session, sizeof(session_rw));
746 coap_lock_lock(return 0);
747 size = coap_session_max_pdu_size_lkd(session_rw);
749 return size;
750}
751
752size_t
754 size_t max_with_header;
755
757#if COAP_CLIENT_SUPPORT
758 /*
759 * Delay if session->doing_first is set.
760 * E.g. Reliable and CSM not in yet for checking block support
761 */
762 coap_session_t *session_rw;
763
764 /*
765 * Need to do this to not get a compiler warning about const parameters
766 * but need to maintain source code backward compatibility
767 */
768 memcpy(&session_rw, &session, sizeof(session_rw));
769 if (coap_client_delay_first(session_rw) == 0) {
770 coap_log_debug("coap_client_delay_first: timeout\n");
771 /* Have to go with the defaults */
772 }
773#endif /* COAP_CLIENT_SUPPORT */
774
775 max_with_header = (size_t)(session->mtu - session->tls_overhead);
776
777 return coap_session_max_pdu_size_internal(session, max_with_header);
778}
779
780void
781coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
782 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE) {
783 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
784 coap_log_debug("* %s: Restricting MTU size to %u\n",
785 coap_session_str(session), mtu);
786 }
787 if (mtu < 64)
788 mtu = 64;
789 session->mtu = mtu;
790 if (session->tls_overhead >= session->mtu) {
791 session->tls_overhead = session->mtu;
792 coap_log_err("DTLS overhead exceeds MTU\n");
793 }
794}
795
796ssize_t
798 coap_queue_t *node) {
799 if (node) {
800 coap_queue_t *removed = NULL;
801 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
802 assert(removed == node);
804 node->session = NULL;
805 node->t = 0;
806 } else {
807 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
808 coap_queue_t *q = NULL;
809 /* Check same mid is not getting re-used in violation of RFC7252 */
810 LL_FOREACH(session->delayqueue, q) {
811 if (q->id == pdu->mid) {
812 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
813 coap_session_str(session), pdu->mid);
814 return COAP_INVALID_MID;
815 }
816 }
817 }
818 node = coap_new_node();
819 if (node == NULL)
820 return COAP_INVALID_MID;
821 node->id = pdu->mid;
822 node->pdu = pdu;
823 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
824 uint8_t r;
825 coap_prng_lkd(&r, sizeof(r));
826 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
827 node->timeout = coap_calc_timeout(session, r);
828 }
829 coap_address_copy(&node->remote, &session->addr_info.remote);
830 }
831 LL_APPEND(session->delayqueue, node);
832 coap_log_debug("** %s: mid=0x%04x: delayed\n",
833 coap_session_str(session), node->id);
834 return COAP_PDU_DELAYED;
835}
836
837#if !COAP_DISABLE_TCP
838void
840 coap_pdu_t *pdu;
841 uint8_t buf[4];
842 assert(COAP_PROTO_RELIABLE(session->proto));
843 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
844 session->state = COAP_SESSION_STATE_CSM;
845 session->partial_write = 0;
846 if (session->mtu == 0)
847 coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */
849 if (pdu == NULL
851 coap_encode_var_safe(buf, sizeof(buf),
852 session->context->csm_max_message_size), buf) == 0
854 coap_encode_var_safe(buf, sizeof(buf),
855 0), buf) == 0
856 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
859 coap_encode_var_safe(buf, sizeof(buf),
860 session->max_token_size),
861 buf) == 0)
862 || coap_pdu_encode_header(pdu, session->proto) == 0
863 ) {
865 } else {
866 ssize_t bytes_written;
867
868 pdu->session = session;
869 bytes_written = coap_session_send_pdu(session, pdu);
870 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
872 } else {
873 session->csm_rcv_mtu = session->context->csm_max_message_size;
874 if (session->csm_rcv_mtu > COAP_BERT_BASE)
875 session->csm_bert_loc_support = 1;
876 else
877 session->csm_bert_loc_support = 0;
878 }
879 }
880 if (pdu)
882}
883#endif /* !COAP_DISABLE_TCP */
884
887 coap_mid_t mid;
888
890 mid = coap_session_send_ping_lkd(session);
891 if (mid != COAP_INVALID_MID) {
892 coap_tick_t now;
893
894 coap_ticks(&now);
895 session->last_ping_mid = mid;
896 session->last_rx_tx = now;
897 session->last_ping = now;
898 }
900 return mid;
901}
902
905 coap_pdu_t *ping = NULL;
906
908 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
909 session->con_active)
910 return COAP_INVALID_MID;
911 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
912 uint16_t mid = coap_new_message_id_lkd(session);
913 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
914 }
915#if !COAP_DISABLE_TCP
916 else {
918 }
919#endif /* !COAP_DISABLE_TCP */
920 if (!ping)
921 return COAP_INVALID_MID;
922 return coap_send_internal(session, ping, NULL);
923}
924
925void
927 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
928 coap_log_debug("***%s: session connected\n",
929 coap_session_str(session));
930 if (session->state == COAP_SESSION_STATE_CSM) {
932#if COAP_CLIENT_SUPPORT
934#endif /* COAP_CLIENT_SUPPORT */
935 if (session->doing_first)
936 session->doing_first = 0;
937 }
938 }
939
941 session->partial_write = 0;
942
943 if (session->proto==COAP_PROTO_DTLS) {
944 session->tls_overhead = coap_dtls_get_overhead(session);
945 if (session->tls_overhead >= session->mtu) {
946 session->tls_overhead = session->mtu;
947 coap_log_err("DTLS overhead exceeds MTU\n");
948 }
949 }
950
951 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
952 ssize_t bytes_written;
953 coap_queue_t *q = session->delayqueue;
954 coap_address_t remote;
955
956 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
957 if (session->con_active >= COAP_NSTART(session))
958 break;
959 session->con_active++;
960 }
961 /* Take entry off the queue */
962 session->delayqueue = q->next;
963 q->next = NULL;
964
965 coap_address_copy(&remote, &session->addr_info.remote);
967 coap_log_debug("** %s: mid=0x%04x: transmitted after delay (2)\n",
968 coap_session_str(session), (int)q->pdu->mid);
969 bytes_written = coap_session_send_pdu(session, q->pdu);
970 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
971 if (coap_wait_ack(session->context, session, q) >= 0)
972 q = NULL;
973 }
974 coap_address_copy(&session->addr_info.remote, &remote);
975 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
976 if (q)
978 if (bytes_written < 0)
979 break;
980 } else if (q) {
981 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
982 q->next = session->delayqueue;
983 session->delayqueue = q;
984 if (bytes_written > 0)
985 session->partial_write = (size_t)bytes_written;
986 break;
987 } else {
989 }
990 }
991 }
992}
993
994#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
995static const char *
997 switch (reason) {
999 return "COAP_NACK_TOO_MANY_RETRIES";
1001 return "COAP_NACK_NOT_DELIVERABLE";
1002 case COAP_NACK_RST:
1003 return "COAP_NACK_RST";
1005 return "COAP_NACK_TLS_FAILED";
1007 return "COAP_NACK_ICMP_ISSUE";
1009 return "COAP_NACK_BAD_RESPONSE";
1011 return "COAP_NACK_TLS_LAYER_FAILED";
1013 return "COAP_NACK_WS_LAYER_FAILED";
1015 return "COAP_NACK_WS_FAILED";
1016 default:
1017 return "???";
1018 }
1019}
1020#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
1021
1022void
1024 coap_pdu_t *sent,
1025 const coap_nack_reason_t reason,
1026 const coap_mid_t mid) {
1027 if (session->context->nack_handler) {
1028 coap_bin_const_t token = {0, NULL};
1029
1030 if (sent) {
1031 coap_check_update_token(session, sent);
1032 token = sent->actual_token;
1033 }
1034 coap_lock_callback(session->context->nack_handler(session, sent, reason, mid));
1035 if (sent) {
1036 coap_update_token(sent, token.length, token.s);
1037 }
1038 }
1039#if COAP_CLIENT_SUPPORT
1040 if (reason != COAP_NACK_ICMP_ISSUE) {
1041 session->doing_send_recv = 0;
1042 }
1043#endif /* COAP_CLIENT_SUPPORT */
1044}
1045
1046COAP_API void
1052
1053void
1055#if !COAP_DISABLE_TCP
1056 coap_session_state_t state = session->state;
1057#endif /* !COAP_DISABLE_TCP */
1058 coap_lg_xmit_t *lq, *ltmp;
1059#if COAP_SERVER_SUPPORT
1060 coap_lg_srcv_t *sq, *stmp;
1061#endif /* COAP_SERVER_SUPPORT */
1062#if COAP_CLIENT_SUPPORT
1063 coap_lg_crcv_t *cq, *etmp;
1064#endif /* COAP_CLIENT_SUPPORT */
1065 int sent_nack = 0;
1066 coap_queue_t *q;
1067
1069#if COAP_CLIENT_SUPPORT
1070 coap_session_failed(session);
1071#endif /* COAP_CLIENT_SUPPORT */
1072
1073 q = session->context->sendqueue;
1074 while (q) {
1075 if (q->session == session) {
1076 /* Take the first one */
1077 coap_handle_nack(session, q->pdu, reason, q->id);
1078 sent_nack = 1;
1079 break;
1080 }
1081 q = q->next;
1082 }
1083
1084 if (reason != COAP_NACK_ICMP_ISSUE
1086 || session->session_failed
1087#endif /* COAP_CLIENT_SUPPORT */
1088 ) {
1089 while (session->delayqueue) {
1090 q = session->delayqueue;
1091 session->delayqueue = q->next;
1092 q->next = NULL;
1093 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1094 coap_session_str(session), q->id);
1095 if (q->pdu->type == COAP_MESSAGE_CON) {
1096 coap_handle_nack(session, q->pdu, reason, q->id);
1097 sent_nack = 1;
1098 }
1099
1100#if COAP_CLIENT_SUPPORT
1101 session->doing_send_recv = 0;
1102#endif /* COAP_CLIENT_SUPPORT */
1104 }
1105 }
1106#if COAP_CLIENT_SUPPORT
1107 if (!sent_nack && session->lg_crcv) {
1108 /* Take the first one */
1109 coap_handle_nack(session, session->lg_crcv->sent_pdu, reason,
1110 session->lg_crcv->sent_pdu->mid);
1111 sent_nack = 1;
1112 }
1113#endif /* COAP_CLIENT_SUPPORT */
1114 if (!sent_nack) {
1115 /* Unable to determine which request disconnection was for */
1116 coap_handle_nack(session, NULL, reason, 0);
1117 }
1118 if (reason == COAP_NACK_ICMP_ISSUE) {
1119 coap_log_debug("***%s: session ICMP issue (%s)\n",
1120 coap_session_str(session), coap_nack_name(reason));
1121 return;
1122 }
1123 coap_log_debug("***%s: session disconnected (%s)\n",
1124 coap_session_str(session), coap_nack_name(reason));
1125#if COAP_SERVER_SUPPORT
1126 coap_delete_observers(session->context, session);
1127#endif /* COAP_SERVER_SUPPORT */
1128
1129 if (session->proto == COAP_PROTO_UDP)
1131 else
1132 session->state = COAP_SESSION_STATE_NONE;
1133
1134 session->con_active = 0;
1135
1136 if (session->partial_pdu) {
1138 session->partial_pdu = NULL;
1139 }
1140 session->partial_read = 0;
1141
1142 /* Not done if nack handler called above */
1143 while (session->delayqueue) {
1144 q = session->delayqueue;
1145 session->delayqueue = q->next;
1146 q->next = NULL;
1147 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1148 coap_session_str(session), q->id);
1149#if COAP_CLIENT_SUPPORT
1150 session->doing_send_recv = 0;
1151#endif /* COAP_CLIENT_SUPPORT */
1153 }
1154
1155#if COAP_CLIENT_SUPPORT
1156 if (!session->session_failed) {
1157 /* Need to do this before (D)TLS and socket is closed down */
1158 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1159 LL_DELETE(session->lg_crcv, cq);
1160 coap_block_delete_lg_crcv(session, cq);
1161 }
1162 }
1163#endif /* COAP_CLIENT_SUPPORT */
1164 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1165 LL_DELETE(session->lg_xmit, lq);
1166 coap_block_delete_lg_xmit(session, lq);
1167 }
1168#if COAP_SERVER_SUPPORT
1169 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1170 LL_DELETE(session->lg_srcv, sq);
1171 coap_block_delete_lg_srcv(session, sq);
1172 }
1173#endif /* COAP_SERVER_SUPPORT */
1174 coap_cancel_session_messages(session->context, session, reason);
1175
1176#if !COAP_DISABLE_TCP
1177 if (COAP_PROTO_RELIABLE(session->proto)) {
1178 if (coap_netif_available(session)) {
1182 }
1183#if COAP_CLIENT_SUPPORT
1184 if (state != COAP_SESSION_STATE_NONE && !session->session_failed) {
1188 }
1189#endif /* COAP_CLIENT_SUPPORT */
1190 if (session->doing_first)
1191 session->doing_first = 0;
1192 }
1193#endif /* !COAP_DISABLE_TCP */
1194 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1195 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1196}
1197
1198#if COAP_CLIENT_SUPPORT
1199void
1201 if (session->context->reconnect_time && session->client_initiated) {
1202 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1203 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1204 session->session_failed = 1;
1206 session->retry_count++;
1207 if (session->context->retry_count && session->retry_count >= session->context->retry_count) {
1209 if (session->type != COAP_SESSION_TYPE_CLIENT) {
1211 coap_session_release_lkd(session);
1212 }
1213 }
1214 coap_ticks(&session->last_rx_tx);
1215 }
1216}
1217#endif /* COAP_CLIENT_SUPPORT */
1218
1219#if COAP_SERVER_SUPPORT
1220static void
1221coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1222 const coap_addr_tuple_t *addr_info) {
1223 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1224 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1225 addr_hash->lport = coap_address_get_port(&addr_info->local);
1226 addr_hash->proto = proto;
1227}
1228
1231 const coap_packet_t *packet, coap_tick_t now) {
1232 coap_session_t *session;
1233 coap_session_t *rtmp;
1234 unsigned int num_idle = 0;
1235 unsigned int num_hs = 0;
1236 coap_session_t *oldest = NULL;
1237 coap_session_t *oldest_hs = NULL;
1238 coap_addr_hash_t addr_hash;
1239
1240 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1241 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1242 if (session) {
1243 /* Maybe mcast or unicast IP address which is not in the hash */
1244 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1245 session->ifindex = packet->ifindex;
1246 session->last_rx_tx = now;
1247 return session;
1248 }
1249
1250#if COAP_CLIENT_SUPPORT
1251 SESSIONS_FIND(endpoint->context->sessions, addr_hash, session);
1252 if (session) {
1253 /* Maybe mcast or unicast IP address which is not in the hash */
1254 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1255 session->ifindex = packet->ifindex;
1256 session->last_rx_tx = now;
1257 return session;
1258 }
1259
1260 if (coap_is_mcast(&packet->addr_info.local)) {
1261 /* Check if this a proxy client packet we sent on another socket */
1262 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1263 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1266 /* Drop looped back packet to stop recursion / confusion */
1267 return NULL;
1268 }
1269 }
1270 }
1271#endif /* COAP_CLIENT_SUPPORT */
1272 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1273 if (session->ref == 0 && session->delayqueue == NULL) {
1274 if (
1276 !(session->client_initiated && endpoint->context->reconnect_time) &&
1277#endif /* COAP_CLIENT_SUPPORT */
1278 session->type == COAP_SESSION_TYPE_SERVER) {
1279 ++num_idle;
1280 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1281 oldest = session;
1282
1283 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1284 ++num_hs;
1285 /* See if this is a partial (D)TLS session set up
1286 which needs to be cleared down to prevent DOS */
1287 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1288 if (oldest_hs == NULL ||
1289 session->last_rx_tx < oldest_hs->last_rx_tx)
1290 oldest_hs = session;
1291 }
1292 }
1293 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1294 ++num_hs;
1295 /* See if this is a partial (D)TLS session set up for Client Hello
1296 which needs to be cleared down to prevent DOS */
1297 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1298 if (oldest_hs == NULL ||
1299 session->last_rx_tx < oldest_hs->last_rx_tx)
1300 oldest_hs = session;
1301 }
1302 }
1303 }
1304 }
1305
1306 if (endpoint->context->max_idle_sessions > 0 &&
1307 num_idle >= endpoint->context->max_idle_sessions) {
1309 coap_session_free(oldest);
1310 } else if (oldest_hs) {
1311 coap_log_warn("***%s: Incomplete session timed out\n",
1312 coap_session_str(oldest_hs));
1314 coap_session_free(oldest_hs);
1315 }
1316
1317 if (num_hs > (endpoint->context->max_handshake_sessions ?
1318 endpoint->context->max_handshake_sessions :
1320 /* Maxed out on number of sessions in (D)TLS negotiation state */
1321 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1322 "large. New request ignored\n");
1323 return NULL;
1324 }
1325
1326 if (endpoint->proto == COAP_PROTO_DTLS) {
1327 /*
1328 * Need to check that this actually is a Client Hello before wasting
1329 * time allocating and then freeing off session.
1330 */
1331
1332 /*
1333 * Generic header structure of the DTLS record layer.
1334 * typedef struct __attribute__((__packed__)) {
1335 * uint8_t content_type; content type of the included message
1336 * uint16_t version; Protocol version
1337 * uint16_t epoch; counter for cipher state changes
1338 * uint8_t sequence_number[6]; sequence number
1339 * uint16_t length; length of the following fragment
1340 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1341 * } dtls_record_handshake_t;
1342 */
1343#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1344#define DTLS_CT_ALERT 21 /* Content Type Alert */
1345#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1346#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1347#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1348#define DTLS_CT_CID 25 /* Content Type Connection ID */
1349#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1350#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1351
1352 const uint8_t *payload = (const uint8_t *)packet->payload;
1353 size_t length = packet->length;
1354 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1355 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1356 payload[OFF_CONTENT_TYPE], length,
1357 OFF_HANDSHAKE_TYPE + 1);
1358 return NULL;
1359 }
1360 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1361 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1362 /* Client may have changed its IP address */
1363 int changed = 0;
1364
1365 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1366 if (session->client_cid) {
1367 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1368 length > (OFF_CID_DTLS13 + session->client_cid->length) &&
1369 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1370 session->client_cid->length) == 0) ||
1371 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1372 length > (OFF_CID + session->client_cid->length) &&
1373 memcmp(session->client_cid->s, &payload[OFF_CID],
1374 session->client_cid->length) == 0)) {
1375 /* Updating IP address */
1376 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1377 SESSIONS_DELETE(endpoint->sessions, session);
1378 session->addr_info = packet->addr_info;
1379 memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash));
1380 SESSIONS_ADD(endpoint->sessions, session);
1381 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1382 return session;
1383 }
1384 }
1385 }
1386 if (!changed) {
1387 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1388 return NULL;
1389 }
1390 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1391 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1392 /* only log if not a late alert */
1393 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1394 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1395 payload[OFF_CONTENT_TYPE],
1396 payload[OFF_HANDSHAKE_TYPE]);
1397 return NULL;
1398 }
1399 }
1400
1402 &addr_hash, &packet->addr_info.local,
1403 &packet->addr_info.remote,
1404 packet->ifindex, endpoint->context, endpoint);
1405 if (session) {
1406 session->last_rx_tx = now;
1407 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1408 sizeof(session->sock.lfunc));
1409 if (endpoint->proto == COAP_PROTO_UDP)
1411 else if (endpoint->proto == COAP_PROTO_DTLS) {
1412 session->type = COAP_SESSION_TYPE_HELLO;
1413 }
1414 SESSIONS_ADD(endpoint->sessions, session);
1415 coap_log_debug("***%s: session %p: new incoming session\n",
1416 coap_session_str(session), (void *)session);
1418 }
1419 return session;
1420}
1421
1424 coap_tick_t now) {
1425 if (session) {
1426 session->last_rx_tx = now;
1427 session->type = COAP_SESSION_TYPE_SERVER;
1428 coap_dtls_establish(session);
1429 }
1430 return session;
1431}
1432#endif /* COAP_SERVER_SUPPORT */
1433
1434#if COAP_CLIENT_SUPPORT
1435static coap_session_t *
1436coap_session_create_client(coap_context_t *ctx,
1437 const coap_address_t *local_if,
1438 const coap_address_t *server,
1439 coap_proto_t proto) {
1440 coap_session_t *session = NULL;
1441 int default_port = COAP_DEFAULT_PORT;
1442
1443 assert(server);
1444
1445 switch (proto) {
1446 case COAP_PROTO_UDP:
1447 default_port = COAP_DEFAULT_PORT;
1448 break;
1449 case COAP_PROTO_DTLS:
1450 if (!coap_dtls_is_supported()) {
1451 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1452 return NULL;
1453 }
1454 default_port = COAPS_DEFAULT_PORT;
1455 break;
1456 case COAP_PROTO_TCP:
1457 if (!coap_tcp_is_supported()) {
1458 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1459 return NULL;
1460 }
1461 default_port = COAP_DEFAULT_PORT;
1462 break;
1463 case COAP_PROTO_TLS:
1464 if (!coap_tls_is_supported()) {
1465 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1466 return NULL;
1467 }
1468 default_port = COAPS_DEFAULT_PORT;
1469 break;
1470 case COAP_PROTO_WS:
1471 if (!coap_ws_is_supported()) {
1472 coap_log_crit("coap_new_client_session*: WS not supported\n");
1473 return NULL;
1474 }
1475 default_port = 80;
1476 break;
1477 case COAP_PROTO_WSS:
1478 if (!coap_wss_is_supported()) {
1479 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1480 return NULL;
1481 }
1482 default_port = 443;
1483 break;
1484 case COAP_PROTO_NONE:
1485 case COAP_PROTO_LAST:
1486 default:
1487 assert(0);
1488 return NULL;
1489 }
1490 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1491 local_if, server, 0, ctx, NULL);
1492 if (!session)
1493 goto error;
1494
1496 session->sock.session = session;
1497 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1498 sizeof(session->sock.lfunc));
1499
1500 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1501 coap_session_t *s, *rtmp;
1502 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1503 goto error;
1504 }
1505 /* Check that this is not a duplicate 4-tuple */
1506 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1509 &s->addr_info.local) &&
1511 &s->addr_info.remote)) {
1512 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1513 coap_session_str(session), (void *)session);
1514 goto error;
1515 }
1516 }
1517#ifdef WITH_CONTIKI
1518 session->sock.context = ctx;
1519#endif /* WITH_CONTIKI */
1520#if !COAP_DISABLE_TCP
1521 } else if (COAP_PROTO_RELIABLE(proto)) {
1522 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1523 goto error;
1524 }
1525#endif /* !COAP_DISABLE_TCP */
1526 }
1527
1528 session->sock.session = session;
1529#ifdef COAP_EPOLL_SUPPORT
1530 coap_epoll_ctl_add(&session->sock,
1531 EPOLLIN |
1532 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1533 EPOLLOUT : 0),
1534 __func__);
1535#endif /* COAP_EPOLL_SUPPORT */
1536
1538 if (local_if)
1539 session->sock.flags |= COAP_SOCKET_BOUND;
1540#if COAP_SERVER_SUPPORT
1541 if (ctx->proxy_uri_resource)
1542 session->proxy_session = 1;
1543#endif /* COAP_SERVER_SUPPORT */
1544 SESSIONS_ADD(ctx->sessions, session);
1545 return session;
1546
1547error:
1548 /*
1549 * Need to add in the session as coap_session_release_lkd()
1550 * will call SESSIONS_DELETE in coap_session_free().
1551 */
1552 if (session)
1553 SESSIONS_ADD(ctx->sessions, session);
1554 coap_session_release_lkd(session);
1555 return NULL;
1556}
1557
1558static void
1559coap_session_check_connect(coap_session_t *session) {
1560 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1561 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1562 }
1563#if !COAP_DISABLE_TCP
1564 if (COAP_PROTO_RELIABLE(session->proto)) {
1565 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1567 if (session->client_initiated) {
1568 session->doing_first = 1;
1569 }
1570 } else {
1571 /* Initial connect worked immediately */
1572 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1573 }
1574 }
1575#endif /* !COAP_DISABLE_TCP */
1576 coap_ticks(&session->last_rx_tx);
1577}
1578#endif /* COAP_CLIENT_SUPPORT */
1579
1580void
1582 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1583 coap_session_connected(session);
1584#if !COAP_DISABLE_TCP
1585 if (COAP_PROTO_RELIABLE(session->proto))
1586 coap_session_send_csm(session);
1587#endif /* !COAP_DISABLE_TCP */
1588}
1589
1590#if COAP_CLIENT_SUPPORT
1591int
1593 int default_port = COAP_DEFAULT_PORT;
1594
1595 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close && !session->session_failed)
1596 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1597
1598 switch (session->proto) {
1599 case COAP_PROTO_UDP:
1600 default_port = COAP_DEFAULT_PORT;
1601 break;
1602 case COAP_PROTO_DTLS:
1603 default_port = COAPS_DEFAULT_PORT;
1604 break;
1605 case COAP_PROTO_TCP:
1606 default_port = COAP_DEFAULT_PORT;
1607 break;
1608 case COAP_PROTO_TLS:
1609 default_port = COAPS_DEFAULT_PORT;
1610 break;
1611 case COAP_PROTO_WS:
1612 default_port = 80;
1613 break;
1614 case COAP_PROTO_WSS:
1615 default_port = 443;
1616 break;
1617 case COAP_PROTO_NONE:
1618 case COAP_PROTO_LAST:
1619 default:
1620 assert(0);
1621 return 0;
1622 }
1623 session->sock.session = session;
1624 coap_log_debug("***%s: trying to reconnect\n", coap_session_str(session));
1626 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1627 session->state = COAP_SESSION_STATE_NONE;
1628 if (!coap_netif_dgrm_connect(session, &session->local_reconnect, &session->addr_info.remote,
1629 default_port)) {
1630 goto error;
1631 }
1633#ifdef WITH_CONTIKI
1634 session->sock.context = session->context;
1635#endif /* WITH_CONTIKI */
1636#if !COAP_DISABLE_TCP
1637 } else if (COAP_PROTO_RELIABLE(session->proto)) {
1638 if (!coap_netif_strm_connect1(session, &session->local_reconnect, &session->addr_info.remote,
1639 default_port)) {
1640 goto error;
1641 }
1642#endif /* !COAP_DISABLE_TCP */
1643 } else {
1644 goto error;
1645 }
1646#ifdef COAP_EPOLL_SUPPORT
1647 coap_epoll_ctl_add(&session->sock,
1648 EPOLLIN |
1649 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1650 EPOLLOUT : 0),
1651 __func__);
1652#endif /* COAP_EPOLL_SUPPORT */
1653
1655 session->sock.flags |= COAP_SOCKET_BOUND;
1656 coap_session_check_connect(session);
1657 return 1;
1658error:
1659 return 0;
1660}
1661
1662void
1664 coap_lg_crcv_t *lg_crcv, *etmp;
1665
1666 if (!session->session_failed)
1667 return;
1668 coap_log_debug("***%s: session re-established\n",
1669 coap_session_str(session));
1670 session->session_failed = 0;
1671 session->retry_count = 0;
1673 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
1675 coap_send_internal(session, lg_crcv->sent_pdu, NULL);
1676 }
1677}
1678
1681 const coap_address_t *local_if,
1682 const coap_address_t *server,
1683 coap_proto_t proto) {
1684 coap_session_t *session;
1685
1686 coap_lock_lock(return NULL);
1687 session = coap_new_client_session_lkd(ctx, local_if, server, proto);
1689 return session;
1690}
1691
1694 const coap_address_t *local_if,
1695 const coap_address_t *server,
1696 coap_proto_t proto) {
1697 coap_session_t *session;
1698
1700 session = coap_session_create_client(ctx, local_if, server,
1701 proto);
1702 if (session) {
1703 coap_log_debug("***%s: session %p: created outgoing session\n",
1704 coap_session_str(session), (void *)session);
1705 coap_session_check_connect(session);
1706 }
1707 return session;
1708}
1709
1712 const coap_address_t *local_if,
1713 const coap_address_t *server,
1714 coap_proto_t proto, const char *identity,
1715 const uint8_t *key, unsigned key_len) {
1716 coap_session_t *session;
1717
1718 coap_lock_lock(return NULL);
1719 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1721 return session;
1722}
1723
1726 const coap_address_t *local_if,
1727 const coap_address_t *server,
1728 coap_proto_t proto, const char *identity,
1729 const uint8_t *key, unsigned key_len) {
1730 coap_dtls_cpsk_t setup_data;
1731
1733 memset(&setup_data, 0, sizeof(setup_data));
1735
1736 if (identity) {
1737 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1738 setup_data.psk_info.identity.length = strlen(identity);
1739 }
1740
1741 if (key && key_len > 0) {
1742 setup_data.psk_info.key.s = key;
1743 setup_data.psk_info.key.length = key_len;
1744 }
1745
1746 return coap_new_client_session_psk2_lkd(ctx, local_if, server,
1747 proto, &setup_data);
1748}
1749
1750/*
1751 * Check the validity of the SNI to send to the server.
1752 *
1753 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1754 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1755 */
1756static void
1757coap_sanitize_client_sni(char **client_sni) {
1758 char *cp;
1759
1760 if (*client_sni == NULL)
1761 return;
1762
1763 cp = *client_sni;
1764 switch (*cp) {
1765 case '0':
1766 case '1':
1767 case '2':
1768 case '3':
1769 case '4':
1770 case '5':
1771 case '6':
1772 case '7':
1773 case '8':
1774 case '9':
1775 case 'a':
1776 case 'b':
1777 case 'c':
1778 case 'd':
1779 case 'e':
1780 case 'f':
1781 case 'A':
1782 case 'B':
1783 case 'C':
1784 case 'D':
1785 case 'E':
1786 case 'F':
1787 case ':':
1788 break;
1789 case '\000':
1790 /* Empty entry invalid */
1791 *client_sni = NULL;
1792 return;
1793 default:
1794 /* Does not start with a hex digit or : - not literal IP. */
1795 return;
1796 }
1797 /* Check for IPv4 */
1798 while (*cp) {
1799 switch (*cp) {
1800 case '0':
1801 case '1':
1802 case '2':
1803 case '3':
1804 case '4':
1805 case '5':
1806 case '6':
1807 case '7':
1808 case '8':
1809 case '9':
1810 case '.':
1811 break;
1812 default:
1813 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1814 goto check_ipv6;
1815 }
1816 cp++;
1817 }
1818 /* IPv4 address - not allowed. */
1819 *client_sni = NULL;
1820 return;
1821
1822check_ipv6:
1823 /* Check for IPv6 */
1824 cp = *client_sni;
1825 while (*cp) {
1826 switch (*cp) {
1827 case '0':
1828 case '1':
1829 case '2':
1830 case '3':
1831 case '4':
1832 case '5':
1833 case '6':
1834 case '7':
1835 case '8':
1836 case '9':
1837 case 'a':
1838 case 'b':
1839 case 'c':
1840 case 'd':
1841 case 'e':
1842 case 'f':
1843 case 'A':
1844 case 'B':
1845 case 'C':
1846 case 'D':
1847 case 'E':
1848 case 'F':
1849 case ':':
1850 break;
1851 case '%':
1852 /* Start of i/f specification, Previous is IPv6 */
1853 *client_sni = NULL;
1854 return;
1855 default:
1856 /* Not of format xx:xx::xx. */
1857 return;
1858 }
1859 cp++;
1860 }
1861 *client_sni = NULL;
1862 return;
1863}
1864
1867 const coap_address_t *local_if,
1868 const coap_address_t *server,
1869 coap_proto_t proto,
1870 coap_dtls_cpsk_t *setup_data) {
1871 coap_session_t *session;
1872
1873 coap_lock_lock(return NULL);
1874 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, setup_data);
1876 return session;
1877}
1878
1881 const coap_address_t *local_if,
1882 const coap_address_t *server,
1883 coap_proto_t proto,
1884 coap_dtls_cpsk_t *setup_data) {
1885 coap_session_t *session;
1886
1888 session = coap_session_create_client(ctx, local_if, server, proto);
1889
1890 if (!session || !setup_data)
1891 return NULL;
1892
1893 session->cpsk_setup_data = *setup_data;
1894 if (setup_data->psk_info.identity.s) {
1895 session->psk_identity =
1897 setup_data->psk_info.identity.length);
1898 if (!session->psk_identity) {
1899 coap_log_warn("Cannot store session Identity (PSK)\n");
1900 coap_session_release_lkd(session);
1901 return NULL;
1902 }
1904 coap_log_warn("Identity (PSK) not defined\n");
1905 coap_session_release_lkd(session);
1906 return NULL;
1907 }
1908
1909 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1910 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1911 setup_data->psk_info.key.length);
1912 if (!session->psk_key) {
1913 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1914 coap_session_release_lkd(session);
1915 return NULL;
1916 }
1918 coap_log_warn("Pre-shared key (PSK) not defined\n");
1919 coap_session_release_lkd(session);
1920 return NULL;
1921 }
1922
1923 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
1924
1926 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
1927 coap_session_release_lkd(session);
1928 return NULL;
1929 }
1930 }
1931 coap_log_debug("***%s: new outgoing session\n",
1932 coap_session_str(session));
1933 coap_session_check_connect(session);
1934 return session;
1935}
1936#endif /* COAP_CLIENT_SUPPORT */
1937
1938int
1940 const coap_bin_const_t *psk_hint
1941 ) {
1942 /* We may be refreshing the hint with the same hint */
1943 coap_bin_const_t *old_psk_hint = session->psk_hint;
1944
1945 if (psk_hint && psk_hint->s) {
1946 if (session->psk_hint) {
1947 if (coap_binary_equal(session->psk_hint, psk_hint))
1948 return 1;
1949 }
1950 session->psk_hint = coap_new_bin_const(psk_hint->s,
1951 psk_hint->length);
1952 if (!session->psk_hint) {
1953 coap_log_err("No memory to store identity hint (PSK)\n");
1954 if (old_psk_hint)
1955 coap_delete_bin_const(old_psk_hint);
1956 return 0;
1957 }
1958 } else {
1959 session->psk_hint = NULL;
1960 }
1961 if (old_psk_hint)
1962 coap_delete_bin_const(old_psk_hint);
1963
1964 return 1;
1965}
1966
1967int
1969 const coap_bin_const_t *psk_key
1970 ) {
1971 /* We may be refreshing the key with the same key */
1972 coap_bin_const_t *old_psk_key = session->psk_key;
1973
1974 if (psk_key && psk_key->s) {
1975 if (session->psk_key) {
1976 if (coap_binary_equal(session->psk_key, psk_key))
1977 return 1;
1978 }
1979 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1980 if (!session->psk_key) {
1981 coap_log_err("No memory to store pre-shared key (PSK)\n");
1982 if (old_psk_key)
1983 coap_delete_bin_const(old_psk_key);
1984 return 0;
1985 }
1986 } else {
1987 session->psk_key = NULL;
1988 }
1989 if (old_psk_key)
1990 coap_delete_bin_const(old_psk_key);
1991
1992 return 1;
1993}
1994
1995int
1997 const coap_bin_const_t *psk_identity
1998 ) {
1999 /* We may be refreshing the identity with the same identity */
2000 coap_bin_const_t *old_psk_identity = session->psk_identity;
2001
2002 if (psk_identity && psk_identity->s) {
2003 if (session->psk_identity) {
2004 if (coap_binary_equal(session->psk_identity, psk_identity))
2005 return 1;
2006 }
2007 session->psk_identity = coap_new_bin_const(psk_identity->s,
2008 psk_identity->length);
2009 if (!session->psk_identity) {
2010 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
2011 if (old_psk_identity)
2012 coap_delete_bin_const(old_psk_identity);
2013 return 0;
2014 }
2015 } else {
2016 session->psk_identity = NULL;
2017 }
2018 if (old_psk_identity)
2019 coap_delete_bin_const(old_psk_identity);
2020
2021 return 1;
2022}
2023
2024#if COAP_SERVER_SUPPORT
2025const coap_bin_const_t *
2027 if (session)
2028 return session->psk_hint;
2029 return NULL;
2030}
2031#endif /* COAP_SERVER_SUPPORT */
2032
2033const coap_bin_const_t *
2035 const coap_bin_const_t *psk_identity = NULL;
2036 if (session) {
2037 psk_identity = session->psk_identity;
2038 if (psk_identity == NULL) {
2039 psk_identity = &session->cpsk_setup_data.psk_info.identity;
2040 }
2041 }
2042 return psk_identity;
2043}
2044
2045const coap_bin_const_t *
2047 if (session)
2048 return session->psk_key;
2049 return NULL;
2050}
2051
2052#if COAP_CLIENT_SUPPORT
2055 const coap_address_t *local_if,
2056 const coap_address_t *server,
2057 coap_proto_t proto,
2058 coap_dtls_pki_t *setup_data) {
2059 coap_session_t *session;
2060
2061 coap_lock_lock(return NULL);
2062 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, setup_data);
2064 return session;
2065}
2066
2069 const coap_address_t *local_if,
2070 const coap_address_t *server,
2071 coap_proto_t proto,
2072 coap_dtls_pki_t *setup_data) {
2073 coap_session_t *session;
2074 coap_dtls_pki_t l_setup_data;
2075
2076 if (!setup_data)
2077 return NULL;
2078 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
2079 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
2080 return NULL;
2081 }
2082
2084 l_setup_data = *setup_data;
2085 coap_sanitize_client_sni(&l_setup_data.client_sni);
2086
2087 session = coap_session_create_client(ctx, local_if, server, proto);
2088
2089 if (!session) {
2090 return NULL;
2091 }
2092
2094 /* we know that setup_data is not NULL */
2095 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
2096 coap_session_release_lkd(session);
2097 return NULL;
2098 }
2099 }
2100 coap_log_debug("***%s: new outgoing session\n",
2101 coap_session_str(session));
2102 coap_session_check_connect(session);
2103 return session;
2104}
2105#endif /* ! COAP_CLIENT_SUPPORT */
2106
2107#if COAP_SERVER_SUPPORT
2108#if !COAP_DISABLE_TCP
2111 coap_session_t *session;
2112 coap_tick_t now;
2113
2115 NULL, NULL, NULL, 0, ctx, ep);
2116 if (!session)
2117 goto error;
2118
2119 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
2120 if (!coap_netif_strm_accept(ep, session, extra))
2121 goto error;
2122
2123 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
2124
2125 session->sock.session = session;
2126#ifdef COAP_EPOLL_SUPPORT
2127 coap_epoll_ctl_add(&session->sock,
2128 EPOLLIN,
2129 __func__);
2130#endif /* COAP_EPOLL_SUPPORT */
2131 SESSIONS_ADD(ep->sessions, session);
2132 if (session) {
2133 coap_ticks(&now);
2134 session->last_rx_tx = now;
2135 coap_log_debug("***%s: session %p: new incoming session\n",
2136 coap_session_str(session), (void *)session);
2140 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
2141 }
2142 return session;
2143
2144error:
2145 /*
2146 * Need to add in the session as coap_session_release_lkd()
2147 * will call SESSIONS_DELETE in coap_session_free().
2148 */
2149 if (session) {
2150 SESSIONS_ADD(ep->sessions, session);
2151 coap_session_free(session);
2152 }
2153 return NULL;
2154}
2155#endif /* !COAP_DISABLE_TCP */
2156#endif /* COAP_SERVER_SUPPORT */
2157
2158void
2160 const uint8_t *data) {
2161 session->tx_token = coap_decode_var_bytes8(data, len);
2162 /*
2163 * Decrement as when first used by coap_session_new_token() it will
2164 * get incremented
2165 */
2166 session->tx_token--;
2167}
2168
2169void
2171 uint8_t *data) {
2172 *len = coap_encode_var_safe8(data,
2173 sizeof(session->tx_token), ++session->tx_token);
2174}
2175
2176COAP_API uint16_t
2178 uint16_t mid;
2179
2180 coap_lock_lock(return 0);
2181 mid = coap_new_message_id_lkd(session);
2183 return mid;
2184}
2185
2186uint16_t
2189 if (COAP_PROTO_NOT_RELIABLE(session->proto))
2190 return ++session->tx_mid;
2191 /* TCP/TLS have no notion of mid */
2192 return 0;
2193}
2194
2195const coap_address_t *
2197 if (session)
2198 return &session->addr_info.remote;
2199 return NULL;
2200}
2201
2202const coap_address_t *
2204 if (session)
2205 return &session->addr_info.local;
2206 return NULL;
2207}
2208
2209const coap_address_t *
2211#if COAP_CLIENT_SUPPORT
2212 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
2213 session->sock.flags & COAP_SOCKET_MULTICAST)
2214 return &session->sock.mcast_addr;
2215#else /* ! COAP_CLIENT_SUPPORT */
2216 (void)session;
2217#endif /* ! COAP_CLIENT_SUPPORT */
2218 return NULL;
2219}
2220
2223 if (session)
2224 return session->context;
2225 return NULL;
2226}
2227
2230 if (session)
2231 return session->proto;
2232 return 0;
2233}
2234
2237 if (session)
2238 return session->type;
2239 return 0;
2240}
2241
2242COAP_API int
2244 int ret;
2245
2246 coap_lock_lock(return 0);
2247 ret = coap_session_set_type_client_lkd(session, 1);
2249 return ret;
2250}
2251
2252int
2254#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2255 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
2257 session->type = COAP_SESSION_TYPE_CLIENT;
2258 if (session->endpoint) {
2259 SESSIONS_DELETE(session->endpoint->sessions, session);
2260 SESSIONS_ADD(session->context->sessions, session);
2261 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2262 session->sock = session->endpoint->sock;
2263 session->sock.flags |= COAP_SOCKET_SLAVE;
2264 session->sock.flags &= ~COAP_SOCKET_CAN_READ;
2265 session->sock.flags &= ~COAP_SOCKET_WANT_READ;
2266 } else {
2267 session->sock.endpoint = NULL;
2268 }
2269 session->endpoint = NULL;
2270 }
2271 if (report_changed) {
2272 coap_log_debug("***%s: session now is type Client\n",
2273 coap_session_str(session));
2274 }
2275 return 1;
2276 }
2277#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2278 (void)session;
2279 (void)report_changed;
2280#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2281 return 0;
2282}
2283
2284COAP_API int
2286 int ret;
2287
2288 coap_lock_lock(return 0);
2289 ret = coap_session_set_type_server_lkd(session);
2291 return ret;
2292}
2293
2294int
2296#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2297 if (session && session->type == COAP_SESSION_TYPE_CLIENT) {
2298 coap_endpoint_t *ep;
2299 coap_mid_t mid;
2300 coap_pdu_t *ping = NULL;
2301
2302 LL_FOREACH(session->context->endpoint, ep) {
2303 if (session->proto == ep->proto)
2304 break;
2305 }
2306 if (!ep) {
2307 /* Need a dummy endpoint to 'hang' off as this is now a server */
2308 ep = coap_malloc_endpoint();
2309 if (!ep) {
2310 coap_log_warn("coap_new_endpoint: malloc");
2311 return 0;
2312 }
2313
2314 memset(ep, 0, sizeof(coap_endpoint_t));
2315 ep->context = session->context;
2316 ep->proto = session->proto;
2317 ep->sock.endpoint = ep;
2318 memcpy(&ep->sock.lfunc, coap_layers_coap[session->proto], sizeof(ep->sock.lfunc));
2319
2321 LL_PREPEND(session->context->endpoint, ep);
2322 }
2323 /* Need to use coap_send_lkd() here to get traffic flowing */
2324 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2326 }
2327#if !COAP_DISABLE_TCP
2328 else {
2330 }
2331#endif /* !COAP_DISABLE_TCP */
2332 if (!ping) {
2333 session->type = COAP_SESSION_TYPE_SERVER;
2334 return 0;
2335 }
2336 mid = coap_send_lkd(session, ping);
2337 if (mid != COAP_INVALID_MID) {
2338 coap_tick_t now;
2339
2340 coap_ticks(&now);
2341 session->last_ping_mid = mid;
2342 session->last_rx_tx = now;
2343 session->last_ping = now;
2344 }
2345 if (session->context) {
2346 if (session->context->sessions) {
2347 SESSIONS_DELETE(session->context->sessions, session);
2348 }
2349 }
2350 SESSIONS_ADD(ep->sessions, session);
2351 session->endpoint = ep;
2352 session->type = COAP_SESSION_TYPE_SERVER;
2353 coap_session_release_lkd(session);
2354 coap_log_debug("***%s: session now is type Server\n",
2355 coap_session_str(session));
2356 return 1;
2357 }
2358#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2359 (void)session;
2360#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2361 return 0;
2362}
2363
2366 if (session)
2367 return session->state;
2368 return 0;
2369}
2370
2373#if COAP_SERVER_SUPPORT
2374 if (session)
2375 return session->endpoint;
2376#else /* ! COAP_SERVER_SUPPORT */
2377 (void)session;
2378#endif /* ! COAP_SERVER_SUPPORT */
2379 return 0;
2380}
2381
2382int
2384 if (session)
2385 return session->ifindex;
2386 return -1;
2387}
2388
2389void *
2391 coap_tls_library_t *tls_lib) {
2392 if (session)
2393 return coap_dtls_get_tls(session, tls_lib);
2394 return NULL;
2395}
2396
2397static const char *
2399 switch (proto) {
2400 case COAP_PROTO_UDP:
2401 return "UDP ";
2402 case COAP_PROTO_DTLS:
2403 return "DTLS";
2404 case COAP_PROTO_TCP:
2405 return "TCP ";
2406 case COAP_PROTO_TLS:
2407 return "TLS ";
2408 case COAP_PROTO_WS:
2409 return "WS ";
2410 case COAP_PROTO_WSS:
2411 return "WSS ";
2412 case COAP_PROTO_NONE:
2413 case COAP_PROTO_LAST:
2414 default:
2415 return "????" ;
2416 break;
2417 }
2418 return NULL;
2419}
2420
2421#if COAP_SERVER_SUPPORT
2423coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2424 coap_endpoint_t *endpoint;
2425
2426 coap_lock_lock(return NULL);
2427 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2429 return endpoint;
2430}
2431
2433coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2434 coap_proto_t proto) {
2435 coap_endpoint_t *ep = NULL;
2436
2437 if (!context || !listen_addr) {
2438 coap_log_debug("coap_new_endpoint: Both context and listen_addr need to be defined\n");
2439 return NULL;
2440 }
2441
2443
2444 switch (proto) {
2445 case COAP_PROTO_UDP:
2446 break;
2447 case COAP_PROTO_DTLS:
2448 if (!coap_dtls_is_supported()) {
2449 coap_log_warn("coap_new_endpoint: DTLS not supported\n");
2450 return NULL;
2451 }
2452 break;
2453 case COAP_PROTO_TLS:
2454 if (!coap_tls_is_supported()) {
2455 coap_log_warn("coap_new_endpoint: TLS not supported\n");
2456 return NULL;
2457 }
2458 break;
2459 case COAP_PROTO_TCP:
2460 if (!coap_tcp_is_supported()) {
2461 coap_log_warn("coap_new_endpoint: TCP not supported\n");
2462 return NULL;
2463 }
2464 break;
2465 case COAP_PROTO_WS:
2466 if (!coap_ws_is_supported()) {
2467 coap_log_warn("coap_new_endpoint: WS not supported\n");
2468 return NULL;
2469 }
2470 break;
2471 case COAP_PROTO_WSS:
2472 if (!coap_wss_is_supported()) {
2473 coap_log_warn("coap_new_endpoint: WSS not supported\n");
2474 return NULL;
2475 }
2476 break;
2477 case COAP_PROTO_NONE:
2478 case COAP_PROTO_LAST:
2479 default:
2480 coap_log_crit("coap_new_endpoint: Unsupported protocol %d\n", proto);
2481 return NULL;
2482 }
2483
2484 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2485 proto == COAP_PROTO_WSS) {
2487 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2488 "coap_context_set_pki() not called\n");
2489 return NULL;
2490 }
2491 }
2492 ep = coap_malloc_endpoint();
2493 if (!ep) {
2494 coap_log_warn("coap_new_endpoint: malloc");
2495 return NULL;
2496 }
2497
2498 memset(ep, 0, sizeof(coap_endpoint_t));
2499 ep->context = context;
2500 ep->proto = proto;
2501 ep->sock.endpoint = ep;
2502 if (proto < COAP_PROTO_LAST) {
2503 /*
2504 * Should be caught above, but come compilers realize that proto
2505 * includes COAP_PROTO_LAST, but there is no table entry for that
2506 */
2507 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2508 }
2509
2510 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2511 if (!coap_netif_dgrm_listen(ep, listen_addr))
2512 goto error;
2513#ifdef WITH_CONTIKI
2514 ep->sock.context = context;
2515#endif /* WITH_CONTIKI */
2516#if !COAP_DISABLE_TCP
2517 } else if (COAP_PROTO_RELIABLE(proto)) {
2518 if (!coap_netif_strm_listen(ep, listen_addr))
2519 goto error;
2520#endif /* !COAP_DISABLE_TCP */
2521 } else {
2522 coap_log_crit("coap_new_endpoint: Protocol type not supported %d\n", proto);
2523 goto error;
2524 }
2525
2527#ifndef INET6_ADDRSTRLEN
2528#define INET6_ADDRSTRLEN 40
2529#endif
2530 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2531
2532 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2533 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2534 addr_str);
2535 }
2536 }
2537
2539
2540#ifdef COAP_EPOLL_SUPPORT
2541 ep->sock.endpoint = ep;
2543 EPOLLIN,
2544 __func__);
2545#endif /* COAP_EPOLL_SUPPORT */
2546
2547 LL_PREPEND(context->endpoint, ep);
2548 return ep;
2549
2550error:
2552 return NULL;
2553}
2554
2555void
2557 ep->default_mtu = (uint16_t)mtu;
2558}
2559
2560COAP_API void
2562 if (ep) {
2563 coap_context_t *context = ep->context;
2564 if (context) {
2565 coap_lock_lock(return);
2566 }
2568 if (context) {
2570 }
2571 }
2572}
2573
2574void
2576 if (ep) {
2577 coap_session_t *session, *rtmp;
2578
2579 if (ep->context) {
2580 /* If fully allocated and inserted */
2582 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2583 if (session->ref > 0) {
2585#if COAP_CLIENT_SUPPORT
2586 if (session->client_initiated) {
2587 coap_session_release_lkd(session);
2588 }
2589#endif /* COAP_CLIENT_SUPPORT */
2590 }
2591 assert(session->ref == 0);
2592 if (session->ref == 0) {
2594 coap_session_free(session);
2595 }
2596 }
2597 if (coap_netif_available_ep(ep)) {
2598 /*
2599 * ep->sock.endpoint is set in coap_new_endpoint().
2600 * ep->sock.session is never set.
2601 *
2602 * session->sock.session is set for both clients and servers (when a
2603 * new session is accepted), but does not affect the endpoint.
2604 *
2605 * So, it is safe to call coap_netif_close_ep() after all the sessions
2606 * have been freed above as we are only working with the endpoint sock.
2607 */
2608#ifdef COAP_EPOLL_SUPPORT
2609 assert(ep->sock.session == NULL);
2610#endif /* COAP_EPOLL_SUPPORT */
2612 }
2613
2614 if (ep->context->endpoint) {
2615 LL_DELETE(ep->context->endpoint, ep);
2616 }
2617 }
2618
2619 if (ep->app_cb) {
2620 ep->app_cb(ep->app_data);
2621 }
2622
2624 }
2625}
2626
2627void *
2629 assert(endpoint);
2630 return endpoint->app_data;
2631}
2632
2633COAP_API void *
2634coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *app_data,
2636 void *old_data;
2637
2638 coap_lock_lock(return NULL);
2639 old_data = coap_endpoint_set_app_data_lkd(endpoint, app_data, callback);
2641 return old_data;
2642}
2643
2644void *
2645coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *app_data,
2647 void *old_data = endpoint->app_data;
2648
2649 endpoint->app_data = app_data;
2650 endpoint->app_cb = app_data ? callback : NULL;
2651 return old_data;
2652}
2653
2654#endif /* COAP_SERVER_SUPPORT */
2655
2658 const coap_address_t *remote_addr,
2659 int ifindex) {
2660 coap_session_t *s, *rtmp;
2661#if COAP_CLIENT_SUPPORT
2662 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2663 if (s->ifindex == ifindex) {
2664 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2665 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2666 return s;
2667 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2668 return s;
2669 }
2670 }
2671#endif /* COAP_CLIENT_SUPPORT */
2672#if COAP_SERVER_SUPPORT
2673 coap_endpoint_t *ep;
2674
2675 LL_FOREACH(ctx->endpoint, ep) {
2676 SESSIONS_ITER(ep->sessions, s, rtmp) {
2677 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2678 remote_addr))
2679 return s;
2680 }
2681 }
2682#endif /* COAP_SERVER_SUPPORT */
2683 return NULL;
2684}
2685
2686#ifndef INET6_ADDRSTRLEN
2687#define INET6_ADDRSTRLEN 46
2688#endif
2689const char *
2691 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2692 char *p = szSession, *end = szSession + sizeof(szSession);
2693
2694 if (!session) {
2695 return "Session not defined";
2696 }
2697 if (coap_print_addr(&session->addr_info.local,
2698 (unsigned char *)p, end - p) > 0)
2699 p += strlen(p);
2700 if (p + 6 < end) {
2701 strcpy(p, " <-> ");
2702 p += 5;
2703 }
2704 if (p + 1 < end) {
2705 if (coap_print_addr(&session->addr_info.remote,
2706 (unsigned char *)p, end - p) > 0)
2707 p += strlen(p);
2708 }
2709 if (session->ifindex > 0 && p + 1 < end)
2710 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2711 if (p + 6 < end) {
2712 strcpy(p, " ");
2713 p++;
2714 strcpy(p, coap_proto_name(session->proto));
2715 }
2716
2717 return szSession;
2718}
2719
2720#if COAP_SERVER_SUPPORT
2721const char *
2722coap_endpoint_str(const coap_endpoint_t *endpoint) {
2723 static char szEndpoint[128];
2724 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2725 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2726 p += strlen(p);
2727 if (p + 6 < end) {
2728 if (endpoint->proto == COAP_PROTO_UDP) {
2729 strcpy(p, " UDP");
2730 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2731 strcpy(p, " DTLS");
2732 } else {
2733 strcpy(p, " NONE");
2734 }
2735 }
2736
2737 return szEndpoint;
2738}
2739#endif /* COAP_SERVER_SUPPORT */
2740#if COAP_CLIENT_SUPPORT
2741void
2743 session->no_observe_cancel = 1;
2744}
2745
2746void
2748 if (session->type != COAP_SESSION_TYPE_CLIENT) {
2749 coap_lock_lock(return);
2751 coap_session_release_lkd(session);
2753 }
2754}
2755#endif /* COAP_CLIENT_SUPPORT */
2756#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.
#define PRIu32
#define COAP_CLIENT_SUPPORT
coap_nack_reason_t
Definition coap_io.h:66
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:68
@ COAP_NACK_WS_FAILED
Definition coap_io.h:75
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:67
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:70
@ COAP_NACK_TLS_LAYER_FAILED
Definition coap_io.h:73
@ COAP_NACK_ICMP_ISSUE
Definition coap_io.h:71
@ COAP_NACK_WS_LAYER_FAILED
Definition coap_io.h:74
@ COAP_NACK_RST
Definition coap_io.h:69
@ COAP_NACK_BAD_RESPONSE
Definition coap_io.h:72
#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
coap_endpoint_t * coap_malloc_endpoint(void)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
void coap_mfree_endpoint(coap_endpoint_t *ep)
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:46
@ COAP_STRING
Definition coap_mem.h:34
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:261
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:147
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)
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:1480
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
void coap_check_update_token(coap_session_t *session, coap_pdu_t *pdu)
The function checks if the token needs to be updated before PDU is presented to the application (only...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *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:151
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:166
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
#define RESOURCES_ITER(r, tmp)
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:4963
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:2972
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:1348
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:1232
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:1849
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1258
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:3076
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:3115
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:56
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.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
#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:67
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:77
@ COAP_EVENT_SESSION_CONNECTED
Triggered when TCP layer completes exchange of CSM information.
Definition coap_event.h:65
@ COAP_EVENT_RECONNECT_FAILED
Triggered when a session failed, and a reconnect is going to be attempted.
Definition coap_event.h:147
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition coap_event.h:59
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition coap_event.h:69
@ 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:153
@ COAP_EVENT_RECONNECT_NO_MORE
Triggered when a session failed, and retry reconnect attempts failed.
Definition coap_event.h:151
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition coap_event.h:67
@ 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:149
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition coap_event.h:57
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition coap_event.h:55
@ COAP_EVENT_KEEPALIVE_FAILURE
Triggered when no response to a keep alive (ping) packet.
Definition coap_event.h:142
#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
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.
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:1655
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:194
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:417
#define COAP_PDU_MAX_UDP_HEADER_SIZE
coap_pdu_t * coap_new_pdu_lkd(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition coap_pdu.c:173
#define COAP_PDU_DELAYED
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#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:1517
#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:783
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:41
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:268
#define COAP_TOKEN_DEFAULT_MAX
Definition coap_pdu.h:60
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
Definition coap_pdu.h:204
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:318
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition coap_pdu.h:203
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:42
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:271
#define COAP_DEFAULT_MTU
Definition coap_pdu.h:45
#define COAP_BERT_BASE
Definition coap_pdu.h:48
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition coap_pdu.h:202
@ COAP_PROTO_WS
Definition coap_pdu.h:324
@ COAP_PROTO_DTLS
Definition coap_pdu.h:321
@ COAP_PROTO_UDP
Definition coap_pdu.h:320
@ COAP_PROTO_NONE
Definition coap_pdu.h:319
@ COAP_PROTO_TLS
Definition coap_pdu.h:323
@ COAP_PROTO_WSS
Definition coap_pdu.h:325
@ COAP_PROTO_TCP
Definition coap_pdu.h:322
@ COAP_PROTO_LAST
Definition coap_pdu.h:326
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:371
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:372
@ COAP_EMPTY_CODE
Definition coap_pdu.h:333
@ COAP_MESSAGE_NON
Definition coap_pdu.h:74
@ COAP_MESSAGE_CON
Definition coap_pdu.h:73
#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_session_t * coap_new_client_session_psk2_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)
Creates a new client session to the designated server with PSK credentials.
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.
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
#define COAP_ACK_RANDOM_FACTOR(s)
void coap_free_endpoint_lkd(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
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.
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_session_t * coap_new_client_session_pki_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)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
coap_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:1110
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).
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_endpoint_t * coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra)
Creates a new server session for the specified endpoint.
@ 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.
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.
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_API void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
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:120
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:61
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:214
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_bin_const_t *token)
Removes any subscription for session observer from resource and releases the allocated storage.
int coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
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:933
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:938
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:60
coap_address_t local
local address and port
Definition coap_io.h:61
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:67
size_t length
length of binary data
Definition coap_str.h:68
const uint8_t * s
read-only binary data
Definition coap_str.h:69
coap_session_t * session
The CoAP stack's global state is stored in a coap_context_t object.
unsigned int reconnect_time
Time to wait before reconnecting a failed client session.
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
uint8_t retry_count
Number of times to retry reconnecting a.
coap_resource_t * resources
hash table or list of known resources
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
coap_queue_t * sendqueue
uint32_t max_token_size
Largest token size supported RFC8974.
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_endpoint_t * endpoint
the endpoints used for listening
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
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 virtual endpoint that can be attached to coap_context_t.
void * app_data
application-specific data
coap_context_t * context
endpoint's context
uint16_t default_mtu
default mtu for this interface
coap_session_t * sessions
hash table or list of active sessions
coap_app_data_free_callback_t app_cb
call-back to release app_data
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
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) client receive information.
coap_binary_t * app_token
app requesting PDU token
coap_pdu_t * sent_pdu
The sent pdu with all the data.
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) server receive information.
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,...
coap_endpoint_t * endpoint
session's endpoint
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
uint8_t doing_first
Set if doing client's first request.
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
uint8_t client_initiated
Session initially started as a client.
uint64_t tx_token
Next token number to use.
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_address_t local_reconnect
local address to initiate reconnect from
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
unsigned ref_proxy_subs
reference count of current proxy subscriptions
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
uint8_t doing_send_recv
Set if coap_send_recv() active.
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_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_bin_const_t * req_token
Token in request pdu of coap_send_recv()
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
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.
uint8_t retry_count
Number of retries trying to re-connect.
coap_context_t * context
session's context
uint8_t session_failed
Set if session failed and can try re-connect.
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.
coap_pdu_t * cached_pdu
Cached copy of last ACK response PDU.
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_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
coap_address_t mcast_addr
remote address and port (multicast track)
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values