Loading...
Searching...
No Matches
coap_session.c
Go to the documentation of this file.
/* coap_session.c -- Session management for libcoap
*
* Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
* Copyright (C) 2022-2023 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap3/coap_internal.h"
#ifndef COAP_SESSION_C_
#define COAP_SESSION_C_
#include <stdio.h>
#ifdef COAP_EPOLL_SUPPORT
#include <sys/epoll.h>
#include <sys/timerfd.h>
#endif /* COAP_EPOLL_SUPPORT */
coap_fixed_point_t
coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part * fp2.fractional_part;
res.integer_part = fp1.integer_part * fp2.integer_part + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part * u2;
res.integer_part = fp1.integer_part * u2 + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2) {
coap_fixed_point_t res;
uint32_t fr = fp1.fractional_part + fp2.fractional_part;
res.integer_part = fp1.integer_part + fp2.integer_part + fr/1000;
res.fractional_part = fr % 1000;
return res;
}
coap_fixed_point_t
coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res = fp1;
res.integer_part += u2;
return res;
}
coap_fixed_point_t
coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res = fp1;
res.integer_part -= u2;
return res;
}
coap_fixed_point_t
coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2) {
coap_fixed_point_t res;
uint32_t num = (fp1.integer_part * 1000 + fp1.fractional_part) / u2;
res.integer_part = num / 1000;
res.fractional_part = num % 1000;
return res;
}
#if COAP_Q_BLOCK_SUPPORT
coap_fixed_point_t
coap_get_non_timeout_random(coap_session_t *session) {
coap_fixed_point_t res;
uint8_t ran;
coap_prng(&ran, sizeof(ran));
res = coap_sub_fixed_uint(COAP_ACK_RANDOM_FACTOR(session), 1);
res = coap_multi_fixed_uint(res, ran);
res = coap_div_fixed_uint(res, 0xff);
res = coap_add_fixed_fixed(COAP_NON_TIMEOUT(session), res);
return res;
}
coap_tick_t
coap_get_non_timeout_random_ticks(coap_session_t *session) {
coap_fixed_point_t res = coap_get_non_timeout_random(session);
coap_tick_t ticks = (res.integer_part * COAP_TICKS_PER_SECOND +
res.fractional_part * COAP_TICKS_PER_SECOND / 1000);
return ticks;
}
/*
* Save away derived Congestion Control parameters for speed of access.
* They will get updated whenever a component variable is updated.
*/
/*
* NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
* ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM
*
* Do not include NON_TIMEOUT_RANDOM as that changes
*/
static void
coap_session_fix_non_probing_wait_base(coap_session_t *s) {
coap_fixed_point_t res;
res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
COAP_NON_PROBING_WAIT_BASE(s) = res;
}
/*
* NON_PARTIAL_TIMEOUT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
* ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT
*/
static void
coap_session_fix_non_partial_timeout(coap_session_t *s) {
coap_fixed_point_t res;
res = coap_multi_fixed_uint(COAP_NON_TIMEOUT(s),
((1 << (COAP_NON_MAX_RETRANSMIT(s) + 1)) -1));
res = coap_multi_fixed_fixed(res, COAP_ACK_RANDOM_FACTOR(s));
res = coap_add_fixed_uint(res, 2 * COAP_DEFAULT_MAX_LATENCY);
res = coap_add_fixed_fixed(res, COAP_NON_TIMEOUT(s));
COAP_NON_PARTIAL_TIMEOUT(s) = res;
}
#endif /* COAP_Q_BLOCK_SUPPORT */
void
coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->ack_timeout = value;
coap_log_debug("***%s: session ack_timeout set to %u.%03u\n",
coap_session_str(session), session->ack_timeout.integer_part,
session->ack_timeout.fractional_part);
}
}
void
coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->ack_random_factor = value;
coap_log_debug("***%s: session ack_random_factor set to %u.%03u\n",
coap_session_str(session), session->ack_random_factor.integer_part,
session->ack_random_factor.fractional_part);
#if COAP_Q_BLOCK_SUPPORT
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
#endif /* COAP_Q_BLOCK_SUPPORT */
}
return;
}
void
coap_session_set_max_retransmit(coap_session_t *session, uint16_t value) {
if (value > 0) {
session->max_retransmit = value;
coap_log_debug("***%s: session max_retransmit set to %u\n",
coap_session_str(session), session->max_retransmit);
}
}
void
coap_session_set_nstart(coap_session_t *session, uint16_t value) {
if (value > 0) {
session->nstart = value;
coap_log_debug("***%s: session nstart set to %u\n",
coap_session_str(session), session->nstart);
}
}
void
coap_session_set_default_leisure(coap_session_t *session,
coap_fixed_point_t value) {
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->default_leisure = value;
coap_log_debug("***%s: session default_leisure set to %u.%03u\n",
coap_session_str(session), session->default_leisure.integer_part,
session->default_leisure.fractional_part);
}
}
void
coap_session_set_probing_rate(coap_session_t *session, uint32_t value) {
if (value > 0) {
session->probing_rate = value;
coap_log_debug("***%s: session probing_rate set to %" PRIu32 "\n",
coap_session_str(session), session->probing_rate);
}
}
void
coap_session_set_max_payloads(coap_session_t *session, uint16_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value > 0) {
session->max_payloads = value;
coap_log_debug("***%s: session max_payloads set to %u\n",
coap_session_str(session), session->max_payloads);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value > 0) {
session->non_max_retransmit = value;
coap_log_debug("***%s: session non_max_retransmit set to %u\n",
coap_session_str(session), session->non_max_retransmit);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_timeout(coap_session_t *session,
coap_fixed_point_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value.integer_part > 0 && value.fractional_part < 1000) {
session->non_timeout = value;
coap_log_debug("***%s: session non_timeout set to %u.%03u\n",
coap_session_str(session), session->non_timeout.integer_part,
session->non_timeout.fractional_part);
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
}
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
void
coap_session_set_non_receive_timeout(coap_session_t *session,
coap_fixed_point_t value) {
#if COAP_Q_BLOCK_SUPPORT
if (value.integer_part > 0 && value.fractional_part < 1000)
session->non_receive_timeout = value;
coap_log_debug("***%s: session non_receive_timeout set to %u.%03u\n",
coap_session_str(session),
session->non_receive_timeout.integer_part,
session->non_receive_timeout.fractional_part);
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
(void)value;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_ack_timeout(const coap_session_t *session) {
return session->ack_timeout;
}
coap_fixed_point_t
coap_session_get_ack_random_factor(const coap_session_t *session) {
return session->ack_random_factor;
}
uint16_t
coap_session_get_max_retransmit(const coap_session_t *session) {
return session->max_retransmit;
}
uint16_t
coap_session_get_nstart(const coap_session_t *session) {
return session->nstart;
}
coap_fixed_point_t
coap_session_get_default_leisure(const coap_session_t *session) {
return session->default_leisure;
}
uint32_t
coap_session_get_probing_rate(const coap_session_t *session) {
return session->probing_rate;
}
uint16_t
coap_session_get_max_payloads(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->max_payloads;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_MAX_PAYLOADS;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
uint16_t
coap_session_get_non_max_retransmit(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_max_retransmit;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_MAX_RETRANSMIT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_non_timeout(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_timeout;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_TIMEOUT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_fixed_point_t
coap_session_get_non_receive_timeout(const coap_session_t *session) {
#if COAP_Q_BLOCK_SUPPORT
return session->non_receive_timeout;
#else /* ! COAP_Q_BLOCK_SUPPORT */
(void)session;
return COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
#endif /* ! COAP_Q_BLOCK_SUPPORT */
}
coap_session_t *
coap_session_reference(coap_session_t *session) {
++session->ref;
return session;
}
void
coap_session_release(coap_session_t *session) {
if (session) {
#ifndef __COVERITY__
assert(session->ref > 0);
if (session->ref > 0)
--session->ref;
if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT)
coap_session_free(session);
#else /* __COVERITY__ */
/* Coverity scan is fooled by the reference counter leading to
* false positives for USE_AFTER_FREE. */
--session->ref;
__coverity_negative_sink__(session->ref);
/* Indicate that resources are released properly. */
if (session->ref == 0 && session->type == COAP_SESSION_TYPE_CLIENT) {
__coverity_free__(session);
}
#endif /* __COVERITY__ */
}
}
void
coap_session_set_app_data(coap_session_t *session, void *app_data) {
assert(session);
session->app = app_data;
}
void *
coap_session_get_app_data(const coap_session_t *session) {
assert(session);
return session->app;
}
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) {
coap_session_t *session = (coap_session_t *)coap_malloc_type(COAP_SESSION,
sizeof(coap_session_t));
#if ! COAP_SERVER_SUPPORT
(void)endpoint;
#endif /* ! COAP_SERVER_SUPPORT */
if (!session)
return NULL;
memset(session, 0, sizeof(*session));
session->proto = proto;
session->type = type;
if (addr_hash)
memcpy(&session->addr_hash, addr_hash, sizeof(session->addr_hash));
else
memset(&session->addr_hash, 0, sizeof(session->addr_hash));
if (local_addr)
coap_address_copy(&session->addr_info.local, local_addr);
else
coap_address_init(&session->addr_info.local);
if (remote_addr)
coap_address_copy(&session->addr_info.remote, remote_addr);
else
coap_address_init(&session->addr_info.remote);
session->ifindex = ifindex;
session->context = context;
#if COAP_SERVER_SUPPORT
session->endpoint = endpoint;
if (endpoint)
session->mtu = endpoint->default_mtu;
else
#endif /* COAP_SERVER_SUPPORT */
session->mtu = COAP_DEFAULT_MTU;
session->block_mode = context->block_mode;
if (proto == COAP_PROTO_DTLS) {
session->tls_overhead = 29;
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
session->ack_timeout = COAP_DEFAULT_ACK_TIMEOUT;
session->ack_random_factor = COAP_DEFAULT_ACK_RANDOM_FACTOR;
session->max_retransmit = COAP_DEFAULT_MAX_RETRANSMIT;
session->nstart = COAP_DEFAULT_NSTART;
session->default_leisure = COAP_DEFAULT_DEFAULT_LEISURE;
session->probing_rate = COAP_DEFAULT_PROBING_RATE;
#if COAP_Q_BLOCK_SUPPORT
session->max_payloads = COAP_DEFAULT_MAX_PAYLOADS;
session->non_max_retransmit = COAP_DEFAULT_NON_MAX_RETRANSMIT;
session->non_timeout = COAP_DEFAULT_NON_TIMEOUT;
session->non_receive_timeout = COAP_DEFAULT_NON_RECEIVE_TIMEOUT;
coap_session_fix_non_probing_wait_base(session);
coap_session_fix_non_partial_timeout(session);
#endif /* COAP_Q_BLOCK_SUPPORT */
session->dtls_event = -1;
session->last_ping_mid = COAP_INVALID_MID;
session->last_ack_mid = COAP_INVALID_MID;
session->last_con_mid = COAP_INVALID_MID;
session->max_token_size = context->max_token_size; /* RFC8974 */
if (session->type != COAP_SESSION_TYPE_CLIENT)
session->max_token_checked = COAP_EXT_T_CHECKED;
/* Randomly initialize */
/* TCP/TLS have no notion of mid */
if (COAP_PROTO_NOT_RELIABLE(session->proto))
coap_prng((unsigned char *)&session->tx_mid, sizeof(session->tx_mid));
coap_prng((unsigned char *)&session->tx_rtag, sizeof(session->tx_rtag));
return session;
}
void
coap_session_mfree(coap_session_t *session) {
coap_queue_t *q, *tmp;
coap_lg_xmit_t *lq, *ltmp;
#if COAP_CLIENT_SUPPORT
coap_lg_crcv_t *lg_crcv, *etmp;
/* Need to do this before (D)TLS and socket is closed down */
LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, etmp) {
if (lg_crcv->observe_set && session->no_observe_cancel == 0) {
/* Need to close down observe */
if (coap_cancel_observe(session, lg_crcv->app_token, COAP_MESSAGE_NON)) {
/* Need to delete node we set up for NON */
coap_queue_t *queue = session->context->sendqueue;
while (queue) {
if (queue->session == session) {
coap_delete_node(queue);
break;
}
queue = queue->next;
}
}
}
LL_DELETE(session->lg_crcv, lg_crcv);
coap_block_delete_lg_crcv(session, lg_crcv);
}
#endif /* COAP_CLIENT_SUPPORT */
if (session->partial_pdu)
coap_delete_pdu(session->partial_pdu);
session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
if (session->psk_identity)
coap_delete_bin_const(session->psk_identity);
if (session->psk_key)
coap_delete_bin_const(session->psk_key);
if (session->psk_hint)
coap_delete_bin_const(session->psk_hint);
#if COAP_SERVER_SUPPORT
coap_cache_entry_t *cp, *ctmp;
HASH_ITER(hh, session->context->cache, cp, ctmp) {
/* cp->session is NULL if not session based */
if (cp->session == session) {
coap_delete_cache_entry(session->context, cp);
}
}
#endif /* COAP_SERVER_SUPPORT */
LL_FOREACH_SAFE(session->delayqueue, q, tmp) {
if (q->pdu->type==COAP_MESSAGE_CON && session->context &&
session->context->nack_handler) {
coap_check_update_token(session, q->pdu);
session->context->nack_handler(session, q->pdu,
session->proto == COAP_PROTO_DTLS ?
COAP_NACK_TLS_FAILED : COAP_NACK_NOT_DELIVERABLE,
q->id);
}
coap_delete_node(q);
}
LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
LL_DELETE(session->lg_xmit, lq);
coap_block_delete_lg_xmit(session, lq);
}
#if COAP_SERVER_SUPPORT
coap_lg_srcv_t *sq, *stmp;
LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
LL_DELETE(session->lg_srcv, sq);
coap_block_delete_lg_srcv(session, sq);
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_OSCORE_SUPPORT
coap_delete_oscore_associations(session);
#endif /* COAP_OSCORE_SUPPORT */
#if COAP_WS_SUPPORT
coap_free_type(COAP_STRING, session->ws);
coap_delete_str_const(session->ws_host);
#endif /* COAP_WS_SUPPORT */
}
void
coap_session_free(coap_session_t *session) {
if (!session)
return;
assert(session->ref == 0);
if (session->ref)
return;
/* Make sure nothing gets deleted under our feet */
coap_session_reference(session);
coap_session_mfree(session);
#if COAP_SERVER_SUPPORT
if (session->endpoint) {
if (session->endpoint->sessions)
SESSIONS_DELETE(session->endpoint->sessions, session);
} else
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
if (session->context) {
if (session->context->sessions)
SESSIONS_DELETE(session->context->sessions, session);
}
#endif /* COAP_CLIENT_SUPPORT */
coap_delete_bin_const(session->last_token);
coap_log_debug("***%s: session %p: closed\n", coap_session_str(session),
(void *)session);
assert(session->ref == 1);
coap_free_type(COAP_SESSION, session);
}
static size_t
coap_session_max_pdu_size_internal(const coap_session_t *session,
size_t max_with_header) {
#if COAP_DISABLE_TCP
(void)session;
return max_with_header > 4 ? max_with_header - 4 : 0;
#else /* !COAP_DISABLE_TCP */
if (COAP_PROTO_NOT_RELIABLE(session->proto))
return max_with_header > 4 ? max_with_header - 4 : 0;
/* we must assume there is no token to be on the safe side */
if (max_with_header <= 2)
return 0;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP0 + 2)
return max_with_header - 2;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP8 + 3)
return max_with_header - 3;
else if (max_with_header <= COAP_MAX_MESSAGE_SIZE_TCP16 + 4)
return max_with_header - 4;
else
return max_with_header - 6;
#endif /* !COAP_DISABLE_TCP */
}
size_t
coap_session_max_pdu_rcv_size(const coap_session_t *session) {
if (session->csm_rcv_mtu)
return coap_session_max_pdu_size_internal(session,
(size_t)(session->csm_rcv_mtu));
return coap_session_max_pdu_size_internal(session,
(size_t)(session->mtu - session->tls_overhead));
}
size_t
coap_session_max_pdu_size(const coap_session_t *session) {
size_t max_with_header;
#if COAP_CLIENT_SUPPORT
/*
* Delay if session->doing_first is set.
* E.g. Reliable and CSM not in yet for checking block support
*/
coap_session_t *session_rw;
/*
* Need to do this to not get a compiler warning about const parameters
* but need to maintain source code backward compatibility
*/
memcpy(&session_rw, &session, sizeof(session_rw));
if (coap_client_delay_first(session_rw) == 0) {
coap_log_debug("coap_client_delay_first: timeout\n");
/* Have to go with the defaults */
}
#endif /* COAP_CLIENT_SUPPORT */
max_with_header = (size_t)(session->mtu - session->tls_overhead);
return coap_session_max_pdu_size_internal(session, max_with_header);
}
void
coap_session_set_mtu(coap_session_t *session, unsigned mtu) {
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
#endif
if (mtu < 64)
mtu = 64;
session->mtu = mtu;
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
ssize_t
coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
coap_queue_t *node) {
if (node) {
coap_queue_t *removed = NULL;
coap_remove_from_queue(&session->context->sendqueue, session, node->id, &removed);
assert(removed == node);
coap_session_release(node->session);
node->session = NULL;
node->t = 0;
} else {
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
coap_queue_t *q = NULL;
/* Check same mid is not getting re-used in violation of RFC7252 */
LL_FOREACH(session->delayqueue, q) {
if (q->id == pdu->mid) {
coap_log_err("** %s: mid=0x%04x: already in-use - dropped\n",
coap_session_str(session), pdu->mid);
return COAP_INVALID_MID;
}
}
}
node = coap_new_node();
if (node == NULL)
return COAP_INVALID_MID;
node->id = pdu->mid;
node->pdu = pdu;
if (pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
uint8_t r;
coap_prng(&r, sizeof(r));
/* add timeout in range [ACK_TIMEOUT...ACK_TIMEOUT * ACK_RANDOM_FACTOR] */
node->timeout = coap_calc_timeout(session, r);
}
}
LL_APPEND(session->delayqueue, node);
coap_log_debug("** %s: mid=0x%04x: delayed\n",
coap_session_str(session), node->id);
return COAP_PDU_DELAYED;
}
#if !COAP_DISABLE_TCP
void
coap_session_send_csm(coap_session_t *session) {
coap_pdu_t *pdu;
uint8_t buf[4];
assert(COAP_PROTO_RELIABLE(session->proto));
coap_log_debug("***%s: sending CSM\n", coap_session_str(session));
session->state = COAP_SESSION_STATE_CSM;
session->partial_write = 0;
if (session->mtu == 0)
session->mtu = COAP_DEFAULT_MTU; /* base value */
pdu = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_CSM, 0, 20);
if (pdu == NULL
|| coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE,
coap_encode_var_safe(buf, sizeof(buf),
session->context->csm_max_message_size), buf) == 0
|| coap_add_option_internal(pdu, COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER,
coap_encode_var_safe(buf, sizeof(buf),
0), buf) == 0
|| (session->max_token_size > COAP_TOKEN_DEFAULT_MAX &&
coap_add_option_internal(pdu,
COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH,
coap_encode_var_safe(buf, sizeof(buf),
session->max_token_size),
buf) == 0)
|| coap_pdu_encode_header(pdu, session->proto) == 0
) {
coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
} else {
ssize_t bytes_written;
pdu->session = session;
bytes_written = coap_session_send_pdu(session, pdu);
if (bytes_written != (ssize_t)pdu->used_size + pdu->hdr_size) {
coap_session_disconnected(session, COAP_NACK_NOT_DELIVERABLE);
} else {
session->csm_rcv_mtu = session->context->csm_max_message_size;
if (session->csm_rcv_mtu > COAP_BERT_BASE)
session->csm_bert_loc_support = 1;
else
session->csm_bert_loc_support = 0;
}
}
if (pdu)
coap_delete_pdu(pdu);
}
#endif /* !COAP_DISABLE_TCP */
coap_mid_t
coap_session_send_ping(coap_session_t *session) {
coap_pdu_t *ping = NULL;
if (session->state != COAP_SESSION_STATE_ESTABLISHED ||
session->con_active)
return COAP_INVALID_MID;
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
uint16_t mid = coap_new_message_id(session);
ping = coap_pdu_init(COAP_MESSAGE_CON, 0, mid, 0);
}
#if !COAP_DISABLE_TCP
else {
ping = coap_pdu_init(COAP_MESSAGE_CON, COAP_SIGNALING_CODE_PING, 0, 1);
}
#endif /* !COAP_DISABLE_TCP */
if (!ping)
return COAP_INVALID_MID;
return coap_send_internal(session, ping);
}
void
coap_session_connected(coap_session_t *session) {
if (session->state != COAP_SESSION_STATE_ESTABLISHED) {
coap_log_debug("***%s: session connected\n",
coap_session_str(session));
if (session->state == COAP_SESSION_STATE_CSM) {
coap_handle_event(session->context, COAP_EVENT_SESSION_CONNECTED, session);
if (session->doing_first)
session->doing_first = 0;
}
}
session->state = COAP_SESSION_STATE_ESTABLISHED;
session->partial_write = 0;
if (session->proto==COAP_PROTO_DTLS) {
session->tls_overhead = coap_dtls_get_overhead(session);
if (session->tls_overhead >= session->mtu) {
session->tls_overhead = session->mtu;
coap_log_err("DTLS overhead exceeds MTU\n");
}
}
while (session->delayqueue && session->state == COAP_SESSION_STATE_ESTABLISHED) {
ssize_t bytes_written;
coap_queue_t *q = session->delayqueue;
if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (session->con_active >= COAP_NSTART(session))
break;
session->con_active++;
}
/* Take entry off the queue */
session->delayqueue = q->next;
q->next = NULL;
coap_log_debug("** %s: mid=0x%04x: transmitted after delay\n",
coap_session_str(session), (int)q->pdu->mid);
bytes_written = coap_session_send_pdu(session, q->pdu);
if (q->pdu->type == COAP_MESSAGE_CON && COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (coap_wait_ack(session->context, session, q) >= 0)
q = NULL;
}
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
if (q)
coap_delete_node(q);
if (bytes_written < 0)
break;
} else {
if (bytes_written <= 0 || (size_t)bytes_written < q->pdu->used_size + q->pdu->hdr_size) {
q->next = session->delayqueue;
session->delayqueue = q;
if (bytes_written > 0)
session->partial_write = (size_t)bytes_written;
break;
} else {
coap_delete_node(q);
}
}
}
}
#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
static const char *
coap_nack_name(coap_nack_reason_t reason) {
switch (reason) {
case COAP_NACK_TOO_MANY_RETRIES:
return "COAP_NACK_TOO_MANY_RETRIES";
case COAP_NACK_NOT_DELIVERABLE:
return "COAP_NACK_NOT_DELIVERABLE";
case COAP_NACK_RST:
return "COAP_NACK_RST";
case COAP_NACK_TLS_FAILED:
return "COAP_NACK_TLS_FAILED";
case COAP_NACK_ICMP_ISSUE:
return "COAP_NACK_ICMP_ISSUE";
case COAP_NACK_BAD_RESPONSE:
return "COAP_NACK_BAD_RESPONSE";
case COAP_NACK_TLS_LAYER_FAILED:
return "COAP_NACK_TLS_LAYER_FAILED";
case COAP_NACK_WS_LAYER_FAILED:
return "COAP_NACK_WS_LAYER_FAILED";
case COAP_NACK_WS_FAILED:
return "COAP_NACK_WS_FAILED";
default:
return "???";
}
}
#endif /* COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG */
void
coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason) {
#if !COAP_DISABLE_TCP
coap_session_state_t state = session->state;
#endif /* !COAP_DISABLE_TCP */
coap_lg_xmit_t *lq, *ltmp;
#if COAP_SERVER_SUPPORT
coap_lg_srcv_t *sq, *stmp;
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
coap_lg_crcv_t *cq, *etmp;
#endif /* COAP_CLIENT_SUPPORT */
if (reason == COAP_NACK_ICMP_ISSUE) {
if (session->context->nack_handler) {
int sent_nack = 0;
coap_queue_t *q = session->context->sendqueue;
while (q) {
if (q->session == session) {
/* Take the first one */
coap_bin_const_t token = q->pdu->actual_token;
coap_check_update_token(session, q->pdu);
session->context->nack_handler(session, q->pdu, reason, q->id);
coap_update_token(q->pdu, token.length, token.s);
sent_nack = 1;
break;
}
q = q->next;
}
#if COAP_CLIENT_SUPPORT
if (!sent_nack && session->lg_crcv) {
/* Take the first one */
session->context->nack_handler(session, &session->lg_crcv->pdu, reason,
session->lg_crcv->pdu.mid);
sent_nack = 1;
}
#endif /* COAP_CLIENT_SUPPORT */
if (!sent_nack) {
/* Unable to determine which request ICMP issue was for */
session->context->nack_handler(session, NULL, reason, 0);
}
}
coap_log_debug("***%s: session issue (%s)\n",
coap_session_str(session), coap_nack_name(reason));
return;
}
coap_log_debug("***%s: session disconnected (%s)\n",
coap_session_str(session), coap_nack_name(reason));
#if COAP_SERVER_SUPPORT
coap_delete_observers(session->context, session);
#endif /* COAP_SERVER_SUPPORT */
if (session->proto == COAP_PROTO_UDP)
session->state = COAP_SESSION_STATE_ESTABLISHED;
else
session->state = COAP_SESSION_STATE_NONE;
session->con_active = 0;
if (session->partial_pdu) {
coap_delete_pdu(session->partial_pdu);
session->partial_pdu = NULL;
}
session->partial_read = 0;
while (session->delayqueue) {
coap_queue_t *q = session->delayqueue;
session->delayqueue = q->next;
q->next = NULL;
coap_log_debug("** %s: mid=0x%04x: not transmitted after disconnect\n",
coap_session_str(session), q->id);
if (q && q->pdu->type == COAP_MESSAGE_CON
&& session->context->nack_handler) {
coap_check_update_token(session, q->pdu);
session->context->nack_handler(session, q->pdu, reason, q->id);
}
if (q)
coap_delete_node(q);
}
#if COAP_CLIENT_SUPPORT
/* Need to do this before (D)TLS and socket is closed down */
LL_FOREACH_SAFE(session->lg_crcv, cq, etmp) {
LL_DELETE(session->lg_crcv, cq);
coap_block_delete_lg_crcv(session, cq);
}
#endif /* COAP_CLIENT_SUPPORT */
LL_FOREACH_SAFE(session->lg_xmit, lq, ltmp) {
LL_DELETE(session->lg_xmit, lq);
coap_block_delete_lg_xmit(session, lq);
}
#if COAP_SERVER_SUPPORT
LL_FOREACH_SAFE(session->lg_srcv, sq, stmp) {
LL_DELETE(session->lg_srcv, sq);
coap_block_delete_lg_srcv(session, sq);
}
#endif /* COAP_SERVER_SUPPORT */
coap_cancel_session_messages(session->context, session, reason);
#if !COAP_DISABLE_TCP
if (COAP_PROTO_RELIABLE(session->proto)) {
if (coap_netif_available(session)) {
coap_handle_event(session->context,
state == COAP_SESSION_STATE_CONNECTING ?
COAP_EVENT_TCP_FAILED : COAP_EVENT_TCP_CLOSED, session);
}
if (state != COAP_SESSION_STATE_NONE) {
coap_handle_event(session->context,
state == COAP_SESSION_STATE_ESTABLISHED ?
COAP_EVENT_SESSION_CLOSED : COAP_EVENT_SESSION_FAILED, session);
}
if (session->doing_first)
session->doing_first = 0;
}
#endif /* !COAP_DISABLE_TCP */
session->sock.lfunc[COAP_LAYER_SESSION].l_close(session);
}
#if COAP_SERVER_SUPPORT
static void
coap_make_addr_hash(coap_addr_hash_t *addr_hash, coap_proto_t proto,
const coap_addr_tuple_t *addr_info) {
memset(addr_hash, 0, sizeof(coap_addr_hash_t));
coap_address_copy(&addr_hash->remote, &addr_info->remote);
addr_hash->lport = coap_address_get_port(&addr_info->local);
addr_hash->proto = proto;
}
coap_session_t *
coap_endpoint_get_session(coap_endpoint_t *endpoint,
const coap_packet_t *packet, coap_tick_t now) {
coap_session_t *session;
coap_session_t *rtmp;
unsigned int num_idle = 0;
unsigned int num_hs = 0;
coap_session_t *oldest = NULL;
coap_session_t *oldest_hs = NULL;
coap_addr_hash_t addr_hash;
coap_make_addr_hash(&addr_hash, endpoint->proto, &packet->addr_info);
SESSIONS_FIND(endpoint->sessions, addr_hash, session);
if (session) {
/* Maybe mcast or unicast IP address which is not in the hash */
coap_address_copy(&session->addr_info.local, &packet->addr_info.local);
session->ifindex = packet->ifindex;
session->last_rx_tx = now;
return session;
}
SESSIONS_ITER(endpoint->sessions, session, rtmp) {
if (session->ref == 0 && session->delayqueue == NULL) {
if (session->type == COAP_SESSION_TYPE_SERVER) {
++num_idle;
if (oldest==NULL || session->last_rx_tx < oldest->last_rx_tx)
oldest = session;
if (session->state == COAP_SESSION_STATE_HANDSHAKE) {
++num_hs;
/* See if this is a partial (D)TLS session set up
which needs to be cleared down to prevent DOS */
if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
if (oldest_hs == NULL ||
session->last_rx_tx < oldest_hs->last_rx_tx)
oldest_hs = session;
}
}
} else if (session->type == COAP_SESSION_TYPE_HELLO) {
++num_hs;
/* See if this is a partial (D)TLS session set up for Client Hello
which needs to be cleared down to prevent DOS */
if ((session->last_rx_tx + COAP_PARTIAL_SESSION_TIMEOUT_TICKS) < now) {
if (oldest_hs == NULL ||
session->last_rx_tx < oldest_hs->last_rx_tx)
oldest_hs = session;
}
}
}
}
if (endpoint->context->max_idle_sessions > 0 &&
num_idle >= endpoint->context->max_idle_sessions) {
coap_handle_event(oldest->context, COAP_EVENT_SERVER_SESSION_DEL, oldest);
coap_session_free(oldest);
} else if (oldest_hs) {
coap_log_warn("***%s: Incomplete session timed out\n",
coap_session_str(oldest_hs));
coap_handle_event(oldest_hs->context, COAP_EVENT_SERVER_SESSION_DEL, oldest_hs);
coap_session_free(oldest_hs);
}
if (num_hs > (endpoint->context->max_handshake_sessions ?
endpoint->context->max_handshake_sessions :
COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS)) {
/* Maxed out on number of sessions in (D)TLS negotiation state */
coap_log_debug("Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
"large. New request ignored\n");
return NULL;
}
if (endpoint->proto == COAP_PROTO_DTLS) {
/*
* Need to check that this actually is a Client Hello before wasting
* time allocating and then freeing off session.
*/
/*
* Generic header structure of the DTLS record layer.
* typedef struct __attribute__((__packed__)) {
* uint8_t content_type; content type of the included message
* uint16_t version; Protocol version
* uint16_t epoch; counter for cipher state changes
* uint8_t sequence_number[6]; sequence number
* uint16_t length; length of the following fragment
* uint8_t handshake; If content_type == DTLS_CT_HANDSHAKE
* } dtls_record_handshake_t;
*/
#define OFF_CONTENT_TYPE 0 /* offset of content_type in dtls_record_handshake_t */
#define DTLS_CT_ALERT 21 /* Content Type Alert */
#define DTLS_CT_HANDSHAKE 22 /* Content Type Handshake */
#define OFF_HANDSHAKE_TYPE 13 /* offset of handshake in dtls_record_handshake_t */
#define DTLS_HT_CLIENT_HELLO 1 /* Client Hello handshake type */
const uint8_t *payload = (const uint8_t *)packet->payload;
size_t length = packet->length;
if (length < (OFF_HANDSHAKE_TYPE + 1)) {
coap_log_debug("coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
payload[OFF_CONTENT_TYPE], length,
OFF_HANDSHAKE_TYPE + 1);
return NULL;
}
if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
/* only log if not a late alert */
if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
coap_log_debug("coap_dtls_hello: ContentType %d Handshake %d dropped\n",
payload[OFF_CONTENT_TYPE], payload[OFF_HANDSHAKE_TYPE]);
return NULL;
}
}
session = coap_make_session(endpoint->proto, COAP_SESSION_TYPE_SERVER,
&addr_hash, &packet->addr_info.local,
&packet->addr_info.remote,
packet->ifindex, endpoint->context, endpoint);
if (session) {
session->last_rx_tx = now;
memcpy(session->sock.lfunc, endpoint->sock.lfunc,
sizeof(session->sock.lfunc));
if (endpoint->proto == COAP_PROTO_UDP)
session->state = COAP_SESSION_STATE_ESTABLISHED;
else if (endpoint->proto == COAP_PROTO_DTLS) {
session->type = COAP_SESSION_TYPE_HELLO;
}
SESSIONS_ADD(endpoint->sessions, session);
coap_log_debug("***%s: session %p: new incoming session\n",
coap_session_str(session), (void *)session);
coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
}
return session;
}
coap_session_t *
coap_session_new_dtls_session(coap_session_t *session,
coap_tick_t now) {
if (session) {
session->last_rx_tx = now;
session->type = COAP_SESSION_TYPE_SERVER;
coap_dtls_establish(session);
}
return session;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
static coap_session_t *
coap_session_create_client(coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto) {
coap_session_t *session = NULL;
int default_port = COAP_DEFAULT_PORT;
assert(server);
switch (proto) {
case COAP_PROTO_UDP:
default_port = COAP_DEFAULT_PORT;
break;
case COAP_PROTO_DTLS:
if (!coap_dtls_is_supported()) {
coap_log_crit("coap_new_client_session*: DTLS not supported\n");
return NULL;
}
default_port = COAPS_DEFAULT_PORT;
break;
case COAP_PROTO_TCP:
if (!coap_tcp_is_supported()) {
coap_log_crit("coap_new_client_session*: TCP not supported\n");
return NULL;
}
default_port = COAP_DEFAULT_PORT;
break;
case COAP_PROTO_TLS:
if (!coap_tls_is_supported()) {
coap_log_crit("coap_new_client_session*: TLS not supported\n");
return NULL;
}
default_port = COAPS_DEFAULT_PORT;
break;
case COAP_PROTO_WS:
if (!coap_ws_is_supported()) {
coap_log_crit("coap_new_client_session*: WS not supported\n");
return NULL;
}
default_port = 80;
break;
case COAP_PROTO_WSS:
if (!coap_wss_is_supported()) {
coap_log_crit("coap_new_client_session*: WSS not supported\n");
return NULL;
}
default_port = 443;
break;
case COAP_PROTO_NONE:
case COAP_PROTO_LAST:
default:
assert(0);
return NULL;
}
session = coap_make_session(proto, COAP_SESSION_TYPE_CLIENT, NULL,
local_if, server, 0, ctx, NULL);
if (!session)
goto error;
coap_session_reference(session);
session->sock.session = session;
memcpy(&session->sock.lfunc, coap_layers_coap[proto],
sizeof(session->sock.lfunc));
if (COAP_PROTO_NOT_RELIABLE(proto)) {
coap_session_t *s, *rtmp;
if (!coap_netif_dgrm_connect(session, local_if, server, default_port)) {
goto error;
}
/* Check that this is not a duplicate 4-tuple */
SESSIONS_ITER_SAFE(ctx->sessions, s, rtmp) {
if (COAP_PROTO_NOT_RELIABLE(s->proto) &&
coap_address_equals(&session->addr_info.local,
&s->addr_info.local) &&
coap_address_equals(&session->addr_info.remote,
&s->addr_info.remote)) {
coap_log_warn("***%s: session %p: duplicate - already exists\n",
coap_session_str(session), (void *)session);
goto error;
}
}
#ifdef WITH_CONTIKI
session->sock.context = ctx;
#endif /* WITH_CONTIKI */
#if !COAP_DISABLE_TCP
} else if (COAP_PROTO_RELIABLE(proto)) {
if (!coap_netif_strm_connect1(session, local_if, server, default_port)) {
goto error;
}
#endif /* !COAP_DISABLE_TCP */
}
#ifdef COAP_EPOLL_SUPPORT
session->sock.session = session;
coap_epoll_ctl_add(&session->sock,
EPOLLIN |
((session->sock.flags & COAP_SOCKET_WANT_CONNECT) ?
EPOLLOUT : 0),
__func__);
#endif /* COAP_EPOLL_SUPPORT */
session->sock.flags |= COAP_SOCKET_NOT_EMPTY | COAP_SOCKET_WANT_READ;
if (local_if)
session->sock.flags |= COAP_SOCKET_BOUND;
#if COAP_SERVER_SUPPORT
if (ctx->proxy_uri_resource)
session->proxy_session = 1;
#endif /* COAP_SERVER_SUPPORT */
SESSIONS_ADD(ctx->sessions, session);
return session;
error:
/*
* Need to add in the session as coap_session_release()
* will call SESSIONS_DELETE in coap_session_free().
*/
if (session)
SESSIONS_ADD(ctx->sessions, session);
coap_session_release(session);
return NULL;
}
static void
coap_session_check_connect(coap_session_t *session) {
if (COAP_PROTO_NOT_RELIABLE(session->proto)) {
session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
}
#if !COAP_DISABLE_TCP
if (COAP_PROTO_RELIABLE(session->proto)) {
if (session->sock.flags & COAP_SOCKET_WANT_CONNECT) {
session->state = COAP_SESSION_STATE_CONNECTING;
if (session->state != COAP_SESSION_STATE_ESTABLISHED &&
session->state != COAP_SESSION_STATE_NONE &&
session->type == COAP_SESSION_TYPE_CLIENT) {
session->doing_first = 1;
}
} else {
/* Initial connect worked immediately */
session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
}
}
#endif /* !COAP_DISABLE_TCP */
coap_ticks(&session->last_rx_tx);
}
#endif /* COAP_CLIENT_SUPPORT */
void
coap_session_establish(coap_session_t *session) {
if (COAP_PROTO_NOT_RELIABLE(session->proto))
coap_session_connected(session);
#if !COAP_DISABLE_TCP
if (COAP_PROTO_RELIABLE(session->proto))
coap_session_send_csm(session);
#endif /* !COAP_DISABLE_TCP */
}
#if COAP_CLIENT_SUPPORT
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) {
coap_session_t *session = coap_session_create_client(ctx, local_if, server,
proto);
if (session) {
coap_log_debug("***%s: session %p: created outgoing session\n",
coap_session_str(session), (void *)session);
coap_session_check_connect(session);
}
return session;
}
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) {
coap_dtls_cpsk_t setup_data;
memset(&setup_data, 0, sizeof(setup_data));
setup_data.version = COAP_DTLS_CPSK_SETUP_VERSION;
if (identity) {
setup_data.psk_info.identity.s = (const uint8_t *)identity;
setup_data.psk_info.identity.length = strlen(identity);
}
if (key && key_len > 0) {
setup_data.psk_info.key.s = key;
setup_data.psk_info.key.length = key_len;
}
return coap_new_client_session_psk2(ctx, local_if, server,
proto, &setup_data);
}
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) {
coap_session_t *session = coap_session_create_client(ctx, local_if,
server, proto);
if (!session)
return NULL;
session->cpsk_setup_data = *setup_data;
if (setup_data->psk_info.identity.s) {
session->psk_identity =
coap_new_bin_const(setup_data->psk_info.identity.s,
setup_data->psk_info.identity.length);
if (!session->psk_identity) {
coap_log_warn("Cannot store session Identity (PSK)\n");
coap_session_release(session);
return NULL;
}
} else if (coap_dtls_is_supported() || coap_tls_is_supported()) {
coap_log_warn("Identity (PSK) not defined\n");
coap_session_release(session);
return NULL;
}
if (setup_data->psk_info.key.s && setup_data->psk_info.key.length > 0) {
session->psk_key = coap_new_bin_const(setup_data->psk_info.key.s,
setup_data->psk_info.key.length);
if (!session->psk_key) {
coap_log_warn("Cannot store session pre-shared key (PSK)\n");
coap_session_release(session);
return NULL;
}
} else if (coap_dtls_is_supported() || coap_tls_is_supported()) {
coap_log_warn("Pre-shared key (PSK) not defined\n");
coap_session_release(session);
return NULL;
}
if (coap_dtls_is_supported() || coap_tls_is_supported()) {
if (!coap_dtls_context_set_cpsk(ctx, setup_data)) {
coap_session_release(session);
return NULL;
}
}
coap_log_debug("***%s: new outgoing session\n",
coap_session_str(session));
coap_session_check_connect(session);
return session;
}
#endif /* COAP_CLIENT_SUPPORT */
int
coap_session_refresh_psk_hint(coap_session_t *session,
const coap_bin_const_t *psk_hint
) {
/* We may be refreshing the hint with the same hint */
coap_bin_const_t *old_psk_hint = session->psk_hint;
if (psk_hint && psk_hint->s) {
if (session->psk_hint) {
if (coap_binary_equal(session->psk_hint, psk_hint))
return 1;
}
session->psk_hint = coap_new_bin_const(psk_hint->s,
psk_hint->length);
if (!session->psk_hint) {
coap_log_err("No memory to store identity hint (PSK)\n");
if (old_psk_hint)
coap_delete_bin_const(old_psk_hint);
return 0;
}
} else {
session->psk_hint = NULL;
}
if (old_psk_hint)
coap_delete_bin_const(old_psk_hint);
return 1;
}
int
coap_session_refresh_psk_key(coap_session_t *session,
const coap_bin_const_t *psk_key
) {
/* We may be refreshing the key with the same key */
coap_bin_const_t *old_psk_key = session->psk_key;
if (psk_key && psk_key->s) {
if (session->psk_key) {
if (coap_binary_equal(session->psk_key, psk_key))
return 1;
}
session->psk_key = coap_new_bin_const(psk_key->s, psk_key->length);
if (!session->psk_key) {
coap_log_err("No memory to store pre-shared key (PSK)\n");
if (old_psk_key)
coap_delete_bin_const(old_psk_key);
return 0;
}
} else {
session->psk_key = NULL;
}
if (old_psk_key)
coap_delete_bin_const(old_psk_key);
return 1;
}
int
coap_session_refresh_psk_identity(coap_session_t *session,
const coap_bin_const_t *psk_identity
) {
/* We may be refreshing the identity with the same identity */
coap_bin_const_t *old_psk_identity = session->psk_identity;
if (psk_identity && psk_identity->s) {
if (session->psk_identity) {
if (coap_binary_equal(session->psk_identity, psk_identity))
return 1;
}
session->psk_identity = coap_new_bin_const(psk_identity->s,
psk_identity->length);
if (!session->psk_identity) {
coap_log_err("No memory to store pre-shared key identity (PSK)\n");
if (old_psk_identity)
coap_delete_bin_const(old_psk_identity);
return 0;
}
} else {
session->psk_identity = NULL;
}
if (old_psk_identity)
coap_delete_bin_const(old_psk_identity);
return 1;
}
#if COAP_SERVER_SUPPORT
const coap_bin_const_t *
coap_session_get_psk_hint(const coap_session_t *session) {
if (session)
return session->psk_hint;
return NULL;
}
#endif /* COAP_SERVER_SUPPORT */
const coap_bin_const_t *
coap_session_get_psk_identity(const coap_session_t *session) {
const coap_bin_const_t *psk_identity = NULL;
if (session) {
psk_identity = session->psk_identity;
if (psk_identity == NULL) {
psk_identity = &session->cpsk_setup_data.psk_info.identity;
}
}
return psk_identity;
}
const coap_bin_const_t *
coap_session_get_psk_key(const coap_session_t *session) {
if (session)
return session->psk_key;
return NULL;
}
#if COAP_CLIENT_SUPPORT
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) {
coap_session_t *session;
if (coap_dtls_is_supported() || coap_tls_is_supported()) {
if (!setup_data) {
return NULL;
} else {
if (setup_data->version != COAP_DTLS_PKI_SETUP_VERSION) {
coap_log_err("coap_new_client_session_pki: Wrong version of setup_data\n");
return NULL;
}
}
}
session = coap_session_create_client(ctx, local_if, server, proto);
if (!session) {
return NULL;
}
if (coap_dtls_is_supported() || coap_tls_is_supported()) {
/* we know that setup_data is not NULL */
if (!coap_dtls_context_set_pki(ctx, setup_data, COAP_DTLS_ROLE_CLIENT)) {
coap_session_release(session);
return NULL;
}
}
coap_log_debug("***%s: new outgoing session\n",
coap_session_str(session));
coap_session_check_connect(session);
return session;
}
#endif /* ! COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
#if !COAP_DISABLE_TCP
coap_session_t *
coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep) {
coap_session_t *session;
session = coap_make_session(ep->proto, COAP_SESSION_TYPE_SERVER,
NULL, NULL, NULL, 0, ctx, ep);
if (!session)
goto error;
memcpy(session->sock.lfunc, ep->sock.lfunc, sizeof(session->sock.lfunc));
if (!coap_netif_strm_accept(ep, session))
goto error;
coap_make_addr_hash(&session->addr_hash, session->proto, &session->addr_info);
#ifdef COAP_EPOLL_SUPPORT
session->sock.session = session;
coap_epoll_ctl_add(&session->sock,
EPOLLIN,
__func__);
#endif /* COAP_EPOLL_SUPPORT */
SESSIONS_ADD(ep->sessions, session);
if (session) {
coap_log_debug("***%s: session %p: new incoming session\n",
coap_session_str(session), (void *)session);
coap_handle_event(session->context, COAP_EVENT_TCP_CONNECTED, session);
coap_handle_event(session->context, COAP_EVENT_SERVER_SESSION_NEW, session);
session->state = COAP_SESSION_STATE_CONNECTING;
session->sock.lfunc[COAP_LAYER_SESSION].l_establish(session);
}
return session;
error:
/*
* Need to add in the session as coap_session_release()
* will call SESSIONS_DELETE in coap_session_free().
*/
if (session) {
SESSIONS_ADD(ep->sessions, session);
coap_session_free(session);
}
return NULL;
}
#endif /* !COAP_DISABLE_TCP */
#endif /* COAP_SERVER_SUPPORT */
void
coap_session_init_token(coap_session_t *session, size_t len,
const uint8_t *data) {
session->tx_token = coap_decode_var_bytes8(data, len);
}
void
coap_session_new_token(coap_session_t *session, size_t *len,
uint8_t *data) {
*len = coap_encode_var_safe8(data,
sizeof(session->tx_token), ++session->tx_token);
}
uint16_t
coap_new_message_id(coap_session_t *session) {
if (COAP_PROTO_NOT_RELIABLE(session->proto))
return ++session->tx_mid;
/* TCP/TLS have no notion of mid */
return 0;
}
const coap_address_t *
coap_session_get_addr_remote(const coap_session_t *session) {
if (session)
return &session->addr_info.remote;
return NULL;
}
const coap_address_t *
coap_session_get_addr_local(const coap_session_t *session) {
if (session)
return &session->addr_info.local;
return NULL;
}
const coap_address_t *
coap_session_get_addr_mcast(const coap_session_t *session) {
#if COAP_CLIENT_SUPPORT
if (session && session->type == COAP_SESSION_TYPE_CLIENT &&
session->sock.flags & COAP_SOCKET_MULTICAST)
return &session->sock.mcast_addr;
#else /* ! COAP_CLIENT_SUPPORT */
(void)session;
#endif /* ! COAP_CLIENT_SUPPORT */
return NULL;
}
coap_context_t *
coap_session_get_context(const coap_session_t *session) {
if (session)
return session->context;
return NULL;
}
coap_proto_t
coap_session_get_proto(const coap_session_t *session) {
if (session)
return session->proto;
return 0;
}
coap_session_type_t
coap_session_get_type(const coap_session_t *session) {
if (session)
return session->type;
return 0;
}
#if COAP_CLIENT_SUPPORT
int
coap_session_set_type_client(coap_session_t *session) {
#if COAP_SERVER_SUPPORT
if (session && session->type == COAP_SESSION_TYPE_SERVER) {
coap_session_reference(session);
session->type = COAP_SESSION_TYPE_CLIENT;
return 1;
}
#else /* ! COAP_SERVER_SUPPORT */
(void)session;
#endif /* ! COAP_SERVER_SUPPORT */
return 0;
}
#endif /* COAP_CLIENT_SUPPORT */
coap_session_state_t
coap_session_get_state(const coap_session_t *session) {
if (session)
return session->state;
return 0;
}
int
coap_session_get_ifindex(const coap_session_t *session) {
if (session)
return session->ifindex;
return -1;
}
void *
coap_session_get_tls(const coap_session_t *session,
coap_tls_library_t *tls_lib) {
if (session)
return coap_dtls_get_tls(session, tls_lib);
return NULL;
}
static const char *
coap_proto_name(coap_proto_t proto) {
switch (proto) {
case COAP_PROTO_UDP:
return "UDP ";
case COAP_PROTO_DTLS:
return "DTLS";
case COAP_PROTO_TCP:
return "TCP ";
case COAP_PROTO_TLS:
return "TLS ";
case COAP_PROTO_WS:
return "WS ";
case COAP_PROTO_WSS:
return "WSS ";
case COAP_PROTO_NONE:
case COAP_PROTO_LAST:
default:
return "????" ;
break;
}
return NULL;
}
#if COAP_SERVER_SUPPORT
coap_endpoint_t *
coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto) {
coap_endpoint_t *ep = NULL;
assert(context);
assert(listen_addr);
assert(proto != COAP_PROTO_NONE);
if (proto == COAP_PROTO_DTLS && !coap_dtls_is_supported()) {
coap_log_crit("coap_new_endpoint: DTLS not supported\n");
goto error;
}
if (proto == COAP_PROTO_TLS && !coap_tls_is_supported()) {
coap_log_crit("coap_new_endpoint: TLS not supported\n");
goto error;
}
if (proto == COAP_PROTO_TCP && !coap_tcp_is_supported()) {
coap_log_crit("coap_new_endpoint: TCP not supported\n");
goto error;
}
if (proto == COAP_PROTO_WS && !coap_ws_is_supported()) {
coap_log_crit("coap_new_endpoint: WS not supported\n");
goto error;
}
if (proto == COAP_PROTO_WSS && !coap_wss_is_supported()) {
coap_log_crit("coap_new_endpoint: WSS not supported\n");
goto error;
}
if (proto == COAP_PROTO_DTLS || proto == COAP_PROTO_TLS ||
proto == COAP_PROTO_WSS) {
if (!coap_dtls_context_check_keys_enabled(context)) {
coap_log_info("coap_new_endpoint: one of coap_context_set_psk() or "
"coap_context_set_pki() not called\n");
goto error;
}
}
ep = coap_malloc_endpoint();
if (!ep) {
coap_log_warn("coap_new_endpoint: malloc");
goto error;
}
memset(ep, 0, sizeof(coap_endpoint_t));
ep->context = context;
ep->proto = proto;
ep->sock.endpoint = ep;
assert(proto < COAP_PROTO_LAST);
memcpy(&ep->sock.lfunc, coap_layers_coap[proto], sizeof(ep->sock.lfunc));
if (COAP_PROTO_NOT_RELIABLE(proto)) {
if (!coap_netif_dgrm_listen(ep, listen_addr))
goto error;
#ifdef WITH_CONTIKI
ep->sock.context = context;
#endif /* WITH_CONTIKI */
#if !COAP_DISABLE_TCP
} else if (COAP_PROTO_RELIABLE(proto)) {
if (!coap_netif_strm_listen(ep, listen_addr))
goto error;
#endif /* !COAP_DISABLE_TCP */
} else {
coap_log_crit("coap_new_endpoint: protocol not supported\n");
goto error;
}
if (COAP_LOG_DEBUG <= coap_get_log_level()) {
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 40
#endif
unsigned char addr_str[INET6_ADDRSTRLEN + 8];
if (coap_print_addr(&ep->bind_addr, addr_str, INET6_ADDRSTRLEN + 8)) {
coap_log_debug("created %s endpoint %s\n", coap_proto_name(ep->proto),
addr_str);
}
}
ep->default_mtu = COAP_DEFAULT_MTU;
#ifdef COAP_EPOLL_SUPPORT
ep->sock.endpoint = ep;
coap_epoll_ctl_add(&ep->sock,
EPOLLIN,
__func__);
#endif /* COAP_EPOLL_SUPPORT */
LL_PREPEND(context->endpoint, ep);
return ep;
error:
coap_free_endpoint(ep);
return NULL;
}
void
coap_endpoint_set_default_mtu(coap_endpoint_t *ep, unsigned mtu) {
ep->default_mtu = (uint16_t)mtu;
}
void
coap_free_endpoint(coap_endpoint_t *ep) {
if (ep) {
coap_session_t *session, *rtmp;
SESSIONS_ITER_SAFE(ep->sessions, session, rtmp) {
assert(session->ref == 0);
if (session->ref == 0) {
coap_session_free(session);
}
}
if (coap_netif_available_ep(ep)) {
/*
* ep->sock.endpoint is set in coap_new_endpoint().
* ep->sock.session is never set.
*
* session->sock.session is set for both clients and servers (when a
* new session is accepted), but does not affect the endpoint.
*
* So, it is safe to call coap_netif_close_ep() after all the sessions
* have been freed above as we are only working with the endpoint sock.
*/
#ifdef COAP_EPOLL_SUPPORT
assert(ep->sock.session == NULL);
#endif /* COAP_EPOLL_SUPPORT */
coap_netif_close_ep(ep);
}
if (ep->context && ep->context->endpoint) {
LL_DELETE(ep->context->endpoint, ep);
}
coap_mfree_endpoint(ep);
}
}
#endif /* COAP_SERVER_SUPPORT */
coap_session_t *
coap_session_get_by_peer(const coap_context_t *ctx,
const coap_address_t *remote_addr,
int ifindex) {
coap_session_t *s, *rtmp;
#if COAP_CLIENT_SUPPORT
SESSIONS_ITER(ctx->sessions, s, rtmp) {
if (s->ifindex == ifindex) {
if (s->sock.flags & COAP_SOCKET_MULTICAST) {
if (coap_address_equals(&s->sock.mcast_addr, remote_addr))
return s;
} else if (coap_address_equals(&s->addr_info.remote, remote_addr))
return s;
}
}
#endif /* COAP_CLIENT_SUPPORT */
#if COAP_SERVER_SUPPORT
coap_endpoint_t *ep;
LL_FOREACH(ctx->endpoint, ep) {
SESSIONS_ITER(ep->sessions, s, rtmp) {
if (s->ifindex == ifindex && coap_address_equals(&s->addr_info.remote,
remote_addr))
return s;
}
}
#endif /* COAP_SERVER_SUPPORT */
return NULL;
}
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 46
#endif
const char *
coap_session_str(const coap_session_t *session) {
static char szSession[2 * (INET6_ADDRSTRLEN + 8) + 24];
char *p = szSession, *end = szSession + sizeof(szSession);
if (coap_print_addr(&session->addr_info.local,
(unsigned char *)p, end - p) > 0)
p += strlen(p);
if (p + 6 < end) {
strcpy(p, " <-> ");
p += 5;
}
if (p + 1 < end) {
if (coap_print_addr(&session->addr_info.remote,
(unsigned char *)p, end - p) > 0)
p += strlen(p);
}
if (session->ifindex > 0 && p + 1 < end)
p += snprintf(p, end - p, " (if%d)", session->ifindex);
if (p + 6 < end) {
strcpy(p, " ");
p++;
strcpy(p, coap_proto_name(session->proto));
}
return szSession;
}
#if COAP_SERVER_SUPPORT
const char *
coap_endpoint_str(const coap_endpoint_t *endpoint) {
static char szEndpoint[128];
char *p = szEndpoint, *end = szEndpoint + sizeof(szEndpoint);
if (coap_print_addr(&endpoint->bind_addr, (unsigned char *)p, end - p) > 0)
p += strlen(p);
if (p + 6 < end) {
if (endpoint->proto == COAP_PROTO_UDP) {
strcpy(p, " UDP");
p += 4;
} else if (endpoint->proto == COAP_PROTO_DTLS) {
strcpy(p, " DTLS");
p += 5;
} else {
strcpy(p, " NONE");
p += 5;
}
}
return szEndpoint;
}
#endif /* COAP_SERVER_SUPPORT */
#if COAP_CLIENT_SUPPORT
void
coap_session_set_no_observe_cancel(coap_session_t *session) {
session->no_observe_cancel = 1;
}
#endif /* COAP_CLIENT_SUPPORT */
#endif /* COAP_SESSION_C_ */
coap_address_init
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
Definition coap_address.c:243
coap_address_get_port
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
Definition coap_address.c:44
coap_address_copy
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
Definition coap_address.c:798
coap_address_equals
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:81
coap_internal.h
Pulls together all the internal only header files.
PRIu32
#define PRIu32
Definition coap_internal.h:45
coap_nack_reason_t
coap_nack_reason_t
Definition coap_io.h:69
COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_NOT_DELIVERABLE
Definition coap_io.h:71
COAP_NACK_WS_FAILED
@ COAP_NACK_WS_FAILED
Definition coap_io.h:78
COAP_NACK_TOO_MANY_RETRIES
@ COAP_NACK_TOO_MANY_RETRIES
Definition coap_io.h:70
COAP_NACK_TLS_FAILED
@ COAP_NACK_TLS_FAILED
Definition coap_io.h:73
COAP_NACK_TLS_LAYER_FAILED
@ COAP_NACK_TLS_LAYER_FAILED
Definition coap_io.h:76
COAP_NACK_ICMP_ISSUE
@ COAP_NACK_ICMP_ISSUE
Definition coap_io.h:74
COAP_NACK_WS_LAYER_FAILED
@ COAP_NACK_WS_LAYER_FAILED
Definition coap_io.h:77
COAP_NACK_RST
@ COAP_NACK_RST
Definition coap_io.h:72
COAP_NACK_BAD_RESPONSE
@ COAP_NACK_BAD_RESPONSE
Definition coap_io.h:75
COAP_SOCKET_MULTICAST
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Definition coap_io_internal.h:73
coap_epoll_ctl_add
void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to add the state of events that epoll is to track for the appropriate file de...
COAP_SOCKET_NOT_EMPTY
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
Definition coap_io_internal.h:62
COAP_SOCKET_BOUND
#define COAP_SOCKET_BOUND
the socket is bound
Definition coap_io_internal.h:63
COAP_SOCKET_WANT_READ
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
Definition coap_io_internal.h:65
coap_malloc_endpoint
coap_endpoint_t * coap_malloc_endpoint(void)
COAP_SOCKET_WANT_CONNECT
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
Definition coap_io_internal.h:68
coap_mfree_endpoint
void coap_mfree_endpoint(coap_endpoint_t *ep)
coap_layers_coap
coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST]
Definition coap_layers.c:39
COAP_LAYER_SESSION
@ COAP_LAYER_SESSION
Definition coap_layers_internal.h:23
COAP_SESSION
@ COAP_SESSION
Definition coap_mem.h:50
COAP_STRING
@ COAP_STRING
Definition coap_mem.h:38
coap_malloc_type
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.
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_dtls_context_set_pki
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:77
coap_dtls_get_tls
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
Definition coap_notls.c:122
coap_dtls_get_overhead
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
Definition coap_notls.c:225
coap_dtls_context_check_keys_enabled
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
Definition coap_notls.c:111
coap_session_send_ping
coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
Definition coap_session.c:738
coap_proto_name
static const char * coap_proto_name(coap_proto_t proto)
Definition coap_session.c:1683
coap_nack_name
static const char * coap_nack_name(coap_nack_reason_t reason)
Definition coap_session.c:821
coap_make_session
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:388
coap_session_max_pdu_size_internal
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
Definition coap_session.c:575
INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN
Definition coap_session.c:1879
coap_session_set_no_observe_cancel
void coap_session_set_no_observe_cancel(coap_session_t *session)
Disable client automatically sending observe cancel on session close.
SESSIONS_ADD
#define SESSIONS_ADD(e, obj)
Definition coap_session_internal.h:529
SESSIONS_ITER_SAFE
#define SESSIONS_ITER_SAFE(e, el, rtmp)
Definition coap_session_internal.h:538
COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
Definition coap_session_internal.h:26
SESSIONS_DELETE
#define SESSIONS_DELETE(e, obj)
Definition coap_session_internal.h:532
COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
Definition coap_session_internal.h:27
SESSIONS_ITER
#define SESSIONS_ITER(e, el, rtmp)
Definition coap_session_internal.h:535
SESSIONS_FIND
#define SESSIONS_FIND(e, k, res)
Definition coap_session_internal.h:541
coap_tcp_is_supported
int coap_tcp_is_supported(void)
Check whether TCP is available.
Definition coap_tcp.c:37
coap_block_delete_lg_srcv
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
coap_block_delete_lg_crcv
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
coap_check_update_token
void coap_check_update_token(coap_session_t *session, coap_pdu_t *pdu)
The function checks if the token needs to be updated before PDU is presented to the application (only...
coap_block_delete_lg_xmit
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Definition coap_block.c:2142
coap_cancel_observe
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.
coap_delete_cache_entry
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_DEFAULT_NON_MAX_RETRANSMIT
#define COAP_DEFAULT_NON_MAX_RETRANSMIT
The number of times for requests for re-transmission of missing Q-Block1 when no response has been re...
Definition coap_session.h:518
coap_session_get_non_max_retransmit
uint16_t coap_session_get_non_max_retransmit(const coap_session_t *session)
Get the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition coap_session.c:318
coap_session_get_default_leisure
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:298
coap_session_set_max_retransmit
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:174
COAP_DEFAULT_NON_TIMEOUT
#define COAP_DEFAULT_NON_TIMEOUT
The delay (+ ACK_RANDOM_FACTOR) to introduce once NON MAX_PAYLOADS Q-Block1 or Q-Block2 have been sen...
Definition coap_session.h:527
coap_session_get_non_timeout
coap_fixed_point_t coap_session_get_non_timeout(const coap_session_t *session)
Get the CoAP MAX_PAYLOADS limit delay timeout.
Definition coap_session.c:328
coap_session_set_ack_random_factor
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:158
COAP_DEFAULT_MAX_LATENCY
#define COAP_DEFAULT_MAX_LATENCY
The MAX_LATENCY definition.
Definition coap_session.h:542
coap_session_get_ack_random_factor
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:283
COAP_DEFAULT_ACK_RANDOM_FACTOR
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
Definition coap_session.h:467
coap_session_get_max_retransmit
uint16_t coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
Definition coap_session.c:288
coap_session_set_max_payloads
void coap_session_set_max_payloads(coap_session_t *session, uint16_t value)
Set the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition coap_session.c:212
COAP_DEFAULT_MAX_RETRANSMIT
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
Definition coap_session.h:475
coap_session_get_probing_rate
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:303
COAP_DEFAULT_ACK_TIMEOUT
#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:458
COAP_DEFAULT_DEFAULT_LEISURE
#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:492
coap_session_set_ack_timeout
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:148
COAP_DEFAULT_NSTART
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition coap_session.h:484
coap_session_set_non_receive_timeout
void coap_session_set_non_receive_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non receive timeout delay timeout.
Definition coap_session.c:262
coap_session_set_nstart
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:183
COAP_DEFAULT_PROBING_RATE
#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:500
coap_session_set_non_max_retransmit
void coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
Definition coap_session.c:228
coap_session_get_max_payloads
uint16_t coap_session_get_max_payloads(const coap_session_t *session)
Get the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
Definition coap_session.c:308
COAP_DEFAULT_NON_RECEIVE_TIMEOUT
#define COAP_DEFAULT_NON_RECEIVE_TIMEOUT
The time to wait for any missing Q-Block1 or Q-Block2 packets before requesting re-transmission of mi...
Definition coap_session.h:536
coap_session_set_probing_rate
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:203
coap_session_set_default_leisure
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:192
coap_session_get_non_receive_timeout
coap_fixed_point_t coap_session_get_non_receive_timeout(const coap_session_t *session)
Get the CoAP non receive timeout delay timeout.
Definition coap_session.c:338
coap_session_get_nstart
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:293
COAP_DEFAULT_MAX_PAYLOADS
#define COAP_DEFAULT_MAX_PAYLOADS
Number of Q-Block1 or Q-Block2 payloads that can be sent in a burst before a delay has to kick in.
Definition coap_session.h:509
coap_session_get_ack_timeout
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:278
coap_session_set_non_timeout
void coap_session_set_non_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non timeout delay timeout.
Definition coap_session.c:244
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:144
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:159
coap_prng
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:140
coap_remove_from_queue
int coap_remove_from_queue(coap_queue_t **queue, coap_session_t *session, coap_mid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition coap_net.c:2237
coap_delete_node
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition coap_net.c:203
coap_client_delay_first
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
Definition coap_net.c:986
coap_send_internal
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1381
coap_calc_timeout
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r)
Calculates the initial timeout based on the session CoAP transmission parameters 'ack_timeout',...
Definition coap_net.c:881
coap_wait_ack
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
Definition coap_net.c:907
coap_new_node
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition coap_net.c:232
coap_cancel_session_messages
void coap_cancel_session_messages(coap_context_t *context, coap_session_t *session, coap_nack_reason_t reason)
Cancels all outstanding messages for session session.
Definition coap_net.c:2282
coap_new_message_id
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition coap_session.c:1590
coap_handle_event
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 coap_net.c:3917
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_dtls_establish
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
Definition coap_dtls.c:21
coap_session_new_dtls_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.
coap_dtls_context_set_cpsk
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.
coap_tls_is_supported
int coap_tls_is_supported(void)
Check whether TLS is available.
Definition coap_notls.c:28
COAP_DTLS_PKI_SETUP_VERSION
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
Definition coap_dtls.h:279
coap_dtls_is_supported
int coap_dtls_is_supported(void)
Check whether DTLS is available.
Definition coap_notls.c:23
COAP_DTLS_CPSK_SETUP_VERSION
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
Definition coap_dtls.h:373
coap_tls_library_t
coap_tls_library_t
Definition coap_dtls.h:92
COAP_DTLS_ROLE_CLIENT
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
Definition coap_dtls.h:44
coap_encode_var_safe
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
coap_decode_var_bytes8
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
Definition coap_encode.c:67
coap_encode_var_safe8
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:77
COAP_EVENT_SESSION_CONNECTED
@ COAP_EVENT_SESSION_CONNECTED
Triggered when TCP layer completes exchange of CSM information.
Definition coap_event.h:61
COAP_EVENT_TCP_FAILED
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
Definition coap_event.h:55
COAP_EVENT_SESSION_FAILED
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
Definition coap_event.h:65
COAP_EVENT_SERVER_SESSION_NEW
@ 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
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
Definition coap_event.h:63
COAP_EVENT_SERVER_SESSION_DEL
@ 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
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
Definition coap_event.h:53
COAP_EVENT_TCP_CONNECTED
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
Definition coap_event.h:51
coap_log_debug
#define coap_log_debug(...)
Definition coap_debug.h:120
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:91
coap_print_addr
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:218
coap_endpoint_str
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
coap_session_str
const char * coap_session_str(const coap_session_t *session)
Get session description.
Definition coap_session.c:1882
coap_log_info
#define coap_log_info(...)
Definition coap_debug.h:108
coap_log_warn
#define coap_log_warn(...)
Definition coap_debug.h:102
coap_log_err
#define coap_log_err(...)
Definition coap_debug.h:96
coap_log_crit
#define coap_log_crit(...)
Definition coap_debug.h:90
COAP_LOG_DEBUG
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
coap_netif_dgrm_listen
int coap_netif_dgrm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif datagram listem (udp).
coap_netif_close_ep
void coap_netif_close_ep(coap_endpoint_t *endpoint)
Layer function interface for Netif close for a endpoint.
coap_netif_strm_connect1
int coap_netif_strm_connect1(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif stream connect (tcp).
coap_netif_strm_listen
int coap_netif_strm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif stream listem (tcp).
coap_netif_strm_accept
int coap_netif_strm_accept(coap_endpoint_t *endpoint, coap_session_t *session)
Layer function interface for Netif stream accept.
coap_netif_dgrm_connect
int coap_netif_dgrm_connect(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif datagram connect (udp).
coap_netif_available
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
Definition coap_netif.c:25
coap_netif_available_ep
int coap_netif_available_ep(coap_endpoint_t *endpoint)
Function interface to check whether netif for endpoint is still available.
coap_delete_oscore_associations
void coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
coap_update_token
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
Definition coap_pdu.c:361
COAP_PDU_DELAYED
#define COAP_PDU_DELAYED
Definition coap_pdu_internal.h:85
COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_MAX_MESSAGE_SIZE_TCP8
Definition coap_pdu_internal.h:44
COAP_MAX_MESSAGE_SIZE_TCP0
#define COAP_MAX_MESSAGE_SIZE_TCP0
Definition coap_pdu_internal.h:43
coap_pdu_encode_header
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
Definition coap_pdu.c:1405
COAP_MAX_MESSAGE_SIZE_TCP16
#define COAP_MAX_MESSAGE_SIZE_TCP16
Definition coap_pdu_internal.h:45
coap_add_option_internal
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:701
COAP_DEFAULT_PORT
#define COAP_DEFAULT_PORT
Definition coap_pdu.h:37
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition coap_pdu.c:163
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:255
COAP_TOKEN_DEFAULT_MAX
#define COAP_TOKEN_DEFAULT_MAX
Definition coap_pdu.h:56
COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
Definition coap_pdu.h:191
coap_proto_t
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:304
COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
Definition coap_pdu.h:190
COAPS_DEFAULT_PORT
#define COAPS_DEFAULT_PORT
Definition coap_pdu.h:38
coap_pdu_init
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:97
COAP_INVALID_MID
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:258
COAP_DEFAULT_MTU
#define COAP_DEFAULT_MTU
Definition coap_pdu.h:41
COAP_BERT_BASE
#define COAP_BERT_BASE
Definition coap_pdu.h:44
COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
Definition coap_pdu.h:189
COAP_PROTO_WS
@ COAP_PROTO_WS
Definition coap_pdu.h:310
COAP_PROTO_DTLS
@ COAP_PROTO_DTLS
Definition coap_pdu.h:307
COAP_PROTO_UDP
@ COAP_PROTO_UDP
Definition coap_pdu.h:306
COAP_PROTO_NONE
@ COAP_PROTO_NONE
Definition coap_pdu.h:305
COAP_PROTO_TLS
@ COAP_PROTO_TLS
Definition coap_pdu.h:309
COAP_PROTO_WSS
@ COAP_PROTO_WSS
Definition coap_pdu.h:311
COAP_PROTO_TCP
@ COAP_PROTO_TCP
Definition coap_pdu.h:308
COAP_PROTO_LAST
@ COAP_PROTO_LAST
Definition coap_pdu.h:312
COAP_SIGNALING_CODE_CSM
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:357
COAP_SIGNALING_CODE_PING
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:358
COAP_MESSAGE_NON
@ COAP_MESSAGE_NON
Definition coap_pdu.h:66
COAP_MESSAGE_CON
@ COAP_MESSAGE_CON
Definition coap_pdu.h:65
COAP_NON_PROBING_WAIT_BASE
#define COAP_NON_PROBING_WAIT_BASE(s)
Definition coap_session_internal.h:403
coap_new_server_session
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep)
Creates a new server session for the specified endpoint.
coap_session_delay_pdu
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
Definition coap_session.c:650
coap_session_refresh_psk_hint
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).
Definition coap_session.c:1376
coap_session_send_csm
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:691
coap_add_fixed_uint
coap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition coap_session.c:60
coap_session_max_pdu_rcv_size
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
Definition coap_session.c:598
coap_sub_fixed_uint
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition coap_session.c:68
coap_endpoint_get_session
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.
COAP_ACK_RANDOM_FACTOR
#define COAP_ACK_RANDOM_FACTOR(s)
Definition coap_session_internal.h:390
coap_session_establish
void coap_session_establish(coap_session_t *session)
Layer function interface for layer below session accept/connect being established.
Definition coap_session.c:1270
coap_add_fixed_fixed
coap_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition coap_session.c:50
coap_get_non_timeout_random_ticks
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
COAP_NSTART
#define COAP_NSTART(s)
Definition coap_session_internal.h:392
coap_session_refresh_psk_key
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).
Definition coap_session.c:1405
coap_session_connected
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
Definition coap_session.c:759
coap_multi_fixed_fixed
coap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
Definition coap_session.c:30
coap_session_send_pdu
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
Definition coap_net.c:784
coap_div_fixed_uint
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition coap_session.c:76
coap_session_free
void coap_session_free(coap_session_t *session)
Definition coap_session.c:545
coap_session_mfree
void coap_session_mfree(coap_session_t *session)
Definition coap_session.c:464
coap_session_refresh_psk_identity
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).
Definition coap_session.c:1433
coap_get_non_timeout_random
coap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session)
COAP_NON_MAX_RETRANSMIT
#define COAP_NON_MAX_RETRANSMIT(s)
Definition coap_session_internal.h:397
COAP_NON_TIMEOUT
#define COAP_NON_TIMEOUT(s)
Definition coap_session_internal.h:398
coap_multi_fixed_uint
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
Definition coap_session.c:40
COAP_NON_PARTIAL_TIMEOUT
#define COAP_NON_PARTIAL_TIMEOUT(s)
Definition coap_session_internal.h:404
COAP_EXT_T_CHECKED
@ COAP_EXT_T_CHECKED
Token size valid.
Definition coap_session_internal.h:60
coap_session_type_t
coap_session_type_t
coap_session_type_t values
Definition coap_session.h:43
coap_session_set_mtu
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
Definition coap_session.c:635
coap_session_get_context
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
Definition coap_session.c:1624
coap_session_get_addr_local
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address and port from the session.
Definition coap_session.c:1605
coap_session_get_proto
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
Definition coap_session.c:1631
coap_session_max_pdu_size
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition coap_session.c:608
coap_session_get_psk_key
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
Definition coap_session.c:1483
coap_session_get_tls
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)
Definition coap_session.c:1675
coap_session_get_state
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
Definition coap_session.c:1661
coap_free_endpoint
void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
coap_session_state_t
coap_session_state_t
coap_session_state_t values
Definition coap_session.h:54
coap_new_client_session_pki
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_new_endpoint
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.
COAP_PROTO_NOT_RELIABLE
#define COAP_PROTO_NOT_RELIABLE(p)
Definition coap_session.h:36
coap_session_init_token
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.
Definition coap_session.c:1577
COAP_PROTO_RELIABLE
#define COAP_PROTO_RELIABLE(p)
Definition coap_session.h:37
coap_new_client_session_psk
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.
coap_session_new_token
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
Definition coap_session.c:1583
coap_session_get_psk_identity
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
Definition coap_session.c:1471
coap_session_set_app_data
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
Definition coap_session.c:376
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition coap_session.c:354
coap_session_get_by_peer
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
Get the session associated with the specified remote_addr and index.
Definition coap_session.c:1849
coap_endpoint_set_default_mtu
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
coap_session_get_psk_hint
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
coap_session_get_addr_remote
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
Definition coap_session.c:1598
coap_new_client_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.
coap_session_set_type_client
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
coap_new_client_session_psk2
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.
coap_session_disconnected
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
Definition coap_session.c:848
coap_session_get_addr_mcast
const coap_address_t * coap_session_get_addr_mcast(const coap_session_t *session)
Get the remote multicast IP address and port from the session if the original target IP was multicast...
Definition coap_session.c:1612
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition coap_session.c:348
coap_session_get_ifindex
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
Definition coap_session.c:1668
coap_session_get_app_data
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:382
coap_session_get_type
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
Definition coap_session.c:1638
COAP_SESSION_TYPE_HELLO
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
Definition coap_session.h:47
COAP_SESSION_TYPE_SERVER
@ COAP_SESSION_TYPE_SERVER
server-side
Definition coap_session.h:46
COAP_SESSION_TYPE_CLIENT
@ COAP_SESSION_TYPE_CLIENT
client-side
Definition coap_session.h:45
COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_HANDSHAKE
Definition coap_session.h:57
COAP_SESSION_STATE_CSM
@ COAP_SESSION_STATE_CSM
Definition coap_session.h:58
COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_ESTABLISHED
Definition coap_session.h:59
COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_NONE
Definition coap_session.h:55
COAP_SESSION_STATE_CONNECTING
@ COAP_SESSION_STATE_CONNECTING
Definition coap_session.h:56
coap_delete_bin_const
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:120
coap_delete_str_const
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition coap_str.c:61
coap_new_bin_const
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
coap_binary_equal
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:203
coap_delete_observers
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
coap_ws_is_supported
int coap_ws_is_supported(void)
Check whether WebSockets is available.
Definition coap_ws.c:935
coap_wss_is_supported
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Definition coap_ws.c:940
coap_addr_hash_t
Only used for servers for hashing incoming packets.
Definition coap_session_internal.h:40
coap_addr_hash_t::lport
uint16_t lport
local port
Definition coap_session_internal.h:42
coap_addr_hash_t::remote
coap_address_t remote
remote address and port
Definition coap_session_internal.h:41
coap_addr_hash_t::proto
coap_proto_t proto
CoAP protocol.
Definition coap_session_internal.h:43
coap_addr_tuple_t
Definition coap_io.h:55
coap_addr_tuple_t::remote
coap_address_t remote
remote address and port
Definition coap_io.h:56
coap_addr_tuple_t::local
coap_address_t local
local address and port
Definition coap_io.h:57
coap_address_t
Multi-purpose address abstraction.
Definition coap_address.h:109
coap_bin_const_t
CoAP binary data definition with const data.
Definition coap_str.h:64
coap_bin_const_t::length
size_t length
length of binary data
Definition coap_str.h:65
coap_bin_const_t::s
const uint8_t * s
read-only binary data
Definition coap_str.h:66
coap_cache_entry_t
Definition coap_cache_internal.h:41
coap_cache_entry_t::session
coap_session_t * session
Definition coap_cache_internal.h:44
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition coap_net_internal.h:49
coap_context_t::sessions
coap_session_t * sessions
client sessions
Definition coap_net_internal.h:78
coap_context_t::nack_handler
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
Definition coap_net_internal.h:102
coap_context_t::csm_max_message_size
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
Definition coap_net_internal.h:161
coap_context_t::max_handshake_sessions
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
Definition coap_net_internal.h:153
coap_context_t::sendqueue
coap_queue_t * sendqueue
Definition coap_net_internal.h:73
coap_context_t::max_token_size
uint32_t max_token_size
Largest token size supported RFC8974.
Definition coap_net_internal.h:172
coap_context_t::cache
coap_cache_entry_t * cache
CoAP cache-entry cache.
Definition coap_net_internal.h:165
coap_context_t::endpoint
coap_endpoint_t * endpoint
the endpoints used for listening
Definition coap_net_internal.h:75
coap_context_t::proxy_uri_resource
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
Definition coap_net_internal.h:56
coap_context_t::block_mode
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
Definition coap_net_internal.h:191
coap_context_t::max_idle_sessions
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
Definition coap_net_internal.h:150
coap_dtls_cpsk_info_t::key
coap_bin_const_t key
Definition coap_dtls.h:349
coap_dtls_cpsk_info_t::identity
coap_bin_const_t identity
Definition coap_dtls.h:348
coap_dtls_cpsk_t
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:378
coap_dtls_cpsk_t::version
uint8_t version
Definition coap_dtls.h:379
coap_dtls_cpsk_t::psk_info
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
Definition coap_dtls.h:407
coap_dtls_pki_t
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:284
coap_dtls_pki_t::version
uint8_t version
Definition coap_dtls.h:285
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition coap_session_internal.h:219
coap_endpoint_t::context
coap_context_t * context
endpoint's context
Definition coap_session_internal.h:221
coap_endpoint_t::default_mtu
uint16_t default_mtu
default mtu for this interface
Definition coap_session_internal.h:223
coap_endpoint_t::sessions
coap_session_t * sessions
hash table or list of active sessions
Definition coap_session_internal.h:227
coap_endpoint_t::bind_addr
coap_address_t bind_addr
local interface address
Definition coap_session_internal.h:226
coap_endpoint_t::sock
coap_socket_t sock
socket object for the interface, if any
Definition coap_session_internal.h:224
coap_endpoint_t::proto
coap_proto_t proto
protocol used on this interface
Definition coap_session_internal.h:222
coap_fixed_point_t
Abstraction of a fixed point number that can be used where necessary instead of a float.
Definition coap_session.h:30
coap_fixed_point_t::fractional_part
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
Definition coap_session.h:32
coap_fixed_point_t::integer_part
uint16_t integer_part
Integer part of fixed point variable.
Definition coap_session.h:31
coap_layer_func_t::l_establish
coap_layer_establish_t l_establish
Definition coap_layers_internal.h:109
coap_layer_func_t::l_close
coap_layer_close_t l_close
Definition coap_layers_internal.h:110
coap_lg_crcv_t
Structure to hold large body (many blocks) client receive information.
Definition coap_block_internal.h:146
coap_lg_crcv_t::pdu
coap_pdu_t pdu
skeletal PDU
Definition coap_block_internal.h:167
coap_lg_crcv_t::app_token
coap_binary_t * app_token
app requesting PDU token
Definition coap_block_internal.h:162
coap_lg_crcv_t::observe_set
uint8_t observe_set
Set if this is an observe receive PDU.
Definition coap_block_internal.h:150
coap_lg_srcv_t
Structure to hold large body (many blocks) server receive information.
Definition coap_block_internal.h:177
coap_lg_xmit_t
Structure to hold large body (many blocks) transmission information.
Definition coap_block_internal.h:117
coap_packet_t
Definition coap_io_internal.h:207
coap_packet_t::length
size_t length
length of payload
Definition coap_io_internal.h:210
coap_packet_t::addr_info
coap_addr_tuple_t addr_info
local and remote addresses
Definition coap_io_internal.h:208
coap_packet_t::payload
unsigned char * payload
payload
Definition coap_io_internal.h:211
coap_packet_t::ifindex
int ifindex
the interface index
Definition coap_io_internal.h:209
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:119
coap_pdu_t::hdr_size
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
Definition coap_pdu_internal.h:126
coap_pdu_t::actual_token
coap_bin_const_t actual_token
Actual token in pdu.
Definition coap_pdu_internal.h:132
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition coap_pdu_internal.h:123
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition coap_pdu_internal.h:135
coap_pdu_t::session
coap_session_t * session
Session responsible for PDU or NULL.
Definition coap_pdu_internal.h:158
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition coap_pdu_internal.h:120
coap_queue_t
Queue entry.
Definition coap_net_internal.h:34
coap_queue_t::session
coap_session_t * session
the CoAP session
Definition coap_net_internal.h:41
coap_queue_t::pdu
coap_pdu_t * pdu
the CoAP PDU to send
Definition coap_net_internal.h:43
coap_queue_t::timeout
unsigned int timeout
the randomized timeout value
Definition coap_net_internal.h:40
coap_queue_t::next
struct coap_queue_t * next
Definition coap_net_internal.h:35
coap_queue_t::id
coap_mid_t id
CoAP message id.
Definition coap_net_internal.h:42
coap_queue_t::t
coap_tick_t t
when to send PDU for the next time
Definition coap_net_internal.h:36
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition coap_session_internal.h:68
coap_session_t::lg_xmit
coap_lg_xmit_t * lg_xmit
list of large transmissions
Definition coap_session_internal.h:96
coap_session_t::max_token_checked
volatile uint8_t max_token_checked
Check for max token size coap_ext_token_check_t.
Definition coap_session_internal.h:200
coap_session_t::psk_key
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
Definition coap_session_internal.h:127
coap_session_t::endpoint
coap_endpoint_t * endpoint
session's endpoint
Definition coap_session_internal.h:84
coap_session_t::doing_first
uint8_t doing_first
Set if doing client's first request.
Definition coap_session_internal.h:182
coap_session_t::last_rx_tx
coap_tick_t last_rx_tx
Definition coap_session_internal.h:111
coap_session_t::sock
coap_socket_t sock
socket object for the session, if any
Definition coap_session_internal.h:81
coap_session_t::partial_pdu
coap_pdu_t * partial_pdu
incomplete incoming pdu
Definition coap_session_internal.h:110
coap_session_t::max_token_size
uint32_t max_token_size
Largest token size supported RFC8974.
Definition coap_session_internal.h:204
coap_session_t::nstart
uint16_t nstart
maximum concurrent confirmable xmits (default 1)
Definition coap_session_internal.h:152
coap_session_t::psk_identity
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
Definition coap_session_internal.h:118
coap_session_t::state
coap_session_state_t state
current state of relationship with peer
Definition coap_session_internal.h:71
coap_session_t::tx_token
uint64_t tx_token
Next token number to use.
Definition coap_session_internal.h:205
coap_session_t::block_mode
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
Definition coap_session_internal.h:181
coap_session_t::csm_bert_loc_support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
Definition coap_session_internal.h:180
coap_session_t::addr_info
coap_addr_tuple_t addr_info
remote/local address info
Definition coap_session_internal.h:79
coap_session_t::proto
coap_proto_t proto
protocol used
Definition coap_session_internal.h:69
coap_session_t::tx_mid
uint16_t tx_mid
the last message id that was used in this session
Definition coap_session_internal.h:88
coap_session_t::ref
unsigned ref
reference count from queues
Definition coap_session_internal.h:73
coap_session_t::csm_rcv_mtu
size_t csm_rcv_mtu
CSM mtu (rcv)
Definition coap_session_internal.h:76
coap_session_t::psk_hint
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
Definition coap_session_internal.h:136
coap_session_t::last_token
coap_bin_const_t * last_token
Definition coap_session_internal.h:206
coap_session_t::cpsk_setup_data
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
Definition coap_session_internal.h:116
coap_session_t::mtu
size_t mtu
path or CSM mtu (xmt)
Definition coap_session_internal.h:75
coap_session_t::no_observe_cancel
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
Definition coap_session_internal.h:185
coap_session_t::partial_read
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
Definition coap_session_internal.h:108
coap_session_t::dtls_event
int dtls_event
Tracking any (D)TLS events on this session.
Definition coap_session_internal.h:176
coap_session_t::max_retransmit
uint16_t max_retransmit
maximum re-transmit count (default 4)
Definition coap_session_internal.h:150
coap_session_t::ack_random_factor
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
Definition coap_session_internal.h:148
coap_session_t::proxy_session
uint8_t proxy_session
Set if this is an ongoing proxy session.
Definition coap_session_internal.h:183
coap_session_t::con_active
uint8_t con_active
Active CON request sent.
Definition coap_session_internal.h:90
coap_session_t::delayqueue
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
Definition coap_session_internal.h:94
coap_session_t::tls_overhead
size_t tls_overhead
overhead of TLS layer
Definition coap_session_internal.h:74
coap_session_t::app
void * app
application-specific data
Definition coap_session_internal.h:145
coap_session_t::tx_rtag
uint32_t tx_rtag
Next Request-Tag number to use.
Definition coap_session_internal.h:178
coap_session_t::last_ping_mid
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
Definition coap_session_internal.h:92
coap_session_t::lg_srcv
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
Definition coap_session_internal.h:101
coap_session_t::lg_crcv
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
Definition coap_session_internal.h:98
coap_session_t::ack_timeout
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
Definition coap_session_internal.h:146
coap_session_t::default_leisure
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
Definition coap_session_internal.h:154
coap_session_t::last_con_mid
coap_mid_t last_con_mid
The last CON mid that has been been processed.
Definition coap_session_internal.h:210
coap_session_t::type
coap_session_type_t type
client or server side socket
Definition coap_session_internal.h:70
coap_session_t::probing_rate
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
Definition coap_session_internal.h:156
coap_session_t::last_ack_mid
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
Definition coap_session_internal.h:208
coap_session_t::context
coap_context_t * context
session's context
Definition coap_session_internal.h:86
coap_session_t::partial_write
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
Definition coap_session_internal.h:103
coap_session_t::addr_hash
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
Definition coap_session_internal.h:77
coap_session_t::ifindex
int ifindex
interface index
Definition coap_session_internal.h:80
coap_socket_t::lfunc
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
Definition coap_io_internal.h:55
coap_socket_t::session
coap_session_t * session
Used to determine session owner.
Definition coap_io_internal.h:47
coap_socket_t::endpoint
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
Definition coap_io_internal.h:49
coap_socket_t::mcast_addr
coap_address_t mcast_addr
remote address and port (multicast track)
Definition coap_io_internal.h:53
coap_socket_t::flags
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
Definition coap_io_internal.h:46