libcoap 4.3.1
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
30#ifdef HAVE_INTTYPES_H
31#include <inttypes.h>
32#else /* ! HAVE_INTTYPES_H */
33#define PRIu32 "u"
34#endif /* ! HAVE_INTTYPES_H */
35
36void
38 if (value.integer_part > 0 && value.fractional_part < 1000) {
39 session->ack_timeout = value;
40 coap_log(LOG_DEBUG, "***%s: session ack_timeout set to %u.%03u\n",
41 coap_session_str(session), session->ack_timeout.integer_part,
43 }
44}
45
46void
48 coap_fixed_point_t value) {
49 if (value.integer_part > 0 && value.fractional_part < 1000) {
50 session->ack_random_factor = value;
51 coap_log(LOG_DEBUG, "***%s: session ack_random_factor set to %u.%03u\n",
54 }
55}
56
57void
59 if (value > 0) {
60 session->max_retransmit = value;
61 coap_log(LOG_DEBUG, "***%s: session max_retransmit set to %u\n",
62 coap_session_str(session), session->max_retransmit);
63 }
64}
65
66void
67coap_session_set_nstart(coap_session_t *session, uint16_t value) {
68 if (value > 0) {
69 session->nstart = value;
70 coap_log(LOG_DEBUG, "***%s: session nstart set to %u\n",
71 coap_session_str(session), session->nstart);
72 }
73}
74
75void
77 coap_fixed_point_t value) {
78 if (value.integer_part > 0 && value.fractional_part < 1000) {
79 session->default_leisure = value;
80 coap_log(LOG_DEBUG, "***%s: session default_leisure set to %u.%03u\n",
83 }
84}
85
86void
88 if (value > 0) {
89 session->probing_rate = value;
90 coap_log(LOG_DEBUG, "***%s: session probing_rate set to %" PRIu32 "\n",
91 coap_session_str(session), session->probing_rate);
92 }
93}
94
97 return session->ack_timeout;
98}
99
102 return session->ack_random_factor;
103}
104
105uint16_t
107 return session->max_retransmit;
108}
109
110uint16_t
112 return session->nstart;
113}
114
117 return session->default_leisure;
118}
119
120uint32_t
122 return session->probing_rate;
123}
124
127 ++session->ref;
128 return session;
129}
130
131void
133 if (session) {
134#ifndef __COVERITY__
135 assert(session->ref > 0);
136 if (session->ref > 0)
137 --session->ref;
138 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
139 coap_session_free(session);
140#else /* __COVERITY__ */
141 /* Coverity scan is fooled by the reference counter leading to
142 * false positives for USE_AFTER_FREE. */
143 --session->ref;
144 __coverity_negative_sink__(session->ref);
145 /* Indicate that resources are released properly. */
146 if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
147 __coverity_free__(session);
148 }
149#endif /* __COVERITY__ */
150 }
151}
152
153void
154coap_session_set_app_data(coap_session_t *session, void *app_data) {
155 assert(session);
156 session->app = app_data;
157}
158
159void *
161 assert(session);
162 return session->app;
163}
164
165static coap_session_t *
167 const coap_addr_hash_t *addr_hash,
168 const coap_address_t *local_addr,
169 const coap_address_t *remote_addr, int ifindex,
170 coap_context_t *context, coap_endpoint_t *endpoint) {
172 sizeof(coap_session_t));
173#if ! COAP_SERVER_SUPPORT
174 (void)endpoint;
175#endif /* ! COAP_SERVER_SUPPORT */
176 if (!session)
177 return NULL;
178 memset(session, 0, sizeof(*session));
179 session->proto = proto;
180 session->type = type;
181 if (addr_hash)
182 memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
183 else
184 memset(&session->addr_hash, 0, sizeof(session->addr_hash));
185 if (local_addr)
186 coap_address_copy(&session->addr_info.local, local_addr);
187 else
189 if (remote_addr)
190 coap_address_copy(&session->addr_info.remote, remote_addr);
191 else
193 session->ifindex = ifindex;
194 session->context = context;
195#if COAP_SERVER_SUPPORT
196 session->endpoint = endpoint;
197 if (endpoint)
198 session->mtu = endpoint->default_mtu;
199 else
200#endif /* COAP_SERVER_SUPPORT */
201 session->mtu = COAP_DEFAULT_MTU;
202 session->block_mode = context->block_mode;
203 if (proto == COAP_PROTO_DTLS) {
204 session->tls_overhead = 29;
205 if (session->tls_overhead >= session->mtu) {
206 session->tls_overhead = session->mtu;
207 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
208 }
209 }
213 session->nstart = COAP_DEFAULT_NSTART;
216 session->dtls_event = -1;
220
221 /* Randomly initialize */
222 coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
223 coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
224
225 return session;
226}
227
229 coap_queue_t *q, *tmp;
230 coap_lg_xmit_t *lq, *ltmp;
231
232#if COAP_CLIENT_SUPPORT
233 coap_lg_crcv_t *cq, *etmp;
234
235 /* Need to do this before (D)TLS and socket is closed down */
236 LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
237 if (cq->observe_set && session->no_observe_cancel == 0) {
238 /* Need to close down observe */
240 /* Need to delete node we set up for NON */
241 coap_queue_t *queue = session->context->sendqueue;
242
243 while (queue) {
244 if (queue->session == session) {
245 coap_delete_node(queue);
246 break;
247 }
248 queue = queue->next;
249 }
250 }
251 }
252 LL_DELETE(session->lg_crcv, cq);
253 coap_block_delete_lg_crcv(session, cq);
254 }
255#endif /* COAP_CLIENT_SUPPORT */
256
257 if (session->partial_pdu)
259 if (session->proto == COAP_PROTO_DTLS)
260 coap_dtls_free_session(session);
261#if !COAP_DISABLE_TCP
262 else if (session->proto == COAP_PROTO_TLS)
263 coap_tls_free_session(session);
264#endif /* !COAP_DISABLE_TCP */
265 if (session->sock.flags != COAP_SOCKET_EMPTY)
266 coap_socket_close(&session->sock);
267 if (session->psk_identity)
268 coap_free(session->psk_identity);
269 if (session->psk_key)
270 coap_free(session->psk_key);
271 if (session->psk_hint)
272 coap_free(session->psk_hint);
273
274#if COAP_SERVER_SUPPORT
275 coap_cache_entry_t *cp, *ctmp;
276 HASH_ITER(hh, session->context->cache, cp, ctmp) {
277 /* cp->session is NULL if not session based */
278 if (cp->session == session) {
279 coap_delete_cache_entry(session->context, cp);
280 }
281 }
282#endif /* COAP_SERVER_SUPPORT */
283 LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
284 if (q->pdu->type==COAP_MESSAGE_CON && session->context && session->context->nack_handler)
287 }
288 LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
289 LL_DELETE(session->lg_xmit, lq);
290 coap_block_delete_lg_xmit(session, lq);
291 }
292#if COAP_SERVER_SUPPORT
293 coap_lg_srcv_t *sq, *stmp;
294
295 LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
296 LL_DELETE(session->lg_srcv, sq);
297 coap_block_delete_lg_srcv(session, sq);
298 }
299#endif /* COAP_SERVER_SUPPORT */
300}
301
303 if (!session)
304 return;
305 assert(session->ref == 0);
306 if (session->ref)
307 return;
308 coap_session_mfree(session);
309#if COAP_SERVER_SUPPORT
310 if (session->endpoint) {
311 if (session->endpoint->sessions)
312 SESSIONS_DELETE(session->endpoint->sessions, session);
313 } else
314#endif /* COAP_SERVER_SUPPORT */
315#if COAP_CLIENT_SUPPORT
316 if (session->context) {
317 if (session->context->sessions)
318 SESSIONS_DELETE(session->context->sessions, session);
319 }
320#endif /* COAP_CLIENT_SUPPORT */
322 coap_log(LOG_DEBUG, "***%s: session %p: closed\n", coap_session_str(session),
323 (void *)session);
324
326}
327
328static size_t
330 size_t max_with_header) {
331#if COAP_DISABLE_TCP
332 return max_with_header > 4 ? max_with_header - 4 : 0;
333#else /* !COAP_DISABLE_TCP */
334 if (COAP_PROTO_NOT_RELIABLE(session->proto))
335 return max_with_header > 4 ? max_with_header - 4 : 0;
336 /* we must assume there is no token to be on the safe side */
337 if (max_with_header <= 2)
338 return 0;
339 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
340 return max_with_header - 2;
341 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
342 return max_with_header - 3;
343 else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
344 return max_with_header - 4;
345 else
346 return max_with_header - 6;
347#endif /* !COAP_DISABLE_TCP */
348}
349
350size_t
352 if (session->csm_rcv_mtu)
354 (size_t)(session->csm_rcv_mtu));
355
357 (size_t)(session->mtu - session->tls_overhead));
358}
359
360size_t
362 size_t max_with_header;
363
364#if COAP_CLIENT_SUPPORT
365 /*
366 * Delay if session->doing_first is set.
367 * E.g. Reliable and CSM not in yet for checking block support
368 */
369 coap_session_t *session_rw;
370
371 /*
372 * Need to do this to not get a compiler warning about const parameters
373 * but need to maintain source code backward compatibility
374 */
375 memcpy(&session_rw, &session, sizeof(session_rw));
376 if (coap_client_delay_first(session_rw) == 0) {
377 coap_log(LOG_DEBUG, "coap_client_delay_first: timeout\n");
378 /* Have to go with the defaults */
379 }
380#endif /* COAP_CLIENT_SUPPORT */
381
382 max_with_header = (size_t)(session->mtu - session->tls_overhead);
383
384 return coap_session_max_pdu_size_internal(session, max_with_header);
385}
386
387void coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
388#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
389 if (mtu > COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
391#endif
392 if (mtu < 64)
393 mtu = 64;
394 session->mtu = mtu;
395 if (session->tls_overhead >= session->mtu) {
396 session->tls_overhead = session->mtu;
397 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
398 }
399}
400
401ssize_t coap_session_send(coap_session_t *session, const uint8_t *data, size_t datalen) {
402 ssize_t bytes_written;
403
404 coap_socket_t *sock = &session->sock;
405#if COAP_SERVER_SUPPORT
406 if (sock->flags == COAP_SOCKET_EMPTY) {
407 assert(session->endpoint != NULL);
408 sock = &session->endpoint->sock;
409 }
410#endif /* COAP_SERVER_SUPPORT */
411
412 bytes_written = coap_socket_send(sock, session, data, datalen);
413 if (bytes_written == (ssize_t)datalen) {
414 coap_ticks(&session->last_rx_tx);
415 coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
416 coap_session_str(session), datalen);
417 } else {
418 coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
419 coap_session_str(session), datalen);
420 }
421 return bytes_written;
422}
423
424ssize_t coap_session_write(coap_session_t *session, const uint8_t *data, size_t datalen) {
425 ssize_t bytes_written = coap_socket_write(&session->sock, data, datalen);
426 if (bytes_written > 0) {
427 coap_ticks(&session->last_rx_tx);
428 coap_log(LOG_DEBUG, "* %s: sent %zd bytes\n",
429 coap_session_str(session), bytes_written);
430 } else if (bytes_written < 0) {
431 coap_log(LOG_DEBUG, "* %s: failed to send %zd bytes\n",
432 coap_session_str(session), datalen );
433 }
434 return bytes_written;
435}
436
437ssize_t
439 coap_queue_t *node)
440{
441 if ( node ) {
442 coap_queue_t *removed = NULL;
443 coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
444 assert(removed == node);
446 node->session = NULL;
447 node->t = 0;
448 } else {
449 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
450 coap_queue_t *q = NULL;
451 /* Check same mid is not getting re-used in violation of RFC7252 */
452 LL_FOREACH(session->delayqueue, q) {
453 if (q->id == pdu->mid) {
454 coap_log(LOG_ERR, "** %s: mid=0x%x: already in-use - dropped\n",
455 coap_session_str(session), pdu->mid);
456 return COAP_INVALID_MID;
457 }
458 }
459 }
460 node = coap_new_node();
461 if (node == NULL)
462 return COAP_INVALID_MID;
463 node->id = pdu->mid;
464 node->pdu = pdu;
465 if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
466 uint8_t r;
467 coap_prng(&r, sizeof(r));
468 /* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
469 node->timeout = coap_calc_timeout(session, r);
470 }
471 }
472 LL_APPEND(session->delayqueue, node);
473 coap_log(LOG_DEBUG, "** %s: mid=0x%x: delayed\n",
474 coap_session_str(session), node->id);
475 return COAP_PDU_DELAYED;
476}
477
478#if !COAP_DISABLE_TCP
480 coap_pdu_t *pdu;
481 uint8_t buf[4];
482 assert(COAP_PROTO_RELIABLE(session->proto));
483 coap_log(LOG_DEBUG, "***%s: sending CSM\n", coap_session_str(session));
484 session->state = COAP_SESSION_STATE_CSM;
485 session->partial_write = 0;
486 if (session->mtu == 0)
487 session->mtu = COAP_DEFAULT_MTU; /* base value */
489 if ( pdu == NULL
491 coap_encode_var_safe(buf, sizeof(buf),
492 session->context->csm_max_message_size), buf) == 0
494 coap_encode_var_safe(buf, sizeof(buf),
495 0), buf) == 0
496 || coap_pdu_encode_header(pdu, session->proto) == 0
497 ) {
499 } else {
500 ssize_t bytes_written = coap_session_send_pdu(session, pdu);
501 if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
503 } else {
504 session->csm_rcv_mtu = session->context->csm_max_message_size;
505 if (session->csm_rcv_mtu > COAP_BERT_BASE)
506 session->csm_bert_loc_support = 1;
507 else
508 session->csm_bert_loc_support = 0;
509 }
510 }
511 if (pdu)
512 coap_delete_pdu(pdu);
513}
514#endif /* !COAP_DISABLE_TCP */
515
517 coap_pdu_t *ping = NULL;
519 return COAP_INVALID_MID;
520 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
521 uint16_t mid = coap_new_message_id (session);
522 ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
523 }
524#if !COAP_DISABLE_TCP
525 else {
527 }
528#endif /* !COAP_DISABLE_TCP */
529 if (!ping)
530 return COAP_INVALID_MID;
531 return coap_send_internal(session, ping);
532}
533
535 if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
536 coap_log(LOG_DEBUG, "***%s: session connected\n",
537 coap_session_str(session));
538 if (session->state == COAP_SESSION_STATE_CSM)
540 if (session->doing_first)
541 session->doing_first = 0;
542 }
543
545 session->partial_write = 0;
546
547 if ( session->proto==COAP_PROTO_DTLS) {
548 session->tls_overhead = coap_dtls_get_overhead(session);
549 if (session->tls_overhead >= session->mtu) {
550 session->tls_overhead = session->mtu;
551 coap_log(LOG_ERR, "DTLS overhead exceeds MTU\n");
552 }
553 }
554
555 while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
556 ssize_t bytes_written;
557 coap_queue_t *q = session->delayqueue;
558 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
559 if (session->con_active >= COAP_NSTART(session))
560 break;
561 session->con_active++;
562 }
563 /* Take entry off the queue */
564 session->delayqueue = q->next;
565 q->next = NULL;
566
567 coap_log(LOG_DEBUG, "** %s: mid=0x%x: transmitted after delay\n",
568 coap_session_str(session), (int)q->pdu->mid);
569 bytes_written = coap_session_send_pdu(session, q->pdu);
570 if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
571 if (coap_wait_ack(session->context, session, q) >= 0)
572 q = NULL;
573 }
574 if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
575 if (q)
577 if (bytes_written < 0)
578 break;
579 } else {
580 if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
581 q->next = session->delayqueue;
582 session->delayqueue = q;
583 if (bytes_written > 0)
584 session->partial_write = (size_t)bytes_written;
585 break;
586 } else {
588 }
589 }
590 }
591}
592
594#if !COAP_DISABLE_TCP
595 coap_session_state_t state = session->state;
596#endif /* !COAP_DISABLE_TCP */
597
598 coap_log(LOG_DEBUG, "***%s: session disconnected (reason %d)\n",
599 coap_session_str(session), reason);
600#if COAP_SERVER_SUPPORT
601 coap_delete_observers( session->context, session );
602#endif /* COAP_SERVER_SUPPORT */
603
604 if ( session->tls) {
605 if (session->proto == COAP_PROTO_DTLS)
606 coap_dtls_free_session(session);
607#if !COAP_DISABLE_TCP
608 else if (session->proto == COAP_PROTO_TLS)
609 coap_tls_free_session(session);
610#endif /* !COAP_DISABLE_TCP */
611 session->tls = NULL;
612 }
613
614 if (session->proto == COAP_PROTO_UDP)
616 else
618
619 session->con_active = 0;
620
621 if (session->partial_pdu) {
623 session->partial_pdu = NULL;
624 }
625 session->partial_read = 0;
626
627 while (session->delayqueue) {
628 coap_queue_t *q = session->delayqueue;
629 session->delayqueue = q->next;
630 q->next = NULL;
631 coap_log(LOG_DEBUG, "** %s: mid=0x%x: not transmitted after disconnect\n",
632 coap_session_str(session), q->id);
633 if (q->pdu->type==COAP_MESSAGE_CON
634 && COAP_PROTO_NOT_RELIABLE(session->proto)
635 && reason == COAP_NACK_ICMP_ISSUE)
636 {
637 /* Make sure that we try a re-transmit later on ICMP error */
638 if (coap_wait_ack(session->context, session, q) >= 0) {
639 if (session->context->nack_handler) {
640 session->context->nack_handler(session, q->pdu, reason, q->id);
641 }
642 q = NULL;
643 }
644 }
645 if (q && q->pdu->type == COAP_MESSAGE_CON
646 && session->context->nack_handler)
647 {
648 session->context->nack_handler(session, q->pdu, reason, q->id);
649 }
650 if (q)
652 }
653 if (reason != COAP_NACK_ICMP_ISSUE) {
654 coap_cancel_session_messages(session->context, session, reason);
655 }
656 else if (session->context->nack_handler) {
657 coap_queue_t *q = session->context->sendqueue;
658 while (q) {
659 if (q->session == session) {
660 session->context->nack_handler(session, q->pdu, reason, q->id);
661 }
662 q = q->next;
663 }
664 }
665
666#if !COAP_DISABLE_TCP
667 if (COAP_PROTO_RELIABLE(session->proto)) {
668 if (session->sock.flags != COAP_SOCKET_EMPTY) {
669 coap_socket_close(&session->sock);
670 coap_handle_event(session->context,
673 }
674 if (state != COAP_SESSION_STATE_NONE) {
675 coap_handle_event(session->context,
678 }
679 if (session->doing_first)
680 session->doing_first = 0;
681 }
682#endif /* !COAP_DISABLE_TCP */
683}
684
685#if COAP_SERVER_SUPPORT
686static void
687coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
688 const coap_addr_tuple_t *addr_info) {
689 memset(addr_hash, 0, sizeof(coap_addr_hash_t));
690 coap_address_copy(&addr_hash->remote, &addr_info->remote);
691 addr_hash->lport = coap_address_get_port(&addr_info->local);
692 addr_hash->proto = proto;
693}
694
697 const coap_packet_t *packet, coap_tick_t now) {
698 coap_session_t *session;
699 coap_session_t *rtmp;
700 unsigned int num_idle = 0;
701 unsigned int num_hs = 0;
702 coap_session_t *oldest = NULL;
703 coap_session_t *oldest_hs = NULL;
704 coap_addr_hash_t addr_hash;
705
706 coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
707 SESSIONS_FIND(endpoint->sessions, addr_hash, session);
708 if (session) {
709 /* Maybe mcast or unicast IP address which is not in the hash */
710 coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
711 session->ifindex = packet->ifindex;
712 session->last_rx_tx = now;
713 return session;
714 }
715
716 SESSIONS_ITER(endpoint->sessions, session, rtmp) {
717 if (session->ref == 0 && session->delayqueue == NULL) {
718 if (session->type == COAP_SESSION_TYPE_SERVER) {
719 ++num_idle;
720 if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
721 oldest = session;
722
723 if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
724 ++num_hs;
725 /* See if this is a partial (D)TLS session set up
726 which needs to be cleared down to prevent DOS */
727 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
728 if (oldest_hs == NULL ||
729 session->last_rx_tx < oldest_hs->last_rx_tx)
730 oldest_hs = session;
731 }
732 }
733 }
734 else if (session->type == COAP_SESSION_TYPE_HELLO) {
735 ++num_hs;
736 /* See if this is a partial (D)TLS session set up for Client Hello
737 which needs to be cleared down to prevent DOS */
738 if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
739 if (oldest_hs == NULL ||
740 session->last_rx_tx < oldest_hs->last_rx_tx)
741 oldest_hs = session;
742 }
743 }
744 }
745 }
746
747 if (endpoint->context->max_idle_sessions > 0 &&
748 num_idle >= endpoint->context->max_idle_sessions) {
750 coap_session_free(oldest);
751 }
752 else if (oldest_hs) {
753 coap_log(LOG_WARNING, "***%s: Incomplete session timed out\n",
754 coap_session_str(oldest_hs));
756 coap_session_free(oldest_hs);
757 }
758
759 if (num_hs > (endpoint->context->max_handshake_sessions ?
762 /* Maxed out on number of sessions in (D)TLS negotiation state */
764 "Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
765 "large. New request ignored\n");
766 return NULL;
767 }
768
769 if (endpoint->proto == COAP_PROTO_DTLS) {
770 /*
771 * Need to check that this actually is a Client Hello before wasting
772 * time allocating and then freeing off session.
773 */
774
775 /*
776 * Generic header structure of the DTLS record layer.
777 * typedef struct __attribute__((__packed__)) {
778 * uint8_t content_type; content type of the included message
779 * uint16_t version; Protocol version
780 * uint16_t epoch; counter for cipher state changes
781 * uint8_t sequence_number[6]; sequence number
782 * uint16_t length; length of the following fragment
783 * uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
784 * } dtls_record_handshake_t;
785 */
786#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
787#define DTLS_CT_ALERT 21 /* Content Type Alert */
788#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
789#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
790#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
791
792#ifdef WITH_LWIP
793 const uint8_t *payload = (const uint8_t*)packet->pbuf->payload;
794 size_t length = packet->pbuf->len;
795#else /* ! WITH_LWIP */
796 const uint8_t *payload = (const uint8_t*)packet->payload;
797 size_t length = packet->length;
798#endif /* ! WITH_LWIP */
799 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
801 "coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
802 payload[OFF_CONTENT_TYPE], length,
803 OFF_HANDSHAKE_TYPE + 1);
804 return NULL;
805 }
806 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
807 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
808 /* only log if not a late alert */
809 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
811 "coap_dtls_hello: ContentType %d Handshake %d dropped\n",
812 payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
813 return NULL;
814 }
815 }
816
818 &addr_hash, &packet->addr_info.local,
819 &packet->addr_info.remote,
820 packet->ifindex, endpoint->context, endpoint);
821 if (session) {
822 session->last_rx_tx = now;
823 if (endpoint->proto == COAP_PROTO_UDP)
825 else if (endpoint->proto == COAP_PROTO_DTLS) {
826 session->type = COAP_SESSION_TYPE_HELLO;
827 }
828 SESSIONS_ADD(endpoint->sessions, session);
829 coap_log(LOG_DEBUG, "***%s: session %p: new incoming session\n",
830 coap_session_str(session), (void *)session);
832 }
833 return session;
834}
835
838 coap_tick_t now) {
839 if (session) {
840 session->last_rx_tx = now;
842 session->tls = coap_dtls_new_server_session(session);
843 if (session->tls) {
845 } else {
846 coap_session_free(session);
847 session = NULL;
848 }
849 }
850 return session;
851}
852#endif /* COAP_SERVER_SUPPORT */
853
854#ifdef COAP_EPOLL_SUPPORT
855static void
856coap_epoll_ctl_add(coap_socket_t *sock,
857 uint32_t events,
858 const char *func
859) {
860 int ret;
861 struct epoll_event event;
862 coap_context_t *context;
863
864 if (sock == NULL)
865 return;
866
867#if COAP_SERVER_SUPPORT
868 context = sock->session ? sock->session->context :
869 sock->endpoint ? sock->endpoint->context : NULL;
870#else /* ! COAP_SERVER_SUPPORT */
871 context = sock->session ? sock->session->context : NULL;
872#endif /* ! COAP_SERVER_SUPPORT */
873 if (context == NULL)
874 return;
875
876 /* Needed if running 32bit as ptr is only 32bit */
877 memset(&event, 0, sizeof(event));
878 event.events = events;
879 event.data.ptr = sock;
880
881 ret = epoll_ctl(context->epfd, EPOLL_CTL_ADD, sock->fd, &event);
882 if (ret == -1) {
884 "%s: epoll_ctl ADD failed: %s (%d)\n",
885 func,
886 coap_socket_strerror(), errno);
887 }
888}
889#endif /* COAP_EPOLL_SUPPORT */
890
891#if COAP_CLIENT_SUPPORT
892static coap_session_t *
893coap_session_create_client(
894 coap_context_t *ctx,
895 const coap_address_t *local_if,
896 const coap_address_t *server,
897 coap_proto_t proto
898) {
899 coap_session_t *session = NULL;
900
901 assert(server);
902
903 switch(proto) {
904 case COAP_PROTO_UDP:
905 break;
906 case COAP_PROTO_DTLS:
907 if (!coap_dtls_is_supported()) {
908 coap_log(LOG_CRIT, "coap_new_client_session*: DTLS not supported\n");
909 return NULL;
910 }
911 break;
912 case COAP_PROTO_TCP:
913 if (!coap_tcp_is_supported()) {
914 coap_log(LOG_CRIT, "coap_new_client_session*: TCP not supported\n");
915 return NULL;
916 }
917 break;
918 case COAP_PROTO_TLS:
919 if (!coap_tls_is_supported()) {
920 coap_log(LOG_CRIT, "coap_new_client_session*: TLS not supported\n");
921 return NULL;
922 }
923 break;
924 case COAP_PROTO_NONE:
925 default:
926 assert(0);
927 break;
928 }
929 session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
930 local_if, server, 0, ctx, NULL);
931 if (!session)
932 goto error;
933
934 coap_session_reference(session);
935
936 if (proto == COAP_PROTO_UDP || proto == COAP_PROTO_DTLS) {
937 coap_session_t *s, *rtmp;
938 if (!coap_socket_connect_udp(&session->sock, local_if, server,
940 &session->addr_info.local, &session->addr_info.remote)) {
941 goto error;
942 }
943 /* Check that this is not a duplicate 4-tuple */
944 SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
945 if ((s->proto == COAP_PROTO_UDP || s->proto == COAP_PROTO_DTLS) &&
947 &s->addr_info.local) &&
949 &s->addr_info.remote)) {
950 coap_log(LOG_WARNING, "***%s: session %p: duplicate - already exists\n",
951 coap_session_str(session), (void *)session);
952 goto error;
953 }
954 }
955#if !COAP_DISABLE_TCP
956 } else if (proto == COAP_PROTO_TCP || proto == COAP_PROTO_TLS) {
957 if (!coap_socket_connect_tcp1(&session->sock, local_if, server,
959 &session->addr_info.local, &session->addr_info.remote)) {
960 goto error;
961 }
962#endif /* !COAP_DISABLE_TCP */
963 }
964
965#ifdef COAP_EPOLL_SUPPORT
966 session->sock.session = session;
967 coap_epoll_ctl_add(&session->sock,
968 EPOLLIN |
969 ((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
970 EPOLLOUT : 0),
971 __func__);
972#endif /* COAP_EPOLL_SUPPORT */
973
975 if (local_if)
976 session->sock.flags |= COAP_SOCKET_BOUND;
977#if COAP_SERVER_SUPPORT
978 if (ctx->proxy_uri_resource)
979 session->proxy_session = 1;
980#endif /* COAP_SERVER_SUPPORT */
981 SESSIONS_ADD(ctx->sessions, session);
982 return session;
983
984error:
985 /*
986 * Need to add in the session as coap_session_release()
987 * will call SESSIONS_DELETE in coap_session_free().
988 */
989 if (session)
990 SESSIONS_ADD(ctx->sessions, session);
991 coap_session_release(session);
992 return NULL;
993}
994
995static coap_session_t *
996coap_session_connect(coap_session_t *session) {
997 if (session->proto == COAP_PROTO_UDP) {
999 } else if (session->proto == COAP_PROTO_DTLS) {
1000 session->tls = coap_dtls_new_client_session(session);
1001 if (session->tls) {
1003 } else {
1004 /* Need to free session object. As a new session may not yet
1005 * have been referenced, we call coap_session_reference() first
1006 * before trying to release the object.
1007 */
1008 coap_session_reference(session);
1009 coap_session_release(session);
1010 return NULL;
1011 }
1012#if !COAP_DISABLE_TCP
1013 } else {
1014 if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS) {
1015 if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
1017 if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
1018 session->state != COAP_SESSION_STATE_NONE &&
1019 COAP_PROTO_RELIABLE(session->proto) &&
1020 session->type == COAP_SESSION_TYPE_CLIENT) {
1021 session->doing_first = 1;
1022 }
1023 } else if (session->proto == COAP_PROTO_TLS) {
1024 int connected = 0;
1025 session->tls = coap_tls_new_client_session(session, &connected);
1026 if (session->tls) {
1028 if (connected)
1029 coap_session_send_csm(session);
1030 } else {
1031 /* Need to free session object. As a new session may not yet
1032 * have been referenced, we call coap_session_reference()
1033 * first before trying to release the object.
1034 */
1035 coap_session_reference(session);
1036 coap_session_release(session);
1037 return NULL;
1038 }
1039 } else {
1040 coap_session_send_csm(session);
1041 }
1042 }
1043#endif /* !COAP_DISABLE_TCP */
1044 }
1045 coap_ticks(&session->last_rx_tx);
1046 return session;
1047}
1048#endif /* COAP_CLIENT_SUPPORT */
1049
1050#if COAP_SERVER_SUPPORT
1051static coap_session_t *
1052coap_session_accept(coap_session_t *session) {
1053#if !COAP_DISABLE_TCP
1054 if (session->proto == COAP_PROTO_TCP || session->proto == COAP_PROTO_TLS)
1056 if (session->proto == COAP_PROTO_TCP) {
1057 coap_session_send_csm(session);
1058 } else if (session->proto == COAP_PROTO_TLS) {
1059 int connected = 0;
1060 session->tls = coap_tls_new_server_session(session, &connected);
1061 if (session->tls) {
1063 if (connected) {
1065 coap_session_send_csm(session);
1066 }
1067 } else {
1068 /* Need to free session object. As a new session may not yet
1069 * have been referenced, we call coap_session_reference() first
1070 * before trying to release the object.
1071 */
1072 coap_session_reference(session);
1073 coap_session_release(session);
1074 session = NULL;
1075 }
1076 }
1077#endif /* COAP_DISABLE_TCP */
1078 return session;
1079}
1080#endif /* COAP_SERVER_SUPPORT */
1081
1082#if COAP_CLIENT_SUPPORT
1084 coap_context_t *ctx,
1085 const coap_address_t *local_if,
1086 const coap_address_t *server,
1087 coap_proto_t proto
1088) {
1089 coap_session_t *session = coap_session_create_client(ctx, local_if, server,
1090 proto);
1091 if (session) {
1092 coap_log(LOG_DEBUG, "***%s: session %p: created outgoing session\n",
1093 coap_session_str(session), (void *)session);
1094 session = coap_session_connect(session);
1095 }
1096 return session;
1097}
1098
1100 coap_context_t *ctx,
1101 const coap_address_t *local_if,
1102 const coap_address_t *server,
1103 coap_proto_t proto,
1104 const char *identity,
1105 const uint8_t *key,
1106 unsigned key_len
1107) {
1108 coap_dtls_cpsk_t setup_data;
1109
1110 memset (&setup_data, 0, sizeof(setup_data));
1112
1113 if (identity) {
1114 setup_data.psk_info.identity.s = (const uint8_t *)identity;
1115 setup_data.psk_info.identity.length = strlen(identity);
1116 }
1117
1118 if (key && key_len > 0) {
1119 setup_data.psk_info.key.s = key;
1120 setup_data.psk_info.key.length = key_len;
1121 }
1122
1123 return coap_new_client_session_psk2(ctx, local_if, server,
1124 proto, &setup_data);
1125}
1126
1128 coap_context_t *ctx,
1129 const coap_address_t *local_if,
1130 const coap_address_t *server,
1131 coap_proto_t proto,
1132 coap_dtls_cpsk_t *setup_data
1133) {
1134 coap_session_t *session = coap_session_create_client(ctx, local_if,
1135 server, proto);
1136
1137 if (!session)
1138 return NULL;
1139
1140 session->cpsk_setup_data = *setup_data;
1141 if (setup_data->psk_info.identity.s) {
1142 session->psk_identity =
1144 setup_data->psk_info.identity.length);
1145 if (!session->psk_identity) {
1146 coap_log(LOG_WARNING, "Cannot store session Identity (PSK)\n");
1147 coap_session_release(session);
1148 return NULL;
1149 }
1150 }
1152 coap_log(LOG_WARNING, "Identity (PSK) not defined\n");
1153 coap_session_release(session);
1154 return NULL;
1155 }
1156
1157 if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
1158 session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
1159 setup_data->psk_info.key.length);
1160 if (!session->psk_key) {
1161 coap_log(LOG_WARNING, "Cannot store session pre-shared key (PSK)\n");
1162 coap_session_release(session);
1163 return NULL;
1164 }
1165 }
1167 coap_log(LOG_WARNING, "Pre-shared key (PSK) not defined\n");
1168 coap_session_release(session);
1169 return NULL;
1170 }
1171
1173 if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
1174 coap_session_release(session);
1175 return NULL;
1176 }
1177 }
1178 coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
1179 coap_session_str(session));
1180 return coap_session_connect(session);
1181}
1182#endif /* ! COAP_CLIENT_SUPPORT */
1183
1184int
1186 const coap_bin_const_t *psk_hint
1187) {
1188 /* We may be refreshing the hint with the same hint */
1189 coap_bin_const_t *old_psk_hint = session->psk_hint;
1190
1191 if (psk_hint && psk_hint->s) {
1192 if (session->psk_hint) {
1193 if (coap_binary_equal(session->psk_hint, psk_hint))
1194 return 1;
1195 }
1196 session->psk_hint = coap_new_bin_const(psk_hint->s,
1197 psk_hint->length);
1198 if (!session->psk_hint) {
1199 coap_log(LOG_ERR, "No memory to store identity hint (PSK)\n");
1200 if (old_psk_hint)
1201 coap_delete_bin_const(old_psk_hint);
1202 return 0;
1203 }
1204 }
1205 else {
1206 session->psk_hint = NULL;
1207 }
1208 if (old_psk_hint)
1209 coap_delete_bin_const(old_psk_hint);
1210
1211 return 1;
1212}
1213
1214int
1216 const coap_bin_const_t *psk_key
1217) {
1218 /* We may be refreshing the key with the same key */
1219 coap_bin_const_t *old_psk_key = session->psk_key;
1220
1221 if (psk_key && psk_key->s) {
1222 if (session->psk_key) {
1223 if (coap_binary_equal(session->psk_key, psk_key))
1224 return 1;
1225 }
1226 session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
1227 if (!session->psk_key) {
1228 coap_log(LOG_ERR, "No memory to store pre-shared key (PSK)\n");
1229 if (old_psk_key)
1230 coap_delete_bin_const(old_psk_key);
1231 return 0;
1232 }
1233 }
1234 else {
1235 session->psk_key = NULL;
1236 }
1237 if (old_psk_key)
1238 coap_delete_bin_const(old_psk_key);
1239
1240 return 1;
1241}
1242
1243int
1245 const coap_bin_const_t *psk_identity
1246) {
1247 /* We may be refreshing the identity with the same identity */
1248 coap_bin_const_t *old_psk_identity = session->psk_identity;
1249
1250 if (psk_identity && psk_identity->s) {
1251 if (session->psk_identity) {
1252 if (coap_binary_equal(session->psk_identity, psk_identity))
1253 return 1;
1254 }
1255 session->psk_identity = coap_new_bin_const(psk_identity->s,
1256 psk_identity->length);
1257 if (!session->psk_identity) {
1258 coap_log(LOG_ERR, "No memory to store pre-shared key identity (PSK)\n");
1259 if (old_psk_identity)
1260 coap_delete_bin_const(old_psk_identity);
1261 return 0;
1262 }
1263 }
1264 else {
1265 session->psk_identity = NULL;
1266 }
1267 if (old_psk_identity)
1268 coap_delete_bin_const(old_psk_identity);
1269
1270 return 1;
1271}
1272
1273#if COAP_SERVER_SUPPORT
1274const coap_bin_const_t *
1276 if (session)
1277 return session->psk_hint;
1278 return NULL;
1279}
1280#endif /* COAP_SERVER_SUPPORT */
1281
1282const coap_bin_const_t *
1284 const coap_bin_const_t *psk_identity = NULL;
1285 if (session) {
1286 psk_identity = session->psk_identity;
1287 if (psk_identity == NULL) {
1288 psk_identity = &session->cpsk_setup_data.psk_info.identity;
1289 }
1290 }
1291 return psk_identity;
1292}
1293
1294const coap_bin_const_t *
1296 if (session)
1297 return session->psk_key;
1298 return NULL;
1299}
1300
1301#if COAP_CLIENT_SUPPORT
1303 coap_context_t *ctx,
1304 const coap_address_t *local_if,
1305 const coap_address_t *server,
1306 coap_proto_t proto,
1307 coap_dtls_pki_t* setup_data
1308) {
1309 coap_session_t *session;
1310
1312 if (!setup_data) {
1313 return NULL;
1314 } else {
1315 if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
1317 "coap_new_client_session_pki: Wrong version of setup_data\n");
1318 return NULL;
1319 }
1320 }
1321
1322 }
1323 session = coap_session_create_client(ctx, local_if, server, proto);
1324
1325 if (!session) {
1326 return NULL;
1327 }
1328
1330 /* we know that setup_data is not NULL */
1331 if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
1332 coap_session_release(session);
1333 return NULL;
1334 }
1335 }
1336 coap_log(LOG_DEBUG, "***%s: new outgoing session\n",
1337 coap_session_str(session));
1338 return coap_session_connect(session);
1339}
1340#endif /* ! COAP_CLIENT_SUPPORT */
1341
1342#if COAP_SERVER_SUPPORT
1344 coap_context_t *ctx,
1345 coap_endpoint_t *ep
1346) {
1347 coap_session_t *session;
1349 NULL, NULL, NULL, 0, ctx, ep );
1350 if (!session)
1351 goto error;
1352
1353#if !COAP_DISABLE_TCP
1354 if (!coap_socket_accept_tcp(&ep->sock, &session->sock,
1355 &session->addr_info.local,
1356 &session->addr_info.remote))
1357 goto error;
1358 coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
1359
1360#endif /* !COAP_DISABLE_TCP */
1363#ifdef COAP_EPOLL_SUPPORT
1364 session->sock.session = session;
1365 coap_epoll_ctl_add(&session->sock,
1366 EPOLLIN,
1367 __func__);
1368#endif /* COAP_EPOLL_SUPPORT */
1369 SESSIONS_ADD(ep->sessions, session);
1370 if (session) {
1371 coap_log(LOG_DEBUG, "***%s: session %p: new incoming session\n",
1372 coap_session_str(session), (void *)session);
1373 /* Returned session may already have been released and is now NULL */
1374 session = coap_session_accept(session);
1375 if(session) {
1377 }
1378 }
1379 return session;
1380
1381error:
1382 /*
1383 * Need to add in the session as coap_session_release()
1384 * will call SESSIONS_DELETE in coap_session_free().
1385 */
1386 if (session) {
1387 SESSIONS_ADD(ep->sessions, session);
1388 coap_session_free(session);
1389 }
1390 return NULL;
1391}
1392#endif /* COAP_SERVER_SUPPORT */
1393
1394void
1396 const uint8_t *data) {
1397 session->tx_token = coap_decode_var_bytes8(data, len);
1398}
1399
1400void coap_session_new_token(coap_session_t *session, size_t *len,
1401 uint8_t *data) {
1402 *len = coap_encode_var_safe8(data,
1403 sizeof(session->tx_token), ++session->tx_token);
1404}
1405
1406uint16_t
1408 return ++session->tx_mid;
1409}
1410
1411const coap_address_t *
1413 if (session)
1414 return &session->addr_info.remote;
1415 return NULL;
1416}
1417
1418const coap_address_t *
1420 if (session)
1421 return &session->addr_info.local;
1422 return NULL;
1423}
1424
1427 if (session)
1428 return session->context;
1429 return NULL;
1430}
1431
1434 if (session)
1435 return session->proto;
1436 return 0;
1437}
1438
1441 if (session)
1442 return session->type;
1443 return 0;
1444}
1445
1446#if COAP_CLIENT_SUPPORT
1447int
1449#if COAP_SERVER_SUPPORT
1450 if (session && session->type == COAP_SESSION_TYPE_SERVER) {
1451 coap_session_reference(session);
1452 session->type = COAP_SESSION_TYPE_CLIENT;
1453 return 1;
1454 }
1455#else /* ! COAP_SERVER_SUPPORT */
1456 (void)session;
1457#endif /* ! COAP_SERVER_SUPPORT */
1458 return 0;
1459}
1460#endif /* COAP_CLIENT_SUPPORT */
1461
1464 if (session)
1465 return session->state;
1466 return 0;
1467}
1468
1470 if (session)
1471 return session->ifindex;
1472 return -1;
1473}
1474
1476 coap_tls_library_t *tls_lib) {
1477 if (session)
1478 return coap_dtls_get_tls(session, tls_lib);
1479 return NULL;
1480}
1481
1482#ifndef WITH_LWIP
1483#if COAP_SERVER_SUPPORT
1485coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
1486 coap_endpoint_t *ep = NULL;
1487
1488 assert(context);
1489 assert(listen_addr);
1490 assert(proto != COAP_PROTO_NONE);
1491
1492 if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
1493 coap_log(LOG_CRIT, "coap_new_endpoint: DTLS not supported\n");
1494 goto error;
1495 }
1496
1497 if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
1498 coap_log(LOG_CRIT, "coap_new_endpoint: TLS not supported\n");
1499 goto error;
1500 }
1501
1502 if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
1503 coap_log(LOG_CRIT, "coap_new_endpoint: TCP not supported\n");
1504 goto error;
1505 }
1506
1507 if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS) {
1510 "coap_new_endpoint: one of coap_context_set_psk() or "
1511 "coap_context_set_pki() not called\n");
1512 goto error;
1513 }
1514 }
1515
1516 ep = coap_malloc_endpoint();
1517 if (!ep) {
1518 coap_log(LOG_WARNING, "coap_new_endpoint: malloc");
1519 goto error;
1520 }
1521
1522 memset(ep, 0, sizeof(coap_endpoint_t));
1523 ep->context = context;
1524 ep->proto = proto;
1525
1526 if (proto==COAP_PROTO_UDP || proto==COAP_PROTO_DTLS) {
1527 if (!coap_socket_bind_udp(&ep->sock, listen_addr, &ep->bind_addr))
1528 goto error;
1530#if !COAP_DISABLE_TCP
1531 } else if (proto==COAP_PROTO_TCP || proto==COAP_PROTO_TLS) {
1532 if (!coap_socket_bind_tcp(&ep->sock, listen_addr, &ep->bind_addr))
1533 goto error;
1535#endif /* !COAP_DISABLE_TCP */
1536 } else {
1537 coap_log(LOG_CRIT, "coap_new_endpoint: protocol not supported\n");
1538 goto error;
1539 }
1540
1541 if (LOG_DEBUG <= coap_get_log_level()) {
1542#ifndef INET6_ADDRSTRLEN
1543#define INET6_ADDRSTRLEN 40
1544#endif
1545 unsigned char addr_str[INET6_ADDRSTRLEN + 8];
1546
1547 if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
1548 coap_log(LOG_DEBUG, "created %s endpoint %s\n",
1549 ep->proto == COAP_PROTO_TLS ? "TLS "
1550 : ep->proto == COAP_PROTO_TCP ? "TCP "
1551 : ep->proto == COAP_PROTO_DTLS ? "DTLS" : "UDP ",
1552 addr_str);
1553 }
1554 }
1555
1557
1559
1560#ifdef COAP_EPOLL_SUPPORT
1561 ep->sock.endpoint = ep;
1562 coap_epoll_ctl_add(&ep->sock,
1563 EPOLLIN,
1564 __func__);
1565#endif /* COAP_EPOLL_SUPPORT */
1566
1567 LL_PREPEND(context->endpoint, ep);
1568 return ep;
1569
1570error:
1572 return NULL;
1573}
1574
1575void coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu) {
1576 ep->default_mtu = (uint16_t)mtu;
1577}
1578
1579void
1581 if (ep) {
1582 coap_session_t *session, *rtmp;
1583
1584 SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
1585 assert(session->ref == 0);
1586 if (session->ref == 0) {
1587 coap_session_free(session);
1588 }
1589 }
1590 if (ep->sock.flags != COAP_SOCKET_EMPTY) {
1591 /*
1592 * ep->sock.endpoint is set in coap_new_endpoint().
1593 * ep->sock.session is never set.
1594 *
1595 * session->sock.session is set for both clients and servers (when a
1596 * new session is accepted), but does not affect the endpoint.
1597 *
1598 * So, it is safe to call coap_socket_close() after all the sessions
1599 * have been freed above as we are only working with the endpoint sock.
1600 */
1601#ifdef COAP_EPOLL_SUPPORT
1602 assert(ep->sock.session == NULL);
1603#endif /* COAP_EPOLL_SUPPORT */
1604 coap_socket_close(&ep->sock);
1605 }
1606
1607 if (ep->context && ep->context->endpoint) {
1608 LL_DELETE(ep->context->endpoint, ep);
1609 }
1611 }
1612}
1613#endif /* COAP_SERVER_SUPPORT */
1614#endif /* WITH_LWIP */
1615
1618 const coap_address_t *remote_addr,
1619 int ifindex) {
1620 coap_session_t *s, *rtmp;
1621#if COAP_CLIENT_SUPPORT
1622 SESSIONS_ITER(ctx->sessions, s, rtmp) {
1623 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1624 remote_addr))
1625 return s;
1626 }
1627#endif /* COAP_CLIENT_SUPPORT */
1628#if COAP_SERVER_SUPPORT
1629 coap_endpoint_t *ep;
1630
1631 LL_FOREACH(ctx->endpoint, ep) {
1632 SESSIONS_ITER(ep->sessions, s, rtmp) {
1633 if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
1634 remote_addr))
1635 return s;
1636 }
1637 }
1638#endif /* COAP_SERVER_SUPPORT */
1639 return NULL;
1640}
1641
1642const char *coap_session_str(const coap_session_t *session) {
1643 static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
1644 char *p = szSession, *end = szSession + sizeof(szSession);
1645 if (coap_print_addr(&session->addr_info.local,
1646 (unsigned char*)p, end - p) > 0)
1647 p += strlen(p);
1648 if (p + 6 < end) {
1649 strcpy(p, " <-> ");
1650 p += 5;
1651 }
1652 if (p + 1 < end) {
1653 if (coap_print_addr(&session->addr_info.remote,
1654 (unsigned char*)p, end - p) > 0)
1655 p += strlen(p);
1656 }
1657 if (session->ifindex > 0 && p + 1 < end)
1658 p += snprintf(p, end - p, " (if%d)", session->ifindex);
1659 if (p + 6 < end) {
1660 if (session->proto == COAP_PROTO_UDP) {
1661 strcpy(p, " UDP ");
1662 p += 4;
1663 } else if (session->proto == COAP_PROTO_DTLS) {
1664 strcpy(p, " DTLS");
1665 p += 5;
1666 } else if (session->proto == COAP_PROTO_TCP) {
1667 strcpy(p, " TCP ");
1668 p += 4;
1669 } else if (session->proto == COAP_PROTO_TLS) {
1670 strcpy(p, " TLS ");
1671 p += 4;
1672 } else {
1673 strcpy(p, " NONE");
1674 p += 5;
1675 }
1676 }
1677
1678 return szSession;
1679}
1680
1681#if COAP_SERVER_SUPPORT
1682const char *coap_endpoint_str(const coap_endpoint_t *endpoint) {
1683 static char szEndpoint[128];
1684 char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
1685 if (coap_print_addr(&endpoint->bind_addr, (unsigned char*)p, end - p) > 0)
1686 p += strlen(p);
1687 if (p + 6 < end) {
1688 if (endpoint->proto == COAP_PROTO_UDP) {
1689 strcpy(p, " UDP");
1690 p += 4;
1691 } else if (endpoint->proto == COAP_PROTO_DTLS) {
1692 strcpy(p, " DTLS");
1693 p += 5;
1694 } else {
1695 strcpy(p, " NONE");
1696 p += 5;
1697 }
1698 }
1699
1700 return szEndpoint;
1701}
1702#endif /* COAP_SERVER_SUPPORT */
1703#ifdef COAP_CLIENT_SUPPORT
1704void
1706 session->no_observe_cancel = 1;
1707}
1708#endif /* COAP_CLIENT_SUPPORT */
1709#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:1610
const char * coap_socket_strerror(void)
Definition: coap_io.c:1604
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)
void coap_free_endpoint(coap_endpoint_t *ep)
void coap_mfree_endpoint(struct coap_endpoint_t *ep)
struct coap_endpoint_t * coap_malloc_endpoint(void)
#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
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
#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:516
#define PRIu32
Definition: coap_session.c:33
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:166
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
Definition: coap_session.c:329
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
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)
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:1105
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:116
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:58
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:47
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:101
#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:106
#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:121
#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:37
#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:67
#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:87
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:76
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:111
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:96
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:2077
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:2121
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:3352
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
Triggered when TCP layer completes exchange of CSM information.
Definition: coap_event.h:61
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition: coap_event.h:55
@ COAP_EVENT_DTLS_CONNECTED
Triggered when (D)TLS session connected.
Definition: coap_event.h:41
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition: coap_event.h:65
@ COAP_EVENT_SERVER_SESSION_NEW
Called in the CoAP IO loop if a new server-side session is created due to an incoming connection.
Definition: coap_event.h:85
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition: coap_event.h:63
@ COAP_EVENT_SERVER_SESSION_DEL
Called in the CoAP IO loop if a server session is deleted (e.g., due to inactivity or because the max...
Definition: coap_event.h:94
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition: coap_event.h:53
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition: coap_event.h:51
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:1188
#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:615
#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:154
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:99
#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:438
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:479
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
Definition: coap_session.c:351
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:401
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:534
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:302
void coap_session_mfree(coap_session_t *session)
Definition: coap_session.c:228
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:424
coap_session_type_t
coap_session_type_t values
Definition: coap_session.h:42
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition: coap_session.c:387
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:361
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:154
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:132
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:593
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:126
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:160
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.
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.
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.
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
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
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
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)
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 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