libcoap 4.3.4-develop-c081bb6
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-2024 Jon Shallow <supjps-libcoap@jpshallow.com>
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 *
8 * This file is part of the CoAP library libcoap. Please see
9 * README for terms of use.
10 */
11
17#include "coap3/coap_internal.h"
18
19#ifndef COAP_SESSION_C_
20#define COAP_SESSION_C_
21
22#include <stdio.h>
23
24#ifdef COAP_EPOLL_SUPPORT
25#include <sys/epoll.h>
26#include <sys/timerfd.h>
27#endif /* COAP_EPOLL_SUPPORT */
28
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(&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
350 ++session->ref;
351 return session;
352}
353
354void
356 if (session) {
358#ifndef __COVERITY__
359 assert(session->ref > 0);
360 if (session->ref > 0)
361 --session->ref;
362 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
363 coap_session_free(session);
364#else /* __COVERITY__ */
365 /* Coverity scan is fooled by the reference counter leading to
366 * false positives for USE_AFTER_FREE. */
367 --session->ref;
368 __coverity_negative_sink__(session->ref);
369 /* Indicate that resources are released properly. */
370 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
371 __coverity_free__(session);
372 }
373#endif /* __COVERITY__ */
374 }
375}
376
377void
378coap_session_set_app_data(coap_session_t *session, void *app_data) {
379 assert(session);
380 session->app = app_data;
381}
382
383void *
385 assert(session);
386 return session->app;
387}
388
389static coap_session_t *
391 const coap_addr_hash_t *addr_hash,
392 const coap_address_t *local_addr,
393 const coap_address_t *remote_addr, int ifindex,
394 coap_context_t *context, coap_endpoint_t *endpoint) {
396 sizeof(coap_session_t));
397#if ! COAP_SERVER_SUPPORT
398 (void)endpoint;
399#endif /* ! COAP_SERVER_SUPPORT */
400 if (!session)
401 return NULL;
402 memset(session, 0, sizeof(*session));
403 session->proto = proto;
404 session->type = type;
405 if (addr_hash)
406 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
407 else
408 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
409 if (local_addr)
410 coap_address_copy(&session->addr_info.local, local_addr);
411 else
413 if (remote_addr)
414 coap_address_copy(&session->addr_info.remote, remote_addr);
415 else
417 session->ifindex = ifindex;
418 session->context = context;
419#if COAP_SERVER_SUPPORT
420 session->endpoint = endpoint;
421 if (endpoint)
422 session->mtu = endpoint->default_mtu;
423 else
424#endif /* COAP_SERVER_SUPPORT */
425 session->mtu = COAP_DEFAULT_MTU;
426 session->block_mode = context->block_mode;
427 if (proto == COAP_PROTO_DTLS) {
428 session->tls_overhead = 29;
429 if (session->tls_overhead >= session->mtu) {
430 session->tls_overhead = session->mtu;
431 coap_log_err("DTLS overhead exceeds MTU\n");
432 }
433 }
437 session->nstart = COAP_DEFAULT_NSTART;
440#if COAP_Q_BLOCK_SUPPORT
441 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
442 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
443 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
444 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
445 coap_session_fix_non_probing_wait_base(session);
446 coap_session_fix_non_partial_timeout(session);
447#endif /* COAP_Q_BLOCK_SUPPORT */
448 session->dtls_event = -1;
453 session->max_token_size = context->max_token_size; /* RFC8974 */
454 if (session->type != COAP_SESSION_TYPE_CLIENT)
456
457 /* Randomly initialize */
458 /* TCP/TLS have no notion of mid */
459 if (COAP_PROTO_NOT_RELIABLE(session->proto))
460 coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
461 coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
462
463 return session;
464}
465
466void
468 coap_queue_t *q, *tmp;
469 coap_lg_xmit_t *lq, *ltmp;
470
471#if COAP_CLIENT_SUPPORT
472 coap_lg_crcv_t *lg_crcv, *etmp;
473
474 /* Need to do this before (D)TLS and socket is closed down */
475 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
476 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
477 /* Need to close down observe */
478 if (coap_cancel_observe(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
479 /* Need to delete node we set up for NON */
480 coap_queue_t *queue = session->context->sendqueue;
481
482 while (queue) {
483 if (queue->session == session) {
484 coap_delete_node(queue);
485 break;
486 }
487 queue = queue->next;
488 }
489 }
490 }
491 LL_DELETE(session->lg_crcv, lg_crcv);
492 coap_block_delete_lg_crcv(session, lg_crcv);
493 }
494#endif /* COAP_CLIENT_SUPPORT */
495
496 if (session->partial_pdu)
498 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
499 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
500 if (session->psk_identity)
502 if (session->psk_key)
504 if (session->psk_hint)
506
507#if COAP_SERVER_SUPPORT
508 coap_cache_entry_t *cp, *ctmp;
509 HASH_ITER(hh, session->context->cache, cp, ctmp) {
510 /* cp->session is NULL if not session based */
511 if (cp->session == session) {
512 coap_delete_cache_entry(session->context, cp);
513 }
514 }
515#endif /* COAP_SERVER_SUPPORT */
516 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
517 if (q->pdu->type==COAP_MESSAGE_CON && session->context->nack_handler) {
518 coap_check_update_token(session, q->pdu);
520 session->context->nack_handler(session, q->pdu,
521 session->proto == COAP_PROTO_DTLS ?
523 q->id));
524 }
526 }
527 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
528 LL_DELETE(session->lg_xmit, lq);
529 coap_block_delete_lg_xmit(session, lq);
530 }
531#if COAP_SERVER_SUPPORT
532 coap_lg_srcv_t *sq, *stmp;
533
534 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
535 LL_DELETE(session->lg_srcv, sq);
536 coap_block_delete_lg_srcv(session, sq);
537 }
538#endif /* COAP_SERVER_SUPPORT */
539#if COAP_OSCORE_SUPPORT
541#endif /* COAP_OSCORE_SUPPORT */
542#if COAP_WS_SUPPORT
543 coap_free_type(COAP_STRING, session->ws);
544 coap_delete_str_const(session->ws_host);
545#endif /* COAP_WS_SUPPORT */
546}
547
548void
550 if (!session)
551 return;
553 assert(session->ref == 0);
554 if (session->ref)
555 return;
556 /* Make sure nothing gets deleted under our feet */
557 coap_session_reference(session);
558 coap_session_mfree(session);
559#if COAP_SERVER_SUPPORT
560 if (session->endpoint) {
561 if (session->endpoint->sessions)
562 SESSIONS_DELETE(session->endpoint->sessions, session);
563 } else
564#endif /* COAP_SERVER_SUPPORT */
565#if COAP_CLIENT_SUPPORT
566 if (session->context) {
567 if (session->context->sessions)
568 SESSIONS_DELETE(session->context->sessions, session);
569 }
570#endif /* COAP_CLIENT_SUPPORT */
572 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
573 (void *)session);
574
575 assert(session->ref == 1);
577}
578
579static size_t
581 size_t max_with_header) {
582#if COAP_DISABLE_TCP
583 (void)session;
584 return max_with_header > 4 ? max_with_header - 4 : 0;
585#else /* !COAP_DISABLE_TCP */
586 if (COAP_PROTO_NOT_RELIABLE(session->proto))
587 return max_with_header > 4 ? max_with_header - 4 : 0;
588 /* we must assume there is no token to be on the safe side */
589 if (max_with_header <= 2)
590 return 0;
591 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
592 return max_with_header - 2;
593 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
594 return max_with_header - 3;
595 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
596 return max_with_header - 4;
597 else
598 return max_with_header - 6;
599#endif /* !COAP_DISABLE_TCP */
600}
601
602size_t
604 if (session->csm_rcv_mtu)
606 (size_t)(session->csm_rcv_mtu));
607
609 (size_t)(session->mtu - session->tls_overhead));
610}
611
612size_t
614 size_t max_with_header;
615
617#if COAP_CLIENT_SUPPORT
618 /*
619 * Delay if session->doing_first is set.
620 * E.g. Reliable and CSM not in yet for checking block support
621 */
622 coap_session_t *session_rw;
623
624 /*
625 * Need to do this to not get a compiler warning about const parameters
626 * but need to maintain source code backward compatibility
627 */
628 memcpy(&session_rw, &session, sizeof(session_rw));
629 if (coap_client_delay_first(session_rw) == 0) {
630 coap_log_debug("coap_client_delay_first: timeout\n");
631 /* Have to go with the defaults */
632 }
633#endif /* COAP_CLIENT_SUPPORT */
634
635 max_with_header = (size_t)(session->mtu - session->tls_overhead);
636
637 return coap_session_max_pdu_size_internal(session, max_with_header);
638}
639
640void
641coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
642#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
643 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
644 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
645#endif
646 if (mtu < 64)
647 mtu = 64;
648 session->mtu = mtu;
649 if (session->tls_overhead >= session->mtu) {
650 session->tls_overhead = session->mtu;
651 coap_log_err("DTLS overhead exceeds MTU\n");
652 }
653}
654
655ssize_t
657 coap_queue_t *node) {
658 if (node) {
659 coap_queue_t *removed = NULL;
660 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
661 assert(removed == node);
663 node->session = NULL;
664 node->t = 0;
665 } else {
666 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
667 coap_queue_t *q = NULL;
668 /* Check same mid is not getting re-used in violation of RFC7252 */
669 LL_FOREACH(session->delayqueue, q) {
670 if (q->id == pdu->mid) {
671 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
672 coap_session_str(session), pdu->mid);
673 return COAP_INVALID_MID;
674 }
675 }
676 }
677 node = coap_new_node();
678 if (node == NULL)
679 return COAP_INVALID_MID;
680 node->id = pdu->mid;
681 node->pdu = pdu;
682 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
683 uint8_t r;
684 coap_prng(&r, sizeof(r));
685 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
686 node->timeout = coap_calc_timeout(session, r);
687 }
688 }
689 LL_APPEND(session->delayqueue, node);
690 coap_log_debug("** %s: mid=0x%04x: delayed\n",
691 coap_session_str(session), node->id);
692 return COAP_PDU_DELAYED;
693}
694
695#if !COAP_DISABLE_TCP
696void
698 coap_pdu_t *pdu;
699 uint8_t buf[4];
700 assert(COAP_PROTO_RELIABLE(session->proto));
701 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
702 session->state = COAP_SESSION_STATE_CSM;
703 session->partial_write = 0;
704 if (session->mtu == 0)
705 session->mtu = COAP_DEFAULT_MTU; /* base value */
707 if (pdu == NULL
709 coap_encode_var_safe(buf, sizeof(buf),
710 session->context->csm_max_message_size), buf) == 0
712 coap_encode_var_safe(buf, sizeof(buf),
713 0), buf) == 0
714 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
717 coap_encode_var_safe(buf, sizeof(buf),
718 session->max_token_size),
719 buf) == 0)
720 || coap_pdu_encode_header(pdu, session->proto) == 0
721 ) {
723 } else {
724 ssize_t bytes_written;
725
726 pdu->session = session;
727 bytes_written = coap_session_send_pdu(session, pdu);
728 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
730 } else {
731 session->csm_rcv_mtu = session->context->csm_max_message_size;
732 if (session->csm_rcv_mtu > COAP_BERT_BASE)
733 session->csm_bert_loc_support = 1;
734 else
735 session->csm_bert_loc_support = 0;
736 }
737 }
738 if (pdu)
739 coap_delete_pdu(pdu);
740}
741#endif /* !COAP_DISABLE_TCP */
742
745 coap_pdu_t *ping = NULL;
746
748 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
749 session->con_active)
750 return COAP_INVALID_MID;
751 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
752 uint16_t mid = coap_new_message_id(session);
753 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
754 }
755#if !COAP_DISABLE_TCP
756 else {
758 }
759#endif /* !COAP_DISABLE_TCP */
760 if (!ping)
761 return COAP_INVALID_MID;
762 return coap_send_internal(session, ping);
763}
764
765void
767 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
768 coap_log_debug("***%s: session connected\n",
769 coap_session_str(session));
770 if (session->state == COAP_SESSION_STATE_CSM) {
772 if (session->doing_first)
773 session->doing_first = 0;
774 }
775 }
776
778 session->partial_write = 0;
779
780 if (session->proto==COAP_PROTO_DTLS) {
781 session->tls_overhead = coap_dtls_get_overhead(session);
782 if (session->tls_overhead >= session->mtu) {
783 session->tls_overhead = session->mtu;
784 coap_log_err("DTLS overhead exceeds MTU\n");
785 }
786 }
787
788 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
789 ssize_t bytes_written;
790 coap_queue_t *q = session->delayqueue;
791 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
792 if (session->con_active >= COAP_NSTART(session))
793 break;
794 session->con_active++;
795 }
796 /* Take entry off the queue */
797 session->delayqueue = q->next;
798 q->next = NULL;
799
800 coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
801 coap_session_str(session), (int)q->pdu->mid);
802 bytes_written = coap_session_send_pdu(session, q->pdu);
803 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
804 if (coap_wait_ack(session->context, session, q) >= 0)
805 q = NULL;
806 }
807 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
808 if (q)
810 if (bytes_written < 0)
811 break;
812 } else {
813 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
814 q->next = session->delayqueue;
815 session->delayqueue = q;
816 if (bytes_written > 0)
817 session->partial_write = (size_t)bytes_written;
818 break;
819 } else {
821 }
822 }
823 }
824}
825
826#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
827static const char *
829 switch (reason) {
831 return "COAP_NACK_TOO_MANY_RETRIES";
833 return "COAP_NACK_NOT_DELIVERABLE";
834 case COAP_NACK_RST:
835 return "COAP_NACK_RST";
837 return "COAP_NACK_TLS_FAILED";
839 return "COAP_NACK_ICMP_ISSUE";
841 return "COAP_NACK_BAD_RESPONSE";
843 return "COAP_NACK_TLS_LAYER_FAILED";
845 return "COAP_NACK_WS_LAYER_FAILED";
847 return "COAP_NACK_WS_FAILED";
848 default:
849 return "???";
850 }
851}
852#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
853
854void
856#if !COAP_DISABLE_TCP
857 coap_session_state_t state = session->state;
858#endif /* !COAP_DISABLE_TCP */
859 coap_lg_xmit_t *lq, *ltmp;
860#if COAP_SERVER_SUPPORT
861 coap_lg_srcv_t *sq, *stmp;
862#endif /* COAP_SERVER_SUPPORT */
863#if COAP_CLIENT_SUPPORT
864 coap_lg_crcv_t *cq, *etmp;
865#endif /* COAP_CLIENT_SUPPORT */
866
868 if (reason == COAP_NACK_ICMP_ISSUE) {
869 if (session->context->nack_handler) {
870 int sent_nack = 0;
871 coap_queue_t *q = session->context->sendqueue;
872 while (q) {
873 if (q->session == session) {
874 /* Take the first one */
876
877 coap_check_update_token(session, q->pdu);
879 session->context->nack_handler(session, q->pdu, reason, q->id));
880 coap_update_token(q->pdu, token.length, token.s);
881 sent_nack = 1;
882 break;
883 }
884 q = q->next;
885 }
886#if COAP_CLIENT_SUPPORT
887 if (!sent_nack && session->lg_crcv) {
888 /* Take the first one */
890 session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
891 session->lg_crcv->pdu.mid));
892 sent_nack = 1;
893 }
894#endif /* COAP_CLIENT_SUPPORT */
895 if (!sent_nack) {
896 /* Unable to determine which request ICMP issue was for */
898 session->context->nack_handler(session, NULL, reason, 0));
899 }
900 }
901 coap_log_debug("***%s: session issue (%s)\n",
902 coap_session_str(session), coap_nack_name(reason));
903 return;
904 }
905 coap_log_debug("***%s: session disconnected (%s)\n",
906 coap_session_str(session), coap_nack_name(reason));
907#if COAP_SERVER_SUPPORT
908 coap_delete_observers(session->context, session);
909#endif /* COAP_SERVER_SUPPORT */
910
911 if (session->proto == COAP_PROTO_UDP)
913 else
915
916 session->con_active = 0;
917
918 if (session->partial_pdu) {
920 session->partial_pdu = NULL;
921 }
922 session->partial_read = 0;
923
924 while (session->delayqueue) {
925 coap_queue_t *q = session->delayqueue;
926 session->delayqueue = q->next;
927 q->next = NULL;
928 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
929 coap_session_str(session), q->id);
930 if (q && q->pdu->type == COAP_MESSAGE_CON
931 && session->context->nack_handler) {
932 coap_check_update_token(session, q->pdu);
934 session->context->nack_handler(session, q->pdu, reason, q->id));
935 }
936 if (q)
938 }
939
940#if COAP_CLIENT_SUPPORT
941 /* Need to do this before (D)TLS and socket is closed down */
942 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
943 LL_DELETE(session->lg_crcv, cq);
944 coap_block_delete_lg_crcv(session, cq);
945 }
946#endif /* COAP_CLIENT_SUPPORT */
947 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
948 LL_DELETE(session->lg_xmit, lq);
949 coap_block_delete_lg_xmit(session, lq);
950 }
951#if COAP_SERVER_SUPPORT
952 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
953 LL_DELETE(session->lg_srcv, sq);
954 coap_block_delete_lg_srcv(session, sq);
955 }
956#endif /* COAP_SERVER_SUPPORT */
957 coap_cancel_session_messages(session->context, session, reason);
958
959#if !COAP_DISABLE_TCP
960 if (COAP_PROTO_RELIABLE(session->proto)) {
961 if (coap_netif_available(session)) {
962 coap_handle_event(session->context,
965 }
966 if (state != COAP_SESSION_STATE_NONE) {
967 coap_handle_event(session->context,
970 }
971 if (session->doing_first)
972 session->doing_first = 0;
973 }
974#endif /* !COAP_DISABLE_TCP */
975 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
976}
977
978#if COAP_SERVER_SUPPORT
979static void
980coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
981 const coap_addr_tuple_t *addr_info) {
982 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
983 coap_address_copy(&addr_hash->remote, &addr_info->remote);
984 addr_hash->lport = coap_address_get_port(&addr_info->local);
985 addr_hash->proto = proto;
986}
987
990 const coap_packet_t *packet, coap_tick_t now) {
991 coap_session_t *session;
992 coap_session_t *rtmp;
993 unsigned int num_idle = 0;
994 unsigned int num_hs = 0;
995 coap_session_t *oldest = NULL;
996 coap_session_t *oldest_hs = NULL;
997 coap_addr_hash_t addr_hash;
998
999 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1000 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1001 if (session) {
1002 /* Maybe mcast or unicast IP address which is not in the hash */
1003 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1004 session->ifindex = packet->ifindex;
1005 session->last_rx_tx = now;
1006 return session;
1007 }
1008
1009 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1010 if (session->ref == 0 && session->delayqueue == NULL) {
1011 if (session->type == COAP_SESSION_TYPE_SERVER) {
1012 ++num_idle;
1013 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1014 oldest = session;
1015
1016 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1017 ++num_hs;
1018 /* See if this is a partial (D)TLS session set up
1019 which needs to be cleared down to prevent DOS */
1020 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1021 if (oldest_hs == NULL ||
1022 session->last_rx_tx < oldest_hs->last_rx_tx)
1023 oldest_hs = session;
1024 }
1025 }
1026 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1027 ++num_hs;
1028 /* See if this is a partial (D)TLS session set up for Client Hello
1029 which needs to be cleared down to prevent DOS */
1030 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1031 if (oldest_hs == NULL ||
1032 session->last_rx_tx < oldest_hs->last_rx_tx)
1033 oldest_hs = session;
1034 }
1035 }
1036 }
1037 }
1038
1039 if (endpoint->context->max_idle_sessions > 0 &&
1040 num_idle >= endpoint->context->max_idle_sessions) {
1042 coap_session_free(oldest);
1043 } else if (oldest_hs) {
1044 coap_log_warn("***%s: Incomplete session timed out\n",
1045 coap_session_str(oldest_hs));
1047 coap_session_free(oldest_hs);
1048 }
1049
1050 if (num_hs > (endpoint->context->max_handshake_sessions ?
1051 endpoint->context->max_handshake_sessions :
1053 /* Maxed out on number of sessions in (D)TLS negotiation state */
1054 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1055 "large. New request ignored\n");
1056 return NULL;
1057 }
1058
1059 if (endpoint->proto == COAP_PROTO_DTLS) {
1060 /*
1061 * Need to check that this actually is a Client Hello before wasting
1062 * time allocating and then freeing off session.
1063 */
1064
1065 /*
1066 * Generic header structure of the DTLS record layer.
1067 * typedef struct __attribute__((__packed__)) {
1068 * uint8_t content_type; content type of the included message
1069 * uint16_t version; Protocol version
1070 * uint16_t epoch; counter for cipher state changes
1071 * uint8_t sequence_number[6]; sequence number
1072 * uint16_t length; length of the following fragment
1073 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1074 * } dtls_record_handshake_t;
1075 */
1076#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1077#define DTLS_CT_ALERT 21 /* Content Type Alert */
1078#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1079#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1080#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1081
1082 const uint8_t *payload = (const uint8_t *)packet->payload;
1083 size_t length = packet->length;
1084 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1085 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1086 payload[OFF_CONTENT_TYPE], length,
1087 OFF_HANDSHAKE_TYPE + 1);
1088 return NULL;
1089 }
1090 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1091 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1092 /* only log if not a late alert */
1093 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1094 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1095 payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
1096 return NULL;
1097 }
1098 }
1099
1101 &addr_hash, &packet->addr_info.local,
1102 &packet->addr_info.remote,
1103 packet->ifindex, endpoint->context, endpoint);
1104 if (session) {
1105 session->last_rx_tx = now;
1106 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1107 sizeof(session->sock.lfunc));
1108 if (endpoint->proto == COAP_PROTO_UDP)
1110 else if (endpoint->proto == COAP_PROTO_DTLS) {
1111 session->type = COAP_SESSION_TYPE_HELLO;
1112 }
1113 SESSIONS_ADD(endpoint->sessions, session);
1114 coap_log_debug("***%s: session %p: new incoming session\n",
1115 coap_session_str(session), (void *)session);
1117 }
1118 return session;
1119}
1120
1123 coap_tick_t now) {
1124 if (session) {
1125 session->last_rx_tx = now;
1126 session->type = COAP_SESSION_TYPE_SERVER;
1127 coap_dtls_establish(session);
1128 }
1129 return session;
1130}
1131#endif /* COAP_SERVER_SUPPORT */
1132
1133#if COAP_CLIENT_SUPPORT
1134static coap_session_t *
1135coap_session_create_client(coap_context_t *ctx,
1136 const coap_address_t *local_if,
1137 const coap_address_t *server,
1138 coap_proto_t proto) {
1139 coap_session_t *session = NULL;
1140 int default_port = COAP_DEFAULT_PORT;
1141
1142 assert(server);
1143
1144 switch (proto) {
1145 case COAP_PROTO_UDP:
1146 default_port = COAP_DEFAULT_PORT;
1147 break;
1148 case COAP_PROTO_DTLS:
1149 if (!coap_dtls_is_supported()) {
1150 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1151 return NULL;
1152 }
1153 default_port = COAPS_DEFAULT_PORT;
1154 break;
1155 case COAP_PROTO_TCP:
1156 if (!coap_tcp_is_supported()) {
1157 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1158 return NULL;
1159 }
1160 default_port = COAP_DEFAULT_PORT;
1161 break;
1162 case COAP_PROTO_TLS:
1163 if (!coap_tls_is_supported()) {
1164 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1165 return NULL;
1166 }
1167 default_port = COAPS_DEFAULT_PORT;
1168 break;
1169 case COAP_PROTO_WS:
1170 if (!coap_ws_is_supported()) {
1171 coap_log_crit("coap_new_client_session*: WS not supported\n");
1172 return NULL;
1173 }
1174 default_port = 80;
1175 break;
1176 case COAP_PROTO_WSS:
1177 if (!coap_wss_is_supported()) {
1178 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1179 return NULL;
1180 }
1181 default_port = 443;
1182 break;
1183 case COAP_PROTO_NONE:
1184 case COAP_PROTO_LAST:
1185 default:
1186 assert(0);
1187 return NULL;
1188 }
1189 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1190 local_if, server, 0, ctx, NULL);
1191 if (!session)
1192 goto error;
1193
1194 coap_session_reference(session);
1195 session->sock.session = session;
1196 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1197 sizeof(session->sock.lfunc));
1198
1199 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1200 coap_session_t *s, *rtmp;
1201 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1202 goto error;
1203 }
1204 /* Check that this is not a duplicate 4-tuple */
1205 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1208 &s->addr_info.local) &&
1210 &s->addr_info.remote)) {
1211 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1212 coap_session_str(session), (void *)session);
1213 goto error;
1214 }
1215 }
1216#ifdef WITH_CONTIKI
1217 session->sock.context = ctx;
1218#endif /* WITH_CONTIKI */
1219#if !COAP_DISABLE_TCP
1220 } else if (COAP_PROTO_RELIABLE(proto)) {
1221 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1222 goto error;
1223 }
1224#endif /* !COAP_DISABLE_TCP */
1225 }
1226
1227#ifdef COAP_EPOLL_SUPPORT
1228 session->sock.session = session;
1229 coap_epoll_ctl_add(&session->sock,
1230 EPOLLIN |
1231 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1232 EPOLLOUT : 0),
1233 __func__);
1234#endif /* COAP_EPOLL_SUPPORT */
1235
1237 if (local_if)
1238 session->sock.flags |= COAP_SOCKET_BOUND;
1239#if COAP_SERVER_SUPPORT
1240 if (ctx->proxy_uri_resource)
1241 session->proxy_session = 1;
1242#endif /* COAP_SERVER_SUPPORT */
1243 SESSIONS_ADD(ctx->sessions, session);
1244 return session;
1245
1246error:
1247 /*
1248 * Need to add in the session as coap_session_release()
1249 * will call SESSIONS_DELETE in coap_session_free().
1250 */
1251 if (session)
1252 SESSIONS_ADD(ctx->sessions, session);
1253 coap_session_release(session);
1254 return NULL;
1255}
1256
1257static void
1258coap_session_check_connect(coap_session_t *session) {
1259 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1260 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1261 }
1262#if !COAP_DISABLE_TCP
1263 if (COAP_PROTO_RELIABLE(session->proto)) {
1264 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1266 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1267 session->state != COAP_SESSION_STATE_NONE &&
1268 session->type == COAP_SESSION_TYPE_CLIENT) {
1269 session->doing_first = 1;
1270 }
1271 } else {
1272 /* Initial connect worked immediately */
1273 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1274 }
1275 }
1276#endif /* !COAP_DISABLE_TCP */
1277 coap_ticks(&session->last_rx_tx);
1278}
1279#endif /* COAP_CLIENT_SUPPORT */
1280
1281void
1283 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1284 coap_session_connected(session);
1285#if !COAP_DISABLE_TCP
1286 if (COAP_PROTO_RELIABLE(session->proto))
1287 coap_session_send_csm(session);
1288#endif /* !COAP_DISABLE_TCP */
1289}
1290
1291#if COAP_CLIENT_SUPPORT
1294 const coap_address_t *local_if,
1295 const coap_address_t *server,
1296 coap_proto_t proto) {
1297 coap_session_t *session;
1298
1300 session = coap_session_create_client(ctx, local_if, server,
1301 proto);
1302 if (session) {
1303 coap_log_debug("***%s: session %p: created outgoing session\n",
1304 coap_session_str(session), (void *)session);
1305 coap_session_check_connect(session);
1306 }
1307 return session;
1308}
1309
1312 const coap_address_t *local_if,
1313 const coap_address_t *server,
1314 coap_proto_t proto, const char *identity,
1315 const uint8_t *key, unsigned key_len) {
1316 coap_dtls_cpsk_t setup_data;
1317
1319 memset(&setup_data, 0, sizeof(setup_data));
1321
1322 if (identity) {
1323 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1324 setup_data.psk_info.identity.length = strlen(identity);
1325 }
1326
1327 if (key && key_len > 0) {
1328 setup_data.psk_info.key.s = key;
1329 setup_data.psk_info.key.length = key_len;
1330 }
1331
1332 return coap_new_client_session_psk2(ctx, local_if, server,
1333 proto, &setup_data);
1334}
1335
1338 const coap_address_t *local_if,
1339 const coap_address_t *server,
1340 coap_proto_t proto,
1341 coap_dtls_cpsk_t *setup_data) {
1342 coap_session_t *session;
1343
1345 session = coap_session_create_client(ctx, local_if, server, proto);
1346
1347 if (!session)
1348 return NULL;
1349
1350 session->cpsk_setup_data = *setup_data;
1351 if (setup_data->psk_info.identity.s) {
1352 session->psk_identity =
1354 setup_data->psk_info.identity.length);
1355 if (!session->psk_identity) {
1356 coap_log_warn("Cannot store session Identity (PSK)\n");
1357 coap_session_release(session);
1358 return NULL;
1359 }
1361 coap_log_warn("Identity (PSK) not defined\n");
1362 coap_session_release(session);
1363 return NULL;
1364 }
1365
1366 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1367 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1368 setup_data->psk_info.key.length);
1369 if (!session->psk_key) {
1370 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1371 coap_session_release(session);
1372 return NULL;
1373 }
1375 coap_log_warn("Pre-shared key (PSK) not defined\n");
1376 coap_session_release(session);
1377 return NULL;
1378 }
1379
1381 if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
1382 coap_session_release(session);
1383 return NULL;
1384 }
1385 }
1386 coap_log_debug("***%s: new outgoing session\n",
1387 coap_session_str(session));
1388 coap_session_check_connect(session);
1389 return session;
1390}
1391#endif /* COAP_CLIENT_SUPPORT */
1392
1393int
1395 const coap_bin_const_t *psk_hint
1396 ) {
1397 /* We may be refreshing the hint with the same hint */
1398 coap_bin_const_t *old_psk_hint = session->psk_hint;
1399
1400 if (psk_hint && psk_hint->s) {
1401 if (session->psk_hint) {
1402 if (coap_binary_equal(session->psk_hint, psk_hint))
1403 return 1;
1404 }
1405 session->psk_hint = coap_new_bin_const(psk_hint->s,
1406 psk_hint->length);
1407 if (!session->psk_hint) {
1408 coap_log_err("No memory to store identity hint (PSK)\n");
1409 if (old_psk_hint)
1410 coap_delete_bin_const(old_psk_hint);
1411 return 0;
1412 }
1413 } else {
1414 session->psk_hint = NULL;
1415 }
1416 if (old_psk_hint)
1417 coap_delete_bin_const(old_psk_hint);
1418
1419 return 1;
1420}
1421
1422int
1424 const coap_bin_const_t *psk_key
1425 ) {
1426 /* We may be refreshing the key with the same key */
1427 coap_bin_const_t *old_psk_key = session->psk_key;
1428
1429 if (psk_key && psk_key->s) {
1430 if (session->psk_key) {
1431 if (coap_binary_equal(session->psk_key, psk_key))
1432 return 1;
1433 }
1434 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1435 if (!session->psk_key) {
1436 coap_log_err("No memory to store pre-shared key (PSK)\n");
1437 if (old_psk_key)
1438 coap_delete_bin_const(old_psk_key);
1439 return 0;
1440 }
1441 } else {
1442 session->psk_key = NULL;
1443 }
1444 if (old_psk_key)
1445 coap_delete_bin_const(old_psk_key);
1446
1447 return 1;
1448}
1449
1450int
1452 const coap_bin_const_t *psk_identity
1453 ) {
1454 /* We may be refreshing the identity with the same identity */
1455 coap_bin_const_t *old_psk_identity = session->psk_identity;
1456
1457 if (psk_identity && psk_identity->s) {
1458 if (session->psk_identity) {
1459 if (coap_binary_equal(session->psk_identity, psk_identity))
1460 return 1;
1461 }
1462 session->psk_identity = coap_new_bin_const(psk_identity->s,
1463 psk_identity->length);
1464 if (!session->psk_identity) {
1465 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1466 if (old_psk_identity)
1467 coap_delete_bin_const(old_psk_identity);
1468 return 0;
1469 }
1470 } else {
1471 session->psk_identity = NULL;
1472 }
1473 if (old_psk_identity)
1474 coap_delete_bin_const(old_psk_identity);
1475
1476 return 1;
1477}
1478
1479#if COAP_SERVER_SUPPORT
1480const coap_bin_const_t *
1482 if (session)
1483 return session->psk_hint;
1484 return NULL;
1485}
1486#endif /* COAP_SERVER_SUPPORT */
1487
1488const coap_bin_const_t *
1490 const coap_bin_const_t *psk_identity = NULL;
1491 if (session) {
1492 psk_identity = session->psk_identity;
1493 if (psk_identity == NULL) {
1494 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1495 }
1496 }
1497 return psk_identity;
1498}
1499
1500const coap_bin_const_t *
1502 if (session)
1503 return session->psk_key;
1504 return NULL;
1505}
1506
1507#if COAP_CLIENT_SUPPORT
1510 const coap_address_t *local_if,
1511 const coap_address_t *server,
1512 coap_proto_t proto,
1513 coap_dtls_pki_t *setup_data) {
1514 coap_session_t *session;
1515
1518 if (!setup_data) {
1519 return NULL;
1520 } else {
1521 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1522 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
1523 return NULL;
1524 }
1525 }
1526
1527 }
1528 session = coap_session_create_client(ctx, local_if, server, proto);
1529
1530 if (!session) {
1531 return NULL;
1532 }
1533
1535 /* we know that setup_data is not NULL */
1536 if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1537 coap_session_release(session);
1538 return NULL;
1539 }
1540 }
1541 coap_log_debug("***%s: new outgoing session\n",
1542 coap_session_str(session));
1543 coap_session_check_connect(session);
1544 return session;
1545}
1546#endif /* ! COAP_CLIENT_SUPPORT */
1547
1548#if COAP_SERVER_SUPPORT
1549#if !COAP_DISABLE_TCP
1552 coap_session_t *session;
1554 NULL, NULL, NULL, 0, ctx, ep);
1555 if (!session)
1556 goto error;
1557
1558 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
1559 if (!coap_netif_strm_accept(ep, session, extra))
1560 goto error;
1561
1562 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1563
1564#ifdef COAP_EPOLL_SUPPORT
1565 session->sock.session = session;
1566 coap_epoll_ctl_add(&session->sock,
1567 EPOLLIN,
1568 __func__);
1569#endif /* COAP_EPOLL_SUPPORT */
1570 SESSIONS_ADD(ep->sessions, session);
1571 if (session) {
1572 coap_log_debug("***%s: session %p: new incoming session\n",
1573 coap_session_str(session), (void *)session);
1577 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1578 }
1579 return session;
1580
1581error:
1582 /*
1583 * Need to add in the session as coap_session_release()
1584 * will call SESSIONS_DELETE in coap_session_free().
1585 */
1586 if (session) {
1587 SESSIONS_ADD(ep->sessions, session);
1588 coap_session_free(session);
1589 }
1590 return NULL;
1591}
1592#endif /* !COAP_DISABLE_TCP */
1593#endif /* COAP_SERVER_SUPPORT */
1594
1595void
1597 const uint8_t *data) {
1598 session->tx_token = coap_decode_var_bytes8(data, len);
1599 /*
1600 * Decrement as when first used by coap_session_new_token() it will
1601 * get incremented
1602 */
1603 session->tx_token--;
1604}
1605
1606void
1608 uint8_t *data) {
1609 *len = coap_encode_var_safe8(data,
1610 sizeof(session->tx_token), ++session->tx_token);
1611}
1612
1613uint16_t
1616 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1617 return ++session->tx_mid;
1618 /* TCP/TLS have no notion of mid */
1619 return 0;
1620}
1621
1622const coap_address_t *
1624 if (session)
1625 return &session->addr_info.remote;
1626 return NULL;
1627}
1628
1629const coap_address_t *
1631 if (session)
1632 return &session->addr_info.local;
1633 return NULL;
1634}
1635
1636const coap_address_t *
1638#if COAP_CLIENT_SUPPORT
1639 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
1640 session->sock.flags & COAP_SOCKET_MULTICAST)
1641 return &session->sock.mcast_addr;
1642#else /* ! COAP_CLIENT_SUPPORT */
1643 (void)session;
1644#endif /* ! COAP_CLIENT_SUPPORT */
1645 return NULL;
1646}
1647
1650 if (session)
1651 return session->context;
1652 return NULL;
1653}
1654
1657 if (session)
1658 return session->proto;
1659 return 0;
1660}
1661
1664 if (session)
1665 return session->type;
1666 return 0;
1667}
1668
1669#if COAP_CLIENT_SUPPORT
1670int
1672#if COAP_SERVER_SUPPORT
1673 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1674 coap_session_reference(session);
1675 session->type = COAP_SESSION_TYPE_CLIENT;
1676 return 1;
1677 }
1678#else /* ! COAP_SERVER_SUPPORT */
1679 (void)session;
1680#endif /* ! COAP_SERVER_SUPPORT */
1681 return 0;
1682}
1683#endif /* COAP_CLIENT_SUPPORT */
1684
1687 if (session)
1688 return session->state;
1689 return 0;
1690}
1691
1692int
1694 if (session)
1695 return session->ifindex;
1696 return -1;
1697}
1698
1699void *
1701 coap_tls_library_t *tls_lib) {
1702 if (session)
1703 return coap_dtls_get_tls(session, tls_lib);
1704 return NULL;
1705}
1706
1707static const char *
1709 switch (proto) {
1710 case COAP_PROTO_UDP:
1711 return "UDP ";
1712 case COAP_PROTO_DTLS:
1713 return "DTLS";
1714 case COAP_PROTO_TCP:
1715 return "TCP ";
1716 case COAP_PROTO_TLS:
1717 return "TLS ";
1718 case COAP_PROTO_WS:
1719 return "WS ";
1720 case COAP_PROTO_WSS:
1721 return "WSS ";
1722 case COAP_PROTO_NONE:
1723 case COAP_PROTO_LAST:
1724 default:
1725 return "????" ;
1726 break;
1727 }
1728 return NULL;
1729}
1730
1731#if COAP_SERVER_SUPPORT
1733coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1734 coap_endpoint_t *ep = NULL;
1735
1736 assert(context);
1737 assert(listen_addr);
1738 assert(proto != COAP_PROTO_NONE);
1739
1740 coap_lock_check_locked(context);
1741
1742 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1743 coap_log_crit("coap_new_endpoint: DTLS not supported\n");
1744 goto error;
1745 }
1746
1747 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1748 coap_log_crit("coap_new_endpoint: TLS not supported\n");
1749 goto error;
1750 }
1751
1752 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1753 coap_log_crit("coap_new_endpoint: TCP not supported\n");
1754 goto error;
1755 }
1756
1757 if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
1758 coap_log_crit("coap_new_endpoint: WS not supported\n");
1759 goto error;
1760 }
1761
1762 if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
1763 coap_log_crit("coap_new_endpoint: WSS not supported\n");
1764 goto error;
1765 }
1766
1767 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
1768 proto == COAP_PROTO_WSS) {
1770 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
1771 "coap_context_set_pki() not called\n");
1772 goto error;
1773 }
1774 }
1775
1776 ep = coap_malloc_endpoint();
1777 if (!ep) {
1778 coap_log_warn("coap_new_endpoint: malloc");
1779 goto error;
1780 }
1781
1782 memset(ep, 0, sizeof(coap_endpoint_t));
1783 ep->context = context;
1784 ep->proto = proto;
1785 ep->sock.endpoint = ep;
1786 assert(proto < COAP_PROTO_LAST);
1787 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
1788
1789 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1790 if (!coap_netif_dgrm_listen(ep, listen_addr))
1791 goto error;
1792#ifdef WITH_CONTIKI
1793 ep->sock.context = context;
1794#endif /* WITH_CONTIKI */
1795#if !COAP_DISABLE_TCP
1796 } else if (COAP_PROTO_RELIABLE(proto)) {
1797 if (!coap_netif_strm_listen(ep, listen_addr))
1798 goto error;
1799#endif /* !COAP_DISABLE_TCP */
1800 } else {
1801 coap_log_crit("coap_new_endpoint: protocol not supported\n");
1802 goto error;
1803 }
1804
1806#ifndef INET6_ADDRSTRLEN
1807#define INET6_ADDRSTRLEN 40
1808#endif
1809 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1810
1811 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1812 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
1813 addr_str);
1814 }
1815 }
1816
1818
1819#ifdef COAP_EPOLL_SUPPORT
1820 ep->sock.endpoint = ep;
1822 EPOLLIN,
1823 __func__);
1824#endif /* COAP_EPOLL_SUPPORT */
1825
1826 LL_PREPEND(context->endpoint, ep);
1827 return ep;
1828
1829error:
1831 return NULL;
1832}
1833
1834void
1836 ep->default_mtu = (uint16_t)mtu;
1837}
1838
1839void
1841 if (ep) {
1842 coap_session_t *session, *rtmp;
1843
1844 if (ep->context)
1846 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
1847 assert(session->ref == 0);
1848 if (session->ref == 0) {
1849 coap_session_free(session);
1850 }
1851 }
1852 if (coap_netif_available_ep(ep)) {
1853 /*
1854 * ep->sock.endpoint is set in coap_new_endpoint().
1855 * ep->sock.session is never set.
1856 *
1857 * session->sock.session is set for both clients and servers (when a
1858 * new session is accepted), but does not affect the endpoint.
1859 *
1860 * So, it is safe to call coap_netif_close_ep() after all the sessions
1861 * have been freed above as we are only working with the endpoint sock.
1862 */
1863#ifdef COAP_EPOLL_SUPPORT
1864 assert(ep->sock.session == NULL);
1865#endif /* COAP_EPOLL_SUPPORT */
1867 }
1868
1869 if (ep->context && ep->context->endpoint) {
1870 LL_DELETE(ep->context->endpoint, ep);
1871 }
1873 }
1874}
1875#endif /* COAP_SERVER_SUPPORT */
1876
1879 const coap_address_t *remote_addr,
1880 int ifindex) {
1881 coap_session_t *s, *rtmp;
1882#if COAP_CLIENT_SUPPORT
1883 SESSIONS_ITER(ctx->sessions, s, rtmp) {
1884 if (s->ifindex == ifindex) {
1885 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
1886 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
1887 return s;
1888 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
1889 return s;
1890 }
1891 }
1892#endif /* COAP_CLIENT_SUPPORT */
1893#if COAP_SERVER_SUPPORT
1894 coap_endpoint_t *ep;
1895
1896 LL_FOREACH(ctx->endpoint, ep) {
1897 SESSIONS_ITER(ep->sessions, s, rtmp) {
1898 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1899 remote_addr))
1900 return s;
1901 }
1902 }
1903#endif /* COAP_SERVER_SUPPORT */
1904 return NULL;
1905}
1906
1907#ifndef INET6_ADDRSTRLEN
1908#define INET6_ADDRSTRLEN 46
1909#endif
1910const char *
1912 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
1913 char *p = szSession, *end = szSession + sizeof(szSession);
1914 if (coap_print_addr(&session->addr_info.local,
1915 (unsigned char *)p, end - p) > 0)
1916 p += strlen(p);
1917 if (p + 6 < end) {
1918 strcpy(p, " <-> ");
1919 p += 5;
1920 }
1921 if (p + 1 < end) {
1922 if (coap_print_addr(&session->addr_info.remote,
1923 (unsigned char *)p, end - p) > 0)
1924 p += strlen(p);
1925 }
1926 if (session->ifindex > 0 && p + 1 < end)
1927 p += snprintf(p, end - p, " (if%d)", session->ifindex);
1928 if (p + 6 < end) {
1929 strcpy(p, " ");
1930 p++;
1931 strcpy(p, coap_proto_name(session->proto));
1932 }
1933
1934 return szSession;
1935}
1936
1937#if COAP_SERVER_SUPPORT
1938const char *
1939coap_endpoint_str(const coap_endpoint_t *endpoint) {
1940 static char szEndpoint[128];
1941 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
1942 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
1943 p += strlen(p);
1944 if (p + 6 < end) {
1945 if (endpoint->proto == COAP_PROTO_UDP) {
1946 strcpy(p, " UDP");
1947 p += 4;
1948 } else if (endpoint->proto == COAP_PROTO_DTLS) {
1949 strcpy(p, " DTLS");
1950 p += 5;
1951 } else {
1952 strcpy(p, " NONE");
1953 p += 5;
1954 }
1955 }
1956
1957 return szEndpoint;
1958}
1959#endif /* COAP_SERVER_SUPPORT */
1960#if COAP_CLIENT_SUPPORT
1961void
1963 session->no_observe_cancel = 1;
1964}
1965#endif /* COAP_CLIENT_SUPPORT */
1966#endif /* COAP_SESSION_C_ */
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:253
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:44
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.c:743
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:81
Pulls together all the internal only header files.
#define PRIu32
Definition: coap_internal.h:45
coap_nack_reason_t
Definition: coap_io.h:69
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:71
@ COAP_NACK_WS_FAILED
Definition: coap_io.h:78
@ COAP_NACK_TOO_MANY_RETRIES
Definition: coap_io.h:70
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:73
@ COAP_NACK_TLS_LAYER_FAILED
Definition: coap_io.h:76
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:74
@ COAP_NACK_WS_LAYER_FAILED
Definition: coap_io.h:77
@ COAP_NACK_RST
Definition: coap_io.h:72
@ COAP_NACK_BAD_RESPONSE
Definition: coap_io.h:75
#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_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
@ COAP_SESSION
Definition: coap_mem.h:50
@ COAP_STRING
Definition: coap_mem.h:38
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().
#define coap_lock_check_locked(s)
#define coap_lock_callback(s, func)
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition: coap_notls.c:77
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition: coap_notls.c:122
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:225
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition: coap_notls.c:111
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:744
static const char * coap_proto_name(coap_proto_t proto)
static const char * coap_nack_name(coap_nack_reason_t reason)
Definition: coap_session.c:828
static coap_session_t * coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addr_hash_t *addr_hash, const coap_address_t *local_addr, const coap_address_t *remote_addr, int ifindex, coap_context_t *context, coap_endpoint_t *endpoint)
Definition: coap_session.c:390
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
Definition: coap_session.c:580
#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)
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:20
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)
Definition: coap_block.c:2211
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
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...
Definition: coap_session.h:520
uint16_t coap_session_get_non_max_retransmit(const coap_session_t *session)
Get the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition: coap_session.c:318
coap_fixed_point_t coap_session_get_default_leisure(const coap_session_t *session)
Get the CoAP default leisure time RFC7252 DEFAULT_LEISURE.
Definition: coap_session.c:298
void coap_session_set_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:174
#define COAP_DEFAULT_NON_TIMEOUT
The delay (+ ACK_RANDOM_FACTOR) to introduce once NON MAX_PAYLOADS Q-Block1 or Q-Block2 have been sen...
Definition: coap_session.h:529
coap_fixed_point_t coap_session_get_non_timeout(const coap_session_t *session)
Get the CoAP MAX_PAYLOADS limit delay timeout.
Definition: coap_session.c:328
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:158
#define COAP_DEFAULT_MAX_LATENCY
The MAX_LATENCY definition.
Definition: coap_session.h:544
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:283
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
Definition: coap_session.h:469
uint16_t coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:288
void coap_session_set_max_payloads(coap_session_t *session, uint16_t value)
Set the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition: coap_session.c:212
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
Definition: coap_session.h:477
uint32_t coap_session_get_probing_rate(const coap_session_t *session)
Get the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
Definition: coap_session.c:303
#define COAP_DEFAULT_ACK_TIMEOUT
Number of seconds when to expect an ACK or a response to an outstanding CON message.
Definition: coap_session.h:460
#define COAP_DEFAULT_DEFAULT_LEISURE
The number of seconds to use as bounds for multicast traffic RFC 7252, Section 4.8 Default value of D...
Definition: coap_session.h:494
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:148
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:486
void coap_session_set_non_receive_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non receive timeout delay timeout.
Definition: coap_session.c:262
void coap_session_set_nstart(coap_session_t *session, uint16_t value)
Set the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:183
#define COAP_DEFAULT_PROBING_RATE
The number of bytes/second allowed when there is no response RFC 7252, Section 4.8 Default value of P...
Definition: coap_session.h:502
void coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition: coap_session.c:228
uint16_t coap_session_get_max_payloads(const coap_session_t *session)
Get the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition: coap_session.c:308
#define COAP_DEFAULT_NON_RECEIVE_TIMEOUT
The time to wait for any missing Q-Block1 or Q-Block2 packets before requesting re-transmission of mi...
Definition: coap_session.h:538
void coap_session_set_probing_rate(coap_session_t *session, uint32_t value)
Set the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
Definition: coap_session.c:203
void coap_session_set_default_leisure(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP default leisure time (for multicast) RFC7252 DEFAULT_LEISURE.
Definition: coap_session.c:192
coap_fixed_point_t coap_session_get_non_receive_timeout(const coap_session_t *session)
Get the CoAP non receive timeout delay timeout.
Definition: coap_session.c:338
uint16_t coap_session_get_nstart(const coap_session_t *session)
Get the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:293
#define COAP_DEFAULT_MAX_PAYLOADS
Number of Q-Block1 or Q-Block2 payloads that can be sent in a burst before a delay has to kick in.
Definition: coap_session.h:511
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
Definition: coap_session.c:278
void coap_session_set_non_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non timeout delay timeout.
Definition: coap_session.c:244
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:143
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:158
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:140
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:2322
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: coap_net.c:204
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:1016
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: coap_net.c:1461
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:913
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: coap_net.c:939
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: coap_net.c:233
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:2367
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
Definition: coap_net.c:4073
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
Definition: coap_net.h:50
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
Definition: coap_dtls.c:21
coap_session_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.
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:28
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition: coap_dtls.h:282
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:23
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition: coap_dtls.h:376
coap_tls_library_t
Definition: coap_dtls.h:93
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition: coap_dtls.h:45
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:61
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition: coap_event.h:55
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition: coap_event.h:65
@ COAP_EVENT_SERVER_SESSION_NEW
Called in the CoAP IO loop if a new server-side session is created due to an incoming connection.
Definition: coap_event.h:85
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition: coap_event.h:63
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
Definition: coap_event.h:94
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition: coap_event.h:53
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition: coap_event.h:51
#define coap_log_debug(...)
Definition: coap_debug.h:120
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:95
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:233
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:108
#define coap_log_warn(...)
Definition: coap_debug.h:102
#define coap_log_err(...)
Definition: coap_debug.h:96
#define coap_log_crit(...)
Definition: coap_debug.h:90
@ COAP_LOG_DEBUG
Definition: coap_debug.h:58
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.
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:367
#define COAP_PDU_DELAYED
#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:1428
#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:722
#define COAP_DEFAULT_PORT
Definition: coap_pdu.h:37
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: coap_pdu.c:168
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: coap_pdu.h:263
#define COAP_TOKEN_DEFAULT_MAX
Definition: coap_pdu.h:56
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
Definition: coap_pdu.h:199
coap_proto_t
CoAP protocol types.
Definition: coap_pdu.h:312
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: coap_pdu.h:198
#define COAPS_DEFAULT_PORT
Definition: coap_pdu.h:38
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: coap_pdu.c:97
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: coap_pdu.h:266
#define COAP_DEFAULT_MTU
Definition: coap_pdu.h:41
#define COAP_BERT_BASE
Definition: coap_pdu.h:44
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: coap_pdu.h:197
@ COAP_PROTO_WS
Definition: coap_pdu.h:318
@ COAP_PROTO_DTLS
Definition: coap_pdu.h:315
@ COAP_PROTO_UDP
Definition: coap_pdu.h:314
@ COAP_PROTO_NONE
Definition: coap_pdu.h:313
@ COAP_PROTO_TLS
Definition: coap_pdu.h:317
@ COAP_PROTO_WSS
Definition: coap_pdu.h:319
@ COAP_PROTO_TCP
Definition: coap_pdu.h:316
@ COAP_PROTO_LAST
Definition: coap_pdu.h:320
@ COAP_SIGNALING_CODE_CSM
Definition: coap_pdu.h:365
@ COAP_SIGNALING_CODE_PING
Definition: coap_pdu.h:366
@ COAP_MESSAGE_NON
Definition: coap_pdu.h:70
@ COAP_MESSAGE_CON
Definition: coap_pdu.h:69
#define COAP_NON_PROBING_WAIT_BASE(s)
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: coap_session.c:656
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.
Definition: coap_session.c:697
coap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:60
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
Definition: coap_session.c:603
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:68
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_session_establish(coap_session_t *session)
Layer function interface for layer below session accept/connect being established.
coap_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition: coap_session.c:50
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.
Definition: coap_session.c:766
coap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition: coap_session.c:30
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:815
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:76
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:549
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:467
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
coap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session)
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_NON_TIMEOUT(s)
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition: coap_session.c:40
#define COAP_NON_PARTIAL_TIMEOUT(s)
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_session_type_t
coap_session_type_t values
Definition: coap_session.h:44
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:641
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.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:613
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.
void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
coap_session_state_t
coap_session_state_t values
Definition: coap_session.h:55
coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
coap_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.
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:37
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)
Definition: coap_session.h:38
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.
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
Definition: coap_session.c:378
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:355
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).
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
coap_session_t * coap_new_client_session(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.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
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.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:855
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_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:348
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:384
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
Definition: coap_session.h:48
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:47
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:46
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:58
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:59
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:60
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:56
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:57
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:203
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition: coap_ws.c:932
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition: coap_ws.c:937
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:56
coap_address_t local
local address and port
Definition: coap_io.h:57
Multi-purpose address abstraction.
Definition: coap_address.h:148
CoAP binary data definition with const data.
Definition: coap_str.h:64
size_t length
length of binary data
Definition: coap_str.h:65
const uint8_t * s
read-only binary data
Definition: coap_str.h:66
coap_session_t * session
The CoAP stack's global state is stored in a coap_context_t object.
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
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
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
coap_bin_const_t key
Definition: coap_dtls.h:352
coap_bin_const_t identity
Definition: coap_dtls.h:351
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:381
uint8_t version
Definition: coap_dtls.h:382
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition: coap_dtls.h:410
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:287
uint8_t version
Definition: coap_dtls.h:288
Abstraction of virtual endpoint that can be attached to coap_context_t.
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_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.
Definition: coap_session.h:31
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:33
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:32
coap_layer_establish_t l_establish
coap_layer_close_t l_close
Structure to hold large body (many blocks) client receive information.
coap_pdu_t pdu
skeletal PDU
coap_binary_t * app_token
app requesting PDU token
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_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.
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.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv)
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
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_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
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_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