/src/libcoap/src/coap_cache.c
Line | Count | Source |
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 | | |
11 | | /** |
12 | | * @file coap_cache.c |
13 | | * @brief CoAP Cache handling |
14 | | */ |
15 | | |
16 | | #include "coap3/coap_libcoap_build.h" |
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. */ |
22 | | static int |
23 | | is_cache_key(uint16_t option_type, size_t cache_ignore_count, |
24 | 0 | const uint16_t *cache_ignore_options) { |
25 | 0 | size_t i; |
26 | | |
27 | | /* https://rfc-editor.org/rfc/rfc7252#section-5.4.6 Nocachekey definition */ |
28 | 0 | if ((option_type & 0x1e) == 0x1c) |
29 | 0 | return 0; |
30 | | /* |
31 | | * https://rfc-editor.org/rfc/rfc7641#section-2 Observe is not a |
32 | | * part of the cache-key. |
33 | | */ |
34 | 0 | if (option_type == COAP_OPTION_OBSERVE) |
35 | 0 | return 0; |
36 | | |
37 | | /* Check for option user has defined as not part of cache-key */ |
38 | 0 | for (i = 0; i < cache_ignore_count; i++) { |
39 | 0 | if (cache_ignore_options[i] == option_type) { |
40 | 0 | return 0; |
41 | 0 | } |
42 | 0 | } |
43 | | |
44 | 0 | return 1; |
45 | 0 | } |
46 | | |
47 | | COAP_API int |
48 | | coap_cache_ignore_options(coap_context_t *ctx, |
49 | | const uint16_t *options, |
50 | 0 | size_t count) { |
51 | 0 | int ret; |
52 | |
|
53 | 0 | coap_lock_lock(return 0); |
54 | 0 | ret = coap_cache_ignore_options_lkd(ctx, options, count); |
55 | 0 | coap_lock_unlock(); |
56 | 0 | return ret; |
57 | 0 | } |
58 | | |
59 | | int |
60 | | coap_cache_ignore_options_lkd(coap_context_t *ctx, |
61 | | const uint16_t *options, |
62 | 0 | size_t count) { |
63 | 0 | coap_lock_check_locked(); |
64 | 0 | if (ctx->cache_ignore_options) { |
65 | 0 | coap_free_type(COAP_STRING, ctx->cache_ignore_options); |
66 | 0 | } |
67 | 0 | if (count) { |
68 | 0 | assert(options); |
69 | 0 | ctx->cache_ignore_options = coap_malloc_type(COAP_STRING, count * sizeof(options[0])); |
70 | 0 | if (ctx->cache_ignore_options) { |
71 | 0 | memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0])); |
72 | 0 | ctx->cache_ignore_count = count; |
73 | 0 | } else { |
74 | 0 | coap_log_warn("Unable to create cache_ignore_options\n"); |
75 | 0 | return 0; |
76 | 0 | } |
77 | 0 | } else { |
78 | 0 | ctx->cache_ignore_options = NULL; |
79 | 0 | ctx->cache_ignore_count = count; |
80 | 0 | } |
81 | 0 | return 1; |
82 | 0 | } |
83 | | |
84 | | coap_cache_key_t * |
85 | | coap_cache_derive_key_w_ignore(const coap_session_t *session, |
86 | | const coap_pdu_t *pdu, |
87 | | coap_cache_session_based_t session_based, |
88 | | const uint16_t *cache_ignore_options, |
89 | 0 | size_t cache_ignore_count) { |
90 | 0 | coap_opt_t *option; |
91 | 0 | coap_opt_iterator_t opt_iter; |
92 | 0 | coap_digest_ctx_t *dctx; |
93 | 0 | coap_digest_t digest; |
94 | 0 | coap_cache_key_t *cache_key; |
95 | |
|
96 | 0 | if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) { |
97 | 0 | return NULL; |
98 | 0 | } |
99 | | |
100 | 0 | dctx = coap_digest_setup(); |
101 | 0 | if (!dctx) |
102 | 0 | return NULL; |
103 | | |
104 | 0 | if (session_based == COAP_CACHE_IS_SESSION_BASED) { |
105 | | /* Include the session ptr */ |
106 | 0 | if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) { |
107 | 0 | goto update_fail; |
108 | 0 | } |
109 | 0 | } |
110 | 0 | while ((option = coap_option_next(&opt_iter))) { |
111 | 0 | if (is_cache_key(opt_iter.number, cache_ignore_count, |
112 | 0 | cache_ignore_options)) { |
113 | 0 | if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number, |
114 | 0 | sizeof(opt_iter.number))) { |
115 | 0 | goto update_fail; |
116 | 0 | } |
117 | 0 | if (!coap_digest_update(dctx, coap_opt_value(option), |
118 | 0 | coap_opt_length(option))) { |
119 | 0 | goto update_fail; |
120 | 0 | } |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | | /* The body of a FETCH payload is part of the cache key, |
125 | | * see https://rfc-editor.org/rfc/rfc8132#section-2 */ |
126 | 0 | if (pdu->code == COAP_REQUEST_CODE_FETCH) { |
127 | 0 | size_t len; |
128 | 0 | const uint8_t *data; |
129 | 0 | if (coap_get_data(pdu, &len, &data)) { |
130 | 0 | if (!coap_digest_update(dctx, data, len)) { |
131 | 0 | goto update_fail; |
132 | 0 | } |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | 0 | if (!coap_digest_final(dctx, &digest)) { |
137 | | /* coap_digest_final() is guaranteed to free off dctx no matter what */ |
138 | 0 | return NULL; |
139 | 0 | } |
140 | 0 | cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t)); |
141 | 0 | if (cache_key) { |
142 | 0 | memcpy(cache_key->key, digest.key, sizeof(cache_key->key)); |
143 | 0 | } |
144 | 0 | return cache_key; |
145 | 0 | update_fail: |
146 | 0 | coap_digest_free(dctx); |
147 | 0 | return NULL; |
148 | 0 | } |
149 | | |
150 | | coap_cache_key_t * |
151 | | coap_cache_derive_key(const coap_session_t *session, |
152 | | const coap_pdu_t *pdu, |
153 | 0 | coap_cache_session_based_t session_based) { |
154 | 0 | return coap_cache_derive_key_w_ignore(session, pdu, session_based, |
155 | 0 | session->context->cache_ignore_options, |
156 | 0 | session->context->cache_ignore_count); |
157 | 0 | } |
158 | | |
159 | | void |
160 | 0 | coap_delete_cache_key(coap_cache_key_t *cache_key) { |
161 | 0 | coap_free_type(COAP_CACHE_KEY, cache_key); |
162 | 0 | } |
163 | | |
164 | | COAP_API coap_cache_entry_t * |
165 | | coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, |
166 | | coap_cache_record_pdu_t record_pdu, |
167 | | coap_cache_session_based_t session_based, |
168 | 0 | unsigned int idle_timeout) { |
169 | 0 | coap_cache_entry_t *cache; |
170 | |
|
171 | 0 | coap_lock_lock(return NULL); |
172 | 0 | cache = coap_new_cache_entry_lkd(session, pdu, record_pdu, session_based, |
173 | 0 | idle_timeout); |
174 | 0 | coap_lock_unlock(); |
175 | 0 | return cache; |
176 | 0 | } |
177 | | |
178 | | coap_cache_entry_t * |
179 | | coap_new_cache_entry_lkd(coap_session_t *session, const coap_pdu_t *pdu, |
180 | | coap_cache_record_pdu_t record_pdu, |
181 | | coap_cache_session_based_t session_based, |
182 | 0 | unsigned int idle_timeout) { |
183 | 0 | coap_cache_entry_t *entry; |
184 | |
|
185 | 0 | coap_lock_check_locked(); |
186 | 0 | entry = coap_malloc_type(COAP_CACHE_ENTRY, |
187 | 0 | sizeof(coap_cache_entry_t)); |
188 | 0 | if (!entry) { |
189 | 0 | return NULL; |
190 | 0 | } |
191 | | |
192 | 0 | memset(entry, 0, sizeof(coap_cache_entry_t)); |
193 | 0 | entry->session = session; |
194 | 0 | if (record_pdu == COAP_CACHE_RECORD_PDU) { |
195 | 0 | entry->pdu = coap_const_pdu_reference_lkd(pdu); |
196 | 0 | } |
197 | 0 | entry->cache_key = coap_cache_derive_key(session, pdu, session_based); |
198 | 0 | if (!entry->cache_key) { |
199 | 0 | coap_delete_pdu_lkd(entry->pdu); |
200 | 0 | coap_free_type(COAP_CACHE_ENTRY, entry); |
201 | 0 | return NULL; |
202 | 0 | } |
203 | 0 | entry->idle_timeout = idle_timeout; |
204 | 0 | if (idle_timeout > 0) { |
205 | 0 | coap_ticks(&entry->expire_ticks); |
206 | 0 | entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND; |
207 | 0 | } |
208 | |
|
209 | 0 | HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry); |
210 | 0 | return entry; |
211 | 0 | } |
212 | | |
213 | | COAP_API coap_cache_entry_t * |
214 | 0 | coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) { |
215 | 0 | coap_cache_entry_t *cache; |
216 | |
|
217 | 0 | coap_lock_lock(return NULL); |
218 | 0 | cache = coap_cache_get_by_key_lkd(ctx, cache_key); |
219 | 0 | coap_lock_unlock(); |
220 | 0 | return cache; |
221 | 0 | } |
222 | | |
223 | | coap_cache_entry_t * |
224 | 0 | coap_cache_get_by_key_lkd(coap_context_t *ctx, const coap_cache_key_t *cache_key) { |
225 | 0 | coap_cache_entry_t *cache_entry = NULL; |
226 | |
|
227 | 0 | coap_lock_check_locked(); |
228 | 0 | assert(cache_key); |
229 | 0 | if (cache_key) { |
230 | 0 | HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry); |
231 | 0 | } |
232 | 0 | if (cache_entry && cache_entry->idle_timeout > 0) { |
233 | 0 | coap_ticks(&cache_entry->expire_ticks); |
234 | 0 | cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; |
235 | 0 | } |
236 | 0 | return cache_entry; |
237 | 0 | } |
238 | | |
239 | | COAP_API coap_cache_entry_t * |
240 | | coap_cache_get_by_pdu(coap_session_t *session, |
241 | | const coap_pdu_t *request, |
242 | 0 | coap_cache_session_based_t session_based) { |
243 | 0 | coap_cache_entry_t *entry; |
244 | |
|
245 | 0 | coap_lock_lock(return NULL); |
246 | 0 | entry = coap_cache_get_by_pdu_lkd(session, request, session_based); |
247 | 0 | coap_lock_unlock(); |
248 | 0 | return entry; |
249 | 0 | } |
250 | | |
251 | | coap_cache_entry_t * |
252 | | coap_cache_get_by_pdu_lkd(coap_session_t *session, |
253 | | const coap_pdu_t *request, |
254 | 0 | coap_cache_session_based_t session_based) { |
255 | 0 | coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based); |
256 | 0 | coap_cache_entry_t *cache_entry; |
257 | |
|
258 | 0 | if (!cache_key) |
259 | 0 | return NULL; |
260 | | |
261 | 0 | coap_lock_check_locked(); |
262 | 0 | cache_entry = coap_cache_get_by_key_lkd(session->context, cache_key); |
263 | 0 | coap_delete_cache_key(cache_key); |
264 | 0 | if (cache_entry && cache_entry->idle_timeout > 0) { |
265 | 0 | coap_ticks(&cache_entry->expire_ticks); |
266 | 0 | cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; |
267 | 0 | } |
268 | 0 | return cache_entry; |
269 | 0 | } |
270 | | |
271 | | void |
272 | 0 | coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry) { |
273 | |
|
274 | 0 | assert(cache_entry); |
275 | | |
276 | 0 | if (cache_entry) { |
277 | 0 | HASH_DELETE(hh, ctx->cache, cache_entry); |
278 | 0 | } |
279 | 0 | if (cache_entry->pdu) { |
280 | 0 | coap_delete_pdu_lkd(cache_entry->pdu); |
281 | 0 | } |
282 | 0 | coap_delete_cache_key(cache_entry->cache_key); |
283 | 0 | if (cache_entry->app_cb && cache_entry->app_data) { |
284 | 0 | coap_lock_callback(cache_entry->app_cb(cache_entry->app_data)); |
285 | 0 | } |
286 | 0 | coap_free_type(COAP_CACHE_ENTRY, cache_entry); |
287 | 0 | } |
288 | | |
289 | | const coap_pdu_t * |
290 | 0 | coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) { |
291 | 0 | return cache_entry->pdu; |
292 | 0 | } |
293 | | |
294 | | COAP_API void |
295 | | coap_cache_set_app_data(coap_cache_entry_t *cache_entry, |
296 | | void *data, |
297 | 0 | coap_app_data_free_callback_t callback) { |
298 | 0 | coap_lock_lock(return); |
299 | 0 | coap_cache_set_app_data2_lkd(cache_entry, data, callback); |
300 | 0 | coap_lock_unlock(); |
301 | 0 | } |
302 | | |
303 | | COAP_API void * |
304 | | coap_cache_set_app_data2(coap_cache_entry_t *cache_entry, void *app_data, |
305 | 0 | coap_app_data_free_callback_t callback) { |
306 | 0 | void *old_data; |
307 | |
|
308 | 0 | coap_lock_lock(return NULL); |
309 | 0 | old_data = coap_cache_set_app_data2_lkd(cache_entry, app_data, callback); |
310 | 0 | coap_lock_unlock(); |
311 | 0 | return old_data; |
312 | 0 | } |
313 | | |
314 | | void * |
315 | | coap_cache_set_app_data2_lkd(coap_cache_entry_t *cache_entry, void *app_data, |
316 | 0 | coap_app_data_free_callback_t callback) { |
317 | 0 | void *old_data = cache_entry->app_data; |
318 | |
|
319 | 0 | cache_entry->app_data = app_data; |
320 | 0 | cache_entry->app_cb = app_data ? callback : NULL; |
321 | 0 | return old_data; |
322 | 0 | } |
323 | | |
324 | | void * |
325 | 0 | coap_cache_get_app_data(const coap_cache_entry_t *cache_entry) { |
326 | 0 | return cache_entry->app_data; |
327 | 0 | } |
328 | | |
329 | | void |
330 | 0 | coap_expire_cache_entries(coap_context_t *ctx) { |
331 | 0 | coap_tick_t now; |
332 | 0 | coap_cache_entry_t *cp, *ctmp; |
333 | |
|
334 | 0 | coap_ticks(&now); |
335 | 0 | HASH_ITER(hh, ctx->cache, cp, ctmp) { |
336 | 0 | if (cp->idle_timeout > 0) { |
337 | 0 | if (cp->expire_ticks <= now) { |
338 | 0 | coap_delete_cache_entry(ctx, cp); |
339 | 0 | } |
340 | 0 | } |
341 | 0 | } |
342 | 0 | } |
343 | | |
344 | | #endif /* ! COAP_SERVER_SUPPORT */ |