libcoap 4.3.5-develop-b9cf572
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
404COAP_API void
405coap_session_set_app_data(coap_session_t *session, void *app_data) {
406 assert(session);
407 coap_lock_lock(return);
408 coap_session_set_app_data2_lkd(session, app_data, NULL);
410}
411
412void *
414 assert(session);
415 return session->app_data;
416}
417
418COAP_API void *
421 void *old_data;
422
423 coap_lock_lock(return NULL);
424 old_data = coap_session_set_app_data2_lkd(session, app_data, callback);
426 return old_data;
427}
428
429void *
432 void *old_data = session->app_data;
433
434 session->app_data = app_data;
435 session->app_cb = app_data ? callback : NULL;
436 return old_data;
437}
438
439static coap_session_t *
441 const coap_addr_hash_t *addr_hash,
442 const coap_address_t *local_addr,
443 const coap_address_t *remote_addr, int ifindex,
444 coap_context_t *context, coap_endpoint_t *endpoint) {
446 sizeof(coap_session_t));
447#if ! COAP_SERVER_SUPPORT
448 (void)endpoint;
449#endif /* ! COAP_SERVER_SUPPORT */
450 if (!session)
451 return NULL;
452 memset(session, 0, sizeof(*session));
453 session->proto = proto;
454 session->type = type;
455 if (addr_hash)
456 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
457 else
458 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
459 if (local_addr) {
460 coap_address_copy(&session->addr_info.local, local_addr);
461#if COAP_CLIENT_SUPPORT
462 coap_address_copy(&session->local_reconnect, local_addr);
463#endif /* COAP_CLIENT_SUPPORT */
464 } else {
466#if COAP_CLIENT_SUPPORT
467 coap_address_init(&session->local_reconnect);
468#endif /* COAP_CLIENT_SUPPORT */
469 }
470 if (remote_addr)
471 coap_address_copy(&session->addr_info.remote, remote_addr);
472 else
474 session->ifindex = ifindex;
475 session->context = context;
476#if COAP_CLIENT_SUPPORT
477 if (type == COAP_SESSION_TYPE_CLIENT) {
478 session->client_initiated = 1;
479 }
480#endif /* COAP_CLIENT_SUPPORT */
481#if COAP_SERVER_SUPPORT
482 session->endpoint = endpoint;
483 if (endpoint)
484 session->mtu = endpoint->default_mtu;
485 else
486#endif /* COAP_SERVER_SUPPORT */
488 session->block_mode = context->block_mode;
489#if COAP_Q_BLOCK_SUPPORT
490 if (session->block_mode & COAP_BLOCK_FORCE_Q_BLOCK) {
491 set_block_mode_has_q(session->block_mode);
492 }
493#endif
494 if (proto == COAP_PROTO_DTLS) {
495 session->tls_overhead = 29;
496 if (session->tls_overhead >= session->mtu) {
497 session->tls_overhead = session->mtu;
498 coap_log_err("DTLS overhead exceeds MTU\n");
499 }
500 }
501 session->rl_ticks_per_packet = context->rl_ticks_per_packet;
505 session->nstart = COAP_DEFAULT_NSTART;
508#if COAP_Q_BLOCK_SUPPORT
509 session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
510 session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
511 session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
512 session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
513 coap_session_fix_non_probing_wait_base(session);
514 coap_session_fix_non_partial_timeout(session);
515#endif /* COAP_Q_BLOCK_SUPPORT */
516 session->dtls_event = -1;
520 session->max_token_size = context->max_token_size; /* RFC8974 */
521 if (session->type != COAP_SESSION_TYPE_CLIENT)
523
524 /* Randomly initialize */
525 /* TCP/TLS have no notion of mid */
526 if (COAP_PROTO_NOT_RELIABLE(session->proto))
527 coap_prng_lkd((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
528 coap_prng_lkd((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
529
530 return session;
531}
532
533void
535 coap_queue_t *q;
536 coap_lg_xmit_t *lq, *ltmp;
537
538#if COAP_PROXY_SUPPORT
539 if (session->ref_proxy_subs)
540 coap_delete_proxy_subscriber(session, NULL, 0, COAP_PROXY_SUBS_ALL);
541#endif /* COAP_PROXY_SUPPORT */
542#if COAP_CLIENT_SUPPORT
543 coap_lg_crcv_t *lg_crcv, *etmp;
544
545 /* Need to do this before (D)TLS and socket is closed down */
546 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
547 if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
548 /* Need to close down observe */
549 if (coap_cancel_observe_lkd(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
550 /* Need to delete node we set up for NON */
551 coap_queue_t *queue = session->context->sendqueue;
552
553 while (queue) {
554 if (queue->session == session) {
556 break;
557 }
558 queue = queue->next;
559 }
560 }
561 }
562 /* In case coap_cancel_observe_lkd() failure, which could clear down lg_crcv */
563 if (!session->lg_crcv)
564 break;
565 LL_DELETE(session->lg_crcv, lg_crcv);
566 coap_block_delete_lg_crcv(session, lg_crcv);
567 }
568#endif /* COAP_CLIENT_SUPPORT */
569
570 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
571 if (q->pdu->type==COAP_MESSAGE_CON) {
572 coap_handle_nack(session, q->pdu,
573 session->proto == COAP_PROTO_DTLS ?
575 q->id);
576 }
578 }
579
580#if COAP_CLIENT_SUPPORT
581 coap_pdu_t *p, *ptmp;
582
583 LL_FOREACH_SAFE(session->doing_first_pdu, p, ptmp) {
584 if (p->type==COAP_MESSAGE_CON) {
585 coap_handle_nack(session, p,
586 session->proto == COAP_PROTO_DTLS ?
588 p->mid);
589 }
591 }
592#endif /* COAP_CLIENT_SUPPORT */
593
594 if (session->partial_pdu)
596 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
597 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
598 if (session->psk_identity)
600 if (session->psk_key)
602 if (session->psk_hint)
604
605#ifdef HAVE_LINUX_ERRQUEUE_H
606 coap_delete_str_const(session->last_data_write);
607#endif /* HAVE_LINUX_ERRQUEUE_H */
608
609#if COAP_SERVER_SUPPORT
610 coap_cache_entry_t *cp, *ctmp;
611 HASH_ITER(hh, session->context->cache, cp, ctmp) {
612 /* cp->session is NULL if not session based */
613 if (cp->session == session) {
614 coap_delete_cache_entry(session->context, cp);
615 }
616 }
617#endif /* COAP_PROXY_SUPPORT */
618 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
619 LL_DELETE(session->lg_xmit, lq);
620 coap_block_delete_lg_xmit(session, lq);
621 }
622#if COAP_SERVER_SUPPORT
623 coap_lg_srcv_t *sq, *stmp;
624
625 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
626 LL_DELETE(session->lg_srcv, sq);
627 coap_block_delete_lg_srcv(session, sq);
628 }
629#endif /* COAP_SERVER_SUPPORT */
630#if COAP_OSCORE_SUPPORT
632 oscore_release_recipient_ctx(&session->recipient_ctx);
633 coap_delete_str_const(session->b_2_retransmit_token);
634#endif /* COAP_OSCORE_SUPPORT */
635#if COAP_WS_SUPPORT
636 coap_free_type(COAP_STRING, session->ws);
637 coap_delete_str_const(session->ws_host);
638#endif /* COAP_WS_SUPPORT */
639}
640
641void
643 if (!session)
644 return;
646 assert(session->ref == 0);
647 if (session->ref)
648 return;
649 /* Make sure nothing gets deleted under our feet */
651 coap_session_mfree(session);
652#if COAP_SERVER_SUPPORT
653 coap_delete_bin_const(session->client_cid);
654 if (session->endpoint) {
655 if (session->endpoint->sessions)
656 SESSIONS_DELETE(session->endpoint->sessions, session);
657 } else
658#endif /* COAP_SERVER_SUPPORT */
659#if COAP_CLIENT_SUPPORT
660 if (session->context) {
661 if (session->context->sessions)
662 SESSIONS_DELETE(session->context->sessions, session);
663 }
664 coap_delete_bin_const(session->req_token);
665#endif /* COAP_CLIENT_SUPPORT */
667 coap_delete_bin_const(session->echo);
668#if COAP_SERVER_SUPPORT
669 coap_delete_pdu_lkd(session->last_resp_pdu);
670#endif /* COAP_SERVER_SUPPORT */
671
672 if (session->app_cb) {
673 coap_lock_callback(session->app_cb(session->app_data));
674 }
675 coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
676 (void *)session);
677 assert(session->ref == 1);
679}
680
681#if COAP_SERVER_SUPPORT
682void
684 int i;
685 coap_queue_t *q;
686
689 coap_cancel_all_messages(session->context, session, NULL);
690 RESOURCES_ITER(session->context->resources, r) {
691 /* In case code is broken somewhere */
692 for (i = 0; i < 1000; i++) {
693 if (!coap_delete_observer(r, session, NULL))
694 break;
695 }
696 }
697 if (session->context->unknown_resource) {
698 /* In case code is broken somewhere */
699 for (i = 0; i < 1000; i++) {
700 if (!coap_delete_observer(session->context->unknown_resource, session, NULL))
701 break;
702 }
703 }
704 if (session->context->proxy_uri_resource) {
705 /* In case code is broken somewhere */
706 for (i = 0; i < 1000; i++) {
707 if (!coap_delete_observer(session->context->proxy_uri_resource, session, NULL))
708 break;
709 }
710 }
711 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
713 }
714 /* Force session to go away */
717
719}
720#endif /* COAP_SERVER_SUPPORT */
721
722static size_t
724 size_t max_with_header) {
725#if COAP_DISABLE_TCP
726 (void)session;
727 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
728 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
729 : 0;
730#else /* !COAP_DISABLE_TCP */
731 if (COAP_PROTO_NOT_RELIABLE(session->proto))
732 return max_with_header > COAP_PDU_MAX_UDP_HEADER_SIZE
733 ? max_with_header - COAP_PDU_MAX_UDP_HEADER_SIZE
734 : 0;
735 /* we must assume there is no token to be on the safe side */
736 if (max_with_header <= 2)
737 return 0;
738 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
739 return max_with_header - 2;
740 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
741 return max_with_header - 3;
742 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
743 return max_with_header - 4;
744 else
745 return max_with_header - COAP_PDU_MAX_TCP_HEADER_SIZE;
746#endif /* !COAP_DISABLE_TCP */
747}
748
749size_t
751 if (session->csm_rcv_mtu)
753 (size_t)(session->csm_rcv_mtu));
754
756 (size_t)(session->mtu - session->tls_overhead));
757}
758
759COAP_API size_t
761 size_t size;
762 coap_session_t *session_rw;
763
764 /*
765 * Need to do this to not get a compiler warning about const parameters
766 * but need to maintain source code backward compatibility
767 */
768 memcpy(&session_rw, &session, sizeof(session_rw));
769 coap_lock_lock(return 0);
770 size = coap_session_max_pdu_size_lkd(session_rw);
772 return size;
773}
774
775size_t
777 size_t max_with_header;
778
780#if COAP_CLIENT_SUPPORT
781 if (COAP_PROTO_RELIABLE(session->proto) &&
782 session->type == COAP_SESSION_TYPE_CLIENT &&
783 session->doing_first) {
784 /*
785 * Delay if session->doing_first is set.
786 * E.g. Reliable and CSM not in yet for checking block support
787 */
788 coap_session_t *session_rw;
789
790 /*
791 * Need to do this to not get a compiler warning about const parameters
792 * but need to maintain source code backward compatibility
793 */
794 memcpy(&session_rw, &session, sizeof(session_rw));
795 if (coap_client_delay_first(session_rw) == 0) {
796 coap_log_debug("coap_client_delay_first: timeout\n");
797 /* Have to go with the defaults */
798 }
799 }
800#endif /* COAP_CLIENT_SUPPORT */
801
802 max_with_header = (size_t)(session->mtu - session->tls_overhead);
803
804 return coap_session_max_pdu_size_internal(session, max_with_header);
805}
806
807void
808coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
811 coap_log_debug("* %s: Restricting MTU size to %u\n",
812 coap_session_str(session), mtu);
813 }
814 if (mtu < 64)
815 mtu = 64;
816 session->mtu = mtu;
817 if (session->tls_overhead >= session->mtu) {
818 session->tls_overhead = session->mtu;
819 coap_log_err("DTLS overhead exceeds MTU\n");
820 }
821}
822
823ssize_t
825 coap_queue_t *node) {
826 if (node) {
827 coap_queue_t *removed = NULL;
828 /* Nodes are per ID, no notion of token */
829 coap_remove_from_queue(&session->context->sendqueue, session, node->id,
830 NULL, &removed);
831 assert(removed == node);
832 /* reuse node */
834 node->session = NULL;
835 node->t = 0;
836 } else {
837 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
838 coap_queue_t *q = NULL;
839 /* Check same mid is not getting reused in violation of RFC7252 */
840 LL_FOREACH(session->delayqueue, q) {
841 if (q->id == pdu->mid) {
842 coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
843 coap_session_str(session), pdu->mid);
844 return COAP_INVALID_MID;
845 }
846 }
847 }
848 node = coap_new_node();
849 if (node == NULL)
850 return COAP_INVALID_MID;
851 node->id = pdu->mid;
852 node->pdu = pdu;
853 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
854 uint8_t r;
855 coap_prng_lkd(&r, sizeof(r));
856 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
857 node->timeout = coap_calc_timeout(session, r);
858 }
859 coap_address_copy(&node->remote, &session->addr_info.remote);
860 }
861 coap_add_to_tail_delayq(session, node);
863 coap_log_debug("** %s: mid=0x%04x: delayed\n",
864 coap_session_str(session), node->id);
865 return COAP_PDU_DELAYED;
866}
867
868#if !COAP_DISABLE_TCP
869void
871 coap_pdu_t *pdu;
872 uint8_t buf[4];
873 assert(COAP_PROTO_RELIABLE(session->proto));
874 coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
875 session->state = COAP_SESSION_STATE_CSM;
876 session->partial_write = 0;
877 if (session->mtu == 0)
878 coap_session_set_mtu(session, COAP_DEFAULT_MTU); /* base value */
880 if (pdu == NULL
882 coap_encode_var_safe(buf, sizeof(buf),
883 session->context->csm_max_message_size), buf) == 0
885 coap_encode_var_safe(buf, sizeof(buf),
886 0), buf) == 0
887 || (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
890 coap_encode_var_safe(buf, sizeof(buf),
891 session->max_token_size),
892 buf) == 0)
893 || coap_pdu_encode_header(pdu, session->proto) == 0
894 ) {
896 } else {
897 ssize_t bytes_written;
898
899 pdu->session = session;
900 bytes_written = coap_session_send_pdu(session, pdu);
901 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
903 } else {
904 session->csm_rcv_mtu = session->context->csm_max_message_size;
905 if (session->csm_rcv_mtu > COAP_BERT_BASE)
906 session->csm_bert_loc_support = 1;
907 else
908 session->csm_bert_loc_support = 0;
909 }
910 }
911 if (pdu)
913}
914#endif /* !COAP_DISABLE_TCP */
915
918 coap_mid_t mid;
919
921 mid = coap_session_send_ping_lkd(session);
922 if (mid != COAP_INVALID_MID) {
923 coap_tick_t now;
924
925 coap_ticks(&now);
926 session->last_ping_mid = mid;
927 session->last_rx_tx = now;
928 session->last_ping = now;
929 }
931 return mid;
932}
933
936 coap_pdu_t *ping = NULL;
937
939 if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
940 session->con_active)
941 return COAP_INVALID_MID;
942 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
943 uint16_t mid = coap_new_message_id_lkd(session);
944 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
945 }
946#if !COAP_DISABLE_TCP
947 else {
949 }
950#endif /* !COAP_DISABLE_TCP */
951 if (!ping)
952 return COAP_INVALID_MID;
953 return coap_send_internal(session, ping, NULL);
954}
955
956void
958 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
959 coap_log_debug("***%s: session connected\n",
960 coap_session_str(session));
961 if (session->state == COAP_SESSION_STATE_CSM) {
963#if COAP_CLIENT_SUPPORT
965 coap_reset_doing_first(session);
966#endif /* COAP_CLIENT_SUPPORT */
967 }
968 }
969
971 session->partial_write = 0;
972
973 if (session->proto==COAP_PROTO_DTLS) {
974 session->tls_overhead = coap_dtls_get_overhead(session);
975 if (session->tls_overhead >= session->mtu) {
976 session->tls_overhead = session->mtu;
977 coap_log_err("DTLS overhead exceeds MTU\n");
978 }
979 }
980
981 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
982 ssize_t bytes_written;
983 coap_queue_t *q = session->delayqueue;
984 coap_address_t remote;
985
986 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
987 if (session->con_active >= COAP_NSTART(session))
988 break;
989 session->con_active++;
990 }
991 /* Take entry off the queue */
993
994 coap_address_copy(&remote, &session->addr_info.remote);
996 coap_log_debug("** %s: mid=0x%04x: transmitted after delay (2)\n",
997 coap_session_str(session), (int)q->id);
998 bytes_written = coap_session_send_pdu(session, q->pdu);
999 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
1000 if (coap_wait_ack(session->context, session, q) >= 0)
1001 q = NULL;
1002 }
1003 coap_address_copy(&session->addr_info.remote, &remote);
1004 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1005 if (q)
1007 if (bytes_written < 0)
1008 break;
1009 } else if (q) {
1010 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
1011 coap_add_to_head_delayq(session, q);
1012 if (bytes_written > 0)
1013 session->partial_write = (size_t)bytes_written;
1014 break;
1015 } else {
1017 }
1018 }
1019 }
1020}
1021
1022#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
1023static const char *
1025 switch (reason) {
1027 return "COAP_NACK_TOO_MANY_RETRIES";
1029 return "COAP_NACK_NOT_DELIVERABLE";
1030 case COAP_NACK_RST:
1031 return "COAP_NACK_RST";
1033 return "COAP_NACK_TLS_FAILED";
1035 return "COAP_NACK_ICMP_ISSUE";
1037 return "COAP_NACK_BAD_RESPONSE";
1039 return "COAP_NACK_TLS_LAYER_FAILED";
1041 return "COAP_NACK_WS_LAYER_FAILED";
1043 return "COAP_NACK_WS_FAILED";
1044 default:
1045 return "???";
1046 }
1047}
1048#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
1049
1050void
1052 coap_pdu_t *sent,
1053 const coap_nack_reason_t reason,
1054 const coap_mid_t mid) {
1055 if (session->context->nack_cb
1057 && !session->doing_first_pdu
1058#endif /* COAP_CLIENT_SUPPORT */
1059 ) {
1060 coap_bin_const_t token = {0, NULL};
1061
1062 if (sent) {
1063 coap_check_update_token(session, sent);
1064 token = sent->actual_token;
1065 }
1066 coap_lock_callback(session->context->nack_cb(session, sent, reason, mid));
1067 if (sent) {
1068 coap_update_token(sent, token.length, token.s);
1069 }
1070 }
1071#if COAP_CLIENT_SUPPORT
1072 if (reason != COAP_NACK_ICMP_ISSUE) {
1073 session->doing_send_recv = 0;
1074 }
1075#endif /* COAP_CLIENT_SUPPORT */
1076}
1077
1078COAP_API void
1084
1085void
1087#if !COAP_DISABLE_TCP
1088 coap_session_state_t state = session->state;
1089#endif /* !COAP_DISABLE_TCP */
1090 coap_lg_xmit_t *lq, *ltmp;
1091#if COAP_SERVER_SUPPORT
1092 coap_lg_srcv_t *sq, *stmp;
1093#endif /* COAP_SERVER_SUPPORT */
1094#if COAP_CLIENT_SUPPORT
1095 coap_lg_crcv_t *cq, *etmp;
1096#endif /* COAP_CLIENT_SUPPORT */
1097 int sent_nack = 0;
1098 coap_queue_t *q;
1099
1101#if COAP_CLIENT_SUPPORT
1102 coap_session_failed(session);
1103#endif /* COAP_CLIENT_SUPPORT */
1104
1105 q = session->context->sendqueue;
1106 while (q) {
1107 if (q->session == session) {
1108 /* Take the first one */
1109 coap_handle_nack(session, q->pdu, reason, q->id);
1110 sent_nack = 1;
1111 break;
1112 }
1113 q = q->next;
1114 }
1115
1116 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
1117 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1118 coap_session_str(session), q->id);
1119 if (q->pdu->type == COAP_MESSAGE_CON) {
1120 coap_handle_nack(session, q->pdu, reason, q->id);
1121 sent_nack = 1;
1122 }
1123
1125 }
1126#if COAP_CLIENT_SUPPORT
1127 session->doing_send_recv = 0;
1128#endif /* COAP_CLIENT_SUPPORT */
1129#if COAP_CLIENT_SUPPORT
1130 if (!sent_nack && session->lg_crcv) {
1131 /* Take the first one */
1132 coap_handle_nack(session, session->lg_crcv->sent_pdu, reason,
1133 session->lg_crcv->sent_pdu->mid);
1134 sent_nack = 1;
1135 }
1136#endif /* COAP_CLIENT_SUPPORT */
1137 if (!sent_nack) {
1138 /* Unable to determine which request disconnection was for */
1139 coap_handle_nack(session, NULL, reason, 0);
1140 }
1141 coap_log_debug("***%s: session disconnected (%s)\n",
1142 coap_session_str(session), coap_nack_name(reason));
1143#if COAP_SERVER_SUPPORT
1144 coap_delete_observers(session->context, session);
1145#endif /* COAP_SERVER_SUPPORT */
1146
1147 if (session->proto == COAP_PROTO_UDP)
1149 else
1150 session->state = COAP_SESSION_STATE_NONE;
1151
1152 session->con_active = 0;
1153
1154 if (session->partial_pdu) {
1156 session->partial_pdu = NULL;
1157 }
1158 session->partial_read = 0;
1159
1160 /* Not done if nack handler called above */
1161 while ((q = coap_remove_first_from_delayq(session)) != NULL) {
1162 coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
1163 coap_session_str(session), q->id);
1164#if COAP_CLIENT_SUPPORT
1165 session->doing_send_recv = 0;
1166#endif /* COAP_CLIENT_SUPPORT */
1168 }
1169
1170#if COAP_CLIENT_SUPPORT
1171 if (!session->session_failed) {
1172 /* Need to do this before (D)TLS and socket is closed down */
1173 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
1174 LL_DELETE(session->lg_crcv, cq);
1175 coap_block_delete_lg_crcv(session, cq);
1176 }
1177 }
1178#endif /* COAP_CLIENT_SUPPORT */
1179 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
1180 LL_DELETE(session->lg_xmit, lq);
1181 coap_block_delete_lg_xmit(session, lq);
1182 }
1183#if COAP_SERVER_SUPPORT
1184 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
1185 LL_DELETE(session->lg_srcv, sq);
1186 coap_block_delete_lg_srcv(session, sq);
1187 }
1188#endif /* COAP_SERVER_SUPPORT */
1189 coap_cancel_session_messages(session->context, session, reason);
1190
1191#if !COAP_DISABLE_TCP
1192 if (COAP_PROTO_RELIABLE(session->proto)) {
1193 if (coap_netif_available(session)) {
1197 }
1198#if COAP_CLIENT_SUPPORT
1199 if (state != COAP_SESSION_STATE_NONE && !session->session_failed) {
1203 }
1204 coap_reset_doing_first(session);
1205#endif /* COAP_CLIENT_SUPPORT */
1206 }
1207#endif /* !COAP_DISABLE_TCP */
1208 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1209 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1210}
1211
1212#if COAP_CLIENT_SUPPORT
1213void
1215 if (session->context->reconnect_time && session->client_initiated) {
1216 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close)
1217 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1218 session->session_failed = 1;
1220 session->retry_count++;
1221 if (session->context->retry_count && session->retry_count >= session->context->retry_count) {
1223 if (session->type != COAP_SESSION_TYPE_CLIENT) {
1225 coap_session_release_lkd(session);
1226 }
1227 }
1228 coap_ticks(&session->last_rx_tx);
1229 }
1230}
1231#endif /* COAP_CLIENT_SUPPORT */
1232
1233#if COAP_SERVER_SUPPORT
1234static void
1235coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
1236 const coap_addr_tuple_t *addr_info) {
1237 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
1238 coap_address_copy(&addr_hash->remote, &addr_info->remote);
1239 addr_hash->lport = coap_address_get_port(&addr_info->local);
1240 addr_hash->proto = proto;
1241}
1242
1243/* Aids debugging when session is created */
1244static coap_session_t *debug_session;
1245
1247coap_endpoint_get_session(coap_endpoint_t *endpoint,
1248 const coap_packet_t *packet, coap_tick_t now) {
1249 coap_session_t *session;
1250 coap_session_t *rtmp;
1251 unsigned int num_idle = 0;
1252 unsigned int num_hs = 0;
1253 coap_session_t *oldest = NULL;
1254 coap_session_t *oldest_hs = NULL;
1255 coap_addr_hash_t addr_hash;
1256
1257 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
1258 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
1259 if (session) {
1260 /* Maybe mcast or unicast IP address which is not in the hash */
1261 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1262 session->ifindex = packet->ifindex;
1263 session->last_rx_tx = now;
1264 return session;
1265 }
1266
1267#if COAP_CLIENT_SUPPORT
1268 SESSIONS_FIND(endpoint->context->sessions, addr_hash, session);
1269 if (session) {
1270 /* Maybe mcast or unicast IP address which is not in the hash */
1271 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
1272 session->ifindex = packet->ifindex;
1273 session->last_rx_tx = now;
1274 return session;
1275 }
1276
1277 if (coap_is_mcast(&packet->addr_info.local)) {
1278 /* Check if this a proxy client packet we sent on another socket */
1279 SESSIONS_ITER(endpoint->context->sessions, session, rtmp) {
1280 if (coap_address_equals(&session->addr_info.remote, &packet->addr_info.local) &&
1283 /* Drop looped back packet to stop recursion / confusion */
1284 return NULL;
1285 }
1286 }
1287 }
1288#endif /* COAP_CLIENT_SUPPORT */
1289 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1290 if (session->ref == 0 && session->delayqueue == NULL &&
1291 session->lg_srcv == NULL && session->lg_xmit == NULL) {
1292 if (
1294 !(session->client_initiated && endpoint->context->reconnect_time) &&
1295#endif /* COAP_CLIENT_SUPPORT */
1296 session->type == COAP_SESSION_TYPE_SERVER) {
1297 ++num_idle;
1298 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
1299 oldest = session;
1300
1301 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
1302 ++num_hs;
1303 /* See if this is a partial (D)TLS session set up
1304 which needs to be cleared down to prevent DOS */
1305 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1306 if (oldest_hs == NULL ||
1307 session->last_rx_tx < oldest_hs->last_rx_tx)
1308 oldest_hs = session;
1309 }
1310 }
1311 } else if (session->type == COAP_SESSION_TYPE_HELLO) {
1312 ++num_hs;
1313 /* See if this is a partial (D)TLS session set up for Client Hello
1314 which needs to be cleared down to prevent DOS */
1315 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
1316 if (oldest_hs == NULL ||
1317 session->last_rx_tx < oldest_hs->last_rx_tx)
1318 oldest_hs = session;
1319 }
1320 }
1321 }
1322 }
1323
1324 if (endpoint->context->max_idle_sessions > 0 &&
1325 num_idle >= endpoint->context->max_idle_sessions) {
1327 coap_session_free(oldest);
1328 } else if (oldest_hs) {
1329 coap_log_warn("***%s: Incomplete session timed out\n",
1330 coap_session_str(oldest_hs));
1332 coap_session_free(oldest_hs);
1333 }
1334
1335 if (num_hs > (endpoint->context->max_handshake_sessions ?
1336 endpoint->context->max_handshake_sessions :
1338 /* Maxed out on number of sessions in (D)TLS negotiation state */
1339 coap_log_debug("Outstanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1340 "large. New request ignored\n");
1341 return NULL;
1342 }
1343
1344 if (endpoint->proto == COAP_PROTO_DTLS) {
1345 /*
1346 * Need to check that this actually is a Client Hello before wasting
1347 * time allocating and then freeing off session.
1348 */
1349
1350 /*
1351 * Generic header structure of the DTLS record layer.
1352 * typedef struct __attribute__((__packed__)) {
1353 * uint8_t content_type; content type of the included message
1354 * uint16_t version; Protocol version
1355 * uint16_t epoch; counter for cipher state changes
1356 * uint8_t sequence_number[6]; sequence number
1357 * uint16_t length; length of the following fragment
1358 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
1359 * } dtls_record_handshake_t;
1360 */
1361#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
1362#define DTLS_CT_ALERT 21 /* Content Type Alert */
1363#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
1364#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
1365#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
1366#define DTLS_CT_CID 25 /* Content Type Connection ID */
1367#define OFF_CID 11 /* offset of CID in dtls_record_handshake_t */
1368#define OFF_CID_DTLS13 1 /* offset of CID in DTLS1.3 Unified Header */
1369
1370 const uint8_t *payload = (const uint8_t *)packet->payload;
1371 size_t length = packet->length;
1372 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1373 coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%" PRIuS " < %d) dropped\n",
1374 payload[OFF_CONTENT_TYPE], length,
1375 OFF_HANDSHAKE_TYPE + 1);
1376 return NULL;
1377 }
1378 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1379 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1380 /* Client may have changed its IP address */
1381 int changed = 0;
1382
1383 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
1384 if (session->client_cid) {
1385 if ((session->is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1386 length > (OFF_CID_DTLS13 + session->client_cid->length) &&
1387 memcmp(session->client_cid->s, &payload[OFF_CID_DTLS13],
1388 session->client_cid->length) == 0) ||
1389 (!session->is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1390 length > (OFF_CID + session->client_cid->length) &&
1391 memcmp(session->client_cid->s, &payload[OFF_CID],
1392 session->client_cid->length) == 0)) {
1393 /* Updating IP address */
1394 coap_log_info("***%s: CID: Old Client Session\n", coap_session_str(session));
1395 SESSIONS_DELETE(endpoint->sessions, session);
1396 session->addr_info = packet->addr_info;
1397 memcpy(&session->addr_hash, &addr_hash, sizeof(session->addr_hash));
1398 SESSIONS_ADD(endpoint->sessions, session);
1399 coap_log_info("***%s: CID: New Client Session\n", coap_session_str(session));
1400 return session;
1401 }
1402 }
1403 }
1404 if (!changed) {
1405 coap_log_debug("coap_dtls_hello: ContentType Connection-IS dropped\n");
1406 return NULL;
1407 }
1408 } else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1409 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1410 /* only log if not a late alert */
1411 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1412 coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1413 payload[OFF_CONTENT_TYPE],
1414 payload[OFF_HANDSHAKE_TYPE]);
1415 return NULL;
1416 }
1417 }
1418
1419 session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER,
1420 &addr_hash, &packet->addr_info.local,
1421 &packet->addr_info.remote,
1422 packet->ifindex, endpoint->context, endpoint);
1423 /* Aids debugging endpoint created session */
1424 debug_session = session;
1425 if (session) {
1426 session->last_rx_tx = now;
1427 memcpy(session->sock.lfunc, endpoint->sock.lfunc,
1428 sizeof(session->sock.lfunc));
1429 if (endpoint->proto == COAP_PROTO_UDP)
1431 else if (endpoint->proto == COAP_PROTO_DTLS) {
1432 session->type = COAP_SESSION_TYPE_HELLO;
1433 }
1434 SESSIONS_ADD(endpoint->sessions, session);
1435 coap_log_debug("***%s: session %p: new incoming session\n",
1436 coap_session_str(session), (void *)session);
1438 }
1439 return session;
1440}
1441
1444 coap_tick_t now) {
1445 if (session) {
1446 session->last_rx_tx = now;
1447 session->type = COAP_SESSION_TYPE_SERVER;
1448 coap_dtls_establish(session);
1449 }
1450 return session;
1451}
1452#endif /* COAP_SERVER_SUPPORT */
1453
1454#if COAP_CLIENT_SUPPORT
1455static coap_session_t *
1456coap_session_create_client(coap_context_t *ctx,
1457 const coap_address_t *local_if,
1458 const coap_address_t *server,
1459 coap_proto_t proto,
1460 void *app_data,
1462 coap_str_const_t *ws_host) {
1463 coap_session_t *session = NULL;
1464 int default_port = COAP_DEFAULT_PORT;
1465
1466 assert(server);
1467
1468 switch (proto) {
1469 case COAP_PROTO_UDP:
1470 default_port = COAP_DEFAULT_PORT;
1471 break;
1472 case COAP_PROTO_DTLS:
1473 if (!coap_dtls_is_supported()) {
1474 coap_log_crit("coap_new_client_session*: DTLS not supported\n");
1475 return NULL;
1476 }
1477 default_port = COAPS_DEFAULT_PORT;
1478 break;
1479 case COAP_PROTO_TCP:
1480 if (!coap_tcp_is_supported()) {
1481 coap_log_crit("coap_new_client_session*: TCP not supported\n");
1482 return NULL;
1483 }
1484 default_port = COAP_DEFAULT_PORT;
1485 break;
1486 case COAP_PROTO_TLS:
1487 if (!coap_tls_is_supported()) {
1488 coap_log_crit("coap_new_client_session*: TLS not supported\n");
1489 return NULL;
1490 }
1491 default_port = COAPS_DEFAULT_PORT;
1492 break;
1493 case COAP_PROTO_WS:
1494 if (!coap_ws_is_supported()) {
1495 coap_log_crit("coap_new_client_session*: WS not supported\n");
1496 return NULL;
1497 }
1498 default_port = 80;
1499 break;
1500 case COAP_PROTO_WSS:
1501 if (!coap_wss_is_supported()) {
1502 coap_log_crit("coap_new_client_session*: WSS not supported\n");
1503 return NULL;
1504 }
1505 default_port = 443;
1506 break;
1507 case COAP_PROTO_NONE:
1508 case COAP_PROTO_LAST:
1509 default:
1510 assert(0);
1511 return NULL;
1512 }
1514 local_if, server, 0, ctx, NULL);
1515 if (!session)
1516 goto error;
1517
1519 session->sock.session = session;
1520 memcpy(&session->sock.lfunc, coap_layers_coap[proto],
1521 sizeof(session->sock.lfunc));
1522
1523 session->app_data = app_data;
1524 session->app_cb = app_data ? callback : NULL;
1525#if COAP_WS_SUPPORT
1526 if (ws_host) {
1527 session->ws_host = coap_new_str_const(ws_host->s, ws_host->length);
1528 }
1529#else /* ! COAP_WS_SUPPORT */
1530 (void)ws_host;
1531#endif /* ! COAP_WS_SUPPORT */
1532
1533 if (COAP_PROTO_NOT_RELIABLE(proto)) {
1534 coap_session_t *s, *rtmp;
1535 if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
1536 goto error;
1537 }
1538 /* Check that this is not a duplicate 4-tuple */
1539 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
1542 &s->addr_info.local) &&
1544 &s->addr_info.remote)) {
1545 coap_log_warn("***%s: session %p: duplicate - already exists\n",
1546 coap_session_str(session), (void *)session);
1547 goto error;
1548 }
1549 }
1550#ifdef WITH_CONTIKI
1551 session->sock.context = ctx;
1552#endif /* WITH_CONTIKI */
1553#if !COAP_DISABLE_TCP
1554 } else if (COAP_PROTO_RELIABLE(proto)) {
1555 if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
1556 goto error;
1557 }
1558#endif /* !COAP_DISABLE_TCP */
1559 }
1560
1561 session->sock.session = session;
1562#ifdef COAP_EPOLL_SUPPORT
1563 coap_epoll_ctl_add(&session->sock,
1564 EPOLLIN |
1565 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1566 EPOLLOUT : 0),
1567 __func__);
1568#endif /* COAP_EPOLL_SUPPORT */
1569
1571 if (local_if)
1572 session->sock.flags |= COAP_SOCKET_BOUND;
1573#if COAP_SERVER_SUPPORT
1574 if (ctx->proxy_uri_resource)
1575 session->proxy_session = 1;
1576#endif /* COAP_SERVER_SUPPORT */
1577 SESSIONS_ADD(ctx->sessions, session);
1578 return session;
1579
1580error:
1581 /*
1582 * Need to add in the session as coap_session_release_lkd()
1583 * will call SESSIONS_DELETE in coap_session_free().
1584 */
1585 if (session)
1586 SESSIONS_ADD(ctx->sessions, session);
1587 coap_session_release_lkd(session);
1588 return NULL;
1589}
1590
1591static void
1592coap_session_check_connect(coap_session_t *session) {
1593 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1594 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1595 }
1596#if !COAP_DISABLE_TCP
1597 if (COAP_PROTO_RELIABLE(session->proto)) {
1598 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1600 if (session->client_initiated) {
1601 session->doing_first = 1;
1602 }
1603 } else {
1604 /* Initial connect worked immediately */
1605 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
1606 }
1607 }
1608#endif /* !COAP_DISABLE_TCP */
1609 coap_ticks(&session->last_rx_tx);
1610}
1611#endif /* COAP_CLIENT_SUPPORT */
1612
1613void
1615 if (COAP_PROTO_NOT_RELIABLE(session->proto))
1616 coap_session_connected(session);
1617#if !COAP_DISABLE_TCP
1618 if (COAP_PROTO_RELIABLE(session->proto))
1619 coap_session_send_csm(session);
1620#endif /* !COAP_DISABLE_TCP */
1621}
1622
1623#if COAP_CLIENT_SUPPORT
1624int
1626 int default_port = COAP_DEFAULT_PORT;
1627
1628 if (session->sock.lfunc[COAP_LAYER_SESSION].l_close && !session->session_failed)
1629 session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
1630
1631 switch (session->proto) {
1632 case COAP_PROTO_UDP:
1633 default_port = COAP_DEFAULT_PORT;
1634 break;
1635 case COAP_PROTO_DTLS:
1636 default_port = COAPS_DEFAULT_PORT;
1637 break;
1638 case COAP_PROTO_TCP:
1639 default_port = COAP_DEFAULT_PORT;
1640 break;
1641 case COAP_PROTO_TLS:
1642 default_port = COAPS_DEFAULT_PORT;
1643 break;
1644 case COAP_PROTO_WS:
1645 default_port = 80;
1646 break;
1647 case COAP_PROTO_WSS:
1648 default_port = 443;
1649 break;
1650 case COAP_PROTO_NONE:
1651 case COAP_PROTO_LAST:
1652 default:
1653 assert(0);
1654 return 0;
1655 }
1656 session->sock.session = session;
1657 coap_log_debug("***%s: trying to reconnect\n", coap_session_str(session));
1659 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
1660 session->state = COAP_SESSION_STATE_NONE;
1661 if (!coap_netif_dgrm_connect(session, &session->local_reconnect, &session->addr_info.remote,
1662 default_port)) {
1663 goto error;
1664 }
1666#ifdef WITH_CONTIKI
1667 session->sock.context = session->context;
1668#endif /* WITH_CONTIKI */
1669#if !COAP_DISABLE_TCP
1670 } else if (COAP_PROTO_RELIABLE(session->proto)) {
1671 if (!coap_netif_strm_connect1(session, &session->local_reconnect, &session->addr_info.remote,
1672 default_port)) {
1673 goto error;
1674 }
1675#endif /* !COAP_DISABLE_TCP */
1676 } else {
1677 goto error;
1678 }
1679#ifdef COAP_EPOLL_SUPPORT
1680 coap_epoll_ctl_add(&session->sock,
1681 EPOLLIN |
1682 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
1683 EPOLLOUT : 0),
1684 __func__);
1685#endif /* COAP_EPOLL_SUPPORT */
1686
1688 session->sock.flags |= COAP_SOCKET_BOUND;
1689 coap_session_check_connect(session);
1690 return 1;
1691error:
1692 return 0;
1693}
1694
1695void
1697 coap_lg_crcv_t *lg_crcv, *etmp;
1698
1699 if (!session->session_failed)
1700 return;
1701 coap_log_debug("***%s: session re-established\n",
1702 coap_session_str(session));
1703 session->session_failed = 0;
1704 session->retry_count = 0;
1706 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
1707 coap_pdu_reference_lkd(lg_crcv->sent_pdu);
1708 coap_send_internal(session, lg_crcv->sent_pdu, NULL);
1709 }
1710}
1711
1714 const coap_address_t *local_if,
1715 const coap_address_t *server,
1716 coap_proto_t proto) {
1717 coap_session_t *session;
1718
1719 coap_lock_lock(return NULL);
1720 session = coap_new_client_session3_lkd(ctx, local_if, server, proto, NULL, NULL, NULL);
1722 return session;
1723}
1724
1727 const coap_address_t *local_if,
1728 const coap_address_t *server,
1729 coap_proto_t proto,
1730 void *app_data,
1732 coap_str_const_t *ws_host) {
1733 coap_session_t *session;
1734
1735 coap_lock_lock(return NULL);
1736 session = coap_new_client_session3_lkd(ctx, local_if, server, proto, app_data, callback, ws_host);
1738 return session;
1739}
1740
1743 const coap_address_t *local_if,
1744 const coap_address_t *server,
1745 coap_proto_t proto,
1746 void *app_data,
1748 coap_str_const_t *ws_host) {
1749 coap_session_t *session;
1750
1752 session = coap_session_create_client(ctx, local_if, server,
1753 proto, app_data, callback, ws_host);
1754 if (session) {
1755 coap_log_debug("***%s: session %p: created outgoing session\n",
1756 coap_session_str(session), (void *)session);
1757 coap_session_check_connect(session);
1758 }
1759 return session;
1760}
1761
1764 const coap_address_t *local_if,
1765 const coap_address_t *server,
1766 coap_proto_t proto, const char *identity,
1767 const uint8_t *key, unsigned key_len) {
1768 coap_session_t *session;
1769
1770 coap_lock_lock(return NULL);
1771 session = coap_new_client_session_psk_lkd(ctx, local_if, server, proto, identity, key, key_len);
1773 return session;
1774}
1775
1778 const coap_address_t *local_if,
1779 const coap_address_t *server,
1780 coap_proto_t proto, const char *identity,
1781 const uint8_t *key, unsigned key_len) {
1782 coap_dtls_cpsk_t setup_data;
1783
1785 memset(&setup_data, 0, sizeof(setup_data));
1787
1788 if (identity) {
1789 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1790 setup_data.psk_info.identity.length = strlen(identity);
1791 }
1792
1793 if (key && key_len > 0) {
1794 setup_data.psk_info.key.s = key;
1795 setup_data.psk_info.key.length = key_len;
1796 }
1797
1798 return coap_new_client_session_psk3_lkd(ctx, local_if, server,
1799 proto, &setup_data, NULL, NULL, NULL);
1800}
1801
1802/*
1803 * Check the validity of the SNI to send to the server.
1804 *
1805 * https://datatracker.ietf.org/doc/html/rfc6066#section-3
1806 * Literal IPv4 and IPv6 addresses are not permitted in "HostName".
1807 */
1808static void
1809coap_sanitize_client_sni(char **client_sni) {
1810
1811 if (*client_sni == NULL)
1812 return;
1813
1814#if defined(HAVE_INET_PTON) && defined(HAVE_ARPA_INET_H)
1815 coap_address_t addr;
1816#if COAP_IPV4_SUPPORT
1817 if (inet_pton(AF_INET, *client_sni, &addr.addr.sin.sin_addr) == 1) {
1818 *client_sni = NULL;
1819 return;
1820 }
1821#endif /* COAP_IPV4_SUPPORT */
1822#if COAP_IPV6_SUPPORT
1823 if (inet_pton(AF_INET6, *client_sni, &addr.addr.sin6.sin6_addr) == 1) {
1824 *client_sni = NULL;
1825 return;
1826 }
1827#endif /* COAP_IPV6_SUPPORT */
1828#else /* ! HAVE_INET_PTON || ! HAVE_ARPA_INET_H */
1829 char *cp;
1830 char has_colon = 0;
1831
1832 cp = *client_sni;
1833 switch (*cp) {
1834 case '0':
1835 case '1':
1836 case '2':
1837 case '3':
1838 case '4':
1839 case '5':
1840 case '6':
1841 case '7':
1842 case '8':
1843 case '9':
1844 case 'a':
1845 case 'b':
1846 case 'c':
1847 case 'd':
1848 case 'e':
1849 case 'f':
1850 case 'A':
1851 case 'B':
1852 case 'C':
1853 case 'D':
1854 case 'E':
1855 case 'F':
1856 case ':':
1857 break;
1858 case '\000':
1859 /* Empty entry invalid */
1860 *client_sni = NULL;
1861 return;
1862 default:
1863 /* Does not start with a hex digit or : - not literal IP. */
1864 return;
1865 }
1866 /* Check for IPv4 */
1867 while (*cp) {
1868 switch (*cp) {
1869 case '0':
1870 case '1':
1871 case '2':
1872 case '3':
1873 case '4':
1874 case '5':
1875 case '6':
1876 case '7':
1877 case '8':
1878 case '9':
1879 case '.':
1880 break;
1881 default:
1882 /* Not of format nnn.nnn.nnn.nnn. Could be IPv6 */
1883 goto check_ipv6;
1884 }
1885 cp++;
1886 }
1887 /* IPv4 address - not allowed. */
1888 *client_sni = NULL;
1889 return;
1890
1891check_ipv6:
1892 /* Check for IPv6 */
1893 cp = *client_sni;
1894 while (*cp) {
1895 switch (*cp) {
1896 case '0':
1897 case '1':
1898 case '2':
1899 case '3':
1900 case '4':
1901 case '5':
1902 case '6':
1903 case '7':
1904 case '8':
1905 case '9':
1906 case 'a':
1907 case 'b':
1908 case 'c':
1909 case 'd':
1910 case 'e':
1911 case 'f':
1912 case 'A':
1913 case 'B':
1914 case 'C':
1915 case 'D':
1916 case 'E':
1917 case 'F':
1918 break;
1919 case ':':
1920 has_colon = 1;
1921 break;
1922 case '%':
1923 /* Start of i/f specification, Previous is IPv6 */
1924 *client_sni = NULL;
1925 return;
1926 default:
1927 /* Not of format xx:xx::xx. */
1928 return;
1929 }
1930 cp++;
1931 }
1932 if (has_colon)
1933 *client_sni = NULL;
1934#endif /* ! HAVE_INET_PTON || ! HAVE_ARPA_INET_H */
1935 return;
1936}
1937
1940 const coap_address_t *local_if,
1941 const coap_address_t *server,
1942 coap_proto_t proto,
1943 coap_dtls_cpsk_t *setup_data) {
1944 coap_session_t *session;
1945
1946 coap_lock_lock(return NULL);
1947 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto, setup_data,
1948 NULL, NULL, NULL);
1950 return session;
1951}
1952
1955 const coap_address_t *local_if,
1956 const coap_address_t *server,
1957 coap_proto_t proto,
1958 coap_dtls_cpsk_t *setup_data,
1959 void *app_data,
1961 coap_str_const_t *ws_host) {
1962 coap_session_t *session;
1963
1964 coap_lock_lock(return NULL);
1965 session = coap_new_client_session_psk3_lkd(ctx, local_if, server, proto,
1966 setup_data, app_data, callback, ws_host);
1968 return session;
1969}
1970
1973 const coap_address_t *local_if,
1974 const coap_address_t *server,
1975 coap_proto_t proto,
1976 coap_dtls_cpsk_t *setup_data,
1977 void *app_data,
1979 coap_str_const_t *ws_host) {
1980 coap_session_t *session;
1981
1983 session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host);
1984
1985 if (!session || !setup_data)
1986 return NULL;
1987
1988 session->cpsk_setup_data = *setup_data;
1989 if (setup_data->psk_info.identity.s) {
1990 session->psk_identity =
1992 setup_data->psk_info.identity.length);
1993 if (!session->psk_identity) {
1994 coap_log_warn("Cannot store session Identity (PSK)\n");
1995 coap_session_release_lkd(session);
1996 return NULL;
1997 }
1999 coap_log_warn("Identity (PSK) not defined\n");
2000 coap_session_release_lkd(session);
2001 return NULL;
2002 }
2003
2004 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
2005 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
2006 setup_data->psk_info.key.length);
2007 if (!session->psk_key) {
2008 coap_log_warn("Cannot store session pre-shared key (PSK)\n");
2009 coap_session_release_lkd(session);
2010 return NULL;
2011 }
2013 coap_log_warn("Pre-shared key (PSK) not defined\n");
2014 coap_session_release_lkd(session);
2015 return NULL;
2016 }
2017
2018 coap_sanitize_client_sni(&session->cpsk_setup_data.client_sni);
2019
2021 if (!coap_dtls_context_set_cpsk(ctx, &session->cpsk_setup_data)) {
2022 coap_session_release_lkd(session);
2023 return NULL;
2024 }
2025 }
2026 coap_log_debug("***%s: new outgoing session\n",
2027 coap_session_str(session));
2028 coap_session_check_connect(session);
2029 return session;
2030}
2031#endif /* COAP_CLIENT_SUPPORT */
2032
2033int
2035 const coap_bin_const_t *psk_hint
2036 ) {
2037 /* We may be refreshing the hint with the same hint */
2038 coap_bin_const_t *old_psk_hint = session->psk_hint;
2039
2040 if (psk_hint && psk_hint->s) {
2041 if (session->psk_hint) {
2042 if (coap_binary_equal(session->psk_hint, psk_hint))
2043 return 1;
2044 }
2045 session->psk_hint = coap_new_bin_const(psk_hint->s,
2046 psk_hint->length);
2047 if (!session->psk_hint) {
2048 coap_log_err("No memory to store identity hint (PSK)\n");
2049 if (old_psk_hint)
2050 coap_delete_bin_const(old_psk_hint);
2051 return 0;
2052 }
2053 } else {
2054 session->psk_hint = NULL;
2055 }
2056 if (old_psk_hint)
2057 coap_delete_bin_const(old_psk_hint);
2058
2059 return 1;
2060}
2061
2062int
2064 const coap_bin_const_t *psk_key
2065 ) {
2066 /* We may be refreshing the key with the same key */
2067 coap_bin_const_t *old_psk_key = session->psk_key;
2068
2069 if (psk_key && psk_key->s) {
2070 if (session->psk_key) {
2071 if (coap_binary_equal(session->psk_key, psk_key))
2072 return 1;
2073 }
2074 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
2075 if (!session->psk_key) {
2076 coap_log_err("No memory to store pre-shared key (PSK)\n");
2077 if (old_psk_key)
2078 coap_delete_bin_const(old_psk_key);
2079 return 0;
2080 }
2081 } else {
2082 session->psk_key = NULL;
2083 }
2084 if (old_psk_key)
2085 coap_delete_bin_const(old_psk_key);
2086
2087 return 1;
2088}
2089
2090int
2092 const coap_bin_const_t *psk_identity
2093 ) {
2094 /* We may be refreshing the identity with the same identity */
2095 coap_bin_const_t *old_psk_identity = session->psk_identity;
2096
2097 if (psk_identity && psk_identity->s) {
2098 if (session->psk_identity) {
2099 if (coap_binary_equal(session->psk_identity, psk_identity))
2100 return 1;
2101 }
2102 session->psk_identity = coap_new_bin_const(psk_identity->s,
2103 psk_identity->length);
2104 if (!session->psk_identity) {
2105 coap_log_err("No memory to store pre-shared key identity (PSK)\n");
2106 if (old_psk_identity)
2107 coap_delete_bin_const(old_psk_identity);
2108 return 0;
2109 }
2110 } else {
2111 session->psk_identity = NULL;
2112 }
2113 if (old_psk_identity)
2114 coap_delete_bin_const(old_psk_identity);
2115
2116 return 1;
2117}
2118
2119#if COAP_SERVER_SUPPORT
2120const coap_bin_const_t *
2122 if (session)
2123 return session->psk_hint;
2124 return NULL;
2125}
2126#endif /* COAP_SERVER_SUPPORT */
2127
2128const coap_bin_const_t *
2130 const coap_bin_const_t *psk_identity = NULL;
2131 if (session) {
2132 psk_identity = session->psk_identity;
2133 if (psk_identity == NULL) {
2134 psk_identity = &session->cpsk_setup_data.psk_info.identity;
2135 }
2136 }
2137 return psk_identity;
2138}
2139
2140const coap_bin_const_t *
2142 if (session)
2143 return session->psk_key;
2144 return NULL;
2145}
2146
2147#if COAP_CLIENT_SUPPORT
2150 const coap_address_t *local_if,
2151 const coap_address_t *server,
2152 coap_proto_t proto,
2153 coap_dtls_pki_t *setup_data) {
2154 coap_session_t *session;
2155
2156 coap_lock_lock(return NULL);
2157 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data,
2158 NULL, NULL, NULL);
2160 return session;
2161}
2162
2165 const coap_address_t *local_if,
2166 const coap_address_t *server,
2167 coap_proto_t proto,
2168 coap_dtls_pki_t *setup_data,
2169 void *app_data,
2171 coap_str_const_t *ws_host) {
2172 coap_session_t *session;
2173
2174 coap_lock_lock(return NULL);
2175 session = coap_new_client_session_pki3_lkd(ctx, local_if, server, proto, setup_data,
2176 app_data, callback, ws_host);
2178 return session;
2179}
2180
2183 const coap_address_t *local_if,
2184 const coap_address_t *server,
2185 coap_proto_t proto,
2186 coap_dtls_pki_t *setup_data,
2187 void *app_data,
2189 coap_str_const_t *ws_host) {
2190 coap_session_t *session;
2191 coap_dtls_pki_t l_setup_data;
2192
2193 if (!setup_data)
2194 return NULL;
2195 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
2196 coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
2197 return NULL;
2198 }
2199
2201 l_setup_data = *setup_data;
2202 coap_sanitize_client_sni(&l_setup_data.client_sni);
2203
2204 session = coap_session_create_client(ctx, local_if, server, proto, app_data, callback, ws_host);
2205
2206 if (!session) {
2207 return NULL;
2208 }
2209
2211 /* we know that setup_data is not NULL */
2212 if (!coap_dtls_context_set_pki(ctx, &l_setup_data, COAP_DTLS_ROLE_CLIENT)) {
2213 coap_session_release_lkd(session);
2214 return NULL;
2215 }
2216 }
2217 coap_log_debug("***%s: new outgoing session\n",
2218 coap_session_str(session));
2219 coap_session_check_connect(session);
2220 return session;
2221}
2222#endif /* ! COAP_CLIENT_SUPPORT */
2223
2224#if COAP_SERVER_SUPPORT
2225#if !COAP_DISABLE_TCP
2227coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra) {
2228 coap_session_t *session;
2229 coap_tick_t now;
2230
2231 session = coap_make_session(ep->proto, COAP_SESSION_TYPE_SERVER,
2232 NULL, NULL, NULL, 0, ctx, ep);
2233 if (!session)
2234 goto error;
2235
2236 memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
2237 if (!coap_netif_strm_accept(ep, session, extra))
2238 goto error;
2239
2240 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
2241
2242 session->sock.session = session;
2243#ifdef COAP_EPOLL_SUPPORT
2244 coap_epoll_ctl_add(&session->sock,
2245 EPOLLIN,
2246 __func__);
2247#endif /* COAP_EPOLL_SUPPORT */
2248 SESSIONS_ADD(ep->sessions, session);
2249 if (session) {
2250 coap_ticks(&now);
2251 session->last_rx_tx = now;
2252 coap_log_debug("***%s: session %p: new incoming session\n",
2253 coap_session_str(session), (void *)session);
2257 session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
2258 }
2259 return session;
2260
2261error:
2262 /*
2263 * Need to add in the session as coap_session_release_lkd()
2264 * will call SESSIONS_DELETE in coap_session_free().
2265 */
2266 if (session) {
2267 SESSIONS_ADD(ep->sessions, session);
2268 coap_session_free(session);
2269 }
2270 return NULL;
2271}
2272#endif /* !COAP_DISABLE_TCP */
2273#endif /* COAP_SERVER_SUPPORT */
2274
2275void
2277 const uint8_t *data) {
2278 session->tx_token = coap_decode_var_bytes8(data, len);
2279 /*
2280 * Decrement as when first used by coap_session_new_token() it will
2281 * get incremented
2282 */
2283 session->tx_token--;
2284}
2285
2286void
2288 uint8_t *data) {
2289 *len = coap_encode_var_safe8(data,
2290 sizeof(session->tx_token), ++session->tx_token);
2291}
2292
2293COAP_API uint16_t
2295 uint16_t mid;
2296
2297 coap_lock_lock(return 0);
2298 mid = coap_new_message_id_lkd(session);
2300 return mid;
2301}
2302
2303uint16_t
2306 if (COAP_PROTO_NOT_RELIABLE(session->proto))
2307 return ++session->tx_mid;
2308 /* TCP/TLS have no notion of mid */
2309 return 0;
2310}
2311
2312const coap_address_t *
2314 if (session)
2315 return &session->addr_info.remote;
2316 return NULL;
2317}
2318
2319const coap_address_t *
2321 if (session)
2322 return &session->addr_info.local;
2323 return NULL;
2324}
2325
2326const coap_address_t *
2328#if COAP_CLIENT_SUPPORT
2329 if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
2330 session->sock.flags & COAP_SOCKET_MULTICAST)
2331 return &session->sock.mcast_addr;
2332#else /* ! COAP_CLIENT_SUPPORT */
2333 (void)session;
2334#endif /* ! COAP_CLIENT_SUPPORT */
2335 return NULL;
2336}
2337
2340 if (session)
2341 return session->context;
2342 return NULL;
2343}
2344
2347 if (session)
2348 return session->proto;
2349 return 0;
2350}
2351
2354 if (session)
2355 return session->type;
2356 return 0;
2357}
2358
2359int
2361#if COAP_OSCORE_SUPPORT
2362 return (session && session->oscore_encryption != 0) ? 1 : 0;
2363#else
2364 (void)session;
2365 return 0;
2366#endif
2367}
2368
2369COAP_API int
2371 int ret;
2372
2373 coap_lock_lock(return 0);
2374 ret = coap_session_set_type_client_lkd(session, 1);
2376 return ret;
2377}
2378
2379int
2381#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2382 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
2384 session->type = COAP_SESSION_TYPE_CLIENT;
2385 if (session->endpoint) {
2386 SESSIONS_DELETE(session->endpoint->sessions, session);
2387 SESSIONS_ADD(session->context->sessions, session);
2388 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
2389 session->sock = session->endpoint->sock;
2390 session->sock.flags |= COAP_SOCKET_SLAVE;
2391 session->sock.flags &= ~COAP_SOCKET_CAN_READ;
2392 session->sock.flags &= ~COAP_SOCKET_WANT_READ;
2393 } else {
2394 session->sock.endpoint = NULL;
2395 }
2396 session->endpoint = NULL;
2397 }
2398 if (report_changed) {
2399 coap_log_debug("***%s: session now is type Client\n",
2400 coap_session_str(session));
2401 }
2402 return 1;
2403 }
2404#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2405 (void)session;
2406 (void)report_changed;
2407#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2408 return 0;
2409}
2410
2411COAP_API int
2413 int ret;
2414
2415 coap_lock_lock(return 0);
2416 ret = coap_session_set_type_server_lkd(session);
2418 return ret;
2419}
2420
2421int
2423#if COAP_CLIENT_SUPPORT && COAP_SERVER_SUPPORT
2424 if (session && session->type == COAP_SESSION_TYPE_CLIENT) {
2425 coap_endpoint_t *ep;
2426 coap_mid_t mid;
2427 coap_pdu_t *ping = NULL;
2428
2429 LL_FOREACH(session->context->endpoint, ep) {
2430 if (session->proto == ep->proto)
2431 break;
2432 }
2433 if (!ep) {
2434 /* Need a dummy endpoint to 'hang' off as this is now a server */
2435 ep = coap_malloc_endpoint();
2436 if (!ep) {
2437 coap_log_warn("coap_new_endpoint: malloc");
2438 return 0;
2439 }
2440
2441 memset(ep, 0, sizeof(coap_endpoint_t));
2442 ep->context = session->context;
2443 ep->proto = session->proto;
2444 ep->sock.endpoint = ep;
2445 memcpy(&ep->sock.lfunc, coap_layers_coap[session->proto], sizeof(ep->sock.lfunc));
2446
2447 ep->default_mtu = COAP_DEFAULT_MTU;
2448 LL_PREPEND(session->context->endpoint, ep);
2449 }
2450 /* Need to use coap_send_lkd() here to get traffic flowing */
2451 if (session->proto == COAP_PROTO_UDP) {
2453 coap_new_message_id_lkd(session), 0);
2454 if (!ping) {
2455 session->type = COAP_SESSION_TYPE_SERVER;
2456 return 0;
2457 }
2458 mid = coap_send_lkd(session, ping);
2459 if (mid != COAP_INVALID_MID) {
2460 coap_tick_t now;
2461
2462 coap_ticks(&now);
2463 session->last_ping_mid = mid;
2464 session->last_rx_tx = now;
2465 session->last_ping = now;
2466 }
2467 }
2468 /*
2469 * (D)TLS will initiate traffic with handshake.
2470 * Reliable protocols will be sending a CSM, so no need for Ping.
2471 */
2472 if (session->context) {
2473 if (session->context->sessions) {
2474 SESSIONS_DELETE(session->context->sessions, session);
2475 }
2476 }
2477 SESSIONS_ADD(ep->sessions, session);
2478 session->endpoint = ep;
2479 session->type = COAP_SESSION_TYPE_SERVER;
2480 coap_session_release_lkd(session);
2481 coap_log_debug("***%s: session now is type Server\n",
2482 coap_session_str(session));
2483 return 1;
2484 }
2485#else /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2486 (void)session;
2487#endif /* ! COAP_CLIENT_SUPPORT || ! COAP_SERVER_SUPPORT */
2488 return 0;
2489}
2490
2493 if (session)
2494 return session->state;
2495 return 0;
2496}
2497
2500#if COAP_SERVER_SUPPORT
2501 if (session)
2502 return session->endpoint;
2503#else /* ! COAP_SERVER_SUPPORT */
2504 (void)session;
2505#endif /* ! COAP_SERVER_SUPPORT */
2506 return 0;
2507}
2508
2509int
2511 if (session)
2512 return session->ifindex;
2513 return -1;
2514}
2515
2516void *
2518 coap_tls_library_t *tls_lib) {
2519 if (session)
2520 return coap_dtls_get_tls(session, tls_lib);
2521 return NULL;
2522}
2523
2524static const char *
2526 switch (proto) {
2527 case COAP_PROTO_UDP:
2528 return "UDP ";
2529 case COAP_PROTO_DTLS:
2530 return "DTLS";
2531 case COAP_PROTO_TCP:
2532 return "TCP ";
2533 case COAP_PROTO_TLS:
2534 return "TLS ";
2535 case COAP_PROTO_WS:
2536 return "WS ";
2537 case COAP_PROTO_WSS:
2538 return "WSS ";
2539 case COAP_PROTO_NONE:
2540 case COAP_PROTO_LAST:
2541 default:
2542 return "????" ;
2543 break;
2544 }
2545 return NULL;
2546}
2547
2548#if COAP_SERVER_SUPPORT
2550coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
2551 coap_endpoint_t *endpoint;
2552
2553 coap_lock_lock(return NULL);
2554 endpoint = coap_new_endpoint_lkd(context, listen_addr, proto);
2556 return endpoint;
2557}
2558
2560coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr,
2561 coap_proto_t proto) {
2562 coap_endpoint_t *ep = NULL;
2563
2564 if (!context || !listen_addr) {
2565 coap_log_debug("coap_new_endpoint: Both context and listen_addr need to be defined\n");
2566 return NULL;
2567 }
2568
2570
2571 switch (proto) {
2572 case COAP_PROTO_UDP:
2573 break;
2574 case COAP_PROTO_DTLS:
2575 if (!coap_dtls_is_supported()) {
2576 coap_log_warn("coap_new_endpoint: DTLS not supported\n");
2577 return NULL;
2578 }
2579 break;
2580 case COAP_PROTO_TLS:
2581 if (!coap_tls_is_supported()) {
2582 coap_log_warn("coap_new_endpoint: TLS not supported\n");
2583 return NULL;
2584 }
2585 break;
2586 case COAP_PROTO_TCP:
2587 if (!coap_tcp_is_supported()) {
2588 coap_log_warn("coap_new_endpoint: TCP not supported\n");
2589 return NULL;
2590 }
2591 break;
2592 case COAP_PROTO_WS:
2593 if (!coap_ws_is_supported()) {
2594 coap_log_warn("coap_new_endpoint: WS not supported\n");
2595 return NULL;
2596 }
2597 break;
2598 case COAP_PROTO_WSS:
2599 if (!coap_wss_is_supported()) {
2600 coap_log_warn("coap_new_endpoint: WSS not supported\n");
2601 return NULL;
2602 }
2603 break;
2604 case COAP_PROTO_NONE:
2605 case COAP_PROTO_LAST:
2606 default:
2607 coap_log_crit("coap_new_endpoint: Unsupported protocol %d\n", proto);
2608 return NULL;
2609 }
2610
2611 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
2612 proto == COAP_PROTO_WSS) {
2614 coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
2615 "coap_context_set_pki() not called\n");
2616 return NULL;
2617 }
2618 }
2619 ep = coap_malloc_endpoint();
2620 if (!ep) {
2621 coap_log_warn("coap_new_endpoint: malloc");
2622 return NULL;
2623 }
2624
2625 memset(ep, 0, sizeof(coap_endpoint_t));
2626 ep->context = context;
2627 ep->proto = proto;
2628 ep->sock.endpoint = ep;
2629 if (proto < COAP_PROTO_LAST) {
2630 /*
2631 * Should be caught above, but come compilers realize that proto
2632 * includes COAP_PROTO_LAST, but there is no table entry for that
2633 */
2634 memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
2635 }
2636
2637 if (COAP_PROTO_NOT_RELIABLE(proto)) {
2638 if (!coap_netif_dgrm_listen(ep, listen_addr))
2639 goto error;
2640#ifdef WITH_CONTIKI
2641 ep->sock.context = context;
2642#endif /* WITH_CONTIKI */
2643#if !COAP_DISABLE_TCP
2644 } else if (COAP_PROTO_RELIABLE(proto)) {
2645 if (!coap_netif_strm_listen(ep, listen_addr))
2646 goto error;
2647#endif /* !COAP_DISABLE_TCP */
2648 } else {
2649 coap_log_crit("coap_new_endpoint: Protocol type not supported %d\n", proto);
2650 goto error;
2651 }
2652
2654#ifndef INET6_ADDRSTRLEN
2655#define INET6_ADDRSTRLEN 40
2656#endif
2657 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
2658
2659 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
2660 coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
2661 addr_str);
2662 }
2663 }
2664
2665 ep->default_mtu = COAP_DEFAULT_MTU;
2666
2667#ifdef COAP_EPOLL_SUPPORT
2668 ep->sock.endpoint = ep;
2669 coap_epoll_ctl_add(&ep->sock,
2670 EPOLLIN,
2671 __func__);
2672#endif /* COAP_EPOLL_SUPPORT */
2673
2674 LL_PREPEND(context->endpoint, ep);
2675 return ep;
2676
2677error:
2678 coap_free_endpoint_lkd(ep);
2679 return NULL;
2680}
2681
2682void
2684 ep->default_mtu = (uint16_t)mtu;
2685}
2686
2687COAP_API void
2689 if (ep) {
2690 coap_context_t *context = ep->context;
2691 if (context) {
2692 coap_lock_lock(return);
2693 }
2694 coap_free_endpoint_lkd(ep);
2695 if (context) {
2697 }
2698 }
2699}
2700
2701void
2702coap_free_endpoint_lkd(coap_endpoint_t *ep) {
2703 if (ep) {
2704 coap_session_t *session, *rtmp;
2705
2706 if (ep->context) {
2707 /* If fully allocated and inserted */
2709 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
2710 if (session->ref > 0) {
2712#if COAP_CLIENT_SUPPORT
2713 if (session->client_initiated) {
2714 coap_session_release_lkd(session);
2715 }
2716#endif /* COAP_CLIENT_SUPPORT */
2717 }
2718 assert(session->ref == 0);
2719 if (session->ref == 0) {
2721 coap_session_free(session);
2722 }
2723 }
2724 if (coap_netif_available_ep(ep)) {
2725 /*
2726 * ep->sock.endpoint is set in coap_new_endpoint().
2727 * ep->sock.session is never set.
2728 *
2729 * session->sock.session is set for both clients and servers (when a
2730 * new session is accepted), but does not affect the endpoint.
2731 *
2732 * So, it is safe to call coap_netif_close_ep() after all the sessions
2733 * have been freed above as we are only working with the endpoint sock.
2734 */
2735#ifdef COAP_EPOLL_SUPPORT
2736 assert(ep->sock.session == NULL);
2737#endif /* COAP_EPOLL_SUPPORT */
2739 }
2740
2741 if (ep->context->endpoint) {
2742 LL_DELETE(ep->context->endpoint, ep);
2743 }
2744 }
2745
2746 if (ep->app_cb) {
2747 coap_lock_callback(ep->app_cb(ep->app_data));
2748 }
2749
2750 coap_mfree_endpoint(ep);
2751 }
2752}
2753
2754void *
2756 assert(endpoint);
2757 return endpoint->app_data;
2758}
2759
2760COAP_API void *
2761coap_endpoint_set_app_data(coap_endpoint_t *endpoint, void *app_data,
2763 void *old_data;
2764
2765 coap_lock_lock(return NULL);
2766 old_data = coap_endpoint_set_app_data_lkd(endpoint, app_data, callback);
2768 return old_data;
2769}
2770
2771void *
2772coap_endpoint_set_app_data_lkd(coap_endpoint_t *endpoint, void *app_data,
2774 void *old_data = endpoint->app_data;
2775
2776 endpoint->app_data = app_data;
2777 endpoint->app_cb = app_data ? callback : NULL;
2778 return old_data;
2779}
2780
2781#endif /* COAP_SERVER_SUPPORT */
2782
2785 const coap_address_t *remote_addr,
2786 int ifindex) {
2787 coap_session_t *s, *rtmp;
2788#if COAP_CLIENT_SUPPORT
2789 SESSIONS_ITER(ctx->sessions, s, rtmp) {
2790 if (s->ifindex == ifindex) {
2791 if (s->sock.flags & COAP_SOCKET_MULTICAST) {
2792 if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
2793 return s;
2794 } else if (coap_address_equals(&s->addr_info.remote, remote_addr))
2795 return s;
2796 }
2797 }
2798#endif /* COAP_CLIENT_SUPPORT */
2799#if COAP_SERVER_SUPPORT
2800 coap_endpoint_t *ep;
2801
2802 LL_FOREACH(ctx->endpoint, ep) {
2803 SESSIONS_ITER(ep->sessions, s, rtmp) {
2804 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
2805 remote_addr))
2806 return s;
2807 }
2808 }
2809#endif /* COAP_SERVER_SUPPORT */
2810 return NULL;
2811}
2812
2813#ifndef INET6_ADDRSTRLEN
2814#define INET6_ADDRSTRLEN 46
2815#endif
2816const char *
2818 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
2819 char *p = szSession, *end = szSession + sizeof(szSession);
2820
2821 if (!session) {
2822 return "Session not defined";
2823 }
2824 if (coap_print_addr(&session->addr_info.local,
2825 (unsigned char *)p, end - p) > 0)
2826 p += strlen(p);
2827 if (p + 6 < end) {
2828 strcpy(p, " <-> ");
2829 p += 5;
2830 }
2831 if (p + 1 < end) {
2832 if (coap_print_addr(&session->addr_info.remote,
2833 (unsigned char *)p, end - p) > 0)
2834 p += strlen(p);
2835 }
2836 if (session->ifindex > 0 && p + 1 < end)
2837 p += snprintf(p, end - p, " (if%d)", session->ifindex);
2838 if (p + 6 < end) {
2839 strcpy(p, " ");
2840 p++;
2841 strcpy(p, coap_proto_name(session->proto));
2842 }
2843
2844 return szSession;
2845}
2846
2847#if COAP_SERVER_SUPPORT
2848const char *
2849coap_endpoint_str(const coap_endpoint_t *endpoint) {
2850 static char szEndpoint[128];
2851 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
2852 if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
2853 p += strlen(p);
2854 if (p + 6 < end) {
2855 if (endpoint->proto == COAP_PROTO_UDP) {
2856 strcpy(p, " UDP");
2857 } else if (endpoint->proto == COAP_PROTO_DTLS) {
2858 strcpy(p, " DTLS");
2859 } else {
2860 strcpy(p, " NONE");
2861 }
2862 }
2863
2864 return szEndpoint;
2865}
2866#endif /* COAP_SERVER_SUPPORT */
2867#if COAP_CLIENT_SUPPORT
2868void
2870 session->no_observe_cancel = 1;
2871}
2872
2873void
2875 if (session->type != COAP_SESSION_TYPE_CLIENT) {
2876 coap_lock_lock(return);
2878 coap_session_release_lkd(session);
2880 }
2881}
2882#endif /* COAP_CLIENT_SUPPORT */
2883#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:234
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
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:252
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:298
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:396
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:291
#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_SAFE(e, el, rtmp)
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define SESSIONS_DELETE(e, obj)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define SESSIONS_ITER(e, el, rtmp)
#define SESSIONS_FIND(e, k, res)
void coap_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:1642
#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:5475
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:1510
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:1390
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:3272
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:2151
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:3248
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:3261
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:1416
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:3389
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:3280
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:3428
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:155
@ 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:161
@ COAP_EVENT_RECONNECT_NO_MORE
Triggered when a session failed, and retry reconnect attempts failed.
Definition coap_event.h:159
@ 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:157
@ 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:150
#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:813
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:241
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:1773
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
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:474
#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
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:1635
#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:863
#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:184
#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:234
#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:187
#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:240
@ COAP_PROTO_DTLS
Definition coap_pdu.h:237
@ COAP_PROTO_UDP
Definition coap_pdu.h:236
@ COAP_PROTO_NONE
Definition coap_pdu.h:235
@ COAP_PROTO_TLS
Definition coap_pdu.h:239
@ COAP_PROTO_WSS
Definition coap_pdu.h:241
@ COAP_PROTO_TCP
Definition coap_pdu.h:238
@ COAP_PROTO_LAST
Definition coap_pdu.h:242
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:287
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:288
@ COAP_EMPTY_CODE
Definition coap_pdu.h:249
@ 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_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.
#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:1267
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)
Notify session that it has failed.
@ 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:938
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:943
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.
struct sockaddr_in sin
union coap_address_t::@026107330322024121002364065033074202334103236375 addr
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_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 is_dtls13
Set if session is DTLS1.3.
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv).
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app_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
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