libcoap 4.3.5
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_lkd(&ran, sizeof(ran));
93 res = coap_multi_fixed_uint(res, ran);
94 res = coap_div_fixed_uint(res, 0xff);
95 res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res);
96 return res;
97}
98
104
105 return ticks;
106}
107
108/*
109 * Save away derived Congestion Control parameters for speed of access.
110 * They will get updated whenever a component variable is updated.
111 */
112
113/*
114 * NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
115 * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM
116 *
117 * Do not include NON_TIMEOUT_RANDOM as that changes
118 */
119static void
120coap_session_fix_non_probing_wait_base(coap_session_t *s) {
122
124 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
128}
129
130/*
131 * NON_PARTIAL_TIMEOUT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
132 * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT
133 */
134static void
135coap_session_fix_non_partial_timeout(coap_session_t *s) {
137
139 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
144}
145#endif /* COAP_Q_BLOCK_SUPPORT */
146
147void
149 if (value.integer_part > 0 && value.fractional_part < 1000) {
150 session->ack_timeout = value;
151 coap_log_debug("***%s: session ack_timeout set to %u.%03u\n",
152 coap_session_str(session), session->ack_timeout.integer_part,
154 }
155}
156
157void
159 coap_fixed_point_t value) {
160 if (value.integer_part > 0 && value.fractional_part < 1000) {
161 session->ack_random_factor = value;
162 coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n",
165#if COAP_Q_BLOCK_SUPPORT
166 coap_session_fix_non_probing_wait_base(session);
167 coap_session_fix_non_partial_timeout(session);
168#endif /* COAP_Q_BLOCK_SUPPORT */
169 }
170 return;
171}
172
173void
175 if (value > 0) {
176 session->max_retransmit = value;
177 coap_log_debug("***%s: session max_retransmit set to %u\n",
178 coap_session_str(session), session->max_retransmit);
179 }
180}
181
182void
183coap_session_set_nstart(coap_session_t *session, uint16_t value) {
184 if (value > 0) {
185 session->nstart = value;
186 coap_log_debug("***%s: session nstart set to %u\n",
187 coap_session_str(session), session->nstart);
188 }
189}
190
191void
193 coap_fixed_point_t value) {
194 if (value.integer_part > 0 && value.fractional_part < 1000) {
195 session->default_leisure = value;
196 coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
199 }
200}
201
202void
204 if (value > 0) {
205 session->probing_rate = value;
206 coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
207 coap_session_str(session), session->probing_rate);
208 }
209}
210
211void
213#if COAP_Q_BLOCK_SUPPORT
214 if (value > 0) {
215 session->max_payloads = value;
216 coap_log_debug("***%s: session max_payloads set to %u\n",
217 coap_session_str(session), session->max_payloads);
218 coap_session_fix_non_probing_wait_base(session);
219 coap_session_fix_non_partial_timeout(session);
220 }
221#else /* ! COAP_Q_BLOCK_SUPPORT */
222 (void)session;
223 (void)value;
224#endif /* ! COAP_Q_BLOCK_SUPPORT */
225}
226
227void
229#if COAP_Q_BLOCK_SUPPORT
230 if (value > 0) {
231 session->non_max_retransmit = value;
232 coap_log_debug("***%s: session non_max_retransmit set to %u\n",
233 coap_session_str(session), session->non_max_retransmit);
234 coap_session_fix_non_probing_wait_base(session);
235 coap_session_fix_non_partial_timeout(session);
236 }
237#else /* ! COAP_Q_BLOCK_SUPPORT */
238 (void)session;
239 (void)value;
240#endif /* ! COAP_Q_BLOCK_SUPPORT */
241}
242
243void
245 coap_fixed_point_t value) {
246#if COAP_Q_BLOCK_SUPPORT
247 if (value.integer_part > 0 && value.fractional_part < 1000) {
248 session->non_timeout = value;
249 coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
250 coap_session_str(session), session->non_timeout.integer_part,
251 session->non_timeout.fractional_part);
252 coap_session_fix_non_probing_wait_base(session);
253 coap_session_fix_non_partial_timeout(session);
254 }
255#else /* ! COAP_Q_BLOCK_SUPPORT */
256 (void)session;
257 (void)value;
258#endif /* ! COAP_Q_BLOCK_SUPPORT */
259}
260
261void
263 coap_fixed_point_t value) {
264#if COAP_Q_BLOCK_SUPPORT
265 if (value.integer_part > 0 && value.fractional_part < 1000)
266 session->non_receive_timeout = value;
267 coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
268 coap_session_str(session),
269 session->non_receive_timeout.integer_part,
270 session->non_receive_timeout.fractional_part);
271#else /* ! COAP_Q_BLOCK_SUPPORT */
272 (void)session;
273 (void)value;
274#endif /* ! COAP_Q_BLOCK_SUPPORT */
275}
276
279 return session->ack_timeout;
280}
281
284 return session->ack_random_factor;
285}
286
287uint16_t
289 return session->max_retransmit;
290}
291
292uint16_t
294 return session->nstart;
295}
296
299 return session->default_leisure;
300}
301
302uint32_t
304 return session->probing_rate;
305}
306
307uint16_t
309#if COAP_Q_BLOCK_SUPPORT
310 return session->max_payloads;
311#else /* ! COAP_Q_BLOCK_SUPPORT */
312 (void)session;
314#endif /* ! COAP_Q_BLOCK_SUPPORT */
315}
316
317uint16_t
319#if COAP_Q_BLOCK_SUPPORT
320 return session->non_max_retransmit;
321#else /* ! COAP_Q_BLOCK_SUPPORT */
322 (void)session;
324#endif /* ! COAP_Q_BLOCK_SUPPORT */
325}
326
329#if COAP_Q_BLOCK_SUPPORT
330 return session->non_timeout;
331#else /* ! COAP_Q_BLOCK_SUPPORT */
332 (void)session;
334#endif /* ! COAP_Q_BLOCK_SUPPORT */
335}
336
339#if COAP_Q_BLOCK_SUPPORT
340 return session->non_receive_timeout;
341#else /* ! COAP_Q_BLOCK_SUPPORT */
342 (void)session;
344#endif /* ! COAP_Q_BLOCK_SUPPORT */
345}
346
349 coap_lock_lock(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 (void)context;
367#endif /* COAP_THREAD_SAFE */
368
369 coap_lock_lock(context, return);
371 coap_lock_unlock(context);
372 }
373}
374
375void
377 if (session) {
379#ifndef __COVERITY__
380 assert(session->ref > 0);
381 if (session->ref > 0)
382 --session->ref;
383 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
384 coap_session_free(session);
385#else /* __COVERITY__ */
386 /* Coverity scan is fooled by the reference counter leading to
387 * false positives for USE_AFTER_FREE. */
388 --session->ref;
389 __coverity_negative_sink__(session->ref);
390 /* Indicate that resources are released properly. */
391 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
392 __coverity_free__(session);
393 }
394#endif /* __COVERITY__ */
395 }
396}
397
398void
399coap_session_set_app_data(coap_session_t *session, void *app_data) {
400 assert(session);
401 session->app = app_data;
402}
403
404void *
406 assert(session);
407 return session->app;
408}
409
410static coap_session_t *
412 const coap_addr_hash_t *addr_hash,
413 const coap_address_t *local_addr,
414 const coap_address_t *remote_addr, int ifindex,
415 coap_context_t *context, coap_endpoint_t *endpoint) {
417 sizeof(coap_session_t));
418#if ! COAP_SERVER_SUPPORT
419 (void)endpoint;
420#endif /* ! COAP_SERVER_SUPPORT */
421 if (!session)
422 return NULL;
423 memset(session, 0, sizeof(*session));
424 session->proto = proto;
425 session->type = type;
426 if (addr_hash)
427 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
428 else
429 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
430 if (local_addr)
431 coap_address_copy(&session->addr_info.local, local_addr);
432 else
434 if (remote_addr)
435 coap_address_copy(&session->addr_info.remote, remote_addr);
436 else
438 session->ifindex = ifindex;
439 session->context = context;
440#if COAP_SERVER_SUPPORT
441 session->endpoint = endpoint;
442 if (endpoint)
443 session->mtu = endpoint->default_mtu;
444 else
445#endif /* COAP_SERVER_SUPPORT */
446 session->mtu = COAP_DEFAULT_MTU;
447 session->block_mode = context->block_mode;
448 if (proto == COAP_PROTO_DTLS) {
449 session->tls_overhead = 29;
450 if (session->tls_overhead >= session->mtu) {
451 session->tls_overhead = session->mtu;
452 coap_log_err("DTLS overhead exceeds MTU\n");
453 }
454 }
458 session->nstart = COAP_DEFAULT_NSTART;
461#if COAP_Q_BLOCK_SUPPORT
462 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
463 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
464 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
465 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
466 coap_session_fix_non_probing_wait_base(session);
467 coap_session_fix_non_partial_timeout(session);
468#endif /* COAP_Q_BLOCK_SUPPORT */
469 session->dtls_event = -1;
474 session->max_token_size = context->max_token_size; /* RFC8974 */
475 if (session->type != COAP_SESSION_TYPE_CLIENT)
477
478 /* Randomly initialize */
479 /* TCP/TLS have no notion of mid */
480 if (COAP_PROTO_NOT_RELIABLE(session->proto))
481 coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
482 coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
483
484 return session;
485}
486
487void
489 coap_queue_t *q, *tmp;
490 coap_lg_xmit_t *lq, *ltmp;
491
492#if COAP_CLIENT_SUPPORT
493 coap_lg_crcv_t *lg_crcv, *etmp;
494
495 /* Need to do this before (D)TLS and socket is closed down */
496 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
497 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
498 /* Need to close down observe */
499 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
500 /* Need to delete node we set up for NON */
501 coap_queue_t *queue = session->context->sendqueue;
502
503 while (queue) {
504 if (queue->session == session) {
506 break;
507 }
508 queue = queue->next;
509 }
510 }
511 }
512 LL_DELETE(session->lg_crcv, lg_crcv);
513 coap_block_delete_lg_crcv(session, lg_crcv);
514 }
515#endif /* COAP_CLIENT_SUPPORT */
516
517 if (session->partial_pdu)
519 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
520 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
521 if (session->psk_identity)
523 if (session->psk_key)
525 if (session->psk_hint)
527
528#if COAP_SERVER_SUPPORT
529 coap_cache_entry_t *cp, *ctmp;
530 HASH_ITER(hh, session->context->cache, cp, ctmp) {
531 /* cp->session is NULL if not session based */
532 if (cp->session == session) {
533 coap_delete_cache_entry(session->context, cp);
534 }
535 }
536#endif /* COAP_SERVER_SUPPORT */
537 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
538 if (q->pdu->type==COAP_MESSAGE_CON && session->context->nack_handler) {
539 coap_check_update_token(session, q->pdu);
541 session->context->nack_handler(session, q->pdu,
542 session->proto == COAP_PROTO_DTLS ?
544 q->id));
545 }
547 }
548 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
549 LL_DELETE(session->lg_xmit, lq);
550 coap_block_delete_lg_xmit(session, lq);
551 }
552#if COAP_SERVER_SUPPORT
553 coap_lg_srcv_t *sq, *stmp;
554
555 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
556 LL_DELETE(session->lg_srcv, sq);
557 coap_block_delete_lg_srcv(session, sq);
558 }
559#endif /* COAP_SERVER_SUPPORT */
560#if COAP_OSCORE_SUPPORT
562#endif /* COAP_OSCORE_SUPPORT */
563#if COAP_WS_SUPPORT
564 coap_free_type(COAP_STRING, session->ws);
565 coap_delete_str_const(session->ws_host);
566#endif /* COAP_WS_SUPPORT */
567}
568
569void
571 if (!session)
572 return;
574 assert(session->ref == 0);
575 if (session->ref)
576 return;
577 /* Make sure nothing gets deleted under our feet */
579 coap_session_mfree(session);
580#if COAP_SERVER_SUPPORT
582 if (session->endpoint) {
583 if (session->endpoint->sessions)
584 SESSIONS_DELETE(session->endpoint->sessions, session);
585 } else
586#endif /* COAP_SERVER_SUPPORT */
587#if COAP_CLIENT_SUPPORT
588 if (session->context) {
589 if (session->context->sessions)
590 SESSIONS_DELETE(session->context->sessions, session);
591 }
592#endif /* COAP_CLIENT_SUPPORT */
594 coap_delete_bin_const(session->echo);
595 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
596 (void *)session);
597
598 assert(session->ref == 1);
600}
601
602static size_t
604 size_t max_with_header) {
605#if COAP_DISABLE_TCP
606 (void)session;
607 return max_with_header > 4 ? max_with_header - 4 : 0;
608#else /* !COAP_DISABLE_TCP */
609 if (COAP_PROTO_NOT_RELIABLE(session->proto))
610 return max_with_header > 4 ? max_with_header - 4 : 0;
611 /* we must assume there is no token to be on the safe side */
612 if (max_with_header <= 2)
613 return 0;
614 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
615 return max_with_header - 2;
616 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
617 return max_with_header - 3;
618 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
619 return max_with_header - 4;
620 else
621 return max_with_header - 6;
622#endif /* !COAP_DISABLE_TCP */
623}
624
625size_t
627 if (session->csm_rcv_mtu)
629 (size_t)(session->csm_rcv_mtu));
630
632 (size_t)(session->mtu - session->tls_overhead));
633}
634
635COAP_API size_t
637 size_t size;
638 coap_session_t *session_rw;
639
640 /*
641 * Need to do this to not get a compiler warning about const parameters
642 * but need to maintain source code backward compatibility
643 */
644 memcpy(&session_rw, &session, sizeof(session_rw));
645 coap_lock_lock(session_rw->context, return 0);
646 size = coap_session_max_pdu_size_lkd(session_rw);
647 coap_lock_unlock(session_rw->context);
648 return size;
649}
650
651size_t
653 size_t max_with_header;
654
656#if COAP_CLIENT_SUPPORT
657 /*
658 * Delay if session->doing_first is set.
659 * E.g. Reliable and CSM not in yet for checking block support
660 */
661 coap_session_t *session_rw;
662
663 /*
664 * Need to do this to not get a compiler warning about const parameters
665 * but need to maintain source code backward compatibility
666 */
667 memcpy(&session_rw, &session, sizeof(session_rw));
668 if (coap_client_delay_first(session_rw) == 0) {
669 coap_log_debug("coap_client_delay_first: timeout\n");
670 /* Have to go with the defaults */
671 }
672#endif /* COAP_CLIENT_SUPPORT */
673
674 max_with_header = (size_t)(session->mtu - session->tls_overhead);
675
676 return coap_session_max_pdu_size_internal(session, max_with_header);
677}
678
679void
680coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
681#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
682 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
683 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
684#endif
685 if (mtu < 64)
686 mtu = 64;
687 session->mtu = mtu;
688 if (session->tls_overhead >= session->mtu) {
689 session->tls_overhead = session->mtu;
690 coap_log_err("DTLS overhead exceeds MTU\n");
691 }
692}
693
694ssize_t
696 coap_queue_t *node) {
697 if (node) {
698 coap_queue_t *removed = NULL;
699 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
700 assert(removed == node);
702 node->session = NULL;
703 node->t = 0;
704 } else {
705 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
706 coap_queue_t *q = NULL;
707 /* Check same mid is not getting re-used in violation of RFC7252 */
708 LL_FOREACH(session->delayqueue, q) {
709 if (q->id == pdu->mid) {
710 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
711 coap_session_str(session), pdu->mid);
712 return COAP_INVALID_MID;
713 }
714 }
715 }
716 node = coap_new_node();
717 if (node == NULL)
718 return COAP_INVALID_MID;
719 node->id = pdu->mid;
720 node->pdu = pdu;
721 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
722 uint8_t r;
723 coap_prng_lkd(&r, sizeof(r));
724 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
725 node->timeout = coap_calc_timeout(session, r);
726 }
727 }
728 LL_APPEND(session->delayqueue, node);
729 coap_log_debug("** %s: mid=0x%04x: delayed\n",
730 coap_session_str(session), node->id);
731 return COAP_PDU_DELAYED;
732}
733
734#if !COAP_DISABLE_TCP
735void
737 coap_pdu_t *pdu;
738 uint8_t buf[4];
739 assert(COAP_PROTO_RELIABLE(session->proto));
740 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
741 session->state = COAP_SESSION_STATE_CSM;
742 session->partial_write = 0;
743 if (session->mtu == 0)
744 session->mtu = COAP_DEFAULT_MTU; /* base value */
746 if (pdu == NULL
748 coap_encode_var_safe(buf, sizeof(buf),
749 session->context->csm_max_message_size), buf) == 0
751 coap_encode_var_safe(buf, sizeof(buf),
752 0), buf) == 0
753 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
756 coap_encode_var_safe(buf, sizeof(buf),
757 session->max_token_size),
758 buf) == 0)
759 || coap_pdu_encode_header(pdu, session->proto) == 0
760 ) {
762 } else {
763 ssize_t bytes_written;
764
765 pdu->session = session;
766 bytes_written = coap_session_send_pdu(session, pdu);
767 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
769 } else {
770 session->csm_rcv_mtu = session->context->csm_max_message_size;
771 if (session->csm_rcv_mtu > COAP_BERT_BASE)
772 session->csm_bert_loc_support = 1;
773 else
774 session->csm_bert_loc_support = 0;
775 }
776 }
777 if (pdu)
778 coap_delete_pdu(pdu);
779}
780#endif /* !COAP_DISABLE_TCP */
781
784 coap_mid_t mid;
785
786 coap_lock_lock(session->context, return COAP_INVALID_MID);
787 mid = coap_session_send_ping_lkd(session);
788 coap_lock_unlock(session->context);
789 return mid;
790}
791
794 coap_pdu_t *ping = NULL;
795
797 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
798 session->con_active)
799 return COAP_INVALID_MID;
800 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
801 uint16_t mid = coap_new_message_id_lkd(session);
802 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
803 }
804#if !COAP_DISABLE_TCP
805 else {
807 }
808#endif /* !COAP_DISABLE_TCP */
809 if (!ping)
810 return COAP_INVALID_MID;
811 return coap_send_internal(session, ping);
812}
813
814void
816 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
817 coap_log_debug("***%s: session connected\n",
818 coap_session_str(session));
819 if (session->state == COAP_SESSION_STATE_CSM) {
821 if (session->doing_first)
822 session->doing_first = 0;
823 }
824 }
825
827 session->partial_write = 0;
828
829 if (session->proto==COAP_PROTO_DTLS) {
830 session->tls_overhead = coap_dtls_get_overhead(session);
831 if (session->tls_overhead >= session->mtu) {
832 session->tls_overhead = session->mtu;
833 coap_log_err("DTLS overhead exceeds MTU\n");
834 }
835 }
836
837 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
838 ssize_t bytes_written;
839 coap_queue_t *q = session->delayqueue;
840 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
841 if (session->con_active >= COAP_NSTART(session))
842 break;
843 session->con_active++;
844 }
845 /* Take entry off the queue */
846 session->delayqueue = q->next;
847 q->next = NULL;
848
849 coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
850 coap_session_str(session), (int)q->pdu->mid);
851 bytes_written = coap_session_send_pdu(session, q->pdu);
852 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
853 if (coap_wait_ack(session->context, session, q) >= 0)
854 q = NULL;
855 }
856 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
857 if (q)
859 if (bytes_written < 0)
860 break;
861 } else if (q) {
862 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
863 q->next = session->delayqueue;
864 session->delayqueue = q;
865 if (bytes_written > 0)
866 session->partial_write = (size_t)bytes_written;
867 break;
868 } else {
870 }
871 }
872 }
873}
874
875#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
876static const char *
878 switch (reason) {
880 return "COAP_NACK_TOO_MANY_RETRIES";
882 return "COAP_NACK_NOT_DELIVERABLE";
883 case COAP_NACK_RST:
884 return "COAP_NACK_RST";
886 return "COAP_NACK_TLS_FAILED";
888 return "COAP_NACK_ICMP_ISSUE";
890 return "COAP_NACK_BAD_RESPONSE";
892 return "COAP_NACK_TLS_LAYER_FAILED";
894 return "COAP_NACK_WS_LAYER_FAILED";
896 return "COAP_NACK_WS_FAILED";
897 default:
898 return "???";
899 }
900}
901#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
902
903COAP_API void
905 coap_lock_lock(session->context, return);
906 coap_session_disconnected_lkd(session, reason);
907 coap_lock_unlock(session->context);
908}
909
910void
912#if !COAP_DISABLE_TCP
913 coap_session_state_t state = session->state;
914#endif /* !COAP_DISABLE_TCP */
915 coap_lg_xmit_t *lq, *ltmp;
916#if COAP_SERVER_SUPPORT
917 coap_lg_srcv_t *sq, *stmp;
918#endif /* COAP_SERVER_SUPPORT */
919#if COAP_CLIENT_SUPPORT
920 coap_lg_crcv_t *cq, *etmp;
921#endif /* COAP_CLIENT_SUPPORT */
922
924 if (session->context->nack_handler) {
925 int sent_nack = 0;
926 coap_queue_t *q = session->context->sendqueue;
927
928 while (q) {
929 if (q->session == session) {
930 /* Take the first one */
932
933 coap_check_update_token(session, q->pdu);
935 session->context->nack_handler(session, q->pdu, reason, q->id));
936 coap_update_token(q->pdu, token.length, token.s);
937 sent_nack = 1;
938 break;
939 }
940 q = q->next;
941 }
942
943 if (reason != COAP_NACK_ICMP_ISSUE) {
944 while (session->delayqueue) {
945 q = session->delayqueue;
946 session->delayqueue = q->next;
947 q->next = NULL;
948 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
949 coap_session_str(session), q->id);
950 if (q->pdu->type == COAP_MESSAGE_CON) {
951 coap_check_update_token(session, q->pdu);
953 session->context->nack_handler(session, q->pdu, reason, q->id));
954 sent_nack = 1;
955 }
957 }
958 }
959#if COAP_CLIENT_SUPPORT
960 if (!sent_nack && session->lg_crcv) {
961 /* Take the first one */
963 session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
964 session->lg_crcv->pdu.mid));
965 sent_nack = 1;
966 }
967#endif /* COAP_CLIENT_SUPPORT */
968 if (!sent_nack) {
969 /* Unable to determine which request disconnection was for */
971 session->context->nack_handler(session, NULL, reason, 0));
972 }
973 }
974 if (reason == COAP_NACK_ICMP_ISSUE) {
975 coap_log_debug("***%s: session ICMP issue (%s)\n",
976 coap_session_str(session), coap_nack_name(reason));
977 return;
978 }
979 coap_log_debug("***%s: session disconnected (%s)\n",
980 coap_session_str(session), coap_nack_name(reason));
981#if COAP_SERVER_SUPPORT
982 coap_delete_observers(session->context, session);
983#endif /* COAP_SERVER_SUPPORT */
984
985 if (session->proto == COAP_PROTO_UDP)
987 else
989
990 session->con_active = 0;
991
992 if (session->partial_pdu) {
994 session->partial_pdu = NULL;
995 }
996 session->partial_read = 0;
997
998 /* Not done if nack handler called above */
999 while (session->delayqueue) {
1000 coap_queue_t *q = session->delayqueue;
1001 session->delayqueue = q->next;
1002 q->next = NULL;
1003 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1004 coap_session_str(session), q->id);
1006 }
1007
1008#if COAP_CLIENT_SUPPORT
1009 /* Need to do this before (D)TLS and socket is closed down */
1010 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1011 LL_DELETE(session->lg_crcv, cq);
1012 coap_block_delete_lg_crcv(session, cq);
1013 }
1014#endif /* COAP_CLIENT_SUPPORT */
1015 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1016 LL_DELETE(session->lg_xmit, lq);
1017 coap_block_delete_lg_xmit(session, lq);
1018 }
1019#if COAP_SERVER_SUPPORT
1020 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1021 LL_DELETE(session->lg_srcv, sq);
1022 coap_block_delete_lg_srcv(session, sq);
1023 }
1024#endif /* COAP_SERVER_SUPPORT */
1025 coap_cancel_session_messages(session->context, session, reason);
1026
1027#if !COAP_DISABLE_TCP
1028 if (COAP_PROTO_RELIABLE(session->proto)) {
1029 if (coap_netif_available(session)) {
1033 }
1034 if (state != COAP_SESSION_STATE_NONE) {
1038 }
1039 if (session->doing_first)
1040 session->doing_first = 0;
1041 }
1042#endif /* !COAP_DISABLE_TCP */
1043 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1044}
1045
1046#if COAP_SERVER_SUPPORT
1047static void
1048coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1049 const coap_addr_tuple_t *addr_info) {
1050 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1051 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1052 addr_hash->lport = coap_address_get_port(&addr_info->local);
1053 addr_hash->proto = proto;
1054}
1055
1058 const coap_packet_t *packet, coap_tick_t now) {
1059 coap_session_t *session;
1060 coap_session_t *rtmp;
1061 unsigned int num_idle = 0;
1062 unsigned int num_hs = 0;
1063 coap_session_t *oldest = NULL;
1064 coap_session_t *oldest_hs = NULL;
1065 coap_addr_hash_t addr_hash;
1066
1067 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1068 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1069 if (session) {
1070 /* Maybe mcast or unicast IP address which is not in the hash */
1071 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1072 session->ifindex = packet->ifindex;
1073 session->last_rx_tx = now;
1074 return session;
1075 }
1076
1077#if COAP_CLIENT_SUPPORT
1078 if (coap_is_mcast(&packet->addr_info.local)) {
1079 /* Check if this a proxy client packet we sent on another socket */
1080 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1081 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1084 /* Drop looped back packet to stop recursion / confusion */
1085 return NULL;
1086 }
1087 }
1088 }
1089#endif /* COAP_CLIENT_SUPPORT */
1090 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1091 if (session->ref == 0 && session->delayqueue == NULL) {
1092 if (session->type == COAP_SESSION_TYPE_SERVER) {
1093 ++num_idle;
1094 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1095 oldest = session;
1096
1097 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1098 ++num_hs;
1099 /* See if this is a partial (D)TLS session set up
1100 which needs to be cleared down to prevent DOS */
1101 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1102 if (oldest_hs == NULL ||
1103 session->last_rx_tx < oldest_hs->last_rx_tx)
1104 oldest_hs = session;
1105 }
1106 }
1107 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1108 ++num_hs;
1109 /* See if this is a partial (D)TLS session set up for Client Hello
1110 which needs to be cleared down to prevent DOS */
1111 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1112 if (oldest_hs == NULL ||
1113 session->last_rx_tx < oldest_hs->last_rx_tx)
1114 oldest_hs = session;
1115 }
1116 }
1117 }
1118 }
1119
1120 if (endpoint->context->max_idle_sessions > 0 &&
1121 num_idle >= endpoint->context->max_idle_sessions) {
1123 coap_session_free(oldest);
1124 } else if (oldest_hs) {
1125 coap_log_warn("***%s: Incomplete session timed out\n",
1126 coap_session_str(oldest_hs));
1128 coap_session_free(oldest_hs);
1129 }
1130
1131 if (num_hs > (endpoint->context->max_handshake_sessions ?
1132 endpoint->context->max_handshake_sessions :
1134 /* Maxed out on number of sessions in (D)TLS negotiation state */
1135 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1136 "large. New request ignored\n");
1137 return NULL;
1138 }
1139
1140 if (endpoint->proto == COAP_PROTO_DTLS) {
1141 /*
1142 * Need to check that this actually is a Client Hello before wasting
1143 * time allocating and then freeing off session.
1144 */
1145
1146 /*
1147 * Generic header structure of the DTLS record layer.
1148 * typedef struct __attribute__((__packed__)) {
1149 * uint8_t content_type; content type of the included message
1150 * uint16_t version; Protocol version
1151 * uint16_t epoch; counter for cipher state changes
1152 * uint8_t sequence_number[6]; sequence number
1153 * uint16_t length; length of the following fragment
1154 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1155 * } dtls_record_handshake_t;
1156 */
1157#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1158#define DTLS_CT_ALERT 21 /* Content Type Alert */
1159#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1160#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1161#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1162#define DTLS_CT_CID 25 /* Content Type Connection ID */
1163#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1164#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1165
1166 const uint8_t *payload = (const uint8_t *)packet->payload;
1167 size_t length = packet->length;
1168 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1169 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1170 payload[OFF_CONTENT_TYPE], length,
1171 OFF_HANDSHAKE_TYPE + 1);
1172 return NULL;
1173 }
1174 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1175 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1176 /* Client may have changed its IP address */
1177 int changed = 0;
1178
1179 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1180 if (session->client_cid) {
1181 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1182 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1183 session->client_cid->length) == 0) ||
1184 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1185 memcmp(session->client_cid->s, &payload[OFF_CID],
1186 session->client_cid->length) == 0)) {
1187 /* Updating IP address */
1188 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1189 SESSIONS_DELETE(endpoint->sessions, session);
1190 session->addr_info = packet->addr_info;
1191 memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash));
1192 SESSIONS_ADD(endpoint->sessions, session);
1193 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1194 return session;
1195 }
1196 }
1197 }
1198 if (!changed) {
1199 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1200 return NULL;
1201 }
1202 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1203 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1204 /* only log if not a late alert */
1205 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1206 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1207 payload[OFF_CONTENT_TYPE],
1208 payload[OFF_HANDSHAKE_TYPE]);
1209 return NULL;
1210 }
1211 }
1212
1214 &addr_hash, &packet->addr_info.local,
1215 &packet->addr_info.remote,
1216 packet->ifindex, endpoint->context, endpoint);
1217 if (session) {
1218 session->last_rx_tx = now;
1219 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1220 sizeof(session->sock.lfunc));
1221 if (endpoint->proto == COAP_PROTO_UDP)
1223 else if (endpoint->proto == COAP_PROTO_DTLS) {
1224 session->type = COAP_SESSION_TYPE_HELLO;
1225 }
1226 SESSIONS_ADD(endpoint->sessions, session);
1227 coap_log_debug("***%s: session %p: new incoming session\n",
1228 coap_session_str(session), (void *)session);
1230 }
1231 return session;
1232}
1233
1236 coap_tick_t now) {
1237 if (session) {
1238 session->last_rx_tx = now;
1239 session->type = COAP_SESSION_TYPE_SERVER;
1240 coap_dtls_establish(session);
1241 }
1242 return session;
1243}
1244#endif /* COAP_SERVER_SUPPORT */
1245
1246#if COAP_CLIENT_SUPPORT
1247static coap_session_t *
1248coap_session_create_client(coap_context_t *ctx,
1249 const coap_address_t *local_if,
1250 const coap_address_t *server,
1251 coap_proto_t proto) {
1252 coap_session_t *session = NULL;
1253 int default_port = COAP_DEFAULT_PORT;
1254
1255 assert(server);
1256
1257 switch (proto) {
1258 case COAP_PROTO_UDP:
1259 default_port = COAP_DEFAULT_PORT;
1260 break;
1261 case COAP_PROTO_DTLS:
1262 if (!coap_dtls_is_supported()) {
1263 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1264 return NULL;
1265 }
1266 default_port = COAPS_DEFAULT_PORT;
1267 break;
1268 case COAP_PROTO_TCP:
1269 if (!coap_tcp_is_supported()) {
1270 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1271 return NULL;
1272 }
1273 default_port = COAP_DEFAULT_PORT;
1274 break;
1275 case COAP_PROTO_TLS:
1276 if (!coap_tls_is_supported()) {
1277 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1278 return NULL;
1279 }
1280 default_port = COAPS_DEFAULT_PORT;
1281 break;
1282 case COAP_PROTO_WS:
1283 if (!coap_ws_is_supported()) {
1284 coap_log_crit("coap_new_client_session*: WS not supported\n");
1285 return NULL;
1286 }
1287 default_port = 80;
1288 break;
1289 case COAP_PROTO_WSS:
1290 if (!coap_wss_is_supported()) {
1291 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1292 return NULL;
1293 }
1294 default_port = 443;
1295 break;
1296 case COAP_PROTO_NONE:
1297 case COAP_PROTO_LAST:
1298 default:
1299 assert(0);
1300 return NULL;
1301 }
1302 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1303 local_if, server, 0, ctx, NULL);
1304 if (!session)
1305 goto error;
1306
1308 session->sock.session = session;
1309 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1310 sizeof(session->sock.lfunc));
1311
1312 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1313 coap_session_t *s, *rtmp;
1314 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1315 goto error;
1316 }
1317 /* Check that this is not a duplicate 4-tuple */
1318 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1321 &s->addr_info.local) &&
1323 &s->addr_info.remote)) {
1324 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1325 coap_session_str(session), (void *)session);
1326 goto error;
1327 }
1328 }
1329#ifdef WITH_CONTIKI
1330 session->sock.context = ctx;
1331#endif /* WITH_CONTIKI */
1332#if !COAP_DISABLE_TCP
1333 } else if (COAP_PROTO_RELIABLE(proto)) {
1334 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1335 goto error;
1336 }
1337#endif /* !COAP_DISABLE_TCP */
1338 }
1339
1340#ifdef COAP_EPOLL_SUPPORT
1341 session->sock.session = session;
1342 coap_epoll_ctl_add(&session->sock,
1343 EPOLLIN |
1344 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1345 EPOLLOUT : 0),
1346 __func__);
1347#endif /* COAP_EPOLL_SUPPORT */
1348
1350 if (local_if)
1351 session->sock.flags |= COAP_SOCKET_BOUND;
1352#if COAP_SERVER_SUPPORT
1353 if (ctx->proxy_uri_resource)
1354 session->proxy_session = 1;
1355#endif /* COAP_SERVER_SUPPORT */
1356 SESSIONS_ADD(ctx->sessions, session);
1357 return session;
1358
1359error:
1360 /*
1361 * Need to add in the session as coap_session_release_lkd()
1362 * will call SESSIONS_DELETE in coap_session_free().
1363 */
1364 if (session)
1365 SESSIONS_ADD(ctx->sessions, session);
1366 coap_session_release_lkd(session);
1367 return NULL;
1368}
1369
1370static void
1371coap_session_check_connect(coap_session_t *session) {
1372 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1373 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1374 }
1375#if !COAP_DISABLE_TCP
1376 if (COAP_PROTO_RELIABLE(session->proto)) {
1377 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1379 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1380 session->state != COAP_SESSION_STATE_NONE &&
1381 session->type == COAP_SESSION_TYPE_CLIENT) {
1382 session->doing_first = 1;
1383 }
1384 } else {
1385 /* Initial connect worked immediately */
1386 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1387 }
1388 }
1389#endif /* !COAP_DISABLE_TCP */
1390 coap_ticks(&session->last_rx_tx);
1391}
1392#endif /* COAP_CLIENT_SUPPORT */
1393
1394void
1396 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1397 coap_session_connected(session);
1398#if !COAP_DISABLE_TCP
1399 if (COAP_PROTO_RELIABLE(session->proto))
1400 coap_session_send_csm(session);
1401#endif /* !COAP_DISABLE_TCP */
1402}
1403
1404#if COAP_CLIENT_SUPPORT
1407 const coap_address_t *local_if,
1408 const coap_address_t *server,
1409 coap_proto_t proto) {
1410 coap_session_t *session;
1411
1412 coap_lock_lock(ctx, return NULL);
1413 session = coap_new_client_session_lkd(ctx, local_if, server, proto);
1414 coap_lock_unlock(ctx);
1415 return session;
1416}
1417
1420 const coap_address_t *local_if,
1421 const coap_address_t *server,
1422 coap_proto_t proto) {
1423 coap_session_t *session;
1424
1426 session = coap_session_create_client(ctx, local_if, server,
1427 proto);
1428 if (session) {
1429 coap_log_debug("***%s: session %p: created outgoing session\n",
1430 coap_session_str(session), (void *)session);
1431 coap_session_check_connect(session);
1432 }
1433 return session;
1434}
1435
1438 const coap_address_t *local_if,
1439 const coap_address_t *server,
1440 coap_proto_t proto, const char *identity,
1441 const uint8_t *key, unsigned key_len) {
1442 coap_session_t *session;
1443
1444 coap_lock_lock(ctx, return NULL);
1445 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1446 coap_lock_unlock(ctx);
1447 return session;
1448}
1449
1452 const coap_address_t *local_if,
1453 const coap_address_t *server,
1454 coap_proto_t proto, const char *identity,
1455 const uint8_t *key, unsigned key_len) {
1456 coap_dtls_cpsk_t setup_data;
1457
1459 memset(&setup_data, 0, sizeof(setup_data));
1461
1462 if (identity) {
1463 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1464 setup_data.psk_info.identity.length = strlen(identity);
1465 }
1466
1467 if (key && key_len > 0) {
1468 setup_data.psk_info.key.s = key;
1469 setup_data.psk_info.key.length = key_len;
1470 }
1471
1472 return coap_new_client_session_psk2_lkd(ctx, local_if, server,
1473 proto, &setup_data);
1474}
1475
1476/*
1477 * Check the validity of the SNI to send to the server.
1478 *
1479 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1480 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1481 */
1482static void
1483coap_sanitize_client_sni(char **client_sni) {
1484 char *cp;
1485
1486 if (*client_sni == NULL)
1487 return;
1488
1489 cp = *client_sni;
1490 switch (*cp) {
1491 case '0':
1492 case '1':
1493 case '2':
1494 case '3':
1495 case '4':
1496 case '5':
1497 case '6':
1498 case '7':
1499 case '8':
1500 case '9':
1501 case 'a':
1502 case 'b':
1503 case 'c':
1504 case 'd':
1505 case 'e':
1506 case 'f':
1507 case 'A':
1508 case 'B':
1509 case 'C':
1510 case 'D':
1511 case 'E':
1512 case 'F':
1513 case ':':
1514 break;
1515 case '\000':
1516 /* Empty entry invalid */
1517 *client_sni = NULL;
1518 return;
1519 default:
1520 /* Does not start with a hex digit or : - not literal IP. */
1521 return;
1522 }
1523 /* Check for IPv4 */
1524 while (*cp) {
1525 switch (*cp) {
1526 case '0':
1527 case '1':
1528 case '2':
1529 case '3':
1530 case '4':
1531 case '5':
1532 case '6':
1533 case '7':
1534 case '8':
1535 case '9':
1536 case '.':
1537 break;
1538 default:
1539 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1540 goto check_ipv6;
1541 }
1542 cp++;
1543 }
1544 /* IPv4 address - not allowed. */
1545 *client_sni = NULL;
1546 return;
1547
1548check_ipv6:
1549 /* Check for IPv6 */
1550 cp = *client_sni;
1551 while (*cp) {
1552 switch (*cp) {
1553 case '0':
1554 case '1':
1555 case '2':
1556 case '3':
1557 case '4':
1558 case '5':
1559 case '6':
1560 case '7':
1561 case '8':
1562 case '9':
1563 case 'a':
1564 case 'b':
1565 case 'c':
1566 case 'd':
1567 case 'e':
1568 case 'f':
1569 case 'A':
1570 case 'B':
1571 case 'C':
1572 case 'D':
1573 case 'E':
1574 case 'F':
1575 case ':':
1576 break;
1577 case '%':
1578 /* Start of i/f specification, Previous is IPv6 */
1579 *client_sni = NULL;
1580 return;
1581 default:
1582 /* Not of format xx:xx::xx. */
1583 return;
1584 }
1585 cp++;
1586 }
1587 *client_sni = NULL;
1588 return;
1589}
1590
1593 const coap_address_t *local_if,
1594 const coap_address_t *server,
1595 coap_proto_t proto,
1596 coap_dtls_cpsk_t *setup_data) {
1597 coap_session_t *session;
1598
1599 coap_lock_lock(ctx, return NULL);
1600 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, setup_data);
1601 coap_lock_unlock(ctx);
1602 return session;
1603}
1604
1607 const coap_address_t *local_if,
1608 const coap_address_t *server,
1609 coap_proto_t proto,
1610 coap_dtls_cpsk_t *setup_data) {
1611 coap_session_t *session;
1612
1614 session = coap_session_create_client(ctx, local_if, server, proto);
1615
1616 if (!session || !setup_data)
1617 return NULL;
1618
1619 session->cpsk_setup_data = *setup_data;
1620 if (setup_data->psk_info.identity.s) {
1621 session->psk_identity =
1623 setup_data->psk_info.identity.length);
1624 if (!session->psk_identity) {
1625 coap_log_warn("Cannot store session Identity (PSK)\n");
1626 coap_session_release_lkd(session);
1627 return NULL;
1628 }
1630 coap_log_warn("Identity (PSK) not defined\n");
1631 coap_session_release_lkd(session);
1632 return NULL;
1633 }
1634
1635 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1636 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1637 setup_data->psk_info.key.length);
1638 if (!session->psk_key) {
1639 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1640 coap_session_release_lkd(session);
1641 return NULL;
1642 }
1644 coap_log_warn("Pre-shared key (PSK) not defined\n");
1645 coap_session_release_lkd(session);
1646 return NULL;
1647 }
1648
1649 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
1650
1652 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
1653 coap_session_release_lkd(session);
1654 return NULL;
1655 }
1656 }
1657 coap_log_debug("***%s: new outgoing session\n",
1658 coap_session_str(session));
1659 coap_session_check_connect(session);
1660 return session;
1661}
1662#endif /* COAP_CLIENT_SUPPORT */
1663
1664int
1666 const coap_bin_const_t *psk_hint
1667 ) {
1668 /* We may be refreshing the hint with the same hint */
1669 coap_bin_const_t *old_psk_hint = session->psk_hint;
1670
1671 if (psk_hint && psk_hint->s) {
1672 if (session->psk_hint) {
1673 if (coap_binary_equal(session->psk_hint, psk_hint))
1674 return 1;
1675 }
1676 session->psk_hint = coap_new_bin_const(psk_hint->s,
1677 psk_hint->length);
1678 if (!session->psk_hint) {
1679 coap_log_err("No memory to store identity hint (PSK)\n");
1680 if (old_psk_hint)
1681 coap_delete_bin_const(old_psk_hint);
1682 return 0;
1683 }
1684 } else {
1685 session->psk_hint = NULL;
1686 }
1687 if (old_psk_hint)
1688 coap_delete_bin_const(old_psk_hint);
1689
1690 return 1;
1691}
1692
1693int
1695 const coap_bin_const_t *psk_key
1696 ) {
1697 /* We may be refreshing the key with the same key */
1698 coap_bin_const_t *old_psk_key = session->psk_key;
1699
1700 if (psk_key && psk_key->s) {
1701 if (session->psk_key) {
1702 if (coap_binary_equal(session->psk_key, psk_key))
1703 return 1;
1704 }
1705 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1706 if (!session->psk_key) {
1707 coap_log_err("No memory to store pre-shared key (PSK)\n");
1708 if (old_psk_key)
1709 coap_delete_bin_const(old_psk_key);
1710 return 0;
1711 }
1712 } else {
1713 session->psk_key = NULL;
1714 }
1715 if (old_psk_key)
1716 coap_delete_bin_const(old_psk_key);
1717
1718 return 1;
1719}
1720
1721int
1723 const coap_bin_const_t *psk_identity
1724 ) {
1725 /* We may be refreshing the identity with the same identity */
1726 coap_bin_const_t *old_psk_identity = session->psk_identity;
1727
1728 if (psk_identity && psk_identity->s) {
1729 if (session->psk_identity) {
1730 if (coap_binary_equal(session->psk_identity, psk_identity))
1731 return 1;
1732 }
1733 session->psk_identity = coap_new_bin_const(psk_identity->s,
1734 psk_identity->length);
1735 if (!session->psk_identity) {
1736 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1737 if (old_psk_identity)
1738 coap_delete_bin_const(old_psk_identity);
1739 return 0;
1740 }
1741 } else {
1742 session->psk_identity = NULL;
1743 }
1744 if (old_psk_identity)
1745 coap_delete_bin_const(old_psk_identity);
1746
1747 return 1;
1748}
1749
1750#if COAP_SERVER_SUPPORT
1751const coap_bin_const_t *
1753 if (session)
1754 return session->psk_hint;
1755 return NULL;
1756}
1757#endif /* COAP_SERVER_SUPPORT */
1758
1759const coap_bin_const_t *
1761 const coap_bin_const_t *psk_identity = NULL;
1762 if (session) {
1763 psk_identity = session->psk_identity;
1764 if (psk_identity == NULL) {
1765 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1766 }
1767 }
1768 return psk_identity;
1769}
1770
1771const coap_bin_const_t *
1773 if (session)
1774 return session->psk_key;
1775 return NULL;
1776}
1777
1778#if COAP_CLIENT_SUPPORT
1781 const coap_address_t *local_if,
1782 const coap_address_t *server,
1783 coap_proto_t proto,
1784 coap_dtls_pki_t *setup_data) {
1785 coap_session_t *session;
1786
1787 coap_lock_lock(ctx, return NULL);
1788 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, setup_data);
1789 coap_lock_unlock(ctx);
1790 return session;
1791}
1792
1795 const coap_address_t *local_if,
1796 const coap_address_t *server,
1797 coap_proto_t proto,
1798 coap_dtls_pki_t *setup_data) {
1799 coap_session_t *session;
1800 coap_dtls_pki_t l_setup_data;
1801
1802 if (!setup_data)
1803 return NULL;
1804 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1805 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
1806 return NULL;
1807 }
1808
1810 l_setup_data = *setup_data;
1811 coap_sanitize_client_sni(&l_setup_data.client_sni);
1812
1813 session = coap_session_create_client(ctx, local_if, server, proto);
1814
1815 if (!session) {
1816 return NULL;
1817 }
1818
1820 /* we know that setup_data is not NULL */
1821 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
1822 coap_session_release_lkd(session);
1823 return NULL;
1824 }
1825 }
1826 coap_log_debug("***%s: new outgoing session\n",
1827 coap_session_str(session));
1828 coap_session_check_connect(session);
1829 return session;
1830}
1831#endif /* ! COAP_CLIENT_SUPPORT */
1832
1833#if COAP_SERVER_SUPPORT
1834#if !COAP_DISABLE_TCP
1837 coap_session_t *session;
1839 NULL, NULL, NULL, 0, ctx, ep);
1840 if (!session)
1841 goto error;
1842
1843 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
1844 if (!coap_netif_strm_accept(ep, session, extra))
1845 goto error;
1846
1847 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1848
1849#ifdef COAP_EPOLL_SUPPORT
1850 session->sock.session = session;
1851 coap_epoll_ctl_add(&session->sock,
1852 EPOLLIN,
1853 __func__);
1854#endif /* COAP_EPOLL_SUPPORT */
1855 SESSIONS_ADD(ep->sessions, session);
1856 if (session) {
1857 coap_log_debug("***%s: session %p: new incoming session\n",
1858 coap_session_str(session), (void *)session);
1862 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1863 }
1864 return session;
1865
1866error:
1867 /*
1868 * Need to add in the session as coap_session_release_lkd()
1869 * will call SESSIONS_DELETE in coap_session_free().
1870 */
1871 if (session) {
1872 SESSIONS_ADD(ep->sessions, session);
1873 coap_session_free(session);
1874 }
1875 return NULL;
1876}
1877#endif /* !COAP_DISABLE_TCP */
1878#endif /* COAP_SERVER_SUPPORT */
1879
1880void
1882 const uint8_t *data) {
1883 session->tx_token = coap_decode_var_bytes8(data, len);
1884 /*
1885 * Decrement as when first used by coap_session_new_token() it will
1886 * get incremented
1887 */
1888 session->tx_token--;
1889}
1890
1891void
1893 uint8_t *data) {
1894 *len = coap_encode_var_safe8(data,
1895 sizeof(session->tx_token), ++session->tx_token);
1896}
1897
1898COAP_API uint16_t
1900 uint16_t mid;
1901
1902 coap_lock_lock(session->context, return 0);
1903 mid = coap_new_message_id_lkd(session);
1904 coap_lock_unlock(session->context);
1905 return mid;
1906}
1907
1908uint16_t
1911 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1912 return ++session->tx_mid;
1913 /* TCP/TLS have no notion of mid */
1914 return 0;
1915}
1916
1917const coap_address_t *
1919 if (session)
1920 return &session->addr_info.remote;
1921 return NULL;
1922}
1923
1924const coap_address_t *
1926 if (session)
1927 return &session->addr_info.local;
1928 return NULL;
1929}
1930
1931const coap_address_t *
1933#if COAP_CLIENT_SUPPORT
1934 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
1935 session->sock.flags & COAP_SOCKET_MULTICAST)
1936 return &session->sock.mcast_addr;
1937#else /* ! COAP_CLIENT_SUPPORT */
1938 (void)session;
1939#endif /* ! COAP_CLIENT_SUPPORT */
1940 return NULL;
1941}
1942
1945 if (session)
1946 return session->context;
1947 return NULL;
1948}
1949
1952 if (session)
1953 return session->proto;
1954 return 0;
1955}
1956
1959 if (session)
1960 return session->type;
1961 return 0;
1962}
1963
1964#if COAP_CLIENT_SUPPORT
1965int
1967#if COAP_SERVER_SUPPORT
1968 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1970 session->type = COAP_SESSION_TYPE_CLIENT;
1971 return 1;
1972 }
1973#else /* ! COAP_SERVER_SUPPORT */
1974 (void)session;
1975#endif /* ! COAP_SERVER_SUPPORT */
1976 return 0;
1977}
1978#endif /* COAP_CLIENT_SUPPORT */
1979
1982 if (session)
1983 return session->state;
1984 return 0;
1985}
1986
1987int
1989 if (session)
1990 return session->ifindex;
1991 return -1;
1992}
1993
1994void *
1996 coap_tls_library_t *tls_lib) {
1997 if (session)
1998 return coap_dtls_get_tls(session, tls_lib);
1999 return NULL;
2000}
2001
2002static const char *
2004 switch (proto) {
2005 case COAP_PROTO_UDP:
2006 return "UDP ";
2007 case COAP_PROTO_DTLS:
2008 return "DTLS";
2009 case COAP_PROTO_TCP:
2010 return "TCP ";
2011 case COAP_PROTO_TLS:
2012 return "TLS ";
2013 case COAP_PROTO_WS:
2014 return "WS ";
2015 case COAP_PROTO_WSS:
2016 return "WSS ";
2017 case COAP_PROTO_NONE:
2018 case COAP_PROTO_LAST:
2019 default:
2020 return "????" ;
2021 break;
2022 }
2023 return NULL;
2024}
2025
2026#if COAP_SERVER_SUPPORT
2028coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2029 coap_endpoint_t *endpoint;
2030
2031 coap_lock_lock(context, return NULL);
2032 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2033 coap_lock_unlock(context);
2034 return endpoint;
2035}
2036
2038coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2039 coap_proto_t proto) {
2040 coap_endpoint_t *ep = NULL;
2041
2042 assert(context);
2043 assert(listen_addr);
2044 assert(proto != COAP_PROTO_NONE);
2045
2046 coap_lock_check_locked(context);
2047
2048 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
2049 coap_log_crit("coap_new_endpoint: DTLS not supported\n");
2050 goto error;
2051 }
2052
2053 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
2054 coap_log_crit("coap_new_endpoint: TLS not supported\n");
2055 goto error;
2056 }
2057
2058 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
2059 coap_log_crit("coap_new_endpoint: TCP not supported\n");
2060 goto error;
2061 }
2062
2063 if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
2064 coap_log_crit("coap_new_endpoint: WS not supported\n");
2065 goto error;
2066 }
2067
2068 if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
2069 coap_log_crit("coap_new_endpoint: WSS not supported\n");
2070 goto error;
2071 }
2072
2073 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2074 proto == COAP_PROTO_WSS) {
2076 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2077 "coap_context_set_pki() not called\n");
2078 goto error;
2079 }
2080 }
2081
2082 ep = coap_malloc_endpoint();
2083 if (!ep) {
2084 coap_log_warn("coap_new_endpoint: malloc");
2085 goto error;
2086 }
2087
2088 memset(ep, 0, sizeof(coap_endpoint_t));
2089 ep->context = context;
2090 ep->proto = proto;
2091 ep->sock.endpoint = ep;
2092 assert(proto < COAP_PROTO_LAST);
2093 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2094
2095 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2096 if (!coap_netif_dgrm_listen(ep, listen_addr))
2097 goto error;
2098#ifdef WITH_CONTIKI
2099 ep->sock.context = context;
2100#endif /* WITH_CONTIKI */
2101#if !COAP_DISABLE_TCP
2102 } else if (COAP_PROTO_RELIABLE(proto)) {
2103 if (!coap_netif_strm_listen(ep, listen_addr))
2104 goto error;
2105#endif /* !COAP_DISABLE_TCP */
2106 } else {
2107 coap_log_crit("coap_new_endpoint: protocol not supported\n");
2108 goto error;
2109 }
2110
2112#ifndef INET6_ADDRSTRLEN
2113#define INET6_ADDRSTRLEN 40
2114#endif
2115 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2116
2117 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2118 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2119 addr_str);
2120 }
2121 }
2122
2124
2125#ifdef COAP_EPOLL_SUPPORT
2126 ep->sock.endpoint = ep;
2128 EPOLLIN,
2129 __func__);
2130#endif /* COAP_EPOLL_SUPPORT */
2131
2132 LL_PREPEND(context->endpoint, ep);
2133 return ep;
2134
2135error:
2137 return NULL;
2138}
2139
2140void
2142 ep->default_mtu = (uint16_t)mtu;
2143}
2144
2145COAP_API void
2147 if (ep) {
2148 coap_context_t *context = ep->context;
2149 if (context) {
2150 coap_lock_lock(context, return);
2151 }
2153 if (context) {
2154 coap_lock_unlock(context);
2155 }
2156 }
2157}
2158
2159void
2161 if (ep) {
2162 coap_session_t *session, *rtmp;
2163
2164 if (ep->context) {
2165 /* If fully allocated and inserted */
2167 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2168 assert(session->ref == 0);
2169 if (session->ref == 0) {
2171 coap_session_free(session);
2172 }
2173 }
2174 if (coap_netif_available_ep(ep)) {
2175 /*
2176 * ep->sock.endpoint is set in coap_new_endpoint().
2177 * ep->sock.session is never set.
2178 *
2179 * session->sock.session is set for both clients and servers (when a
2180 * new session is accepted), but does not affect the endpoint.
2181 *
2182 * So, it is safe to call coap_netif_close_ep() after all the sessions
2183 * have been freed above as we are only working with the endpoint sock.
2184 */
2185#ifdef COAP_EPOLL_SUPPORT
2186 assert(ep->sock.session == NULL);
2187#endif /* COAP_EPOLL_SUPPORT */
2189 }
2190
2191 if (ep->context->endpoint) {
2192 LL_DELETE(ep->context->endpoint, ep);
2193 }
2194 }
2196 }
2197}
2198#endif /* COAP_SERVER_SUPPORT */
2199
2202 const coap_address_t *remote_addr,
2203 int ifindex) {
2204 coap_session_t *s, *rtmp;
2205#if COAP_CLIENT_SUPPORT
2206 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2207 if (s->ifindex == ifindex) {
2208 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2209 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2210 return s;
2211 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2212 return s;
2213 }
2214 }
2215#endif /* COAP_CLIENT_SUPPORT */
2216#if COAP_SERVER_SUPPORT
2217 coap_endpoint_t *ep;
2218
2219 LL_FOREACH(ctx->endpoint, ep) {
2220 SESSIONS_ITER(ep->sessions, s, rtmp) {
2221 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2222 remote_addr))
2223 return s;
2224 }
2225 }
2226#endif /* COAP_SERVER_SUPPORT */
2227 return NULL;
2228}
2229
2230#ifndef INET6_ADDRSTRLEN
2231#define INET6_ADDRSTRLEN 46
2232#endif
2233const char *
2235 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2236 char *p = szSession, *end = szSession + sizeof(szSession);
2237 if (coap_print_addr(&session->addr_info.local,
2238 (unsigned char *)p, end - p) > 0)
2239 p += strlen(p);
2240 if (p + 6 < end) {
2241 strcpy(p, " <-> ");
2242 p += 5;
2243 }
2244 if (p + 1 < end) {
2245 if (coap_print_addr(&session->addr_info.remote,
2246 (unsigned char *)p, end - p) > 0)
2247 p += strlen(p);
2248 }
2249 if (session->ifindex > 0 && p + 1 < end)
2250 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2251 if (p + 6 < end) {
2252 strcpy(p, " ");
2253 p++;
2254 strcpy(p, coap_proto_name(session->proto));
2255 }
2256
2257 return szSession;
2258}
2259
2260#if COAP_SERVER_SUPPORT
2261const char *
2262coap_endpoint_str(const coap_endpoint_t *endpoint) {
2263 static char szEndpoint[128];
2264 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2265 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2266 p += strlen(p);
2267 if (p + 6 < end) {
2268 if (endpoint->proto == COAP_PROTO_UDP) {
2269 strcpy(p, " UDP");
2270 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2271 strcpy(p, " DTLS");
2272 } else {
2273 strcpy(p, " NONE");
2274 }
2275 }
2276
2277 return szEndpoint;
2278}
2279#endif /* COAP_SERVER_SUPPORT */
2280#if COAP_CLIENT_SUPPORT
2281void
2283 session->no_observe_cancel = 1;
2284}
2285#endif /* COAP_CLIENT_SUPPORT */
2286#endif /* COAP_SESSION_C_ */
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
#define PRIu32
coap_nack_reason_t
Definition coap_io.h:62
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:64
@ COAP_NACK_WS_FAILED
Definition coap_io.h:71
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:63
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:66
@ COAP_NACK_TLS_LAYER_FAILED
Definition coap_io.h:69
@ COAP_NACK_ICMP_ISSUE
Definition coap_io.h:67
@ COAP_NACK_WS_LAYER_FAILED
Definition coap_io.h:70
@ COAP_NACK_RST
Definition coap_io.h:65
@ COAP_NACK_BAD_RESPONSE
Definition coap_io.h:68
#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:51
@ COAP_STRING
Definition coap_mem.h:39
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
Definition coap_notls.c:108
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:153
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:256
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:142
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)
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_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition coap_prng.c:178
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:4358
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:227
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:2528
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:1226
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:1679
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:1123
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1149
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition coap_net.c:256
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:2573
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.
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition coap_dtls.h:307
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition coap_dtls.h:405
coap_tls_library_t
Definition coap_dtls.h:70
@ 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_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_lock_callback(c, func)
Dummy for no thread-safe code.
#define coap_lock_check_locked(c)
Dummy for no thread-safe code.
#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:1001
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_tcp_is_supported(void)
Check whether TCP is available.
Definition coap_tcp.c:20
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:41
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition coap_ws.c:933
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:36
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition coap_ws.c:938
Only used for servers for hashing incoming packets.
uint16_t lport
local port
coap_address_t remote
remote address and port
coap_proto_t proto
CoAP protocol.
coap_address_t remote
remote address and port
Definition coap_io.h: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:381
coap_bin_const_t identity
Definition coap_dtls.h:380
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:410
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:437
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition coap_dtls.h:443
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:312
uint8_t version
Definition coap_dtls.h:313
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:368
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.
coap_bin_const_t * client_cid
Contains client CID or NULL.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
uint8_t is_dtls13
Set if session is DTLS1.3.
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv)
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
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