libcoap 4.3.1rc1
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 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
17#include "coap3/coap_internal.h"
18
19#ifndef COAP_SESSION_C_
20#define COAP_SESSION_C_
21
22#include <stdio.h>
23
24#ifdef COAP_EPOLL_SUPPORT
25#include <sys/epoll.h>
26#include <sys/timerfd.h>
27#endif /* COAP_EPOLL_SUPPORT */
28#include <errno.h>
29
30void
32 if (value.integer_part > 0 && value.fractional_part < 1000) {
33 session->ack_timeout = value;
34 coap_log(LOG_DEBUG, "***%s: session ack_timeout set to %u.%03u\n",
35 coap_session_str(session), session->ack_timeout.integer_part,
37 }
38}
39
40void
42 coap_fixed_point_t value) {
43 if (value.integer_part > 0 && value.fractional_part < 1000) {
44 session->ack_random_factor = value;
45 coap_log(LOG_DEBUG, "***%s: session ack_random_factor set to %u.%03u\n",
48 }
49}
50
51void
53 if (value > 0) {
54 session->max_retransmit = value;
55 coap_log(LOG_DEBUG, "***%s: session max_retransmit set to %u\n",
56 coap_session_str(session), session->max_retransmit);
57 }
58}
59
60void
61coap_session_set_nstart(coap_session_t *session, uint16_t value) {
62 if (value > 0) {
63 session->nstart = value;
64 coap_log(LOG_DEBUG, "***%s: session nstart set to %u\n",
65 coap_session_str(session), session->nstart);
66 }
67}
68
69void
71 coap_fixed_point_t value) {
72 if (value.integer_part > 0 && value.fractional_part < 1000) {
73 session->default_leisure = value;
74 coap_log(LOG_DEBUG, "***%s: session default_leisure set to %u.%03u\n",
77 }
78}
79
80void
82 if (value > 0) {
83 session->probing_rate = value;
84 coap_log(LOG_DEBUG, "***%s: session probing_rate set to %u\n",
85 coap_session_str(session), session->probing_rate);
86 }
87}
88
91 return session->ack_timeout;
92}
93
96 return session->ack_random_factor;
97}
98
99uint16_t
101 return session->max_retransmit;
102}
103
104uint16_t
106 return session->nstart;
107}
108
111 return session->default_leisure;
112}
113
114uint32_t
116 return session->probing_rate;
117}
118
121 ++session->ref;
122 return session;
123}
124
125void
127 if (session) {
128#ifndef __COVERITY__
129 assert(session->ref > 0);
130 if (session->ref > 0)
131 --session->ref;
132 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
133 coap_session_free(session);
134#else /* __COVERITY__ */
135 /* Coverity scan is fooled by the reference counter leading to
136 * false positives for USE_AFTER_FREE. */
137 --session->ref;
138 __coverity_negative_sink__(session->ref);
139 /* Indicate that resources are released properly. */
140 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
141 __coverity_free__(session);
142 }
143#endif /* __COVERITY__ */
144 }
145}
146
147void
148coap_session_set_app_data(coap_session_t *session, void *app_data) {
149 assert(session);
150 session->app = app_data;
151}
152
153void *
155 assert(session);
156 return session->app;
157}
158
159static coap_session_t *
161 const coap_addr_hash_t *addr_hash,
162 const coap_address_t *local_addr,
163 const coap_address_t *remote_addr, int ifindex,
164 coap_context_t *context, coap_endpoint_t *endpoint) {
166 sizeof(coap_session_t));
167#if ! COAP_SERVER_SUPPORT
168 (void)endpoint;
169#endif /* ! COAP_SERVER_SUPPORT */
170 if (!session)
171 return NULL;
172 memset(session, 0, sizeof(*session));
173 session->proto = proto;
174 session->type = type;
175 if (addr_hash)
176 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
177 else
178 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
179 if (local_addr)
180 coap_address_copy(&session->addr_info.local, local_addr);
181 else
183 if (remote_addr)
184 coap_address_copy(&session->addr_info.remote, remote_addr);
185 else
187 session->ifindex = ifindex;
188 session->context = context;
189#if COAP_SERVER_SUPPORT
190 session->endpoint = endpoint;
191 if (endpoint)
192 session->mtu = endpoint->default_mtu;
193 else
194#endif /* COAP_SERVER_SUPPORT */
195 session->mtu = COAP_DEFAULT_MTU;
196 session->block_mode = context->block_mode;
197 if (proto == COAP_PROTO_DTLS) {
198 session->tls_overhead = 29;
199 if (session->tls_overhead >= session->mtu) {
200 session->tls_overhead = session->mtu;
201 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
202 }
203 }
207 session->nstart = COAP_DEFAULT_NSTART;
210 session->dtls_event = -1;
214
215 /* Randomly initialize */
216 coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
217 coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
218
219 return session;
220}
221
223 coap_queue_t *q, *tmp;
224 coap_lg_xmit_t *lq, *ltmp;
225
226#if COAP_CLIENT_SUPPORT
227 coap_lg_crcv_t *cq, *etmp;
228
229 /* Need to do this before (D)TLS and socket is closed down */
230 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
231 if (cq->observe_set) {
232 /* Need to close down observe */
234 /* Need to delete node we set up for NON */
235 coap_queue_t *queue = session->context->sendqueue;
236
237 while (queue) {
238 if (queue->session == session) {
239 coap_delete_node(queue);
240 break;
241 }
242 queue = queue->next;
243 }
244 }
245 }
246 LL_DELETE(session->lg_crcv, cq);
247 coap_block_delete_lg_crcv(session, cq);
248 }
249#endif /* COAP_CLIENT_SUPPORT */
250
251 if (session->partial_pdu)
253 if (session->proto == COAP_PROTO_DTLS)
254 coap_dtls_free_session(session);
255#if !COAP_DISABLE_TCP
256 else if (session->proto == COAP_PROTO_TLS)
257 coap_tls_free_session(session);
258#endif /* !COAP_DISABLE_TCP */
259 if (session->sock.flags != COAP_SOCKET_EMPTY)
260 coap_socket_close(&session->sock);
261 if (session->psk_identity)
262 coap_free(session->psk_identity);
263 if (session->psk_key)
264 coap_free(session->psk_key);
265 if (session->psk_hint)
266 coap_free(session->psk_hint);
267
268#if COAP_SERVER_SUPPORT
269 coap_cache_entry_t *cp, *ctmp;
270 HASH_ITER(hh, session->context->cache, cp, ctmp) {
271 /* cp->session is NULL if not session based */
272 if (cp->session == session) {
273 coap_delete_cache_entry(session->context, cp);
274 }
275 }
276#endif /* COAP_SERVER_SUPPORT */
277 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
278 if (q->pdu->type==COAP_MESSAGE_CON && session->context && session->context->nack_handler)
281 }
282 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
283 LL_DELETE(session->lg_xmit, lq);
284 coap_block_delete_lg_xmit(session, lq);
285 }
286#if COAP_SERVER_SUPPORT
287 coap_lg_srcv_t *sq, *stmp;
288
289 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
290 LL_DELETE(session->lg_srcv, sq);
291 coap_block_delete_lg_srcv(session, sq);
292 }
293#endif /* COAP_SERVER_SUPPORT */
294}
295
297 if (!session)
298 return;
299 assert(session->ref == 0);
300 if (session->ref)
301 return;
302 coap_session_mfree(session);
303#if COAP_SERVER_SUPPORT
304 if (session->endpoint) {
305 if (session->endpoint->sessions)
306 SESSIONS_DELETE(session->endpoint->sessions, session);
307 } else
308#endif /* COAP_SERVER_SUPPORT */
309#if COAP_CLIENT_SUPPORT
310 if (session->context) {
311 if (session->context->sessions)
312 SESSIONS_DELETE(session->context->sessions, session);
313 }
314#endif /* COAP_CLIENT_SUPPORT */
316 coap_log(LOG_DEBUG, "***%s: session %p: closed\n", coap_session_str(session),
317 (void *)session);
318
320}
321
322static size_t
324 size_t max_with_header) {
325#if COAP_DISABLE_TCP
326 return max_with_header > 4 ? max_with_header - 4 : 0;
327#else /* !COAP_DISABLE_TCP */
328 if (COAP_PROTO_NOT_RELIABLE(session->proto))
329 return max_with_header > 4 ? max_with_header - 4 : 0;
330 /* we must assume there is no token to be on the safe side */
331 if (max_with_header <= 2)
332 return 0;
333 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
334 return max_with_header - 2;
335 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
336 return max_with_header - 3;
337 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
338 return max_with_header - 4;
339 else
340 return max_with_header - 6;
341#endif /* !COAP_DISABLE_TCP */
342}
343
344size_t
346 if (session->csm_rcv_mtu)
348 (size_t)(session->csm_rcv_mtu));
349
351 (size_t)(session->mtu - session->tls_overhead));
352}
353
354size_t
356 size_t max_with_header;
357
358#if COAP_CLIENT_SUPPORT
359 /*
360 * Delay if session->doing_first is set.
361 * E.g. Reliable and CSM not in yet for checking block support
362 */
363 coap_session_t *session_rw;
364
365 /*
366 * Need to do this to not get a compiler warning about const parameters
367 * but need to maintain source code backward compatibility
368 */
369 memcpy(&session_rw, &session, sizeof(session_rw));
370 if (coap_client_delay_first(session_rw) == 0) {
371 coap_log(LOG_DEBUG, "coap_client_delay_first: timeout\n");
372 /* Have to go with the defaults */
373 }
374#endif /* COAP_CLIENT_SUPPORT */
375
376 max_with_header = (size_t)(session->mtu - session->tls_overhead);
377
378 return coap_session_max_pdu_size_internal(session, max_with_header);
379}
380
381void coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
382#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
383 if (mtu > COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
385#endif
386 if (mtu < 64)
387 mtu = 64;
388 session->mtu = mtu;
389 if (session->tls_overhead >= session->mtu) {
390 session->tls_overhead = session->mtu;
391 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
392 }
393}
394
395ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen) {
396 ssize_t bytes_written;
397
398 coap_socket_t *sock = &session->sock;
399#if COAP_SERVER_SUPPORT
400 if (sock->flags == COAP_SOCKET_EMPTY) {
401 assert(session->endpoint != NULL);
402 sock = &session->endpoint->sock;
403 }
404#endif /* COAP_SERVER_SUPPORT */
405
406 bytes_written = coap_socket_send(sock, session, data, datalen);
407 if (bytes_written == (ssize_t)datalen) {
408 coap_ticks(&session->last_rx_tx);
409 coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
410 coap_session_str(session), datalen);
411 } else {
412 coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
413 coap_session_str(session), datalen);
414 }
415 return bytes_written;
416}
417
418ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen) {
419 ssize_t bytes_written = coap_socket_write(&session->sock, data, datalen);
420 if (bytes_written > 0) {
421 coap_ticks(&session->last_rx_tx);
422 coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
423 coap_session_str(session), bytes_written);
424 } else if (bytes_written < 0) {
425 coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
426 coap_session_str(session), datalen );
427 }
428 return bytes_written;
429}
430
431ssize_t
433 coap_queue_t *node)
434{
435 if ( node ) {
436 coap_queue_t *removed = NULL;
437 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
438 assert(removed == node);
440 node->session = NULL;
441 node->t = 0;
442 } else {
443 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
444 coap_queue_t *q = NULL;
445 /* Check same mid is not getting re-used in violation of RFC7252 */
446 LL_FOREACH(session->delayqueue, q) {
447 if (q->id == pdu->mid) {
448 coap_log(LOG_ERR, "** %s: mid=0x%x: already in-use - dropped\n",
449 coap_session_str(session), pdu->mid);
450 return COAP_INVALID_MID;
451 }
452 }
453 }
454 node = coap_new_node();
455 if (node == NULL)
456 return COAP_INVALID_MID;
457 node->id = pdu->mid;
458 node->pdu = pdu;
459 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
460 uint8_t r;
461 coap_prng(&r, sizeof(r));
462 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
463 node->timeout = coap_calc_timeout(session, r);
464 }
465 }
466 LL_APPEND(session->delayqueue, node);
467 coap_log(LOG_DEBUG, "** %s: mid=0x%x: delayed\n",
468 coap_session_str(session), node->id);
469 return COAP_PDU_DELAYED;
470}
471
472#if !COAP_DISABLE_TCP
474 coap_pdu_t *pdu;
475 uint8_t buf[4];
476 assert(COAP_PROTO_RELIABLE(session->proto));
477 coap_log(LOG_DEBUG, "***%s: sending CSM\n", coap_session_str(session));
478 session->state = COAP_SESSION_STATE_CSM;
479 session->partial_write = 0;
480 if (session->mtu == 0)
481 session->mtu = COAP_DEFAULT_MTU; /* base value */
483 if ( pdu == NULL
485 coap_encode_var_safe(buf, sizeof(buf),
486 session->context->csm_max_message_size), buf) == 0
488 coap_encode_var_safe(buf, sizeof(buf),
489 0), buf) == 0
490 || coap_pdu_encode_header(pdu, session->proto) == 0
491 ) {
493 } else {
494 ssize_t bytes_written = coap_session_send_pdu(session, pdu);
495 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
497 } else {
498 session->csm_rcv_mtu = session->context->csm_max_message_size;
499 if (session->csm_rcv_mtu > COAP_BERT_BASE)
500 session->csm_bert_loc_support = 1;
501 else
502 session->csm_bert_loc_support = 0;
503 }
504 }
505 if (pdu)
506 coap_delete_pdu(pdu);
507}
508#endif /* !COAP_DISABLE_TCP */
509
511 coap_pdu_t *ping = NULL;
513 return COAP_INVALID_MID;
514 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
515 uint16_t mid = coap_new_message_id (session);
516 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
517 }
518#if !COAP_DISABLE_TCP
519 else {
521 }
522#endif /* !COAP_DISABLE_TCP */
523 if (!ping)
524 return COAP_INVALID_MID;
525 return coap_send_internal(session, ping);
526}
527
529 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
530 coap_log(LOG_DEBUG, "***%s: session connected\n",
531 coap_session_str(session));
532 if (session->state == COAP_SESSION_STATE_CSM)
534 if (session->doing_first)
535 session->doing_first = 0;
536 }
537
539 session->partial_write = 0;
540
541 if ( session->proto==COAP_PROTO_DTLS) {
542 session->tls_overhead = coap_dtls_get_overhead(session);
543 if (session->tls_overhead >= session->mtu) {
544 session->tls_overhead = session->mtu;
545 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
546 }
547 }
548
549 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
550 ssize_t bytes_written;
551 coap_queue_t *q = session->delayqueue;
552 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
553 if (session->con_active >= COAP_NSTART(session))
554 break;
555 session->con_active++;
556 }
557 /* Take entry off the queue */
558 session->delayqueue = q->next;
559 q->next = NULL;
560
561 coap_log(LOG_DEBUG, "** %s: mid=0x%x: transmitted after delay\n",
562 coap_session_str(session), (int)q->pdu->mid);
563 bytes_written = coap_session_send_pdu(session, q->pdu);
564 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
565 if (coap_wait_ack(session->context, session, q) >= 0)
566 q = NULL;
567 }
568 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
569 if (q)
571 if (bytes_written < 0)
572 break;
573 } else {
574 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
575 q->next = session->delayqueue;
576 session->delayqueue = q;
577 if (bytes_written > 0)
578 session->partial_write = (size_t)bytes_written;
579 break;
580 } else {
582 }
583 }
584 }
585}
586
588#if !COAP_DISABLE_TCP
589 coap_session_state_t state = session->state;
590#endif /* !COAP_DISABLE_TCP */
591
592 coap_log(LOG_DEBUG, "***%s: session disconnected (reason %d)\n",
593 coap_session_str(session), reason);
594#if COAP_SERVER_SUPPORT
595 coap_delete_observers( session->context, session );
596#endif /* COAP_SERVER_SUPPORT */
597
598 if ( session->tls) {
599 if (session->proto == COAP_PROTO_DTLS)
600 coap_dtls_free_session(session);
601#if !COAP_DISABLE_TCP
602 else if (session->proto == COAP_PROTO_TLS)
603 coap_tls_free_session(session);
604#endif /* !COAP_DISABLE_TCP */
605 session->tls = NULL;
606 }
607
608 if (session->proto == COAP_PROTO_UDP)
610 else
612
613 session->con_active = 0;
614
615 if (session->partial_pdu) {
617 session->partial_pdu = NULL;
618 }
619 session->partial_read = 0;
620
621 while (session->delayqueue) {
622 coap_queue_t *q = session->delayqueue;
623 session->delayqueue = q->next;
624 q->next = NULL;
625 coap_log(LOG_DEBUG, "** %s: mid=0x%x: not transmitted after disconnect\n",
626 coap_session_str(session), q->id);
627 if (q->pdu->type==COAP_MESSAGE_CON
628 && COAP_PROTO_NOT_RELIABLE(session->proto)
629 && reason == COAP_NACK_ICMP_ISSUE)
630 {
631 /* Make sure that we try a re-transmit later on ICMP error */
632 if (coap_wait_ack(session->context, session, q) >= 0) {
633 if (session->context->nack_handler) {
634 session->context->nack_handler(session, q->pdu, reason, q->id);
635 }
636 q = NULL;
637 }
638 }
639 if (q && q->pdu->type == COAP_MESSAGE_CON
640 && session->context->nack_handler)
641 {
642 session->context->nack_handler(session, q->pdu, reason, q->id);
643 }
644 if (q)
646 }
647 if (reason != COAP_NACK_ICMP_ISSUE) {
648 coap_cancel_session_messages(session->context, session, reason);
649 }
650 else if (session->context->nack_handler) {
651 coap_queue_t *q = session->context->sendqueue;
652 while (q) {
653 if (q->session == session) {
654 session->context->nack_handler(session, q->pdu, reason, q->id);
655 }
656 q = q->next;
657 }
658 }
659
660#if !COAP_DISABLE_TCP
661 if (COAP_PROTO_RELIABLE(session->proto)) {
662 if (session->sock.flags != COAP_SOCKET_EMPTY) {
663 coap_socket_close(&session->sock);
664 coap_handle_event(session->context,
667 }
668 if (state != COAP_SESSION_STATE_NONE) {
669 coap_handle_event(session->context,
672 }
673 if (session->doing_first)
674 session->doing_first = 0;
675 }
676#endif /* !COAP_DISABLE_TCP */
677}
678
679#if COAP_SERVER_SUPPORT
680static void
681coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
682 const coap_addr_tuple_t *addr_info) {
683 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
684 coap_address_copy(&addr_hash->remote, &addr_info->remote);
685 addr_hash->lport = coap_address_get_port(&addr_info->local);
686 addr_hash->proto = proto;
687}
688
691 const coap_packet_t *packet, coap_tick_t now) {
692 coap_session_t *session;
693 coap_session_t *rtmp;
694 unsigned int num_idle = 0;
695 unsigned int num_hs = 0;
696 coap_session_t *oldest = NULL;
697 coap_session_t *oldest_hs = NULL;
698 coap_addr_hash_t addr_hash;
699
700 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
701 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
702 if (session) {
703 /* Maybe mcast or unicast IP address which is not in the hash */
704 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
705 session->ifindex = packet->ifindex;
706 session->last_rx_tx = now;
707 return session;
708 }
709
710 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
711 if (session->ref == 0 && session->delayqueue == NULL) {
712 if (session->type == COAP_SESSION_TYPE_SERVER) {
713 ++num_idle;
714 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
715 oldest = session;
716
717 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
718 ++num_hs;
719 /* See if this is a partial (D)TLS session set up
720 which needs to be cleared down to prevent DOS */
721 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
722 if (oldest_hs == NULL ||
723 session->last_rx_tx < oldest_hs->last_rx_tx)
724 oldest_hs = session;
725 }
726 }
727 }
728 else if (session->type == COAP_SESSION_TYPE_HELLO) {
729 ++num_hs;
730 /* See if this is a partial (D)TLS session set up for Client Hello
731 which needs to be cleared down to prevent DOS */
732 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
733 if (oldest_hs == NULL ||
734 session->last_rx_tx < oldest_hs->last_rx_tx)
735 oldest_hs = session;
736 }
737 }
738 }
739 }
740
741 if (endpoint->context->max_idle_sessions > 0 &&
742 num_idle >= endpoint->context->max_idle_sessions) {
744 coap_session_free(oldest);
745 }
746 else if (oldest_hs) {
747 coap_log(LOG_WARNING, "***%s: Incomplete session timed out\n",
748 coap_session_str(oldest_hs));
750 coap_session_free(oldest_hs);
751 }
752
753 if (num_hs > (endpoint->context->max_handshake_sessions ?
756 /* Maxed out on number of sessions in (D)TLS negotiation state */
758 "Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
759 "large. New request ignored\n");
760 return NULL;
761 }
762
763 if (endpoint->proto == COAP_PROTO_DTLS) {
764 /*
765 * Need to check that this actually is a Client Hello before wasting
766 * time allocating and then freeing off session.
767 */
768
769 /*
770 * Generic header structure of the DTLS record layer.
771 * typedef struct __attribute__((__packed__)) {
772 * uint8_t content_type; content type of the included message
773 * uint16_t version; Protocol version
774 * uint16_t epoch; counter for cipher state changes
775 * uint8_t sequence_number[6]; sequence number
776 * uint16_t length; length of the following fragment
777 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
778 * } dtls_record_handshake_t;
779 */
780#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
781#define DTLS_CT_ALERT 21 /* Content Type Alert */
782#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
783#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
784#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
785
786#ifdef WITH_LWIP
787 const uint8_t *payload = (const uint8_t*)packet->pbuf->payload;
788 size_t length = packet->pbuf->len;
789#else /* ! WITH_LWIP */
790 const uint8_t *payload = (const uint8_t*)packet->payload;
791 size_t length = packet->length;
792#endif /* ! WITH_LWIP */
793 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
795 "coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
796 payload[OFF_CONTENT_TYPE], length,
797 OFF_HANDSHAKE_TYPE + 1);
798 return NULL;
799 }
800 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
801 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
802 /* only log if not a late alert */
803 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
805 "coap_dtls_hello: ContentType %d Handshake %d dropped\n",
806 payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
807 return NULL;
808 }
809 }
810
812 &addr_hash, &packet->addr_info.local,
813 &packet->addr_info.remote,
814 packet->ifindex, endpoint->context, endpoint);
815 if (session) {
816 session->last_rx_tx = now;
817 if (endpoint->proto == COAP_PROTO_UDP)
819 else if (endpoint->proto == COAP_PROTO_DTLS) {
820 session->type = COAP_SESSION_TYPE_HELLO;
821 }
822 SESSIONS_ADD(endpoint->sessions, session);
823 coap_log(LOG_DEBUG, "***%s: session %p: new incoming session\n",
824 coap_session_str(session), (void *)session);
826 }
827 return session;
828}
829
832 coap_tick_t now) {
833 if (session) {
834 session->last_rx_tx = now;
836 session->tls = coap_dtls_new_server_session(session);
837 if (session->tls) {
839 } else {
840 coap_session_free(session);
841 session = NULL;
842 }
843 }
844 return session;
845}
846#endif /* COAP_SERVER_SUPPORT */
847
848#ifdef COAP_EPOLL_SUPPORT
849static void
850coap_epoll_ctl_add(coap_socket_t *sock,
851 uint32_t events,
852 const char *func
853) {
854 int ret;
855 struct epoll_event event;
856 coap_context_t *context;
857
858 if (sock == NULL)
859 return;
860
861#if COAP_SERVER_SUPPORT
862 context = sock->session ? sock->session->context :
863 sock->endpoint ? sock->endpoint->context : NULL;
864#else /* ! COAP_SERVER_SUPPORT */
865 context = sock->session ? sock->session->context : NULL;
866#endif /* ! COAP_SERVER_SUPPORT */
867 if (context == NULL)
868 return;
869
870 /* Needed if running 32bit as ptr is only 32bit */
871 memset(&event, 0, sizeof(event));
872 event.events = events;
873 event.data.ptr = sock;
874
875 ret = epoll_ctl(context->epfd, EPOLL_CTL_ADD, sock->fd, &event);
876 if (ret == -1) {
878 "%s: epoll_ctl ADD failed: %s (%d)\n",
879 func,
880 coap_socket_strerror(), errno);
881 }
882}
883#endif /* COAP_EPOLL_SUPPORT */
884
885#if COAP_CLIENT_SUPPORT
886static coap_session_t *
887coap_session_create_client(
888 coap_context_t *ctx,
889 const coap_address_t *local_if,
890 const coap_address_t *server,
891 coap_proto_t proto
892) {
893 coap_session_t *session = NULL;
894
895 assert(server);
896
897 switch(proto) {
898 case COAP_PROTO_UDP:
899 break;
900 case COAP_PROTO_DTLS:
901 if (!coap_dtls_is_supported()) {
902 coap_log(LOG_CRIT, "coap_new_client_session*: DTLS not supported\n");
903 return NULL;
904 }
905 break;
906 case COAP_PROTO_TCP:
907 if (!coap_tcp_is_supported()) {
908 coap_log(LOG_CRIT, "coap_new_client_session*: TCP not supported\n");
909 return NULL;
910 }
911 break;
912 case COAP_PROTO_TLS:
913 if (!coap_tls_is_supported()) {
914 coap_log(LOG_CRIT, "coap_new_client_session*: TLS not supported\n");
915 return NULL;
916 }
917 break;
918 case COAP_PROTO_NONE:
919 default:
920 assert(0);
921 break;
922 }
923 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
924 local_if, server, 0, ctx, NULL);
925 if (!session)
926 goto error;
927
928 coap_session_reference(session);
929
930 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
931 coap_session_t *s, *rtmp;
932 if (!coap_socket_connect_udp(&session->sock, local_if, server,
934 &session->addr_info.local, &session->addr_info.remote)) {
935 goto error;
936 }
937 /* Check that this is not a duplicate 4-tuple */
938 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
939 if ((s->proto == COAP_PROTO_UDP || s->proto == COAP_PROTO_DTLS) &&
941 &s->addr_info.local) &&
943 &s->addr_info.remote)) {
944 coap_log(LOG_WARNING, "***%s: session %p: duplicate - already exists\n",
945 coap_session_str(session), (void *)session);
946 goto error;
947 }
948 }
949#if !COAP_DISABLE_TCP
950 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
951 if (!coap_socket_connect_tcp1(&session->sock, local_if, server,
953 &session->addr_info.local, &session->addr_info.remote)) {
954 goto error;
955 }
956#endif /* !COAP_DISABLE_TCP */
957 }
958
959#ifdef COAP_EPOLL_SUPPORT
960 session->sock.session = session;
961 coap_epoll_ctl_add(&session->sock,
962 EPOLLIN |
963 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
964 EPOLLOUT : 0),
965 __func__);
966#endif /* COAP_EPOLL_SUPPORT */
967
969 if (local_if)
970 session->sock.flags |= COAP_SOCKET_BOUND;
971#if COAP_SERVER_SUPPORT
972 if (ctx->proxy_uri_resource)
973 session->proxy_session = 1;
974#endif /* COAP_SERVER_SUPPORT */
975 SESSIONS_ADD(ctx->sessions, session);
976 return session;
977
978error:
979 /*
980 * Need to add in the session as coap_session_release()
981 * will call SESSIONS_DELETE in coap_session_free().
982 */
983 if (session)
984 SESSIONS_ADD(ctx->sessions, session);
985 coap_session_release(session);
986 return NULL;
987}
988
989static coap_session_t *
990coap_session_connect(coap_session_t *session) {
991 if (session->proto == COAP_PROTO_UDP) {
993 } else if (session->proto == COAP_PROTO_DTLS) {
994 session->tls = coap_dtls_new_client_session(session);
995 if (session->tls) {
997 } else {
998 /* Need to free session object. As a new session may not yet
999 * have been referenced, we call coap_session_reference() first
1000 * before trying to release the object.
1001 */
1002 coap_session_reference(session);
1003 coap_session_release(session);
1004 return NULL;
1005 }
1006#if !COAP_DISABLE_TCP
1007 } else {
1008 if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) {
1009 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1011 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1012 session->state != COAP_SESSION_STATE_NONE &&
1013 COAP_PROTO_RELIABLE(session->proto) &&
1014 session->type == COAP_SESSION_TYPE_CLIENT) {
1015 session->doing_first = 1;
1016 }
1017 } else if (session->proto == COAP_PROTO_TLS) {
1018 int connected = 0;
1019 session->tls = coap_tls_new_client_session(session, &connected);
1020 if (session->tls) {
1022 if (connected)
1023 coap_session_send_csm(session);
1024 } else {
1025 /* Need to free session object. As a new session may not yet
1026 * have been referenced, we call coap_session_reference()
1027 * first before trying to release the object.
1028 */
1029 coap_session_reference(session);
1030 coap_session_release(session);
1031 return NULL;
1032 }
1033 } else {
1034 coap_session_send_csm(session);
1035 }
1036 }
1037#endif /* !COAP_DISABLE_TCP */
1038 }
1039 coap_ticks(&session->last_rx_tx);
1040 return session;
1041}
1042#endif /* COAP_CLIENT_SUPPORT */
1043
1044#if COAP_SERVER_SUPPORT
1045static coap_session_t *
1046coap_session_accept(coap_session_t *session) {
1047#if !COAP_DISABLE_TCP
1048 if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS)
1050 if (session->proto == COAP_PROTO_TCP) {
1051 coap_session_send_csm(session);
1052 } else if (session->proto == COAP_PROTO_TLS) {
1053 int connected = 0;
1054 session->tls = coap_tls_new_server_session(session, &connected);
1055 if (session->tls) {
1057 if (connected) {
1059 coap_session_send_csm(session);
1060 }
1061 } else {
1062 /* Need to free session object. As a new session may not yet
1063 * have been referenced, we call coap_session_reference() first
1064 * before trying to release the object.
1065 */
1066 coap_session_reference(session);
1067 coap_session_release(session);
1068 session = NULL;
1069 }
1070 }
1071#endif /* COAP_DISABLE_TCP */
1072 return session;
1073}
1074#endif /* COAP_SERVER_SUPPORT */
1075
1076#if COAP_CLIENT_SUPPORT
1078 coap_context_t *ctx,
1079 const coap_address_t *local_if,
1080 const coap_address_t *server,
1081 coap_proto_t proto
1082) {
1083 coap_session_t *session = coap_session_create_client(ctx, local_if, server,
1084 proto);
1085 if (session) {
1086 coap_log(LOG_DEBUG, "***%s: session %p: created outgoing session\n",
1087 coap_session_str(session), (void *)session);
1088 session = coap_session_connect(session);
1089 }
1090 return session;
1091}
1092
1094 coap_context_t *ctx,
1095 const coap_address_t *local_if,
1096 const coap_address_t *server,
1097 coap_proto_t proto,
1098 const char *identity,
1099 const uint8_t *key,
1100 unsigned key_len
1101) {
1102 coap_dtls_cpsk_t setup_data;
1103
1104 memset (&setup_data, 0, sizeof(setup_data));
1106
1107 if (identity) {
1108 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1109 setup_data.psk_info.identity.length = strlen(identity);
1110 }
1111
1112 if (key && key_len > 0) {
1113 setup_data.psk_info.key.s = key;
1114 setup_data.psk_info.key.length = key_len;
1115 }
1116
1117 return coap_new_client_session_psk2(ctx, local_if, server,
1118 proto, &setup_data);
1119}
1120
1122 coap_context_t *ctx,
1123 const coap_address_t *local_if,
1124 const coap_address_t *server,
1125 coap_proto_t proto,
1126 coap_dtls_cpsk_t *setup_data
1127) {
1128 coap_session_t *session = coap_session_create_client(ctx, local_if,
1129 server, proto);
1130
1131 if (!session)
1132 return NULL;
1133
1134 session->cpsk_setup_data = *setup_data;
1135 if (setup_data->psk_info.identity.s) {
1136 session->psk_identity =
1138 setup_data->psk_info.identity.length);
1139 if (!session->psk_identity) {
1140 coap_log(LOG_WARNING, "Cannot store session Identity (PSK)\n");
1141 coap_session_release(session);
1142 return NULL;
1143 }
1144 }
1146 coap_log(LOG_WARNING, "Identity (PSK) not defined\n");
1147 coap_session_release(session);
1148 return NULL;
1149 }
1150
1151 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1152 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1153 setup_data->psk_info.key.length);
1154 if (!session->psk_key) {
1155 coap_log(LOG_WARNING, "Cannot store session pre-shared key (PSK)\n");
1156 coap_session_release(session);
1157 return NULL;
1158 }
1159 }
1161 coap_log(LOG_WARNING, "Pre-shared key (PSK) not defined\n");
1162 coap_session_release(session);
1163 return NULL;
1164 }
1165
1167 if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
1168 coap_session_release(session);
1169 return NULL;
1170 }
1171 }
1172 coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
1173 coap_session_str(session));
1174 return coap_session_connect(session);
1175}
1176#endif /* ! COAP_CLIENT_SUPPORT */
1177
1178int
1180 const coap_bin_const_t *psk_hint
1181) {
1182 /* We may be refreshing the hint with the same hint */
1183 coap_bin_const_t *old_psk_hint = session->psk_hint;
1184
1185 if (psk_hint && psk_hint->s) {
1186 if (session->psk_hint) {
1187 if (coap_binary_equal(session->psk_hint, psk_hint))
1188 return 1;
1189 }
1190 session->psk_hint = coap_new_bin_const(psk_hint->s,
1191 psk_hint->length);
1192 if (!session->psk_hint) {
1193 coap_log(LOG_ERR, "No memory to store identity hint (PSK)\n");
1194 if (old_psk_hint)
1195 coap_delete_bin_const(old_psk_hint);
1196 return 0;
1197 }
1198 }
1199 else {
1200 session->psk_hint = NULL;
1201 }
1202 if (old_psk_hint)
1203 coap_delete_bin_const(old_psk_hint);
1204
1205 return 1;
1206}
1207
1208int
1210 const coap_bin_const_t *psk_key
1211) {
1212 /* We may be refreshing the key with the same key */
1213 coap_bin_const_t *old_psk_key = session->psk_key;
1214
1215 if (psk_key && psk_key->s) {
1216 if (session->psk_key) {
1217 if (coap_binary_equal(session->psk_key, psk_key))
1218 return 1;
1219 }
1220 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1221 if (!session->psk_key) {
1222 coap_log(LOG_ERR, "No memory to store pre-shared key (PSK)\n");
1223 if (old_psk_key)
1224 coap_delete_bin_const(old_psk_key);
1225 return 0;
1226 }
1227 }
1228 else {
1229 session->psk_key = NULL;
1230 }
1231 if (old_psk_key)
1232 coap_delete_bin_const(old_psk_key);
1233
1234 return 1;
1235}
1236
1237int
1239 const coap_bin_const_t *psk_identity
1240) {
1241 /* We may be refreshing the identity with the same identity */
1242 coap_bin_const_t *old_psk_identity = session->psk_identity;
1243
1244 if (psk_identity && psk_identity->s) {
1245 if (session->psk_identity) {
1246 if (coap_binary_equal(session->psk_identity, psk_identity))
1247 return 1;
1248 }
1249 session->psk_identity = coap_new_bin_const(psk_identity->s,
1250 psk_identity->length);
1251 if (!session->psk_identity) {
1252 coap_log(LOG_ERR, "No memory to store pre-shared key identity (PSK)\n");
1253 if (old_psk_identity)
1254 coap_delete_bin_const(old_psk_identity);
1255 return 0;
1256 }
1257 }
1258 else {
1259 session->psk_identity = NULL;
1260 }
1261 if (old_psk_identity)
1262 coap_delete_bin_const(old_psk_identity);
1263
1264 return 1;
1265}
1266
1267#if COAP_SERVER_SUPPORT
1268const coap_bin_const_t *
1270 if (session)
1271 return session->psk_hint;
1272 return NULL;
1273}
1274#endif /* COAP_SERVER_SUPPORT */
1275
1276const coap_bin_const_t *
1278 const coap_bin_const_t *psk_identity = NULL;
1279 if (session) {
1280 psk_identity = session->psk_identity;
1281 if (psk_identity == NULL) {
1282 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1283 }
1284 }
1285 return psk_identity;
1286}
1287
1288const coap_bin_const_t *
1290 if (session)
1291 return session->psk_key;
1292 return NULL;
1293}
1294
1295#if COAP_CLIENT_SUPPORT
1297 coap_context_t *ctx,
1298 const coap_address_t *local_if,
1299 const coap_address_t *server,
1300 coap_proto_t proto,
1301 coap_dtls_pki_t* setup_data
1302) {
1303 coap_session_t *session;
1304
1306 if (!setup_data) {
1307 return NULL;
1308 } else {
1309 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1311 "coap_new_client_session_pki: Wrong version of setup_data\n");
1312 return NULL;
1313 }
1314 }
1315
1316 }
1317 session = coap_session_create_client(ctx, local_if, server, proto);
1318
1319 if (!session) {
1320 return NULL;
1321 }
1322
1324 /* we know that setup_data is not NULL */
1325 if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1326 coap_session_release(session);
1327 return NULL;
1328 }
1329 }
1330 coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
1331 coap_session_str(session));
1332 return coap_session_connect(session);
1333}
1334#endif /* ! COAP_CLIENT_SUPPORT */
1335
1336#if COAP_SERVER_SUPPORT
1338 coap_context_t *ctx,
1339 coap_endpoint_t *ep
1340) {
1341 coap_session_t *session;
1343 NULL, NULL, NULL, 0, ctx, ep );
1344 if (!session)
1345 goto error;
1346
1347#if !COAP_DISABLE_TCP
1348 if (!coap_socket_accept_tcp(&ep->sock, &session->sock,
1349 &session->addr_info.local,
1350 &session->addr_info.remote))
1351 goto error;
1352 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1353
1354#endif /* !COAP_DISABLE_TCP */
1357#ifdef COAP_EPOLL_SUPPORT
1358 session->sock.session = session;
1359 coap_epoll_ctl_add(&session->sock,
1360 EPOLLIN,
1361 __func__);
1362#endif /* COAP_EPOLL_SUPPORT */
1363 SESSIONS_ADD(ep->sessions, session);
1364 if (session) {
1365 coap_log(LOG_DEBUG, "***%s: session %p: new incoming session\n",
1366 coap_session_str(session), (void *)session);
1367 /* Returned session may already have been released and is now NULL */
1368 session = coap_session_accept(session);
1369 if(session) {
1371 }
1372 }
1373 return session;
1374
1375error:
1376 /*
1377 * Need to add in the session as coap_session_release()
1378 * will call SESSIONS_DELETE in coap_session_free().
1379 */
1380 SESSIONS_ADD(ep->sessions, session);
1381 coap_session_free(session);
1382 return NULL;
1383}
1384#endif /* COAP_SERVER_SUPPORT */
1385
1386void
1388 const uint8_t *data) {
1389 session->tx_token = coap_decode_var_bytes8(data, len);
1390}
1391
1392void coap_session_new_token(coap_session_t *session, size_t *len,
1393 uint8_t *data) {
1394 *len = coap_encode_var_safe8(data,
1395 sizeof(session->tx_token), ++session->tx_token);
1396}
1397
1398uint16_t
1400 return ++session->tx_mid;
1401}
1402
1403const coap_address_t *
1405 if (session)
1406 return &session->addr_info.remote;
1407 return NULL;
1408}
1409
1410const coap_address_t *
1412 if (session)
1413 return &session->addr_info.local;
1414 return NULL;
1415}
1416
1419 if (session)
1420 return session->context;
1421 return NULL;
1422}
1423
1426 if (session)
1427 return session->proto;
1428 return 0;
1429}
1430
1433 if (session)
1434 return session->type;
1435 return 0;
1436}
1437
1438#if COAP_CLIENT_SUPPORT
1439int
1441#if COAP_SERVER_SUPPORT
1442 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1443 coap_session_reference(session);
1444 session->type = COAP_SESSION_TYPE_CLIENT;
1445 return 1;
1446 }
1447#else /* ! COAP_SERVER_SUPPORT */
1448 (void)session;
1449#endif /* ! COAP_SERVER_SUPPORT */
1450 return 0;
1451}
1452#endif /* COAP_CLIENT_SUPPORT */
1453
1456 if (session)
1457 return session->state;
1458 return 0;
1459}
1460
1462 if (session)
1463 return session->ifindex;
1464 return -1;
1465}
1466
1468 coap_tls_library_t *tls_lib) {
1469 if (session)
1470 return coap_dtls_get_tls(session, tls_lib);
1471 return NULL;
1472}
1473
1474#ifndef WITH_LWIP
1475#if COAP_SERVER_SUPPORT
1477coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1478 coap_endpoint_t *ep = NULL;
1479
1480 assert(context);
1481 assert(listen_addr);
1482 assert(proto != COAP_PROTO_NONE);
1483
1484 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1485 coap_log(LOG_CRIT, "coap_new_endpoint: DTLS not supported\n");
1486 goto error;
1487 }
1488
1489 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1490 coap_log(LOG_CRIT, "coap_new_endpoint: TLS not supported\n");
1491 goto error;
1492 }
1493
1494 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1495 coap_log(LOG_CRIT, "coap_new_endpoint: TCP not supported\n");
1496 goto error;
1497 }
1498
1499 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS) {
1502 "coap_new_endpoint: one of coap_context_set_psk() or "
1503 "coap_context_set_pki() not called\n");
1504 goto error;
1505 }
1506 }
1507
1508 ep = coap_malloc_endpoint();
1509 if (!ep) {
1510 coap_log(LOG_WARNING, "coap_new_endpoint: malloc");
1511 goto error;
1512 }
1513
1514 memset(ep, 0, sizeof(coap_endpoint_t));
1515 ep->context = context;
1516 ep->proto = proto;
1517
1518 if (proto==COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1519 if (!coap_socket_bind_udp(&ep->sock, listen_addr, &ep->bind_addr))
1520 goto error;
1522#if !COAP_DISABLE_TCP
1523 } else if (proto==COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1524 if (!coap_socket_bind_tcp(&ep->sock, listen_addr, &ep->bind_addr))
1525 goto error;
1527#endif /* !COAP_DISABLE_TCP */
1528 } else {
1529 coap_log(LOG_CRIT, "coap_new_endpoint: protocol not supported\n");
1530 goto error;
1531 }
1532
1533 if (LOG_DEBUG <= coap_get_log_level()) {
1534#ifndef INET6_ADDRSTRLEN
1535#define INET6_ADDRSTRLEN 40
1536#endif
1537 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1538
1539 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1540 coap_log(LOG_DEBUG, "created %s endpoint %s\n",
1541 ep->proto == COAP_PROTO_TLS ? "TLS "
1542 : ep->proto == COAP_PROTO_TCP ? "TCP "
1543 : ep->proto == COAP_PROTO_DTLS ? "DTLS" : "UDP ",
1544 addr_str);
1545 }
1546 }
1547
1549
1551
1552#ifdef COAP_EPOLL_SUPPORT
1553 ep->sock.endpoint = ep;
1554 coap_epoll_ctl_add(&ep->sock,
1555 EPOLLIN,
1556 __func__);
1557#endif /* COAP_EPOLL_SUPPORT */
1558
1559 LL_PREPEND(context->endpoint, ep);
1560 return ep;
1561
1562error:
1564 return NULL;
1565}
1566
1567void coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu) {
1568 ep->default_mtu = (uint16_t)mtu;
1569}
1570
1571void
1573 if (ep) {
1574 coap_session_t *session, *rtmp;
1575
1576 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
1577 assert(session->ref == 0);
1578 if (session->ref == 0) {
1579 coap_session_free(session);
1580 }
1581 }
1582 if (ep->sock.flags != COAP_SOCKET_EMPTY) {
1583 /*
1584 * ep->sock.endpoint is set in coap_new_endpoint().
1585 * ep->sock.session is never set.
1586 *
1587 * session->sock.session is set for both clients and servers (when a
1588 * new session is accepted), but does not affect the endpoint.
1589 *
1590 * So, it is safe to call coap_socket_close() after all the sessions
1591 * have been freed above as we are only working with the endpoint sock.
1592 */
1593#ifdef COAP_EPOLL_SUPPORT
1594 assert(ep->sock.session == NULL);
1595#endif /* COAP_EPOLL_SUPPORT */
1596 coap_socket_close(&ep->sock);
1597 }
1598
1599 if (ep->context && ep->context->endpoint) {
1600 LL_DELETE(ep->context->endpoint, ep);
1601 }
1603 }
1604}
1605#endif /* COAP_SERVER_SUPPORT */
1606#endif /* WITH_LWIP */
1607
1610 const coap_address_t *remote_addr,
1611 int ifindex) {
1612 coap_session_t *s, *rtmp;
1613#if COAP_CLIENT_SUPPORT
1614 SESSIONS_ITER(ctx->sessions, s, rtmp) {
1615 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1616 remote_addr))
1617 return s;
1618 }
1619#endif /* COAP_CLIENT_SUPPORT */
1620#if COAP_SERVER_SUPPORT
1621 coap_endpoint_t *ep;
1622
1623 LL_FOREACH(ctx->endpoint, ep) {
1624 SESSIONS_ITER(ep->sessions, s, rtmp) {
1625 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1626 remote_addr))
1627 return s;
1628 }
1629 }
1630#endif /* COAP_SERVER_SUPPORT */
1631 return NULL;
1632}
1633
1634const char *coap_session_str(const coap_session_t *session) {
1635 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
1636 char *p = szSession, *end = szSession + sizeof(szSession);
1637 if (coap_print_addr(&session->addr_info.local,
1638 (unsigned char*)p, end - p) > 0)
1639 p += strlen(p);
1640 if (p + 6 < end) {
1641 strcpy(p, " <-> ");
1642 p += 5;
1643 }
1644 if (p + 1 < end) {
1645 if (coap_print_addr(&session->addr_info.remote,
1646 (unsigned char*)p, end - p) > 0)
1647 p += strlen(p);
1648 }
1649 if (session->ifindex > 0 && p + 1 < end)
1650 p += snprintf(p, end - p, " (if%d)", session->ifindex);
1651 if (p + 6 < end) {
1652 if (session->proto == COAP_PROTO_UDP) {
1653 strcpy(p, " UDP ");
1654 p += 4;
1655 } else if (session->proto == COAP_PROTO_DTLS) {
1656 strcpy(p, " DTLS");
1657 p += 5;
1658 } else if (session->proto == COAP_PROTO_TCP) {
1659 strcpy(p, " TCP ");
1660 p += 4;
1661 } else if (session->proto == COAP_PROTO_TLS) {
1662 strcpy(p, " TLS ");
1663 p += 4;
1664 } else {
1665 strcpy(p, " NONE");
1666 p += 5;
1667 }
1668 }
1669
1670 return szSession;
1671}
1672
1673#if COAP_SERVER_SUPPORT
1674const char *coap_endpoint_str(const coap_endpoint_t *endpoint) {
1675 static char szEndpoint[128];
1676 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
1677 if (coap_print_addr(&endpoint->bind_addr, (unsigned char*)p, end - p) > 0)
1678 p += strlen(p);
1679 if (p + 6 < end) {
1680 if (endpoint->proto == COAP_PROTO_UDP) {
1681 strcpy(p, " UDP");
1682 p += 4;
1683 } else if (endpoint->proto == COAP_PROTO_DTLS) {
1684 strcpy(p, " DTLS");
1685 p += 5;
1686 } else {
1687 strcpy(p, " NONE");
1688 p += 5;
1689 }
1690 }
1691
1692 return szEndpoint;
1693}
1694#endif /* ! COAP_SERVER_SUPPORT */
1695#endif /* COAP_SESSION_C_ */
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition: coap_address.c:107
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition: coap_address.c:38
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: coap_address.c:65
COAP_STATIC_INLINE void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition: coap_address.h:152
Pulls together all the internal only header files.
void coap_socket_close(coap_socket_t *sock)
Definition: coap_io.c:377
ssize_t coap_socket_send(coap_socket_t *sock, coap_session_t *session, const uint8_t *data, size_t data_len)
Definition: coap_io.c:1600
const char * coap_socket_strerror(void)
Definition: coap_io.c:1594
ssize_t coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len)
Definition: coap_io.c:483
int coap_socket_bind_udp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Definition: coap_io.c:161
coap_nack_reason_t
Definition: coap_io.h:69
@ COAP_NACK_NOT_DELIVERABLE
Definition: coap_io.h:71
@ COAP_NACK_TLS_FAILED
Definition: coap_io.h:73
@ COAP_NACK_ICMP_ISSUE
Definition: coap_io.h:74
int coap_socket_connect_udp(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
#define COAP_SOCKET_WANT_ACCEPT
non blocking server socket is waiting for accept
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
coap_endpoint_t * coap_malloc_endpoint(void)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
void coap_mfree_endpoint(coap_endpoint_t *ep)
#define COAP_SOCKET_CONNECTED
the socket is connected
#define COAP_SOCKET_EMPTY
coap_socket_flags_t values
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:41
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition: coap_notls.c:86
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition: coap_notls.c:181
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition: coap_notls.c:75
void coap_dtls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:127
void coap_tls_free_session(coap_session_t *coap_session COAP_UNUSED)
Definition: coap_notls.c:197
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition: coap_session.c:510
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)
Definition: coap_session.c:160
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
Definition: coap_session.c:323
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
#define SESSIONS_ADD(e, obj)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define SESSIONS_DELETE(e, obj)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define SESSIONS_ITER(e, el, rtmp)
#define SESSIONS_FIND(e, k, res)
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition: coap_tcp.c:38
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition: block.c:1053
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
void coap_delete_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...
coap_fixed_point_t coap_session_get_default_leisure(const coap_session_t *session)
Get the CoAP default leisure time RFC7252 DEFAULT_LEISURE.
Definition: coap_session.c:110
void coap_session_set_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP maximum retransmit count before failure.
Definition: coap_session.c:52
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
Definition: coap_session.c:41
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
Definition: coap_session.c:95
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
Definition: coap_session.h:424
uint16_t coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition: coap_session.c:100
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
Definition: coap_session.h:432
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.
Definition: coap_session.c:115
#define COAP_DEFAULT_ACK_TIMEOUT
Number of seconds when to expect an ACK or a response to an outstanding CON message.
Definition: coap_session.h:415
#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...
Definition: coap_session.h:449
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.
Definition: coap_session.c:31
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:441
void coap_session_set_nstart(coap_session_t *session, uint16_t value)
Set the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:61
#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...
Definition: coap_session.h:457
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.
Definition: coap_session.c:81
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.
Definition: coap_session.c:70
uint16_t coap_session_get_nstart(const coap_session_t *session)
Get the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
Definition: coap_session.c:105
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.
Definition: coap_session.c:90
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:127
int coap_prng(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
Definition: coap_prng.c:105
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: net.c:2072
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:236
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition: net.c:1045
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:1242
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: net.c:956
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition: net.c:982
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:265
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: net.c:2116
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_handle_event(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: net.c:3356
void * coap_dtls_new_client_session(coap_session_t *coap_session)
Create a new client-side session.
void * coap_tls_new_client_session(coap_session_t *coap_session, int *connected)
Create a new TLS client-side session.
void * coap_tls_new_server_session(coap_session_t *coap_session, int *connected)
Create a TLS new server-side session.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
void * coap_dtls_new_server_session(coap_session_t *coap_session)
Create a new DTLS server-side session.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition: coap_notls.c:28
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition: coap_dtls.h:251
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition: coap_notls.c:23
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition: coap_dtls.h:345
coap_tls_library_t
Definition: coap_dtls.h:64
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition: coap_dtls.h:44
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:45
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition: encode.c:65
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition: encode.c:75
@ COAP_EVENT_SESSION_CONNECTED
CSM exchange events for reliable protocols only.
Definition: coap_event.h:53
@ COAP_EVENT_TCP_FAILED
Definition: coap_event.h:48
@ COAP_EVENT_DTLS_CONNECTED
Definition: coap_event.h:39
@ COAP_EVENT_SESSION_FAILED
Definition: coap_event.h:55
@ 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:74
@ COAP_EVENT_SESSION_CLOSED
Definition: coap_event.h:54
@ 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:83
@ COAP_EVENT_TCP_CLOSED
Definition: coap_event.h:47
@ COAP_EVENT_TCP_CONNECTED
TCP events for COAP_PROTO_TCP and COAP_PROTO_TLS.
Definition: coap_event.h:46
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:76
#define LOG_DEBUG
Definition: coap_debug.h:81
#define LOG_ERR
Definition: coap_debug.h:69
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:186
#define LOG_CRIT
Definition: coap_debug.h:66
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 LOG_WARNING
Definition: coap_debug.h:72
#define LOG_INFO
Definition: coap_debug.h:78
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:165
#define COAP_PDU_DELAYED
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_MAX_MESSAGE_SIZE_TCP0
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition: pdu.c:1177
#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: pdu.c:604
#define COAP_DEFAULT_PORT
Definition: pdu.h:37
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:148
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition: pdu.h:243
coap_proto_t
CoAP protocol types.
Definition: pdu.h:292
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition: pdu.h:184
#define COAPS_DEFAULT_PORT
Definition: pdu.h:38
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:93
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition: pdu.h:246
#define COAP_DEFAULT_MTU
Definition: pdu.h:41
#define COAP_BERT_BASE
Definition: pdu.h:44
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition: pdu.h:183
@ COAP_PROTO_DTLS
Definition: pdu.h:295
@ COAP_PROTO_UDP
Definition: pdu.h:294
@ COAP_PROTO_NONE
Definition: pdu.h:293
@ COAP_PROTO_TLS
Definition: pdu.h:297
@ COAP_PROTO_TCP
Definition: pdu.h:296
@ COAP_SIGNALING_CODE_CSM
Definition: pdu.h:342
@ COAP_SIGNALING_CODE_PING
Definition: pdu.h:343
@ COAP_MESSAGE_NON
Definition: pdu.h:62
@ COAP_MESSAGE_CON
Definition: pdu.h:61
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition: coap_session.c:432
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.
Definition: coap_session.c:473
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
Definition: coap_session.c:345
ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for datagram data transmission.
Definition: coap_session.c:395
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
#define COAP_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.
Definition: coap_session.c:528
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition: net.c:770
void coap_session_free(coap_session_t *session)
Definition: coap_session.c:296
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:222
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).
ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen)
Function interface for stream data transmission.
Definition: coap_session.c:418
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:42
void coap_free_endpoint(coap_endpoint_t *ep)
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:381
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address from the session.
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:355
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
Definition: coap_session.h:53
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.
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.
#define COAP_PROTO_NOT_RELIABLE(p)
Definition: coap_session.h:36
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)
Definition: coap_session.h:37
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.
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
Definition: coap_session.c:148
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:126
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).
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address from the session.
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.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
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.
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition: coap_session.c:587
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:120
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
Definition: coap_session.c:154
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
Definition: coap_session.h:46
@ COAP_SESSION_TYPE_SERVER
server-side
Definition: coap_session.h:45
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition: coap_session.h:44
@ COAP_SESSION_STATE_HANDSHAKE
Definition: coap_session.h:56
@ COAP_SESSION_STATE_CSM
Definition: coap_session.h:57
@ COAP_SESSION_STATE_ESTABLISHED
Definition: coap_session.h:58
@ COAP_SESSION_STATE_NONE
Definition: coap_session.h:54
@ COAP_SESSION_STATE_CONNECTING
Definition: coap_session.h:55
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition: str.c:109
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: str.c:100
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition: str.h:203
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
int coap_socket_bind_tcp(coap_socket_t *sock, const coap_address_t *listen_addr, coap_address_t *bound_addr)
Create a new TCP socket and then listen for new incoming TCP sessions.
Definition: coap_tcp.c:199
int coap_socket_connect_tcp1(coap_socket_t *sock, const coap_address_t *local_if, const coap_address_t *server, int default_port, coap_address_t *local_addr, coap_address_t *remote_addr)
Create a new TCP socket and initiate the connection.
Definition: coap_tcp.c:44
int coap_socket_accept_tcp(coap_socket_t *server, coap_socket_t *new_client, coap_address_t *local_addr, coap_address_t *remote_addr)
Accept a new incoming TCP session.
Definition: coap_tcp.c:286
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:110
@ COAP_SESSION
Definition: mem.h:51
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().
#define INET6_ADDRSTRLEN
Definition: net.c:68
Only used for servers for hashing incoming packets.
uint16_t lport
local port
coap_address_t remote
remote address and port
coap_proto_t proto
CoAP protocol.
coap_address_t remote
remote address and port
Definition: coap_io.h:56
coap_address_t local
local address and port
Definition: coap_io.h:57
multi-purpose address abstraction
Definition: coap_address.h:96
CoAP binary data definition with const data.
Definition: str.h:64
size_t length
length of binary data
Definition: str.h:65
const uint8_t * s
read-only binary data
Definition: str.h:66
coap_session_t * session
The CoAP stack's global state is stored in a coap_context_t object.
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
coap_queue_t * sendqueue
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_endpoint_t * endpoint
the endpoints used for listening
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
coap_bin_const_t key
Definition: coap_dtls.h:321
coap_bin_const_t identity
Definition: coap_dtls.h:320
The structure used for defining the Client PSK setup data to be used.
Definition: coap_dtls.h:350
uint8_t version
Definition: coap_dtls.h:351
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition: coap_dtls.h:379
The structure used for defining the PKI setup data to be used.
Definition: coap_dtls.h:256
uint8_t version
Definition: coap_dtls.h:257
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
uint16_t default_mtu
default mtu for this interface
coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition: coap_session.h:30
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition: coap_session.h:32
uint16_t integer_part
Integer part of fixed point variable.
Definition: coap_session.h:31
Structure to hold large body (many blocks) client receive information.
coap_binary_t * app_token
app requesting PDU token
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) server receive information.
Structure to hold large body (many blocks) transmission information.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char payload[COAP_RXBUFFER_SIZE]
payload
int ifindex
the interface index
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t hdr_size
actual size used for protocol-specific header
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_pdu_type_t type
message type
Queue entry.
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_endpoint_t * endpoint
session's endpoint
uint8_t doing_first
Set if doing client's first request.
coap_socket_t sock
socket object for the session, if any
coap_pdu_t * partial_pdu
incomplete incoming pdu
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 relationaship with peer
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
key: remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv)
coap_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)
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 sesison.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
application-specific data
uint32_t tx_rtag
Next Request-Tag number to use.
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
coap_mid_t last_con_mid
The last CON mid that has been been processed.
coap_session_type_t type
client or server side socket
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_session_t * session
coap_endpoint_t * endpoint
coap_socket_flags_t flags