libcoap 4.3.5-develop-0b41ee0
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 */
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) {
539 coap_handle_nack(session, q->pdu,
540 session->proto == COAP_PROTO_DTLS ?
542 q->id);
543 }
545 }
546 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
547 LL_DELETE(session->lg_xmit, lq);
548 coap_block_delete_lg_xmit(session, lq);
549 }
550#if COAP_SERVER_SUPPORT
551 coap_lg_srcv_t *sq, *stmp;
552
553 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
554 LL_DELETE(session->lg_srcv, sq);
555 coap_block_delete_lg_srcv(session, sq);
556 }
557#endif /* COAP_SERVER_SUPPORT */
558#if COAP_OSCORE_SUPPORT
560#endif /* COAP_OSCORE_SUPPORT */
561#if COAP_WS_SUPPORT
562 coap_free_type(COAP_STRING, session->ws);
563 coap_delete_str_const(session->ws_host);
564#endif /* COAP_WS_SUPPORT */
565}
566
567void
569 if (!session)
570 return;
572 assert(session->ref == 0);
573 if (session->ref)
574 return;
575 /* Make sure nothing gets deleted under our feet */
577 coap_session_mfree(session);
578#if COAP_SERVER_SUPPORT
580 if (session->endpoint) {
581 if (session->endpoint->sessions)
582 SESSIONS_DELETE(session->endpoint->sessions, session);
583 } else
584#endif /* COAP_SERVER_SUPPORT */
585#if COAP_CLIENT_SUPPORT
586 if (session->context) {
587 if (session->context->sessions)
588 SESSIONS_DELETE(session->context->sessions, session);
589 }
591#endif /* COAP_CLIENT_SUPPORT */
593 coap_delete_bin_const(session->echo);
594#if COAP_SERVER_SUPPORT
596#endif /* COAP_SERVER_SUPPORT */
597 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
598 (void *)session);
599
600 assert(session->ref == 1);
602}
603
604static size_t
606 size_t max_with_header) {
607#if COAP_DISABLE_TCP
608 (void)session;
609 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
610 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
611 : 0;
612#else /* !COAP_DISABLE_TCP */
613 if (COAP_PROTO_NOT_RELIABLE(session->proto))
614 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
615 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
616 : 0;
617 /* we must assume there is no token to be on the safe side */
618 if (max_with_header <= 2)
619 return 0;
620 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
621 return max_with_header - 2;
622 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
623 return max_with_header - 3;
624 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
625 return max_with_header - 4;
626 else
627 return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE;
628#endif /* !COAP_DISABLE_TCP */
629}
630
631size_t
633 if (session->csm_rcv_mtu)
635 (size_t)(session->csm_rcv_mtu));
636
638 (size_t)(session->mtu - session->tls_overhead));
639}
640
641COAP_API size_t
643 size_t size;
644 coap_session_t *session_rw;
645
646 /*
647 * Need to do this to not get a compiler warning about const parameters
648 * but need to maintain source code backward compatibility
649 */
650 memcpy(&session_rw, &session, sizeof(session_rw));
651 coap_lock_lock(session_rw->context, return 0);
652 size = coap_session_max_pdu_size_lkd(session_rw);
653 coap_lock_unlock(session_rw->context);
654 return size;
655}
656
657size_t
659 size_t max_with_header;
660
662#if COAP_CLIENT_SUPPORT
663 /*
664 * Delay if session->doing_first is set.
665 * E.g. Reliable and CSM not in yet for checking block support
666 */
667 coap_session_t *session_rw;
668
669 /*
670 * Need to do this to not get a compiler warning about const parameters
671 * but need to maintain source code backward compatibility
672 */
673 memcpy(&session_rw, &session, sizeof(session_rw));
674 if (coap_client_delay_first(session_rw) == 0) {
675 coap_log_debug("coap_client_delay_first: timeout\n");
676 /* Have to go with the defaults */
677 }
678#endif /* COAP_CLIENT_SUPPORT */
679
680 max_with_header = (size_t)(session->mtu - session->tls_overhead);
681
682 return coap_session_max_pdu_size_internal(session, max_with_header);
683}
684
685void
686coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
687#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
688 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
689 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
690#endif
691 if (mtu < 64)
692 mtu = 64;
693 session->mtu = mtu;
694 if (session->tls_overhead >= session->mtu) {
695 session->tls_overhead = session->mtu;
696 coap_log_err("DTLS overhead exceeds MTU\n");
697 }
698}
699
700ssize_t
702 coap_queue_t *node) {
703 if (node) {
704 coap_queue_t *removed = NULL;
705 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
706 assert(removed == node);
708 node->session = NULL;
709 node->t = 0;
710 } else {
711 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
712 coap_queue_t *q = NULL;
713 /* Check same mid is not getting re-used in violation of RFC7252 */
714 LL_FOREACH(session->delayqueue, q) {
715 if (q->id == pdu->mid) {
716 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
717 coap_session_str(session), pdu->mid);
718 return COAP_INVALID_MID;
719 }
720 }
721 }
722 node = coap_new_node();
723 if (node == NULL)
724 return COAP_INVALID_MID;
725 node->id = pdu->mid;
726 node->pdu = pdu;
727 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
728 uint8_t r;
729 coap_prng_lkd(&r, sizeof(r));
730 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
731 node->timeout = coap_calc_timeout(session, r);
732 }
733 }
734 LL_APPEND(session->delayqueue, node);
735 coap_log_debug("** %s: mid=0x%04x: delayed\n",
736 coap_session_str(session), node->id);
737 return COAP_PDU_DELAYED;
738}
739
740#if !COAP_DISABLE_TCP
741void
743 coap_pdu_t *pdu;
744 uint8_t buf[4];
745 assert(COAP_PROTO_RELIABLE(session->proto));
746 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
747 session->state = COAP_SESSION_STATE_CSM;
748 session->partial_write = 0;
749 if (session->mtu == 0)
750 coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */
752 if (pdu == NULL
754 coap_encode_var_safe(buf, sizeof(buf),
755 session->context->csm_max_message_size), buf) == 0
757 coap_encode_var_safe(buf, sizeof(buf),
758 0), buf) == 0
759 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
762 coap_encode_var_safe(buf, sizeof(buf),
763 session->max_token_size),
764 buf) == 0)
765 || coap_pdu_encode_header(pdu, session->proto) == 0
766 ) {
768 } else {
769 ssize_t bytes_written;
770
771 pdu->session = session;
772 bytes_written = coap_session_send_pdu(session, pdu);
773 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
775 } else {
776 session->csm_rcv_mtu = session->context->csm_max_message_size;
777 if (session->csm_rcv_mtu > COAP_BERT_BASE)
778 session->csm_bert_loc_support = 1;
779 else
780 session->csm_bert_loc_support = 0;
781 }
782 }
783 if (pdu)
785}
786#endif /* !COAP_DISABLE_TCP */
787
790 coap_mid_t mid;
791
792 coap_lock_lock(session->context, return COAP_INVALID_MID);
793 mid = coap_session_send_ping_lkd(session);
794 coap_lock_unlock(session->context);
795 return mid;
796}
797
800 coap_pdu_t *ping = NULL;
801
803 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
804 session->con_active)
805 return COAP_INVALID_MID;
806 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
807 uint16_t mid = coap_new_message_id_lkd(session);
808 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
809 }
810#if !COAP_DISABLE_TCP
811 else {
813 }
814#endif /* !COAP_DISABLE_TCP */
815 if (!ping)
816 return COAP_INVALID_MID;
817 return coap_send_internal(session, ping, NULL);
818}
819
820void
822 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
823 coap_log_debug("***%s: session connected\n",
824 coap_session_str(session));
825 if (session->state == COAP_SESSION_STATE_CSM) {
827 if (session->doing_first)
828 session->doing_first = 0;
829 }
830 }
831
833 session->partial_write = 0;
834
835 if (session->proto==COAP_PROTO_DTLS) {
836 session->tls_overhead = coap_dtls_get_overhead(session);
837 if (session->tls_overhead >= session->mtu) {
838 session->tls_overhead = session->mtu;
839 coap_log_err("DTLS overhead exceeds MTU\n");
840 }
841 }
842
843 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
844 ssize_t bytes_written;
845 coap_queue_t *q = session->delayqueue;
846 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
847 if (session->con_active >= COAP_NSTART(session))
848 break;
849 session->con_active++;
850 }
851 /* Take entry off the queue */
852 session->delayqueue = q->next;
853 q->next = NULL;
854
855 coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
856 coap_session_str(session), (int)q->pdu->mid);
857 bytes_written = coap_session_send_pdu(session, q->pdu);
858 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
859 if (coap_wait_ack(session->context, session, q) >= 0)
860 q = NULL;
861 }
862 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
863 if (q)
865 if (bytes_written < 0)
866 break;
867 } else if (q) {
868 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
869 q->next = session->delayqueue;
870 session->delayqueue = q;
871 if (bytes_written > 0)
872 session->partial_write = (size_t)bytes_written;
873 break;
874 } else {
876 }
877 }
878 }
879}
880
881#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
882static const char *
884 switch (reason) {
886 return "COAP_NACK_TOO_MANY_RETRIES";
888 return "COAP_NACK_NOT_DELIVERABLE";
889 case COAP_NACK_RST:
890 return "COAP_NACK_RST";
892 return "COAP_NACK_TLS_FAILED";
894 return "COAP_NACK_ICMP_ISSUE";
896 return "COAP_NACK_BAD_RESPONSE";
898 return "COAP_NACK_TLS_LAYER_FAILED";
900 return "COAP_NACK_WS_LAYER_FAILED";
902 return "COAP_NACK_WS_FAILED";
903 default:
904 return "???";
905 }
906}
907#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
908
909void
911 coap_pdu_t *sent,
912 const coap_nack_reason_t reason,
913 const coap_mid_t mid) {
914 if (session->context->nack_handler) {
915 coap_bin_const_t token;
916
917 if (sent) {
918 coap_check_update_token(session, sent);
919 token = sent->actual_token;
920 }
922 session->context->nack_handler(session, sent, reason, mid));
923 if (sent) {
924 coap_update_token(sent, token.length, token.s);
925 }
926 }
927#if COAP_CLIENT_SUPPORT
928 if (reason != COAP_NACK_ICMP_ISSUE) {
929 session->doing_send_recv = 0;
930 }
931#endif /* COAP_CLIENT_SUPPORT */
932}
933
934COAP_API void
936 coap_lock_lock(session->context, return);
937 coap_session_disconnected_lkd(session, reason);
938 coap_lock_unlock(session->context);
939}
940
941void
943#if !COAP_DISABLE_TCP
944 coap_session_state_t state = session->state;
945#endif /* !COAP_DISABLE_TCP */
946 coap_lg_xmit_t *lq, *ltmp;
947#if COAP_SERVER_SUPPORT
948 coap_lg_srcv_t *sq, *stmp;
949#endif /* COAP_SERVER_SUPPORT */
950#if COAP_CLIENT_SUPPORT
951 coap_lg_crcv_t *cq, *etmp;
952#endif /* COAP_CLIENT_SUPPORT */
953 int sent_nack = 0;
954 coap_queue_t *q;
955
957 q = session->context->sendqueue;
958
959 while (q) {
960 if (q->session == session) {
961 /* Take the first one */
962 coap_handle_nack(session, q->pdu, reason, q->id);
963 sent_nack = 1;
964 break;
965 }
966 q = q->next;
967 }
968
969 if (reason != COAP_NACK_ICMP_ISSUE) {
970 while (session->delayqueue) {
971 q = session->delayqueue;
972 session->delayqueue = q->next;
973 q->next = NULL;
974 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
975 coap_session_str(session), q->id);
976 if (q->pdu->type == COAP_MESSAGE_CON) {
977 coap_handle_nack(session, q->pdu, reason, q->id);
978 sent_nack = 1;
979 }
980
981#if COAP_CLIENT_SUPPORT
982 session->doing_send_recv = 0;
983#endif /* COAP_CLIENT_SUPPORT */
985 }
986 }
987#if COAP_CLIENT_SUPPORT
988 if (!sent_nack && session->lg_crcv) {
989 /* Take the first one */
990 coap_handle_nack(session, &session->lg_crcv->pdu, reason, session->lg_crcv->pdu.mid);
991 sent_nack = 1;
992 }
993#endif /* COAP_CLIENT_SUPPORT */
994 if (!sent_nack) {
995 /* Unable to determine which request disconnection was for */
996 coap_handle_nack(session, NULL, reason, 0);
997 }
998 if (reason == COAP_NACK_ICMP_ISSUE) {
999 coap_log_debug("***%s: session ICMP issue (%s)\n",
1000 coap_session_str(session), coap_nack_name(reason));
1001 return;
1002 }
1003 coap_log_debug("***%s: session disconnected (%s)\n",
1004 coap_session_str(session), coap_nack_name(reason));
1005#if COAP_SERVER_SUPPORT
1006 coap_delete_observers(session->context, session);
1007#endif /* COAP_SERVER_SUPPORT */
1008
1009 if (session->proto == COAP_PROTO_UDP)
1011 else
1012 session->state = COAP_SESSION_STATE_NONE;
1013
1014 session->con_active = 0;
1015
1016 if (session->partial_pdu) {
1018 session->partial_pdu = NULL;
1019 }
1020 session->partial_read = 0;
1021
1022 /* Not done if nack handler called above */
1023 while (session->delayqueue) {
1024 q = session->delayqueue;
1025 session->delayqueue = q->next;
1026 q->next = NULL;
1027 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1028 coap_session_str(session), q->id);
1029#if COAP_CLIENT_SUPPORT
1030 session->doing_send_recv = 0;
1031#endif /* COAP_CLIENT_SUPPORT */
1033 }
1034
1035#if COAP_CLIENT_SUPPORT
1036 /* Need to do this before (D)TLS and socket is closed down */
1037 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1038 LL_DELETE(session->lg_crcv, cq);
1039 coap_block_delete_lg_crcv(session, cq);
1040 }
1041#endif /* COAP_CLIENT_SUPPORT */
1042 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1043 LL_DELETE(session->lg_xmit, lq);
1044 coap_block_delete_lg_xmit(session, lq);
1045 }
1046#if COAP_SERVER_SUPPORT
1047 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1048 LL_DELETE(session->lg_srcv, sq);
1049 coap_block_delete_lg_srcv(session, sq);
1050 }
1051#endif /* COAP_SERVER_SUPPORT */
1052 coap_cancel_session_messages(session->context, session, reason);
1053
1054#if !COAP_DISABLE_TCP
1055 if (COAP_PROTO_RELIABLE(session->proto)) {
1056 if (coap_netif_available(session)) {
1060 }
1061 if (state != COAP_SESSION_STATE_NONE) {
1065 }
1066 if (session->doing_first)
1067 session->doing_first = 0;
1068 }
1069#endif /* !COAP_DISABLE_TCP */
1070 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1071}
1072
1073#if COAP_SERVER_SUPPORT
1074static void
1075coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1076 const coap_addr_tuple_t *addr_info) {
1077 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1078 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1079 addr_hash->lport = coap_address_get_port(&addr_info->local);
1080 addr_hash->proto = proto;
1081}
1082
1085 const coap_packet_t *packet, coap_tick_t now) {
1086 coap_session_t *session;
1087 coap_session_t *rtmp;
1088 unsigned int num_idle = 0;
1089 unsigned int num_hs = 0;
1090 coap_session_t *oldest = NULL;
1091 coap_session_t *oldest_hs = NULL;
1092 coap_addr_hash_t addr_hash;
1093
1094 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1095 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1096 if (session) {
1097 /* Maybe mcast or unicast IP address which is not in the hash */
1098 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1099 session->ifindex = packet->ifindex;
1100 session->last_rx_tx = now;
1101 return session;
1102 }
1103
1104#if COAP_CLIENT_SUPPORT
1105 if (coap_is_mcast(&packet->addr_info.local)) {
1106 /* Check if this a proxy client packet we sent on another socket */
1107 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1108 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1111 /* Drop looped back packet to stop recursion / confusion */
1112 return NULL;
1113 }
1114 }
1115 }
1116#endif /* COAP_CLIENT_SUPPORT */
1117 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1118 if (session->ref == 0 && session->delayqueue == NULL) {
1119 if (session->type == COAP_SESSION_TYPE_SERVER) {
1120 ++num_idle;
1121 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1122 oldest = session;
1123
1124 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1125 ++num_hs;
1126 /* See if this is a partial (D)TLS session set up
1127 which needs to be cleared down to prevent DOS */
1128 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1129 if (oldest_hs == NULL ||
1130 session->last_rx_tx < oldest_hs->last_rx_tx)
1131 oldest_hs = session;
1132 }
1133 }
1134 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1135 ++num_hs;
1136 /* See if this is a partial (D)TLS session set up for Client Hello
1137 which needs to be cleared down to prevent DOS */
1138 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1139 if (oldest_hs == NULL ||
1140 session->last_rx_tx < oldest_hs->last_rx_tx)
1141 oldest_hs = session;
1142 }
1143 }
1144 }
1145 }
1146
1147 if (endpoint->context->max_idle_sessions > 0 &&
1148 num_idle >= endpoint->context->max_idle_sessions) {
1150 coap_session_free(oldest);
1151 } else if (oldest_hs) {
1152 coap_log_warn("***%s: Incomplete session timed out\n",
1153 coap_session_str(oldest_hs));
1155 coap_session_free(oldest_hs);
1156 }
1157
1158 if (num_hs > (endpoint->context->max_handshake_sessions ?
1159 endpoint->context->max_handshake_sessions :
1161 /* Maxed out on number of sessions in (D)TLS negotiation state */
1162 coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1163 "large. New request ignored\n");
1164 return NULL;
1165 }
1166
1167 if (endpoint->proto == COAP_PROTO_DTLS) {
1168 /*
1169 * Need to check that this actually is a Client Hello before wasting
1170 * time allocating and then freeing off session.
1171 */
1172
1173 /*
1174 * Generic header structure of the DTLS record layer.
1175 * typedef struct __attribute__((__packed__)) {
1176 * uint8_t content_type; content type of the included message
1177 * uint16_t version; Protocol version
1178 * uint16_t epoch; counter for cipher state changes
1179 * uint8_t sequence_number[6]; sequence number
1180 * uint16_t length; length of the following fragment
1181 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1182 * } dtls_record_handshake_t;
1183 */
1184#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1185#define DTLS_CT_ALERT 21 /* Content Type Alert */
1186#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1187#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1188#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1189#define DTLS_CT_CID 25 /* Content Type Connection ID */
1190#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1191#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1192
1193 const uint8_t *payload = (const uint8_t *)packet->payload;
1194 size_t length = packet->length;
1195 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1196 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1197 payload[OFF_CONTENT_TYPE], length,
1198 OFF_HANDSHAKE_TYPE + 1);
1199 return NULL;
1200 }
1201 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1202 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1203 /* Client may have changed its IP address */
1204 int changed = 0;
1205
1206 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1207 if (session->client_cid) {
1208 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1209 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1210 session->client_cid->length) == 0) ||
1211 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1212 memcmp(session->client_cid->s, &payload[OFF_CID],
1213 session->client_cid->length) == 0)) {
1214 /* Updating IP address */
1215 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1216 SESSIONS_DELETE(endpoint->sessions, session);
1217 session->addr_info = packet->addr_info;
1218 memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash));
1219 SESSIONS_ADD(endpoint->sessions, session);
1220 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1221 return session;
1222 }
1223 }
1224 }
1225 if (!changed) {
1226 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1227 return NULL;
1228 }
1229 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1230 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1231 /* only log if not a late alert */
1232 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1233 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1234 payload[OFF_CONTENT_TYPE],
1235 payload[OFF_HANDSHAKE_TYPE]);
1236 return NULL;
1237 }
1238 }
1239
1241 &addr_hash, &packet->addr_info.local,
1242 &packet->addr_info.remote,
1243 packet->ifindex, endpoint->context, endpoint);
1244 if (session) {
1245 session->last_rx_tx = now;
1246 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1247 sizeof(session->sock.lfunc));
1248 if (endpoint->proto == COAP_PROTO_UDP)
1250 else if (endpoint->proto == COAP_PROTO_DTLS) {
1251 session->type = COAP_SESSION_TYPE_HELLO;
1252 }
1253 SESSIONS_ADD(endpoint->sessions, session);
1254 coap_log_debug("***%s: session %p: new incoming session\n",
1255 coap_session_str(session), (void *)session);
1257 }
1258 return session;
1259}
1260
1263 coap_tick_t now) {
1264 if (session) {
1265 session->last_rx_tx = now;
1266 session->type = COAP_SESSION_TYPE_SERVER;
1267 coap_dtls_establish(session);
1268 }
1269 return session;
1270}
1271#endif /* COAP_SERVER_SUPPORT */
1272
1273#if COAP_CLIENT_SUPPORT
1274static coap_session_t *
1275coap_session_create_client(coap_context_t *ctx,
1276 const coap_address_t *local_if,
1277 const coap_address_t *server,
1278 coap_proto_t proto) {
1279 coap_session_t *session = NULL;
1280 int default_port = COAP_DEFAULT_PORT;
1281
1282 assert(server);
1283
1284 switch (proto) {
1285 case COAP_PROTO_UDP:
1286 default_port = COAP_DEFAULT_PORT;
1287 break;
1288 case COAP_PROTO_DTLS:
1289 if (!coap_dtls_is_supported()) {
1290 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1291 return NULL;
1292 }
1293 default_port = COAPS_DEFAULT_PORT;
1294 break;
1295 case COAP_PROTO_TCP:
1296 if (!coap_tcp_is_supported()) {
1297 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1298 return NULL;
1299 }
1300 default_port = COAP_DEFAULT_PORT;
1301 break;
1302 case COAP_PROTO_TLS:
1303 if (!coap_tls_is_supported()) {
1304 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1305 return NULL;
1306 }
1307 default_port = COAPS_DEFAULT_PORT;
1308 break;
1309 case COAP_PROTO_WS:
1310 if (!coap_ws_is_supported()) {
1311 coap_log_crit("coap_new_client_session*: WS not supported\n");
1312 return NULL;
1313 }
1314 default_port = 80;
1315 break;
1316 case COAP_PROTO_WSS:
1317 if (!coap_wss_is_supported()) {
1318 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1319 return NULL;
1320 }
1321 default_port = 443;
1322 break;
1323 case COAP_PROTO_NONE:
1324 case COAP_PROTO_LAST:
1325 default:
1326 assert(0);
1327 return NULL;
1328 }
1329 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
1330 local_if, server, 0, ctx, NULL);
1331 if (!session)
1332 goto error;
1333
1335 session->sock.session = session;
1336 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1337 sizeof(session->sock.lfunc));
1338
1339 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1340 coap_session_t *s, *rtmp;
1341 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1342 goto error;
1343 }
1344 /* Check that this is not a duplicate 4-tuple */
1345 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1348 &s->addr_info.local) &&
1350 &s->addr_info.remote)) {
1351 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1352 coap_session_str(session), (void *)session);
1353 goto error;
1354 }
1355 }
1356#ifdef WITH_CONTIKI
1357 session->sock.context = ctx;
1358#endif /* WITH_CONTIKI */
1359#if !COAP_DISABLE_TCP
1360 } else if (COAP_PROTO_RELIABLE(proto)) {
1361 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1362 goto error;
1363 }
1364#endif /* !COAP_DISABLE_TCP */
1365 }
1366
1367#ifdef COAP_EPOLL_SUPPORT
1368 session->sock.session = session;
1369 coap_epoll_ctl_add(&session->sock,
1370 EPOLLIN |
1371 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1372 EPOLLOUT : 0),
1373 __func__);
1374#endif /* COAP_EPOLL_SUPPORT */
1375
1377 if (local_if)
1378 session->sock.flags |= COAP_SOCKET_BOUND;
1379#if COAP_SERVER_SUPPORT
1380 if (ctx->proxy_uri_resource)
1381 session->proxy_session = 1;
1382#endif /* COAP_SERVER_SUPPORT */
1383 SESSIONS_ADD(ctx->sessions, session);
1384 return session;
1385
1386error:
1387 /*
1388 * Need to add in the session as coap_session_release_lkd()
1389 * will call SESSIONS_DELETE in coap_session_free().
1390 */
1391 if (session)
1392 SESSIONS_ADD(ctx->sessions, session);
1393 coap_session_release_lkd(session);
1394 return NULL;
1395}
1396
1397static void
1398coap_session_check_connect(coap_session_t *session) {
1399 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1400 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1401 }
1402#if !COAP_DISABLE_TCP
1403 if (COAP_PROTO_RELIABLE(session->proto)) {
1404 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1406 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1407 session->state != COAP_SESSION_STATE_NONE &&
1408 session->type == COAP_SESSION_TYPE_CLIENT) {
1409 session->doing_first = 1;
1410 }
1411 } else {
1412 /* Initial connect worked immediately */
1413 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1414 }
1415 }
1416#endif /* !COAP_DISABLE_TCP */
1417 coap_ticks(&session->last_rx_tx);
1418}
1419#endif /* COAP_CLIENT_SUPPORT */
1420
1421void
1423 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1424 coap_session_connected(session);
1425#if !COAP_DISABLE_TCP
1426 if (COAP_PROTO_RELIABLE(session->proto))
1427 coap_session_send_csm(session);
1428#endif /* !COAP_DISABLE_TCP */
1429}
1430
1431#if COAP_CLIENT_SUPPORT
1434 const coap_address_t *local_if,
1435 const coap_address_t *server,
1436 coap_proto_t proto) {
1437 coap_session_t *session;
1438
1439 coap_lock_lock(ctx, return NULL);
1440 session = coap_new_client_session_lkd(ctx, local_if, server, proto);
1441 coap_lock_unlock(ctx);
1442 return session;
1443}
1444
1447 const coap_address_t *local_if,
1448 const coap_address_t *server,
1449 coap_proto_t proto) {
1450 coap_session_t *session;
1451
1453 session = coap_session_create_client(ctx, local_if, server,
1454 proto);
1455 if (session) {
1456 coap_log_debug("***%s: session %p: created outgoing session\n",
1457 coap_session_str(session), (void *)session);
1458 coap_session_check_connect(session);
1459 }
1460 return session;
1461}
1462
1465 const coap_address_t *local_if,
1466 const coap_address_t *server,
1467 coap_proto_t proto, const char *identity,
1468 const uint8_t *key, unsigned key_len) {
1469 coap_session_t *session;
1470
1471 coap_lock_lock(ctx, return NULL);
1472 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1473 coap_lock_unlock(ctx);
1474 return session;
1475}
1476
1479 const coap_address_t *local_if,
1480 const coap_address_t *server,
1481 coap_proto_t proto, const char *identity,
1482 const uint8_t *key, unsigned key_len) {
1483 coap_dtls_cpsk_t setup_data;
1484
1486 memset(&setup_data, 0, sizeof(setup_data));
1488
1489 if (identity) {
1490 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1491 setup_data.psk_info.identity.length = strlen(identity);
1492 }
1493
1494 if (key && key_len > 0) {
1495 setup_data.psk_info.key.s = key;
1496 setup_data.psk_info.key.length = key_len;
1497 }
1498
1499 return coap_new_client_session_psk2_lkd(ctx, local_if, server,
1500 proto, &setup_data);
1501}
1502
1503/*
1504 * Check the validity of the SNI to send to the server.
1505 *
1506 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1507 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1508 */
1509static void
1510coap_sanitize_client_sni(char **client_sni) {
1511 char *cp;
1512
1513 if (*client_sni == NULL)
1514 return;
1515
1516 cp = *client_sni;
1517 switch (*cp) {
1518 case '0':
1519 case '1':
1520 case '2':
1521 case '3':
1522 case '4':
1523 case '5':
1524 case '6':
1525 case '7':
1526 case '8':
1527 case '9':
1528 case 'a':
1529 case 'b':
1530 case 'c':
1531 case 'd':
1532 case 'e':
1533 case 'f':
1534 case 'A':
1535 case 'B':
1536 case 'C':
1537 case 'D':
1538 case 'E':
1539 case 'F':
1540 case ':':
1541 break;
1542 case '\000':
1543 /* Empty entry invalid */
1544 *client_sni = NULL;
1545 return;
1546 default:
1547 /* Does not start with a hex digit or : - not literal IP. */
1548 return;
1549 }
1550 /* Check for IPv4 */
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 '.':
1564 break;
1565 default:
1566 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1567 goto check_ipv6;
1568 }
1569 cp++;
1570 }
1571 /* IPv4 address - not allowed. */
1572 *client_sni = NULL;
1573 return;
1574
1575check_ipv6:
1576 /* Check for IPv6 */
1577 cp = *client_sni;
1578 while (*cp) {
1579 switch (*cp) {
1580 case '0':
1581 case '1':
1582 case '2':
1583 case '3':
1584 case '4':
1585 case '5':
1586 case '6':
1587 case '7':
1588 case '8':
1589 case '9':
1590 case 'a':
1591 case 'b':
1592 case 'c':
1593 case 'd':
1594 case 'e':
1595 case 'f':
1596 case 'A':
1597 case 'B':
1598 case 'C':
1599 case 'D':
1600 case 'E':
1601 case 'F':
1602 case ':':
1603 break;
1604 case '%':
1605 /* Start of i/f specification, Previous is IPv6 */
1606 *client_sni = NULL;
1607 return;
1608 default:
1609 /* Not of format xx:xx::xx. */
1610 return;
1611 }
1612 cp++;
1613 }
1614 *client_sni = NULL;
1615 return;
1616}
1617
1620 const coap_address_t *local_if,
1621 const coap_address_t *server,
1622 coap_proto_t proto,
1623 coap_dtls_cpsk_t *setup_data) {
1624 coap_session_t *session;
1625
1626 coap_lock_lock(ctx, return NULL);
1627 session = coap_new_client_session_psk2_lkd(ctx, local_if, server, proto, setup_data);
1628 coap_lock_unlock(ctx);
1629 return session;
1630}
1631
1634 const coap_address_t *local_if,
1635 const coap_address_t *server,
1636 coap_proto_t proto,
1637 coap_dtls_cpsk_t *setup_data) {
1638 coap_session_t *session;
1639
1641 session = coap_session_create_client(ctx, local_if, server, proto);
1642
1643 if (!session || !setup_data)
1644 return NULL;
1645
1646 session->cpsk_setup_data = *setup_data;
1647 if (setup_data->psk_info.identity.s) {
1648 session->psk_identity =
1650 setup_data->psk_info.identity.length);
1651 if (!session->psk_identity) {
1652 coap_log_warn("Cannot store session Identity (PSK)\n");
1653 coap_session_release_lkd(session);
1654 return NULL;
1655 }
1657 coap_log_warn("Identity (PSK) not defined\n");
1658 coap_session_release_lkd(session);
1659 return NULL;
1660 }
1661
1662 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1663 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1664 setup_data->psk_info.key.length);
1665 if (!session->psk_key) {
1666 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
1667 coap_session_release_lkd(session);
1668 return NULL;
1669 }
1671 coap_log_warn("Pre-shared key (PSK) not defined\n");
1672 coap_session_release_lkd(session);
1673 return NULL;
1674 }
1675
1676 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
1677
1679 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
1680 coap_session_release_lkd(session);
1681 return NULL;
1682 }
1683 }
1684 coap_log_debug("***%s: new outgoing session\n",
1685 coap_session_str(session));
1686 coap_session_check_connect(session);
1687 return session;
1688}
1689#endif /* COAP_CLIENT_SUPPORT */
1690
1691int
1693 const coap_bin_const_t *psk_hint
1694 ) {
1695 /* We may be refreshing the hint with the same hint */
1696 coap_bin_const_t *old_psk_hint = session->psk_hint;
1697
1698 if (psk_hint && psk_hint->s) {
1699 if (session->psk_hint) {
1700 if (coap_binary_equal(session->psk_hint, psk_hint))
1701 return 1;
1702 }
1703 session->psk_hint = coap_new_bin_const(psk_hint->s,
1704 psk_hint->length);
1705 if (!session->psk_hint) {
1706 coap_log_err("No memory to store identity hint (PSK)\n");
1707 if (old_psk_hint)
1708 coap_delete_bin_const(old_psk_hint);
1709 return 0;
1710 }
1711 } else {
1712 session->psk_hint = NULL;
1713 }
1714 if (old_psk_hint)
1715 coap_delete_bin_const(old_psk_hint);
1716
1717 return 1;
1718}
1719
1720int
1722 const coap_bin_const_t *psk_key
1723 ) {
1724 /* We may be refreshing the key with the same key */
1725 coap_bin_const_t *old_psk_key = session->psk_key;
1726
1727 if (psk_key && psk_key->s) {
1728 if (session->psk_key) {
1729 if (coap_binary_equal(session->psk_key, psk_key))
1730 return 1;
1731 }
1732 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1733 if (!session->psk_key) {
1734 coap_log_err("No memory to store pre-shared key (PSK)\n");
1735 if (old_psk_key)
1736 coap_delete_bin_const(old_psk_key);
1737 return 0;
1738 }
1739 } else {
1740 session->psk_key = NULL;
1741 }
1742 if (old_psk_key)
1743 coap_delete_bin_const(old_psk_key);
1744
1745 return 1;
1746}
1747
1748int
1750 const coap_bin_const_t *psk_identity
1751 ) {
1752 /* We may be refreshing the identity with the same identity */
1753 coap_bin_const_t *old_psk_identity = session->psk_identity;
1754
1755 if (psk_identity && psk_identity->s) {
1756 if (session->psk_identity) {
1757 if (coap_binary_equal(session->psk_identity, psk_identity))
1758 return 1;
1759 }
1760 session->psk_identity = coap_new_bin_const(psk_identity->s,
1761 psk_identity->length);
1762 if (!session->psk_identity) {
1763 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
1764 if (old_psk_identity)
1765 coap_delete_bin_const(old_psk_identity);
1766 return 0;
1767 }
1768 } else {
1769 session->psk_identity = NULL;
1770 }
1771 if (old_psk_identity)
1772 coap_delete_bin_const(old_psk_identity);
1773
1774 return 1;
1775}
1776
1777#if COAP_SERVER_SUPPORT
1778const coap_bin_const_t *
1780 if (session)
1781 return session->psk_hint;
1782 return NULL;
1783}
1784#endif /* COAP_SERVER_SUPPORT */
1785
1786const coap_bin_const_t *
1788 const coap_bin_const_t *psk_identity = NULL;
1789 if (session) {
1790 psk_identity = session->psk_identity;
1791 if (psk_identity == NULL) {
1792 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1793 }
1794 }
1795 return psk_identity;
1796}
1797
1798const coap_bin_const_t *
1800 if (session)
1801 return session->psk_key;
1802 return NULL;
1803}
1804
1805#if COAP_CLIENT_SUPPORT
1808 const coap_address_t *local_if,
1809 const coap_address_t *server,
1810 coap_proto_t proto,
1811 coap_dtls_pki_t *setup_data) {
1812 coap_session_t *session;
1813
1814 coap_lock_lock(ctx, return NULL);
1815 session = coap_new_client_session_pki_lkd(ctx, local_if, server, proto, setup_data);
1816 coap_lock_unlock(ctx);
1817 return session;
1818}
1819
1822 const coap_address_t *local_if,
1823 const coap_address_t *server,
1824 coap_proto_t proto,
1825 coap_dtls_pki_t *setup_data) {
1826 coap_session_t *session;
1827 coap_dtls_pki_t l_setup_data;
1828
1829 if (!setup_data)
1830 return NULL;
1831 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1832 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
1833 return NULL;
1834 }
1835
1837 l_setup_data = *setup_data;
1838 coap_sanitize_client_sni(&l_setup_data.client_sni);
1839
1840 session = coap_session_create_client(ctx, local_if, server, proto);
1841
1842 if (!session) {
1843 return NULL;
1844 }
1845
1847 /* we know that setup_data is not NULL */
1848 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
1849 coap_session_release_lkd(session);
1850 return NULL;
1851 }
1852 }
1853 coap_log_debug("***%s: new outgoing session\n",
1854 coap_session_str(session));
1855 coap_session_check_connect(session);
1856 return session;
1857}
1858#endif /* ! COAP_CLIENT_SUPPORT */
1859
1860#if COAP_SERVER_SUPPORT
1861#if !COAP_DISABLE_TCP
1864 coap_session_t *session;
1866 NULL, NULL, NULL, 0, ctx, ep);
1867 if (!session)
1868 goto error;
1869
1870 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
1871 if (!coap_netif_strm_accept(ep, session, extra))
1872 goto error;
1873
1874 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1875
1876#ifdef COAP_EPOLL_SUPPORT
1877 session->sock.session = session;
1878 coap_epoll_ctl_add(&session->sock,
1879 EPOLLIN,
1880 __func__);
1881#endif /* COAP_EPOLL_SUPPORT */
1882 SESSIONS_ADD(ep->sessions, session);
1883 if (session) {
1884 coap_log_debug("***%s: session %p: new incoming session\n",
1885 coap_session_str(session), (void *)session);
1889 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1890 }
1891 return session;
1892
1893error:
1894 /*
1895 * Need to add in the session as coap_session_release_lkd()
1896 * will call SESSIONS_DELETE in coap_session_free().
1897 */
1898 if (session) {
1899 SESSIONS_ADD(ep->sessions, session);
1900 coap_session_free(session);
1901 }
1902 return NULL;
1903}
1904#endif /* !COAP_DISABLE_TCP */
1905#endif /* COAP_SERVER_SUPPORT */
1906
1907void
1909 const uint8_t *data) {
1910 session->tx_token = coap_decode_var_bytes8(data, len);
1911 /*
1912 * Decrement as when first used by coap_session_new_token() it will
1913 * get incremented
1914 */
1915 session->tx_token--;
1916}
1917
1918void
1920 uint8_t *data) {
1921 *len = coap_encode_var_safe8(data,
1922 sizeof(session->tx_token), ++session->tx_token);
1923}
1924
1925COAP_API uint16_t
1927 uint16_t mid;
1928
1929 coap_lock_lock(session->context, return 0);
1930 mid = coap_new_message_id_lkd(session);
1931 coap_lock_unlock(session->context);
1932 return mid;
1933}
1934
1935uint16_t
1938 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1939 return ++session->tx_mid;
1940 /* TCP/TLS have no notion of mid */
1941 return 0;
1942}
1943
1944const coap_address_t *
1946 if (session)
1947 return &session->addr_info.remote;
1948 return NULL;
1949}
1950
1951const coap_address_t *
1953 if (session)
1954 return &session->addr_info.local;
1955 return NULL;
1956}
1957
1958const coap_address_t *
1960#if COAP_CLIENT_SUPPORT
1961 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
1962 session->sock.flags & COAP_SOCKET_MULTICAST)
1963 return &session->sock.mcast_addr;
1964#else /* ! COAP_CLIENT_SUPPORT */
1965 (void)session;
1966#endif /* ! COAP_CLIENT_SUPPORT */
1967 return NULL;
1968}
1969
1972 if (session)
1973 return session->context;
1974 return NULL;
1975}
1976
1979 if (session)
1980 return session->proto;
1981 return 0;
1982}
1983
1986 if (session)
1987 return session->type;
1988 return 0;
1989}
1990
1991COAP_API int
1993 int ret;
1994
1995 coap_lock_lock(session->context, return 0);
1996 ret = coap_session_set_type_client_lkd(session);
1997 coap_lock_unlock(session->context);
1998 return ret;
1999}
2000
2001int
2003#if COAP_SERVER_SUPPORT
2004 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
2006 session->type = COAP_SESSION_TYPE_CLIENT;
2007 return 1;
2008 }
2009#else /* ! COAP_SERVER_SUPPORT */
2010 (void)session;
2011#endif /* ! COAP_SERVER_SUPPORT */
2012 return 0;
2013}
2014
2017 if (session)
2018 return session->state;
2019 return 0;
2020}
2021
2022int
2024 if (session)
2025 return session->ifindex;
2026 return -1;
2027}
2028
2029void *
2031 coap_tls_library_t *tls_lib) {
2032 if (session)
2033 return coap_dtls_get_tls(session, tls_lib);
2034 return NULL;
2035}
2036
2037static const char *
2039 switch (proto) {
2040 case COAP_PROTO_UDP:
2041 return "UDP ";
2042 case COAP_PROTO_DTLS:
2043 return "DTLS";
2044 case COAP_PROTO_TCP:
2045 return "TCP ";
2046 case COAP_PROTO_TLS:
2047 return "TLS ";
2048 case COAP_PROTO_WS:
2049 return "WS ";
2050 case COAP_PROTO_WSS:
2051 return "WSS ";
2052 case COAP_PROTO_NONE:
2053 case COAP_PROTO_LAST:
2054 default:
2055 return "????" ;
2056 break;
2057 }
2058 return NULL;
2059}
2060
2061#if COAP_SERVER_SUPPORT
2063coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2064 coap_endpoint_t *endpoint;
2065
2066 coap_lock_lock(context, return NULL);
2067 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2068 coap_lock_unlock(context);
2069 return endpoint;
2070}
2071
2073coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2074 coap_proto_t proto) {
2075 coap_endpoint_t *ep = NULL;
2076
2077 assert(context);
2078 assert(listen_addr);
2079 assert(proto != COAP_PROTO_NONE);
2080
2081 coap_lock_check_locked(context);
2082
2083 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
2084 coap_log_crit("coap_new_endpoint: DTLS not supported\n");
2085 goto error;
2086 }
2087
2088 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
2089 coap_log_crit("coap_new_endpoint: TLS not supported\n");
2090 goto error;
2091 }
2092
2093 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
2094 coap_log_crit("coap_new_endpoint: TCP not supported\n");
2095 goto error;
2096 }
2097
2098 if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
2099 coap_log_crit("coap_new_endpoint: WS not supported\n");
2100 goto error;
2101 }
2102
2103 if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
2104 coap_log_crit("coap_new_endpoint: WSS not supported\n");
2105 goto error;
2106 }
2107
2108 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2109 proto == COAP_PROTO_WSS) {
2111 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2112 "coap_context_set_pki() not called\n");
2113 goto error;
2114 }
2115 }
2116
2117 ep = coap_malloc_endpoint();
2118 if (!ep) {
2119 coap_log_warn("coap_new_endpoint: malloc");
2120 goto error;
2121 }
2122
2123 memset(ep, 0, sizeof(coap_endpoint_t));
2124 ep->context = context;
2125 ep->proto = proto;
2126 ep->sock.endpoint = ep;
2127 assert(proto < COAP_PROTO_LAST);
2128 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2129
2130 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2131 if (!coap_netif_dgrm_listen(ep, listen_addr))
2132 goto error;
2133#ifdef WITH_CONTIKI
2134 ep->sock.context = context;
2135#endif /* WITH_CONTIKI */
2136#if !COAP_DISABLE_TCP
2137 } else if (COAP_PROTO_RELIABLE(proto)) {
2138 if (!coap_netif_strm_listen(ep, listen_addr))
2139 goto error;
2140#endif /* !COAP_DISABLE_TCP */
2141 } else {
2142 coap_log_crit("coap_new_endpoint: protocol not supported\n");
2143 goto error;
2144 }
2145
2147#ifndef INET6_ADDRSTRLEN
2148#define INET6_ADDRSTRLEN 40
2149#endif
2150 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2151
2152 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2153 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2154 addr_str);
2155 }
2156 }
2157
2159
2160#ifdef COAP_EPOLL_SUPPORT
2161 ep->sock.endpoint = ep;
2163 EPOLLIN,
2164 __func__);
2165#endif /* COAP_EPOLL_SUPPORT */
2166
2167 LL_PREPEND(context->endpoint, ep);
2168 return ep;
2169
2170error:
2172 return NULL;
2173}
2174
2175void
2177 ep->default_mtu = (uint16_t)mtu;
2178}
2179
2180COAP_API void
2182 if (ep) {
2183 coap_context_t *context = ep->context;
2184 if (context) {
2185 coap_lock_lock(context, return);
2186 }
2188 if (context) {
2189 coap_lock_unlock(context);
2190 }
2191 }
2192}
2193
2194void
2196 if (ep) {
2197 coap_session_t *session, *rtmp;
2198
2199 if (ep->context) {
2200 /* If fully allocated and inserted */
2202 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2203 assert(session->ref == 0);
2204 if (session->ref == 0) {
2206 coap_session_free(session);
2207 }
2208 }
2209 if (coap_netif_available_ep(ep)) {
2210 /*
2211 * ep->sock.endpoint is set in coap_new_endpoint().
2212 * ep->sock.session is never set.
2213 *
2214 * session->sock.session is set for both clients and servers (when a
2215 * new session is accepted), but does not affect the endpoint.
2216 *
2217 * So, it is safe to call coap_netif_close_ep() after all the sessions
2218 * have been freed above as we are only working with the endpoint sock.
2219 */
2220#ifdef COAP_EPOLL_SUPPORT
2221 assert(ep->sock.session == NULL);
2222#endif /* COAP_EPOLL_SUPPORT */
2224 }
2225
2226 if (ep->context->endpoint) {
2227 LL_DELETE(ep->context->endpoint, ep);
2228 }
2229 }
2231 }
2232}
2233#endif /* COAP_SERVER_SUPPORT */
2234
2237 const coap_address_t *remote_addr,
2238 int ifindex) {
2239 coap_session_t *s, *rtmp;
2240#if COAP_CLIENT_SUPPORT
2241 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2242 if (s->ifindex == ifindex) {
2243 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2244 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2245 return s;
2246 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2247 return s;
2248 }
2249 }
2250#endif /* COAP_CLIENT_SUPPORT */
2251#if COAP_SERVER_SUPPORT
2252 coap_endpoint_t *ep;
2253
2254 LL_FOREACH(ctx->endpoint, ep) {
2255 SESSIONS_ITER(ep->sessions, s, rtmp) {
2256 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2257 remote_addr))
2258 return s;
2259 }
2260 }
2261#endif /* COAP_SERVER_SUPPORT */
2262 return NULL;
2263}
2264
2265#ifndef INET6_ADDRSTRLEN
2266#define INET6_ADDRSTRLEN 46
2267#endif
2268const char *
2270 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2271 char *p = szSession, *end = szSession + sizeof(szSession);
2272 if (coap_print_addr(&session->addr_info.local,
2273 (unsigned char *)p, end - p) > 0)
2274 p += strlen(p);
2275 if (p + 6 < end) {
2276 strcpy(p, " <-> ");
2277 p += 5;
2278 }
2279 if (p + 1 < end) {
2280 if (coap_print_addr(&session->addr_info.remote,
2281 (unsigned char *)p, end - p) > 0)
2282 p += strlen(p);
2283 }
2284 if (session->ifindex > 0 && p + 1 < end)
2285 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2286 if (p + 6 < end) {
2287 strcpy(p, " ");
2288 p++;
2289 strcpy(p, coap_proto_name(session->proto));
2290 }
2291
2292 return szSession;
2293}
2294
2295#if COAP_SERVER_SUPPORT
2296const char *
2297coap_endpoint_str(const coap_endpoint_t *endpoint) {
2298 static char szEndpoint[128];
2299 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2300 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2301 p += strlen(p);
2302 if (p + 6 < end) {
2303 if (endpoint->proto == COAP_PROTO_UDP) {
2304 strcpy(p, " UDP");
2305 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2306 strcpy(p, " DTLS");
2307 } else {
2308 strcpy(p, " NONE");
2309 }
2310 }
2311
2312 return szEndpoint;
2313}
2314#endif /* COAP_SERVER_SUPPORT */
2315#if COAP_CLIENT_SUPPORT
2316void
2318 session->no_observe_cancel = 1;
2319}
2320#endif /* COAP_CLIENT_SUPPORT */
2321#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:4575
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:2722
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
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_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1708
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:2767
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:101
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:239
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.
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:190
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:413
#define COAP_PDU_MAX_UDP_HEADER_SIZE
#define COAP_PDU_DELAYED
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_MAX_MESSAGE_SIZE_TCP0
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1485
#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:776
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:37
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:99
#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)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
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.
int coap_session_set_type_client_lkd(coap_session_t *session)
Set the session type to client.
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.
COAP_API int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
coap_session_state_t
coap_session_state_t values
#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.
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
uint8_t doing_send_recv
Set if coap_send_recv() active.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
application-specific data
uint32_t tx_rtag
Next Request-Tag number to use.
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_bin_const_t * req_token
Token in request pdu of coap_send_recv()
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
coap_mid_t last_con_mid
The last CON mid that has been been processed.
coap_session_type_t type
client or server side socket
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
coap_pdu_t * cached_pdu
Cached copy of last ACK response PDU.
int ifindex
interface index
coap_bin_const_t * echo
last token used to make a request
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_session_t * session
Used to determine session owner.
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
coap_address_t mcast_addr
remote address and port (multicast track)
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values