libcoap 4.3.5
Loading...
Searching...
No Matches
coap_session_internal.h
Go to the documentation of this file.
1/*
2 * coap_session_internal.h -- Structures, Enums & Functions that are not
3 * exposed to application programming
4 *
5 * Copyright (C) 2010-2024 Olaf Bergmann <bergmann@tzi.org>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
18#ifndef COAP_SESSION_INTERNAL_H_
19#define COAP_SESSION_INTERNAL_H_
20
21#include "coap_internal.h"
22#include "coap_io_internal.h"
23#include "coap_ws_internal.h"
24
25#define COAP_DEFAULT_SESSION_TIMEOUT 300
26#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
27#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
28
45
54
63
73 unsigned ref;
74 size_t tls_overhead;
75 size_t mtu;
76 size_t csm_rcv_mtu;
78 UT_hash_handle hh;
80 int ifindex;
83#if COAP_SERVER_SUPPORT
85#endif /* COAP_SERVER_SUPPORT */
87 void *tls;
88 uint16_t tx_mid;
90 uint8_t con_active;
97#if COAP_CLIENT_SUPPORT
99#endif /* COAP_CLIENT_SUPPORT */
100#if COAP_SERVER_SUPPORT
102#endif /* COAP_SERVER_SUPPORT */
106 uint8_t read_header[8];
145 void *app;
150 uint16_t max_retransmit;
152 uint16_t nstart;
156 uint32_t probing_rate;
158#if COAP_Q_BLOCK_SUPPORT
159 uint16_t max_payloads;
161 uint16_t non_max_retransmit;
163 coap_fixed_point_t non_timeout;
165 coap_fixed_point_t non_receive_timeout;
168 coap_fixed_point_t non_probing_wait_base;
171 coap_fixed_point_t non_partial_timeout;
174#endif /* COAP_Q_BLOCK_SUPPORT */
175 unsigned int dtls_timeout_count;
178 uint32_t tx_rtag;
179 uint32_t block_mode;
182 uint8_t doing_first;
187 uint8_t csm_not_seen;
188#if COAP_OSCORE_SUPPORT
189 uint8_t oscore_encryption;
190 COAP_OSCORE_B_2_STEP b_2_step;
191 oscore_recipient_ctx_t *recipient_ctx;
193 oscore_association_t *associations;
195 uint64_t oscore_r2;
196#endif /* COAP_OSCORE_SUPPORT */
197#if COAP_WS_SUPPORT
198 coap_ws_state_t *ws;
199 coap_str_const_t *ws_host;
200#endif /* COAP_WS_SUPPORT */
201#if COAP_OSCORE_SUPPORT
202 uint8_t done_b_1_2;
203#endif /* COAP_OSCORE_SUPPORT */
204 volatile uint8_t max_token_checked;
206#if COAP_CLIENT_SUPPORT
208#endif /* COAP_CLIENT_SUPPORT */
209 uint8_t is_dtls13;
212 uint32_t max_token_size;
213 uint64_t tx_token;
222#if COAP_SERVER_SUPPORT
224#endif /* COAP_SERVER_SUPPORT */
225};
226
227#if COAP_SERVER_SUPPORT
242#endif /* COAP_SERVER_SUPPORT */
243
246
248 uint32_t u2);
249
252
254 uint32_t u2);
255
257 uint32_t u2);
258
260
262
264
265
272
279
290 coap_nack_reason_t reason);
291
301
313 const coap_bin_const_t *psk_hint);
314
326 const coap_bin_const_t *psk_key);
327
339 const coap_bin_const_t *psk_identity);
340
341#if COAP_SERVER_SUPPORT
352 coap_endpoint_t *ep,
353 void *extra);
354#endif /* COAP_SERVER_SUPPORT */
355
365
375
386
398ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
399
401 coap_queue_t *node);
402
403#if COAP_SERVER_SUPPORT
414 const coap_packet_t *packet, coap_tick_t now);
415
432
441
442#endif /* COAP_SERVER_SUPPORT */
443
450size_t coap_session_max_pdu_rcv_size(const coap_session_t *session);
451
461size_t coap_session_max_pdu_size_lkd(const coap_session_t *session);
462
480 coap_context_t *ctx,
481 const coap_address_t *local_if,
482 const coap_address_t *server,
484);
485
505 const coap_address_t *local_if,
506 const coap_address_t *server,
508 coap_dtls_pki_t *setup_data
509 );
510
533 const coap_address_t *local_if,
534 const coap_address_t *server,
536 const char *identity,
537 const uint8_t *key,
538 unsigned key_len
539 );
540
560 const coap_address_t *local_if,
561 const coap_address_t *server,
563 coap_dtls_cpsk_t *setup_data
564 );
565
566
579 coap_tick_t now);
580
581void coap_session_free(coap_session_t *session);
583
585
587
588#define COAP_SESSION_REF(s) ((s)->ref
589
590/* RFC7252 */
591#define COAP_ACK_TIMEOUT(s) ((s)->ack_timeout)
592#define COAP_ACK_RANDOM_FACTOR(s) ((s)->ack_random_factor)
593#define COAP_MAX_RETRANSMIT(s) ((s)->max_retransmit)
594#define COAP_NSTART(s) ((s)->nstart)
595#define COAP_DEFAULT_LEISURE(s) ((s)->default_leisure)
596#define COAP_PROBING_RATE(s) ((s)->probing_rate)
597/* RFC9177 */
598#define COAP_MAX_PAYLOADS(s) ((s)->max_payloads)
599#define COAP_NON_MAX_RETRANSMIT(s) ((s)->non_max_retransmit)
600#define COAP_NON_TIMEOUT(s) ((s)->non_timeout)
601#define COAP_NON_TIMEOUT_TICKS(s) \
602 (COAP_NON_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
603 COAP_NON_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
604#define COAP_NON_RECEIVE_TIMEOUT(s) ((s)->non_receive_timeout)
605#define COAP_NON_PROBING_WAIT_BASE(s) ((s)->non_probing_wait_base)
606#define COAP_NON_PARTIAL_TIMEOUT(s) ((s)->non_partial_timeout)
607
614#define COAP_DEFAULT_LEISURE_TICKS(s) \
615 (COAP_DEFAULT_LEISURE(s).integer_part * COAP_TICKS_PER_SECOND + \
616 COAP_DEFAULT_LEISURE(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
623#define COAP_MAX_TRANSMIT_SPAN(s) \
624 (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part) * \
625 ((1 << ((s)->max_retransmit)) -1) * \
626 ((s)->ack_random_factor.integer_part * 1000 + \
627 (s)->ack_random_factor.fractional_part) \
628 / 1000000)
629
636#define COAP_MAX_TRANSMIT_WAIT(s) \
637 (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part) * \
638 ((1 << ((s)->max_retransmit + 1)) -1) * \
639 ((s)->ack_random_factor.integer_part * 1000 + \
640 (s)->ack_random_factor.fractional_part) \
641 / 1000000)
642
643#define COAP_MAX_TRANSMIT_WAIT_TICKS(s) \
644 (COAP_MAX_TRANSMIT_WAIT(s) * COAP_TICKS_PER_SECOND)
645
652#define COAP_PROCESSING_DELAY(s) \
653 (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part + \
654 500) / 1000)
655
662#define COAP_MAX_RTT(s) \
663 ((2 * COAP_DEFAULT_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
664
671#define COAP_EXCHANGE_LIFETIME(s) \
672 (COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_DEFAULT_MAX_LATENCY) + \
673 COAP_PROCESSING_DELAY(s))
674
681#define COAP_NON_LIFETIME(s) \
682 (COAP_MAX_TRANSMIT_SPAN(s) + COAP_DEFAULT_MAX_LATENCY)
683
690#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s) ( \
691 COAP_NON_RECEIVE_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
692 COAP_NON_RECEIVE_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
693
702#define COAP_NON_PROBING_WAIT(s) \
703 coap_add_fixed_fixed(COAP_NON_PROBING_WAIT_BASE(s), \
704 COAP_NON_TIMEOUT_RANDOM(s))
705
706#define COAP_NON_PROBING_WAIT_TICKS(s) \
707 (COAP_NON_PROBING_WAIT(s).integer_part * COAP_TICKS_PER_SECOND + \
708 COAP_NON_PROBING_WAIT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
709
716#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s) \
717 (COAP_NON_PARTIAL_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
718 COAP_NON_PARTIAL_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
719
726#define COAP_NON_TIMEOUT_RANDOM(s) \
727 coap_get_non_timeout_random(s)
728
731#define SESSIONS_ADD(e, obj) \
732 HASH_ADD(hh, (e), addr_hash, sizeof((obj)->addr_hash), (obj))
733
734#define SESSIONS_DELETE(e, obj) \
735 HASH_DELETE(hh, (e), (obj))
736
737#define SESSIONS_ITER(e, el, rtmp) \
738 HASH_ITER(hh, (e), el, rtmp)
739
740#define SESSIONS_ITER_SAFE(e, el, rtmp) \
741 for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
742
743#define SESSIONS_FIND(e, k, res) { \
744 HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
745 }
746
747#endif /* COAP_SESSION_INTERNAL_H_ */
Pulls together all the internal only header files.
coap_nack_reason_t
Definition coap_io.h:62
Internal network I/O functions.
Internal CoAP WebSockets support.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:143
coap_response_t
Definition coap_net.h:48
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_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:263
coap_proto_t
CoAP protocol types.
Definition coap_pdu.h:312
void coap_connect_session(coap_session_t *session, coap_tick_t now)
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)
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.
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.
coap_ext_token_check_t
coap_ext_token_check_t values
void coap_read_session(coap_context_t *ctx, coap_session_t *session, coap_tick_t now)
Definition coap_net.c:2080
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.
Definition coap_net.c:1001
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)
COAP_OSCORE_B_2_STEP
coap_session_t * coap_session_reference_lkd(coap_session_t *session)
Increment reference counter on a session.
coap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1, uint32_t u2)
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_NOT_CHECKED
Not checked.
@ COAP_EXT_T_CHECKING
Token size check request sent.
@ COAP_EXT_T_CHECKED
Token size valid.
@ COAP_OSCORE_B_2_NONE
@ COAP_OSCORE_B_2_STEP_3
@ COAP_OSCORE_B_2_STEP_1
@ COAP_OSCORE_B_2_STEP_4
@ COAP_OSCORE_B_2_STEP_5
@ COAP_OSCORE_B_2_STEP_2
coap_session_type_t
coap_session_type_t values
coap_session_state_t
coap_session_state_t values
Only used for servers for hashing incoming packets.
uint16_t lport
local port
coap_address_t remote
remote address and port
coap_proto_t proto
CoAP protocol.
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:64
The CoAP stack's global state is stored in a coap_context_t object.
The structure used for defining the Client PSK setup data to be used.
Definition coap_dtls.h:410
The structure used for defining the PKI setup data to be used.
Definition coap_dtls.h:312
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
struct coap_endpoint_t * next
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.
Structure to hold large body (many blocks) client receive information.
Structure to hold large body (many blocks) server receive information.
Structure to hold large body (many blocks) transmission information.
structure for CoAP PDUs
Queue entry.
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.
uint8_t csm_not_seen
Set if timeout waiting for CSM.
unsigned int dtls_timeout_count
dtls setup retry counter
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.
uint8_t delay_recursive
Set if in coap_client_delay_first()
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
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
coap_bin_const_t * client_cid
Contains client CID or NULL.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t read_header[8]
storage space for header of incoming message header
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint16_t tx_mid
the last message id that was used in this session
uint8_t is_dtls13
Set if session is DTLS1.3.
unsigned ref
reference count from queues
size_t csm_rcv_mtu
CSM mtu (rcv)
coap_response_t last_con_handler_res
The result of calling the response handler of the last CON.
coap_bin_const_t * psk_hint
If client, this field contains the server provided identity hint.
coap_bin_const_t * last_token
coap_dtls_cpsk_t cpsk_setup_data
client provided PSK initial setup data
size_t mtu
path or CSM mtu (xmt)
uint8_t negotiated_cid
Set for a client if CID negotiated.
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.
void * tls
security parameters
uint16_t max_retransmit
maximum re-transmit count (default 4)
uint8_t csm_block_supported
CSM TCP blocks supported.
coap_fixed_point_t ack_random_factor
ack random factor backoff (default 1.5)
uint8_t proxy_session
Set if this is an ongoing proxy session.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
size_t tls_overhead
overhead of TLS layer
void * app
application-specific data
uint32_t tx_rtag
Next Request-Tag number to use.
coap_mid_t last_ping_mid
the last keepalive message id that was used in this session
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_fixed_point_t ack_timeout
timeout waiting for ack (default 2.0 secs)
coap_fixed_point_t default_leisure
Mcast leisure time (default 5.0 secs)
coap_mid_t last_con_mid
The last CON mid that has been been processed.
coap_session_type_t type
client or server side socket
uint32_t probing_rate
Max transfer wait when remote is not respoding (default 1 byte/sec)
coap_mid_t last_ack_mid
The last ACK mid that has been been processed.
coap_context_t * context
session's context
size_t partial_write
if > 0 indicates number of bytes already written from the pdu at the head of sendqueue
coap_addr_hash_t addr_hash
Address hash for server incoming packets.
int ifindex
interface index
coap_bin_const_t * echo
last token used to make a request
CoAP string data definition with const data.
Definition coap_str.h:46
WebSockets session state.