libcoap 4.3.1
coap_cache.c
Go to the documentation of this file.
1/* coap_cache.c -- Caching of CoAP requests
2*
3* Copyright (C) 2020 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
16#include "coap3/coap_internal.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. */
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://tools.ietf.org/html/rfc7252#section-5.4.6 Nocachekey definition */
28 if ((option_type & 0x1e) == 0x1c)
29 return 0;
30 /*
31 * https://tools.ietf.org/html/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
47int
49 const uint16_t *options,
50 size_t count) {
51 if (ctx->cache_ignore_options) {
53 }
54 if (count) {
55 assert(options);
56 ctx->cache_ignore_options = coap_malloc(count * sizeof(options[0]));
57 if (ctx->cache_ignore_options) {
58 memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0]));
59 ctx->cache_ignore_count = count;
60 }
61 else {
62 coap_log(LOG_WARNING, "Unable to create cache_ignore_options\n");
63 return 0;
64 }
65 }
66 else {
67 ctx->cache_ignore_options = NULL;
68 ctx->cache_ignore_count = count;
69 }
70 return 1;
71}
72
75 const coap_pdu_t *pdu,
76 coap_cache_session_based_t session_based,
77 const uint16_t *cache_ignore_options,
78 size_t cache_ignore_count) {
79 coap_opt_t *option;
80 coap_opt_iterator_t opt_iter;
82 coap_digest_t digest;
83 coap_cache_key_t *cache_key;
84
85 if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
86 return NULL;
87 }
88
89 dctx = coap_digest_setup();
90 if (!dctx)
91 return NULL;
92
93 if (session_based == COAP_CACHE_IS_SESSION_BASED) {
94 /* Include the session ptr */
95 if (!coap_digest_update(dctx, (const uint8_t*)&session, sizeof(session))) {
96 goto update_fail;
97 }
98 }
99 while ((option = coap_option_next(&opt_iter))) {
100 if (is_cache_key(opt_iter.number, cache_ignore_count,
101 cache_ignore_options)) {
102 if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number,
103 sizeof(opt_iter.number))) {
104 goto update_fail;
105 }
106 if (!coap_digest_update(dctx, coap_opt_value(option),
107 coap_opt_length(option))) {
108 goto update_fail;
109 }
110 }
111 }
112
113 /* The body of a FETCH payload is part of the cache key,
114 * see https://tools.ietf.org/html/rfc8132#section-2 */
115 if (pdu->code == COAP_REQUEST_CODE_FETCH) {
116 size_t len;
117 const uint8_t *data;
118 if (coap_get_data(pdu, &len, &data)) {
119 if (!coap_digest_update(dctx, data, len)) {
120 goto update_fail;
121 }
122 }
123 }
124
125 if (!coap_digest_final(dctx, &digest)) {
126 /* coap_digest_final() is guaranteed to free off dctx no matter what */
127 return NULL;
128 }
130 if (cache_key) {
131 memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
132 }
133 return cache_key;
134update_fail:
135 coap_digest_free(dctx);
136 return NULL;
137}
138
141 const coap_pdu_t *pdu,
142 coap_cache_session_based_t session_based) {
143 return coap_cache_derive_key_w_ignore(session, pdu, session_based,
145 session->context->cache_ignore_count);
146}
147
148void
150 coap_free_type(COAP_CACHE_KEY, cache_key);
151}
152
155 coap_cache_record_pdu_t record_pdu,
156 coap_cache_session_based_t session_based,
157 unsigned int idle_timeout) {
159 sizeof(coap_cache_entry_t));
160 if (!entry) {
161 return NULL;
162 }
163
164 memset(entry, 0, sizeof(coap_cache_entry_t));
165 entry->session = session;
166 if (record_pdu == COAP_CACHE_RECORD_PDU) {
167 entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size);
168 if (entry->pdu) {
169 if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) {
170 coap_delete_pdu(entry->pdu);
172 return NULL;
173 }
174 /* Need to get the appropriate data across */
175 memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
176 memcpy(entry->pdu->token, pdu->token, pdu->used_size);
177 /* And adjust all the pointers etc. */
178 entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
179 }
180 }
181 entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
182 if (!entry->cache_key) {
184 return NULL;
185 }
186 entry->idle_timeout = idle_timeout;
187 if (idle_timeout > 0) {
188 coap_ticks(&entry->expire_ticks);
189 entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
190 }
191
192 HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
193 return entry;
194}
195
198 coap_cache_entry_t *cache_entry = NULL;
199
200 assert(cache_key);
201 if (cache_key) {
202 HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
203 }
204 if (cache_entry && cache_entry->idle_timeout > 0) {
205 coap_ticks(&cache_entry->expire_ticks);
206 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
207 }
208 return cache_entry;
209}
210
213 const coap_pdu_t *request,
214 coap_cache_session_based_t session_based) {
215 coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
216 coap_cache_entry_t *cache_entry;
217
218 if (!cache_key)
219 return NULL;
220
221 cache_entry = coap_cache_get_by_key(session->context, cache_key);
222 coap_delete_cache_key(cache_key);
223 if (cache_entry && cache_entry->idle_timeout > 0) {
224 coap_ticks(&cache_entry->expire_ticks);
225 cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
226 }
227 return cache_entry;
228}
229
230void
232
233 assert(cache_entry);
234
235 if (cache_entry) {
236 HASH_DELETE(hh, ctx->cache, cache_entry);
237 }
238 if (cache_entry->pdu) {
239 coap_delete_pdu(cache_entry->pdu);
240 }
241 coap_delete_cache_key(cache_entry->cache_key);
242 if (cache_entry->callback && cache_entry->app_data) {
243 cache_entry->callback(cache_entry->app_data);
244 }
245 coap_free_type(COAP_CACHE_ENTRY, cache_entry);
246}
247
248const coap_pdu_t *
249coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) {
250 return cache_entry->pdu;
251}
252
253void
255 void *data,
257 cache_entry->app_data = data;
258 cache_entry->callback = callback;
259}
260
261void *
263 return cache_entry->app_data;
264}
265
266void
268 coap_tick_t now;
269 coap_cache_entry_t *cp, *ctmp;
270
271 coap_ticks(&now);
272 HASH_ITER(hh, ctx->cache, cp, ctmp) {
273 if (cp->idle_timeout > 0) {
274 if (cp->expire_ticks <= now) {
276 }
277 }
278 }
279}
280
281#endif /* ! COAP_SERVER_SUPPORT */
Pulls together all the internal only header files.
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
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
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.
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.
void coap_digest_ctx_t
void coap_expire_cache_entries(coap_context_t *context)
Expire coap_cache_entry_t entries.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
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...
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the 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:42
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
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_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.
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_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_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.
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_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.
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
Definition: coap_cache.h:37
@ COAP_CACHE_RECORD_PDU
Definition: coap_cache.h:44
@ COAP_CACHE_IS_SESSION_BASED
Definition: coap_cache.h:39
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:127
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:142
#define LOG_WARNING
Definition: coap_debug.h:72
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:165
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:152
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:215
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:116
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: coap_option.h:108
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:252
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:223
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:154
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: pdu.c:713
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: pdu.c:99
#define COAP_OPTION_OBSERVE
Definition: pdu.h:115
@ COAP_REQUEST_CODE_FETCH
Definition: pdu.h:310
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:110
COAP_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:103
@ COAP_CACHE_KEY
Definition: mem.h:53
@ COAP_CACHE_ENTRY
Definition: mem.h:54
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().
coap_session_t * session
coap_cache_app_data_free_callback_t callback
coap_tick_t expire_ticks
void * app_data
unsigned int idle_timeout
coap_cache_key_t * cache_key
coap_pdu_t * pdu
The CoAP stack's global state is stored in a coap_context_t object.
uint16_t * cache_ignore_options
CoAP options to ignore when creating a cache-key.
size_t cache_ignore_count
The number of CoAP options to ignore when creating a cache-key.
coap_cache_entry_t * cache
CoAP cache-entry cache.
Iterator to run through PDU options.
Definition: coap_option.h:171
coap_option_num_t number
decoded option number
Definition: coap_option.h:173
structure for CoAP PDUs
uint8_t * token
first byte of token, if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload
coap_pdu_type_t type
message type
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