19#ifndef COAP_SESSION_C_
20#define COAP_SESSION_C_
24#ifdef COAP_EPOLL_SUPPORT
26#include <sys/timerfd.h>
85#if COAP_Q_BLOCK_SUPPORT
162 coap_log_debug(
"***%s: session ack_random_factor set to %u.%03u\n",
165#if COAP_Q_BLOCK_SUPPORT
166 coap_session_fix_non_probing_wait_base(session);
167 coap_session_fix_non_partial_timeout(session);
213#if COAP_Q_BLOCK_SUPPORT
215 session->max_payloads = value;
218 coap_session_fix_non_probing_wait_base(session);
219 coap_session_fix_non_partial_timeout(session);
229#if COAP_Q_BLOCK_SUPPORT
231 session->non_max_retransmit = value;
234 coap_session_fix_non_probing_wait_base(session);
235 coap_session_fix_non_partial_timeout(session);
246#if COAP_Q_BLOCK_SUPPORT
248 session->non_timeout = value;
251 session->non_timeout.fractional_part);
252 coap_session_fix_non_probing_wait_base(session);
253 coap_session_fix_non_partial_timeout(session);
264#if COAP_Q_BLOCK_SUPPORT
266 session->non_receive_timeout = value;
267 coap_log_debug(
"***%s: session non_receive_timeout set to %u.%03u\n",
269 session->non_receive_timeout.integer_part,
270 session->non_receive_timeout.fractional_part);
309#if COAP_Q_BLOCK_SUPPORT
310 return session->max_payloads;
319#if COAP_Q_BLOCK_SUPPORT
320 return session->non_max_retransmit;
329#if COAP_Q_BLOCK_SUPPORT
330 return session->non_timeout;
339#if COAP_Q_BLOCK_SUPPORT
340 return session->non_receive_timeout;
380 assert(session->
ref > 0);
381 if (session->
ref > 0)
389 __coverity_negative_sink__(session->
ref);
392 __coverity_free__(session);
401 session->
app = app_data;
418#if ! COAP_SERVER_SUPPORT
423 memset(session, 0,
sizeof(*session));
424 session->
proto = proto;
425 session->
type = type;
440#if COAP_SERVER_SUPPORT
461#if COAP_Q_BLOCK_SUPPORT
466 coap_session_fix_non_probing_wait_base(session);
467 coap_session_fix_non_partial_timeout(session);
492#if COAP_CLIENT_SUPPORT
496 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, etmp) {
504 if (queue->
session == session) {
512 LL_DELETE(session->
lg_crcv, lg_crcv);
528#if COAP_SERVER_SUPPORT
537 LL_FOREACH_SAFE(session->
delayqueue, q, tmp) {
546 LL_FOREACH_SAFE(session->
lg_xmit, lq, ltmp) {
547 LL_DELETE(session->
lg_xmit, lq);
550#if COAP_SERVER_SUPPORT
553 LL_FOREACH_SAFE(session->
lg_srcv, sq, stmp) {
554 LL_DELETE(session->
lg_srcv, sq);
558#if COAP_OSCORE_SUPPORT
572 assert(session->
ref == 0);
578#if COAP_SERVER_SUPPORT
585#if COAP_CLIENT_SUPPORT
597 assert(session->
ref == 1);
603 size_t max_with_header) {
615 if (max_with_header <= 2)
618 return max_with_header - 2;
620 return max_with_header - 3;
622 return max_with_header - 4;
647 memcpy(&session_rw, &session,
sizeof(session_rw));
656 size_t max_with_header;
659#if COAP_CLIENT_SUPPORT
670 memcpy(&session_rw, &session,
sizeof(session_rw));
684#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
685 if (mtu > COAP_DEFAULT_MAX_PDU_RX_SIZE)
686 mtu = COAP_DEFAULT_MAX_PDU_RX_SIZE;
703 assert(removed == node);
712 if (q->
id == pdu->
mid) {
713 coap_log_err(
"** %s: mid=0x%04x: already in-use - dropped\n",
746 if (session->
mtu == 0)
766 ssize_t bytes_written;
841 ssize_t bytes_written;
862 if (bytes_written < 0)
865 if (bytes_written <= 0 || (
size_t)bytes_written < q->pdu->used_size + q->
pdu->
hdr_size) {
868 if (bytes_written > 0)
878#if COAP_MAX_LOGGING_LEVEL >= _COAP_LOG_DEBUG
883 return "COAP_NACK_TOO_MANY_RETRIES";
885 return "COAP_NACK_NOT_DELIVERABLE";
887 return "COAP_NACK_RST";
889 return "COAP_NACK_TLS_FAILED";
891 return "COAP_NACK_ICMP_ISSUE";
893 return "COAP_NACK_BAD_RESPONSE";
895 return "COAP_NACK_TLS_LAYER_FAILED";
897 return "COAP_NACK_WS_LAYER_FAILED";
899 return "COAP_NACK_WS_FAILED";
924#if COAP_CLIENT_SUPPORT
944#if COAP_SERVER_SUPPORT
947#if COAP_CLIENT_SUPPORT
971 coap_log_debug(
"** %s: mid=0x%04x: not transmitted after disconnect\n",
978#if COAP_CLIENT_SUPPORT
984#if COAP_CLIENT_SUPPORT
985 if (!sent_nack && session->
lg_crcv) {
1002#if COAP_SERVER_SUPPORT
1024 coap_log_debug(
"** %s: mid=0x%04x: not transmitted after disconnect\n",
1026#if COAP_CLIENT_SUPPORT
1032#if COAP_CLIENT_SUPPORT
1034 LL_FOREACH_SAFE(session->
lg_crcv, cq, etmp) {
1035 LL_DELETE(session->
lg_crcv, cq);
1039 LL_FOREACH_SAFE(session->
lg_xmit, lq, ltmp) {
1040 LL_DELETE(session->
lg_xmit, lq);
1043#if COAP_SERVER_SUPPORT
1044 LL_FOREACH_SAFE(session->
lg_srcv, sq, stmp) {
1045 LL_DELETE(session->
lg_srcv, sq);
1051#if !COAP_DISABLE_TCP
1070#if COAP_SERVER_SUPPORT
1077 addr_hash->
proto = proto;
1085 unsigned int num_idle = 0;
1086 unsigned int num_hs = 0;
1091 coap_make_addr_hash(&addr_hash, endpoint->
proto, &packet->
addr_info);
1101#if COAP_CLIENT_SUPPORT
1126 if (oldest_hs == NULL ||
1128 oldest_hs = session;
1136 if (oldest_hs == NULL ||
1138 oldest_hs = session;
1148 }
else if (oldest_hs) {
1159 coap_log_debug(
"Oustanding sessions in COAP_SESSION_STATE_HANDSHAKE too "
1160 "large. New request ignored\n");
1181#define OFF_CONTENT_TYPE 0
1182#define DTLS_CT_ALERT 21
1183#define DTLS_CT_HANDSHAKE 22
1184#define OFF_HANDSHAKE_TYPE 13
1185#define DTLS_HT_CLIENT_HELLO 1
1186#define DTLS_CT_CID 25
1188#define OFF_CID_DTLS13 1
1190 const uint8_t *payload = (
const uint8_t *)packet->
payload;
1191 size_t length = packet->
length;
1192 if (length < (OFF_HANDSHAKE_TYPE + 1)) {
1193 coap_log_debug(
"coap_dtls_hello: ContentType %d Short Packet (%zu < %d) dropped\n",
1194 payload[OFF_CONTENT_TYPE], length,
1195 OFF_HANDSHAKE_TYPE + 1);
1198 if ((payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 ||
1199 payload[OFF_CONTENT_TYPE] == DTLS_CT_CID) {
1205 if ((session->
is_dtls13 && (payload[OFF_CONTENT_TYPE] & 0x30) == 0x30 &&
1206 memcmp(session->
client_cid->
s, &payload[OFF_CID_DTLS13],
1208 (!session->
is_dtls13 && payload[OFF_CONTENT_TYPE] == DTLS_CT_CID &&
1223 coap_log_debug(
"coap_dtls_hello: ContentType Connection-IS dropped\n");
1226 }
else if (payload[OFF_CONTENT_TYPE] != DTLS_CT_HANDSHAKE ||
1227 payload[OFF_HANDSHAKE_TYPE] != DTLS_HT_CLIENT_HELLO) {
1229 if (payload[OFF_CONTENT_TYPE] != DTLS_CT_ALERT)
1230 coap_log_debug(
"coap_dtls_hello: ContentType %d Handshake %d dropped\n",
1231 payload[OFF_CONTENT_TYPE],
1232 payload[OFF_HANDSHAKE_TYPE]);
1270#if COAP_CLIENT_SUPPORT
1287 coap_log_crit(
"coap_new_client_session*: DTLS not supported\n");
1294 coap_log_crit(
"coap_new_client_session*: TCP not supported\n");
1301 coap_log_crit(
"coap_new_client_session*: TLS not supported\n");
1308 coap_log_crit(
"coap_new_client_session*: WS not supported\n");
1315 coap_log_crit(
"coap_new_client_session*: WSS not supported\n");
1327 local_if, server, 0, ctx, NULL);
1348 coap_log_warn(
"***%s: session %p: duplicate - already exists\n",
1354 session->
sock.context = ctx;
1356#if !COAP_DISABLE_TCP
1364#ifdef COAP_EPOLL_SUPPORT
1376#if COAP_SERVER_SUPPORT
1399#if !COAP_DISABLE_TCP
1422#if !COAP_DISABLE_TCP
1428#if COAP_CLIENT_SUPPORT
1450 session = coap_session_create_client(ctx, local_if, server,
1455 coap_session_check_connect(session);
1465 const uint8_t *key,
unsigned key_len) {
1479 const uint8_t *key,
unsigned key_len) {
1483 memset(&setup_data, 0,
sizeof(setup_data));
1491 if (key && key_len > 0) {
1497 proto, &setup_data);
1507coap_sanitize_client_sni(
char **client_sni) {
1510 if (*client_sni == NULL)
1638 session = coap_session_create_client(ctx, local_if, server, proto);
1640 if (!session || !setup_data)
1663 coap_log_warn(
"Cannot store session pre-shared key (PSK)\n");
1683 coap_session_check_connect(session);
1695 if (psk_hint && psk_hint->
s) {
1703 coap_log_err(
"No memory to store identity hint (PSK)\n");
1724 if (psk_key && psk_key->
s) {
1731 coap_log_err(
"No memory to store pre-shared key (PSK)\n");
1752 if (psk_identity && psk_identity->
s) {
1760 coap_log_err(
"No memory to store pre-shared key identity (PSK)\n");
1761 if (old_psk_identity)
1768 if (old_psk_identity)
1774#if COAP_SERVER_SUPPORT
1788 if (psk_identity == NULL) {
1792 return psk_identity;
1802#if COAP_CLIENT_SUPPORT
1829 coap_log_err(
"coap_new_client_session_pki: Wrong version of setup_data\n");
1834 l_setup_data = *setup_data;
1835 coap_sanitize_client_sni(&l_setup_data.
client_sni);
1837 session = coap_session_create_client(ctx, local_if, server, proto);
1852 coap_session_check_connect(session);
1857#if COAP_SERVER_SUPPORT
1858#if !COAP_DISABLE_TCP
1863 NULL, NULL, NULL, 0, ctx, ep);
1873#ifdef COAP_EPOLL_SUPPORT
1906 const uint8_t *data) {
1936 return ++session->
tx_mid;
1957#if COAP_CLIENT_SUPPORT
1977 return session->
proto;
1984 return session->
type;
1988#if COAP_CLIENT_SUPPORT
1991#if COAP_SERVER_SUPPORT
2007 return session->
state;
2050#if COAP_SERVER_SUPPORT
2067 assert(listen_addr);
2100 coap_log_info(
"coap_new_endpoint: one of coap_context_set_psk() or "
2101 "coap_context_set_pki() not called\n");
2123 ep->
sock.context = context;
2125#if !COAP_DISABLE_TCP
2131 coap_log_crit(
"coap_new_endpoint: protocol not supported\n");
2136#ifndef INET6_ADDRSTRLEN
2137#define INET6_ADDRSTRLEN 40
2149#ifdef COAP_EPOLL_SUPPORT
2192 assert(session->
ref == 0);
2193 if (session->
ref == 0) {
2209#ifdef COAP_EPOLL_SUPPORT
2229#if COAP_CLIENT_SUPPORT
2240#if COAP_SERVER_SUPPORT
2254#ifndef INET6_ADDRSTRLEN
2255#define INET6_ADDRSTRLEN 46
2260 char *p = szSession, *end = szSession +
sizeof(szSession);
2262 (
unsigned char *)p, end - p) > 0)
2270 (
unsigned char *)p, end - p) > 0)
2273 if (session->
ifindex > 0 && p + 1 < end)
2274 p += snprintf(p, end - p,
" (if%d)", session->
ifindex);
2284#if COAP_SERVER_SUPPORT
2287 static char szEndpoint[128];
2288 char *p = szEndpoint, *end = szEndpoint +
sizeof(szEndpoint);
2304#if COAP_CLIENT_SUPPORT
void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
uint16_t coap_address_get_port(const coap_address_t *addr)
Returns the port from addr in host byte order.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
@ COAP_NACK_NOT_DELIVERABLE
@ COAP_NACK_TOO_MANY_RETRIES
@ COAP_NACK_TLS_LAYER_FAILED
@ COAP_NACK_WS_LAYER_FAILED
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
void coap_epoll_ctl_add(coap_socket_t *sock, uint32_t events, const char *func)
Epoll specific function to add the state of events that epoll is to track for the appropriate file de...
#define COAP_SOCKET_NOT_EMPTY
the socket is not empty
#define COAP_SOCKET_BOUND
the socket is bound
#define COAP_SOCKET_WANT_READ
non blocking socket is waiting for reading
coap_endpoint_t * coap_malloc_endpoint(void)
#define COAP_SOCKET_WANT_CONNECT
non blocking client socket is waiting for connect
void coap_mfree_endpoint(coap_endpoint_t *ep)
coap_layer_func_t coap_layers_coap[COAP_PROTO_LAST][COAP_LAYER_LAST]
Library specific build wrapper for coap_internal.h.
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
int coap_dtls_context_set_pki(coap_context_t *ctx COAP_UNUSED, const coap_dtls_pki_t *setup_data COAP_UNUSED, const coap_dtls_role_t role COAP_UNUSED)
void * coap_dtls_get_tls(const coap_session_t *c_session COAP_UNUSED, coap_tls_library_t *tls_lib)
unsigned int coap_dtls_get_overhead(coap_session_t *session COAP_UNUSED)
int coap_dtls_context_check_keys_enabled(coap_context_t *ctx COAP_UNUSED)
static const char * coap_proto_name(coap_proto_t proto)
COAP_API coap_mid_t coap_session_send_ping(coap_session_t *session)
Send a ping message for the session.
static const char * coap_nack_name(coap_nack_reason_t reason)
static coap_session_t * coap_make_session(coap_proto_t proto, coap_session_type_t type, const coap_addr_hash_t *addr_hash, const coap_address_t *local_addr, const coap_address_t *remote_addr, int ifindex, coap_context_t *context, coap_endpoint_t *endpoint)
static size_t coap_session_max_pdu_size_internal(const coap_session_t *session, size_t max_with_header)
void coap_session_set_no_observe_cancel(coap_session_t *session)
Disable client automatically sending observe cancel on session close.
#define SESSIONS_ADD(e, obj)
#define SESSIONS_ITER_SAFE(e, el, rtmp)
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS
#define SESSIONS_DELETE(e, obj)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS
#define SESSIONS_ITER(e, el, rtmp)
#define SESSIONS_FIND(e, k, res)
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
void coap_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...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
void coap_delete_cache_entry(coap_context_t *context, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
#define COAP_DEFAULT_NON_MAX_RETRANSMIT
The number of times for requests for re-transmission of missing Q-Block1 when no response has been re...
uint16_t coap_session_get_non_max_retransmit(const coap_session_t *session)
Get the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
coap_fixed_point_t coap_session_get_default_leisure(const coap_session_t *session)
Get the CoAP default leisure time RFC7252 DEFAULT_LEISURE.
void coap_session_set_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP maximum retransmit count before failure.
#define COAP_DEFAULT_NON_TIMEOUT
The delay (+ ACK_RANDOM_FACTOR) to introduce once NON MAX_PAYLOADS Q-Block1 or Q-Block2 have been sen...
coap_fixed_point_t coap_session_get_non_timeout(const coap_session_t *session)
Get the CoAP MAX_PAYLOADS limit delay timeout.
void coap_session_set_ack_random_factor(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP ack randomize factor.
#define COAP_DEFAULT_MAX_LATENCY
The MAX_LATENCY definition.
coap_fixed_point_t coap_session_get_ack_random_factor(const coap_session_t *session)
Get the CoAP ack randomize factor.
#define COAP_DEFAULT_ACK_RANDOM_FACTOR
A factor that is used to randomize the wait time before a message is retransmitted to prevent synchro...
uint16_t coap_session_get_max_retransmit(const coap_session_t *session)
Get the CoAP maximum retransmit before failure.
void coap_session_set_max_payloads(coap_session_t *session, uint16_t value)
Set the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
#define COAP_DEFAULT_MAX_RETRANSMIT
Number of message retransmissions before message sending is stopped.
uint32_t coap_session_get_probing_rate(const coap_session_t *session)
Get the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
#define COAP_DEFAULT_ACK_TIMEOUT
Number of seconds when to expect an ACK or a response to an outstanding CON message.
#define COAP_DEFAULT_DEFAULT_LEISURE
The number of seconds to use as bounds for multicast traffic RFC 7252, Section 4.8 Default value of D...
void coap_session_set_ack_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP initial ack response timeout before the next re-transmit.
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
void coap_session_set_non_receive_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non receive timeout delay timeout.
void coap_session_set_nstart(coap_session_t *session, uint16_t value)
Set the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
#define COAP_DEFAULT_PROBING_RATE
The number of bytes/second allowed when there is no response RFC 7252, Section 4.8 Default value of P...
void coap_session_set_non_max_retransmit(coap_session_t *session, uint16_t value)
Set the CoAP NON maximum retransmit count of missing Q-Block1 or Q-Block2 requested before there is a...
uint16_t coap_session_get_max_payloads(const coap_session_t *session)
Get the CoAP maximum payloads count of Q-Block1 or Q-Block2 before delay is introduced RFC9177 MAX_PA...
#define COAP_DEFAULT_NON_RECEIVE_TIMEOUT
The time to wait for any missing Q-Block1 or Q-Block2 packets before requesting re-transmission of mi...
void coap_session_set_probing_rate(coap_session_t *session, uint32_t value)
Set the CoAP probing rate when there is no response RFC7252 PROBING_RATE.
void coap_session_set_default_leisure(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP default leisure time (for multicast) RFC7252 DEFAULT_LEISURE.
coap_fixed_point_t coap_session_get_non_receive_timeout(const coap_session_t *session)
Get the CoAP non receive timeout delay timeout.
uint16_t coap_session_get_nstart(const coap_session_t *session)
Get the CoAP maximum concurrent transmission count of Confirmable messages RFC7252 NSTART.
#define COAP_DEFAULT_MAX_PAYLOADS
Number of Q-Block1 or Q-Block2 payloads that can be sent in a burst before a delay has to kick in.
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session)
Get the CoAP initial ack response timeout before the next re-transmit.
void coap_session_set_non_timeout(coap_session_t *session, coap_fixed_point_t value)
Set the CoAP non timeout delay timeout.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
int coap_prng_lkd(void *buf, size_t len)
Fills buf with len random bytes using the default pseudo random number generator.
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_delete_node_lkd(coap_queue_t *node)
Destroys specified node.
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.
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
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',...
coap_mid_t coap_wait_ack(coap_context_t *context, coap_session_t *session, coap_queue_t *node)
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
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.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
COAP_API uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
@ COAP_RESPONSE_OK
Response is fine.
void coap_dtls_establish(coap_session_t *session)
Layer function interface for layer below DTLS connect being established.
coap_session_t * coap_session_new_dtls_session(coap_session_t *session, coap_tick_t now)
Create a new DTLS session for the session.
int coap_dtls_context_set_cpsk(coap_context_t *coap_context, coap_dtls_cpsk_t *setup_data)
Set the DTLS context's default client PSK information.
#define COAP_DTLS_PKI_SETUP_VERSION
Latest PKI setup version.
#define COAP_DTLS_CPSK_SETUP_VERSION
Latest CPSK setup version.
@ COAP_DTLS_ROLE_CLIENT
Internal function invoked for client.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
@ COAP_EVENT_SESSION_CONNECTED
Triggered when TCP layer completes exchange of CSM information.
@ COAP_EVENT_TCP_FAILED
Triggered when TCP layer fails for some reason.
@ COAP_EVENT_SESSION_FAILED
Triggered when TCP layer fails following exchange of CSM information.
@ COAP_EVENT_SERVER_SESSION_NEW
Called in the CoAP IO loop if a new server-side session is created due to an incoming connection.
@ COAP_EVENT_SESSION_CLOSED
Triggered when TCP layer closes following exchange of CSM information.
@ 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...
@ COAP_EVENT_TCP_CLOSED
Triggered when TCP layer is closed.
@ COAP_EVENT_TCP_CONNECTED
Triggered when TCP layer connects.
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_lock_callback(c, func)
Dummy for no thread-safe code.
#define coap_lock_check_locked(c)
Dummy for no thread-safe code.
#define coap_log_debug(...)
coap_log_t coap_get_log_level(void)
Get the current logging level.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
const char * coap_endpoint_str(const coap_endpoint_t *endpoint)
Get endpoint description.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define coap_log_err(...)
#define coap_log_crit(...)
int coap_netif_dgrm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif datagram listem (udp).
void coap_netif_close_ep(coap_endpoint_t *endpoint)
Layer function interface for Netif close for a endpoint.
int coap_netif_strm_connect1(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif stream connect (tcp).
int coap_netif_strm_accept(coap_endpoint_t *endpoint, coap_session_t *session, void *extra)
Layer function interface for Netif stream accept.
int coap_netif_strm_listen(coap_endpoint_t *endpoint, const coap_address_t *listen_addr)
Layer function interface for Netif stream listem (tcp).
int coap_netif_dgrm_connect(coap_session_t *session, const coap_address_t *local_if, const coap_address_t *server, int default_port)
Layer function interface for Netif datagram connect (udp).
int coap_netif_available(coap_session_t *session)
Function interface to check whether netif for session is still available.
int coap_netif_available_ep(coap_endpoint_t *endpoint)
Function interface to check whether netif for endpoint is still available.
void coap_delete_oscore_associations(coap_session_t *session)
Cleanup all allocated OSCORE association information.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
#define COAP_PDU_MAX_UDP_HEADER_SIZE
#define COAP_PDU_MAX_TCP_HEADER_SIZE
#define COAP_MAX_MESSAGE_SIZE_TCP8
#define COAP_MAX_MESSAGE_SIZE_TCP0
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
#define COAP_MAX_MESSAGE_SIZE_TCP16
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_DEFAULT_PORT
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_TOKEN_DEFAULT_MAX
#define COAP_SIGNALING_OPTION_EXTENDED_TOKEN_LENGTH
coap_proto_t
CoAP protocol types.
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER
#define COAPS_DEFAULT_PORT
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.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE
@ COAP_SIGNALING_CODE_CSM
@ COAP_SIGNALING_CODE_PING
#define COAP_NON_PROBING_WAIT_BASE(s)
ssize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_queue_t *node)
int coap_session_refresh_psk_hint(coap_session_t *session, const coap_bin_const_t *psk_hint)
Refresh the session's current Identity Hint (PSK).
void coap_session_send_csm(coap_session_t *session)
Notify session transport has just connected and CSM exchange can now start.
coap_session_t * coap_new_client_session_psk2_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
coap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
size_t coap_session_max_pdu_rcv_size(const coap_session_t *session)
Get maximum acceptable receive PDU size.
coap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
coap_session_t * coap_endpoint_get_session(coap_endpoint_t *endpoint, const coap_packet_t *packet, coap_tick_t now)
Lookup the server session for the packet received on an endpoint, or create a new one.
#define COAP_ACK_RANDOM_FACTOR(s)
void coap_free_endpoint_lkd(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
coap_session_t * coap_new_client_session_psk_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
void coap_session_establish(coap_session_t *session)
Layer function interface for layer below session accept/connect being established.
coap_session_t * coap_new_client_session_pki_lkd(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
coap_session_t * coap_new_client_session_lkd(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_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
int coap_session_refresh_psk_key(coap_session_t *session, const coap_bin_const_t *psk_key)
Refresh the session's current pre-shared key (PSK).
void coap_session_connected(coap_session_t *session)
Notify session that it has just connected or reconnected.
coap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1, coap_fixed_point_t fp2)
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu)
Send a pdu according to the session's protocol.
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
coap_mid_t coap_session_send_ping_lkd(coap_session_t *session)
Send a ping message for the session.
coap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
void coap_session_free(coap_session_t *session)
void coap_session_mfree(coap_session_t *session)
void coap_session_release_lkd(coap_session_t *session)
Decrement reference counter on a session.
int coap_session_refresh_psk_identity(coap_session_t *session, const coap_bin_const_t *psk_identity)
Refresh the session's current pre-shared identity (PSK).
coap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session)
#define COAP_NON_MAX_RETRANSMIT(s)
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
#define COAP_NON_TIMEOUT(s)
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
#define COAP_NON_PARTIAL_TIMEOUT(s)
void coap_session_disconnected_lkd(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
coap_endpoint_t * coap_new_endpoint_lkd(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
coap_session_t * coap_new_server_session(coap_context_t *ctx, coap_endpoint_t *ep, void *extra)
Creates a new server session for the specified endpoint.
@ COAP_EXT_T_CHECKED
Token size valid.
COAP_API coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
coap_session_type_t
coap_session_type_t values
COAP_API void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason)
Notify session that it has failed.
void coap_session_set_mtu(coap_session_t *session, unsigned mtu)
Set the session MTU.
coap_context_t * coap_session_get_context(const coap_session_t *session)
Get the session context.
const coap_address_t * coap_session_get_addr_local(const coap_session_t *session)
Get the local IP address and port from the session.
coap_proto_t coap_session_get_proto(const coap_session_t *session)
Get the session protocol type.
const coap_bin_const_t * coap_session_get_psk_key(const coap_session_t *session)
Get the session's current pre-shared key (PSK).
void * coap_session_get_tls(const coap_session_t *session, coap_tls_library_t *tls_lib)
Get the session TLS security ptr (TLS type dependent)
coap_session_state_t coap_session_get_state(const coap_session_t *session)
Get the session state.
coap_session_state_t
coap_session_state_t values
#define COAP_PROTO_NOT_RELIABLE(p)
COAP_API coap_session_t * coap_new_client_session_pki(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_pki_t *setup_data)
Creates a new client session to the designated server with PKI credentials.
void coap_session_init_token(coap_session_t *session, size_t len, const uint8_t *data)
Initializes the token value to use as a starting point.
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
COAP_API coap_session_t * coap_new_client_session(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto)
Creates a new client session to the designated server.
const coap_bin_const_t * coap_session_get_psk_identity(const coap_session_t *session)
Get the server session's current PSK identity (PSK).
void coap_session_set_app_data(coap_session_t *session, void *app_data)
Stores data with the given session.
coap_session_t * coap_session_get_by_peer(const coap_context_t *ctx, const coap_address_t *remote_addr, int ifindex)
Get the session associated with the specified remote_addr and index.
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu)
Set the endpoint's default MTU.
const coap_bin_const_t * coap_session_get_psk_hint(const coap_session_t *session)
Get the server session's current Identity Hint (PSK).
COAP_API coap_endpoint_t * coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto)
Create a new endpoint for communicating with peers.
const coap_address_t * coap_session_get_addr_remote(const coap_session_t *session)
Get the remote IP address and port from the session.
int coap_session_set_type_client(coap_session_t *session)
Set the session type to client.
COAP_API void coap_free_endpoint(coap_endpoint_t *endpoint)
Release an endpoint and all the structures associated with it.
COAP_API void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
COAP_API coap_session_t * coap_new_client_session_psk2(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, coap_dtls_cpsk_t *setup_data)
Creates a new client session to the designated server with PSK credentials.
const coap_address_t * coap_session_get_addr_mcast(const coap_session_t *session)
Get the remote multicast IP address and port from the session if the original target IP was multicast...
COAP_API size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
COAP_API coap_session_t * coap_new_client_session_psk(coap_context_t *ctx, const coap_address_t *local_if, const coap_address_t *server, coap_proto_t proto, const char *identity, const uint8_t *key, unsigned key_len)
Creates a new client session to the designated server with PSK credentials.
int coap_session_get_ifindex(const coap_session_t *session)
Get the session if index.
void * coap_session_get_app_data(const coap_session_t *session)
Returns any application-specific data that has been stored with session using the function coap_sessi...
coap_session_type_t coap_session_get_type(const coap_session_t *session)
Get the session type.
@ COAP_SESSION_TYPE_HELLO
server-side ephemeral session for responding to a client hello
@ COAP_SESSION_TYPE_SERVER
server-side
@ COAP_SESSION_TYPE_CLIENT
client-side
@ COAP_SESSION_STATE_HANDSHAKE
@ COAP_SESSION_STATE_ESTABLISHED
@ COAP_SESSION_STATE_NONE
@ COAP_SESSION_STATE_CONNECTING
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
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...
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
int coap_cancel_observe_lkd(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
int coap_tcp_is_supported(void)
Check whether TCP is available.
int coap_tls_is_supported(void)
Check whether TLS is available.
int coap_ws_is_supported(void)
Check whether WebSockets is available.
int coap_dtls_is_supported(void)
Check whether DTLS is available.
int coap_wss_is_supported(void)
Check whether Secure WebSockets is available.
Only used for servers for hashing incoming packets.
coap_address_t remote
remote address and port
coap_proto_t proto
CoAP protocol.
coap_address_t remote
remote address and port
coap_address_t local
local address and port
Multi-purpose address abstraction.
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
The CoAP stack's global state is stored in a coap_context_t object.
coap_session_t * sessions
client sessions
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
uint32_t csm_max_message_size
Value for CSM Max-Message-Size.
unsigned int max_handshake_sessions
Maximum number of simultaneous negotating sessions per endpoint.
uint32_t max_token_size
Largest token size supported RFC8974.
coap_cache_entry_t * cache
CoAP cache-entry cache.
coap_endpoint_t * endpoint
the endpoints used for listening
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
unsigned int max_idle_sessions
Maximum number of simultaneous unused sessions per endpoint.
coap_bin_const_t identity
The structure used for defining the Client PSK setup data to be used.
char * client_sni
If not NULL, SNI to use in client TLS setup.
coap_dtls_cpsk_info_t psk_info
Client PSK definition.
The structure used for defining the PKI setup data to be used.
char * client_sni
If not NULL, SNI to use in client TLS setup.
Abstraction of virtual endpoint that can be attached to coap_context_t.
coap_context_t * context
endpoint's context
uint16_t default_mtu
default mtu for this interface
coap_session_t * sessions
hash table or list of active sessions
coap_address_t bind_addr
local interface address
coap_socket_t sock
socket object for the interface, if any
coap_proto_t proto
protocol used on this interface
Abstraction of a fixed point number that can be used where necessary instead of a float.
uint16_t fractional_part
Fractional part of fixed point variable 1/1000 (3 points) precision.
uint16_t integer_part
Integer part of fixed point variable.
coap_layer_establish_t l_establish
coap_layer_close_t l_close
Structure to hold large body (many blocks) client receive information.
coap_pdu_t pdu
skeletal PDU
coap_binary_t * app_token
app requesting PDU token
uint8_t observe_set
Set if this is an observe receive PDU.
Structure to hold large body (many blocks) server receive information.
Structure to hold large body (many blocks) transmission information.
size_t length
length of payload
coap_addr_tuple_t addr_info
local and remote addresses
unsigned char * payload
payload
int ifindex
the interface index
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
coap_bin_const_t actual_token
Actual token in pdu.
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
coap_pdu_type_t type
message type
coap_session_t * session
the CoAP session
coap_pdu_t * pdu
the CoAP PDU to send
unsigned int timeout
the randomized timeout value
struct coap_queue_t * next
coap_mid_t id
CoAP message id.
coap_tick_t t
when to send PDU for the next time
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
volatile uint8_t max_token_checked
Check for max token size coap_ext_token_check_t.
coap_bin_const_t * psk_key
If client, this field contains the current pre-shared key for server; When this field is NULL,...
coap_endpoint_t * endpoint
session's endpoint
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t doing_first
Set if doing client's first request.
coap_socket_t sock
socket object for the session, if any
coap_pdu_t * partial_pdu
incomplete incoming pdu
uint32_t max_token_size
Largest token size supported RFC8974.
uint16_t nstart
maximum concurrent confirmable xmits (default 1)
coap_bin_const_t * psk_identity
If client, this field contains the current identity for server; When this field is NULL,...
coap_session_state_t state
current state of relationship with peer
uint64_t tx_token
Next token number to use.
coap_bin_const_t * client_cid
Contains client CID or NULL.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
uint8_t is_dtls13
Set if session is DTLS1.3.
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv)
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
uint8_t doing_send_recv
Set if coap_send_recv() active.
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t no_observe_cancel
Set if do not cancel observe on session close.
size_t partial_read
if > 0 indicates number of bytes already read for an incoming message
int dtls_event
Tracking any (D)TLS events on this session.
uint16_t max_retransmit
maximum re-transmit count (default 4)
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
application-specific data
uint32_t tx_rtag
Next Request-Tag number to use.
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_bin_const_t * req_token
Token in request pdu of coap_send_recv()
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
coap_mid_t last_con_mid
The last CON mid that has been been processed.
coap_session_type_t type
client or server side socket
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_bin_const_t * echo
last token used to make a request
coap_layer_func_t lfunc[COAP_LAYER_LAST]
Layer functions to use.
coap_session_t * session
Used to determine session owner.
coap_endpoint_t * endpoint
Used by the epoll logic for a listening endpoint.
coap_address_t mcast_addr
remote address and port (multicast track)
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values