libcoap 4.3.5-develop-0bd03b6
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 > 0) {
176 session->max_retransmit = value;
177 coap_log_debug("***%s: session max_retransmit set to %u\n",
178 coap_session_str(session), session->max_retransmit);
179 }
180}
181
182void
183coap_session_set_nstart(coap_session_t *session, uint16_t value) {
184 if (value > 0) {
185 session->nstart = value;
186 coap_log_debug("***%s: session nstart set to %u\n",
187 coap_session_str(session), session->nstart);
188 }
189}
190
191void
193 coap_fixed_point_t value) {
194 if (value.integer_part > 0 && value.fractional_part < 1000) {
195 session->default_leisure = value;
196 coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
199 }
200}
201
202void
204 if (value > 0) {
205 session->probing_rate = value;
206 coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
207 coap_session_str(session), session->probing_rate);
208 }
209}
210
211void
213#if COAP_Q_BLOCK_SUPPORT
214 if (value > 0) {
215 session->max_payloads = value;
216 coap_log_debug("***%s: session max_payloads set to %u\n",
217 coap_session_str(session), session->max_payloads);
218 coap_session_fix_non_probing_wait_base(session);
219 coap_session_fix_non_partial_timeout(session);
220 }
221#else /* ! COAP_Q_BLOCK_SUPPORT */
222 (void)session;
223 (void)value;
224#endif /* ! COAP_Q_BLOCK_SUPPORT */
225}
226
227void
229#if COAP_Q_BLOCK_SUPPORT
230 if (value > 0) {
231 session->non_max_retransmit = value;
232 coap_log_debug("***%s: session non_max_retransmit set to %u\n",
233 coap_session_str(session), session->non_max_retransmit);
234 coap_session_fix_non_probing_wait_base(session);
235 coap_session_fix_non_partial_timeout(session);
236 }
237#else /* ! COAP_Q_BLOCK_SUPPORT */
238 (void)session;
239 (void)value;
240#endif /* ! COAP_Q_BLOCK_SUPPORT */
241}
242
243void
245 coap_fixed_point_t value) {
246#if COAP_Q_BLOCK_SUPPORT
247 if (value.integer_part > 0 && value.fractional_part < 1000) {
248 session->non_timeout = value;
249 coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
250 coap_session_str(session), session->non_timeout.integer_part,
251 session->non_timeout.fractional_part);
252 coap_session_fix_non_probing_wait_base(session);
253 coap_session_fix_non_partial_timeout(session);
254 }
255#else /* ! COAP_Q_BLOCK_SUPPORT */
256 (void)session;
257 (void)value;
258#endif /* ! COAP_Q_BLOCK_SUPPORT */
259}
260
261void
263 coap_fixed_point_t value) {
264#if COAP_Q_BLOCK_SUPPORT
265 if (value.integer_part > 0 && value.fractional_part < 1000)
266 session->non_receive_timeout = value;
267 coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
268 coap_session_str(session),
269 session->non_receive_timeout.integer_part,
270 session->non_receive_timeout.fractional_part);
271#else /* ! COAP_Q_BLOCK_SUPPORT */
272 (void)session;
273 (void)value;
274#endif /* ! COAP_Q_BLOCK_SUPPORT */
275}
276
279 return session->ack_timeout;
280}
281
284 return session->ack_random_factor;
285}
286
287uint16_t
289 return session->max_retransmit;
290}
291
292uint16_t
294 return session->nstart;
295}
296
299 return session->default_leisure;
300}
301
302uint32_t
304 return session->probing_rate;
305}
306
307uint16_t
309#if COAP_Q_BLOCK_SUPPORT
310 return session->max_payloads;
311#else /* ! COAP_Q_BLOCK_SUPPORT */
312 (void)session;
314#endif /* ! COAP_Q_BLOCK_SUPPORT */
315}
316
317uint16_t
319#if COAP_Q_BLOCK_SUPPORT
320 return session->non_max_retransmit;
321#else /* ! COAP_Q_BLOCK_SUPPORT */
322 (void)session;
324#endif /* ! COAP_Q_BLOCK_SUPPORT */
325}
326
329#if COAP_Q_BLOCK_SUPPORT
330 return session->non_timeout;
331#else /* ! COAP_Q_BLOCK_SUPPORT */
332 (void)session;
334#endif /* ! COAP_Q_BLOCK_SUPPORT */
335}
336
339#if COAP_Q_BLOCK_SUPPORT
340 return session->non_receive_timeout;
341#else /* ! COAP_Q_BLOCK_SUPPORT */
342 (void)session;
344#endif /* ! COAP_Q_BLOCK_SUPPORT */
345}
346
349 coap_lock_lock(return NULL);
352 return session;
353}
354
357 ++session->ref;
358 return session;
359}
360
361COAP_API void
363 if (session) {
364#if COAP_THREAD_SAFE
365 coap_context_t *context = session->context;
366 (void)context;
367#endif /* COAP_THREAD_SAFE */
368
369 coap_lock_lock(return);
372 }
373}
374
375void
377 if (session) {
379#ifndef __COVERITY__
380 assert(session->ref > 0);
381 if (session->ref > 0)
382 --session->ref;
383 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
384 coap_session_free(session);
385#else /* __COVERITY__ */
386 /* Coverity scan is fooled by the reference counter leading to
387 * false positives for USE_AFTER_FREE. */
388 --session->ref;
389 __coverity_negative_sink__(session->ref);
390 /* Indicate that resources are released properly. */
391 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
392 __coverity_free__(session);
393 }
394#endif /* __COVERITY__ */
395 }
396}
397
398COAP_API void
399coap_session_set_app_data(coap_session_t *session, void *app_data) {
400 assert(session);
401 coap_lock_lock(return);
402 coap_session_set_app_data2_lkd(session, app_data, NULL);
404}
405
406void *
408 assert(session);
409 return session->app_data;
410}
411
412COAP_API void *
415 void *old_data;
416
417 coap_lock_lock(return NULL);
418 old_data = coap_session_set_app_data2_lkd(session, app_data, callback);
420 return old_data;
421}
422
423void *
426 void *old_data = session->app_data;
427
428 session->app_data = app_data;
429 session->app_cb = app_data ? callback : NULL;
430 return old_data;
431}
432
433static coap_session_t *
435 const coap_addr_hash_t *addr_hash,
436 const coap_address_t *local_addr,
437 const coap_address_t *remote_addr, int ifindex,
438 coap_context_t *context, coap_endpoint_t *endpoint) {
440 sizeof(coap_session_t));
441#if ! COAP_SERVER_SUPPORT
442 (void)endpoint;
443#endif /* ! COAP_SERVER_SUPPORT */
444 if (!session)
445 return NULL;
446 memset(session, 0, sizeof(*session));
447 session->proto = proto;
448 session->type = type;
449 if (addr_hash)
450 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
451 else
452 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
453 if (local_addr) {
454 coap_address_copy(&session->addr_info.local, local_addr);
455#if COAP_CLIENT_SUPPORT
456 coap_address_copy(&session->local_reconnect, local_addr);
457#endif /* COAP_CLIENT_SUPPORT */
458 } else {
460#if COAP_CLIENT_SUPPORT
462#endif /* COAP_CLIENT_SUPPORT */
463 }
464 if (remote_addr)
465 coap_address_copy(&session->addr_info.remote, remote_addr);
466 else
468 session->ifindex = ifindex;
469 session->context = context;
470#if COAP_SERVER_SUPPORT
471 session->endpoint = endpoint;
472 if (endpoint)
473 session->mtu = endpoint->default_mtu;
474 else
475#endif /* COAP_SERVER_SUPPORT */
477 session->block_mode = context->block_mode;
478 if (proto == COAP_PROTO_DTLS) {
479 session->tls_overhead = 29;
480 if (session->tls_overhead >= session->mtu) {
481 session->tls_overhead = session->mtu;
482 coap_log_err("DTLS overhead exceeds MTU\n");
483 }
484 }
488 session->nstart = COAP_DEFAULT_NSTART;
491#if COAP_Q_BLOCK_SUPPORT
492 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
493 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
494 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
495 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
496 coap_session_fix_non_probing_wait_base(session);
497 coap_session_fix_non_partial_timeout(session);
498#endif /* COAP_Q_BLOCK_SUPPORT */
499 session->dtls_event = -1;
504 session->max_token_size = context->max_token_size; /* RFC8974 */
505 if (session->type != COAP_SESSION_TYPE_CLIENT)
507
508 /* Randomly initialize */
509 /* TCP/TLS have no notion of mid */
510 if (COAP_PROTO_NOT_RELIABLE(session->proto))
511 coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
512 coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
513
514 return session;
515}
516
517void
519 coap_queue_t *q, *tmp;
520 coap_lg_xmit_t *lq, *ltmp;
521
522#if COAP_PROXY_SUPPORT
523 if (session->ref_proxy_subs)
524 coap_delete_proxy_subscriber(session, NULL, 0, COAP_PROXY_SUBS_ALL);
525#endif /* COAP_PROXY_SUPPORT */
526#if COAP_CLIENT_SUPPORT
527 coap_lg_crcv_t *lg_crcv, *etmp;
528
529 /* Need to do this before (D)TLS and socket is closed down */
530 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
531 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
532 /* Need to close down observe */
533 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
534 /* Need to delete node we set up for NON */
535 coap_queue_t *queue = session->context->sendqueue;
536
537 while (queue) {
538 if (queue->session == session) {
540 break;
541 }
542 queue = queue->next;
543 }
544 }
545 }
546 /* In case coap_cancel_observe_lkd() failure, which could clear down lg_crcv */
547 if (!session->lg_crcv)
548 break;
549 LL_DELETE(session->lg_crcv, lg_crcv);
550 coap_block_delete_lg_crcv(session, lg_crcv);
551 }
552#endif /* COAP_CLIENT_SUPPORT */
553
554 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
555 if (q->pdu->type==COAP_MESSAGE_CON) {
556 coap_handle_nack(session, q->pdu,
557 session->proto == COAP_PROTO_DTLS ?
559 q->id);
560 }
562 }
563 if (session->partial_pdu)
565 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
566 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
567 if (session->psk_identity)
569 if (session->psk_key)
571 if (session->psk_hint)
573
574#if COAP_SERVER_SUPPORT
575 coap_cache_entry_t *cp, *ctmp;
576 HASH_ITER(hh, session->context->cache, cp, ctmp) {
577 /* cp->session is NULL if not session based */
578 if (cp->session == session) {
579 coap_delete_cache_entry(session->context, cp);
580 }
581 }
582#endif /* COAP_PROXY_SUPPORT */
583 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
584 LL_DELETE(session->lg_xmit, lq);
585 coap_block_delete_lg_xmit(session, lq);
586 }
587#if COAP_SERVER_SUPPORT
588 coap_lg_srcv_t *sq, *stmp;
589
590 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
591 LL_DELETE(session->lg_srcv, sq);
592 coap_block_delete_lg_srcv(session, sq);
593 }
594#endif /* COAP_SERVER_SUPPORT */
595#if COAP_OSCORE_SUPPORT
597#endif /* COAP_OSCORE_SUPPORT */
598#if COAP_WS_SUPPORT
599 coap_free_type(COAP_STRING, session->ws);
600 coap_delete_str_const(session->ws_host);
601#endif /* COAP_WS_SUPPORT */
602}
603
604void
606 if (!session)
607 return;
609 assert(session->ref == 0);
610 if (session->ref)
611 return;
612 /* Make sure nothing gets deleted under our feet */
614 coap_session_mfree(session);
615#if COAP_SERVER_SUPPORT
617 if (session->endpoint) {
618 if (session->endpoint->sessions)
619 SESSIONS_DELETE(session->endpoint->sessions, session);
620 } else
621#endif /* COAP_SERVER_SUPPORT */
622#if COAP_CLIENT_SUPPORT
623 if (session->context) {
624 if (session->context->sessions)
625 SESSIONS_DELETE(session->context->sessions, session);
626 }
628#endif /* COAP_CLIENT_SUPPORT */
630 coap_delete_bin_const(session->echo);
631#if COAP_SERVER_SUPPORT
633#endif /* COAP_SERVER_SUPPORT */
634
635 if (session->app_cb) {
636 session->app_cb(session->app_data);
637 }
638 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
639 (void *)session);
640 assert(session->ref == 1);
642}
643
644#if COAP_SERVER_SUPPORT
645void
647 int i;
648
651 coap_cancel_all_messages(session->context, session, NULL);
652 RESOURCES_ITER(session->context->resources, r) {
653 /* In case code is broken somewhere */
654 for (i = 0; i < 1000; i++) {
655 if (!coap_delete_observer(r, session, NULL))
656 break;
657 }
658 }
659 if (session->context->unknown_resource) {
660 /* In case code is broken somewhere */
661 for (i = 0; i < 1000; i++) {
662 if (!coap_delete_observer(session->context->unknown_resource, session, NULL))
663 break;
664 }
665 }
666 if (session->context->proxy_uri_resource) {
667 /* In case code is broken somewhere */
668 for (i = 0; i < 1000; i++) {
669 if (!coap_delete_observer(session->context->proxy_uri_resource, session, NULL))
670 break;
671 }
672 }
673 while (session->delayqueue) {
674 coap_queue_t *q = session->delayqueue;
675
676 session->delayqueue = q->next;
678 }
679 /* Force session to go away */
682
684}
685#endif /* COAP_SERVER_SUPPORT */
686
687static size_t
689 size_t max_with_header) {
690#if COAP_DISABLE_TCP
691 (void)session;
692 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
693 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
694 : 0;
695#else /* !COAP_DISABLE_TCP */
696 if (COAP_PROTO_NOT_RELIABLE(session->proto))
697 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
698 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
699 : 0;
700 /* we must assume there is no token to be on the safe side */
701 if (max_with_header <= 2)
702 return 0;
703 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
704 return max_with_header - 2;
705 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
706 return max_with_header - 3;
707 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
708 return max_with_header - 4;
709 else
710 return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE;
711#endif /* !COAP_DISABLE_TCP */
712}
713
714size_t
716 if (session->csm_rcv_mtu)
718 (size_t)(session->csm_rcv_mtu));
719
721 (size_t)(session->mtu - session->tls_overhead));
722}
723
724COAP_API size_t
726 size_t size;
727 coap_session_t *session_rw;
728
729 /*
730 * Need to do this to not get a compiler warning about const parameters
731 * but need to maintain source code backward compatibility
732 */
733 memcpy(&session_rw, &session, sizeof(session_rw));
734 coap_lock_lock(return 0);
735 size = coap_session_max_pdu_size_lkd(session_rw);
737 return size;
738}
739
740size_t
742 size_t max_with_header;
743
745#if COAP_CLIENT_SUPPORT
746 /*
747 * Delay if session->doing_first is set.
748 * E.g. Reliable and CSM not in yet for checking block support
749 */
750 coap_session_t *session_rw;
751
752 /*
753 * Need to do this to not get a compiler warning about const parameters
754 * but need to maintain source code backward compatibility
755 */
756 memcpy(&session_rw, &session, sizeof(session_rw));
757 if (coap_client_delay_first(session_rw) == 0) {
758 coap_log_debug("coap_client_delay_first: timeout\n");
759 /* Have to go with the defaults */
760 }
761#endif /* COAP_CLIENT_SUPPORT */
762
763 max_with_header = (size_t)(session->mtu - session->tls_overhead);
764
765 return coap_session_max_pdu_size_internal(session, max_with_header);
766}
767
768void
769coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
770 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE) {
771 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
772 coap_log_debug("* %s: Restricting MTU size to %u\n",
773 coap_session_str(session), mtu);
774 }
775 if (mtu < 64)
776 mtu = 64;
777 session->mtu = mtu;
778 if (session->tls_overhead >= session->mtu) {
779 session->tls_overhead = session->mtu;
780 coap_log_err("DTLS overhead exceeds MTU\n");
781 }
782}
783
784ssize_t
786 coap_queue_t *node) {
787 if (node) {
788 coap_queue_t *removed = NULL;
789 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
790 assert(removed == node);
792 node->session = NULL;
793 node->t = 0;
794 } else {
795 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
796 coap_queue_t *q = NULL;
797 /* Check same mid is not getting re-used in violation of RFC7252 */
798 LL_FOREACH(session->delayqueue, q) {
799 if (q->id == pdu->mid) {
800 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
801 coap_session_str(session), pdu->mid);
802 return COAP_INVALID_MID;
803 }
804 }
805 }
806 node = coap_new_node();
807 if (node == NULL)
808 return COAP_INVALID_MID;
809 node->id = pdu->mid;
810 node->pdu = pdu;
811 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
812 uint8_t r;
813 coap_prng_lkd(&r, sizeof(r));
814 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
815 node->timeout = coap_calc_timeout(session, r);
816 }
817 coap_address_copy(&node->remote, &session->addr_info.remote);
818 }
819 LL_APPEND(session->delayqueue, node);
820 coap_log_debug("** %s: mid=0x%04x: delayed\n",
821 coap_session_str(session), node->id);
822 return COAP_PDU_DELAYED;
823}
824
825#if !COAP_DISABLE_TCP
826void
828 coap_pdu_t *pdu;
829 uint8_t buf[4];
830 assert(COAP_PROTO_RELIABLE(session->proto));
831 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
832 session->state = COAP_SESSION_STATE_CSM;
833 session->partial_write = 0;
834 if (session->mtu == 0)
835 coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */
837 if (pdu == NULL
839 coap_encode_var_safe(buf, sizeof(buf),
840 session->context->csm_max_message_size), buf) == 0
842 coap_encode_var_safe(buf, sizeof(buf),
843 0), buf) == 0
844 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
847 coap_encode_var_safe(buf, sizeof(buf),
848 session->max_token_size),
849 buf) == 0)
850 || coap_pdu_encode_header(pdu, session->proto) == 0
851 ) {
853 } else {
854 ssize_t bytes_written;
855
856 pdu->session = session;
857 bytes_written = coap_session_send_pdu(session, pdu);
858 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
860 } else {
861 session->csm_rcv_mtu = session->context->csm_max_message_size;
862 if (session->csm_rcv_mtu > COAP_BERT_BASE)
863 session->csm_bert_loc_support = 1;
864 else
865 session->csm_bert_loc_support = 0;
866 }
867 }
868 if (pdu)
870}
871#endif /* !COAP_DISABLE_TCP */
872
875 coap_mid_t mid;
876
878 mid = coap_session_send_ping_lkd(session);
879 if (mid != COAP_INVALID_MID) {
880 coap_tick_t now;
881
882 coap_ticks(&now);
883 session->last_ping_mid = mid;
884 session->last_rx_tx = now;
885 session->last_ping = now;
886 }
888 return mid;
889}
890
893 coap_pdu_t *ping = NULL;
894
896 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
897 session->con_active)
898 return COAP_INVALID_MID;
899 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
900 uint16_t mid = coap_new_message_id_lkd(session);
901 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
902 }
903#if !COAP_DISABLE_TCP
904 else {
906 }
907#endif /* !COAP_DISABLE_TCP */
908 if (!ping)
909 return COAP_INVALID_MID;
910 return coap_send_internal(session, ping, NULL);
911}
912
913void
915 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
916 coap_log_debug("***%s: session connected\n",
917 coap_session_str(session));
918 if (session->state == COAP_SESSION_STATE_CSM) {
920#if COAP_CLIENT_SUPPORT
922#endif /* COAP_CLIENT_SUPPORT */
923 if (session->doing_first)
924 session->doing_first = 0;
925 }
926 }
927
929 session->partial_write = 0;
930
931 if (session->proto==COAP_PROTO_DTLS) {
932 session->tls_overhead = coap_dtls_get_overhead(session);
933 if (session->tls_overhead >= session->mtu) {
934 session->tls_overhead = session->mtu;
935 coap_log_err("DTLS overhead exceeds MTU\n");
936 }
937 }
938
939 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
940 ssize_t bytes_written;
941 coap_queue_t *q = session->delayqueue;
942 coap_address_t remote;
943
944 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
945 if (session->con_active >= COAP_NSTART(session))
946 break;
947 session->con_active++;
948 }
949 /* Take entry off the queue */
950 session->delayqueue = q->next;
951 q->next = NULL;
952
953 coap_address_copy(&remote, &session->addr_info.remote);
955 coap_log_debug("** %s: mid=0x%04x: transmitted after delay (2)\n",
956 coap_session_str(session), (int)q->pdu->mid);
957 bytes_written = coap_session_send_pdu(session, q->pdu);
958 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
959 if (coap_wait_ack(session->context, session, q) >= 0)
960 q = NULL;
961 }
962 coap_address_copy(&session->addr_info.remote, &remote);
963 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
964 if (q)
966 if (bytes_written < 0)
967 break;
968 } else if (q) {
969 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
970 q->next = session->delayqueue;
971 session->delayqueue = q;
972 if (bytes_written > 0)
973 session->partial_write = (size_t)bytes_written;
974 break;
975 } else {
977 }
978 }
979 }
980}
981
982#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
983static const char *
985 switch (reason) {
987 return "COAP_NACK_TOO_MANY_RETRIES";
989 return "COAP_NACK_NOT_DELIVERABLE";
990 case COAP_NACK_RST:
991 return "COAP_NACK_RST";
993 return "COAP_NACK_TLS_FAILED";
995 return "COAP_NACK_ICMP_ISSUE";
997 return "COAP_NACK_BAD_RESPONSE";
999 return "COAP_NACK_TLS_LAYER_FAILED";
1001 return "COAP_NACK_WS_LAYER_FAILED";
1003 return "COAP_NACK_WS_FAILED";
1004 default:
1005 return "???";
1006 }
1007}
1008#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
1009
1010void
1012 coap_pdu_t *sent,
1013 const coap_nack_reason_t reason,
1014 const coap_mid_t mid) {
1015 if (session->context->nack_handler) {
1016 coap_bin_const_t token = {0, NULL};
1017
1018 if (sent) {
1019 coap_check_update_token(session, sent);
1020 token = sent->actual_token;
1021 }
1022 coap_lock_callback(session->context->nack_handler(session, sent, reason, mid));
1023 if (sent) {
1024 coap_update_token(sent, token.length, token.s);
1025 }
1026 }
1027#if COAP_CLIENT_SUPPORT
1028 if (reason != COAP_NACK_ICMP_ISSUE) {
1029 session->doing_send_recv = 0;
1030 }
1031#endif /* COAP_CLIENT_SUPPORT */
1032}
1033
1034COAP_API void
1040
1041void
1043#if !COAP_DISABLE_TCP
1044 coap_session_state_t state = session->state;
1045#endif /* !COAP_DISABLE_TCP */
1046 coap_lg_xmit_t *lq, *ltmp;
1047#if COAP_SERVER_SUPPORT
1048 coap_lg_srcv_t *sq, *stmp;
1049#endif /* COAP_SERVER_SUPPORT */
1050#if COAP_CLIENT_SUPPORT
1051 coap_lg_crcv_t *cq, *etmp;
1052#endif /* COAP_CLIENT_SUPPORT */
1053 int sent_nack = 0;
1054 coap_queue_t *q;
1055
1057#if COAP_CLIENT_SUPPORT
1058 coap_session_failed(session);
1059#endif /* COAP_CLIENT_SUPPORT */
1060
1061 q = session->context->sendqueue;
1062 while (q) {
1063 if (q->session == session) {
1064 /* Take the first one */
1065 coap_handle_nack(session, q->pdu, reason, q->id);
1066 sent_nack = 1;
1067 break;
1068 }
1069 q = q->next;
1070 }
1071
1072 if (reason != COAP_NACK_ICMP_ISSUE) {
1073 while (session->delayqueue) {
1074 q = session->delayqueue;
1075 session->delayqueue = q->next;
1076 q->next = NULL;
1077 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1078 coap_session_str(session), q->id);
1079 if (q->pdu->type == COAP_MESSAGE_CON) {
1080 coap_handle_nack(session, q->pdu, reason, q->id);
1081 sent_nack = 1;
1082 }
1083
1084#if COAP_CLIENT_SUPPORT
1085 session->doing_send_recv = 0;
1086#endif /* COAP_CLIENT_SUPPORT */
1088 }
1089 }
1090#if COAP_CLIENT_SUPPORT
1091 if (!sent_nack && session->lg_crcv) {
1092 /* Take the first one */
1093 coap_handle_nack(session, session->lg_crcv->sent_pdu, reason,
1094 session->lg_crcv->sent_pdu->mid);
1095 sent_nack = 1;
1096 }
1097#endif /* COAP_CLIENT_SUPPORT */
1098 if (!sent_nack) {
1099 /* Unable to determine which request disconnection was for */
1100 coap_handle_nack(session, NULL, reason, 0);
1101 }
1102 if (reason == COAP_NACK_ICMP_ISSUE) {
1103 coap_log_debug("***%s: session ICMP issue (%s)\n",
1104 coap_session_str(session), coap_nack_name(reason));
1105 return;
1106 }
1107 coap_log_debug("***%s: session disconnected (%s)\n",
1108 coap_session_str(session), coap_nack_name(reason));
1109#if COAP_SERVER_SUPPORT
1110 coap_delete_observers(session->context, session);
1111#endif /* COAP_SERVER_SUPPORT */
1112
1113 if (session->proto == COAP_PROTO_UDP)
1115 else
1116 session->state = COAP_SESSION_STATE_NONE;
1117
1118 session->con_active = 0;
1119
1120 if (session->partial_pdu) {
1122 session->partial_pdu = NULL;
1123 }
1124 session->partial_read = 0;
1125
1126 /* Not done if nack handler called above */
1127 while (session->delayqueue) {
1128 q = session->delayqueue;
1129 session->delayqueue = q->next;
1130 q->next = NULL;
1131 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1132 coap_session_str(session), q->id);
1133#if COAP_CLIENT_SUPPORT
1134 session->doing_send_recv = 0;
1135#endif /* COAP_CLIENT_SUPPORT */
1137 }
1138
1139#if COAP_CLIENT_SUPPORT
1140 if (!session->session_failed) {
1141 /* Need to do this before (D)TLS and socket is closed down */
1142 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1143 LL_DELETE(session->lg_crcv, cq);
1144 coap_block_delete_lg_crcv(session, cq);
1145 }
1146 }
1147#endif /* COAP_CLIENT_SUPPORT */
1148 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1149 LL_DELETE(session->lg_xmit, lq);
1150 coap_block_delete_lg_xmit(session, lq);
1151 }
1152#if COAP_SERVER_SUPPORT
1153 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1154 LL_DELETE(session->lg_srcv, sq);
1155 coap_block_delete_lg_srcv(session, sq);
1156 }
1157#endif /* COAP_SERVER_SUPPORT */
1158 coap_cancel_session_messages(session->context, session, reason);
1159
1160#if !COAP_DISABLE_TCP
1161 if (COAP_PROTO_RELIABLE(session->proto)) {
1162 if (coap_netif_available(session)) {
1166 }
1167#if COAP_CLIENT_SUPPORT
1168 if (state != COAP_SESSION_STATE_NONE && !session->session_failed) {
1172 }
1173#endif /* COAP_CLIENT_SUPPORT */
1174 if (session->doing_first)
1175 session->doing_first = 0;
1176 }
1177#endif /* !COAP_DISABLE_TCP */
1178 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1179 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1180}
1181
1182#if COAP_CLIENT_SUPPORT
1183void
1185 if (session->context->reconnect_time &&
1187 session->type == COAP_SESSION_TYPE_CLIENT) {
1188 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1189 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1190 session->session_failed = 1;
1191 coap_ticks(&session->last_rx_tx);
1192 }
1193}
1194#endif /* COAP_CLIENT_SUPPORT */
1195
1196#if COAP_SERVER_SUPPORT
1197static void
1198coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1199 const coap_addr_tuple_t *addr_info) {
1200 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1201 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1202 addr_hash->lport = coap_address_get_port(&addr_info->local);
1203 addr_hash->proto = proto;
1204}
1205
1208 const coap_packet_t *packet, coap_tick_t now) {
1209 coap_session_t *session;
1210 coap_session_t *rtmp;
1211 unsigned int num_idle = 0;
1212 unsigned int num_hs = 0;
1213 coap_session_t *oldest = NULL;
1214 coap_session_t *oldest_hs = NULL;
1215 coap_addr_hash_t addr_hash;
1216
1217 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1218 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1219 if (session) {
1220 /* Maybe mcast or unicast IP address which is not in the hash */
1221 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1222 session->ifindex = packet->ifindex;
1223 session->last_rx_tx = now;
1224 return session;
1225 }
1226
1227#if COAP_CLIENT_SUPPORT
1228 SESSIONS_FIND(endpoint->context->sessions, addr_hash, session);
1229 if (session) {
1230 /* Maybe mcast or unicast IP address which is not in the hash */
1231 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1232 session->ifindex = packet->ifindex;
1233 session->last_rx_tx = now;
1234 return session;
1235 }
1236
1237 if (coap_is_mcast(&packet->addr_info.local)) {
1238 /* Check if this a proxy client packet we sent on another socket */
1239 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1240 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1243 /* Drop looped back packet to stop recursion / confusion */
1244 return NULL;
1245 }
1246 }
1247 }
1248#endif /* COAP_CLIENT_SUPPORT */
1249 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1250 if (session->ref == 0 && session->delayqueue == NULL) {
1251 if (session->type == COAP_SESSION_TYPE_SERVER) {
1252 ++num_idle;
1253 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1254 oldest = session;
1255
1256 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1257 ++num_hs;
1258 /* See if this is a partial (D)TLS session set up
1259 which needs to be cleared down to prevent DOS */
1260 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1261 if (oldest_hs == NULL ||
1262 session->last_rx_tx < oldest_hs->last_rx_tx)
1263 oldest_hs = session;
1264 }
1265 }
1266 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1267 ++num_hs;
1268 /* See if this is a partial (D)TLS session set up for Client Hello
1269 which needs to be cleared down to prevent DOS */
1270 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1271 if (oldest_hs == NULL ||
1272 session->last_rx_tx < oldest_hs->last_rx_tx)
1273 oldest_hs = session;
1274 }
1275 }
1276 }
1277 }
1278
1279 if (endpoint->context->max_idle_sessions > 0 &&
1280 num_idle >= endpoint->context->max_idle_sessions) {
1282 coap_session_free(oldest);
1283 } else if (oldest_hs) {
1284 coap_log_warn("***%s: Incomplete session timed out\n",
1285 coap_session_str(oldest_hs));
1287 coap_session_free(oldest_hs);
1288 }
1289
1290 if (num_hs > (endpoint->context->max_handshake_sessions ?
1291 endpoint->context->max_handshake_sessions :
1293 /* Maxed out on number of sessions in (D)TLS negotiation state */
1294 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1295 "large. New request ignored\n");
1296 return NULL;
1297 }
1298
1299 if (endpoint->proto == COAP_PROTO_DTLS) {
1300 /*
1301 * Need to check that this actually is a Client Hello before wasting
1302 * time allocating and then freeing off session.
1303 */
1304
1305 /*
1306 * Generic header structure of the DTLS record layer.
1307 * typedef struct __attribute__((__packed__)) {
1308 * uint8_t content_type; content type of the included message
1309 * uint16_t version; Protocol version
1310 * uint16_t epoch; counter for cipher state changes
1311 * uint8_t sequence_number[6]; sequence number
1312 * uint16_t length; length of the following fragment
1313 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1314 * } dtls_record_handshake_t;
1315 */
1316#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1317#define DTLS_CT_ALERT 21 /* Content Type Alert */
1318#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1319#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1320#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1321#define DTLS_CT_CID 25 /* Content Type Connection ID */
1322#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1323#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1324
1325 const uint8_t *payload = (const uint8_t *)packet->payload;
1326 size_t length = packet->length;
1327 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1328 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1329 payload[OFF_CONTENT_TYPE], length,
1330 OFF_HANDSHAKE_TYPE + 1);
1331 return NULL;
1332 }
1333 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1334 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1335 /* Client may have changed its IP address */
1336 int changed = 0;
1337
1338 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1339 if (session->client_cid) {
1340 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1341 length > (OFF_CID_DTLS13 + session->client_cid->length) &&
1342 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1343 session->client_cid->length) == 0) ||
1344 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1345 length > (OFF_CID + session->client_cid->length) &&
1346 memcmp(session->client_cid->s, &payload[OFF_CID],
1347 session->client_cid->length) == 0)) {
1348 /* Updating IP address */
1349 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1350 SESSIONS_DELETE(endpoint->sessions, session);
1351 session->addr_info = packet->addr_info;
1352 memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash));
1353 SESSIONS_ADD(endpoint->sessions, session);
1354 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1355 return session;
1356 }
1357 }
1358 }
1359 if (!changed) {
1360 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1361 return NULL;
1362 }
1363 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1364 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1365 /* only log if not a late alert */
1366 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1367 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1368 payload[OFF_CONTENT_TYPE],
1369 payload[OFF_HANDSHAKE_TYPE]);
1370 return NULL;
1371 }
1372 }
1373
1375 &addr_hash, &packet->addr_info.local,
1376 &packet->addr_info.remote,
1377 packet->ifindex, endpoint->context, endpoint);
1378 if (session) {
1379 session->last_rx_tx = now;
1380 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1381 sizeof(session->sock.lfunc));
1382 if (endpoint->proto == COAP_PROTO_UDP)
1384 else if (endpoint->proto == COAP_PROTO_DTLS) {
1385 session->type = COAP_SESSION_TYPE_HELLO;
1386 }
1387 SESSIONS_ADD(endpoint->sessions, session);
1388 coap_log_debug("***%s: session %p: new incoming session\n",
1389 coap_session_str(session), (void *)session);
1391 }
1392 return session;
1393}
1394
1397 coap_tick_t now) {
1398 if (session) {
1399 session->last_rx_tx = now;
1400 session->type = COAP_SESSION_TYPE_SERVER;
1401 coap_dtls_establish(session);
1402 }
1403 return session;
1404}
1405#endif /* COAP_SERVER_SUPPORT */
1406
1407#if COAP_CLIENT_SUPPORT
1408static coap_session_t *
1409coap_session_create_client(coap_context_t *ctx,
1410 const coap_address_t *local_if,
1411 const coap_address_t *server,
1412 coap_proto_t proto) {
1413 coap_session_t *session = NULL;
1414 int default_port = COAP_DEFAULT_PORT;
1415
1416 assert(server);
1417
1418 switch (proto) {
1419 case COAP_PROTO_UDP:
1420 default_port = COAP_DEFAULT_PORT;
1421 break;
1422 case COAP_PROTO_DTLS:
1423 if (!coap_dtls_is_supported()) {
1424 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1425 return NULL;
1426 }
1427 default_port = COAPS_DEFAULT_PORT;
1428 break;
1429 case COAP_PROTO_TCP:
1430 if (!coap_tcp_is_supported()) {
1431 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1432 return NULL;
1433 }
1434 default_port = COAP_DEFAULT_PORT;
1435 break;
1436 case COAP_PROTO_TLS:
1437 if (!coap_tls_is_supported()) {
1438 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1439 return NULL;
1440 }
1441 default_port = COAPS_DEFAULT_PORT;
1442 break;
1443 case COAP_PROTO_WS:
1444 if (!coap_ws_is_supported()) {
1445 coap_log_crit("coap_new_client_session*: WS not supported\n");
1446 return NULL;
1447 }
1448 default_port = 80;
1449 break;
1450 case COAP_PROTO_WSS:
1451 if (!coap_wss_is_supported()) {
1452 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1453 return NULL;
1454 }
1455 default_port = 443;
1456 break;
1457 case COAP_PROTO_NONE:
1458 case COAP_PROTO_LAST:
1459 default:
1460 assert(0);
1461 return NULL;
1462 }
1463 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1464 local_if, server, 0, ctx, NULL);
1465 if (!session)
1466 goto error;
1467
1469 session->sock.session = session;
1470 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1471 sizeof(session->sock.lfunc));
1472
1473 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1474 coap_session_t *s, *rtmp;
1475 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1476 goto error;
1477 }
1478 /* Check that this is not a duplicate 4-tuple */
1479 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1482 &s->addr_info.local) &&
1484 &s->addr_info.remote)) {
1485 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1486 coap_session_str(session), (void *)session);
1487 goto error;
1488 }
1489 }
1490#ifdef WITH_CONTIKI
1491 session->sock.context = ctx;
1492#endif /* WITH_CONTIKI */
1493#if !COAP_DISABLE_TCP
1494 } else if (COAP_PROTO_RELIABLE(proto)) {
1495 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1496 goto error;
1497 }
1498#endif /* !COAP_DISABLE_TCP */
1499 }
1500
1501 session->sock.session = session;
1502#ifdef COAP_EPOLL_SUPPORT
1503 coap_epoll_ctl_add(&session->sock,
1504 EPOLLIN |
1505 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1506 EPOLLOUT : 0),
1507 __func__);
1508#endif /* COAP_EPOLL_SUPPORT */
1509
1511 if (local_if)
1512 session->sock.flags |= COAP_SOCKET_BOUND;
1513#if COAP_SERVER_SUPPORT
1514 if (ctx->proxy_uri_resource)
1515 session->proxy_session = 1;
1516#endif /* COAP_SERVER_SUPPORT */
1517 SESSIONS_ADD(ctx->sessions, session);
1518 return session;
1519
1520error:
1521 /*
1522 * Need to add in the session as coap_session_release_lkd()
1523 * will call SESSIONS_DELETE in coap_session_free().
1524 */
1525 if (session)
1526 SESSIONS_ADD(ctx->sessions, session);
1527 coap_session_release_lkd(session);
1528 return NULL;
1529}
1530
1531static void
1532coap_session_check_connect(coap_session_t *session) {
1533 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1534 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1535 }
1536#if !COAP_DISABLE_TCP
1537 if (COAP_PROTO_RELIABLE(session->proto)) {
1538 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1540 if (session->type == COAP_SESSION_TYPE_CLIENT) {
1541 session->doing_first = 1;
1542 }
1543 } else {
1544 /* Initial connect worked immediately */
1545 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1546 }
1547 }
1548#endif /* !COAP_DISABLE_TCP */
1549 coap_ticks(&session->last_rx_tx);
1550}
1551#endif /* COAP_CLIENT_SUPPORT */
1552
1553void
1555 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1556 coap_session_connected(session);
1557#if !COAP_DISABLE_TCP
1558 if (COAP_PROTO_RELIABLE(session->proto))
1559 coap_session_send_csm(session);
1560#endif /* !COAP_DISABLE_TCP */
1561}
1562
1563#if COAP_CLIENT_SUPPORT
1564int
1566 int default_port = COAP_DEFAULT_PORT;
1567
1568 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close && !session->session_failed)
1569 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1570
1571 switch (session->proto) {
1572 case COAP_PROTO_UDP:
1573 default_port = COAP_DEFAULT_PORT;
1574 break;
1575 case COAP_PROTO_DTLS:
1576 default_port = COAPS_DEFAULT_PORT;
1577 break;
1578 case COAP_PROTO_TCP:
1579 default_port = COAP_DEFAULT_PORT;
1580 break;
1581 case COAP_PROTO_TLS:
1582 default_port = COAPS_DEFAULT_PORT;
1583 break;
1584 case COAP_PROTO_WS:
1585 default_port = 80;
1586 break;
1587 case COAP_PROTO_WSS:
1588 default_port = 443;
1589 break;
1590 case COAP_PROTO_NONE:
1591 case COAP_PROTO_LAST:
1592 default:
1593 assert(0);
1594 return 0;
1595 }
1596 session->sock.session = session;
1597 coap_log_debug("***%s: trying to reconnect\n", coap_session_str(session));
1598 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1599 session->state = COAP_SESSION_STATE_NONE;
1600 if (!coap_netif_dgrm_connect(session, &session->local_reconnect, &session->addr_info.remote,
1601 default_port)) {
1602 goto error;
1603 }
1605#ifdef WITH_CONTIKI
1606 session->sock.context = session->context;
1607#endif /* WITH_CONTIKI */
1608#if !COAP_DISABLE_TCP
1609 } else if (COAP_PROTO_RELIABLE(session->proto)) {
1610 if (!coap_netif_strm_connect1(session, &session->local_reconnect, &session->addr_info.remote,
1611 default_port)) {
1612 goto error;
1613 }
1614#endif /* !COAP_DISABLE_TCP */
1615 } else {
1616 goto error;
1617 }
1618#ifdef COAP_EPOLL_SUPPORT
1619 coap_epoll_ctl_add(&session->sock,
1620 EPOLLIN |
1621 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1622 EPOLLOUT : 0),
1623 __func__);
1624#endif /* COAP_EPOLL_SUPPORT */
1625
1627 session->sock.flags |= COAP_SOCKET_BOUND;
1628 coap_session_check_connect(session);
1629 return 1;
1630error:
1631 return 0;
1632}
1633
1634void
1636 coap_lg_crcv_t *lg_crcv, *etmp;
1637
1638 if (!session->session_failed)
1639 return;
1640 coap_log_debug("***%s: session re-established\n",
1641 coap_session_str(session));
1642 session->session_failed = 0;
1643 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
1645 coap_send_internal(session, lg_crcv->sent_pdu, NULL);
1646 }
1647}
1648
1651 const coap_address_t *local_if,
1652 const coap_address_t *server,
1653 coap_proto_t proto) {
1654 coap_session_t *session;
1655
1656 coap_lock_lock(return NULL);
1657 session = coap_new_client_session_lkd(ctx, local_if, server, proto);
1659 return session;
1660}
1661
1664 const coap_address_t *local_if,
1665 const coap_address_t *server,
1666 coap_proto_t proto) {
1667 coap_session_t *session;
1668
1670 session = coap_session_create_client(ctx, local_if, server,
1671 proto);
1672 if (session) {
1673 coap_log_debug("***%s: session %p: created outgoing session\n",
1674 coap_session_str(session), (void *)session);
1675 coap_session_check_connect(session);
1676 }
1677 return session;
1678}
1679
1682 const coap_address_t *local_if,
1683 const coap_address_t *server,
1684 coap_proto_t proto, const char *identity,
1685 const uint8_t *key, unsigned key_len) {
1686 coap_session_t *session;
1687
1688 coap_lock_lock(return NULL);
1689 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1691 return session;
1692}
1693
1696 const coap_address_t *local_if,
1697 const coap_address_t *server,
1698 coap_proto_t proto, const char *identity,
1699 const uint8_t *key, unsigned key_len) {
1700 coap_dtls_cpsk_t setup_data;
1701
1703 memset(&setup_data, 0, sizeof(setup_data));
1705
1706 if (identity) {
1707 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1708 setup_data.psk_info.identity.length = strlen(identity);
1709 }
1710
1711 if (key && key_len > 0) {
1712 setup_data.psk_info.key.s = key;
1713 setup_data.psk_info.key.length = key_len;
1714 }
1715
1716 return coap_new_client_session_psk2_lkd(ctx, local_if, server,
1717 proto, &setup_data);
1718}
1719
1720/*
1721 * Check the validity of the SNI to send to the server.
1722 *
1723 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1724 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1725 */
1726static void
1727coap_sanitize_client_sni(char **client_sni) {
1728 char *cp;
1729
1730 if (*client_sni == NULL)
1731 return;
1732
1733 cp = *client_sni;
1734 switch (*cp) {
1735 case '0':
1736 case '1':
1737 case '2':
1738 case '3':
1739 case '4':
1740 case '5':
1741 case '6':
1742 case '7':
1743 case '8':
1744 case '9':
1745 case 'a':
1746 case 'b':
1747 case 'c':
1748 case 'd':
1749 case 'e':
1750 case 'f':
1751 case 'A':
1752 case 'B':
1753 case 'C':
1754 case 'D':
1755 case 'E':
1756 case 'F':
1757 case ':':
1758 break;
1759 case '\000':
1760 /* Empty entry invalid */
1761 *client_sni = NULL;
1762 return;
1763 default:
1764 /* Does not start with a hex digit or : - not literal IP. */
1765 return;
1766 }
1767 /* Check for IPv4 */
1768 while (*cp) {
1769 switch (*cp) {
1770 case '0':
1771 case '1':
1772 case '2':
1773 case '3':
1774 case '4':
1775 case '5':
1776 case '6':
1777 case '7':
1778 case '8':
1779 case '9':
1780 case '.':
1781 break;
1782 default:
1783 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1784 goto check_ipv6;
1785 }
1786 cp++;
1787 }
1788 /* IPv4 address - not allowed. */
1789 *client_sni = NULL;
1790 return;
1791
1792check_ipv6:
1793 /* Check for IPv6 */
1794 cp = *client_sni;
1795 while (*cp) {
1796 switch (*cp) {
1797 case '0':
1798 case '1':
1799 case '2':
1800 case '3':
1801 case '4':
1802 case '5':
1803 case '6':
1804 case '7':
1805 case '8':
1806 case '9':
1807 case 'a':
1808 case 'b':
1809 case 'c':
1810 case 'd':
1811 case 'e':
1812 case 'f':
1813 case 'A':
1814 case 'B':
1815 case 'C':
1816 case 'D':
1817 case 'E':
1818 case 'F':
1819 case ':':
1820 break;
1821 case '%':
1822 /* Start of i/f specification, Previous is IPv6 */
1823 *client_sni = NULL;
1824 return;
1825 default:
1826 /* Not of format xx:xx::xx. */
1827 return;
1828 }
1829 cp++;
1830 }
1831 *client_sni = NULL;
1832 return;
1833}
1834
1837 const coap_address_t *local_if,
1838 const coap_address_t *server,
1839 coap_proto_t proto,
1840 coap_dtls_cpsk_t *setup_data) {
1841 coap_session_t *session;
1842
1843 coap_lock_lock(return NULL);
1844 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, setup_data);
1846 return session;
1847}
1848
1851 const coap_address_t *local_if,
1852 const coap_address_t *server,
1853 coap_proto_t proto,
1854 coap_dtls_cpsk_t *setup_data) {
1855 coap_session_t *session;
1856
1858 session = coap_session_create_client(ctx, local_if, server, proto);
1859
1860 if (!session || !setup_data)
1861 return NULL;
1862
1863 session->cpsk_setup_data = *setup_data;
1864 if (setup_data->psk_info.identity.s) {
1865 session->psk_identity =
1867 setup_data->psk_info.identity.length);
1868 if (!session->psk_identity) {
1869 coap_log_warn("Cannot store session Identity (PSK)\n");
1870 coap_session_release_lkd(session);
1871 return NULL;
1872 }
1874 coap_log_warn("Identity (PSK) not defined\n");
1875 coap_session_release_lkd(session);
1876 return NULL;
1877 }
1878
1879 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1880 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1881 setup_data->psk_info.key.length);
1882 if (!session->psk_key) {
1883 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1884 coap_session_release_lkd(session);
1885 return NULL;
1886 }
1888 coap_log_warn("Pre-shared key (PSK) not defined\n");
1889 coap_session_release_lkd(session);
1890 return NULL;
1891 }
1892
1893 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
1894
1896 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
1897 coap_session_release_lkd(session);
1898 return NULL;
1899 }
1900 }
1901 coap_log_debug("***%s: new outgoing session\n",
1902 coap_session_str(session));
1903 coap_session_check_connect(session);
1904 return session;
1905}
1906#endif /* COAP_CLIENT_SUPPORT */
1907
1908int
1910 const coap_bin_const_t *psk_hint
1911 ) {
1912 /* We may be refreshing the hint with the same hint */
1913 coap_bin_const_t *old_psk_hint = session->psk_hint;
1914
1915 if (psk_hint && psk_hint->s) {
1916 if (session->psk_hint) {
1917 if (coap_binary_equal(session->psk_hint, psk_hint))
1918 return 1;
1919 }
1920 session->psk_hint = coap_new_bin_const(psk_hint->s,
1921 psk_hint->length);
1922 if (!session->psk_hint) {
1923 coap_log_err("No memory to store identity hint (PSK)\n");
1924 if (old_psk_hint)
1925 coap_delete_bin_const(old_psk_hint);
1926 return 0;
1927 }
1928 } else {
1929 session->psk_hint = NULL;
1930 }
1931 if (old_psk_hint)
1932 coap_delete_bin_const(old_psk_hint);
1933
1934 return 1;
1935}
1936
1937int
1939 const coap_bin_const_t *psk_key
1940 ) {
1941 /* We may be refreshing the key with the same key */
1942 coap_bin_const_t *old_psk_key = session->psk_key;
1943
1944 if (psk_key && psk_key->s) {
1945 if (session->psk_key) {
1946 if (coap_binary_equal(session->psk_key, psk_key))
1947 return 1;
1948 }
1949 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1950 if (!session->psk_key) {
1951 coap_log_err("No memory to store pre-shared key (PSK)\n");
1952 if (old_psk_key)
1953 coap_delete_bin_const(old_psk_key);
1954 return 0;
1955 }
1956 } else {
1957 session->psk_key = NULL;
1958 }
1959 if (old_psk_key)
1960 coap_delete_bin_const(old_psk_key);
1961
1962 return 1;
1963}
1964
1965int
1967 const coap_bin_const_t *psk_identity
1968 ) {
1969 /* We may be refreshing the identity with the same identity */
1970 coap_bin_const_t *old_psk_identity = session->psk_identity;
1971
1972 if (psk_identity && psk_identity->s) {
1973 if (session->psk_identity) {
1974 if (coap_binary_equal(session->psk_identity, psk_identity))
1975 return 1;
1976 }
1977 session->psk_identity = coap_new_bin_const(psk_identity->s,
1978 psk_identity->length);
1979 if (!session->psk_identity) {
1980 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1981 if (old_psk_identity)
1982 coap_delete_bin_const(old_psk_identity);
1983 return 0;
1984 }
1985 } else {
1986 session->psk_identity = NULL;
1987 }
1988 if (old_psk_identity)
1989 coap_delete_bin_const(old_psk_identity);
1990
1991 return 1;
1992}
1993
1994#if COAP_SERVER_SUPPORT
1995const coap_bin_const_t *
1997 if (session)
1998 return session->psk_hint;
1999 return NULL;
2000}
2001#endif /* COAP_SERVER_SUPPORT */
2002
2003const coap_bin_const_t *
2005 const coap_bin_const_t *psk_identity = NULL;
2006 if (session) {
2007 psk_identity = session->psk_identity;
2008 if (psk_identity == NULL) {
2009 psk_identity = &session->cpsk_setup_data.psk_info.identity;
2010 }
2011 }
2012 return psk_identity;
2013}
2014
2015const coap_bin_const_t *
2017 if (session)
2018 return session->psk_key;
2019 return NULL;
2020}
2021
2022#if COAP_CLIENT_SUPPORT
2025 const coap_address_t *local_if,
2026 const coap_address_t *server,
2027 coap_proto_t proto,
2028 coap_dtls_pki_t *setup_data) {
2029 coap_session_t *session;
2030
2031 coap_lock_lock(return NULL);
2032 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, setup_data);
2034 return session;
2035}
2036
2039 const coap_address_t *local_if,
2040 const coap_address_t *server,
2041 coap_proto_t proto,
2042 coap_dtls_pki_t *setup_data) {
2043 coap_session_t *session;
2044 coap_dtls_pki_t l_setup_data;
2045
2046 if (!setup_data)
2047 return NULL;
2048 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
2049 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
2050 return NULL;
2051 }
2052
2054 l_setup_data = *setup_data;
2055 coap_sanitize_client_sni(&l_setup_data.client_sni);
2056
2057 session = coap_session_create_client(ctx, local_if, server, proto);
2058
2059 if (!session) {
2060 return NULL;
2061 }
2062
2064 /* we know that setup_data is not NULL */
2065 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
2066 coap_session_release_lkd(session);
2067 return NULL;
2068 }
2069 }
2070 coap_log_debug("***%s: new outgoing session\n",
2071 coap_session_str(session));
2072 coap_session_check_connect(session);
2073 return session;
2074}
2075#endif /* ! COAP_CLIENT_SUPPORT */
2076
2077#if COAP_SERVER_SUPPORT
2078#if !COAP_DISABLE_TCP
2081 coap_session_t *session;
2082 coap_tick_t now;
2083
2085 NULL, NULL, NULL, 0, ctx, ep);
2086 if (!session)
2087 goto error;
2088
2089 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
2090 if (!coap_netif_strm_accept(ep, session, extra))
2091 goto error;
2092
2093 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
2094
2095#ifdef COAP_EPOLL_SUPPORT
2096 session->sock.session = session;
2097 coap_epoll_ctl_add(&session->sock,
2098 EPOLLIN,
2099 __func__);
2100#endif /* COAP_EPOLL_SUPPORT */
2101 SESSIONS_ADD(ep->sessions, session);
2102 if (session) {
2103 coap_ticks(&now);
2104 session->last_rx_tx = now;
2105 coap_log_debug("***%s: session %p: new incoming session\n",
2106 coap_session_str(session), (void *)session);
2110 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
2111 }
2112 return session;
2113
2114error:
2115 /*
2116 * Need to add in the session as coap_session_release_lkd()
2117 * will call SESSIONS_DELETE in coap_session_free().
2118 */
2119 if (session) {
2120 SESSIONS_ADD(ep->sessions, session);
2121 coap_session_free(session);
2122 }
2123 return NULL;
2124}
2125#endif /* !COAP_DISABLE_TCP */
2126#endif /* COAP_SERVER_SUPPORT */
2127
2128void
2130 const uint8_t *data) {
2131 session->tx_token = coap_decode_var_bytes8(data, len);
2132 /*
2133 * Decrement as when first used by coap_session_new_token() it will
2134 * get incremented
2135 */
2136 session->tx_token--;
2137}
2138
2139void
2141 uint8_t *data) {
2142 *len = coap_encode_var_safe8(data,
2143 sizeof(session->tx_token), ++session->tx_token);
2144}
2145
2146COAP_API uint16_t
2148 uint16_t mid;
2149
2150 coap_lock_lock(return 0);
2151 mid = coap_new_message_id_lkd(session);
2153 return mid;
2154}
2155
2156uint16_t
2159 if (COAP_PROTO_NOT_RELIABLE(session->proto))
2160 return ++session->tx_mid;
2161 /* TCP/TLS have no notion of mid */
2162 return 0;
2163}
2164
2165const coap_address_t *
2167 if (session)
2168 return &session->addr_info.remote;
2169 return NULL;
2170}
2171
2172const coap_address_t *
2174 if (session)
2175 return &session->addr_info.local;
2176 return NULL;
2177}
2178
2179const coap_address_t *
2181#if COAP_CLIENT_SUPPORT
2182 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
2183 session->sock.flags & COAP_SOCKET_MULTICAST)
2184 return &session->sock.mcast_addr;
2185#else /* ! COAP_CLIENT_SUPPORT */
2186 (void)session;
2187#endif /* ! COAP_CLIENT_SUPPORT */
2188 return NULL;
2189}
2190
2193 if (session)
2194 return session->context;
2195 return NULL;
2196}
2197
2200 if (session)
2201 return session->proto;
2202 return 0;
2203}
2204
2207 if (session)
2208 return session->type;
2209 return 0;
2210}
2211
2212COAP_API int
2214 int ret;
2215
2216 coap_lock_lock(return 0);
2217 ret = coap_session_set_type_client_lkd(session, 1);
2219 return ret;
2220}
2221
2222int
2224#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2225 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
2227 session->type = COAP_SESSION_TYPE_CLIENT;
2228 if (session->endpoint) {
2229 SESSIONS_DELETE(session->endpoint->sessions, session);
2230 SESSIONS_ADD(session->context->sessions, session);
2231 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2232 session->sock = session->endpoint->sock;
2233 session->sock.flags |= COAP_SOCKET_SLAVE;
2234 session->sock.flags &= ~COAP_SOCKET_CAN_READ;
2235 session->sock.flags &= ~COAP_SOCKET_WANT_READ;
2236 }
2237 session->endpoint = NULL;
2238 }
2239 if (report_changed) {
2240 coap_log_debug("***%s: session now is type Client\n",
2241 coap_session_str(session));
2242 }
2243 return 1;
2244 }
2245#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2246 (void)session;
2247 (void)report_changed;
2248#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2249 return 0;
2250}
2251
2252COAP_API int
2254 int ret;
2255
2256 coap_lock_lock(return 0);
2257 ret = coap_session_set_type_server_lkd(session);
2259 return ret;
2260}
2261
2262int
2264#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2265 if (session && session->type == COAP_SESSION_TYPE_CLIENT) {
2266 coap_endpoint_t *ep;
2267 coap_mid_t mid;
2268 coap_pdu_t *ping = NULL;
2269
2270 LL_FOREACH(session->context->endpoint, ep) {
2271 if (session->proto == ep->proto)
2272 break;
2273 }
2274 if (!ep) {
2275 /* Need a dummy endpoint to 'hang' off as this is now a server */
2276 ep = coap_malloc_endpoint();
2277 if (!ep) {
2278 coap_log_warn("coap_new_endpoint: malloc");
2279 return 0;
2280 }
2281
2282 memset(ep, 0, sizeof(coap_endpoint_t));
2283 ep->context = session->context;
2284 ep->proto = session->proto;
2285 ep->sock.endpoint = ep;
2286 memcpy(&ep->sock.lfunc, coap_layers_coap[session->proto], sizeof(ep->sock.lfunc));
2287
2289 LL_PREPEND(session->context->endpoint, ep);
2290 }
2291 /* Need to use coap_send_lkd() here to get traffic flowing */
2292 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2294 }
2295#if !COAP_DISABLE_TCP
2296 else {
2298 }
2299#endif /* !COAP_DISABLE_TCP */
2300 if (!ping) {
2301 session->type = COAP_SESSION_TYPE_SERVER;
2302 return 0;
2303 }
2304 mid = coap_send_lkd(session, ping);
2305 if (mid != COAP_INVALID_MID) {
2306 coap_tick_t now;
2307
2308 coap_ticks(&now);
2309 session->last_ping_mid = mid;
2310 session->last_rx_tx = now;
2311 session->last_ping = now;
2312 }
2313 if (session->context) {
2314 if (session->context->sessions) {
2315 SESSIONS_DELETE(session->context->sessions, session);
2316 }
2317 }
2318 SESSIONS_ADD(ep->sessions, session);
2319 session->endpoint = ep;
2320 session->type = COAP_SESSION_TYPE_SERVER;
2321 coap_session_release_lkd(session);
2322 coap_log_debug("***%s: session now is type Server\n",
2323 coap_session_str(session));
2324 return 1;
2325 }
2326#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2327 (void)session;
2328#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2329 return 0;
2330}
2331
2334 if (session)
2335 return session->state;
2336 return 0;
2337}
2338
2341#if COAP_SERVER_SUPPORT
2342 if (session)
2343 return session->endpoint;
2344#else /* ! COAP_SERVER_SUPPORT */
2345 (void)session;
2346#endif /* ! COAP_SERVER_SUPPORT */
2347 return 0;
2348}
2349
2350int
2352 if (session)
2353 return session->ifindex;
2354 return -1;
2355}
2356
2357void *
2359 coap_tls_library_t *tls_lib) {
2360 if (session)
2361 return coap_dtls_get_tls(session, tls_lib);
2362 return NULL;
2363}
2364
2365static const char *
2367 switch (proto) {
2368 case COAP_PROTO_UDP:
2369 return "UDP ";
2370 case COAP_PROTO_DTLS:
2371 return "DTLS";
2372 case COAP_PROTO_TCP:
2373 return "TCP ";
2374 case COAP_PROTO_TLS:
2375 return "TLS ";
2376 case COAP_PROTO_WS:
2377 return "WS ";
2378 case COAP_PROTO_WSS:
2379 return "WSS ";
2380 case COAP_PROTO_NONE:
2381 case COAP_PROTO_LAST:
2382 default:
2383 return "????" ;
2384 break;
2385 }
2386 return NULL;
2387}
2388
2389#if COAP_SERVER_SUPPORT
2391coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2392 coap_endpoint_t *endpoint;
2393
2394 coap_lock_lock(return NULL);
2395 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2397 return endpoint;
2398}
2399
2401coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2402 coap_proto_t proto) {
2403 coap_endpoint_t *ep = NULL;
2404
2405 if (!context || !listen_addr) {
2406 coap_log_debug("coap_new_endpoint: Both context and listen_addr need to be defined\n");
2407 return NULL;
2408 }
2409
2411
2412 switch (proto) {
2413 case COAP_PROTO_UDP:
2414 break;
2415 case COAP_PROTO_DTLS:
2416 if (!coap_dtls_is_supported()) {
2417 coap_log_warn("coap_new_endpoint: DTLS not supported\n");
2418 return NULL;
2419 }
2420 break;
2421 case COAP_PROTO_TLS:
2422 if (!coap_tls_is_supported()) {
2423 coap_log_warn("coap_new_endpoint: TLS not supported\n");
2424 return NULL;
2425 }
2426 break;
2427 case COAP_PROTO_TCP:
2428 if (!coap_tcp_is_supported()) {
2429 coap_log_warn("coap_new_endpoint: TCP not supported\n");
2430 return NULL;
2431 }
2432 break;
2433 case COAP_PROTO_WS:
2434 if (!coap_ws_is_supported()) {
2435 coap_log_warn("coap_new_endpoint: WS not supported\n");
2436 return NULL;
2437 }
2438 break;
2439 case COAP_PROTO_WSS:
2440 if (!coap_wss_is_supported()) {
2441 coap_log_warn("coap_new_endpoint: WSS not supported\n");
2442 return NULL;
2443 }
2444 break;
2445 case COAP_PROTO_NONE:
2446 case COAP_PROTO_LAST:
2447 default:
2448 coap_log_crit("coap_new_endpoint: Unsupported protocol %d\n", proto);
2449 return NULL;
2450 }
2451
2452 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2453 proto == COAP_PROTO_WSS) {
2455 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2456 "coap_context_set_pki() not called\n");
2457 return NULL;
2458 }
2459 }
2460 ep = coap_malloc_endpoint();
2461 if (!ep) {
2462 coap_log_warn("coap_new_endpoint: malloc");
2463 return NULL;
2464 }
2465
2466 memset(ep, 0, sizeof(coap_endpoint_t));
2467 ep->context = context;
2468 ep->proto = proto;
2469 ep->sock.endpoint = ep;
2470 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2471
2472 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2473 if (!coap_netif_dgrm_listen(ep, listen_addr))
2474 goto error;
2475#ifdef WITH_CONTIKI
2476 ep->sock.context = context;
2477#endif /* WITH_CONTIKI */
2478#if !COAP_DISABLE_TCP
2479 } else if (COAP_PROTO_RELIABLE(proto)) {
2480 if (!coap_netif_strm_listen(ep, listen_addr))
2481 goto error;
2482#endif /* !COAP_DISABLE_TCP */
2483 } else {
2484 coap_log_crit("coap_new_endpoint: Protocol type not supported %d\n", proto);
2485 goto error;
2486 }
2487
2489#ifndef INET6_ADDRSTRLEN
2490#define INET6_ADDRSTRLEN 40
2491#endif
2492 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2493
2494 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2495 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2496 addr_str);
2497 }
2498 }
2499
2501
2502#ifdef COAP_EPOLL_SUPPORT
2503 ep->sock.endpoint = ep;
2505 EPOLLIN,
2506 __func__);
2507#endif /* COAP_EPOLL_SUPPORT */
2508
2509 LL_PREPEND(context->endpoint, ep);
2510 return ep;
2511
2512error:
2514 return NULL;
2515}
2516
2517void
2519 ep->default_mtu = (uint16_t)mtu;
2520}
2521
2522COAP_API void
2524 if (ep) {
2525 coap_context_t *context = ep->context;
2526 if (context) {
2527 coap_lock_lock(return);
2528 }
2530 if (context) {
2532 }
2533 }
2534}
2535
2536void
2538 if (ep) {
2539 coap_session_t *session, *rtmp;
2540
2541 if (ep->context) {
2542 /* If fully allocated and inserted */
2544 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2545 assert(session->ref == 0);
2546 if (session->ref == 0) {
2548 coap_session_free(session);
2549 }
2550 }
2551 if (coap_netif_available_ep(ep)) {
2552 /*
2553 * ep->sock.endpoint is set in coap_new_endpoint().
2554 * ep->sock.session is never set.
2555 *
2556 * session->sock.session is set for both clients and servers (when a
2557 * new session is accepted), but does not affect the endpoint.
2558 *
2559 * So, it is safe to call coap_netif_close_ep() after all the sessions
2560 * have been freed above as we are only working with the endpoint sock.
2561 */
2562#ifdef COAP_EPOLL_SUPPORT
2563 assert(ep->sock.session == NULL);
2564#endif /* COAP_EPOLL_SUPPORT */
2566 }
2567
2568 if (ep->context->endpoint) {
2569 LL_DELETE(ep->context->endpoint, ep);
2570 }
2571 }
2572
2573 if (ep->app_cb) {
2574 ep->app_cb(ep->app_data);
2575 }
2576
2578 }
2579}
2580
2581void *
2583 assert(endpoint);
2584 return endpoint->app_data;
2585}
2586
2587COAP_API void *
2588coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *app_data,
2590 void *old_data;
2591
2592 coap_lock_lock(return NULL);
2593 old_data = coap_endpoint_set_app_data_lkd(endpoint, app_data, callback);
2595 return old_data;
2596}
2597
2598void *
2599coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *app_data,
2601 void *old_data = endpoint->app_data;
2602
2603 endpoint->app_data = app_data;
2604 endpoint->app_cb = app_data ? callback : NULL;
2605 return old_data;
2606}
2607
2608#endif /* COAP_SERVER_SUPPORT */
2609
2612 const coap_address_t *remote_addr,
2613 int ifindex) {
2614 coap_session_t *s, *rtmp;
2615#if COAP_CLIENT_SUPPORT
2616 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2617 if (s->ifindex == ifindex) {
2618 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2619 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2620 return s;
2621 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2622 return s;
2623 }
2624 }
2625#endif /* COAP_CLIENT_SUPPORT */
2626#if COAP_SERVER_SUPPORT
2627 coap_endpoint_t *ep;
2628
2629 LL_FOREACH(ctx->endpoint, ep) {
2630 SESSIONS_ITER(ep->sessions, s, rtmp) {
2631 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2632 remote_addr))
2633 return s;
2634 }
2635 }
2636#endif /* COAP_SERVER_SUPPORT */
2637 return NULL;
2638}
2639
2640#ifndef INET6_ADDRSTRLEN
2641#define INET6_ADDRSTRLEN 46
2642#endif
2643const char *
2645 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2646 char *p = szSession, *end = szSession + sizeof(szSession);
2647 if (coap_print_addr(&session->addr_info.local,
2648 (unsigned char *)p, end - p) > 0)
2649 p += strlen(p);
2650 if (p + 6 < end) {
2651 strcpy(p, " <-> ");
2652 p += 5;
2653 }
2654 if (p + 1 < end) {
2655 if (coap_print_addr(&session->addr_info.remote,
2656 (unsigned char *)p, end - p) > 0)
2657 p += strlen(p);
2658 }
2659 if (session->ifindex > 0 && p + 1 < end)
2660 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2661 if (p + 6 < end) {
2662 strcpy(p, " ");
2663 p++;
2664 strcpy(p, coap_proto_name(session->proto));
2665 }
2666
2667 return szSession;
2668}
2669
2670#if COAP_SERVER_SUPPORT
2671const char *
2672coap_endpoint_str(const coap_endpoint_t *endpoint) {
2673 static char szEndpoint[128];
2674 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2675 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2676 p += strlen(p);
2677 if (p + 6 < end) {
2678 if (endpoint->proto == COAP_PROTO_UDP) {
2679 strcpy(p, " UDP");
2680 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2681 strcpy(p, " DTLS");
2682 } else {
2683 strcpy(p, " NONE");
2684 }
2685 }
2686
2687 return szEndpoint;
2688}
2689#endif /* COAP_SERVER_SUPPORT */
2690#if COAP_CLIENT_SUPPORT
2691void
2693 session->no_observe_cancel = 1;
2694}
2695#endif /* COAP_CLIENT_SUPPORT */
2696#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
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:60
@ COAP_STRING
Definition coap_mem.h:48
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_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:1478
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)
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:152
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:167
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:4914
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:230
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:2945
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:1346
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:1230
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:1847
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1256
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition coap_net.c:259
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:3049
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:3088
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
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:59
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_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_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_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.
Definition coap_pdu.h:317
#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:323
@ COAP_PROTO_DTLS
Definition coap_pdu.h:320
@ COAP_PROTO_UDP
Definition coap_pdu.h:319
@ COAP_PROTO_NONE
Definition coap_pdu.h:318
@ COAP_PROTO_TLS
Definition coap_pdu.h:322
@ COAP_PROTO_WSS
Definition coap_pdu.h:324
@ COAP_PROTO_TCP
Definition coap_pdu.h:321
@ COAP_PROTO_LAST
Definition coap_pdu.h:325
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:370
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:371
@ COAP_EMPTY_CODE
Definition coap_pdu.h:332
@ COAP_MESSAGE_NON
Definition coap_pdu.h: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:1108
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.
Definition coap_tcp.c:29
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.
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
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.
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