libcoap  4.3.0rc2
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 * This file is part of the CoAP library libcoap. Please see
6 * README for terms of use.
7 */
8 
9 #include "coap3/coap_internal.h"
10 
11 /* Determines if the given option_type denotes an option type that can
12  * be used as CacheKey. Options that can be cache keys are not Unsafe
13  * and not marked explicitly as NoCacheKey. */
14 static int
15 is_cache_key(const coap_context_t *ctx, uint16_t option_type) {
16  int nocachekey = (option_type & 0x1e) == 0x1c;
17  size_t i;
18 
19  /*
20  * https://tools.ietf.org/html/rfc7641#section-2 Observe is not a
21  * part of the cache-key.
22  */
23  if (option_type == COAP_OPTION_OBSERVE) {
24  nocachekey = 1;
25  }
26 
27  /* Check for option user has defined as not part of cache-key */
28  for (i = 0; i < ctx->cache_ignore_count; i++) {
29  if (ctx->cache_ignore_options[i] == option_type) {
30  nocachekey = 1;
31  break;
32  }
33  }
34 
35  return nocachekey == 0;
36 }
37 
38 int
40  const uint16_t *options,
41  size_t count) {
42  if (ctx->cache_ignore_options) {
44  }
45  if (count) {
46  assert(options);
47  ctx->cache_ignore_options = coap_malloc(count * sizeof(options[0]));
48  if (ctx->cache_ignore_options) {
49  memcpy(ctx->cache_ignore_options, options, count * sizeof(options[0]));
50  ctx->cache_ignore_count = count;
51  }
52  else {
53  coap_log(LOG_WARNING, "Unable to create cache_ignore_options\n");
54  return 0;
55  }
56  }
57  else {
58  ctx->cache_ignore_options = NULL;
59  ctx->cache_ignore_count = count;
60  }
61  return 1;
62 }
63 
66  const coap_pdu_t *pdu,
67  coap_cache_session_based_t session_based) {
68  coap_opt_t *option;
69  coap_opt_iterator_t opt_iter;
70  coap_digest_ctx_t *dctx;
71  coap_digest_t digest;
72  coap_cache_key_t *cache_key;
73 
74  if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
75  return NULL;
76  }
77 
78  dctx = coap_digest_setup();
79  if (!dctx)
80  return NULL;
81 
82  if (session_based == COAP_CACHE_IS_SESSION_BASED) {
83  /* Include the session ptr */
84  if (!coap_digest_update(dctx, (const uint8_t*)session, sizeof(session))) {
85  coap_digest_free(dctx);
86  return NULL;
87  }
88  }
89  while ((option = coap_option_next(&opt_iter))) {
90  if (is_cache_key(session->context, opt_iter.number)) {
91  if (!coap_digest_update(dctx, option, coap_opt_size(option))) {
92  coap_digest_free(dctx);
93  return NULL;
94  }
95  }
96  }
97 
98  /* The body of a FETCH payload is part of the cache key,
99  * see https://tools.ietf.org/html/rfc8132#section-2 */
100  if (pdu->code == COAP_REQUEST_CODE_FETCH) {
101  size_t len;
102  const uint8_t *data;
103  if (coap_get_data(pdu, &len, &data)) {
104  if (!coap_digest_update(dctx, data, len)) {
105  coap_digest_free(dctx);
106  return NULL;
107  }
108  }
109  }
110 
111  if (!coap_digest_final(dctx, &digest)) {
112  /* coap_digest_final() is guaranteed to free off dctx no matter what */
113  return NULL;
114  }
115  cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t));
116  if (cache_key) {
117  memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
118  }
119  return cache_key;
120 }
121 
122 void
124  coap_free_type(COAP_CACHE_KEY, cache_key);
125 }
126 
129  coap_cache_record_pdu_t record_pdu,
130  coap_cache_session_based_t session_based,
131  unsigned int idle_timeout) {
133  sizeof(coap_cache_entry_t));
134  if (!entry) {
135  return NULL;
136  }
137 
138  memset(entry, 0, sizeof(coap_cache_entry_t));
139  entry->session = session;
140  if (record_pdu == COAP_CACHE_RECORD_PDU) {
141  entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size);
142  if (entry->pdu) {
143  if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) {
144  coap_delete_pdu(entry->pdu);
146  return NULL;
147  }
148  /* Need to get the appropriate data across */
149  memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
150  memcpy(entry->pdu->token, pdu->token, pdu->used_size);
151  /* And adjust all the pointers etc. */
152  entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
153  }
154  }
155  entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
156  if (!entry->cache_key) {
158  return NULL;
159  }
160  entry->idle_timeout = idle_timeout;
161  if (idle_timeout > 0) {
162  coap_ticks(&entry->expire_ticks);
163  entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
164  }
165 
166  HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
167  return entry;
168 }
169 
172  coap_cache_entry_t *cache_entry = NULL;
173 
174  assert(cache_key);
175  if (cache_key) {
176  HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
177  }
178  if (cache_entry && cache_entry->idle_timeout > 0) {
179  coap_ticks(&cache_entry->expire_ticks);
180  cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
181  }
182  return cache_entry;
183 }
184 
187  const coap_pdu_t *request,
188  coap_cache_session_based_t session_based) {
189  coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
190  coap_cache_entry_t *cache_entry;
191 
192  if (!cache_key)
193  return NULL;
194 
195  cache_entry = coap_cache_get_by_key(session->context, cache_key);
196  coap_delete_cache_key(cache_key);
197  if (cache_entry && cache_entry->idle_timeout > 0) {
198  coap_ticks(&cache_entry->expire_ticks);
199  cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
200  }
201  return cache_entry;
202 }
203 
204 void
206 
207  assert(cache_entry);
208 
209  if (cache_entry) {
210  HASH_DELETE(hh, ctx->cache, cache_entry);
211  }
212  if (cache_entry->pdu) {
213  coap_delete_pdu(cache_entry->pdu);
214  }
215  coap_delete_cache_key(cache_entry->cache_key);
216  if (cache_entry->callback && cache_entry->app_data) {
217  cache_entry->callback(cache_entry->app_data);
218  }
219  coap_free_type(COAP_CACHE_ENTRY, cache_entry);
220 }
221 
222 const coap_pdu_t *
224  return cache_entry->pdu;
225 }
226 
227 void
229  void *data,
231  cache_entry->app_data = data;
232  cache_entry->callback = callback;
233 }
234 
235 void *
237  return cache_entry->app_data;
238 }
239 
240 void
242  coap_tick_t now;
243  coap_cache_entry_t *cp, *ctmp;
244 
245  coap_ticks(&now);
246  HASH_ITER(hh, ctx->cache, cp, ctmp) {
247  if (cp->idle_timeout > 0) {
248  if (cp->expire_ticks <= now) {
249  coap_delete_cache_entry(ctx, cp);
250  }
251  }
252  }
253 }
254 
static int is_cache_key(const coap_context_t *ctx, uint16_t option_type)
Definition: coap_cache.c:15
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:203
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
Definition: coap_notls.c:192
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:220
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:208
void coap_digest_ctx_t
void coap_expire_cache_entries(coap_context_t *ctx)
Expire coap_cache_entry_t entries.
Definition: coap_cache.c:241
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:205
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:186
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:223
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
Definition: coap_cache.c:123
coap_cache_record_pdu_t
Definition: coap_cache.h:38
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:31
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:65
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:171
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:39
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:128
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:236
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:228
coap_cache_session_based_t
Definition: coap_cache.h:33
@ COAP_CACHE_RECORD_PDU
Definition: coap_cache.h:40
@ COAP_CACHE_IS_SESSION_BASED
Definition: coap_cache.h:35
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:120
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition: coap_time.h:135
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:150
@ LOG_WARNING
Warning.
Definition: coap_debug.h:54
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:146
#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:110
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
Definition: pdu.c:205
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:140
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:652
#define COAP_OPTION_OBSERVE
Definition: pdu.h:109
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:85
@ COAP_REQUEST_CODE_FETCH
Definition: pdu.h:296
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:96
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:103
@ COAP_CACHE_KEY
Definition: mem.h:46
@ COAP_CACHE_ENTRY
Definition: mem.h:47
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:281
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
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:448
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1059
#define HASH_ADD(hh, head, fieldname, keylen_in, add)
Definition: uthash.h:428
#define HASH_FIND(hh, head, keyptr, keylen, out)
Definition: uthash.h:166