Loading...
Searching...
No Matches
coap_cache.c
Go to the documentation of this file.
/* coap_cache.c -- Caching of CoAP requests
*
* Copyright (C) 2020-2023 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap3/coap_internal.h"
#if COAP_SERVER_SUPPORT
/* Determines if the given option_type denotes an option type that can
* be used as CacheKey. Options that can be cache keys are not Unsafe
* and not marked explicitly as NoCacheKey. */
static int
is_cache_key(uint16_t option_type, size_t cache_ignore_count,
const uint16_t *cache_ignore_options) {
size_t i;
/* https://rfc-editor.org/rfc/rfc7252#section-5.4.6 Nocachekey definition */
if ((option_type & 0x1e) == 0x1c)
return 0;
/*
* https://rfc-editor.org/rfc/rfc7641#section-2 Observe is not a
* part of the cache-key.
*/
if (option_type == COAP_OPTION_OBSERVE)
return 0;
/* Check for option user has defined as not part of cache-key */
for (i = 0; i < cache_ignore_count; i++) {
if (cache_ignore_options[i] == option_type) {
return 0;
}
}
return 1;
}
int
coap_cache_ignore_options(coap_context_t *ctx,
const uint16_t *options,
size_t count) {
if (ctx->cache_ignore_options) {
coap_free_type(COAP_STRING, ctx->cache_ignore_options);
}
if (count) {
assert(options);
ctx->cache_ignore_options = coap_malloc_type(COAP_STRING, count * sizeof(options[0]));
if (ctx->cache_ignore_options) {
memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0]));
ctx->cache_ignore_count = count;
} else {
coap_log_warn("Unable to create cache_ignore_options\n");
return 0;
}
} else {
ctx->cache_ignore_options = NULL;
ctx->cache_ignore_count = count;
}
return 1;
}
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 *cache_ignore_options,
size_t cache_ignore_count) {
coap_opt_t *option;
coap_opt_iterator_t opt_iter;
coap_digest_ctx_t *dctx;
coap_digest_t digest;
coap_cache_key_t *cache_key;
if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
return NULL;
}
dctx = coap_digest_setup();
if (!dctx)
return NULL;
if (session_based == COAP_CACHE_IS_SESSION_BASED) {
/* Include the session ptr */
if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) {
goto update_fail;
}
}
while ((option = coap_option_next(&opt_iter))) {
if (is_cache_key(opt_iter.number, cache_ignore_count,
cache_ignore_options)) {
if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number,
sizeof(opt_iter.number))) {
goto update_fail;
}
if (!coap_digest_update(dctx, coap_opt_value(option),
coap_opt_length(option))) {
goto update_fail;
}
}
}
/* The body of a FETCH payload is part of the cache key,
* see https://rfc-editor.org/rfc/rfc8132#section-2 */
if (pdu->code == COAP_REQUEST_CODE_FETCH) {
size_t len;
const uint8_t *data;
if (coap_get_data(pdu, &len, &data)) {
if (!coap_digest_update(dctx, data, len)) {
goto update_fail;
}
}
}
if (!coap_digest_final(dctx, &digest)) {
/* coap_digest_final() is guaranteed to free off dctx no matter what */
return NULL;
}
cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t));
if (cache_key) {
memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
}
return cache_key;
update_fail:
coap_digest_free(dctx);
return NULL;
}
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) {
return coap_cache_derive_key_w_ignore(session, pdu, session_based,
session->context->cache_ignore_options,
session->context->cache_ignore_count);
}
void
coap_delete_cache_key(coap_cache_key_t *cache_key) {
coap_free_type(COAP_CACHE_KEY, cache_key);
}
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_timeout) {
coap_cache_entry_t *entry = coap_malloc_type(COAP_CACHE_ENTRY,
sizeof(coap_cache_entry_t));
if (!entry) {
return NULL;
}
memset(entry, 0, sizeof(coap_cache_entry_t));
entry->session = session;
if (record_pdu == COAP_CACHE_RECORD_PDU) {
entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size);
if (entry->pdu) {
if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) {
coap_delete_pdu(entry->pdu);
coap_free_type(COAP_CACHE_ENTRY, entry);
return NULL;
}
/* Need to get the appropriate data across */
memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
memcpy(entry->pdu->token, pdu->token, pdu->used_size);
/* And adjust all the pointers etc. */
entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
}
}
entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
if (!entry->cache_key) {
coap_free_type(COAP_CACHE_ENTRY, entry);
return NULL;
}
entry->idle_timeout = idle_timeout;
if (idle_timeout > 0) {
coap_ticks(&entry->expire_ticks);
entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
}
HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
return entry;
}
coap_cache_entry_t *
coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) {
coap_cache_entry_t *cache_entry = NULL;
assert(cache_key);
if (cache_key) {
HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
}
if (cache_entry && cache_entry->idle_timeout > 0) {
coap_ticks(&cache_entry->expire_ticks);
cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
}
return cache_entry;
}
coap_cache_entry_t *
coap_cache_get_by_pdu(coap_session_t *session,
const coap_pdu_t *request,
coap_cache_session_based_t session_based) {
coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
coap_cache_entry_t *cache_entry;
if (!cache_key)
return NULL;
cache_entry = coap_cache_get_by_key(session->context, cache_key);
coap_delete_cache_key(cache_key);
if (cache_entry && cache_entry->idle_timeout > 0) {
coap_ticks(&cache_entry->expire_ticks);
cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
}
return cache_entry;
}
void
coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry) {
assert(cache_entry);
if (cache_entry) {
HASH_DELETE(hh, ctx->cache, cache_entry);
}
if (cache_entry->pdu) {
coap_delete_pdu(cache_entry->pdu);
}
coap_delete_cache_key(cache_entry->cache_key);
if (cache_entry->callback && cache_entry->app_data) {
cache_entry->callback(cache_entry->app_data);
}
coap_free_type(COAP_CACHE_ENTRY, cache_entry);
}
const coap_pdu_t *
coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) {
return cache_entry->pdu;
}
void
coap_cache_set_app_data(coap_cache_entry_t *cache_entry,
void *data,
coap_cache_app_data_free_callback_t callback) {
cache_entry->app_data = data;
cache_entry->callback = callback;
}
void *
coap_cache_get_app_data(const coap_cache_entry_t *cache_entry) {
return cache_entry->app_data;
}
void
coap_expire_cache_entries(coap_context_t *ctx) {
coap_tick_t now;
coap_cache_entry_t *cp, *ctmp;
coap_ticks(&now);
HASH_ITER(hh, ctx->cache, cp, ctmp) {
if (cp->idle_timeout > 0) {
if (cp->expire_ticks <= now) {
coap_delete_cache_entry(ctx, cp);
}
}
}
}
#endif /* ! COAP_SERVER_SUPPORT */
coap_internal.h
Pulls together all the internal only header files.
COAP_CACHE_KEY
@ COAP_CACHE_KEY
Definition coap_mem.h:52
COAP_CACHE_ENTRY
@ COAP_CACHE_ENTRY
Definition coap_mem.h:53
COAP_STRING
@ COAP_STRING
Definition coap_mem.h:38
coap_malloc_type
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.
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_opt_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition coap_option.h:26
coap_digest_free
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
coap_digest_final
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
coap_digest_update
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t
void coap_digest_ctx_t
Definition coap_cache_internal.h:61
coap_expire_cache_entries
void coap_expire_cache_entries(coap_context_t *context)
Expire coap_cache_entry_t entries.
coap_digest_setup
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
coap_delete_cache_entry
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_delete_cache_key
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_derive_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
coap_cache_record_pdu_t
Definition coap_cache.h:42
coap_cache_app_data_free_callback_t
void(* coap_cache_app_data_free_callback_t)(void *data)
Callback to free off the app data when the cache-entry is being deleted / freed off.
Definition coap_cache.h:35
coap_cache_get_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_cache_get_by_pdu
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.
coap_cache_ignore_options
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.
coap_cache_derive_key_w_ignore
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.
coap_new_cache_entry
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_cache_get_app_data
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_cache_get_by_key
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_set_app_data
void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void *data, coap_cache_app_data_free_callback_t callback)
Stores data with the given cache entry.
coap_cache_session_based_t
coap_cache_session_based_t
Definition coap_cache.h:37
COAP_CACHE_RECORD_PDU
@ COAP_CACHE_RECORD_PDU
Definition coap_cache.h:44
COAP_CACHE_IS_SESSION_BASED
@ COAP_CACHE_IS_SESSION_BASED
Definition coap_cache.h:39
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:144
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:159
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_log_warn
#define coap_log_warn(...)
Definition coap_debug.h:102
coap_option_next
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition coap_option.c:153
coap_opt_length
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition coap_option.c:212
coap_option_iterator_init
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.
Definition coap_option.c:117
COAP_OPT_ALL
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition coap_option.h:108
coap_opt_value
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition coap_option.c:249
coap_pdu_resize
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition coap_pdu.c:245
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition coap_pdu.c:163
coap_get_data
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:797
coap_pdu_init
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:97
COAP_OPTION_OBSERVE
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:119
COAP_REQUEST_CODE_FETCH
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:325
coap_cache_entry_t
Definition coap_cache_internal.h:41
coap_cache_entry_t::session
coap_session_t * session
Definition coap_cache_internal.h:44
coap_cache_entry_t::callback
coap_cache_app_data_free_callback_t callback
Definition coap_cache_internal.h:49
coap_cache_entry_t::expire_ticks
coap_tick_t expire_ticks
Definition coap_cache_internal.h:47
coap_cache_entry_t::app_data
void * app_data
Definition coap_cache_internal.h:46
coap_cache_entry_t::idle_timeout
unsigned int idle_timeout
Definition coap_cache_internal.h:48
coap_cache_entry_t::cache_key
coap_cache_key_t * cache_key
Definition coap_cache_internal.h:43
coap_cache_entry_t::pdu
coap_pdu_t * pdu
Definition coap_cache_internal.h:45
coap_cache_key_t
Definition coap_cache_internal.h:37
coap_cache_key_t::key
uint8_t key[32]
Definition coap_cache_internal.h:38
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition coap_net_internal.h:49
coap_context_t::cache_ignore_options
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
Definition coap_net_internal.h:166
coap_context_t::cache_ignore_count
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
Definition coap_net_internal.h:168
coap_context_t::cache
coap_cache_entry_t * cache
CoAP cache-entry cache.
Definition coap_net_internal.h:165
coap_digest_t
Definition coap_cache_internal.h:33
coap_digest_t::key
uint8_t key[32]
Definition coap_cache_internal.h:34
coap_opt_iterator_t
Iterator to run through PDU options.
Definition coap_option.h:168
coap_opt_iterator_t::number
coap_option_num_t number
decoded option number
Definition coap_option.h:170
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:119
coap_pdu_t::token
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
Definition coap_pdu_internal.h:139
coap_pdu_t::code
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
Definition coap_pdu_internal.h:121
coap_pdu_t::data
uint8_t * data
first byte of payload, if any
Definition coap_pdu_internal.h:141
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition coap_pdu_internal.h:123
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition coap_pdu_internal.h:135
coap_pdu_t::alloc_size
size_t alloc_size
allocated storage for token, options and payload
Definition coap_pdu_internal.h:133
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition coap_pdu_internal.h:120
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition coap_session_internal.h:68
coap_session_t::context
coap_context_t * context
session's context
Definition coap_session_internal.h:86