/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 | 0 | session_based == COAP_CACHE_IS_SESSION_BASED_NO_DATA) { |
106 | | /* Include the session ptr */ |
107 | 0 | if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) { |
108 | 0 | goto update_fail; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | while ((option = coap_option_next(&opt_iter))) { |
112 | 0 | if (is_cache_key(opt_iter.number, cache_ignore_count, |
113 | 0 | cache_ignore_options)) { |
114 | 0 | if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number, |
115 | 0 | sizeof(opt_iter.number))) { |
116 | 0 | goto update_fail; |
117 | 0 | } |
118 | 0 | if (!coap_digest_update(dctx, coap_opt_value(option), |
119 | 0 | coap_opt_length(option))) { |
120 | 0 | goto update_fail; |
121 | 0 | } |
122 | 0 | } |
123 | 0 | } |
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 | 0 | if (pdu->code == COAP_REQUEST_CODE_FETCH && |
128 | 0 | session_based != COAP_CACHE_IS_SESSION_BASED_NO_DATA) { |
129 | 0 | size_t len; |
130 | 0 | const uint8_t *data; |
131 | 0 | if (coap_get_data(pdu, &len, &data)) { |
132 | 0 | if (!coap_digest_update(dctx, data, len)) { |
133 | 0 | goto update_fail; |
134 | 0 | } |
135 | 0 | } |
136 | 0 | } |
137 | | |
138 | 0 | if (!coap_digest_final(dctx, &digest)) { |
139 | | /* coap_digest_final() is guaranteed to free off dctx no matter what */ |
140 | 0 | return NULL; |
141 | 0 | } |
142 | 0 | cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t)); |
143 | 0 | if (cache_key) { |
144 | 0 | memcpy(cache_key->key, digest.key, sizeof(cache_key->key)); |
145 | 0 | } |
146 | 0 | return cache_key; |
147 | 0 | update_fail: |
148 | 0 | coap_digest_free(dctx); |
149 | 0 | return NULL; |
150 | 0 | } |
151 | | |
152 | | coap_cache_key_t * |
153 | | coap_cache_derive_key(const coap_session_t *session, |
154 | | const coap_pdu_t *pdu, |
155 | 0 | coap_cache_session_based_t session_based) { |
156 | 0 | return coap_cache_derive_key_w_ignore(session, pdu, session_based, |
157 | 0 | session->context->cache_ignore_options, |
158 | 0 | session->context->cache_ignore_count); |
159 | 0 | } |
160 | | |
161 | | void |
162 | 0 | coap_delete_cache_key(coap_cache_key_t *cache_key) { |
163 | 0 | coap_free_type(COAP_CACHE_KEY, cache_key); |
164 | 0 | } |
165 | | |
166 | | COAP_API coap_cache_entry_t * |
167 | | coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu, |
168 | | coap_cache_record_pdu_t record_pdu, |
169 | | coap_cache_session_based_t session_based, |
170 | 0 | unsigned int idle_timeout) { |
171 | 0 | coap_cache_entry_t *cache; |
172 | |
|
173 | 0 | coap_lock_lock(return NULL); |
174 | 0 | cache = coap_new_cache_entry_lkd(session, pdu, record_pdu, session_based, |
175 | 0 | idle_timeout); |
176 | 0 | coap_lock_unlock(); |
177 | 0 | return cache; |
178 | 0 | } |
179 | | |
180 | | coap_cache_entry_t * |
181 | | coap_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 | 0 | unsigned int idle_timeout) { |
185 | 0 | coap_cache_entry_t *entry; |
186 | |
|
187 | 0 | coap_lock_check_locked(); |
188 | 0 | entry = coap_malloc_type(COAP_CACHE_ENTRY, |
189 | 0 | sizeof(coap_cache_entry_t)); |
190 | 0 | if (!entry) { |
191 | 0 | return NULL; |
192 | 0 | } |
193 | | |
194 | 0 | memset(entry, 0, sizeof(coap_cache_entry_t)); |
195 | 0 | entry->session = session; |
196 | 0 | if (record_pdu == COAP_CACHE_RECORD_PDU) { |
197 | 0 | entry->pdu = coap_const_pdu_reference_lkd(pdu); |
198 | 0 | } |
199 | 0 | entry->cache_key = coap_cache_derive_key(session, pdu, session_based); |
200 | 0 | if (!entry->cache_key) { |
201 | 0 | coap_delete_pdu_lkd(entry->pdu); |
202 | 0 | coap_free_type(COAP_CACHE_ENTRY, entry); |
203 | 0 | return NULL; |
204 | 0 | } |
205 | 0 | entry->idle_timeout = idle_timeout; |
206 | 0 | if (idle_timeout > 0) { |
207 | 0 | coap_ticks(&entry->expire_ticks); |
208 | 0 | entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND; |
209 | 0 | } |
210 | |
|
211 | 0 | HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry); |
212 | 0 | return entry; |
213 | 0 | } |
214 | | |
215 | | COAP_API coap_cache_entry_t * |
216 | 0 | coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) { |
217 | 0 | coap_cache_entry_t *cache; |
218 | |
|
219 | 0 | coap_lock_lock(return NULL); |
220 | 0 | cache = coap_cache_get_by_key_lkd(ctx, cache_key); |
221 | 0 | coap_lock_unlock(); |
222 | 0 | return cache; |
223 | 0 | } |
224 | | |
225 | | coap_cache_entry_t * |
226 | 0 | coap_cache_get_by_key_lkd(coap_context_t *ctx, const coap_cache_key_t *cache_key) { |
227 | 0 | coap_cache_entry_t *cache_entry = NULL; |
228 | |
|
229 | 0 | coap_lock_check_locked(); |
230 | 0 | assert(cache_key); |
231 | 0 | if (cache_key) { |
232 | 0 | HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry); |
233 | 0 | } |
234 | 0 | if (cache_entry && cache_entry->idle_timeout > 0) { |
235 | 0 | coap_ticks(&cache_entry->expire_ticks); |
236 | 0 | cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; |
237 | 0 | } |
238 | 0 | return cache_entry; |
239 | 0 | } |
240 | | |
241 | | COAP_API coap_cache_entry_t * |
242 | | coap_cache_get_by_pdu(coap_session_t *session, |
243 | | const coap_pdu_t *request, |
244 | 0 | coap_cache_session_based_t session_based) { |
245 | 0 | coap_cache_entry_t *entry; |
246 | |
|
247 | 0 | coap_lock_lock(return NULL); |
248 | 0 | entry = coap_cache_get_by_pdu_lkd(session, request, session_based); |
249 | 0 | coap_lock_unlock(); |
250 | 0 | return entry; |
251 | 0 | } |
252 | | |
253 | | coap_cache_entry_t * |
254 | | coap_cache_get_by_pdu_lkd(coap_session_t *session, |
255 | | const coap_pdu_t *request, |
256 | 0 | coap_cache_session_based_t session_based) { |
257 | 0 | coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based); |
258 | 0 | coap_cache_entry_t *cache_entry; |
259 | |
|
260 | 0 | if (!cache_key) |
261 | 0 | return NULL; |
262 | | |
263 | 0 | coap_lock_check_locked(); |
264 | 0 | cache_entry = coap_cache_get_by_key_lkd(session->context, cache_key); |
265 | 0 | coap_delete_cache_key(cache_key); |
266 | 0 | if (cache_entry && cache_entry->idle_timeout > 0) { |
267 | 0 | coap_ticks(&cache_entry->expire_ticks); |
268 | 0 | cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; |
269 | 0 | } |
270 | 0 | return cache_entry; |
271 | 0 | } |
272 | | |
273 | | void |
274 | 0 | coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry) { |
275 | |
|
276 | 0 | assert(cache_entry); |
277 | | |
278 | 0 | if (cache_entry) { |
279 | 0 | HASH_DELETE(hh, ctx->cache, cache_entry); |
280 | 0 | } |
281 | 0 | if (cache_entry->pdu) { |
282 | 0 | coap_delete_pdu_lkd(cache_entry->pdu); |
283 | 0 | } |
284 | 0 | coap_delete_cache_key(cache_entry->cache_key); |
285 | 0 | if (cache_entry->app_cb && cache_entry->app_data) { |
286 | 0 | coap_lock_callback(cache_entry->app_cb(cache_entry->app_data)); |
287 | 0 | } |
288 | 0 | coap_free_type(COAP_CACHE_ENTRY, cache_entry); |
289 | 0 | } |
290 | | |
291 | | const coap_pdu_t * |
292 | 0 | coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) { |
293 | 0 | return cache_entry->pdu; |
294 | 0 | } |
295 | | |
296 | | COAP_API void |
297 | | coap_cache_set_app_data(coap_cache_entry_t *cache_entry, |
298 | | void *data, |
299 | 0 | coap_app_data_free_callback_t callback) { |
300 | 0 | coap_lock_lock(return); |
301 | 0 | coap_cache_set_app_data2_lkd(cache_entry, data, callback); |
302 | 0 | coap_lock_unlock(); |
303 | 0 | } |
304 | | |
305 | | COAP_API void * |
306 | | coap_cache_set_app_data2(coap_cache_entry_t *cache_entry, void *app_data, |
307 | 0 | coap_app_data_free_callback_t callback) { |
308 | 0 | void *old_data; |
309 | |
|
310 | 0 | coap_lock_lock(return NULL); |
311 | 0 | old_data = coap_cache_set_app_data2_lkd(cache_entry, app_data, callback); |
312 | 0 | coap_lock_unlock(); |
313 | 0 | return old_data; |
314 | 0 | } |
315 | | |
316 | | void * |
317 | | coap_cache_set_app_data2_lkd(coap_cache_entry_t *cache_entry, void *app_data, |
318 | 0 | coap_app_data_free_callback_t callback) { |
319 | 0 | void *old_data = cache_entry->app_data; |
320 | |
|
321 | 0 | cache_entry->app_data = app_data; |
322 | 0 | cache_entry->app_cb = app_data ? callback : NULL; |
323 | 0 | return old_data; |
324 | 0 | } |
325 | | |
326 | | void * |
327 | 0 | coap_cache_get_app_data(const coap_cache_entry_t *cache_entry) { |
328 | 0 | return cache_entry->app_data; |
329 | 0 | } |
330 | | |
331 | | void |
332 | 26 | coap_expire_cache_entries(coap_context_t *ctx) { |
333 | 26 | coap_tick_t now; |
334 | 26 | coap_cache_entry_t *cp, *ctmp; |
335 | | |
336 | 26 | coap_ticks(&now); |
337 | 26 | HASH_ITER(hh, ctx->cache, cp, ctmp) { |
338 | 0 | if (cp->idle_timeout > 0) { |
339 | 0 | if (cp->expire_ticks <= now) { |
340 | 0 | coap_delete_cache_entry(ctx, cp); |
341 | 0 | } |
342 | 0 | } |
343 | 0 | } |
344 | 26 | } |
345 | | |
346 | | #endif /* ! COAP_SERVER_SUPPORT */ |