• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* coap_cache.c -- Caching of CoAP requests
2 *
3 * Copyright (C) 2020-2023 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 /**
12  * @file coap_cache.c
13  * @brief CoAP Cache handling
14  */
15 
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. */
22 static int
is_cache_key(uint16_t option_type,size_t cache_ignore_count,const uint16_t * cache_ignore_options)23 is_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://rfc-editor.org/rfc/rfc7252#section-5.4.6 Nocachekey definition */
28   if ((option_type & 0x1e) == 0x1c)
29     return 0;
30   /*
31    * https://rfc-editor.org/rfc/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 
47 int
coap_cache_ignore_options(coap_context_t * ctx,const uint16_t * options,size_t count)48 coap_cache_ignore_options(coap_context_t *ctx,
49                           const uint16_t *options,
50                           size_t count) {
51   if (ctx->cache_ignore_options) {
52     coap_free_type(COAP_STRING, ctx->cache_ignore_options);
53   }
54   if (count) {
55     assert(options);
56     ctx->cache_ignore_options = coap_malloc_type(COAP_STRING, 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     } else {
61       coap_log_warn("Unable to create cache_ignore_options\n");
62       return 0;
63     }
64   } else {
65     ctx->cache_ignore_options = NULL;
66     ctx->cache_ignore_count = count;
67   }
68   return 1;
69 }
70 
71 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)72 coap_cache_derive_key_w_ignore(const coap_session_t *session,
73                                const coap_pdu_t *pdu,
74                                coap_cache_session_based_t session_based,
75                                const uint16_t *cache_ignore_options,
76                                size_t cache_ignore_count) {
77   coap_opt_t *option;
78   coap_opt_iterator_t opt_iter;
79   coap_digest_ctx_t *dctx;
80   coap_digest_t digest;
81   coap_cache_key_t *cache_key;
82 
83   if (!coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL)) {
84     return NULL;
85   }
86 
87   dctx = coap_digest_setup();
88   if (!dctx)
89     return NULL;
90 
91   if (session_based == COAP_CACHE_IS_SESSION_BASED) {
92     /* Include the session ptr */
93     if (!coap_digest_update(dctx, (const uint8_t *)&session, sizeof(session))) {
94       goto update_fail;
95     }
96   }
97   while ((option = coap_option_next(&opt_iter))) {
98     if (is_cache_key(opt_iter.number, cache_ignore_count,
99                      cache_ignore_options)) {
100       if (!coap_digest_update(dctx, (const uint8_t *)&opt_iter.number,
101                               sizeof(opt_iter.number))) {
102         goto update_fail;
103       }
104       if (!coap_digest_update(dctx, coap_opt_value(option),
105                               coap_opt_length(option))) {
106         goto update_fail;
107       }
108     }
109   }
110 
111   /* The body of a FETCH payload is part of the cache key,
112    * see https://rfc-editor.org/rfc/rfc8132#section-2 */
113   if (pdu->code == COAP_REQUEST_CODE_FETCH) {
114     size_t len;
115     const uint8_t *data;
116     if (coap_get_data(pdu, &len, &data)) {
117       if (!coap_digest_update(dctx, data, len)) {
118         goto update_fail;
119       }
120     }
121   }
122 
123   if (!coap_digest_final(dctx, &digest)) {
124     /* coap_digest_final() is guaranteed to free off dctx no matter what */
125     return NULL;
126   }
127   cache_key = coap_malloc_type(COAP_CACHE_KEY, sizeof(coap_cache_key_t));
128   if (cache_key) {
129     memcpy(cache_key->key, digest.key, sizeof(cache_key->key));
130   }
131   return cache_key;
132 update_fail:
133   coap_digest_free(dctx);
134   return NULL;
135 }
136 
137 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)138 coap_cache_derive_key(const coap_session_t *session,
139                       const coap_pdu_t *pdu,
140                       coap_cache_session_based_t session_based) {
141   return coap_cache_derive_key_w_ignore(session, pdu, session_based,
142                                         session->context->cache_ignore_options,
143                                         session->context->cache_ignore_count);
144 }
145 
146 void
coap_delete_cache_key(coap_cache_key_t * cache_key)147 coap_delete_cache_key(coap_cache_key_t *cache_key) {
148   coap_free_type(COAP_CACHE_KEY, cache_key);
149 }
150 
151 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)152 coap_new_cache_entry(coap_session_t *session, const coap_pdu_t *pdu,
153                      coap_cache_record_pdu_t record_pdu,
154                      coap_cache_session_based_t session_based,
155                      unsigned int idle_timeout) {
156   coap_cache_entry_t *entry = coap_malloc_type(COAP_CACHE_ENTRY,
157                                                sizeof(coap_cache_entry_t));
158   if (!entry) {
159     return NULL;
160   }
161 
162   memset(entry, 0, sizeof(coap_cache_entry_t));
163   entry->session = session;
164   if (record_pdu == COAP_CACHE_RECORD_PDU) {
165     entry->pdu = coap_pdu_init(pdu->type, pdu->code, pdu->mid, pdu->alloc_size);
166     if (entry->pdu) {
167       if (!coap_pdu_resize(entry->pdu, pdu->alloc_size)) {
168         coap_delete_pdu(entry->pdu);
169         coap_free_type(COAP_CACHE_ENTRY, entry);
170         return NULL;
171       }
172       /* Need to get the appropriate data across */
173       memcpy(entry->pdu, pdu, offsetof(coap_pdu_t, token));
174       memcpy(entry->pdu->token, pdu->token, pdu->used_size);
175       /* And adjust all the pointers etc. */
176       entry->pdu->data = entry->pdu->token + (pdu->data - pdu->token);
177     }
178   }
179   entry->cache_key = coap_cache_derive_key(session, pdu, session_based);
180   if (!entry->cache_key) {
181     coap_free_type(COAP_CACHE_ENTRY, entry);
182     return NULL;
183   }
184   entry->idle_timeout = idle_timeout;
185   if (idle_timeout > 0) {
186     coap_ticks(&entry->expire_ticks);
187     entry->expire_ticks += idle_timeout * COAP_TICKS_PER_SECOND;
188   }
189 
190   HASH_ADD(hh, session->context->cache, cache_key[0], sizeof(coap_cache_key_t), entry);
191   return entry;
192 }
193 
194 coap_cache_entry_t *
coap_cache_get_by_key(coap_context_t * ctx,const coap_cache_key_t * cache_key)195 coap_cache_get_by_key(coap_context_t *ctx, const coap_cache_key_t *cache_key) {
196   coap_cache_entry_t *cache_entry = NULL;
197 
198   assert(cache_key);
199   if (cache_key) {
200     HASH_FIND(hh, ctx->cache, cache_key, sizeof(coap_cache_key_t), cache_entry);
201   }
202   if (cache_entry && cache_entry->idle_timeout > 0) {
203     coap_ticks(&cache_entry->expire_ticks);
204     cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
205   }
206   return cache_entry;
207 }
208 
209 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)210 coap_cache_get_by_pdu(coap_session_t *session,
211                       const coap_pdu_t *request,
212                       coap_cache_session_based_t session_based) {
213   coap_cache_key_t *cache_key = coap_cache_derive_key(session, request, session_based);
214   coap_cache_entry_t *cache_entry;
215 
216   if (!cache_key)
217     return NULL;
218 
219   cache_entry = coap_cache_get_by_key(session->context, cache_key);
220   coap_delete_cache_key(cache_key);
221   if (cache_entry && cache_entry->idle_timeout > 0) {
222     coap_ticks(&cache_entry->expire_ticks);
223     cache_entry->expire_ticks += cache_entry->idle_timeout * COAP_TICKS_PER_SECOND;
224   }
225   return cache_entry;
226 }
227 
228 void
coap_delete_cache_entry(coap_context_t * ctx,coap_cache_entry_t * cache_entry)229 coap_delete_cache_entry(coap_context_t *ctx, coap_cache_entry_t *cache_entry) {
230 
231   assert(cache_entry);
232 
233   if (cache_entry) {
234     HASH_DELETE(hh, ctx->cache, cache_entry);
235   }
236   if (cache_entry->pdu) {
237     coap_delete_pdu(cache_entry->pdu);
238   }
239   coap_delete_cache_key(cache_entry->cache_key);
240   if (cache_entry->callback && cache_entry->app_data) {
241     cache_entry->callback(cache_entry->app_data);
242   }
243   coap_free_type(COAP_CACHE_ENTRY, cache_entry);
244 }
245 
246 const coap_pdu_t *
coap_cache_get_pdu(const coap_cache_entry_t * cache_entry)247 coap_cache_get_pdu(const coap_cache_entry_t *cache_entry) {
248   return cache_entry->pdu;
249 }
250 
251 void
coap_cache_set_app_data(coap_cache_entry_t * cache_entry,void * data,coap_cache_app_data_free_callback_t callback)252 coap_cache_set_app_data(coap_cache_entry_t *cache_entry,
253                         void *data,
254                         coap_cache_app_data_free_callback_t callback) {
255   cache_entry->app_data = data;
256   cache_entry->callback = callback;
257 }
258 
259 void *
coap_cache_get_app_data(const coap_cache_entry_t * cache_entry)260 coap_cache_get_app_data(const coap_cache_entry_t *cache_entry) {
261   return cache_entry->app_data;
262 }
263 
264 void
coap_expire_cache_entries(coap_context_t * ctx)265 coap_expire_cache_entries(coap_context_t *ctx) {
266   coap_tick_t now;
267   coap_cache_entry_t *cp, *ctmp;
268 
269   coap_ticks(&now);
270   HASH_ITER(hh, ctx->cache, cp, ctmp) {
271     if (cp->idle_timeout > 0) {
272       if (cp->expire_ticks <= now) {
273         coap_delete_cache_entry(ctx, cp);
274       }
275     }
276   }
277 }
278 
279 #endif /* ! COAP_SERVER_SUPPORT */
280