libcoap 4.3.5-develop-93694e6
Loading...
Searching...
No Matches
coap_cache.c
Go to the documentation of this file.
1/* coap_cache.c -- Caching of CoAP requests
2*
3* Copyright (C) 2020-2026 Olaf Bergmann <bergmann@tzi.org>
4*
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7* This file is part of the CoAP library libcoap. Please see
8* README for terms of use.
9*/
10
15
17
18#if COAP_SERVER_SUPPORT
19/* Determines if the given option_type denotes an option type that can
20 * be used as CacheKey. Options that can be cache keys are not Unsafe
21 * and not marked explicitly as NoCacheKey. */
22static int
23is_cache_key(uint16_t option_type, size_t cache_ignore_count,
24 const uint16_t *cache_ignore_options) {
25 size_t i;
26
27 /* https://rfc-editor.org/rfc/rfc7252#section-5.4.6 Nocachekey definition */
28 if ((option_type & 0x1e) == 0x1c)
29 return 0;
30 /*
31 * https://rfc-editor.org/rfc/rfc7641#section-2 Observe is not a
32 * part of the cache-key.
33 */
34 if (option_type == COAP_OPTION_OBSERVE)
35 return 0;
36
37 /* Check for option user has defined as not part of cache-key */
38 for (i = 0; i < cache_ignore_count; i++) {
39 if (cache_ignore_options[i] == option_type) {
40 return 0;
41 }
42 }
43
44 return 1;
45}
46
47COAP_API int
49 const uint16_t *options,
50 size_t count) {
51 int ret;
52
53 coap_lock_lock(return 0);
54 ret = coap_cache_ignore_options_lkd(ctx, options, count);
56 return ret;
57}
58
59int
60coap_cache_ignore_options_lkd(coap_context_t *ctx,
61 const uint16_t *options,
62 size_t count) {
64 if (ctx->cache_ignore_options) {
65 coap_free_type(COAP_STRING, ctx->cache_ignore_options);
66 }
67 if (count) {
68 assert(options);
69 ctx->cache_ignore_options = coap_malloc_type(COAP_STRING, count * sizeof(options[0]));
70 if (ctx->cache_ignore_options) {
71 memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0]));
72 ctx->cache_ignore_count = count;
73 } else {
74 coap_log_warn("Unable to create cache_ignore_options\n");
75 return 0;
76 }
77 } else {
78 ctx->cache_ignore_options = NULL;
79 ctx->cache_ignore_count = count;
80 }
81 return 1;
82}
83
86 const coap_pdu_t *pdu,
87 coap_cache_session_based_t session_based,
88 const uint16_t *cache_ignore_options,
89 size_t cache_ignore_count) {
90 coap_opt_t *option;
91 coap_opt_iterator_t opt_iter;
92 coap_digest_ctx_t *dctx;
93 coap_digest_t digest;
94 coap_cache_key_t *cache_key;
95
96 if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
97 return NULL;
98 }
99
100 dctx = coap_digest_setup();
101 if (!dctx)
102 return NULL;
103
104 if (session_based == COAP_CACHE_IS_SESSION_BASED ||
105 session_based == COAP_CACHE_IS_SESSION_BASED_NO_DATA) {
106 /* Include the session ptr */
107 if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) {
108 goto update_fail;
109 }
110 }
111 while ((option = coap_option_next(&opt_iter))) {
112 if (is_cache_key(opt_iter.number, cache_ignore_count,
113 cache_ignore_options)) {
114 if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number,
115 sizeof(opt_iter.number))) {
116 goto update_fail;
117 }
118 if (!coap_digest_update(dctx, coap_opt_value(option),
119 coap_opt_length(option))) {
120 goto update_fail;
121 }
122 }
123 }
124
125 /* The body of a FETCH payload is part of the cache key,
126 * see https://rfc-editor.org/rfc/rfc8132#section-2 */
127 if (pdu->code == COAP_REQUEST_CODE_FETCH &&
128 session_based != COAP_CACHE_IS_SESSION_BASED_NO_DATA) {
129 size_t len;
130 const uint8_t *data;
131 if (coap_get_data(pdu, &len, &data)) {
132 if (!coap_digest_update(dctx, data, len)) {
133 goto update_fail;
134 }
135 }
136 }
137
138 if (!coap_digest_final(dctx, &digest)) {
139 /* coap_digest_final() is guaranteed to free off dctx no matter what */
140 return NULL;
141 }
143 if (cache_key) {
144 memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
145 }
146 return cache_key;
147update_fail:
148 coap_digest_free(dctx);
149 return NULL;
150}
151
154 const coap_pdu_t *pdu,
155 coap_cache_session_based_t session_based) {
156 return coap_cache_derive_key_w_ignore(session, pdu, session_based,
157 session->context->cache_ignore_options,
158 session->context->cache_ignore_count);
159}
160
161void
163 coap_free_type(COAP_CACHE_KEY, cache_key);
164}
165
168 coap_cache_record_pdu_t record_pdu,
169 coap_cache_session_based_t session_based,
170 unsigned int idle_timeout) {
171 coap_cache_entry_t *cache;
172
173 coap_lock_lock(return NULL);
174 cache = coap_new_cache_entry_lkd(session, pdu, record_pdu, session_based,
175 idle_timeout);
177 return cache;
178}
179
181coap_new_cache_entry_lkd(coap_session_t *session, const coap_pdu_t *pdu,
182 coap_cache_record_pdu_t record_pdu,
183 coap_cache_session_based_t session_based,
184 unsigned int idle_timeout) {
185 coap_cache_entry_t *entry;
186
189 sizeof(coap_cache_entry_t));
190 if (!entry) {
191 return NULL;
192 }
193
194 memset(entry, 0, sizeof(coap_cache_entry_t));
195 entry->session = session;
196 if (record_pdu == COAP_CACHE_RECORD_PDU) {
197 entry->pdu = coap_const_pdu_reference_lkd(pdu);
198 }
199 entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
200 if (!entry->cache_key) {
201 coap_delete_pdu_lkd(entry->pdu);
203 return NULL;
204 }
205 entry->idle_timeout = idle_timeout;
206 if (idle_timeout > 0) {
207 coap_ticks(&entry->expire_ticks);
208 entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
209 }
210
211 HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
212 return entry;
213}
214
217 coap_cache_entry_t *cache;
218
219 coap_lock_lock(return NULL);
220 cache = coap_cache_get_by_key_lkd(ctx, cache_key);
222 return cache;
223}
224
226coap_cache_get_by_key_lkd(coap_context_t *ctx, const coap_cache_key_t *cache_key) {
227 coap_cache_entry_t *cache_entry = NULL;
228
230 assert(cache_key);
231 if (cache_key) {
232 HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
233 }
234 if (cache_entry && cache_entry->idle_timeout > 0) {
235 coap_ticks(&cache_entry->expire_ticks);
236 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
237 }
238 return cache_entry;
239}
240
243 const coap_pdu_t *request,
244 coap_cache_session_based_t session_based) {
245 coap_cache_entry_t *entry;
246
247 coap_lock_lock(return NULL);
248 entry = coap_cache_get_by_pdu_lkd(session, request, session_based);
250 return entry;
251}
252
254coap_cache_get_by_pdu_lkd(coap_session_t *session,
255 const coap_pdu_t *request,
256 coap_cache_session_based_t session_based) {
257 coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
258 coap_cache_entry_t *cache_entry;
259
260 if (!cache_key)
261 return NULL;
262
264 cache_entry = coap_cache_get_by_key_lkd(session->context, cache_key);
265 coap_delete_cache_key(cache_key);
266 if (cache_entry && cache_entry->idle_timeout > 0) {
267 coap_ticks(&cache_entry->expire_ticks);
268 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
269 }
270 return cache_entry;
271}
272
273void
275
276 assert(cache_entry);
277
278 if (cache_entry) {
279 HASH_DELETE(hh, ctx->cache, cache_entry);
280 }
281 if (cache_entry->pdu) {
282 coap_delete_pdu_lkd(cache_entry->pdu);
283 }
284 coap_delete_cache_key(cache_entry->cache_key);
285 if (cache_entry->app_cb && cache_entry->app_data) {
286 coap_lock_callback(cache_entry->app_cb(cache_entry->app_data));
287 }
288 coap_free_type(COAP_CACHE_ENTRY, cache_entry);
289}
290
291const coap_pdu_t *
292coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) {
293 return cache_entry->pdu;
294}
295
296COAP_API void
298 void *data,
300 coap_lock_lock(return);
301 coap_cache_set_app_data2_lkd(cache_entry, data, callback);
303}
304
305COAP_API void *
306coap_cache_set_app_data2(coap_cache_entry_t *cache_entry, void *app_data,
308 void *old_data;
309
310 coap_lock_lock(return NULL);
311 old_data = coap_cache_set_app_data2_lkd(cache_entry, app_data, callback);
313 return old_data;
314}
315
316void *
317coap_cache_set_app_data2_lkd(coap_cache_entry_t *cache_entry, void *app_data,
319 void *old_data = cache_entry->app_data;
320
321 cache_entry->app_data = app_data;
322 cache_entry->app_cb = app_data ? callback : NULL;
323 return old_data;
324}
325
326void *
328 return cache_entry->app_data;
329}
330
331void
332coap_expire_cache_entries(coap_context_t *ctx) {
333 coap_tick_t now;
334 coap_cache_entry_t *cp, *ctmp;
335
336 coap_ticks(&now);
337 HASH_ITER(hh, ctx->cache, cp, ctmp) {
338 if (cp->idle_timeout > 0) {
339 if (cp->expire_ticks <= now) {
341 }
342 }
343 }
344}
345
346#endif /* ! COAP_SERVER_SUPPORT */
struct coap_cache_key_t coap_cache_key_t
struct coap_cache_entry_t coap_cache_entry_t
Library specific build wrapper for coap_internal.h.
#define COAP_API
@ COAP_CACHE_KEY
Definition coap_mem.h:47
@ COAP_CACHE_ENTRY
Definition coap_mem.h:48
@ COAP_STRING
Definition coap_mem.h:33
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
#define NULL
Definition coap_option.h:30
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
@ COAP_OPTION_OBSERVE
Definition coap_option.h:75
void coap_delete_cache_entry(coap_context_t *context, coap_cache_entry_t *cache_entry)
Remove a cache-entry from the hash list and free off all the appropriate contents apart from app_data...
COAP_API int coap_cache_ignore_options(coap_context_t *context, const uint16_t *options, size_t count)
Define the CoAP options that are not to be included when calculating the cache-key.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
COAP_API coap_cache_entry_t * coap_cache_get_by_key(coap_context_t *context, const coap_cache_key_t *cache_key)
Searches for a cache-entry identified by cache_key.
coap_cache_key_t * coap_cache_derive_key(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based)
Calculates a cache-key for the given CoAP PDU.
coap_cache_record_pdu_t
Definition coap_cache.h:45
COAP_API coap_cache_entry_t * coap_cache_get_by_pdu(coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based)
Searches for a cache-entry corresponding to pdu.
const coap_pdu_t * coap_cache_get_pdu(const coap_cache_entry_t *cache_entry)
Returns the PDU information stored in the coap_cache entry.
COAP_DEPRECATED void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void *data, coap_app_data_free_callback_t callback)
Stores data with the given cache entry.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
void * coap_cache_get_app_data(const coap_cache_entry_t *cache_entry)
Returns any application-specific data that has been stored with cache_entry using the function coap_c...
COAP_API coap_cache_entry_t * coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, coap_cache_record_pdu_t record_pdu, coap_cache_session_based_t session_based, unsigned int idle_time)
Create a new cache-entry hash keyed by cache-key derived from the PDU.
COAP_API void * coap_cache_set_app_data2(coap_cache_entry_t *cache_entry, void *data, coap_app_data_free_callback_t callback)
Stores data with the given cache_entry, returning the previously stored value or NULL.
coap_cache_session_based_t
Definition coap_cache.h:39
@ COAP_CACHE_RECORD_PDU
Definition coap_cache.h:47
@ COAP_CACHE_IS_SESSION_BASED_NO_DATA
Definition coap_cache.h:42
@ COAP_CACHE_IS_SESSION_BASED
Definition coap_cache.h:41
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:164
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
Definition coap_time.c:90
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_warn(...)
Definition coap_debug.h:108
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:197
coap_pdu_t * coap_const_pdu_reference_lkd(const coap_pdu_t *pdu)
Increment reference counter on a const pdu to stop it prematurely getting freed off when coap_delete_...
Definition coap_pdu.c:1768
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition coap_pdu.c:966
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:255
void(* coap_app_data_free_callback_t)(void *data)
Callback to free off the app data when the entry is being deleted / freed off.
The CoAP stack's global state is stored in a coap_context_t object.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
structure for CoAP PDUs
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_context_t * context
session's context