libcoap 4.3.5-develop-5347a62
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-2026 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
16
18
19#ifndef COAP_SESSION_C_
20#define COAP_SESSION_C_
21
22#include <stdio.h>
23
24#if defined(HAVE_INET_PTON) && defined(HAVE_ARPA_INET_H)
25#include <arpa/inet.h>
26#endif /* HAVE_INET_PTON && HAVE_ARPA_INET_H */
27
28#ifdef COAP_EPOLL_SUPPORT
29#include <sys/epoll.h>
30#include <sys/timerfd.h>
31#endif /* COAP_EPOLL_SUPPORT */
32
36 uint32_t fr = fp1.fractional_part * fp2.fractional_part;
37
38 res.integer_part = fp1.integer_part * fp2.integer_part + fr/1000;
39 res.fractional_part = fr % 1000;
40 return res;
41}
42
46 uint32_t fr = fp1.fractional_part * u2;
47
48 res.integer_part = fp1.integer_part * u2 + fr/1000;
49 res.fractional_part = fr % 1000;
50 return res;
51}
52
56 uint32_t fr = fp1.fractional_part + fp2.fractional_part;
57
58 res.integer_part = fp1.integer_part + fp2.integer_part + fr/1000;
59 res.fractional_part = fr % 1000;
60 return res;
61}
62
65 coap_fixed_point_t res = fp1;
66
67 res.integer_part += u2;
68 return res;
69}
70
73 coap_fixed_point_t res = fp1;
74
75 res.integer_part -= u2;
76 return res;
77}
78
82 uint32_t num = (fp1.integer_part * 1000 + fp1.fractional_part) / u2;
83
84 res.integer_part = num / 1000;
85 res.fractional_part = num % 1000;
86 return res;
87}
88
89#if COAP_Q_BLOCK_SUPPORT
93 uint8_t ran;
94
95 coap_prng_lkd(&ran, sizeof(ran));
97 res = coap_multi_fixed_uint(res, ran);
98 res = coap_div_fixed_uint(res, 0xff);
99 res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res);
100 return res;
101}
102
108
109 return ticks;
110}
111
112/*
113 * Save away derived Congestion Control parameters for speed of access.
114 * They will get updated whenever a component variable is updated.
115 */
116
117/*
118 * NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
119 * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM
120 *
121 * Do not include NON_TIMEOUT_RANDOM as that changes
122 */
123static void
124coap_session_fix_non_probing_wait_base(coap_session_t *s) {
126
128 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
132}
133
134/*
135 * NON_PARTIAL_TIMEOUT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
136 * ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT
137 */
138static void
139coap_session_fix_non_partial_timeout(coap_session_t *s) {
141
143 ((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
148}
149#endif /* COAP_Q_BLOCK_SUPPORT */
150
151void
153 if (value.integer_part > 0 && value.fractional_part < 1000) {
154 session->ack_timeout = value;
155 coap_log_debug("***%s: session ack_timeout set to %u.%03u\n",
156 coap_session_str(session), session->ack_timeout.integer_part,
158 }
159}
160
161void
163 coap_fixed_point_t value) {
164 if (value.integer_part > 0 && value.fractional_part < 1000) {
165 session->ack_random_factor = value;
166 coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n",
169#if COAP_Q_BLOCK_SUPPORT
170 coap_session_fix_non_probing_wait_base(session);
171 coap_session_fix_non_partial_timeout(session);
172#endif /* COAP_Q_BLOCK_SUPPORT */
173 }
174 return;
175}
176
177void
179 if (value > 0xff)
180 value = 0xff;
181 if (value > 0) {
182 session->max_retransmit = value;
183 coap_log_debug("***%s: session max_retransmit set to %u\n",
184 coap_session_str(session), session->max_retransmit);
185 }
186}
187
188void
189coap_session_set_nstart(coap_session_t *session, uint16_t value) {
190 if (value > 0) {
191 session->nstart = value;
192 coap_log_debug("***%s: session nstart set to %u\n",
193 coap_session_str(session), session->nstart);
194 }
195}
196
197void
199 coap_fixed_point_t value) {
200 if (value.integer_part > 0 && value.fractional_part < 1000) {
201 session->default_leisure = value;
202 coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
205 }
206}
207
208void
210 if (value > 0) {
211 session->probing_rate = value;
212 coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
213 coap_session_str(session), session->probing_rate);
214 }
215}
216
217void
219#if COAP_Q_BLOCK_SUPPORT
220 if (value > 0) {
221 session->max_payloads = value;
222 coap_log_debug("***%s: session max_payloads set to %u\n",
223 coap_session_str(session), session->max_payloads);
224 coap_session_fix_non_probing_wait_base(session);
225 coap_session_fix_non_partial_timeout(session);
226 }
227#else /* ! COAP_Q_BLOCK_SUPPORT */
228 (void)session;
229 (void)value;
230#endif /* ! COAP_Q_BLOCK_SUPPORT */
231}
232
233void
235#if COAP_Q_BLOCK_SUPPORT
236 if (value > 0) {
237 session->non_max_retransmit = value;
238 coap_log_debug("***%s: session non_max_retransmit set to %u\n",
239 coap_session_str(session), session->non_max_retransmit);
240 coap_session_fix_non_probing_wait_base(session);
241 coap_session_fix_non_partial_timeout(session);
242 }
243#else /* ! COAP_Q_BLOCK_SUPPORT */
244 (void)session;
245 (void)value;
246#endif /* ! COAP_Q_BLOCK_SUPPORT */
247}
248
249void
251 coap_fixed_point_t value) {
252#if COAP_Q_BLOCK_SUPPORT
253 if (value.integer_part > 0 && value.fractional_part < 1000) {
254 session->non_timeout = value;
255 coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
256 coap_session_str(session), session->non_timeout.integer_part,
257 session->non_timeout.fractional_part);
258 coap_session_fix_non_probing_wait_base(session);
259 coap_session_fix_non_partial_timeout(session);
260 }
261#else /* ! COAP_Q_BLOCK_SUPPORT */
262 (void)session;
263 (void)value;
264#endif /* ! COAP_Q_BLOCK_SUPPORT */
265}
266
267void
269 coap_fixed_point_t value) {
270#if COAP_Q_BLOCK_SUPPORT
271 if (value.integer_part > 0 && value.fractional_part < 1000)
272 session->non_receive_timeout = value;
273 coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
274 coap_session_str(session),
275 session->non_receive_timeout.integer_part,
276 session->non_receive_timeout.fractional_part);
277#else /* ! COAP_Q_BLOCK_SUPPORT */
278 (void)session;
279 (void)value;
280#endif /* ! COAP_Q_BLOCK_SUPPORT */
281}
282
285 return session->ack_timeout;
286}
287
290 return session->ack_random_factor;
291}
292
293uint16_t
295 return session->max_retransmit;
296}
297
298uint16_t
300 return session->nstart;
301}
302
305 return session->default_leisure;
306}
307
308uint32_t
310 return session->probing_rate;
311}
312
313uint16_t
315#if COAP_Q_BLOCK_SUPPORT
316 return session->max_payloads;
317#else /* ! COAP_Q_BLOCK_SUPPORT */
318 (void)session;
320#endif /* ! COAP_Q_BLOCK_SUPPORT */
321}
322
323uint16_t
325#if COAP_Q_BLOCK_SUPPORT
326 return session->non_max_retransmit;
327#else /* ! COAP_Q_BLOCK_SUPPORT */
328 (void)session;
330#endif /* ! COAP_Q_BLOCK_SUPPORT */
331}
332
335#if COAP_Q_BLOCK_SUPPORT
336 return session->non_timeout;
337#else /* ! COAP_Q_BLOCK_SUPPORT */
338 (void)session;
340#endif /* ! COAP_Q_BLOCK_SUPPORT */
341}
342
345#if COAP_Q_BLOCK_SUPPORT
346 return session->non_receive_timeout;
347#else /* ! COAP_Q_BLOCK_SUPPORT */
348 (void)session;
350#endif /* ! COAP_Q_BLOCK_SUPPORT */
351}
352
355 coap_lock_lock(return NULL);
358 return session;
359}
360
363 ++session->ref;
364 return session;
365}
366
367COAP_API void
369 if (session) {
370#if COAP_THREAD_SAFE
371 coap_context_t *context = session->context;
372 (void)context;
373#endif /* COAP_THREAD_SAFE */
374
375 coap_lock_lock(return);
378 }
379}
380
381void
383 if (session) {
385#ifndef __COVERITY__
386 assert(session->ref > 0);
387 if (session->ref > 0)
388 --session->ref;
389 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
390 coap_session_free(session);
391#else /* __COVERITY__ */
392 /* Coverity scan is fooled by the reference counter leading to
393 * false positives for USE_AFTER_FREE. */
394 --session->ref;
395 __coverity_negative_sink__(session->ref);
396 /* Indicate that resources are released properly. */
397 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
398 __coverity_free__(session);
399 }
400#endif /* __COVERITY__ */
401 }
402}
403
404void
406 if (session) {
409 session->hold_in_use++;
410 }
411}
412
413void
415 if (session) {
417 assert(session->hold_in_use > 0);
418 if (session->hold_in_use == 1 && session->disconnect_pending &&
419 !session->disconnecting) {
420 session->disconnect_pending = 0;
422 }
423 if (session->hold_in_use > 0)
424 session->hold_in_use--;
426 }
427}
428
429COAP_API void
430coap_session_set_app_data(coap_session_t *session, void *app_data) {
431 assert(session);
432 coap_lock_lock(return);
433 coap_session_set_app_data2_lkd(session, app_data, NULL);
435}
436
437void *
439 assert(session);
440 return session->app_data;
441}
442
443COAP_API void *
446 void *old_data;
447
448 coap_lock_lock(return NULL);
449 old_data = coap_session_set_app_data2_lkd(session, app_data, callback);
451 return old_data;
452}
453
454void *
457 void *old_data = session->app_data;
458
459 session->app_data = app_data;
460 session->app_cb = app_data ? callback : NULL;
461 return old_data;
462}
463
464static coap_session_t *
466 const coap_addr_hash_t *addr_hash,
467 const coap_address_t *local_addr,
468 const coap_address_t *remote_addr, int ifindex,
469 coap_context_t *context, coap_endpoint_t *endpoint) {
471 sizeof(coap_session_t));
472#if ! COAP_SERVER_SUPPORT
473 (void)endpoint;
474#endif /* ! COAP_SERVER_SUPPORT */
475 if (!session)
476 return NULL;
477 memset(session, 0, sizeof(*session));
478 session->proto = proto;
479 session->type = type;
480 if (addr_hash)
481 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
482 else
483 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
484 if (local_addr) {
485 coap_address_copy(&session->addr_info.local, local_addr);
486#if COAP_CLIENT_SUPPORT
487 coap_address_copy(&session->local_reconnect, local_addr);
488#endif /* COAP_CLIENT_SUPPORT */
489 } else {
491#if COAP_CLIENT_SUPPORT
492 coap_address_init(&session->local_reconnect);
493#endif /* COAP_CLIENT_SUPPORT */
494 }
495 if (remote_addr)
496 coap_address_copy(&session->addr_info.remote, remote_addr);
497 else
499 session->ifindex = ifindex;
500 session->context = context;
501#if COAP_CLIENT_SUPPORT
502 if (type == COAP_SESSION_TYPE_CLIENT) {
503 session->client_initiated = 1;
504 }
505#endif /* COAP_CLIENT_SUPPORT */
506#if COAP_SERVER_SUPPORT
507 session->endpoint = endpoint;
508 if (endpoint)
509 session->mtu = endpoint->default_mtu;
510 else
511#endif /* COAP_SERVER_SUPPORT */
513 session->block_mode = context->block_mode;
514#if COAP_Q_BLOCK_SUPPORT
515 if (session->block_mode & COAP_BLOCK_FORCE_Q_BLOCK) {
516 set_block_mode_has_q(session->block_mode);
517 }
518#endif
519 if (proto == COAP_PROTO_DTLS) {
520 session->tls_overhead = 29;
521 if (session->tls_overhead >= session->mtu) {
522 session->tls_overhead = session->mtu;
523 coap_log_err("DTLS overhead exceeds MTU\n");
524 }
525 }
526 session->rl_ticks_per_packet = context->rl_ticks_per_packet;
530 session->nstart = COAP_DEFAULT_NSTART;
533#if COAP_Q_BLOCK_SUPPORT
534 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
535 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
536 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
537 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
538 coap_session_fix_non_probing_wait_base(session);
539 coap_session_fix_non_partial_timeout(session);
540#endif /* COAP_Q_BLOCK_SUPPORT */
541 session->dtls_event = -1;
545 session->max_token_size = context->max_token_size; /* RFC8974 */
546 if (session->type != COAP_SESSION_TYPE_CLIENT)
548
549 /* Randomly initialize */
550 /* TCP/TLS have no notion of mid */
551 if (COAP_PROTO_NOT_RELIABLE(session->proto))
552 coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
553 coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
554
555 return session;
556}
557
558void
560 coap_queue_t *q;
561 coap_lg_xmit_t *lq, *ltmp;
562
563#if COAP_PROXY_SUPPORT
564 if (session->ref_proxy_subs)
565 coap_delete_proxy_subscriber(session, NULL, 0, COAP_PROXY_SUBS_ALL);
566#endif /* COAP_PROXY_SUPPORT */
567#if COAP_CLIENT_SUPPORT
568 coap_lg_crcv_t *lg_crcv, *etmp;
569
570 /* Need to do this before (D)TLS and socket is closed down */
571 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
572 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
573 /* Need to close down observe */
574 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
575 /* Need to delete node we set up for NON */
576 coap_queue_t *queue = session->context->sendqueue;
577
578 while (queue) {
579 if (queue->session == session) {
581 break;
582 }
583 queue = queue->next;
584 }
585 }
586 }
587 /* In case coap_cancel_observe_lkd() failure, which could clear down lg_crcv */
588 if (!session->lg_crcv)
589 break;
590 LL_DELETE(session->lg_crcv, lg_crcv);
591 coap_block_delete_lg_crcv(session, lg_crcv);
592 }
593#endif /* COAP_CLIENT_SUPPORT */
594
595 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
596 if (q->pdu->type==COAP_MESSAGE_CON) {
597 coap_handle_nack(session, q->pdu,
598 session->proto == COAP_PROTO_DTLS ?
600 q->id);
601 }
603 }
604
605#if COAP_CLIENT_SUPPORT
606 coap_pdu_t *p, *ptmp;
607
608 LL_FOREACH_SAFE(session->doing_first_pdu, p, ptmp) {
609 if (p->type==COAP_MESSAGE_CON) {
610 coap_handle_nack(session, p,
611 session->proto == COAP_PROTO_DTLS ?
613 p->mid);
614 }
616 }
617#endif /* COAP_CLIENT_SUPPORT */
618
619 if (session->partial_pdu)
621 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
622 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
623 if (session->psk_identity)
625 if (session->psk_key)
627 if (session->psk_hint)
629
630#ifdef HAVE_LINUX_ERRQUEUE_H
631 coap_delete_str_const(session->last_data_write);
632#endif /* HAVE_LINUX_ERRQUEUE_H */
633
634#if COAP_SERVER_SUPPORT
635 coap_cache_entry_t *cp, *ctmp;
636 HASH_ITER(hh, session->context->cache, cp, ctmp) {
637 /* cp->session is NULL if not session based */
638 if (cp->session == session) {
639 coap_delete_cache_entry(session->context, cp);
640 }
641 }
642#endif /* COAP_PROXY_SUPPORT */
643 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
644 LL_DELETE(session->lg_xmit, lq);
645 coap_block_delete_lg_xmit(session, lq);
646 }
647#if COAP_SERVER_SUPPORT
648 coap_lg_srcv_t *sq, *stmp;
649
650 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
651 LL_DELETE(session->lg_srcv, sq);
652 coap_block_delete_lg_srcv(session, sq);
653 }
654#endif /* COAP_SERVER_SUPPORT */
655#if COAP_OSCORE_SUPPORT
657 oscore_release_recipient_ctx(&session->recipient_ctx);
658 coap_delete_str_const(session->b_2_retransmit_token);
659#endif /* COAP_OSCORE_SUPPORT */
660#if COAP_WS_SUPPORT
661 if (session->ws)
662 coap_free_type(COAP_STRING, session->ws->rd_data);
663 coap_free_type(COAP_STRING, session->ws);
664 coap_delete_str_const(session->ws_host);
665#endif /* COAP_WS_SUPPORT */
666}
667
668void
670 if (!session)
671 return;
673 assert(session->ref == 0);
674 if (session->ref)
675 return;
676 /* Make sure nothing gets deleted under our feet */
678 coap_session_mfree(session);
679#if COAP_SERVER_SUPPORT
680 coap_delete_bin_const(session->client_cid);
681 if (session->endpoint) {
682 if (session->endpoint->sessions)
683 SESSIONS_DELETE(session->endpoint->sessions, session);
684 } else
685#endif /* COAP_SERVER_SUPPORT */
686#if COAP_CLIENT_SUPPORT
687 if (session->context) {
688 if (session->context->sessions)
689 SESSIONS_DELETE(session->context->sessions, session);
690 }
691 coap_delete_bin_const(session->req_token);
692#endif /* COAP_CLIENT_SUPPORT */
694 coap_delete_bin_const(session->echo);
695#if COAP_SERVER_SUPPORT
696 coap_delete_pdu_lkd(session->last_resp_pdu);
697#endif /* COAP_SERVER_SUPPORT */
698
699 if (session->app_cb) {
700 coap_lock_callback(session->app_cb(session->app_data));
701 }
702 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
703 (void *)session);
704 assert(session->ref == 1);
706}
707
708#if COAP_SERVER_SUPPORT
709void
711 int i;
712 coap_queue_t *q;
713
716 coap_cancel_all_messages(session->context, session, NULL);
717 RESOURCES_ITER(session->context->resources, r) {
718 /* In case code is broken somewhere */
719 for (i = 0; i < 1000; i++) {
720 if (!coap_delete_observer(r, session, NULL))
721 break;
722 }
723 }
724 if (session->context->unknown_resource) {
725 /* In case code is broken somewhere */
726 for (i = 0; i < 1000; i++) {
727 if (!coap_delete_observer(session->context->unknown_resource, session, NULL))
728 break;
729 }
730 }
731 if (session->context->proxy_uri_resource) {
732 /* In case code is broken somewhere */
733 for (i = 0; i < 1000; i++) {
734 if (!coap_delete_observer(session->context->proxy_uri_resource, session, NULL))
735 break;
736 }
737 }
738 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
740 }
741 /* Force session to go away */
744
746}
747#endif /* COAP_SERVER_SUPPORT */
748
749static size_t
751 size_t max_with_header) {
752#if COAP_DISABLE_TCP
753 (void)session;
754 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
755 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
756 : 0;
757#else /* !COAP_DISABLE_TCP */
758 if (COAP_PROTO_NOT_RELIABLE(session->proto))
759 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
760 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
761 : 0;
762 /* we must assume there is no token to be on the safe side */
763 if (max_with_header <= 2)
764 return 0;
765 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
766 return max_with_header - 2;
767 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
768 return max_with_header - 3;
769 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
770 return max_with_header - 4;
771 else
772 return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE;
773#endif /* !COAP_DISABLE_TCP */
774}
775
776size_t
778 if (session->csm_rcv_mtu) {
779#if COAP_WS_SUPPORT
780 if (session->proto == COAP_PROTO_WS || session->proto == COAP_PROTO_WSS)
781 return session->csm_rcv_mtu > COAP_PDU_WS_HEADER_SIZE ?
783#endif /* COAP_WS_SUPPORT */
785 (size_t)(session->csm_rcv_mtu));
786 }
787
789 (size_t)(session->mtu - session->tls_overhead));
790}
791
792COAP_API size_t
794 size_t size;
795 coap_session_t *session_rw;
796
797 /*
798 * Need to do this to not get a compiler warning about const parameters
799 * but need to maintain source code backward compatibility
800 */
801 memcpy(&session_rw, &session, sizeof(session_rw));
802 coap_lock_lock(return 0);
803 size = coap_session_max_pdu_size_lkd(session_rw);
805 return size;
806}
807
808size_t
810 size_t max_with_header;
811
813#if COAP_CLIENT_SUPPORT
814 if (COAP_PROTO_RELIABLE(session->proto) &&
815 session->type == COAP_SESSION_TYPE_CLIENT &&
816 session->doing_first) {
817 /*
818 * Delay if session->doing_first is set.
819 * E.g. Reliable and CSM not in yet for checking block support
820 */
821 coap_session_t *session_rw;
822
823 /*
824 * Need to do this to not get a compiler warning about const parameters
825 * but need to maintain source code backward compatibility
826 */
827 memcpy(&session_rw, &session, sizeof(session_rw));
828 if (coap_client_delay_first(session_rw) == 0) {
829 coap_log_debug("coap_client_delay_first: timeout\n");
830 /* Have to go with the defaults */
831 }
832 }
833#endif /* COAP_CLIENT_SUPPORT */
834
835 max_with_header = (size_t)(session->mtu - session->tls_overhead);
836
837 return coap_session_max_pdu_size_internal(session, max_with_header);
838}
839
840void
841coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
844 coap_log_debug("* %s: Restricting MTU size to %u\n",
845 coap_session_str(session), mtu);
846 }
847 if (mtu < 64)
848 mtu = 64;
849 session->mtu = mtu;
850 if (session->tls_overhead >= session->mtu) {
851 session->tls_overhead = session->mtu;
852 coap_log_err("DTLS overhead exceeds MTU\n");
853 }
854}
855
856ssize_t
858 coap_queue_t *node) {
859 if (node) {
860 coap_queue_t *removed = NULL;
861 /* Nodes are per ID, no notion of token */
862 coap_remove_from_queue(&session->context->sendqueue, session, node->id,
863 NULL, &removed);
864 assert(removed == node);
865 /* reuse node */
867 node->session = NULL;
868 node->t = 0;
869 } else {
870 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
871 coap_queue_t *q = NULL;
872 /* Check same mid is not getting reused in violation of RFC7252 */
873 LL_FOREACH(session->delayqueue, q) {
874 if (q->id == pdu->mid) {
875 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
876 coap_session_str(session), pdu->mid);
877 return COAP_INVALID_MID;
878 }
879 }
880 }
881 node = coap_new_node();
882 if (node == NULL)
883 return COAP_INVALID_MID;
884 node->id = pdu->mid;
885 node->pdu = pdu;
886 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
887 uint8_t r;
888 coap_prng_lkd(&r, sizeof(r));
889 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
890 node->timeout = coap_calc_timeout(session, r);
891 }
892 coap_address_copy(&node->remote, &session->addr_info.remote);
893 }
894 coap_add_to_tail_delayq(session, node);
896 coap_log_debug("** %s: mid=0x%04x: delayed\n",
897 coap_session_str(session), node->id);
898 return COAP_PDU_DELAYED;
899}
900
901#if !COAP_DISABLE_TCP
902void
904 coap_pdu_t *pdu;
905 uint8_t buf[4];
906 uint32_t max_message_size = session->context->csm_max_message_size;
907
908 assert(COAP_PROTO_RELIABLE(session->proto));
909 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
910 session->state = COAP_SESSION_STATE_CSM;
911 session->partial_write = 0;
912 if (session->mtu == 0)
913 coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */
914#if COAP_WS_SUPPORT
915 if (session->proto == COAP_PROTO_WS || session->proto == COAP_PROTO_WSS) {
916 /* The PDU reserves more headroom than the two-byte CoAP header on WS. */
917 const uint32_t pdu_limit = COAP_DEFAULT_MAX_PDU_RX_SIZE -
919
920 /* coap_ws_read() returns a whole CoAP message in the receive buffer. */
921 if (max_message_size > COAP_RXBUFFER_SIZE)
922 max_message_size = COAP_RXBUFFER_SIZE;
923 if (max_message_size > pdu_limit)
924 max_message_size = pdu_limit;
925 }
926#endif /* COAP_WS_SUPPORT */
928 if (pdu == NULL
930 coap_encode_var_safe(buf, sizeof(buf),
931 max_message_size), buf) == 0
933 coap_encode_var_safe(buf, sizeof(buf),
934 0), buf) == 0
935 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
938 coap_encode_var_safe(buf, sizeof(buf),
939 session->max_token_size),
940 buf) == 0)
941 || coap_pdu_encode_header(pdu, session->proto) == 0
942 ) {
944 } else {
945 ssize_t bytes_written;
946
947 pdu->session = session;
948 bytes_written = coap_session_send_pdu(session, pdu);
949 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
951 } else {
952 session->csm_rcv_mtu = max_message_size;
953 if (session->csm_rcv_mtu > COAP_BERT_BASE)
954 session->csm_bert_loc_support = 1;
955 else
956 session->csm_bert_loc_support = 0;
957 }
958 }
959 if (pdu)
961}
962#endif /* !COAP_DISABLE_TCP */
963
966 coap_mid_t mid;
967
969 mid = coap_session_send_ping_lkd(session);
970 if (mid != COAP_INVALID_MID) {
971 coap_tick_t now;
972
973 coap_ticks(&now);
974 session->last_ping_mid = mid;
975 session->last_rx_tx = now;
976 session->last_ping = now;
977 }
979 return mid;
980}
981
984 coap_pdu_t *ping = NULL;
985
987 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
988 session->con_active)
989 return COAP_INVALID_MID;
990 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
991 uint16_t mid = coap_new_message_id_lkd(session);
992 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
993 }
994#if !COAP_DISABLE_TCP
995 else {
997 }
998#endif /* !COAP_DISABLE_TCP */
999 if (!ping)
1000 return COAP_INVALID_MID;
1001 return coap_send_internal(session, ping, NULL);
1002}
1003
1004void
1006 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
1007 coap_log_debug("***%s: session connected\n",
1008 coap_session_str(session));
1009 if (session->state == COAP_SESSION_STATE_CSM) {
1011#if COAP_CLIENT_SUPPORT
1013 coap_reset_doing_first(session);
1014#endif /* COAP_CLIENT_SUPPORT */
1015 }
1016 }
1017
1019 session->partial_write = 0;
1020
1021 if (session->proto==COAP_PROTO_DTLS) {
1022 session->tls_overhead = coap_dtls_get_overhead(session);
1023 if (session->tls_overhead >= session->mtu) {
1024 session->tls_overhead = session->mtu;
1025 coap_log_err("DTLS overhead exceeds MTU\n");
1026 }
1027 }
1028
1029 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED &&
1030 !session->disconnect_pending) {
1031 ssize_t bytes_written;
1032 coap_queue_t *q = session->delayqueue;
1033 coap_address_t remote;
1034
1035 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
1036 if (session->con_active >= COAP_NSTART(session))
1037 break;
1038 session->con_active++;
1039 }
1040 /* Take entry off the queue */
1041 q = coap_remove_first_from_delayq(session);
1042
1043 coap_address_copy(&remote, &session->addr_info.remote);
1044 coap_address_copy(&session->addr_info.remote, &q->remote);
1045 coap_log_debug("** %s: mid=0x%04x: transmitted after delay (2)\n",
1046 coap_session_str(session), (int)q->id);
1047 bytes_written = coap_session_send_pdu(session, q->pdu);
1048 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
1049 if (coap_wait_ack(session->context, session, q) >= 0)
1050 q = NULL;
1051 }
1052 coap_address_copy(&session->addr_info.remote, &remote);
1053 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1054 if (q)
1056 if (bytes_written < 0)
1057 break;
1058 } else if (q) {
1059 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
1060 coap_add_to_head_delayq(session, q);
1061 if (bytes_written > 0)
1062 session->partial_write = (size_t)bytes_written;
1063 break;
1064 } else {
1066 }
1067 }
1068 }
1069}
1070
1071#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
1072static const char *
1074 switch (reason) {
1076 return "COAP_NACK_TOO_MANY_RETRIES";
1078 return "COAP_NACK_NOT_DELIVERABLE";
1079 case COAP_NACK_RST:
1080 return "COAP_NACK_RST";
1082 return "COAP_NACK_TLS_FAILED";
1084 return "COAP_NACK_ICMP_ISSUE";
1086 return "COAP_NACK_BAD_RESPONSE";
1088 return "COAP_NACK_TLS_LAYER_FAILED";
1090 return "COAP_NACK_WS_LAYER_FAILED";
1092 return "COAP_NACK_WS_FAILED";
1093 default:
1094 return "???";
1095 }
1096}
1097#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
1098
1099void
1101 coap_pdu_t *sent,
1102 const coap_nack_reason_t reason,
1103 const coap_mid_t mid) {
1104 if (session->context->nack_cb
1106 && !session->doing_first_pdu
1107#endif /* COAP_CLIENT_SUPPORT */
1108 ) {
1109 coap_bin_const_t token = {0, NULL};
1110
1111 if (sent) {
1112 coap_check_update_token(session, sent);
1113 token = sent->actual_token;
1114 }
1115 coap_lock_callback(session->context->nack_cb(session, sent, reason, mid));
1116 if (sent) {
1117 coap_update_token(sent, token.length, token.s);
1118 }
1119 }
1120#if COAP_CLIENT_SUPPORT
1121 if (reason != COAP_NACK_ICMP_ISSUE) {
1122 session->doing_send_recv = 0;
1123 }
1124#endif /* COAP_CLIENT_SUPPORT */
1125}
1126
1127COAP_API void
1133
1134void
1136 coap_nack_reason_t reason) {
1138 if (session->hold_in_use == 0 && !session->disconnecting) {
1139 coap_session_disconnected_do_it_lkd(session, reason);
1140 return;
1141 }
1142 if (!session->disconnect_pending) {
1143 coap_log_debug("***%s: session in use, disconnect deferred\n",
1144 coap_session_str(session));
1145 session->disconnect_pending = 1;
1146 session->disconnect_reason = reason;
1147 }
1148#if COAP_CLIENT_SUPPORT
1149 /* Any coap_send_recv() waiting on this session returns now */
1150 session->doing_send_recv = 0;
1151#endif /* COAP_CLIENT_SUPPORT */
1152}
1153
1154void
1156#if !COAP_DISABLE_TCP
1157 coap_session_state_t state = session->state;
1158#endif /* !COAP_DISABLE_TCP */
1159 coap_lg_xmit_t *lq, *ltmp;
1160#if COAP_SERVER_SUPPORT
1161 coap_lg_srcv_t *sq, *stmp;
1162#endif /* COAP_SERVER_SUPPORT */
1163#if COAP_CLIENT_SUPPORT
1164 coap_lg_crcv_t *cq, *etmp;
1165#endif /* COAP_CLIENT_SUPPORT */
1166 int sent_nack = 0;
1167 uint8_t disconnecting = session->disconnecting;
1168 coap_queue_t *q;
1169
1171 /*
1172 * A disconnect asked for from within the callbacks below does not
1173 * disconnect the session again.
1174 */
1175 session->disconnecting = 1;
1176#if COAP_CLIENT_SUPPORT
1177 coap_session_failed(session);
1178#endif /* COAP_CLIENT_SUPPORT */
1179
1180 q = session->context->sendqueue;
1181 while (q) {
1182 if (q->session == session) {
1183 /* Take the first one */
1184 coap_handle_nack(session, q->pdu, reason, q->id);
1185 sent_nack = 1;
1186 break;
1187 }
1188 q = q->next;
1189 }
1190
1191 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
1192 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1193 coap_session_str(session), q->id);
1194 if (q->pdu->type == COAP_MESSAGE_CON) {
1195 coap_handle_nack(session, q->pdu, reason, q->id);
1196 sent_nack = 1;
1197 }
1198
1200 }
1201#if COAP_CLIENT_SUPPORT
1202 session->doing_send_recv = 0;
1203#endif /* COAP_CLIENT_SUPPORT */
1204#if COAP_CLIENT_SUPPORT
1205 if (!sent_nack && session->lg_crcv) {
1206 /* Take the first one */
1207 coap_handle_nack(session, session->lg_crcv->sent_pdu, reason,
1208 session->lg_crcv->sent_pdu->mid);
1209 sent_nack = 1;
1210 }
1211#endif /* COAP_CLIENT_SUPPORT */
1212 if (!sent_nack) {
1213 /* Unable to determine which request disconnection was for */
1214 coap_handle_nack(session, NULL, reason, 0);
1215 }
1216 coap_log_debug("***%s: session disconnected (%s)\n",
1217 coap_session_str(session), coap_nack_name(reason));
1218#if COAP_SERVER_SUPPORT
1219 coap_delete_observers(session->context, session);
1220#endif /* COAP_SERVER_SUPPORT */
1221
1222 if (session->proto == COAP_PROTO_UDP)
1224 else
1225 session->state = COAP_SESSION_STATE_NONE;
1226
1227 session->con_active = 0;
1228
1229 if (session->partial_pdu) {
1231 session->partial_pdu = NULL;
1232 }
1233 session->partial_read = 0;
1234
1235 /* Not done if nack handler called above */
1236 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
1237 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1238 coap_session_str(session), q->id);
1239#if COAP_CLIENT_SUPPORT
1240 session->doing_send_recv = 0;
1241#endif /* COAP_CLIENT_SUPPORT */
1243 }
1244
1245#if COAP_CLIENT_SUPPORT
1246 if (!session->session_failed) {
1247 /* Need to do this before (D)TLS and socket is closed down */
1248 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1249 LL_DELETE(session->lg_crcv, cq);
1250 coap_block_delete_lg_crcv(session, cq);
1251 }
1252 }
1253#endif /* COAP_CLIENT_SUPPORT */
1254 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1255 LL_DELETE(session->lg_xmit, lq);
1256 coap_block_delete_lg_xmit(session, lq);
1257 }
1258#if COAP_SERVER_SUPPORT
1259 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1260 LL_DELETE(session->lg_srcv, sq);
1261 coap_block_delete_lg_srcv(session, sq);
1262 }
1263#endif /* COAP_SERVER_SUPPORT */
1264 coap_cancel_session_messages(session->context, session, reason);
1265
1266#if !COAP_DISABLE_TCP
1267 if (COAP_PROTO_RELIABLE(session->proto)) {
1268 if (coap_netif_available(session)) {
1272 }
1273#if COAP_CLIENT_SUPPORT
1274 if (state != COAP_SESSION_STATE_NONE && !session->session_failed) {
1278 }
1279 coap_reset_doing_first(session);
1280#endif /* COAP_CLIENT_SUPPORT */
1281 }
1282#endif /* !COAP_DISABLE_TCP */
1283 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1284 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1285 session->disconnecting = disconnecting;
1286 session->disconnect_pending = 0;
1287}
1288
1289#if COAP_CLIENT_SUPPORT
1290void
1292 if (session->context->reconnect_time && session->client_initiated) {
1293 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1294 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1295 session->session_failed = 1;
1297 session->retry_count++;
1298 if (session->context->retry_count && session->retry_count >= session->context->retry_count) {
1300 if (session->type != COAP_SESSION_TYPE_CLIENT) {
1302 coap_session_release_lkd(session);
1303 }
1304 }
1305 coap_ticks(&session->last_rx_tx);
1306 }
1307}
1308#endif /* COAP_CLIENT_SUPPORT */
1309
1310#if COAP_SERVER_SUPPORT
1311static void
1312coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1313 const coap_addr_tuple_t *addr_info) {
1314 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1315 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1316 addr_hash->lport = coap_address_get_port(&addr_info->local);
1317 addr_hash->proto = proto;
1318}
1319
1320void
1321coap_session_cid_update_addr(coap_session_t *session) {
1322 if (!session->cid_addr_pending || !session->endpoint)
1323 return;
1324 session->cid_addr_pending = 0;
1325 /* Updating IP address */
1326 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1327 SESSIONS_DELETE(session->endpoint->sessions, session);
1328 session->addr_info = session->cid_addr_info;
1329 coap_make_addr_hash(&session->addr_hash, session->endpoint->proto,
1330 &session->addr_info);
1331 SESSIONS_ADD(session->endpoint->sessions, session);
1332 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1333}
1334
1335/* Aids debugging when session is created */
1336static coap_session_t *debug_session;
1337
1339coap_endpoint_get_session(coap_endpoint_t *endpoint,
1340 const coap_packet_t *packet, coap_tick_t now) {
1341 coap_session_t *session;
1342 coap_session_t *rtmp;
1343 unsigned int num_idle = 0;
1344 unsigned int num_hs = 0;
1345 coap_session_t *oldest = NULL;
1346 coap_session_t *oldest_hs = NULL;
1347 coap_addr_hash_t addr_hash;
1348
1349 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1350 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1351 if (session) {
1352 /* Maybe mcast or unicast IP address which is not in the hash */
1353 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1354 session->ifindex = packet->ifindex;
1355 session->last_rx_tx = now;
1356 /* Packet is from the current address, not from a new CID address */
1357 session->cid_addr_pending = 0;
1358 return session;
1359 }
1360
1361#if COAP_CLIENT_SUPPORT
1362 SESSIONS_FIND(endpoint->context->sessions, addr_hash, session);
1363 if (session) {
1364 /* Maybe mcast or unicast IP address which is not in the hash */
1365 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1366 session->ifindex = packet->ifindex;
1367 session->last_rx_tx = now;
1368 return session;
1369 }
1370
1371 if (coap_is_mcast(&packet->addr_info.local)) {
1372 /* Check if this a proxy client packet we sent on another socket */
1373 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1374 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1377 /* Drop looped back packet to stop recursion / confusion */
1378 return NULL;
1379 }
1380 }
1381 }
1382#endif /* COAP_CLIENT_SUPPORT */
1383 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1384 if (session->ref == 0 && session->delayqueue == NULL &&
1385 session->lg_srcv == NULL && session->lg_xmit == NULL) {
1386 if (
1388 !(session->client_initiated && endpoint->context->reconnect_time) &&
1389#endif /* COAP_CLIENT_SUPPORT */
1390 session->type == COAP_SESSION_TYPE_SERVER) {
1391 ++num_idle;
1392 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1393 oldest = session;
1394
1395 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1396 ++num_hs;
1397 /* See if this is a partial (D)TLS session set up
1398 which needs to be cleared down to prevent DOS */
1399 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1400 if (oldest_hs == NULL ||
1401 session->last_rx_tx < oldest_hs->last_rx_tx)
1402 oldest_hs = session;
1403 }
1404 }
1405 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1406 ++num_hs;
1407 /* See if this is a partial (D)TLS session set up for Client Hello
1408 which needs to be cleared down to prevent DOS */
1409 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1410 if (oldest_hs == NULL ||
1411 session->last_rx_tx < oldest_hs->last_rx_tx)
1412 oldest_hs = session;
1413 }
1414 }
1415 }
1416 }
1417
1418 if (endpoint->context->max_idle_sessions > 0 &&
1419 num_idle >= endpoint->context->max_idle_sessions) {
1421 coap_session_free(oldest);
1422 } else if (oldest_hs) {
1423 coap_log_warn("***%s: Incomplete session timed out\n",
1424 coap_session_str(oldest_hs));
1426 coap_session_free(oldest_hs);
1427 }
1428
1429 if (num_hs > (endpoint->context->max_handshake_sessions ?
1430 endpoint->context->max_handshake_sessions :
1432 /* Maxed out on number of sessions in (D)TLS negotiation state */
1433 coap_log_debug("Outstanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1434 "large. New request ignored\n");
1435 return NULL;
1436 }
1437
1438 if (endpoint->proto == COAP_PROTO_DTLS) {
1439 /*
1440 * Need to check that this actually is a Client Hello before wasting
1441 * time allocating and then freeing off session.
1442 */
1443
1444 /*
1445 * Generic header structure of the DTLS record layer.
1446 * typedef struct __attribute__((__packed__)) {
1447 * uint8_t content_type; content type of the included message
1448 * uint16_t version; Protocol version
1449 * uint16_t epoch; counter for cipher state changes
1450 * uint8_t sequence_number[6]; sequence number
1451 * uint16_t length; length of the following fragment
1452 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1453 * } dtls_record_handshake_t;
1454 */
1455#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1456#define DTLS_CT_ALERT 21 /* Content Type Alert */
1457#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1458#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1459#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1460#define DTLS_CT_CID 25 /* Content Type Connection ID */
1461#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1462#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1463
1464 const uint8_t *payload = (const uint8_t *)packet->payload;
1465 size_t length = packet->length;
1466 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1467 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%" PRIuS " < %d) dropped\n",
1468 payload[OFF_CONTENT_TYPE], length,
1469 OFF_HANDSHAKE_TYPE + 1);
1470 return NULL;
1471 }
1472 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1473 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1474 /* Client may have changed its IP address */
1475 int changed = 0;
1476
1477 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1478 if (session->client_cid) {
1479 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1480 length > (OFF_CID_DTLS13 + session->client_cid->length) &&
1481 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1482 session->client_cid->length) == 0) ||
1483 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1484 length > (OFF_CID + session->client_cid->length) &&
1485 memcmp(session->client_cid->s, &payload[OFF_CID],
1486 session->client_cid->length) == 0)) {
1487 /*
1488 * The record has not been authenticated yet, so only remember
1489 * the new address. coap_session_cid_update_addr() moves the
1490 * session once the DTLS layer has decrypted the record
1491 * (RFC 9146 Section 6).
1492 */
1493 coap_log_debug("***%s: CID: Client Session may have new address\n",
1494 coap_session_str(session));
1495 session->cid_addr_info = packet->addr_info;
1496 session->cid_addr_pending = 1;
1497 return session;
1498 }
1499 }
1500 }
1501 if (!changed) {
1502 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1503 return NULL;
1504 }
1505 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1506 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1507 /* only log if not a late alert */
1508 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1509 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1510 payload[OFF_CONTENT_TYPE],
1511 payload[OFF_HANDSHAKE_TYPE]);
1512 return NULL;
1513 }
1514 }
1515
1516 session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER,
1517 &addr_hash, &packet->addr_info.local,
1518 &packet->addr_info.remote,
1519 packet->ifindex, endpoint->context, endpoint);
1520 /* Aids debugging endpoint created session */
1521 debug_session = session;
1522 if (session) {
1523 session->last_rx_tx = now;
1524 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1525 sizeof(session->sock.lfunc));
1526 if (endpoint->proto == COAP_PROTO_UDP)
1528 else if (endpoint->proto == COAP_PROTO_DTLS) {
1529 session->type = COAP_SESSION_TYPE_HELLO;
1530 }
1531 SESSIONS_ADD(endpoint->sessions, session);
1532 coap_log_debug("***%s: session %p: new incoming session\n",
1533 coap_session_str(session), (void *)session);
1535 }
1536 return session;
1537}
1538
1541 coap_tick_t now) {
1542 if (session) {
1543 session->last_rx_tx = now;
1544 session->type = COAP_SESSION_TYPE_SERVER;
1545 coap_dtls_establish(session);
1546 }
1547 return session;
1548}
1549#endif /* COAP_SERVER_SUPPORT */
1550
1551#if COAP_CLIENT_SUPPORT
1552static coap_session_t *
1553coap_session_create_client(coap_context_t *ctx,
1554 const coap_address_t *local_if,
1555 const coap_address_t *server,
1556 coap_proto_t proto,
1557 void *app_data,
1559 coap_str_const_t *ws_host) {
1560 coap_session_t *session = NULL;
1561 int default_port = COAP_DEFAULT_PORT;
1562
1563 assert(server);
1564
1565 switch (proto) {
1566 case COAP_PROTO_UDP:
1567 default_port = COAP_DEFAULT_PORT;
1568 break;
1569 case COAP_PROTO_DTLS:
1570 if (!coap_dtls_is_supported()) {
1571 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1572 return NULL;
1573 }
1574 default_port = COAPS_DEFAULT_PORT;
1575 break;
1576 case COAP_PROTO_TCP:
1577 if (!coap_tcp_is_supported()) {
1578 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1579 return NULL;
1580 }
1581 default_port = COAP_DEFAULT_PORT;
1582 break;
1583 case COAP_PROTO_TLS:
1584 if (!coap_tls_is_supported()) {
1585 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1586 return NULL;
1587 }
1588 default_port = COAPS_DEFAULT_PORT;
1589 break;
1590 case COAP_PROTO_WS:
1591 if (!coap_ws_is_supported()) {
1592 coap_log_crit("coap_new_client_session*: WS not supported\n");
1593 return NULL;
1594 }
1595 default_port = 80;
1596 break;
1597 case COAP_PROTO_WSS:
1598 if (!coap_wss_is_supported()) {
1599 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1600 return NULL;
1601 }
1602 default_port = 443;
1603 break;
1604 case COAP_PROTO_NONE:
1605 case COAP_PROTO_LAST:
1606 default:
1607 assert(0);
1608 return NULL;
1609 }
1611 local_if, server, 0, ctx, NULL);
1612 if (!session)
1613 goto error;
1614
1616 session->sock.session = session;
1617 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1618 sizeof(session->sock.lfunc));
1619
1620 session->app_data = app_data;
1621 session->app_cb = app_data ? callback : NULL;
1622#if COAP_WS_SUPPORT
1623 if (ws_host) {
1624 session->ws_host = coap_new_str_const(ws_host->s, ws_host->length);
1625 }
1626#else /* ! COAP_WS_SUPPORT */
1627 (void)ws_host;
1628#endif /* ! COAP_WS_SUPPORT */
1629
1630 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1631 coap_session_t *s, *rtmp;
1632 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1633 goto error;
1634 }
1635 /* Check that this is not a duplicate 4-tuple */
1636 SESSIONS_ITER_CL_SAFE(ctx->sessions, s, rtmp) {
1639 &s->addr_info.local) &&
1641 &s->addr_info.remote)) {
1642 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1643 coap_session_str(session), (void *)session);
1644 goto error;
1645 }
1646 }
1647#ifdef WITH_CONTIKI
1648 session->sock.context = ctx;
1649#endif /* WITH_CONTIKI */
1650#if !COAP_DISABLE_TCP
1651 } else if (COAP_PROTO_RELIABLE(proto)) {
1652 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1653 goto error;
1654 }
1655#endif /* !COAP_DISABLE_TCP */
1656 }
1657
1658 session->sock.session = session;
1659#ifdef COAP_EPOLL_SUPPORT
1660 coap_epoll_ctl_add(&session->sock,
1661 EPOLLIN |
1662 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1663 EPOLLOUT : 0),
1664 __func__);
1665#endif /* COAP_EPOLL_SUPPORT */
1666
1668 if (local_if)
1669 session->sock.flags |= COAP_SOCKET_BOUND;
1670#if COAP_SERVER_SUPPORT
1671 if (ctx->proxy_uri_resource)
1672 session->proxy_session = 1;
1673#endif /* COAP_SERVER_SUPPORT */
1674 SESSIONS_ADD(ctx->sessions, session);
1675 return session;
1676
1677error:
1678 /*
1679 * Need to add in the session as coap_session_release_lkd()
1680 * will call SESSIONS_DELETE in coap_session_free().
1681 */
1682 if (session)
1683 SESSIONS_ADD(ctx->sessions, session);
1684 coap_session_release_lkd(session);
1685 return NULL;
1686}
1687
1688static void
1689coap_session_check_connect(coap_session_t *session) {
1690 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1691 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1692 }
1693#if !COAP_DISABLE_TCP
1694 if (COAP_PROTO_RELIABLE(session->proto)) {
1695 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1697 if (session->client_initiated) {
1698 session->doing_first = 1;
1699 }
1700 } else {
1701 /* Initial connect worked immediately */
1702 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1703 }
1704 }
1705#endif /* !COAP_DISABLE_TCP */
1706 coap_ticks(&session->last_rx_tx);
1707}
1708#endif /* COAP_CLIENT_SUPPORT */
1709
1710void
1712 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1713 coap_session_connected(session);
1714#if !COAP_DISABLE_TCP
1715 if (COAP_PROTO_RELIABLE(session->proto))
1716 coap_session_send_csm(session);
1717#endif /* !COAP_DISABLE_TCP */
1718}
1719
1720#if COAP_CLIENT_SUPPORT
1721int
1723 int default_port = COAP_DEFAULT_PORT;
1724
1725 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close && !session->session_failed)
1726 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1727
1728 switch (session->proto) {
1729 case COAP_PROTO_UDP:
1730 default_port = COAP_DEFAULT_PORT;
1731 break;
1732 case COAP_PROTO_DTLS:
1733 default_port = COAPS_DEFAULT_PORT;
1734 break;
1735 case COAP_PROTO_TCP:
1736 default_port = COAP_DEFAULT_PORT;
1737 break;
1738 case COAP_PROTO_TLS:
1739 default_port = COAPS_DEFAULT_PORT;
1740 break;
1741 case COAP_PROTO_WS:
1742 default_port = 80;
1743 break;
1744 case COAP_PROTO_WSS:
1745 default_port = 443;
1746 break;
1747 case COAP_PROTO_NONE:
1748 case COAP_PROTO_LAST:
1749 default:
1750 assert(0);
1751 return 0;
1752 }
1753 session->sock.session = session;
1754 coap_log_debug("***%s: trying to reconnect\n", coap_session_str(session));
1756 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1757 session->state = COAP_SESSION_STATE_NONE;
1758 if (!coap_netif_dgrm_connect(session, &session->local_reconnect, &session->addr_info.remote,
1759 default_port)) {
1760 goto error;
1761 }
1763#ifdef WITH_CONTIKI
1764 session->sock.context = session->context;
1765#endif /* WITH_CONTIKI */
1766#if !COAP_DISABLE_TCP
1767 } else if (COAP_PROTO_RELIABLE(session->proto)) {
1768 if (!coap_netif_strm_connect1(session, &session->local_reconnect, &session->addr_info.remote,
1769 default_port)) {
1770 goto error;
1771 }
1772#endif /* !COAP_DISABLE_TCP */
1773 } else {
1774 goto error;
1775 }
1776#ifdef COAP_EPOLL_SUPPORT
1777 coap_epoll_ctl_add(&session->sock,
1778 EPOLLIN |
1779 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1780 EPOLLOUT : 0),
1781 __func__);
1782#endif /* COAP_EPOLL_SUPPORT */
1783
1785 session->sock.flags |= COAP_SOCKET_BOUND;
1786 coap_session_check_connect(session);
1787 return 1;
1788error:
1789 return 0;
1790}
1791
1792void
1794 coap_lg_crcv_t *lg_crcv, *etmp;
1795
1796 if (!session->session_failed)
1797 return;
1798 coap_log_debug("***%s: session re-established\n",
1799 coap_session_str(session));
1800 session->session_failed = 0;
1801 session->retry_count = 0;
1803 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
1804 coap_pdu_reference_lkd(lg_crcv->sent_pdu);
1805 coap_send_internal(session, lg_crcv->sent_pdu, NULL);
1806 }
1807}
1808
1811 const coap_address_t *local_if,
1812 const coap_address_t *server,
1813 coap_proto_t proto) {
1814 coap_session_t *session;
1815
1816 coap_lock_lock(return NULL);
1817 session = coap_new_client_session3_lkd(ctx, local_if, server, proto, NULL, NULL, NULL);
1819 return session;
1820}
1821
1824 const coap_address_t *local_if,
1825 const coap_address_t *server,
1826 coap_proto_t proto,
1827 void *app_data,
1829 coap_str_const_t *ws_host) {
1830 coap_session_t *session;
1831
1832 coap_lock_lock(return NULL);
1833 session = coap_new_client_session3_lkd(ctx, local_if, server, proto, app_data, callback, ws_host);
1835 return session;
1836}
1837
1840 const coap_address_t *local_if,
1841 const coap_address_t *server,
1842 coap_proto_t proto,
1843 void *app_data,
1845 coap_str_const_t *ws_host) {
1846 coap_session_t *session;
1847
1849 session = coap_session_create_client(ctx, local_if, server,
1850 proto, app_data, callback, ws_host);
1851 if (session) {
1852 coap_log_debug("***%s: session %p: created outgoing session\n",
1853 coap_session_str(session), (void *)session);
1854 coap_session_check_connect(session);
1855 }
1856 return session;
1857}
1858
1861 const coap_address_t *local_if,
1862 const coap_address_t *server,
1863 coap_proto_t proto, const char *identity,
1864 const uint8_t *key, unsigned key_len) {
1865 coap_session_t *session;
1866
1867 coap_lock_lock(return NULL);
1868 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1870 return session;
1871}
1872
1875 const coap_address_t *local_if,
1876 const coap_address_t *server,
1877 coap_proto_t proto, const char *identity,
1878 const uint8_t *key, unsigned key_len) {
1879 coap_dtls_cpsk_t setup_data;
1880
1882 memset(&setup_data, 0, sizeof(setup_data));
1884
1885 if (identity) {
1886 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1887 setup_data.psk_info.identity.length = strlen(identity);
1888 }
1889
1890 if (key && key_len > 0) {
1891 setup_data.psk_info.key.s = key;
1892 setup_data.psk_info.key.length = key_len;
1893 }
1894
1895 return coap_new_client_session_psk3_lkd(ctx, local_if, server,
1896 proto, &setup_data, NULL, NULL, NULL);
1897}
1898
1899/*
1900 * Check the validity of the SNI to send to the server.
1901 *
1902 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1903 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1904 */
1905static void
1906coap_sanitize_client_sni(char **client_sni) {
1907
1908 if (*client_sni == NULL)
1909 return;
1910
1911#if defined(HAVE_INET_PTON) && defined(HAVE_ARPA_INET_H)
1912 coap_address_t addr;
1913#if COAP_IPV4_SUPPORT
1914 if (inet_pton(AF_INET, *client_sni, &addr.addr.sin.sin_addr) == 1) {
1915 *client_sni = NULL;
1916 return;
1917 }
1918#endif /* COAP_IPV4_SUPPORT */
1919#if COAP_IPV6_SUPPORT
1920 if (inet_pton(AF_INET6, *client_sni, &addr.addr.sin6.sin6_addr) == 1) {
1921 *client_sni = NULL;
1922 return;
1923 }
1924#endif /* COAP_IPV6_SUPPORT */
1925#else /* ! HAVE_INET_PTON || ! HAVE_ARPA_INET_H */
1926 char *cp;
1927 char has_colon = 0;
1928
1929 cp = *client_sni;
1930 switch (*cp) {
1931 case '0':
1932 case '1':
1933 case '2':
1934 case '3':
1935 case '4':
1936 case '5':
1937 case '6':
1938 case '7':
1939 case '8':
1940 case '9':
1941 case 'a':
1942 case 'b':
1943 case 'c':
1944 case 'd':
1945 case 'e':
1946 case 'f':
1947 case 'A':
1948 case 'B':
1949 case 'C':
1950 case 'D':
1951 case 'E':
1952 case 'F':
1953 case ':':
1954 break;
1955 case '\000':
1956 /* Empty entry invalid */
1957 *client_sni = NULL;
1958 return;
1959 default:
1960 /* Does not start with a hex digit or : - not literal IP. */
1961 return;
1962 }
1963 /* Check for IPv4 */
1964 while (*cp) {
1965 switch (*cp) {
1966 case '0':
1967 case '1':
1968 case '2':
1969 case '3':
1970 case '4':
1971 case '5':
1972 case '6':
1973 case '7':
1974 case '8':
1975 case '9':
1976 case '.':
1977 break;
1978 default:
1979 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1980 goto check_ipv6;
1981 }
1982 cp++;
1983 }
1984 /* IPv4 address - not allowed. */
1985 *client_sni = NULL;
1986 return;
1987
1988check_ipv6:
1989 /* Check for IPv6 */
1990 cp = *client_sni;
1991 while (*cp) {
1992 switch (*cp) {
1993 case '0':
1994 case '1':
1995 case '2':
1996 case '3':
1997 case '4':
1998 case '5':
1999 case '6':
2000 case '7':
2001 case '8':
2002 case '9':
2003 case 'a':
2004 case 'b':
2005 case 'c':
2006 case 'd':
2007 case 'e':
2008 case 'f':
2009 case 'A':
2010 case 'B':
2011 case 'C':
2012 case 'D':
2013 case 'E':
2014 case 'F':
2015 break;
2016 case ':':
2017 has_colon = 1;
2018 break;
2019 case '%':
2020 /* Start of i/f specification, Previous is IPv6 */
2021 *client_sni = NULL;
2022 return;
2023 default:
2024 /* Not of format xx:xx::xx. */
2025 return;
2026 }
2027 cp++;
2028 }
2029 if (has_colon)
2030 *client_sni = NULL;
2031#endif /* ! HAVE_INET_PTON || ! HAVE_ARPA_INET_H */
2032 return;
2033}
2034
2037 const coap_address_t *local_if,
2038 const coap_address_t *server,
2039 coap_proto_t proto,
2040 coap_dtls_cpsk_t *setup_data) {
2041 coap_session_t *session;
2042
2043 coap_lock_lock(return NULL);
2044 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, setup_data,
2045 NULL, NULL, NULL);
2047 return session;
2048}
2049
2052 const coap_address_t *local_if,
2053 const coap_address_t *server,
2054 coap_proto_t proto,
2055 coap_dtls_cpsk_t *setup_data,
2056 void *app_data,
2058 coap_str_const_t *ws_host) {
2059 coap_session_t *session;
2060
2061 coap_lock_lock(return NULL);
2062 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto,
2063 setup_data, app_data, callback, ws_host);
2065 return session;
2066}
2067
2070 const coap_address_t *local_if,
2071 const coap_address_t *server,
2072 coap_proto_t proto,
2073 coap_dtls_cpsk_t *setup_data,
2074 void *app_data,
2076 coap_str_const_t *ws_host) {
2077 coap_session_t *session;
2078
2080 session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host);
2081
2082 if (!session || !setup_data)
2083 return NULL;
2084
2085 session->cpsk_setup_data = *setup_data;
2086 if (setup_data->psk_info.identity.s) {
2087 session->psk_identity =
2089 setup_data->psk_info.identity.length);
2090 if (!session->psk_identity) {
2091 coap_log_warn("Cannot store session Identity (PSK)\n");
2092 coap_session_release_lkd(session);
2093 return NULL;
2094 }
2096 coap_log_warn("Identity (PSK) not defined\n");
2097 coap_session_release_lkd(session);
2098 return NULL;
2099 }
2100
2101 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
2102 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
2103 setup_data->psk_info.key.length);
2104 if (!session->psk_key) {
2105 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
2106 coap_session_release_lkd(session);
2107 return NULL;
2108 }
2110 coap_log_warn("Pre-shared key (PSK) not defined\n");
2111 coap_session_release_lkd(session);
2112 return NULL;
2113 }
2114
2115 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
2116
2118 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
2119 coap_session_release_lkd(session);
2120 return NULL;
2121 }
2122 }
2123 coap_log_debug("***%s: new outgoing session\n",
2124 coap_session_str(session));
2125 coap_session_check_connect(session);
2126 return session;
2127}
2128#endif /* COAP_CLIENT_SUPPORT */
2129
2130int
2132 const coap_bin_const_t *psk_hint
2133 ) {
2134 /* We may be refreshing the hint with the same hint */
2135 coap_bin_const_t *old_psk_hint = session->psk_hint;
2136
2137 if (psk_hint && psk_hint->s) {
2138 if (session->psk_hint) {
2139 if (coap_binary_equal(session->psk_hint, psk_hint))
2140 return 1;
2141 }
2142 session->psk_hint = coap_new_bin_const(psk_hint->s,
2143 psk_hint->length);
2144 if (!session->psk_hint) {
2145 coap_log_err("No memory to store identity hint (PSK)\n");
2146 if (old_psk_hint)
2147 coap_delete_bin_const(old_psk_hint);
2148 return 0;
2149 }
2150 } else {
2151 session->psk_hint = NULL;
2152 }
2153 if (old_psk_hint)
2154 coap_delete_bin_const(old_psk_hint);
2155
2156 return 1;
2157}
2158
2159int
2161 const coap_bin_const_t *psk_key
2162 ) {
2163 /* We may be refreshing the key with the same key */
2164 coap_bin_const_t *old_psk_key = session->psk_key;
2165
2166 if (psk_key && psk_key->s) {
2167 if (session->psk_key) {
2168 if (coap_binary_equal(session->psk_key, psk_key))
2169 return 1;
2170 }
2171 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
2172 if (!session->psk_key) {
2173 coap_log_err("No memory to store pre-shared key (PSK)\n");
2174 if (old_psk_key)
2175 coap_delete_bin_const(old_psk_key);
2176 return 0;
2177 }
2178 } else {
2179 session->psk_key = NULL;
2180 }
2181 if (old_psk_key)
2182 coap_delete_bin_const(old_psk_key);
2183
2184 return 1;
2185}
2186
2187int
2189 const coap_bin_const_t *psk_identity
2190 ) {
2191 /* We may be refreshing the identity with the same identity */
2192 coap_bin_const_t *old_psk_identity = session->psk_identity;
2193
2194 if (psk_identity && psk_identity->s) {
2195 if (session->psk_identity) {
2196 if (coap_binary_equal(session->psk_identity, psk_identity))
2197 return 1;
2198 }
2199 session->psk_identity = coap_new_bin_const(psk_identity->s,
2200 psk_identity->length);
2201 if (!session->psk_identity) {
2202 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
2203 if (old_psk_identity)
2204 coap_delete_bin_const(old_psk_identity);
2205 return 0;
2206 }
2207 } else {
2208 session->psk_identity = NULL;
2209 }
2210 if (old_psk_identity)
2211 coap_delete_bin_const(old_psk_identity);
2212
2213 return 1;
2214}
2215
2216#if COAP_SERVER_SUPPORT
2217const coap_bin_const_t *
2219 if (session)
2220 return session->psk_hint;
2221 return NULL;
2222}
2223#endif /* COAP_SERVER_SUPPORT */
2224
2225const coap_bin_const_t *
2227 const coap_bin_const_t *psk_identity = NULL;
2228 if (session) {
2229 psk_identity = session->psk_identity;
2230 if (psk_identity == NULL) {
2231 psk_identity = &session->cpsk_setup_data.psk_info.identity;
2232 }
2233 }
2234 return psk_identity;
2235}
2236
2237const coap_bin_const_t *
2239 if (session)
2240 return session->psk_key;
2241 return NULL;
2242}
2243
2244#if COAP_CLIENT_SUPPORT
2247 const coap_address_t *local_if,
2248 const coap_address_t *server,
2249 coap_proto_t proto,
2250 coap_dtls_pki_t *setup_data) {
2251 coap_session_t *session;
2252
2253 coap_lock_lock(return NULL);
2254 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data,
2255 NULL, NULL, NULL);
2257 return session;
2258}
2259
2262 const coap_address_t *local_if,
2263 const coap_address_t *server,
2264 coap_proto_t proto,
2265 coap_dtls_pki_t *setup_data,
2266 void *app_data,
2268 coap_str_const_t *ws_host) {
2269 coap_session_t *session;
2270
2271 coap_lock_lock(return NULL);
2272 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data,
2273 app_data, callback, ws_host);
2275 return session;
2276}
2277
2280 const coap_address_t *local_if,
2281 const coap_address_t *server,
2282 coap_proto_t proto,
2283 coap_dtls_pki_t *setup_data,
2284 void *app_data,
2286 coap_str_const_t *ws_host) {
2287 coap_session_t *session;
2288 coap_dtls_pki_t l_setup_data;
2289
2290 if (!setup_data)
2291 return NULL;
2292 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
2293 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
2294 return NULL;
2295 }
2296
2298 l_setup_data = *setup_data;
2299 coap_sanitize_client_sni(&l_setup_data.client_sni);
2300
2301 session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host);
2302
2303 if (!session) {
2304 return NULL;
2305 }
2306
2308 /* we know that setup_data is not NULL */
2309 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
2310 coap_session_release_lkd(session);
2311 return NULL;
2312 }
2313 }
2314 coap_log_debug("***%s: new outgoing session\n",
2315 coap_session_str(session));
2316 coap_session_check_connect(session);
2317 return session;
2318}
2319#endif /* ! COAP_CLIENT_SUPPORT */
2320
2321#if COAP_SERVER_SUPPORT
2322#if !COAP_DISABLE_TCP
2324coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra) {
2325 coap_session_t *session;
2326 coap_tick_t now;
2327
2328 session = coap_make_session(ep->proto, COAP_SESSION_TYPE_SERVER,
2329 NULL, NULL, NULL, 0, ctx, ep);
2330 if (!session)
2331 goto error;
2332
2333 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
2334 if (!coap_netif_strm_accept(ep, session, extra))
2335 goto error;
2336
2337 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
2338
2339 session->sock.session = session;
2340#ifdef COAP_EPOLL_SUPPORT
2341 coap_epoll_ctl_add(&session->sock,
2342 EPOLLIN,
2343 __func__);
2344#endif /* COAP_EPOLL_SUPPORT */
2345 SESSIONS_ADD(ep->sessions, session);
2346 if (session) {
2347 coap_ticks(&now);
2348 session->last_rx_tx = now;
2349 coap_log_debug("***%s: session %p: new incoming session\n",
2350 coap_session_str(session), (void *)session);
2354 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
2355 }
2356 return session;
2357
2358error:
2359 /*
2360 * Need to add in the session as coap_session_release_lkd()
2361 * will call SESSIONS_DELETE in coap_session_free().
2362 */
2363 if (session) {
2364 SESSIONS_ADD(ep->sessions, session);
2365 coap_session_free(session);
2366 }
2367 return NULL;
2368}
2369#endif /* !COAP_DISABLE_TCP */
2370#endif /* COAP_SERVER_SUPPORT */
2371
2372void
2374 const uint8_t *data) {
2375 session->tx_token = coap_decode_var_bytes8(data, len);
2376 /*
2377 * Decrement as when first used by coap_session_new_token() it will
2378 * get incremented
2379 */
2380 session->tx_token--;
2381}
2382
2383void
2385 uint8_t *data) {
2386 *len = coap_encode_var_safe8(data,
2387 sizeof(session->tx_token), ++session->tx_token);
2388}
2389
2390COAP_API uint16_t
2392 uint16_t mid;
2393
2394 coap_lock_lock(return 0);
2395 mid = coap_new_message_id_lkd(session);
2397 return mid;
2398}
2399
2400uint16_t
2403 if (COAP_PROTO_NOT_RELIABLE(session->proto))
2404 return ++session->tx_mid;
2405 /* TCP/TLS have no notion of mid */
2406 return 0;
2407}
2408
2409const coap_address_t *
2411 if (session)
2412 return &session->addr_info.remote;
2413 return NULL;
2414}
2415
2416const coap_address_t *
2418 if (session)
2419 return &session->addr_info.local;
2420 return NULL;
2421}
2422
2423const coap_address_t *
2425#if COAP_CLIENT_SUPPORT
2426 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
2427 session->sock.flags & COAP_SOCKET_MULTICAST)
2428 return &session->sock.mcast_addr;
2429#else /* ! COAP_CLIENT_SUPPORT */
2430 (void)session;
2431#endif /* ! COAP_CLIENT_SUPPORT */
2432 return NULL;
2433}
2434
2437 if (session)
2438 return session->context;
2439 return NULL;
2440}
2441
2444 if (session)
2445 return session->proto;
2446 return 0;
2447}
2448
2451 if (session)
2452 return session->type;
2453 return 0;
2454}
2455
2456int
2458#if COAP_OSCORE_SUPPORT
2459 return (session && session->oscore_encryption != 0) ? 1 : 0;
2460#else
2461 (void)session;
2462 return 0;
2463#endif
2464}
2465
2466COAP_API int
2468 int ret;
2469
2470 coap_lock_lock(return 0);
2471 ret = coap_session_set_type_client_lkd(session, 1);
2473 return ret;
2474}
2475
2476int
2478#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2479 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
2481 session->type = COAP_SESSION_TYPE_CLIENT;
2482 if (session->endpoint) {
2483 SESSIONS_DELETE(session->endpoint->sessions, session);
2484 SESSIONS_ADD(session->context->sessions, session);
2485 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2486 session->sock = session->endpoint->sock;
2487 session->sock.flags |= COAP_SOCKET_SLAVE;
2488 session->sock.flags &= ~COAP_SOCKET_CAN_READ;
2489 session->sock.flags &= ~COAP_SOCKET_WANT_READ;
2490 } else {
2491 session->sock.endpoint = NULL;
2492 }
2493 session->endpoint = NULL;
2494 }
2495 if (report_changed) {
2496 coap_log_debug("***%s: session now is type Client\n",
2497 coap_session_str(session));
2498 }
2499 return 1;
2500 }
2501#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2502 (void)session;
2503 (void)report_changed;
2504#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2505 return 0;
2506}
2507
2508COAP_API int
2510 int ret;
2511
2512 coap_lock_lock(return 0);
2513 ret = coap_session_set_type_server_lkd(session);
2515 return ret;
2516}
2517
2518int
2520#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2521 if (session && session->type == COAP_SESSION_TYPE_CLIENT) {
2522 coap_endpoint_t *ep;
2523 coap_mid_t mid;
2524 coap_pdu_t *ping = NULL;
2525
2526 LL_FOREACH(session->context->endpoint, ep) {
2527 if (session->proto == ep->proto)
2528 break;
2529 }
2530 if (!ep) {
2531 /* Need a dummy endpoint to 'hang' off as this is now a server */
2532 ep = coap_malloc_endpoint();
2533 if (!ep) {
2534 coap_log_warn("coap_new_endpoint: malloc");
2535 return 0;
2536 }
2537
2538 memset(ep, 0, sizeof(coap_endpoint_t));
2539 ep->context = session->context;
2540 ep->proto = session->proto;
2541 ep->sock.endpoint = ep;
2542 memcpy(&ep->sock.lfunc, coap_layers_coap[session->proto], sizeof(ep->sock.lfunc));
2543
2544 ep->default_mtu = COAP_DEFAULT_MTU;
2545 LL_PREPEND(session->context->endpoint, ep);
2546 }
2547 /* Need to use coap_send_lkd() here to get traffic flowing */
2548 if (session->proto == COAP_PROTO_UDP) {
2550 coap_new_message_id_lkd(session), 0);
2551 if (!ping) {
2552 session->type = COAP_SESSION_TYPE_SERVER;
2553 return 0;
2554 }
2555 mid = coap_send_lkd(session, ping);
2556 if (mid != COAP_INVALID_MID) {
2557 coap_tick_t now;
2558
2559 coap_ticks(&now);
2560 session->last_ping_mid = mid;
2561 session->last_rx_tx = now;
2562 session->last_ping = now;
2563 }
2564 }
2565 /*
2566 * (D)TLS will initiate traffic with handshake.
2567 * Reliable protocols will be sending a CSM, so no need for Ping.
2568 */
2569 if (session->context) {
2570 if (session->context->sessions) {
2571 SESSIONS_DELETE(session->context->sessions, session);
2572 }
2573 }
2574 SESSIONS_ADD(ep->sessions, session);
2575 session->endpoint = ep;
2576 session->type = COAP_SESSION_TYPE_SERVER;
2577 coap_session_release_lkd(session);
2578 coap_log_debug("***%s: session now is type Server\n",
2579 coap_session_str(session));
2580 return 1;
2581 }
2582#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2583 (void)session;
2584#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2585 return 0;
2586}
2587
2590 if (session)
2591 return session->state;
2592 return 0;
2593}
2594
2597#if COAP_SERVER_SUPPORT
2598 if (session)
2599 return session->endpoint;
2600#else /* ! COAP_SERVER_SUPPORT */
2601 (void)session;
2602#endif /* ! COAP_SERVER_SUPPORT */
2603 return 0;
2604}
2605
2606int
2608 if (session)
2609 return session->ifindex;
2610 return -1;
2611}
2612
2613void *
2615 coap_tls_library_t *tls_lib) {
2616 if (session)
2617 return coap_dtls_get_tls(session, tls_lib);
2618 return NULL;
2619}
2620
2621static const char *
2623 switch (proto) {
2624 case COAP_PROTO_UDP:
2625 return "UDP ";
2626 case COAP_PROTO_DTLS:
2627 return "DTLS";
2628 case COAP_PROTO_TCP:
2629 return "TCP ";
2630 case COAP_PROTO_TLS:
2631 return "TLS ";
2632 case COAP_PROTO_WS:
2633 return "WS ";
2634 case COAP_PROTO_WSS:
2635 return "WSS ";
2636 case COAP_PROTO_NONE:
2637 case COAP_PROTO_LAST:
2638 default:
2639 return "????" ;
2640 break;
2641 }
2642 return NULL;
2643}
2644
2645#if COAP_SERVER_SUPPORT
2647coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2648 coap_endpoint_t *endpoint;
2649
2650 coap_lock_lock(return NULL);
2651 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2653 return endpoint;
2654}
2655
2657coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2658 coap_proto_t proto) {
2659 coap_endpoint_t *ep = NULL;
2660
2661 if (!context || !listen_addr) {
2662 coap_log_debug("coap_new_endpoint: Both context and listen_addr need to be defined\n");
2663 return NULL;
2664 }
2665
2667
2668 switch (proto) {
2669 case COAP_PROTO_UDP:
2670 break;
2671 case COAP_PROTO_DTLS:
2672 if (!coap_dtls_is_supported()) {
2673 coap_log_warn("coap_new_endpoint: DTLS not supported\n");
2674 return NULL;
2675 }
2676 break;
2677 case COAP_PROTO_TLS:
2678 if (!coap_tls_is_supported()) {
2679 coap_log_warn("coap_new_endpoint: TLS not supported\n");
2680 return NULL;
2681 }
2682 break;
2683 case COAP_PROTO_TCP:
2684 if (!coap_tcp_is_supported()) {
2685 coap_log_warn("coap_new_endpoint: TCP not supported\n");
2686 return NULL;
2687 }
2688 break;
2689 case COAP_PROTO_WS:
2690 if (!coap_ws_is_supported()) {
2691 coap_log_warn("coap_new_endpoint: WS not supported\n");
2692 return NULL;
2693 }
2694 break;
2695 case COAP_PROTO_WSS:
2696 if (!coap_wss_is_supported()) {
2697 coap_log_warn("coap_new_endpoint: WSS not supported\n");
2698 return NULL;
2699 }
2700 break;
2701 case COAP_PROTO_NONE:
2702 case COAP_PROTO_LAST:
2703 default:
2704 coap_log_crit("coap_new_endpoint: Unsupported protocol %d\n", proto);
2705 return NULL;
2706 }
2707
2708 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2709 proto == COAP_PROTO_WSS) {
2711 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2712 "coap_context_set_pki() not called\n");
2713 return NULL;
2714 }
2715 }
2716 ep = coap_malloc_endpoint();
2717 if (!ep) {
2718 coap_log_warn("coap_new_endpoint: malloc");
2719 return NULL;
2720 }
2721
2722 memset(ep, 0, sizeof(coap_endpoint_t));
2723 ep->context = context;
2724 ep->proto = proto;
2725 ep->sock.endpoint = ep;
2726 if (proto < COAP_PROTO_LAST) {
2727 /*
2728 * Should be caught above, but come compilers realize that proto
2729 * includes COAP_PROTO_LAST, but there is no table entry for that
2730 */
2731 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2732 }
2733
2734 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2735 if (!coap_netif_dgrm_listen(ep, listen_addr))
2736 goto error;
2737#ifdef WITH_CONTIKI
2738 ep->sock.context = context;
2739#endif /* WITH_CONTIKI */
2740#if !COAP_DISABLE_TCP
2741 } else if (COAP_PROTO_RELIABLE(proto)) {
2742 if (!coap_netif_strm_listen(ep, listen_addr))
2743 goto error;
2744#endif /* !COAP_DISABLE_TCP */
2745 } else {
2746 coap_log_crit("coap_new_endpoint: Protocol type not supported %d\n", proto);
2747 goto error;
2748 }
2749
2751#ifndef INET6_ADDRSTRLEN
2752#define INET6_ADDRSTRLEN 40
2753#endif
2754 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2755
2756 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2757 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2758 addr_str);
2759 }
2760 }
2761
2762 ep->default_mtu = COAP_DEFAULT_MTU;
2763
2764#ifdef COAP_EPOLL_SUPPORT
2765 ep->sock.endpoint = ep;
2766 coap_epoll_ctl_add(&ep->sock,
2767 EPOLLIN,
2768 __func__);
2769#endif /* COAP_EPOLL_SUPPORT */
2770
2771 LL_PREPEND(context->endpoint, ep);
2772 return ep;
2773
2774error:
2775 coap_free_endpoint_lkd(ep);
2776 return NULL;
2777}
2778
2779void
2781 ep->default_mtu = (uint16_t)mtu;
2782}
2783
2784COAP_API void
2786 if (ep) {
2787 coap_context_t *context = ep->context;
2788 if (context) {
2789 coap_lock_lock(return);
2790 }
2791 coap_free_endpoint_lkd(ep);
2792 if (context) {
2794 }
2795 }
2796}
2797
2798void
2799coap_free_endpoint_lkd(coap_endpoint_t *ep) {
2800 if (ep) {
2801 coap_session_t *session, *rtmp;
2802 coap_addr_hash_t t_addr_hash;
2803
2804 if (ep->context) {
2805 /* If fully allocated and inserted */
2807 SESSIONS_ITER_EP_SAFE(ep->sessions, session, rtmp, t_addr_hash) {
2808 if (session->ref > 0) {
2810#if COAP_CLIENT_SUPPORT
2811 if (session->client_initiated) {
2812 coap_session_release_lkd(session);
2813 }
2814#endif /* COAP_CLIENT_SUPPORT */
2815 }
2816 assert(session->ref == 0);
2817 if (session->ref == 0) {
2819 coap_session_free(session);
2820 }
2821 }
2822 if (coap_netif_available_ep(ep)) {
2823 /*
2824 * ep->sock.endpoint is set in coap_new_endpoint().
2825 * ep->sock.session is never set.
2826 *
2827 * session->sock.session is set for both clients and servers (when a
2828 * new session is accepted), but does not affect the endpoint.
2829 *
2830 * So, it is safe to call coap_netif_close_ep() after all the sessions
2831 * have been freed above as we are only working with the endpoint sock.
2832 */
2833#ifdef COAP_EPOLL_SUPPORT
2834 assert(ep->sock.session == NULL);
2835#endif /* COAP_EPOLL_SUPPORT */
2837 }
2838
2839 if (ep->context->endpoint) {
2840 LL_DELETE(ep->context->endpoint, ep);
2841 }
2842 }
2843
2844 if (ep->app_cb) {
2845 coap_lock_callback(ep->app_cb(ep->app_data));
2846 }
2847
2848 coap_mfree_endpoint(ep);
2849 }
2850}
2851
2852void *
2854 assert(endpoint);
2855 return endpoint->app_data;
2856}
2857
2858COAP_API void *
2859coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *app_data,
2861 void *old_data;
2862
2863 coap_lock_lock(return NULL);
2864 old_data = coap_endpoint_set_app_data_lkd(endpoint, app_data, callback);
2866 return old_data;
2867}
2868
2869void *
2870coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *app_data,
2872 void *old_data = endpoint->app_data;
2873
2874 endpoint->app_data = app_data;
2875 endpoint->app_cb = app_data ? callback : NULL;
2876 return old_data;
2877}
2878
2879#endif /* COAP_SERVER_SUPPORT */
2880
2883 const coap_address_t *remote_addr,
2884 int ifindex) {
2885 coap_session_t *s, *rtmp;
2886#if COAP_CLIENT_SUPPORT
2887 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2888 if (s->ifindex == ifindex) {
2889 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2890 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2891 return s;
2892 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2893 return s;
2894 }
2895 }
2896#endif /* COAP_CLIENT_SUPPORT */
2897#if COAP_SERVER_SUPPORT
2898 coap_endpoint_t *ep;
2899
2900 LL_FOREACH(ctx->endpoint, ep) {
2901 SESSIONS_ITER(ep->sessions, s, rtmp) {
2902 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2903 remote_addr))
2904 return s;
2905 }
2906 }
2907#endif /* COAP_SERVER_SUPPORT */
2908 return NULL;
2909}
2910
2911#ifndef INET6_ADDRSTRLEN
2912#define INET6_ADDRSTRLEN 46
2913#endif
2914const char *
2916 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2917 char *p = szSession, *end = szSession + sizeof(szSession);
2918
2919 if (!session) {
2920 return "Session not defined";
2921 }
2922 if (coap_print_addr(&session->addr_info.local,
2923 (unsigned char *)p, end - p) > 0)
2924 p += strlen(p);
2925 if (p + 6 < end) {
2926 strcpy(p, " <-> ");
2927 p += 5;
2928 }
2929 if (p + 1 < end) {
2930 if (coap_print_addr(&session->addr_info.remote,
2931 (unsigned char *)p, end - p) > 0)
2932 p += strlen(p);
2933 }
2934 if (session->ifindex > 0 && p + 1 < end)
2935 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2936 if (p + 6 < end) {
2937 strcpy(p, " ");
2938 p++;
2939 strcpy(p, coap_proto_name(session->proto));
2940 }
2941
2942 return szSession;
2943}
2944
2945#if COAP_SERVER_SUPPORT
2946const char *
2947coap_endpoint_str(const coap_endpoint_t *endpoint) {
2948 static char szEndpoint[128];
2949 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2950 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2951 p += strlen(p);
2952 if (p + 6 < end) {
2953 if (endpoint->proto == COAP_PROTO_UDP) {
2954 strcpy(p, " UDP");
2955 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2956 strcpy(p, " DTLS");
2957 } else {
2958 strcpy(p, " NONE");
2959 }
2960 }
2961
2962 return szEndpoint;
2963}
2964#endif /* COAP_SERVER_SUPPORT */
2965#if COAP_CLIENT_SUPPORT
2966void
2968 session->no_observe_cancel = 1;
2969}
2970
2971void
2973 if (session->type != COAP_SESSION_TYPE_CLIENT) {
2974 coap_lock_lock(return);
2976 coap_session_release_lkd(session);
2978 }
2979}
2980#endif /* COAP_CLIENT_SUPPORT */
2981
2982#if COAP_SERVER_SUPPORT
2983/*
2984 * Get the next session in the linked list off an endpoint (server).
2985 */
2988 coap_session_t *session_curr,
2989 coap_session_t **session_next,
2990 coap_addr_hash_t *addr_hash) {
2991 coap_session_t *ch;
2992
2993 if (!session_head || !*session_next)
2994 return NULL;
2995
2996 /* Check session just used is still valid */
2997 SESSIONS_FIND_HP(session_head, addr_hash, ch);
2998 if (ch && ch == session_curr) {
2999 /* Use the next one */
3000 if ((ch = session_curr->hh.next))
3001 goto have_valid_next;
3002 return NULL;
3003 }
3004 coap_log_debug("Current session %p no longer exists\n", (void *)session_curr);
3005
3006 /* Check session provided for next is still valid */
3007 for (ch = session_head; ch; ch = ch->hh.next) {
3008 if (ch == *session_next)
3009 break;
3010 }
3011 if (ch) {
3012 /* Use this one */
3013 goto have_valid_next;
3014 }
3015 coap_log_debug("Next session %p no longer exists\n", (void *)*session_next);
3016
3017 /* Go back to the start of the list */
3018 ch = session_head;
3019 coap_log_debug("Restarting session list with %p\n", (void *)ch);
3020
3021have_valid_next:
3022 *session_next = ch->hh.next;
3023 if (ch) {
3024 memcpy(addr_hash, &ch->addr_hash, sizeof(*addr_hash));
3025 }
3026 return ch;
3027}
3028#endif /* COAP_SERVER_SUPPORT */
3029
3030#if COAP_CLIENT_SUPPORT
3031/*
3032 * Get the next session in the linked list off an context (client).
3033 */
3036 coap_session_t *session_curr,
3037 coap_session_t **session_next) {
3038 coap_session_t *ch;
3039
3040 if (!session_head)
3041 return NULL;
3042
3043 /* Check session just used is still valid */
3044 for (ch = session_head; ch; ch = ch->hh.next) {
3045 if (ch == session_curr)
3046 break;
3047 }
3048 if (ch) {
3049 /* Use the next one */
3050 if ((ch = session_curr->hh.next))
3051 goto have_valid_next;
3052 return NULL;
3053 }
3054 coap_log_debug("Current session %p no longer exists\n", (void *)session_curr);
3055
3056 /* Check session provided for next is still valid */
3057 for (ch = session_head; ch; ch = ch->hh.next) {
3058 if (ch == *session_next)
3059 break;
3060 }
3061 if (ch) {
3062 /* Use this one */
3063 goto have_valid_next;
3064 }
3065 coap_log_debug("Next session %p no longer exists\n", (void *)*session_next);
3066
3067 /* Go back to the start of the list */
3068 ch = session_head;
3069 coap_log_debug("Restarting session list with %p\n", (void *)ch);
3070
3071have_valid_next:
3072 *session_next = ch->hh.next;
3073 return ch;
3074}
3075#endif /* COAP_CLIENT_SUPPORT */
3076
3077#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 INET6_ADDRSTRLEN
Definition coap_debug.c:259
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_endpoint_t coap_endpoint_t
struct coap_cache_entry_t coap_cache_entry_t
struct coap_lg_srcv_t coap_lg_srcv_t
#define PRIuS
#define PRIu32
#define COAP_CLIENT_SUPPORT
#define COAP_RXBUFFER_SIZE
Definition coap_io.h:31
coap_nack_reason_t
Definition coap_io.h:64
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:66
@ COAP_NACK_WS_FAILED
Definition coap_io.h:73
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:65
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:68
@ COAP_NACK_TLS_LAYER_FAILED
Definition coap_io.h:71
@ COAP_NACK_ICMP_ISSUE
Definition coap_io.h:69
@ COAP_NACK_WS_LAYER_FAILED
Definition coap_io.h:72
@ COAP_NACK_RST
Definition coap_io.h:67
@ COAP_NACK_BAD_RESPONSE
Definition coap_io.h:70
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to add the state of events that epoll is to track for the appropriate file de...
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_SLAVE
socket is a slave socket - do not close
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#define COAP_SOCKET_CAN_READ
non blocking socket can now read without blocking
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:45
@ COAP_STRING
Definition coap_mem.h:33
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:395
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:441
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:539
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:434
#define NULL
Definition coap_option.h:30
uint16_t coap_option_num_t
Definition coap_option.h:37
@ COAP_SIG_OPT_BLOCK_WISE_TRANSFER
@ COAP_SIG_OPT_EXTENDED_TOKEN_LENGTH
@ COAP_SIG_OPT_MAX_MESSAGE_SIZE
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)
void coap_call_home_stop_reconnecting(coap_session_t *session)
Disable CoAP Server repeating Call-Home reconnect to CoAP Client.
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_EP_SAFE(e, el, rtmp, hhtmp)
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define SESSIONS_DELETE(e, obj)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define SESSIONS_ITER_CL_SAFE(e, el, rtmp)
#define SESSIONS_FIND_HP(e, k, res)
#define SESSIONS_ITER(e, el, rtmp)
#define SESSIONS_FIND(e, k, res)
coap_session_t * coap_get_next_session_ep(coap_session_t *session_head, coap_session_t *session_curr, coap_session_t **session_next, coap_addr_hash_t *addr_hash)
Determine what is the next session that is to be used for the next iteration.
coap_session_t * coap_get_next_session_cl(coap_session_t *session_head, coap_session_t *session_curr, coap_session_t **session_next)
Determine what is the next session that is to be used for the next iteration.
void coap_reset_doing_first(coap_session_t *session)
Reset doing the first packet state when testing for optional functionality.
coap_mid_t coap_send_lkd(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1649
#define coap_check_update_token(a, b)
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Remove a lg_xmit.
#define COAP_BLOCK_FORCE_Q_BLOCK
Definition coap_block.h:73
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:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
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:198
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:5563
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:206
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:1517
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:1397
void coap_add_to_head_delayq(coap_session_t *session, coap_queue_t *node)
This function adds the node to the head of the delayqueue.
Definition coap_net.c:3329
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:2170
coap_queue_t * coap_remove_first_from_delayq(coap_session_t *session)
This function removes the first node from the delayqueue.
Definition coap_net.c:3305
void coap_add_to_tail_delayq(coap_session_t *session, coap_queue_t *node)
This function adds the node to the tail of the delayqueue.
Definition coap_net.c:3318
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1423
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition coap_net.c:235
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:3446
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t mid, coap_bin_const_t *token, coap_queue_t **node)
This function removes the element with given mid from the list given list.
Definition coap_net.c:3337
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, coap_bin_const_t *token)
Cancels all outstanding messages for session session that have the specified token.
Definition coap_net.c:3485
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
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:53
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
Definition coap_dtls.c:260
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition coap_dtls.h:312
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition coap_dtls.h:413
coap_tls_library_t
Definition coap_dtls.h:74
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:49
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:71
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:81
@ COAP_EVENT_SESSION_CONNECTED
Triggered when TCP layer completes exchange of CSM information.
Definition coap_event.h:63
@ COAP_EVENT_RECONNECT_FAILED
Triggered when a session failed, and a reconnect is going to be attempted.
Definition coap_event.h:157
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition coap_event.h:57
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition coap_event.h:67
@ COAP_EVENT_SERVER_SESSION_NEW
Called in the CoAP IO loop if a new server-side session is created due to an incoming connection.
Definition coap_event.h:89
@ COAP_EVENT_RECONNECT_STARTED
Triggered when a session starts to reconnect.
Definition coap_event.h:163
@ COAP_EVENT_RECONNECT_NO_MORE
Triggered when a session failed, and retry reconnect attempts failed.
Definition coap_event.h:161
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition coap_event.h:65
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
Definition coap_event.h:98
@ COAP_EVENT_RECONNECT_SUCCESS
Triggered when a session failed, and a reconnect is successful.
Definition coap_event.h:159
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition coap_event.h:55
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition coap_event.h:53
@ COAP_EVENT_KEEPALIVE_FAILURE
Triggered when no response to a keep alive (ping) packet.
Definition coap_event.h:152
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
Definition coap_debug.h:126
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:103
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:845
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:266
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
Definition coap_debug.h:114
#define coap_log_warn(...)
Definition coap_debug.h:108
#define coap_log_err(...)
Definition coap_debug.h:102
#define coap_log_crit(...)
Definition coap_debug.h:96
@ COAP_LOG_DEBUG
Definition coap_debug.h:64
int coap_netif_dgrm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif datagram listem (udp).
void coap_netif_close_ep(coap_endpoint_t *endpoint)
Layer function interface for Netif close for a endpoint.
int coap_netif_strm_connect1(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif stream connect (tcp).
int coap_netif_strm_accept(coap_endpoint_t *endpoint, coap_session_t *session, void *extra)
Layer function interface for Netif stream accept.
int coap_netif_strm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif stream listem (tcp).
int coap_netif_dgrm_connect(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif datagram connect (udp).
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition coap_netif.c:25
int coap_netif_available_ep(coap_endpoint_t *endpoint)
Function interface to check whether netif for endpoint is still available.
void coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
void oscore_release_recipient_ctx(oscore_recipient_ctx_t **recipient_ctx)
Cleanup recipient context, including releasing the oscore context if the oscore context referenced is...
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
Definition coap_pdu.c:1768
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:192
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:469
#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_DEFAULT_MAX_PDU_RX_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP0
#define COAP_PDU_MAX_HEADER_SIZE
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:1630
#define COAP_PDU_WS_HEADER_SIZE
#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:858
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:39
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:195
#define COAP_TOKEN_DEFAULT_MAX
Definition coap_pdu.h:58
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:245
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:40
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:104
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:198
#define COAP_DEFAULT_MTU
Definition coap_pdu.h:43
#define COAP_BERT_BASE
Definition coap_pdu.h:46
@ COAP_PROTO_WS
Definition coap_pdu.h:251
@ COAP_PROTO_DTLS
Definition coap_pdu.h:248
@ COAP_PROTO_UDP
Definition coap_pdu.h:247
@ COAP_PROTO_NONE
Definition coap_pdu.h:246
@ COAP_PROTO_TLS
Definition coap_pdu.h:250
@ COAP_PROTO_WSS
Definition coap_pdu.h:252
@ COAP_PROTO_TCP
Definition coap_pdu.h:249
@ COAP_PROTO_LAST
Definition coap_pdu.h:253
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:298
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:299
@ COAP_EMPTY_CODE
Definition coap_pdu.h:260
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
coap_session_t * coap_new_client_session_pki3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials along with app_data infor...
#define COAP_NON_PROBING_WAIT_BASE(s)
void coap_session_reestablished(coap_session_t *session)
Session has been re-connected to server.
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
void coap_session_hold_lkd(coap_session_t *session)
Mark the session as in use for a unit of work (reading and dispatching received data,...
void coap_session_disconnected_do_it_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
coap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
int coap_session_set_type_server_lkd(coap_session_t *session)
Set the session type to server.
void coap_session_unhold_lkd(coap_session_t *session)
End a unit of work started by coap_session_hold_lkd().
#define COAP_ACK_RANDOM_FACTOR(s)
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.
coap_session_t * coap_new_client_session_psk3_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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_reconnect(coap_session_t *session)
Close the current session (if not already closed) and reconnect to server (client session only).
void coap_session_server_keepalive_failed(coap_session_t *session)
Clear down a session following a keepalive failure.
void * coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *data, coap_app_data_free_callback_t callback)
Stores data with the given endpoint, returning the previously stored value or NULL.
void coap_session_establish(coap_session_t *session)
Layer function interface for layer below session accept/connect being established.
coap_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_NSTART(s)
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
void coap_session_failed(coap_session_t *session)
Session has failed due to a socket error.
coap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition coap_net.c:1271
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
coap_mid_t coap_session_send_ping_lkd(coap_session_t *session)
Send a ping message for the session.
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
void coap_session_free(coap_session_t *session)
void coap_session_mfree(coap_session_t *session)
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
coap_session_t * coap_new_client_session3_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
int coap_session_set_type_client_lkd(coap_session_t *session, int report_changed)
Set the session type to client.
coap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session)
#define COAP_NON_MAX_RETRANSMIT(s)
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
#define COAP_NON_TIMEOUT(s)
void * coap_session_set_app_data2_lkd(coap_session_t *session, void *app_data, coap_app_data_free_callback_t callback)
Stores data with the given session, returning the previously stored value or NULL.
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
#define COAP_NON_PARTIAL_TIMEOUT(s)
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
The session is to be disconnected (a send on it has failed, the peer or the (D)TLS layer has closed i...
@ COAP_EXT_T_CHECKED
Token size valid.
COAP_API void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
COAP_API coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
coap_session_type_t
coap_session_type_t values
COAP_API void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
COAP_API void * coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *data, coap_app_data_free_callback_t callback)
Stores data with the given endpoint, returning the previously stored value or NULL.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
COAP_API coap_session_t * coap_new_client_session_pki3(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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PKI credentials along with app_data infor...
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.
int coap_session_is_oscore(const coap_session_t *session)
Check if the session is using OSCORE.
COAP_API int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent).
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
coap_session_state_t
coap_session_state_t values
void * coap_endpoint_get_app_data(const coap_endpoint_t *endpoint)
Returns any application-specific data that has been stored with endpoint using the function coap_endp...
coap_endpoint_t * coap_session_get_endpoint(const coap_session_t *session)
Get the endpoint of a session.
#define COAP_PROTO_NOT_RELIABLE(p)
COAP_API coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
void coap_session_init_token(coap_session_t *session, size_t len, const uint8_t *data)
Initializes the token value to use as a starting point.
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
COAP_API coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
Get the session associated with the specified remote_addr and index.
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
COAP_API coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
coap_session_t * coap_new_client_session_psk3(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, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, with PSK credentials along with app_data infor...
COAP_API void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
COAP_API coap_session_t * coap_new_client_session3(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, void *app_data, coap_app_data_free_callback_t callback, coap_str_const_t *ws_host)
Creates a new client session to the designated server, along with app_data information (as per coap_s...
COAP_API void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
COAP_API coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
const coap_address_t * coap_session_get_addr_mcast(const coap_session_t *session)
Get the remote multicast IP address and port from the session if the original target IP was multicast...
COAP_API size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
COAP_API int coap_session_set_type_server(coap_session_t *session)
Set the session type to server.
COAP_API coap_session_t * coap_new_client_session_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
void(* coap_app_data_free_callback_t)(void *data)
Callback to free off the app data when the entry is being deleted / freed off.
COAP_API void * coap_session_set_app_data2(coap_session_t *session, void *app_data, coap_app_data_free_callback_t callback)
Stores data with the given session, returning the previously stored value or NULL.
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
@ COAP_SESSION_TYPE_SERVER
server-side
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_CONNECTING
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:130
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:65
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:119
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:222
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition coap_str.c:55
int coap_tcp_is_supported(void)
Check whether TCP is available.
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:977
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:982
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:58
coap_address_t local
local address and port
Definition coap_io.h:59
Multi-purpose address abstraction.
union coap_address_t::@356070177072120110060332164236135107255353032357 addr
struct sockaddr_in sin
struct sockaddr_in6 sin6
CoAP binary data definition with const data.
Definition coap_str.h:65
size_t length
length of binary data
Definition coap_str.h:66
const uint8_t * s
read-only binary data
Definition coap_str.h:67
The CoAP stack's global state is stored in a coap_context_t object.
uint64_t rl_ticks_per_packet
If not 0, rate limit NON to ticks per packet.
coap_nack_handler_t nack_cb
Called when a response issue has occurred.
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
coap_queue_t * sendqueue
uint32_t max_token_size
Largest token size supported RFC8974.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_bin_const_t key
Definition coap_dtls.h:389
coap_bin_const_t identity
Definition coap_dtls.h:388
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:418
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:445
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition coap_dtls.h:451
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:317
uint8_t version
Definition coap_dtls.h:318
char * client_sni
If not NULL, SNI to use in client TLS setup.
Definition coap_dtls.h:376
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) transmission information.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
structure for CoAP PDUs
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
coap_bin_const_t actual_token
Actual token in pdu.
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
coap_pdu_type_t type
message type
Queue entry.
coap_address_t remote
For re-transmission - where the node is going.
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
volatile uint8_t max_token_checked
Check for max token size coap_ext_token_check_t.
coap_nack_reason_t disconnect_reason
reason for the pending disconnect
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_app_data_free_callback_t app_cb
call-back to release app_data
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
coap_mid_t last_resp_mid
The last response mid that has been been processed.
uint64_t tx_token
Next token number to use.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local).
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
uint8_t disconnect_pending
Set if a disconnect is deferred until the session is no longer in use.
uint8_t is_dtls13
Set if session is DTLS1.3.
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv).
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
unsigned hold_in_use
depth of work in progress on the session (coap_session_hold_lkd())
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app_data
application-specific data
uint32_t tx_rtag
Next Request-Tag number to use.
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
uint8_t disconnecting
Set while the session is being disconnected.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
uint64_t rl_ticks_per_packet
If not 0, rate limit NON to ticks per packet.
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs).
coap_session_type_t type
client or server side socket
uint32_t probing_rate
Max transfer wait when remote is not responding (default 1 byte/sec).
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_bin_const_t * echo
last token used to make a request
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_session_t * session
Used to determine session owner.
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition with const data.
Definition coap_str.h:47
const uint8_t * s
read-only string data
Definition coap_str.h:49
size_t length
length of string
Definition coap_str.h:48