libcoap  4.3.0
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 
11 #include "coap3/coap_internal.h"
12 
13 /* Determines if the given option_type denotes an option type that can
14  * be used as CacheKey. Options that can be cache keys are not Unsafe
15  * and not marked explicitly as NoCacheKey. */
16 static int
17 is_cache_key(uint16_t option_type, size_t cache_ignore_count,
18  const uint16_t *cache_ignore_options) {
19  size_t i;
20 
21  /* https://tools.ietf.org/html/rfc7252#section-5.4.6 Nocachekey definition */
22  if ((option_type & 0x1e) == 0x1c)
23  return 0;
24  /*
25  * https://tools.ietf.org/html/rfc7641#section-2 Observe is not a
26  * part of the cache-key.
27  */
28  if (option_type == COAP_OPTION_OBSERVE)
29  return 0;
30 
31  /* Check for option user has defined as not part of cache-key */
32  for (i = 0; i < cache_ignore_count; i++) {
33  if (cache_ignore_options[i] == option_type) {
34  return 0;
35  }
36  }
37 
38  return 1;
39 }
40 
41 int
43  const uint16_t *options,
44  size_t count) {
45  if (ctx->cache_ignore_options) {
47  }
48  if (count) {
49  assert(options);
50  ctx->cache_ignore_options = coap_malloc(count * sizeof(options[0]));
51  if (ctx->cache_ignore_options) {
52  memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0]));
53  ctx->cache_ignore_count = count;
54  }
55  else {
56  coap_log(LOG_WARNING, "Unable to create cache_ignore_options\n");
57  return 0;
58  }
59  }
60  else {
61  ctx->cache_ignore_options = NULL;
62  ctx->cache_ignore_count = count;
63  }
64  return 1;
65 }
66 
69  const coap_pdu_t *pdu,
70  coap_cache_session_based_t session_based,
71  const uint16_t *cache_ignore_options,
72  size_t cache_ignore_count) {
73  coap_opt_t *option;
74  coap_opt_iterator_t opt_iter;
75  coap_digest_ctx_t *dctx;
76  coap_digest_t digest;
77  coap_cache_key_t *cache_key;
78 
79  if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
80  return NULL;
81  }
82 
83  dctx = coap_digest_setup();
84  if (!dctx)
85  return NULL;
86 
87  if (session_based == COAP_CACHE_IS_SESSION_BASED) {
88  /* Include the session ptr */
89  if (!coap_digest_update(dctx, (const uint8_t*)session, sizeof(session))) {
90  coap_digest_free(dctx);
91  return NULL;
92  }
93  }
94  while ((option = coap_option_next(&opt_iter))) {
95  if (is_cache_key(opt_iter.number, cache_ignore_count,
96  cache_ignore_options)) {
97  if (!coap_digest_update(dctx, option, coap_opt_size(option))) {
98  coap_digest_free(dctx);
99  return NULL;
100  }
101  }
102  }
103 
104  /* The body of a FETCH payload is part of the cache key,
105  * see https://tools.ietf.org/html/rfc8132#section-2 */
106  if (pdu->code == COAP_REQUEST_CODE_FETCH) {
107  size_t len;
108  const uint8_t *data;
109  if (coap_get_data(pdu, &len, &data)) {
110  if (!coap_digest_update(dctx, data, len)) {
111  coap_digest_free(dctx);
112  return NULL;
113  }
114  }
115  }
116 
117  if (!coap_digest_final(dctx, &digest)) {
118  /* coap_digest_final() is guaranteed to free off dctx no matter what */
119  return NULL;
120  }
121  cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t));
122  if (cache_key) {
123  memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
124  }
125  return cache_key;
126 }
127 
130  const coap_pdu_t *pdu,
131  coap_cache_session_based_t session_based) {
132  return coap_cache_derive_key_w_ignore(session, pdu, session_based,
133  session->context->cache_ignore_options,
134  session->context->cache_ignore_count);
135 }
136 
137 void
139  coap_free_type(COAP_CACHE_KEY, cache_key);
140 }
141 
144  coap_cache_record_pdu_t record_pdu,
145  coap_cache_session_based_t session_based,
146  unsigned int idle_timeout) {
148  sizeof(coap_cache_entry_t));
149  if (!entry) {
150  return NULL;
151  }
152 
153  memset(entry, 0, sizeof(coap_cache_entry_t));
154  entry->session = session;
155  if (record_pdu == COAP_CACHE_RECORD_PDU) {
156  entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size);
157  if (entry->pdu) {
158  if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) {
159  coap_delete_pdu(entry->pdu);
161  return NULL;
162  }
163  /* Need to get the appropriate data across */
164  memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
165  memcpy(entry->pdu->token, pdu->token, pdu->used_size);
166  /* And adjust all the pointers etc. */
167  entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
168  }
169  }
170  entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
171  if (!entry->cache_key) {
173  return NULL;
174  }
175  entry->idle_timeout = idle_timeout;
176  if (idle_timeout > 0) {
177  coap_ticks(&entry->expire_ticks);
178  entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
179  }
180 
181  HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
182  return entry;
183 }
184 
187  coap_cache_entry_t *cache_entry = NULL;
188 
189  assert(cache_key);
190  if (cache_key) {
191  HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
192  }
193  if (cache_entry && cache_entry->idle_timeout > 0) {
194  coap_ticks(&cache_entry->expire_ticks);
195  cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
196  }
197  return cache_entry;
198 }
199 
202  const coap_pdu_t *request,
203  coap_cache_session_based_t session_based) {
204  coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
205  coap_cache_entry_t *cache_entry;
206 
207  if (!cache_key)
208  return NULL;
209 
210  cache_entry = coap_cache_get_by_key(session->context, cache_key);
211  coap_delete_cache_key(cache_key);
212  if (cache_entry && cache_entry->idle_timeout > 0) {
213  coap_ticks(&cache_entry->expire_ticks);
214  cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
215  }
216  return cache_entry;
217 }
218 
219 void
221 
222  assert(cache_entry);
223 
224  if (cache_entry) {
225  HASH_DELETE(hh, ctx->cache, cache_entry);
226  }
227  if (cache_entry->pdu) {
228  coap_delete_pdu(cache_entry->pdu);
229  }
230  coap_delete_cache_key(cache_entry->cache_key);
231  if (cache_entry->callback && cache_entry->app_data) {
232  cache_entry->callback(cache_entry->app_data);
233  }
234  coap_free_type(COAP_CACHE_ENTRY, cache_entry);
235 }
236 
237 const coap_pdu_t *
239  return cache_entry->pdu;
240 }
241 
242 void
244  void *data,
246  cache_entry->app_data = data;
247  cache_entry->callback = callback;
248 }
249 
250 void *
252  return cache_entry->app_data;
253 }
254 
255 void
257  coap_tick_t now;
258  coap_cache_entry_t *cp, *ctmp;
259 
260  coap_ticks(&now);
261  HASH_ITER(hh, ctx->cache, cp, ctmp) {
262  if (cp->idle_timeout > 0) {
263  if (cp->expire_ticks <= now) {
264  coap_delete_cache_entry(ctx, cp);
265  }
266  }
267  }
268 }
269 
static int is_cache_key(uint16_t option_type, size_t cache_ignore_count, const uint16_t *cache_ignore_options)
Definition: coap_cache.c:17
Pulls together all the internal only header files.
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
Definition: coap_notls.c:205
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
Definition: coap_notls.c:194
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.
Definition: coap_notls.c:222
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.
Definition: coap_notls.c:210
void coap_digest_ctx_t
void coap_expire_cache_entries(coap_context_t *ctx)
Expire coap_cache_entry_t entries.
Definition: coap_cache.c:256
void coap_delete_cache_entry(coap_context_t *ctx, 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...
Definition: coap_cache.c:220
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)
Searches for a cache-entry corresponding to pdu.
Definition: coap_cache.c:201
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.
Definition: coap_cache.c:238
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
Definition: coap_cache.c:138
coap_cache_record_pdu_t
Definition: coap_cache.h:40
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)
Calculates a cache-key for the given CoAP PDU.
Definition: coap_cache.c:68
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:33
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.
Definition: coap_cache.c:129
coap_cache_entry_t * coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key)
Searches for a cache-entry identified by cache_key.
Definition: coap_cache.c:186
int coap_cache_ignore_options(coap_context_t *ctx, const uint16_t *options, size_t count)
Define the CoAP options that are not to be included when calculating the cache-key.
Definition: coap_cache.c:42
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)
Create a new cache-entry hash keyed by cache-key derived from the PDU.
Definition: coap_cache.c:143
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...
Definition: coap_cache.c:251
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.
Definition: coap_cache.c:243
coap_cache_session_based_t
Definition: coap_cache.h:35
@ COAP_CACHE_RECORD_PDU
Definition: coap_cache.h:42
@ COAP_CACHE_IS_SESSION_BASED
Definition: coap_cache.h:37
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:122
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:137
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:152
@ LOG_WARNING
Warning.
Definition: coap_debug.h:56
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:148
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
Definition: option.h:107
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: option.c:112
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:207
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:142
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:654
#define COAP_OPTION_OBSERVE
Definition: pdu.h:111
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:87
@ COAP_REQUEST_CODE_FETCH
Definition: pdu.h:298
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_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
Definition: mem.h:98
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:105
@ COAP_CACHE_KEY
Definition: mem.h:48
@ COAP_CACHE_ENTRY
Definition: mem.h:49
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
size_t coap_opt_size(const coap_opt_t *opt)
Returns the size of the given option, taking into account a possible option jump.
Definition: option.c:283
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:26
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: option.h:170
coap_option_num_t number
decoded option number
Definition: option.h:172
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
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