Lines Matching +full:file +full:- +full:entry +full:- +full:cache
1 /* coap_cache.c -- Caching of CoAP requests
3 * Copyright (C) 2020-2023 Olaf Bergmann <bergmann@tzi.org>
5 * SPDX-License-Identifier: BSD-2-Clause
7 * This file is part of the CoAP library libcoap. Please see
12 * @file coap_cache.c
13 * @brief CoAP Cache handling
20 * be used as CacheKey. Options that can be cache keys are not Unsafe
27 /* https://rfc-editor.org/rfc/rfc7252#section-5.4.6 Nocachekey definition */ in is_cache_key()
31 * https://rfc-editor.org/rfc/rfc7641#section-2 Observe is not a in is_cache_key()
32 * part of the cache-key. in is_cache_key()
37 /* Check for option user has defined as not part of cache-key */ in is_cache_key()
51 if (ctx->cache_ignore_options) { in coap_cache_ignore_options()
52 coap_free_type(COAP_STRING, ctx->cache_ignore_options); in coap_cache_ignore_options()
56 ctx->cache_ignore_options = coap_malloc_type(COAP_STRING, count * sizeof(options[0])); in coap_cache_ignore_options()
57 if (ctx->cache_ignore_options) { in coap_cache_ignore_options()
58 memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0])); in coap_cache_ignore_options()
59 ctx->cache_ignore_count = count; in coap_cache_ignore_options()
65 ctx->cache_ignore_options = NULL; in coap_cache_ignore_options()
66 ctx->cache_ignore_count = count; in coap_cache_ignore_options()
111 /* The body of a FETCH payload is part of the cache key, in coap_cache_derive_key_w_ignore()
112 * see https://rfc-editor.org/rfc/rfc8132#section-2 */ in coap_cache_derive_key_w_ignore()
113 if (pdu->code == COAP_REQUEST_CODE_FETCH) { in coap_cache_derive_key_w_ignore()
129 memcpy(cache_key->key, digest.key, sizeof(cache_key->key)); in coap_cache_derive_key_w_ignore()
142 session->context->cache_ignore_options, in coap_cache_derive_key()
143 session->context->cache_ignore_count); in coap_cache_derive_key()
156 coap_cache_entry_t *entry = coap_malloc_type(COAP_CACHE_ENTRY, in coap_new_cache_entry() local
158 if (!entry) { in coap_new_cache_entry()
162 memset(entry, 0, sizeof(coap_cache_entry_t)); in coap_new_cache_entry()
163 entry->session = session; in coap_new_cache_entry()
165 entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size); in coap_new_cache_entry()
166 if (entry->pdu) { in coap_new_cache_entry()
167 if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) { in coap_new_cache_entry()
168 coap_delete_pdu(entry->pdu); in coap_new_cache_entry()
169 coap_free_type(COAP_CACHE_ENTRY, entry); in coap_new_cache_entry()
173 memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token)); in coap_new_cache_entry()
174 memcpy(entry->pdu->token, pdu->token, pdu->used_size); in coap_new_cache_entry()
176 entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token); in coap_new_cache_entry()
179 entry->cache_key = coap_cache_derive_key(session, pdu, session_based); in coap_new_cache_entry()
180 if (!entry->cache_key) { in coap_new_cache_entry()
181 coap_free_type(COAP_CACHE_ENTRY, entry); in coap_new_cache_entry()
184 entry->idle_timeout = idle_timeout; in coap_new_cache_entry()
186 coap_ticks(&entry->expire_ticks); in coap_new_cache_entry()
187 entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND; in coap_new_cache_entry()
190 HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry); in coap_new_cache_entry()
191 return entry; in coap_new_cache_entry()
200 HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry); in coap_cache_get_by_key()
202 if (cache_entry && cache_entry->idle_timeout > 0) { in coap_cache_get_by_key()
203 coap_ticks(&cache_entry->expire_ticks); in coap_cache_get_by_key()
204 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; in coap_cache_get_by_key()
219 cache_entry = coap_cache_get_by_key(session->context, cache_key); in coap_cache_get_by_pdu()
221 if (cache_entry && cache_entry->idle_timeout > 0) { in coap_cache_get_by_pdu()
222 coap_ticks(&cache_entry->expire_ticks); in coap_cache_get_by_pdu()
223 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND; in coap_cache_get_by_pdu()
234 HASH_DELETE(hh, ctx->cache, cache_entry); in coap_delete_cache_entry()
236 if (cache_entry->pdu) { in coap_delete_cache_entry()
237 coap_delete_pdu(cache_entry->pdu); in coap_delete_cache_entry()
239 coap_delete_cache_key(cache_entry->cache_key); in coap_delete_cache_entry()
240 if (cache_entry->callback && cache_entry->app_data) { in coap_delete_cache_entry()
241 cache_entry->callback(cache_entry->app_data); in coap_delete_cache_entry()
248 return cache_entry->pdu; in coap_cache_get_pdu()
255 cache_entry->app_data = data; in coap_cache_set_app_data()
256 cache_entry->callback = callback; in coap_cache_set_app_data()
261 return cache_entry->app_data; in coap_cache_get_app_data()
270 HASH_ITER(hh, ctx->cache, cp, ctmp) { in coap_expire_cache_entries()
271 if (cp->idle_timeout > 0) { in coap_expire_cache_entries()
272 if (cp->expire_ticks <= now) { in coap_expire_cache_entries()