1c87c5fbaSopenharmony_ci/*
2c87c5fbaSopenharmony_ci * coap_session_internal.h -- Structures, Enums & Functions that are not
3c87c5fbaSopenharmony_ci * exposed to application programming
4c87c5fbaSopenharmony_ci *
5c87c5fbaSopenharmony_ci * Copyright (C) 2010-2023 Olaf Bergmann <bergmann@tzi.org>
6c87c5fbaSopenharmony_ci *
7c87c5fbaSopenharmony_ci * SPDX-License-Identifier: BSD-2-Clause
8c87c5fbaSopenharmony_ci *
9c87c5fbaSopenharmony_ci * This file is part of the CoAP library libcoap. Please see README for terms
10c87c5fbaSopenharmony_ci * of use.
11c87c5fbaSopenharmony_ci */
12c87c5fbaSopenharmony_ci
13c87c5fbaSopenharmony_ci/**
14c87c5fbaSopenharmony_ci * @file coap_session_internal.h
15c87c5fbaSopenharmony_ci * @brief CoAP session internal information
16c87c5fbaSopenharmony_ci */
17c87c5fbaSopenharmony_ci
18c87c5fbaSopenharmony_ci#ifndef COAP_SESSION_INTERNAL_H_
19c87c5fbaSopenharmony_ci#define COAP_SESSION_INTERNAL_H_
20c87c5fbaSopenharmony_ci
21c87c5fbaSopenharmony_ci#include "coap_internal.h"
22c87c5fbaSopenharmony_ci#include "coap_io_internal.h"
23c87c5fbaSopenharmony_ci#include "coap_ws_internal.h"
24c87c5fbaSopenharmony_ci
25c87c5fbaSopenharmony_ci#define COAP_DEFAULT_SESSION_TIMEOUT 300
26c87c5fbaSopenharmony_ci#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
27c87c5fbaSopenharmony_ci#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
28c87c5fbaSopenharmony_ci
29c87c5fbaSopenharmony_ci/**
30c87c5fbaSopenharmony_ci * @ingroup internal_api
31c87c5fbaSopenharmony_ci * @defgroup session_internal Sessions
32c87c5fbaSopenharmony_ci * Internal API for handling Sessions
33c87c5fbaSopenharmony_ci * @{
34c87c5fbaSopenharmony_ci */
35c87c5fbaSopenharmony_ci
36c87c5fbaSopenharmony_ci/**
37c87c5fbaSopenharmony_ci * Only used for servers for hashing incoming packets. Cannot have local IP
38c87c5fbaSopenharmony_ci * address as this may be an initial multicast and subsequent unicast address
39c87c5fbaSopenharmony_ci */
40c87c5fbaSopenharmony_cistruct coap_addr_hash_t {
41c87c5fbaSopenharmony_ci  coap_address_t remote;       /**< remote address and port */
42c87c5fbaSopenharmony_ci  uint16_t lport;              /**< local port */
43c87c5fbaSopenharmony_ci  coap_proto_t proto;          /**< CoAP protocol */
44c87c5fbaSopenharmony_ci};
45c87c5fbaSopenharmony_ci
46c87c5fbaSopenharmony_citypedef enum {
47c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_NONE = 0,
48c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP_1,
49c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP_2,
50c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP_3,
51c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP_4,
52c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP_5,
53c87c5fbaSopenharmony_ci} COAP_OSCORE_B_2_STEP;
54c87c5fbaSopenharmony_ci
55c87c5fbaSopenharmony_ci/**
56c87c5fbaSopenharmony_ci * coap_ext_token_check_t values
57c87c5fbaSopenharmony_ci */
58c87c5fbaSopenharmony_citypedef enum coap_ext_token_check_t {
59c87c5fbaSopenharmony_ci  COAP_EXT_T_NOT_CHECKED = 0, /**< Not checked */
60c87c5fbaSopenharmony_ci  COAP_EXT_T_CHECKED,         /**< Token size valid */
61c87c5fbaSopenharmony_ci  COAP_EXT_T_CHECKING,        /**< Token size check request sent */
62c87c5fbaSopenharmony_ci} coap_ext_token_check_t;
63c87c5fbaSopenharmony_ci
64c87c5fbaSopenharmony_ci/**
65c87c5fbaSopenharmony_ci * Abstraction of virtual session that can be attached to coap_context_t
66c87c5fbaSopenharmony_ci * (client) or coap_endpoint_t (server).
67c87c5fbaSopenharmony_ci */
68c87c5fbaSopenharmony_cistruct coap_session_t {
69c87c5fbaSopenharmony_ci  coap_proto_t proto;               /**< protocol used */
70c87c5fbaSopenharmony_ci  coap_session_type_t type;         /**< client or server side socket */
71c87c5fbaSopenharmony_ci  coap_session_state_t state;       /**< current state of relationship with
72c87c5fbaSopenharmony_ci                                         peer */
73c87c5fbaSopenharmony_ci  unsigned ref;                     /**< reference count from queues */
74c87c5fbaSopenharmony_ci  size_t tls_overhead;              /**< overhead of TLS layer */
75c87c5fbaSopenharmony_ci  size_t mtu;                       /**< path or CSM mtu (xmt) */
76c87c5fbaSopenharmony_ci  size_t csm_rcv_mtu;               /**< CSM mtu (rcv) */
77c87c5fbaSopenharmony_ci  coap_addr_hash_t addr_hash;  /**< Address hash for server incoming packets */
78c87c5fbaSopenharmony_ci  UT_hash_handle hh;
79c87c5fbaSopenharmony_ci  coap_addr_tuple_t addr_info;      /**< remote/local address info */
80c87c5fbaSopenharmony_ci  int ifindex;                      /**< interface index */
81c87c5fbaSopenharmony_ci  coap_socket_t sock;               /**< socket object for the session, if
82c87c5fbaSopenharmony_ci                                         any */
83c87c5fbaSopenharmony_ci#if COAP_SERVER_SUPPORT
84c87c5fbaSopenharmony_ci  coap_endpoint_t *endpoint;        /**< session's endpoint */
85c87c5fbaSopenharmony_ci#endif /* COAP_SERVER_SUPPORT */
86c87c5fbaSopenharmony_ci  coap_context_t *context;          /**< session's context */
87c87c5fbaSopenharmony_ci  void *tls;                        /**< security parameters */
88c87c5fbaSopenharmony_ci  uint16_t tx_mid;                  /**< the last message id that was used in
89c87c5fbaSopenharmony_ci                                         this session */
90c87c5fbaSopenharmony_ci  uint8_t con_active;               /**< Active CON request sent */
91c87c5fbaSopenharmony_ci  uint8_t csm_block_supported;      /**< CSM TCP blocks supported */
92c87c5fbaSopenharmony_ci  coap_mid_t last_ping_mid;         /**< the last keepalive message id that was
93c87c5fbaSopenharmony_ci                                         used in this session */
94c87c5fbaSopenharmony_ci  coap_queue_t *delayqueue;         /**< list of delayed messages waiting to
95c87c5fbaSopenharmony_ci                                         be sent */
96c87c5fbaSopenharmony_ci  coap_lg_xmit_t *lg_xmit;          /**< list of large transmissions */
97c87c5fbaSopenharmony_ci#if COAP_CLIENT_SUPPORT
98c87c5fbaSopenharmony_ci  coap_lg_crcv_t *lg_crcv;       /**< Client list of expected large receives */
99c87c5fbaSopenharmony_ci#endif /* COAP_CLIENT_SUPPORT */
100c87c5fbaSopenharmony_ci#if COAP_SERVER_SUPPORT
101c87c5fbaSopenharmony_ci  coap_lg_srcv_t *lg_srcv;       /**< Server list of expected large receives */
102c87c5fbaSopenharmony_ci#endif /* COAP_SERVER_SUPPORT */
103c87c5fbaSopenharmony_ci  size_t partial_write;             /**< if > 0 indicates number of bytes
104c87c5fbaSopenharmony_ci                                         already written from the pdu at the
105c87c5fbaSopenharmony_ci                                         head of sendqueue */
106c87c5fbaSopenharmony_ci  uint8_t read_header[8];           /**< storage space for header of incoming
107c87c5fbaSopenharmony_ci                                         message header */
108c87c5fbaSopenharmony_ci  size_t partial_read;              /**< if > 0 indicates number of bytes
109c87c5fbaSopenharmony_ci                                        already read for an incoming message */
110c87c5fbaSopenharmony_ci  coap_pdu_t *partial_pdu;          /**< incomplete incoming pdu */
111c87c5fbaSopenharmony_ci  coap_tick_t last_rx_tx;
112c87c5fbaSopenharmony_ci  coap_tick_t last_tx_rst;
113c87c5fbaSopenharmony_ci  coap_tick_t last_ping;
114c87c5fbaSopenharmony_ci  coap_tick_t last_pong;
115c87c5fbaSopenharmony_ci  coap_tick_t csm_tx;
116c87c5fbaSopenharmony_ci  coap_dtls_cpsk_t cpsk_setup_data; /**< client provided PSK initial setup
117c87c5fbaSopenharmony_ci                                         data */
118c87c5fbaSopenharmony_ci  coap_bin_const_t *psk_identity;   /**< If client, this field contains the
119c87c5fbaSopenharmony_ci                                      current identity for server; When this
120c87c5fbaSopenharmony_ci                                      field is NULL, the current identity is
121c87c5fbaSopenharmony_ci                                      contained in cpsk_setup_data
122c87c5fbaSopenharmony_ci
123c87c5fbaSopenharmony_ci                                      If server, this field contains the client
124c87c5fbaSopenharmony_ci                                      provided identity.
125c87c5fbaSopenharmony_ci
126c87c5fbaSopenharmony_ci                                      Value maintained internally */
127c87c5fbaSopenharmony_ci  coap_bin_const_t *psk_key;        /**< If client, this field contains the
128c87c5fbaSopenharmony_ci                                      current pre-shared key for server;
129c87c5fbaSopenharmony_ci                                      When this field is NULL, the current
130c87c5fbaSopenharmony_ci                                      key is contained in cpsk_setup_data
131c87c5fbaSopenharmony_ci
132c87c5fbaSopenharmony_ci                                      If server, this field contains the
133c87c5fbaSopenharmony_ci                                      client's current key.
134c87c5fbaSopenharmony_ci
135c87c5fbaSopenharmony_ci                                      Value maintained internally */
136c87c5fbaSopenharmony_ci  coap_bin_const_t *psk_hint;       /**< If client, this field contains the
137c87c5fbaSopenharmony_ci                                      server provided identity hint.
138c87c5fbaSopenharmony_ci
139c87c5fbaSopenharmony_ci                                      If server, this field contains the
140c87c5fbaSopenharmony_ci                                      current hint for the client; When this
141c87c5fbaSopenharmony_ci                                      field is NULL, the current hint is
142c87c5fbaSopenharmony_ci                                      contained in context->spsk_setup_data
143c87c5fbaSopenharmony_ci
144c87c5fbaSopenharmony_ci                                      Value maintained internally */
145c87c5fbaSopenharmony_ci  void *app;                        /**< application-specific data */
146c87c5fbaSopenharmony_ci  coap_fixed_point_t ack_timeout;   /**< timeout waiting for ack
147c87c5fbaSopenharmony_ci                                         (default 2.0 secs) */
148c87c5fbaSopenharmony_ci  coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default
149c87c5fbaSopenharmony_ci                                             1.5) */
150c87c5fbaSopenharmony_ci  uint16_t max_retransmit;          /**< maximum re-transmit count
151c87c5fbaSopenharmony_ci                                         (default 4) */
152c87c5fbaSopenharmony_ci  uint16_t nstart;                  /**< maximum concurrent confirmable xmits
153c87c5fbaSopenharmony_ci                                         (default 1) */
154c87c5fbaSopenharmony_ci  coap_fixed_point_t default_leisure; /**< Mcast leisure time
155c87c5fbaSopenharmony_ci                                           (default 5.0 secs) */
156c87c5fbaSopenharmony_ci  uint32_t probing_rate;            /**< Max transfer wait when remote is not
157c87c5fbaSopenharmony_ci                                         respoding (default 1 byte/sec) */
158c87c5fbaSopenharmony_ci#if COAP_Q_BLOCK_SUPPORT
159c87c5fbaSopenharmony_ci  uint16_t max_payloads;            /**< maximum Q-BlockX payloads before delay
160c87c5fbaSopenharmony_ci                                         (default 10) */
161c87c5fbaSopenharmony_ci  uint16_t non_max_retransmit;      /**< maximum Q-BlockX non re-transmit count
162c87c5fbaSopenharmony_ci                                         (default 4) */
163c87c5fbaSopenharmony_ci  coap_fixed_point_t non_timeout;   /**< Q-BlockX timeout waiting for response
164c87c5fbaSopenharmony_ci                                         (default 2.0 secs) */
165c87c5fbaSopenharmony_ci  coap_fixed_point_t non_receive_timeout;  /**< Q-BlockX receive timeout before
166c87c5fbaSopenharmony_ci                                         requesting missing packets.
167c87c5fbaSopenharmony_ci                                         (default 4.0 secs) */
168c87c5fbaSopenharmony_ci  coap_fixed_point_t non_probing_wait_base; /**< Q-BlockX max wait time base
169c87c5fbaSopenharmony_ci                                              while probing
170c87c5fbaSopenharmony_ci                                             (default 247.0 secs) */
171c87c5fbaSopenharmony_ci  coap_fixed_point_t non_partial_timeout; /**< Q-BlockX time to wait before
172c87c5fbaSopenharmony_ci                                           discarding partial data for a body.
173c87c5fbaSopenharmony_ci                                           (default 247.0 secs) */
174c87c5fbaSopenharmony_ci#endif /* COAP_Q_BLOCK_SUPPORT */
175c87c5fbaSopenharmony_ci  unsigned int dtls_timeout_count;      /**< dtls setup retry counter */
176c87c5fbaSopenharmony_ci  int dtls_event;                       /**< Tracking any (D)TLS events on this
177c87c5fbaSopenharmony_ci                                             session */
178c87c5fbaSopenharmony_ci  uint32_t tx_rtag;               /**< Next Request-Tag number to use */
179c87c5fbaSopenharmony_ci  uint8_t csm_bert_rem_support;  /**< CSM TCP BERT blocks supported (remote) */
180c87c5fbaSopenharmony_ci  uint8_t csm_bert_loc_support;  /**< CSM TCP BERT blocks supported (local) */
181c87c5fbaSopenharmony_ci  uint8_t block_mode;             /**< Zero or more COAP_BLOCK_ or'd options */
182c87c5fbaSopenharmony_ci  uint8_t doing_first;            /**< Set if doing client's first request */
183c87c5fbaSopenharmony_ci  uint8_t proxy_session;        /**< Set if this is an ongoing proxy session */
184c87c5fbaSopenharmony_ci  uint8_t delay_recursive;        /**< Set if in coap_client_delay_first() */
185c87c5fbaSopenharmony_ci  uint8_t no_observe_cancel;      /**< Set if do not cancel observe on session
186c87c5fbaSopenharmony_ci                                       close */
187c87c5fbaSopenharmony_ci#if COAP_OSCORE_SUPPORT
188c87c5fbaSopenharmony_ci  uint8_t oscore_encryption;      /**< OSCORE is used for this session  */
189c87c5fbaSopenharmony_ci  COAP_OSCORE_B_2_STEP b_2_step;  /**< Appendix B.2 negotiation step */
190c87c5fbaSopenharmony_ci  oscore_recipient_ctx_t *recipient_ctx; /**< OSCORE recipient context
191c87c5fbaSopenharmony_ci                                              for session */
192c87c5fbaSopenharmony_ci  oscore_association_t *associations; /**< OSCORE set of response
193c87c5fbaSopenharmony_ci                                           associations */
194c87c5fbaSopenharmony_ci  uint64_t oscore_r2;             /**< R2 for RFC8613 Appendix B.2 */
195c87c5fbaSopenharmony_ci#endif /* COAP_OSCORE_SUPPORT */
196c87c5fbaSopenharmony_ci#if COAP_WS_SUPPORT
197c87c5fbaSopenharmony_ci  coap_ws_state_t *ws;            /**< WS state */
198c87c5fbaSopenharmony_ci  coap_str_const_t *ws_host;      /**< Host to use in WS Request */
199c87c5fbaSopenharmony_ci#endif /* COAP_WS_SUPPORT */
200c87c5fbaSopenharmony_ci  volatile uint8_t max_token_checked; /**< Check for max token size
201c87c5fbaSopenharmony_ci                                           coap_ext_token_check_t */
202c87c5fbaSopenharmony_ci  uint16_t remote_test_mid;       /**< mid used for checking remote
203c87c5fbaSopenharmony_ci                                       support */
204c87c5fbaSopenharmony_ci  uint32_t max_token_size;        /**< Largest token size supported RFC8974 */
205c87c5fbaSopenharmony_ci  uint64_t tx_token;              /**< Next token number to use */
206c87c5fbaSopenharmony_ci  coap_bin_const_t *last_token;   /** last token used to make a request */
207c87c5fbaSopenharmony_ci  coap_bin_const_t *echo;         /**< Echo value to send with next request */
208c87c5fbaSopenharmony_ci  coap_mid_t last_ack_mid;        /**< The last ACK mid that has been
209c87c5fbaSopenharmony_ci                                       been processed */
210c87c5fbaSopenharmony_ci  coap_mid_t last_con_mid;        /**< The last CON mid that has been
211c87c5fbaSopenharmony_ci                                       been processed */
212c87c5fbaSopenharmony_ci};
213c87c5fbaSopenharmony_ci
214c87c5fbaSopenharmony_ci#if COAP_SERVER_SUPPORT
215c87c5fbaSopenharmony_ci/**
216c87c5fbaSopenharmony_ci * Abstraction of virtual endpoint that can be attached to coap_context_t. The
217c87c5fbaSopenharmony_ci * keys (port, bind_addr) must uniquely identify this endpoint.
218c87c5fbaSopenharmony_ci */
219c87c5fbaSopenharmony_cistruct coap_endpoint_t {
220c87c5fbaSopenharmony_ci  struct coap_endpoint_t *next;
221c87c5fbaSopenharmony_ci  coap_context_t *context;        /**< endpoint's context */
222c87c5fbaSopenharmony_ci  coap_proto_t proto;             /**< protocol used on this interface */
223c87c5fbaSopenharmony_ci  uint16_t default_mtu;           /**< default mtu for this interface */
224c87c5fbaSopenharmony_ci  coap_socket_t sock;             /**< socket object for the interface, if
225c87c5fbaSopenharmony_ci                                       any */
226c87c5fbaSopenharmony_ci  coap_address_t bind_addr;       /**< local interface address */
227c87c5fbaSopenharmony_ci  coap_session_t *sessions;       /**< hash table or list of active sessions */
228c87c5fbaSopenharmony_ci};
229c87c5fbaSopenharmony_ci#endif /* COAP_SERVER_SUPPORT */
230c87c5fbaSopenharmony_ci
231c87c5fbaSopenharmony_cicoap_fixed_point_t coap_multi_fixed_fixed(coap_fixed_point_t fp1,
232c87c5fbaSopenharmony_ci                                          coap_fixed_point_t fp2);
233c87c5fbaSopenharmony_ci
234c87c5fbaSopenharmony_cicoap_fixed_point_t coap_multi_fixed_uint(coap_fixed_point_t fp1,
235c87c5fbaSopenharmony_ci                                         uint32_t u2);
236c87c5fbaSopenharmony_ci
237c87c5fbaSopenharmony_cicoap_fixed_point_t coap_add_fixed_fixed(coap_fixed_point_t fp1,
238c87c5fbaSopenharmony_ci                                        coap_fixed_point_t fp2);
239c87c5fbaSopenharmony_ci
240c87c5fbaSopenharmony_cicoap_fixed_point_t coap_add_fixed_uint(coap_fixed_point_t fp1,
241c87c5fbaSopenharmony_ci                                       uint32_t u2);
242c87c5fbaSopenharmony_ci
243c87c5fbaSopenharmony_cicoap_fixed_point_t coap_sub_fixed_uint(coap_fixed_point_t fp1,
244c87c5fbaSopenharmony_ci                                       uint32_t u2);
245c87c5fbaSopenharmony_ci
246c87c5fbaSopenharmony_cicoap_fixed_point_t coap_div_fixed_uint(coap_fixed_point_t fp1, uint32_t u2);
247c87c5fbaSopenharmony_ci
248c87c5fbaSopenharmony_cicoap_fixed_point_t coap_get_non_timeout_random(coap_session_t *session);
249c87c5fbaSopenharmony_ci
250c87c5fbaSopenharmony_cicoap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session);
251c87c5fbaSopenharmony_ci
252c87c5fbaSopenharmony_ci
253c87c5fbaSopenharmony_ci/**
254c87c5fbaSopenharmony_ci * Notify session transport has just connected and CSM exchange can now start.
255c87c5fbaSopenharmony_ci *
256c87c5fbaSopenharmony_ci * @param session The CoAP session.
257c87c5fbaSopenharmony_ci */
258c87c5fbaSopenharmony_civoid coap_session_send_csm(coap_session_t *session);
259c87c5fbaSopenharmony_ci
260c87c5fbaSopenharmony_ci/**
261c87c5fbaSopenharmony_ci * Notify session that it has just connected or reconnected.
262c87c5fbaSopenharmony_ci *
263c87c5fbaSopenharmony_ci * @param session The CoAP session.
264c87c5fbaSopenharmony_ci */
265c87c5fbaSopenharmony_civoid coap_session_connected(coap_session_t *session);
266c87c5fbaSopenharmony_ci
267c87c5fbaSopenharmony_ci/**
268c87c5fbaSopenharmony_ci * Refresh the session's current Identity Hint (PSK).
269c87c5fbaSopenharmony_ci * Note: A copy of @p psk_hint is maintained in the session by libcoap.
270c87c5fbaSopenharmony_ci *
271c87c5fbaSopenharmony_ci * @param session  The current coap_session_t object.
272c87c5fbaSopenharmony_ci * @param psk_hint If NULL, the Identity Hint will revert to the
273c87c5fbaSopenharmony_ci *                 initial Identity Hint used at session setup.
274c87c5fbaSopenharmony_ci *
275c87c5fbaSopenharmony_ci * @return @c 1 if successful, else @c 0.
276c87c5fbaSopenharmony_ci */
277c87c5fbaSopenharmony_ciint coap_session_refresh_psk_hint(coap_session_t *session,
278c87c5fbaSopenharmony_ci                                  const coap_bin_const_t *psk_hint);
279c87c5fbaSopenharmony_ci
280c87c5fbaSopenharmony_ci/**
281c87c5fbaSopenharmony_ci * Refresh the session's current pre-shared key (PSK).
282c87c5fbaSopenharmony_ci * Note: A copy of @p psk_key is maintained in the session by libcoap.
283c87c5fbaSopenharmony_ci *
284c87c5fbaSopenharmony_ci * @param session  The current coap_session_t object.
285c87c5fbaSopenharmony_ci * @param psk_key  If NULL, the pre-shared key will revert to the
286c87c5fbaSopenharmony_ci *                 initial pre-shared key used at session setup.
287c87c5fbaSopenharmony_ci *
288c87c5fbaSopenharmony_ci * @return @c 1 if successful, else @c 0.
289c87c5fbaSopenharmony_ci */
290c87c5fbaSopenharmony_ciint coap_session_refresh_psk_key(coap_session_t *session,
291c87c5fbaSopenharmony_ci                                 const coap_bin_const_t *psk_key);
292c87c5fbaSopenharmony_ci
293c87c5fbaSopenharmony_ci/**
294c87c5fbaSopenharmony_ci * Refresh the session's current pre-shared identity (PSK).
295c87c5fbaSopenharmony_ci * Note: A copy of @p psk_identity is maintained in the session by libcoap.
296c87c5fbaSopenharmony_ci *
297c87c5fbaSopenharmony_ci * @param session  The current coap_session_t object.
298c87c5fbaSopenharmony_ci * @param psk_identity  If NULL, the pre-shared identity will revert to the
299c87c5fbaSopenharmony_ci *                 initial pre-shared key used as session setup.
300c87c5fbaSopenharmony_ci *
301c87c5fbaSopenharmony_ci * @return @c 1 if successful, else @c 0.
302c87c5fbaSopenharmony_ci */
303c87c5fbaSopenharmony_ciint coap_session_refresh_psk_identity(coap_session_t *session,
304c87c5fbaSopenharmony_ci                                      const coap_bin_const_t *psk_identity);
305c87c5fbaSopenharmony_ci
306c87c5fbaSopenharmony_ci#if COAP_SERVER_SUPPORT
307c87c5fbaSopenharmony_ci/**
308c87c5fbaSopenharmony_ci * Creates a new server session for the specified endpoint.
309c87c5fbaSopenharmony_ci * @param ctx The CoAP context.
310c87c5fbaSopenharmony_ci * @param ep An endpoint where an incoming connection request is pending.
311c87c5fbaSopenharmony_ci *
312c87c5fbaSopenharmony_ci * @return A new CoAP session or NULL if failed. Call coap_session_release to
313c87c5fbaSopenharmony_ci * add to unused queue.
314c87c5fbaSopenharmony_ci */
315c87c5fbaSopenharmony_cicoap_session_t *coap_new_server_session(
316c87c5fbaSopenharmony_ci    coap_context_t *ctx,
317c87c5fbaSopenharmony_ci    coap_endpoint_t *ep
318c87c5fbaSopenharmony_ci);
319c87c5fbaSopenharmony_ci#endif /* COAP_SERVER_SUPPORT */
320c87c5fbaSopenharmony_ci
321c87c5fbaSopenharmony_ci/**
322c87c5fbaSopenharmony_ci * Layer function interface for layer below session accept/connect being
323c87c5fbaSopenharmony_ci * established. This function initiates a CSM request for reliable protocols,
324c87c5fbaSopenharmony_ci * or coap_session_connect() for un-reliable protocols.
325c87c5fbaSopenharmony_ci *
326c87c5fbaSopenharmony_ci * @param session Session that the lower layer accept/connect was done on.
327c87c5fbaSopenharmony_ci *
328c87c5fbaSopenharmony_ci */
329c87c5fbaSopenharmony_civoid coap_session_establish(coap_session_t *session);
330c87c5fbaSopenharmony_ci
331c87c5fbaSopenharmony_ci/**
332c87c5fbaSopenharmony_ci * Send a pdu according to the session's protocol. This function returns
333c87c5fbaSopenharmony_ci * the number of bytes that have been transmitted, or a value less than zero
334c87c5fbaSopenharmony_ci * on error.
335c87c5fbaSopenharmony_ci *
336c87c5fbaSopenharmony_ci * @param session          Session to send pdu on.
337c87c5fbaSopenharmony_ci * @param pdu              The pdu to send.
338c87c5fbaSopenharmony_ci *
339c87c5fbaSopenharmony_ci * @return                 The number of bytes written on success, or a value
340c87c5fbaSopenharmony_ci *                         less than zero on error.
341c87c5fbaSopenharmony_ci */
342c87c5fbaSopenharmony_cissize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
343c87c5fbaSopenharmony_ci
344c87c5fbaSopenharmony_cissize_t coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
345c87c5fbaSopenharmony_ci                               coap_queue_t *node);
346c87c5fbaSopenharmony_ci
347c87c5fbaSopenharmony_ci#if COAP_SERVER_SUPPORT
348c87c5fbaSopenharmony_ci/**
349c87c5fbaSopenharmony_ci * Lookup the server session for the packet received on an endpoint, or create
350c87c5fbaSopenharmony_ci * a new one.
351c87c5fbaSopenharmony_ci *
352c87c5fbaSopenharmony_ci * @param endpoint Active endpoint the packet was received on.
353c87c5fbaSopenharmony_ci * @param packet Received packet.
354c87c5fbaSopenharmony_ci * @param now The current time in ticks.
355c87c5fbaSopenharmony_ci * @return The CoAP session or @c NULL if error.
356c87c5fbaSopenharmony_ci */
357c87c5fbaSopenharmony_cicoap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint,
358c87c5fbaSopenharmony_ci                                          const coap_packet_t *packet, coap_tick_t now);
359c87c5fbaSopenharmony_ci#endif /* COAP_SERVER_SUPPORT */
360c87c5fbaSopenharmony_ci
361c87c5fbaSopenharmony_ci/**
362c87c5fbaSopenharmony_ci * Get maximum acceptable receive PDU size
363c87c5fbaSopenharmony_ci *
364c87c5fbaSopenharmony_ci * @param session The CoAP session.
365c87c5fbaSopenharmony_ci * @return maximum PDU size, not including header (but including token).
366c87c5fbaSopenharmony_ci */
367c87c5fbaSopenharmony_cisize_t coap_session_max_pdu_rcv_size(const coap_session_t *session);
368c87c5fbaSopenharmony_ci
369c87c5fbaSopenharmony_ci/**
370c87c5fbaSopenharmony_ci * Create a new DTLS session for the @p session.
371c87c5fbaSopenharmony_ci * Note: the @p session is released if no DTLS server session can be created.
372c87c5fbaSopenharmony_ci *
373c87c5fbaSopenharmony_ci * @ingroup dtls_internal
374c87c5fbaSopenharmony_ci *
375c87c5fbaSopenharmony_ci * @param session   Session to add DTLS session to
376c87c5fbaSopenharmony_ci * @param now       The current time in ticks.
377c87c5fbaSopenharmony_ci *
378c87c5fbaSopenharmony_ci * @return CoAP session or @c NULL if error.
379c87c5fbaSopenharmony_ci */
380c87c5fbaSopenharmony_cicoap_session_t *coap_session_new_dtls_session(coap_session_t *session,
381c87c5fbaSopenharmony_ci                                              coap_tick_t now);
382c87c5fbaSopenharmony_ci
383c87c5fbaSopenharmony_civoid coap_session_free(coap_session_t *session);
384c87c5fbaSopenharmony_civoid coap_session_mfree(coap_session_t *session);
385c87c5fbaSopenharmony_ci
386c87c5fbaSopenharmony_ci#define COAP_SESSION_REF(s) ((s)->ref
387c87c5fbaSopenharmony_ci
388c87c5fbaSopenharmony_ci/* RFC7252 */
389c87c5fbaSopenharmony_ci#define COAP_ACK_TIMEOUT(s) ((s)->ack_timeout)
390c87c5fbaSopenharmony_ci#define COAP_ACK_RANDOM_FACTOR(s) ((s)->ack_random_factor)
391c87c5fbaSopenharmony_ci#define COAP_MAX_RETRANSMIT(s) ((s)->max_retransmit)
392c87c5fbaSopenharmony_ci#define COAP_NSTART(s) ((s)->nstart)
393c87c5fbaSopenharmony_ci#define COAP_DEFAULT_LEISURE(s) ((s)->default_leisure)
394c87c5fbaSopenharmony_ci#define COAP_PROBING_RATE(s) ((s)->probing_rate)
395c87c5fbaSopenharmony_ci/* RFC9177 */
396c87c5fbaSopenharmony_ci#define COAP_MAX_PAYLOADS(s) ((s)->max_payloads)
397c87c5fbaSopenharmony_ci#define COAP_NON_MAX_RETRANSMIT(s) ((s)->non_max_retransmit)
398c87c5fbaSopenharmony_ci#define COAP_NON_TIMEOUT(s) ((s)->non_timeout)
399c87c5fbaSopenharmony_ci#define COAP_NON_TIMEOUT_TICKS(s) \
400c87c5fbaSopenharmony_ci  (COAP_NON_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
401c87c5fbaSopenharmony_ci   COAP_NON_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
402c87c5fbaSopenharmony_ci#define COAP_NON_RECEIVE_TIMEOUT(s) ((s)->non_receive_timeout)
403c87c5fbaSopenharmony_ci#define COAP_NON_PROBING_WAIT_BASE(s) ((s)->non_probing_wait_base)
404c87c5fbaSopenharmony_ci#define COAP_NON_PARTIAL_TIMEOUT(s) ((s)->non_partial_timeout)
405c87c5fbaSopenharmony_ci
406c87c5fbaSopenharmony_ci/**
407c87c5fbaSopenharmony_ci * The DEFAULT_LEISURE definition for the session (s).
408c87c5fbaSopenharmony_ci *
409c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8
410c87c5fbaSopenharmony_ci * Initial value 5.0 seconds
411c87c5fbaSopenharmony_ci */
412c87c5fbaSopenharmony_ci#define COAP_DEFAULT_LEISURE_TICKS(s) \
413c87c5fbaSopenharmony_ci  (COAP_DEFAULT_LEISURE(s).integer_part * COAP_TICKS_PER_SECOND + \
414c87c5fbaSopenharmony_ci   COAP_DEFAULT_LEISURE(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
415c87c5fbaSopenharmony_ci/**
416c87c5fbaSopenharmony_ci * The MAX_TRANSMIT_SPAN definition for the session (s).
417c87c5fbaSopenharmony_ci *
418c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_SPAN
419c87c5fbaSopenharmony_ci *  ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT)) - 1) * ACK_RANDOM_FACTOR
420c87c5fbaSopenharmony_ci */
421c87c5fbaSopenharmony_ci#define COAP_MAX_TRANSMIT_SPAN(s) \
422c87c5fbaSopenharmony_ci  (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part) * \
423c87c5fbaSopenharmony_ci   ((1 << ((s)->max_retransmit)) -1) * \
424c87c5fbaSopenharmony_ci   ((s)->ack_random_factor.integer_part * 1000 + \
425c87c5fbaSopenharmony_ci    (s)->ack_random_factor.fractional_part) \
426c87c5fbaSopenharmony_ci   / 1000000)
427c87c5fbaSopenharmony_ci
428c87c5fbaSopenharmony_ci/**
429c87c5fbaSopenharmony_ci * The MAX_TRANSMIT_WAIT definition for the session (s).
430c87c5fbaSopenharmony_ci *
431c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_WAIT
432c87c5fbaSopenharmony_ci *  ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT + 1)) - 1) * ACK_RANDOM_FACTOR
433c87c5fbaSopenharmony_ci */
434c87c5fbaSopenharmony_ci#define COAP_MAX_TRANSMIT_WAIT(s) \
435c87c5fbaSopenharmony_ci  (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part) * \
436c87c5fbaSopenharmony_ci   ((1 << ((s)->max_retransmit + 1)) -1) * \
437c87c5fbaSopenharmony_ci   ((s)->ack_random_factor.integer_part * 1000 + \
438c87c5fbaSopenharmony_ci    (s)->ack_random_factor.fractional_part) \
439c87c5fbaSopenharmony_ci   / 1000000)
440c87c5fbaSopenharmony_ci
441c87c5fbaSopenharmony_ci#define COAP_MAX_TRANSMIT_WAIT_TICKS(s) \
442c87c5fbaSopenharmony_ci  (COAP_MAX_TRANSMIT_WAIT(s) * COAP_TICKS_PER_SECOND)
443c87c5fbaSopenharmony_ci
444c87c5fbaSopenharmony_ci/**
445c87c5fbaSopenharmony_ci * The PROCESSING_DELAY definition for the session (s).
446c87c5fbaSopenharmony_ci *
447c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of PROCESSING_DELAY
448c87c5fbaSopenharmony_ci *  PROCESSING_DELAY set to ACK_TIMEOUT
449c87c5fbaSopenharmony_ci */
450c87c5fbaSopenharmony_ci#define COAP_PROCESSING_DELAY(s) \
451c87c5fbaSopenharmony_ci  (((s)->ack_timeout.integer_part * 1000 + (s)->ack_timeout.fractional_part + \
452c87c5fbaSopenharmony_ci    500) / 1000)
453c87c5fbaSopenharmony_ci
454c87c5fbaSopenharmony_ci/**
455c87c5fbaSopenharmony_ci * The MAX_RTT definition for the session (s).
456c87c5fbaSopenharmony_ci *
457c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of MAX_RTT
458c87c5fbaSopenharmony_ci *  (2 * MAX_LATENCY) + PROCESSING_DELAY
459c87c5fbaSopenharmony_ci */
460c87c5fbaSopenharmony_ci#define COAP_MAX_RTT(s) \
461c87c5fbaSopenharmony_ci  ((2 * COAP_DEFAULT_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
462c87c5fbaSopenharmony_ci
463c87c5fbaSopenharmony_ci/**
464c87c5fbaSopenharmony_ci * The EXCHANGE_LIFETIME definition for the session (s).
465c87c5fbaSopenharmony_ci *
466c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of EXCHANGE_LIFETIME
467c87c5fbaSopenharmony_ci *  MAX_TRANSMIT_SPAN + (2 * MAX_LATENCY) + PROCESSING_DELAY
468c87c5fbaSopenharmony_ci */
469c87c5fbaSopenharmony_ci#define COAP_EXCHANGE_LIFETIME(s) \
470c87c5fbaSopenharmony_ci  (COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_DEFAULT_MAX_LATENCY) + \
471c87c5fbaSopenharmony_ci   COAP_PROCESSING_DELAY(s))
472c87c5fbaSopenharmony_ci
473c87c5fbaSopenharmony_ci/**
474c87c5fbaSopenharmony_ci * The NON_LIFETIME definition for the session (s).
475c87c5fbaSopenharmony_ci *
476c87c5fbaSopenharmony_ci * RFC 7252, Section 4.8.2 Calculation of NON_LIFETIME
477c87c5fbaSopenharmony_ci *  MAX_TRANSMIT_SPAN + MAX_LATENCY
478c87c5fbaSopenharmony_ci */
479c87c5fbaSopenharmony_ci#define COAP_NON_LIFETIME(s) \
480c87c5fbaSopenharmony_ci  (COAP_MAX_TRANSMIT_SPAN(s) + COAP_DEFAULT_MAX_LATENCY)
481c87c5fbaSopenharmony_ci
482c87c5fbaSopenharmony_ci/**
483c87c5fbaSopenharmony_ci * The NON_RECEIVE_TIMEOUT definition for the session (s).
484c87c5fbaSopenharmony_ci *
485c87c5fbaSopenharmony_ci * RFC9177 Section 6.2
486c87c5fbaSopenharmony_ci * 2 * NON_TIMEOUT
487c87c5fbaSopenharmony_ci */
488c87c5fbaSopenharmony_ci#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s) ( \
489c87c5fbaSopenharmony_ci                                            COAP_NON_RECEIVE_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
490c87c5fbaSopenharmony_ci                                            COAP_NON_RECEIVE_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
491c87c5fbaSopenharmony_ci
492c87c5fbaSopenharmony_ci/**
493c87c5fbaSopenharmony_ci * The NON_PROBING_WAIT definition for the session (s).
494c87c5fbaSopenharmony_ci *
495c87c5fbaSopenharmony_ci * RFC9177 Section 6.2
496c87c5fbaSopenharmony_ci *  NON_PROBING_WAIT = NON_TIMEOUT * ((2 ** NON_MAX_RETRANSMIT) - 1) *
497c87c5fbaSopenharmony_ci *  ACK_RANDOM_FACTOR + (2 * MAX_LATENCY) + NON_TIMEOUT_RANDOM
498c87c5fbaSopenharmony_ci * Default is 247-248 seconds
499c87c5fbaSopenharmony_ci */
500c87c5fbaSopenharmony_ci#define COAP_NON_PROBING_WAIT(s) \
501c87c5fbaSopenharmony_ci  coap_add_fixed_fixed(COAP_NON_PROBING_WAIT_BASE(s), \
502c87c5fbaSopenharmony_ci                       COAP_NON_TIMEOUT_RANDOM(s))
503c87c5fbaSopenharmony_ci
504c87c5fbaSopenharmony_ci#define COAP_NON_PROBING_WAIT_TICKS(s) \
505c87c5fbaSopenharmony_ci  (COAP_NON_PROBING_WAIT(s).integer_part * COAP_TICKS_PER_SECOND + \
506c87c5fbaSopenharmony_ci   COAP_NON_PROBING_WAIT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
507c87c5fbaSopenharmony_ci
508c87c5fbaSopenharmony_ci/**
509c87c5fbaSopenharmony_ci * The NON_PARTIAL_TIMEOUT definition for the session (s).
510c87c5fbaSopenharmony_ci *
511c87c5fbaSopenharmony_ci * RFC9177 Section 6.2
512c87c5fbaSopenharmony_ci * Initial value EXCHANGE_LIFETIME (247 seconds)
513c87c5fbaSopenharmony_ci */
514c87c5fbaSopenharmony_ci#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s) \
515c87c5fbaSopenharmony_ci  (COAP_NON_PARTIAL_TIMEOUT(s).integer_part * COAP_TICKS_PER_SECOND + \
516c87c5fbaSopenharmony_ci   COAP_NON_PARTIAL_TIMEOUT(s).fractional_part * COAP_TICKS_PER_SECOND / 1000)
517c87c5fbaSopenharmony_ci
518c87c5fbaSopenharmony_ci/**
519c87c5fbaSopenharmony_ci * The NON_TIMEOUT_RANDOM definition for the session (s).
520c87c5fbaSopenharmony_ci *
521c87c5fbaSopenharmony_ci * RFC9177 Section 6.2
522c87c5fbaSopenharmony_ci * Default is 2-3 seconds
523c87c5fbaSopenharmony_ci */
524c87c5fbaSopenharmony_ci#define COAP_NON_TIMEOUT_RANDOM(s) \
525c87c5fbaSopenharmony_ci  coap_get_non_timeout_random(s)
526c87c5fbaSopenharmony_ci
527c87c5fbaSopenharmony_ci/** @} */
528c87c5fbaSopenharmony_ci
529c87c5fbaSopenharmony_ci#define SESSIONS_ADD(e, obj) \
530c87c5fbaSopenharmony_ci  HASH_ADD(hh, (e), addr_hash, sizeof((obj)->addr_hash), (obj))
531c87c5fbaSopenharmony_ci
532c87c5fbaSopenharmony_ci#define SESSIONS_DELETE(e, obj) \
533c87c5fbaSopenharmony_ci  HASH_DELETE(hh, (e), (obj))
534c87c5fbaSopenharmony_ci
535c87c5fbaSopenharmony_ci#define SESSIONS_ITER(e, el, rtmp)  \
536c87c5fbaSopenharmony_ci  HASH_ITER(hh, (e), el, rtmp)
537c87c5fbaSopenharmony_ci
538c87c5fbaSopenharmony_ci#define SESSIONS_ITER_SAFE(e, el, rtmp) \
539c87c5fbaSopenharmony_ci  for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
540c87c5fbaSopenharmony_ci
541c87c5fbaSopenharmony_ci#define SESSIONS_FIND(e, k, res) {                     \
542c87c5fbaSopenharmony_ci    HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
543c87c5fbaSopenharmony_ci  }
544c87c5fbaSopenharmony_ci
545c87c5fbaSopenharmony_ci#endif /* COAP_SESSION_INTERNAL_H_ */
546