libcoap 4.3.5rc1
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-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
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
349 coap_lock_lock(session->context, return NULL);
351 coap_lock_unlock(session->context);
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#endif /* COAP_THREAD_SAFE */
367
368 coap_lock_lock(context, return);
370 coap_lock_unlock(context);
371 }
372}
373
374void
376 if (session) {
378#ifndef __COVERITY__
379 assert(session->ref > 0);
380 if (session->ref > 0)
381 --session->ref;
382 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
383 coap_session_free(session);
384#else /* __COVERITY__ */
385 /* Coverity scan is fooled by the reference counter leading to
386 * false positives for USE_AFTER_FREE. */
387 --session->ref;
388 __coverity_negative_sink__(session->ref);
389 /* Indicate that resources are released properly. */
390 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
391 __coverity_free__(session);
392 }
393#endif /* __COVERITY__ */
394 }
395}
396
397void
398coap_session_set_app_data(coap_session_t *session, void *app_data) {
399 assert(session);
400 session->app = app_data;
401}
402
403void *
405 assert(session);
406 return session->app;
407}
408
409static coap_session_t *
411 const coap_addr_hash_t *addr_hash,
412 const coap_address_t *local_addr,
413 const coap_address_t *remote_addr, int ifindex,
414 coap_context_t *context, coap_endpoint_t *endpoint) {
416 sizeof(coap_session_t));
417#if ! COAP_SERVER_SUPPORT
418 (void)endpoint;
419#endif /* ! COAP_SERVER_SUPPORT */
420 if (!session)
421 return NULL;
422 memset(session, 0, sizeof(*session));
423 session->proto = proto;
424 session->type = type;
425 if (addr_hash)
426 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
427 else
428 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
429 if (local_addr)
430 coap_address_copy(&session->addr_info.local, local_addr);
431 else
433 if (remote_addr)
434 coap_address_copy(&session->addr_info.remote, remote_addr);
435 else
437 session->ifindex = ifindex;
438 session->context = context;
439#if COAP_SERVER_SUPPORT
440 session->endpoint = endpoint;
441 if (endpoint)
442 session->mtu = endpoint->default_mtu;
443 else
444#endif /* COAP_SERVER_SUPPORT */
445 session->mtu = COAP_DEFAULT_MTU;
446 session->block_mode = context->block_mode;
447 if (proto == COAP_PROTO_DTLS) {
448 session->tls_overhead = 29;
449 if (session->tls_overhead >= session->mtu) {
450 session->tls_overhead = session->mtu;
451 coap_log_err("DTLS overhead exceeds MTU\n");
452 }
453 }
457 session->nstart = COAP_DEFAULT_NSTART;
460#if COAP_Q_BLOCK_SUPPORT
461 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
462 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
463 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
464 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
465 coap_session_fix_non_probing_wait_base(session);
466 coap_session_fix_non_partial_timeout(session);
467#endif /* COAP_Q_BLOCK_SUPPORT */
468 session->dtls_event = -1;
473 session->max_token_size = context->max_token_size; /* RFC8974 */
474 if (session->type != COAP_SESSION_TYPE_CLIENT)
476
477 /* Randomly initialize */
478 /* TCP/TLS have no notion of mid */
479 if (COAP_PROTO_NOT_RELIABLE(session->proto))
480 coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
481 coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
482
483 return session;
484}
485
486void
488 coap_queue_t *q, *tmp;
489 coap_lg_xmit_t *lq, *ltmp;
490
491#if COAP_CLIENT_SUPPORT
492 coap_lg_crcv_t *lg_crcv, *etmp;
493
494 /* Need to do this before (D)TLS and socket is closed down */
495 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
496 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
497 /* Need to close down observe */
498 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
499 /* Need to delete node we set up for NON */
500 coap_queue_t *queue = session->context->sendqueue;
501
502 while (queue) {
503 if (queue->session == session) {
505 break;
506 }
507 queue = queue->next;
508 }
509 }
510 }
511 LL_DELETE(session->lg_crcv, lg_crcv);
512 coap_block_delete_lg_crcv(session, lg_crcv);
513 }
514#endif /* COAP_CLIENT_SUPPORT */
515
516 if (session->partial_pdu)
518 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
519 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
520 if (session->psk_identity)
522 if (session->psk_key)
524 if (session->psk_hint)
526
527#if COAP_SERVER_SUPPORT
528 coap_cache_entry_t *cp, *ctmp;
529 HASH_ITER(hh, session->context->cache, cp, ctmp) {
530 /* cp->session is NULL if not session based */
531 if (cp->session == session) {
532 coap_delete_cache_entry(session->context, cp);
533 }
534 }
535#endif /* COAP_SERVER_SUPPORT */
536 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
537 if (q->pdu->type==COAP_MESSAGE_CON && session->context->nack_handler) {
538 coap_check_update_token(session, q->pdu);
540 session->context->nack_handler(session, q->pdu,
541 session->proto == COAP_PROTO_DTLS ?
543 q->id));
544 }
546 }
547 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
548 LL_DELETE(session->lg_xmit, lq);
549 coap_block_delete_lg_xmit(session, lq);
550 }
551#if COAP_SERVER_SUPPORT
552 coap_lg_srcv_t *sq, *stmp;
553
554 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
555 LL_DELETE(session->lg_srcv, sq);
556 coap_block_delete_lg_srcv(session, sq);
557 }
558#endif /* COAP_SERVER_SUPPORT */
559#if COAP_OSCORE_SUPPORT
561#endif /* COAP_OSCORE_SUPPORT */
562#if COAP_WS_SUPPORT
563 coap_free_type(COAP_STRING, session->ws);
564 coap_delete_str_const(session->ws_host);
565#endif /* COAP_WS_SUPPORT */
566}
567
568void
570 if (!session)
571 return;
573 assert(session->ref == 0);
574 if (session->ref)
575 return;
576 /* Make sure nothing gets deleted under our feet */
578 coap_session_mfree(session);
579#if COAP_SERVER_SUPPORT
580 if (session->endpoint) {
581 if (session->endpoint->sessions)
582 SESSIONS_DELETE(session->endpoint->sessions, session);
583 } else
584#endif /* COAP_SERVER_SUPPORT */
585#if COAP_CLIENT_SUPPORT
586 if (session->context) {
587 if (session->context->sessions)
588 SESSIONS_DELETE(session->context->sessions, session);
589 }
590#endif /* COAP_CLIENT_SUPPORT */
592 coap_delete_bin_const(session->echo);
593 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
594 (void *)session);
595
596 assert(session->ref == 1);
598}
599
600static size_t
602 size_t max_with_header) {
603#if COAP_DISABLE_TCP
604 (void)session;
605 return max_with_header > 4 ? max_with_header - 4 : 0;
606#else /* !COAP_DISABLE_TCP */
607 if (COAP_PROTO_NOT_RELIABLE(session->proto))
608 return max_with_header > 4 ? max_with_header - 4 : 0;
609 /* we must assume there is no token to be on the safe side */
610 if (max_with_header <= 2)
611 return 0;
612 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
613 return max_with_header - 2;
614 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
615 return max_with_header - 3;
616 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
617 return max_with_header - 4;
618 else
619 return max_with_header - 6;
620#endif /* !COAP_DISABLE_TCP */
621}
622
623size_t
625 if (session->csm_rcv_mtu)
627 (size_t)(session->csm_rcv_mtu));
628
630 (size_t)(session->mtu - session->tls_overhead));
631}
632
633COAP_API size_t
635 size_t size;
636 coap_session_t *session_rw;
637
638 /*
639 * Need to do this to not get a compiler warning about const parameters
640 * but need to maintain source code backward compatibility
641 */
642 memcpy(&session_rw, &session, sizeof(session_rw));
643 coap_lock_lock(session_rw->context, return 0);
644 size = coap_session_max_pdu_size_lkd(session_rw);
645 coap_lock_unlock(session_rw->context);
646 return size;
647}
648
649size_t
651 size_t max_with_header;
652
654#if COAP_CLIENT_SUPPORT
655 /*
656 * Delay if session->doing_first is set.
657 * E.g. Reliable and CSM not in yet for checking block support
658 */
659 coap_session_t *session_rw;
660
661 /*
662 * Need to do this to not get a compiler warning about const parameters
663 * but need to maintain source code backward compatibility
664 */
665 memcpy(&session_rw, &session, sizeof(session_rw));
666 if (coap_client_delay_first(session_rw) == 0) {
667 coap_log_debug("coap_client_delay_first: timeout\n");
668 /* Have to go with the defaults */
669 }
670#endif /* COAP_CLIENT_SUPPORT */
671
672 max_with_header = (size_t)(session->mtu - session->tls_overhead);
673
674 return coap_session_max_pdu_size_internal(session, max_with_header);
675}
676
677void
678coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
679#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
680 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
681 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
682#endif
683 if (mtu < 64)
684 mtu = 64;
685 session->mtu = mtu;
686 if (session->tls_overhead >= session->mtu) {
687 session->tls_overhead = session->mtu;
688 coap_log_err("DTLS overhead exceeds MTU\n");
689 }
690}
691
692ssize_t
694 coap_queue_t *node) {
695 if (node) {
696 coap_queue_t *removed = NULL;
697 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
698 assert(removed == node);
700 node->session = NULL;
701 node->t = 0;
702 } else {
703 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
704 coap_queue_t *q = NULL;
705 /* Check same mid is not getting re-used in violation of RFC7252 */
706 LL_FOREACH(session->delayqueue, q) {
707 if (q->id == pdu->mid) {
708 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
709 coap_session_str(session), pdu->mid);
710 return COAP_INVALID_MID;
711 }
712 }
713 }
714 node = coap_new_node();
715 if (node == NULL)
716 return COAP_INVALID_MID;
717 node->id = pdu->mid;
718 node->pdu = pdu;
719 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
720 uint8_t r;
721 coap_prng(&r, sizeof(r));
722 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
723 node->timeout = coap_calc_timeout(session, r);
724 }
725 }
726 LL_APPEND(session->delayqueue, node);
727 coap_log_debug("** %s: mid=0x%04x: delayed\n",
728 coap_session_str(session), node->id);
729 return COAP_PDU_DELAYED;
730}
731
732#if !COAP_DISABLE_TCP
733void
735 coap_pdu_t *pdu;
736 uint8_t buf[4];
737 assert(COAP_PROTO_RELIABLE(session->proto));
738 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
739 session->state = COAP_SESSION_STATE_CSM;
740 session->partial_write = 0;
741 if (session->mtu == 0)
742 session->mtu = COAP_DEFAULT_MTU; /* base value */
744 if (pdu == NULL
746 coap_encode_var_safe(buf, sizeof(buf),
747 session->context->csm_max_message_size), buf) == 0
749 coap_encode_var_safe(buf, sizeof(buf),
750 0), buf) == 0
751 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
754 coap_encode_var_safe(buf, sizeof(buf),
755 session->max_token_size),
756 buf) == 0)
757 || coap_pdu_encode_header(pdu, session->proto) == 0
758 ) {
760 } else {
761 ssize_t bytes_written;
762
763 pdu->session = session;
764 bytes_written = coap_session_send_pdu(session, pdu);
765 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
767 } else {
768 session->csm_rcv_mtu = session->context->csm_max_message_size;
769 if (session->csm_rcv_mtu > COAP_BERT_BASE)
770 session->csm_bert_loc_support = 1;
771 else
772 session->csm_bert_loc_support = 0;
773 }
774 }
775 if (pdu)
776 coap_delete_pdu(pdu);
777}
778#endif /* !COAP_DISABLE_TCP */
779
782 coap_mid_t mid;
783
784 coap_lock_lock(session->context, return COAP_INVALID_MID);
785 mid = coap_session_send_ping_lkd(session);
786 coap_lock_unlock(session->context);
787 return mid;
788}
789
792 coap_pdu_t *ping = NULL;
793
795 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
796 session->con_active)
797 return COAP_INVALID_MID;
798 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
799 uint16_t mid = coap_new_message_id_lkd(session);
800 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
801 }
802#if !COAP_DISABLE_TCP
803 else {
805 }
806#endif /* !COAP_DISABLE_TCP */
807 if (!ping)
808 return COAP_INVALID_MID;
809 return coap_send_internal(session, ping);
810}
811
812void
814 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
815 coap_log_debug("***%s: session connected\n",
816 coap_session_str(session));
817 if (session->state == COAP_SESSION_STATE_CSM) {
819 if (session->doing_first)
820 session->doing_first = 0;
821 }
822 }
823
825 session->partial_write = 0;
826
827 if (session->proto==COAP_PROTO_DTLS) {
828 session->tls_overhead = coap_dtls_get_overhead(session);
829 if (session->tls_overhead >= session->mtu) {
830 session->tls_overhead = session->mtu;
831 coap_log_err("DTLS overhead exceeds MTU\n");
832 }
833 }
834
835 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
836 ssize_t bytes_written;
837 coap_queue_t *q = session->delayqueue;
838 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
839 if (session->con_active >= COAP_NSTART(session))
840 break;
841 session->con_active++;
842 }
843 /* Take entry off the queue */
844 session->delayqueue = q->next;
845 q->next = NULL;
846
847 coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
848 coap_session_str(session), (int)q->pdu->mid);
849 bytes_written = coap_session_send_pdu(session, q->pdu);
850 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
851 if (coap_wait_ack(session->context, session, q) >= 0)
852 q = NULL;
853 }
854 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
855 if (q)
857 if (bytes_written < 0)
858 break;
859 } else if (q) {
860 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
861 q->next = session->delayqueue;
862 session->delayqueue = q;
863 if (bytes_written > 0)
864 session->partial_write = (size_t)bytes_written;
865 break;
866 } else {
868 }
869 }
870 }
871}
872
873#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
874static const char *
876 switch (reason) {
878 return "COAP_NACK_TOO_MANY_RETRIES";
880 return "COAP_NACK_NOT_DELIVERABLE";
881 case COAP_NACK_RST:
882 return "COAP_NACK_RST";
884 return "COAP_NACK_TLS_FAILED";
886 return "COAP_NACK_ICMP_ISSUE";
888 return "COAP_NACK_BAD_RESPONSE";
890 return "COAP_NACK_TLS_LAYER_FAILED";
892 return "COAP_NACK_WS_LAYER_FAILED";
894 return "COAP_NACK_WS_FAILED";
895 default:
896 return "???";
897 }
898}
899#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
900
901COAP_API void
903 coap_lock_lock(session->context, return);
904 coap_session_disconnected_lkd(session, reason);
905 coap_lock_unlock(session->context);
906}
907
908void
910#if !COAP_DISABLE_TCP
911 coap_session_state_t state = session->state;
912#endif /* !COAP_DISABLE_TCP */
913 coap_lg_xmit_t *lq, *ltmp;
914#if COAP_SERVER_SUPPORT
915 coap_lg_srcv_t *sq, *stmp;
916#endif /* COAP_SERVER_SUPPORT */
917#if COAP_CLIENT_SUPPORT
918 coap_lg_crcv_t *cq, *etmp;
919#endif /* COAP_CLIENT_SUPPORT */
920
922 if (session->context->nack_handler) {
923 int sent_nack = 0;
924 coap_queue_t *q = session->context->sendqueue;
925
926 while (q) {
927 if (q->session == session) {
928 /* Take the first one */
930
931 coap_check_update_token(session, q->pdu);
933 session->context->nack_handler(session, q->pdu, reason, q->id));
934 coap_update_token(q->pdu, token.length, token.s);
935 sent_nack = 1;
936 break;
937 }
938 q = q->next;
939 }
940
941 if (reason != COAP_NACK_ICMP_ISSUE) {
942 while (session->delayqueue) {
943 q = session->delayqueue;
944 session->delayqueue = q->next;
945 q->next = NULL;
946 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
947 coap_session_str(session), q->id);
948 if (q->pdu->type == COAP_MESSAGE_CON) {
949 coap_check_update_token(session, q->pdu);
951 session->context->nack_handler(session, q->pdu, reason, q->id));
952 sent_nack = 1;
953 }
955 }
956 }
957#if COAP_CLIENT_SUPPORT
958 if (!sent_nack && session->lg_crcv) {
959 /* Take the first one */
961 session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
962 session->lg_crcv->pdu.mid));
963 sent_nack = 1;
964 }
965#endif /* COAP_CLIENT_SUPPORT */
966 if (!sent_nack) {
967 /* Unable to determine which request disconnection was for */
969 session->context->nack_handler(session, NULL, reason, 0));
970 }
971 }
972 if (reason == COAP_NACK_ICMP_ISSUE) {
973 coap_log_debug("***%s: session ICMP issue (%s)\n",
974 coap_session_str(session), coap_nack_name(reason));
975 return;
976 }
977 coap_log_debug("***%s: session disconnected (%s)\n",
978 coap_session_str(session), coap_nack_name(reason));
979#if COAP_SERVER_SUPPORT
980 coap_delete_observers(session->context, session);
981#endif /* COAP_SERVER_SUPPORT */
982
983 if (session->proto == COAP_PROTO_UDP)
985 else
987
988 session->con_active = 0;
989
990 if (session->partial_pdu) {
992 session->partial_pdu = NULL;
993 }
994 session->partial_read = 0;
995
996 /* Not done if nack handler called above */
997 while (session->delayqueue) {
998 coap_queue_t *q = session->delayqueue;
999 session->delayqueue = q->next;
1000 q->next = NULL;
1001 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1002 coap_session_str(session), q->id);
1004 }
1005
1006#if COAP_CLIENT_SUPPORT
1007 /* Need to do this before (D)TLS and socket is closed down */
1008 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1009 LL_DELETE(session->lg_crcv, cq);
1010 coap_block_delete_lg_crcv(session, cq);
1011 }
1012#endif /* COAP_CLIENT_SUPPORT */
1013 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1014 LL_DELETE(session->lg_xmit, lq);
1015 coap_block_delete_lg_xmit(session, lq);
1016 }
1017#if COAP_SERVER_SUPPORT
1018 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1019 LL_DELETE(session->lg_srcv, sq);
1020 coap_block_delete_lg_srcv(session, sq);
1021 }
1022#endif /* COAP_SERVER_SUPPORT */
1023 coap_cancel_session_messages(session->context, session, reason);
1024
1025#if !COAP_DISABLE_TCP
1026 if (COAP_PROTO_RELIABLE(session->proto)) {
1027 if (coap_netif_available(session)) {
1031 }
1032 if (state != COAP_SESSION_STATE_NONE) {
1036 }
1037 if (session->doing_first)
1038 session->doing_first = 0;
1039 }
1040#endif /* !COAP_DISABLE_TCP */
1041 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1042}
1043
1044#if COAP_SERVER_SUPPORT
1045static void
1046coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1047 const coap_addr_tuple_t *addr_info) {
1048 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1049 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1050 addr_hash->lport = coap_address_get_port(&addr_info->local);
1051 addr_hash->proto = proto;
1052}
1053
1056 const coap_packet_t *packet, coap_tick_t now) {
1057 coap_session_t *session;
1058 coap_session_t *rtmp;
1059 unsigned int num_idle = 0;
1060 unsigned int num_hs = 0;
1061 coap_session_t *oldest = NULL;
1062 coap_session_t *oldest_hs = NULL;
1063 coap_addr_hash_t addr_hash;
1064
1065 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1066 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1067 if (session) {
1068 /* Maybe mcast or unicast IP address which is not in the hash */
1069 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1070 session->ifindex = packet->ifindex;
1071 session->last_rx_tx = now;
1072 return session;
1073 }
1074
1075 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1076 if (session->ref == 0 && session->delayqueue == NULL) {
1077 if (session->type == COAP_SESSION_TYPE_SERVER) {
1078 ++num_idle;
1079 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1080 oldest = session;
1081
1082 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1083 ++num_hs;
1084 /* See if this is a partial (D)TLS session set up
1085 which needs to be cleared down to prevent DOS */
1086 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1087 if (oldest_hs == NULL ||
1088 session->last_rx_tx < oldest_hs->last_rx_tx)
1089 oldest_hs = session;
1090 }
1091 }
1092 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1093 ++num_hs;
1094 /* See if this is a partial (D)TLS session set up for Client Hello
1095 which needs to be cleared down to prevent DOS */
1096 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1097 if (oldest_hs == NULL ||
1098 session->last_rx_tx < oldest_hs->last_rx_tx)
1099 oldest_hs = session;
1100 }
1101 }
1102 }
1103 }
1104
1105 if (endpoint->context->max_idle_sessions > 0 &&
1106 num_idle >= endpoint->context->max_idle_sessions) {
1108 coap_session_free(oldest);
1109 } else if (oldest_hs) {
1110 coap_log_warn("***%s: Incomplete session timed out\n",
1111 coap_session_str(oldest_hs));
1113 coap_session_free(oldest_hs);
1114 }
1115
1116 if (num_hs > (endpoint->context->max_handshake_sessions ?
1117 endpoint->context->max_handshake_sessions :
1119 /* Maxed out on number of sessions in (D)TLS negotiation state */
1120 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1121 "large. New request ignored\n");
1122 return NULL;
1123 }
1124
1125 if (endpoint->proto == COAP_PROTO_DTLS) {
1126 /*
1127 * Need to check that this actually is a Client Hello before wasting
1128 * time allocating and then freeing off session.
1129 */
1130
1131 /*
1132 * Generic header structure of the DTLS record layer.
1133 * typedef struct __attribute__((__packed__)) {
1134 * uint8_t content_type; content type of the included message
1135 * uint16_t version; Protocol version
1136 * uint16_t epoch; counter for cipher state changes
1137 * uint8_t sequence_number[6]; sequence number
1138 * uint16_t length; length of the following fragment
1139 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1140 * } dtls_record_handshake_t;
1141 */
1142#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1143#define DTLS_CT_ALERT 21 /* Content Type Alert */
1144#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1145#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1146#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1147
1148 const uint8_t *payload = (const uint8_t *)packet->payload;
1149 size_t length = packet->length;
1150 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1151 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1152 payload[OFF_CONTENT_TYPE], length,
1153 OFF_HANDSHAKE_TYPE + 1);
1154 return NULL;
1155 }
1156 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1157 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1158 /* only log if not a late alert */
1159 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1160 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1161 payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
1162 return NULL;
1163 }
1164 }
1165
1167 &addr_hash, &packet->addr_info.local,
1168 &packet->addr_info.remote,
1169 packet->ifindex, endpoint->context, endpoint);
1170 if (session) {
1171 session->last_rx_tx = now;
1172 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1173 sizeof(session->sock.lfunc));
1174 if (endpoint->proto == COAP_PROTO_UDP)
1176 else if (endpoint->proto == COAP_PROTO_DTLS) {
1177 session->type = COAP_SESSION_TYPE_HELLO;
1178 }
1179 SESSIONS_ADD(endpoint->sessions, session);
1180 coap_log_debug("***%s: session %p: new incoming session\n",
1181 coap_session_str(session), (void *)session);
1183 }
1184 return session;
1185}
1186
1189 coap_tick_t now) {
1190 if (session) {
1191 session->last_rx_tx = now;
1192 session->type = COAP_SESSION_TYPE_SERVER;
1193 coap_dtls_establish(session);
1194 }
1195 return session;
1196}
1197#endif /* COAP_SERVER_SUPPORT */
1198
1199#if COAP_CLIENT_SUPPORT
1200static coap_session_t *
1201coap_session_create_client(coap_context_t *ctx,
1202 const coap_address_t *local_if,
1203 const coap_address_t *server,
1204 coap_proto_t proto) {
1205 coap_session_t *session = NULL;
1206 int default_port = COAP_DEFAULT_PORT;
1207
1208 assert(server);
1209
1210 switch (proto) {
1211 case COAP_PROTO_UDP:
1212 default_port = COAP_DEFAULT_PORT;
1213 break;
1214 case COAP_PROTO_DTLS:
1215 if (!coap_dtls_is_supported()) {
1216 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1217 return NULL;
1218 }
1219 default_port = COAPS_DEFAULT_PORT;
1220 break;
1221 case COAP_PROTO_TCP:
1222 if (!coap_tcp_is_supported()) {
1223 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1224 return NULL;
1225 }
1226 default_port = COAP_DEFAULT_PORT;
1227 break;
1228 case COAP_PROTO_TLS:
1229 if (!coap_tls_is_supported()) {
1230 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1231 return NULL;
1232 }
1233 default_port = COAPS_DEFAULT_PORT;
1234 break;
1235 case COAP_PROTO_WS:
1236 if (!coap_ws_is_supported()) {
1237 coap_log_crit("coap_new_client_session*: WS not supported\n");
1238 return NULL;
1239 }
1240 default_port = 80;
1241 break;
1242 case COAP_PROTO_WSS:
1243 if (!coap_wss_is_supported()) {
1244 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1245 return NULL;
1246 }
1247 default_port = 443;
1248 break;
1249 case COAP_PROTO_NONE:
1250 case COAP_PROTO_LAST:
1251 default:
1252 assert(0);
1253 return NULL;
1254 }
1255 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1256 local_if, server, 0, ctx, NULL);
1257 if (!session)
1258 goto error;
1259
1261 session->sock.session = session;
1262 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1263 sizeof(session->sock.lfunc));
1264
1265 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1266 coap_session_t *s, *rtmp;
1267 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1268 goto error;
1269 }
1270 /* Check that this is not a duplicate 4-tuple */
1271 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1274 &s->addr_info.local) &&
1276 &s->addr_info.remote)) {
1277 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1278 coap_session_str(session), (void *)session);
1279 goto error;
1280 }
1281 }
1282#ifdef WITH_CONTIKI
1283 session->sock.context = ctx;
1284#endif /* WITH_CONTIKI */
1285#if !COAP_DISABLE_TCP
1286 } else if (COAP_PROTO_RELIABLE(proto)) {
1287 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1288 goto error;
1289 }
1290#endif /* !COAP_DISABLE_TCP */
1291 }
1292
1293#ifdef COAP_EPOLL_SUPPORT
1294 session->sock.session = session;
1295 coap_epoll_ctl_add(&session->sock,
1296 EPOLLIN |
1297 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1298 EPOLLOUT : 0),
1299 __func__);
1300#endif /* COAP_EPOLL_SUPPORT */
1301
1303 if (local_if)
1304 session->sock.flags |= COAP_SOCKET_BOUND;
1305#if COAP_SERVER_SUPPORT
1306 if (ctx->proxy_uri_resource)
1307 session->proxy_session = 1;
1308#endif /* COAP_SERVER_SUPPORT */
1309 SESSIONS_ADD(ctx->sessions, session);
1310 return session;
1311
1312error:
1313 /*
1314 * Need to add in the session as coap_session_release_lkd()
1315 * will call SESSIONS_DELETE in coap_session_free().
1316 */
1317 if (session)
1318 SESSIONS_ADD(ctx->sessions, session);
1319 coap_session_release_lkd(session);
1320 return NULL;
1321}
1322
1323static void
1324coap_session_check_connect(coap_session_t *session) {
1325 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1326 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1327 }
1328#if !COAP_DISABLE_TCP
1329 if (COAP_PROTO_RELIABLE(session->proto)) {
1330 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1332 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1333 session->state != COAP_SESSION_STATE_NONE &&
1334 session->type == COAP_SESSION_TYPE_CLIENT) {
1335 session->doing_first = 1;
1336 }
1337 } else {
1338 /* Initial connect worked immediately */
1339 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1340 }
1341 }
1342#endif /* !COAP_DISABLE_TCP */
1343 coap_ticks(&session->last_rx_tx);
1344}
1345#endif /* COAP_CLIENT_SUPPORT */
1346
1347void
1349 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1350 coap_session_connected(session);
1351#if !COAP_DISABLE_TCP
1352 if (COAP_PROTO_RELIABLE(session->proto))
1353 coap_session_send_csm(session);
1354#endif /* !COAP_DISABLE_TCP */
1355}
1356
1357#if COAP_CLIENT_SUPPORT
1360 const coap_address_t *local_if,
1361 const coap_address_t *server,
1362 coap_proto_t proto) {
1363 coap_session_t *session;
1364
1365 coap_lock_lock(ctx, return NULL);
1366 session = coap_new_client_session_lkd(ctx, local_if, server, proto);
1367 coap_lock_unlock(ctx);
1368 return session;
1369}
1370
1373 const coap_address_t *local_if,
1374 const coap_address_t *server,
1375 coap_proto_t proto) {
1376 coap_session_t *session;
1377
1379 session = coap_session_create_client(ctx, local_if, server,
1380 proto);
1381 if (session) {
1382 coap_log_debug("***%s: session %p: created outgoing session\n",
1383 coap_session_str(session), (void *)session);
1384 coap_session_check_connect(session);
1385 }
1386 return session;
1387}
1388
1391 const coap_address_t *local_if,
1392 const coap_address_t *server,
1393 coap_proto_t proto, const char *identity,
1394 const uint8_t *key, unsigned key_len) {
1395 coap_session_t *session;
1396
1397 coap_lock_lock(ctx, return NULL);
1398 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1399 coap_lock_unlock(ctx);
1400 return session;
1401}
1402
1405 const coap_address_t *local_if,
1406 const coap_address_t *server,
1407 coap_proto_t proto, const char *identity,
1408 const uint8_t *key, unsigned key_len) {
1409 coap_dtls_cpsk_t setup_data;
1410
1412 memset(&setup_data, 0, sizeof(setup_data));
1414
1415 if (identity) {
1416 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1417 setup_data.psk_info.identity.length = strlen(identity);
1418 }
1419
1420 if (key && key_len > 0) {
1421 setup_data.psk_info.key.s = key;
1422 setup_data.psk_info.key.length = key_len;
1423 }
1424
1425 return coap_new_client_session_psk2_lkd(ctx, local_if, server,
1426 proto, &setup_data);
1427}
1428
1431 const coap_address_t *local_if,
1432 const coap_address_t *server,
1433 coap_proto_t proto,
1434 coap_dtls_cpsk_t *setup_data) {
1435 coap_session_t *session;
1436
1437 coap_lock_lock(ctx, return NULL);
1438 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, setup_data);
1439 coap_lock_unlock(ctx);
1440 return session;
1441}
1442
1445 const coap_address_t *local_if,
1446 const coap_address_t *server,
1447 coap_proto_t proto,
1448 coap_dtls_cpsk_t *setup_data) {
1449 coap_session_t *session;
1450
1452 session = coap_session_create_client(ctx, local_if, server, proto);
1453
1454 if (!session)
1455 return NULL;
1456
1457 session->cpsk_setup_data = *setup_data;
1458 if (setup_data->psk_info.identity.s) {
1459 session->psk_identity =
1461 setup_data->psk_info.identity.length);
1462 if (!session->psk_identity) {
1463 coap_log_warn("Cannot store session Identity (PSK)\n");
1464 coap_session_release_lkd(session);
1465 return NULL;
1466 }
1468 coap_log_warn("Identity (PSK) not defined\n");
1469 coap_session_release_lkd(session);
1470 return NULL;
1471 }
1472
1473 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1474 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1475 setup_data->psk_info.key.length);
1476 if (!session->psk_key) {
1477 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1478 coap_session_release_lkd(session);
1479 return NULL;
1480 }
1482 coap_log_warn("Pre-shared key (PSK) not defined\n");
1483 coap_session_release_lkd(session);
1484 return NULL;
1485 }
1486
1488 if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
1489 coap_session_release_lkd(session);
1490 return NULL;
1491 }
1492 }
1493 coap_log_debug("***%s: new outgoing session\n",
1494 coap_session_str(session));
1495 coap_session_check_connect(session);
1496 return session;
1497}
1498#endif /* COAP_CLIENT_SUPPORT */
1499
1500int
1502 const coap_bin_const_t *psk_hint
1503 ) {
1504 /* We may be refreshing the hint with the same hint */
1505 coap_bin_const_t *old_psk_hint = session->psk_hint;
1506
1507 if (psk_hint && psk_hint->s) {
1508 if (session->psk_hint) {
1509 if (coap_binary_equal(session->psk_hint, psk_hint))
1510 return 1;
1511 }
1512 session->psk_hint = coap_new_bin_const(psk_hint->s,
1513 psk_hint->length);
1514 if (!session->psk_hint) {
1515 coap_log_err("No memory to store identity hint (PSK)\n");
1516 if (old_psk_hint)
1517 coap_delete_bin_const(old_psk_hint);
1518 return 0;
1519 }
1520 } else {
1521 session->psk_hint = NULL;
1522 }
1523 if (old_psk_hint)
1524 coap_delete_bin_const(old_psk_hint);
1525
1526 return 1;
1527}
1528
1529int
1531 const coap_bin_const_t *psk_key
1532 ) {
1533 /* We may be refreshing the key with the same key */
1534 coap_bin_const_t *old_psk_key = session->psk_key;
1535
1536 if (psk_key && psk_key->s) {
1537 if (session->psk_key) {
1538 if (coap_binary_equal(session->psk_key, psk_key))
1539 return 1;
1540 }
1541 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1542 if (!session->psk_key) {
1543 coap_log_err("No memory to store pre-shared key (PSK)\n");
1544 if (old_psk_key)
1545 coap_delete_bin_const(old_psk_key);
1546 return 0;
1547 }
1548 } else {
1549 session->psk_key = NULL;
1550 }
1551 if (old_psk_key)
1552 coap_delete_bin_const(old_psk_key);
1553
1554 return 1;
1555}
1556
1557int
1559 const coap_bin_const_t *psk_identity
1560 ) {
1561 /* We may be refreshing the identity with the same identity */
1562 coap_bin_const_t *old_psk_identity = session->psk_identity;
1563
1564 if (psk_identity && psk_identity->s) {
1565 if (session->psk_identity) {
1566 if (coap_binary_equal(session->psk_identity, psk_identity))
1567 return 1;
1568 }
1569 session->psk_identity = coap_new_bin_const(psk_identity->s,
1570 psk_identity->length);
1571 if (!session->psk_identity) {
1572 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1573 if (old_psk_identity)
1574 coap_delete_bin_const(old_psk_identity);
1575 return 0;
1576 }
1577 } else {
1578 session->psk_identity = NULL;
1579 }
1580 if (old_psk_identity)
1581 coap_delete_bin_const(old_psk_identity);
1582
1583 return 1;
1584}
1585
1586#if COAP_SERVER_SUPPORT
1587const coap_bin_const_t *
1589 if (session)
1590 return session->psk_hint;
1591 return NULL;
1592}
1593#endif /* COAP_SERVER_SUPPORT */
1594
1595const coap_bin_const_t *
1597 const coap_bin_const_t *psk_identity = NULL;
1598 if (session) {
1599 psk_identity = session->psk_identity;
1600 if (psk_identity == NULL) {
1601 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1602 }
1603 }
1604 return psk_identity;
1605}
1606
1607const coap_bin_const_t *
1609 if (session)
1610 return session->psk_key;
1611 return NULL;
1612}
1613
1614#if COAP_CLIENT_SUPPORT
1617 const coap_address_t *local_if,
1618 const coap_address_t *server,
1619 coap_proto_t proto,
1620 coap_dtls_pki_t *setup_data) {
1621 coap_session_t *session;
1622
1623 coap_lock_lock(ctx, return NULL);
1624 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, setup_data);
1625 coap_lock_unlock(ctx);
1626 return session;
1627}
1628
1631 const coap_address_t *local_if,
1632 const coap_address_t *server,
1633 coap_proto_t proto,
1634 coap_dtls_pki_t *setup_data) {
1635 coap_session_t *session;
1636
1639 if (!setup_data) {
1640 return NULL;
1641 } else {
1642 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1643 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
1644 return NULL;
1645 }
1646 }
1647
1648 }
1649 session = coap_session_create_client(ctx, local_if, server, proto);
1650
1651 if (!session) {
1652 return NULL;
1653 }
1654
1656 /* we know that setup_data is not NULL */
1657 if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1658 coap_session_release_lkd(session);
1659 return NULL;
1660 }
1661 }
1662 coap_log_debug("***%s: new outgoing session\n",
1663 coap_session_str(session));
1664 coap_session_check_connect(session);
1665 return session;
1666}
1667#endif /* ! COAP_CLIENT_SUPPORT */
1668
1669#if COAP_SERVER_SUPPORT
1670#if !COAP_DISABLE_TCP
1673 coap_session_t *session;
1675 NULL, NULL, NULL, 0, ctx, ep);
1676 if (!session)
1677 goto error;
1678
1679 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
1680 if (!coap_netif_strm_accept(ep, session, extra))
1681 goto error;
1682
1683 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1684
1685#ifdef COAP_EPOLL_SUPPORT
1686 session->sock.session = session;
1687 coap_epoll_ctl_add(&session->sock,
1688 EPOLLIN,
1689 __func__);
1690#endif /* COAP_EPOLL_SUPPORT */
1691 SESSIONS_ADD(ep->sessions, session);
1692 if (session) {
1693 coap_log_debug("***%s: session %p: new incoming session\n",
1694 coap_session_str(session), (void *)session);
1698 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1699 }
1700 return session;
1701
1702error:
1703 /*
1704 * Need to add in the session as coap_session_release_lkd()
1705 * will call SESSIONS_DELETE in coap_session_free().
1706 */
1707 if (session) {
1708 SESSIONS_ADD(ep->sessions, session);
1709 coap_session_free(session);
1710 }
1711 return NULL;
1712}
1713#endif /* !COAP_DISABLE_TCP */
1714#endif /* COAP_SERVER_SUPPORT */
1715
1716void
1718 const uint8_t *data) {
1719 session->tx_token = coap_decode_var_bytes8(data, len);
1720 /*
1721 * Decrement as when first used by coap_session_new_token() it will
1722 * get incremented
1723 */
1724 session->tx_token--;
1725}
1726
1727void
1729 uint8_t *data) {
1730 *len = coap_encode_var_safe8(data,
1731 sizeof(session->tx_token), ++session->tx_token);
1732}
1733
1734COAP_API uint16_t
1736 uint16_t mid;
1737
1738 coap_lock_lock(session->context, return 0);
1739 mid = coap_new_message_id_lkd(session);
1740 coap_lock_unlock(session->context);
1741 return mid;
1742}
1743
1744uint16_t
1747 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1748 return ++session->tx_mid;
1749 /* TCP/TLS have no notion of mid */
1750 return 0;
1751}
1752
1753const coap_address_t *
1755 if (session)
1756 return &session->addr_info.remote;
1757 return NULL;
1758}
1759
1760const coap_address_t *
1762 if (session)
1763 return &session->addr_info.local;
1764 return NULL;
1765}
1766
1767const coap_address_t *
1769#if COAP_CLIENT_SUPPORT
1770 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
1771 session->sock.flags & COAP_SOCKET_MULTICAST)
1772 return &session->sock.mcast_addr;
1773#else /* ! COAP_CLIENT_SUPPORT */
1774 (void)session;
1775#endif /* ! COAP_CLIENT_SUPPORT */
1776 return NULL;
1777}
1778
1781 if (session)
1782 return session->context;
1783 return NULL;
1784}
1785
1788 if (session)
1789 return session->proto;
1790 return 0;
1791}
1792
1795 if (session)
1796 return session->type;
1797 return 0;
1798}
1799
1800#if COAP_CLIENT_SUPPORT
1801int
1803#if COAP_SERVER_SUPPORT
1804 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1806 session->type = COAP_SESSION_TYPE_CLIENT;
1807 return 1;
1808 }
1809#else /* ! COAP_SERVER_SUPPORT */
1810 (void)session;
1811#endif /* ! COAP_SERVER_SUPPORT */
1812 return 0;
1813}
1814#endif /* COAP_CLIENT_SUPPORT */
1815
1818 if (session)
1819 return session->state;
1820 return 0;
1821}
1822
1823int
1825 if (session)
1826 return session->ifindex;
1827 return -1;
1828}
1829
1830void *
1832 coap_tls_library_t *tls_lib) {
1833 if (session)
1834 return coap_dtls_get_tls(session, tls_lib);
1835 return NULL;
1836}
1837
1838static const char *
1840 switch (proto) {
1841 case COAP_PROTO_UDP:
1842 return "UDP ";
1843 case COAP_PROTO_DTLS:
1844 return "DTLS";
1845 case COAP_PROTO_TCP:
1846 return "TCP ";
1847 case COAP_PROTO_TLS:
1848 return "TLS ";
1849 case COAP_PROTO_WS:
1850 return "WS ";
1851 case COAP_PROTO_WSS:
1852 return "WSS ";
1853 case COAP_PROTO_NONE:
1854 case COAP_PROTO_LAST:
1855 default:
1856 return "????" ;
1857 break;
1858 }
1859 return NULL;
1860}
1861
1862#if COAP_SERVER_SUPPORT
1864coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1865 coap_endpoint_t *endpoint;
1866
1867 coap_lock_lock(context, return NULL);
1868 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
1869 coap_lock_unlock(context);
1870 return endpoint;
1871}
1872
1874coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
1875 coap_proto_t proto) {
1876 coap_endpoint_t *ep = NULL;
1877
1878 assert(context);
1879 assert(listen_addr);
1880 assert(proto != COAP_PROTO_NONE);
1881
1882 coap_lock_check_locked(context);
1883
1884 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1885 coap_log_crit("coap_new_endpoint: DTLS not supported\n");
1886 goto error;
1887 }
1888
1889 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1890 coap_log_crit("coap_new_endpoint: TLS not supported\n");
1891 goto error;
1892 }
1893
1894 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1895 coap_log_crit("coap_new_endpoint: TCP not supported\n");
1896 goto error;
1897 }
1898
1899 if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
1900 coap_log_crit("coap_new_endpoint: WS not supported\n");
1901 goto error;
1902 }
1903
1904 if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
1905 coap_log_crit("coap_new_endpoint: WSS not supported\n");
1906 goto error;
1907 }
1908
1909 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
1910 proto == COAP_PROTO_WSS) {
1912 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
1913 "coap_context_set_pki() not called\n");
1914 goto error;
1915 }
1916 }
1917
1918 ep = coap_malloc_endpoint();
1919 if (!ep) {
1920 coap_log_warn("coap_new_endpoint: malloc");
1921 goto error;
1922 }
1923
1924 memset(ep, 0, sizeof(coap_endpoint_t));
1925 ep->context = context;
1926 ep->proto = proto;
1927 ep->sock.endpoint = ep;
1928 assert(proto < COAP_PROTO_LAST);
1929 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
1930
1931 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1932 if (!coap_netif_dgrm_listen(ep, listen_addr))
1933 goto error;
1934#ifdef WITH_CONTIKI
1935 ep->sock.context = context;
1936#endif /* WITH_CONTIKI */
1937#if !COAP_DISABLE_TCP
1938 } else if (COAP_PROTO_RELIABLE(proto)) {
1939 if (!coap_netif_strm_listen(ep, listen_addr))
1940 goto error;
1941#endif /* !COAP_DISABLE_TCP */
1942 } else {
1943 coap_log_crit("coap_new_endpoint: protocol not supported\n");
1944 goto error;
1945 }
1946
1948#ifndef INET6_ADDRSTRLEN
1949#define INET6_ADDRSTRLEN 40
1950#endif
1951 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1952
1953 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1954 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
1955 addr_str);
1956 }
1957 }
1958
1960
1961#ifdef COAP_EPOLL_SUPPORT
1962 ep->sock.endpoint = ep;
1964 EPOLLIN,
1965 __func__);
1966#endif /* COAP_EPOLL_SUPPORT */
1967
1968 LL_PREPEND(context->endpoint, ep);
1969 return ep;
1970
1971error:
1973 return NULL;
1974}
1975
1976void
1978 ep->default_mtu = (uint16_t)mtu;
1979}
1980
1981COAP_API void
1983 if (ep) {
1984 coap_context_t *context = ep->context;
1985 if (context) {
1986 coap_lock_lock(context, return);
1987 }
1989 if (context) {
1990 coap_lock_unlock(context);
1991 }
1992 }
1993}
1994
1995void
1997 if (ep) {
1998 coap_session_t *session, *rtmp;
1999
2000 if (ep->context) {
2001 /* If fully allocated and inserted */
2003 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2004 assert(session->ref == 0);
2005 if (session->ref == 0) {
2007 coap_session_free(session);
2008 }
2009 }
2010 if (coap_netif_available_ep(ep)) {
2011 /*
2012 * ep->sock.endpoint is set in coap_new_endpoint().
2013 * ep->sock.session is never set.
2014 *
2015 * session->sock.session is set for both clients and servers (when a
2016 * new session is accepted), but does not affect the endpoint.
2017 *
2018 * So, it is safe to call coap_netif_close_ep() after all the sessions
2019 * have been freed above as we are only working with the endpoint sock.
2020 */
2021#ifdef COAP_EPOLL_SUPPORT
2022 assert(ep->sock.session == NULL);
2023#endif /* COAP_EPOLL_SUPPORT */
2025 }
2026
2027 if (ep->context->endpoint) {
2028 LL_DELETE(ep->context->endpoint, ep);
2029 }
2030 }
2032 }
2033}
2034#endif /* COAP_SERVER_SUPPORT */
2035
2038 const coap_address_t *remote_addr,
2039 int ifindex) {
2040 coap_session_t *s, *rtmp;
2041#if COAP_CLIENT_SUPPORT
2042 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2043 if (s->ifindex == ifindex) {
2044 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2045 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2046 return s;
2047 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2048 return s;
2049 }
2050 }
2051#endif /* COAP_CLIENT_SUPPORT */
2052#if COAP_SERVER_SUPPORT
2053 coap_endpoint_t *ep;
2054
2055 LL_FOREACH(ctx->endpoint, ep) {
2056 SESSIONS_ITER(ep->sessions, s, rtmp) {
2057 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2058 remote_addr))
2059 return s;
2060 }
2061 }
2062#endif /* COAP_SERVER_SUPPORT */
2063 return NULL;
2064}
2065
2066#ifndef INET6_ADDRSTRLEN
2067#define INET6_ADDRSTRLEN 46
2068#endif
2069const char *
2071 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2072 char *p = szSession, *end = szSession + sizeof(szSession);
2073 if (coap_print_addr(&session->addr_info.local,
2074 (unsigned char *)p, end - p) > 0)
2075 p += strlen(p);
2076 if (p + 6 < end) {
2077 strcpy(p, " <-> ");
2078 p += 5;
2079 }
2080 if (p + 1 < end) {
2081 if (coap_print_addr(&session->addr_info.remote,
2082 (unsigned char *)p, end - p) > 0)
2083 p += strlen(p);
2084 }
2085 if (session->ifindex > 0 && p + 1 < end)
2086 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2087 if (p + 6 < end) {
2088 strcpy(p, " ");
2089 p++;
2090 strcpy(p, coap_proto_name(session->proto));
2091 }
2092
2093 return szSession;
2094}
2095
2096#if COAP_SERVER_SUPPORT
2097const char *
2098coap_endpoint_str(const coap_endpoint_t *endpoint) {
2099 static char szEndpoint[128];
2100 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2101 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2102 p += strlen(p);
2103 if (p + 6 < end) {
2104 if (endpoint->proto == COAP_PROTO_UDP) {
2105 strcpy(p, " UDP");
2106 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2107 strcpy(p, " DTLS");
2108 } else {
2109 strcpy(p, " NONE");
2110 }
2111 }
2112
2113 return szEndpoint;
2114}
2115#endif /* COAP_SERVER_SUPPORT */
2116#if COAP_CLIENT_SUPPORT
2117void
2119 session->no_observe_cancel = 1;
2120}
2121#endif /* COAP_CLIENT_SUPPORT */
2122#endif /* COAP_SESSION_C_ */
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
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: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
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ 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().
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:90
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:135
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:238
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:124
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)
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition coap_tcp.c:20
#define coap_lock_unlock(c)
#define coap_lock_lock(c, failed)
#define coap_lock_callback(c, func)
#define coap_lock_check_locked(c)
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: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_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:4253
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:226
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:2467
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:1153
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:1583
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:1050
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1076
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition coap_net.c:255
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:2512
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:50
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.
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition coap_dtls.h:349
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition coap_dtls.h:443
coap_tls_library_t
Definition coap_dtls.h:112
@ 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:397
#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:1469
#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:760
#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:179
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)
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)
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)
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.
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.
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:928
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
coap_mid_t coap_session_send_ping_lkd(coap_session_t *session)
Send a ping message for the session.
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
void coap_session_free(coap_session_t *session)
void coap_session_mfree(coap_session_t *session)
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
coap_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)
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 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.
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.
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
#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).
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
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.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
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 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_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:211
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_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.
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:419
coap_bin_const_t identity
Definition coap_dtls.h:418
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:448
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition coap_dtls.h:477
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:354
uint8_t version
Definition coap_dtls.h:355
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.
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_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_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