COAP_CACHE(3)

πŸ“¦ version 4.3.0, in ubuntu jammy

okay so bestie, sit down, hydrate, and get comfy πŸ«– because this is COAP_CACHE(3) from the libcoap Manual, and it's literally all about working with CoAP cache functions. no cap, it's a lot, but we're gonna get through it together πŸ’–


✨ NAME ✨

coap_cache, coap_cache_derive_key, coap_cache_derive_key_w_ignore, coap_cache_delete_key, coap_cache_ignore_options, coap_new_cache_entry, coap_delete_cache_entry, coap_cache_get_by_key, coap_cache_get_by_pdu β€” the whole girl squad for working with CoAP cache functions πŸ’…


πŸ“š SYNOPSIS πŸ“š

#include <coap3/coap.h>

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);

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);

void coap_delete_cache_key(coap_cache_key_t *cache_key);

int coap_cache_ignore_options(coap_context_t *context, const uint16_t
*options, size_t count);

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);

void coap_delete_cache_entry(coap_context_t *context, coap_cache_entry_t
*cache_entry);

coap_cache_entry_t *coap_cache_get_by_key(coap_context_t *context, const
coap_cache_key_t *cache_key);

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);

const coap_pdu_t *coap_cache_get_pdu(const coap_cache_entry_t
*cache_entry);

void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void
*data, coap_cache_app_data_free_callback_t callback);

void *coap_cache_get_app_data(const coap_cache_entry_t *cache_entry);

and like, for specific (D)TLS library support, you link with -lcoap-3-notls, -lcoap-3-gnutls, -lcoap-3-openssl, -lcoap-3-mbedtls or -lcoap-3-tinydtls. otherwise? you link with -lcoap-3 to get the default (D)TLS library support. pick your fighter ✨


🧠 DESCRIPTION 🧠

the CoAP Cache provides support for two opaque objects that can be used for tracking requests and responses. two. count 'em. 🫢

πŸ—οΈ the first one: Cache Keys

the first is the ability to derive a Cache Key from the cacheable parts of a CoAP PDU, as defined in https://tools.ietf.org/html/rfc7252#section-5.6 updated by https://tools.ietf.org/html/rfc7641#section-2 and https://tools.ietf.org/html/rfc8132#section-2. iykyk πŸ“œ

the Cache Key is a SHA256 digest if libcoap was built with TLS support, otherwise it uses the coap_hash() function β€” using the information abstracted from the PDU and (optionally) the CoAP session. either way, slay.

this Cache Key can then be used to match against incoming PDUs, and then appropriate action logic can take place. main character energy fr.

there's also support for excluding specific CoAP options from the Cache Key. examples? excluding CoAP BLOCK1 and BLOCK2 Options for the client or server, for ease of tracking a large PUT or GET response β€” but NOT excluding these CoAP options in a proxy where it makes sense to cache the individual blocks. context is everything, bestie πŸ’…

πŸ—‚οΈ the second one: Cache Entries

the second is providing Cache Entries (which can be looked up by PDU and hence by Cache Key) which hold additional information to make information tracking simpler. these Cache Entries are automatically deleted when a session closes or a context is deleted. and they're maintained on a hashed list for speed of lookup. efficiency queen πŸ‘‘

πŸ”’ the enums

the following enums are defined:

typedef enum coap_cache_session_based_t {
  COAP_CACHE_NOT_SESSION_BASED,
  COAP_CACHE_IS_SESSION_BASED
} coap_cache_session_based_t;

typedef enum coap_cache_record_pdu_t {
  COAP_CACHE_NOT_RECORD_PDU,
  COAP_CACHE_RECORD_PDU
} coap_cache_record_pdu_t;

πŸ› οΈ the functions, one by one

  • the coap_cache_derive_key() function abstracts all the non NoCacheKey CoAP options, ignores the CoAP Observer option and includes a FETCH body from pdu. if session_based is COAP_CACHE_IS_SESSION_BASED, then the session pointer is also included. CoAP options can be specifically ignored by the use of coap_cache_ignore_options(). a digest is then built from all of the information and returned. NULL is returned on error. 😭
  • the coap_cache_derive_key_w_ignore() function abstracts all the non NoCacheKey CoAP options, ignores the CoAP Observer option and includes a FETCH body from pdu. further options to ignore are specified by the ignore_count of ignore_options. if session_based is COAP_CACHE_IS_SESSION_BASED, then the session pointer is also included. a digest is then built from all of the information and returned. NULL is returned on error.
  • the coap_cache_ignore_options() function is used to store in context a list of count options held in options. the specified options will not be included in the data used for the coap_cache_derive_key() function.
  • the coap_new_cache_entry() function will create a new Cache Entry based on the Cache Key derived from the pdu, session_based and session. if record_pdu is COAP_CACHE_RECORD_PDU, then a copy of the pdu is stored in the Cache Entry for subsequent retrieval. the Cache Entry can also store application specific data (coap_cache_set_app_data() and coap_cache_get_app_data()). idle_timeout in seconds defines the length of time not being used before it gets deleted. if idle_timeout is set to 0, then the Cache Entry will not get idle expired. the created Cache Entry is returned, or NULL on error. ✨
  • the coap_delete_cache_entry() function can be used to delete the Cache Entry cache_entry. this will remove the Cache Entry from the hash lookup list and free off any internally held data. if the Cache Entry is session based, then it will automatically get deleted when the session is freed off or when the idle timeout expires. bye bestie πŸ‘‹
  • the coap_cache_get_by_key() function will locate the Cache Entry held in the context environment that has Cache Key cache_key. returns NULL if the Cache Key was not found.
  • the coap_cache_get_by_pdu() function will locate the Cache Entry held in the context environment that has a Cache Key derived from the pdu and whether session_based or not.
  • the coap_cache_get_pdu() function returns the PDU that was stored with the Cache Entry when it was created with coap_new_cache_entry() and record_pdu was set. if a PDU was not initially stored, NULL is returned.
⚠️ NOTE: a copy of the returned PDU must be taken for using in sending a CoAP packet. do not skip this step, i am begging πŸ™ˆ
  • the coap_cache_set_app_data() function is used to associate data with the cache_entry. if callback is not NULL, it points to a function to free off data when the cache_entry is deleted. if any data has been previously stored in the cache_entry, the pointer to the old data will get overwritten, but the old data will not get freed off. so like, keep track of that, fr fr πŸ’€

πŸ“ž the callback prototype

the callback handler function prototype is defined as:

typedef void (*coap_cache_app_data_free_callback_t)(void *data);

where data is passed into the callback function whenever the Cache Entry is deleted.

  • and the coap_cache_get_app_data() function is used to get the previously stored data in the cache_entry. the retrieval arc 🫢

πŸ” RETURN VALUES πŸ”

  • coap_cache_derive_key() and coap_cache_derive_key_w_ignore() functions return a newly created Cache Key or NULL if there is a creation failure.
  • coap_cache_ignore_options() function returns 1 if success, 0 on failure.
  • coap_new_cache_entry(), coap_cache_get_by_key() and coap_cache_get_by_pdu() functions return the Cache Entry or NULL if there is a failure.
  • coap_cache_get_pdu() function returns the PDU that is held within the Cache Entry or NULL if there is no PDU available.

okay bestie that is NOT the whole page, there is more coming so stay tuned πŸ’–βœ¨πŸŽ€

✨ EXAMPLES ✨

okay bestie, this is the part where it gets real. we're gonna walk through an actual PUT handler that supports BLOCK1 β€” like, chunked uploads but make it CoAP πŸ’…. it shows you how to stash partially-received body data in a cache entry and then glue it all back together. slay.


πŸŽ€ PUT Handler supporting BLOCK1

#include <coap3/coap.h>

static coap_binary_t *example_data_ptr = NULL;
static int example_data_media_type = COAP_MEDIATYPE_TEXT_PLAIN;

static void
cache_free_app_data(void *data) {
  coap_binary_t *bdata = (coap_binary_t*)data;
  coap_delete_binary(bdata);
}

/*
 * Large Data PUT handler
 */

static void
hnd_put_example_data(coap_context_t *ctx,
        coap_resource_t *resource,
        coap_session_t *session,
        coap_pdu_t *request,
        coap_binary_t *token,
        coap_string_t *query,
        coap_pdu_t *response
) {
  size_t size;
  const uint8_t *data;
  coap_opt_iterator_t opt_iter;
  coap_opt_t *option;
  size_t offset;
  size_t total;
  coap_binary_t *data_so_far;

  /* Remove (void) definition if variable is used */
  (void)ctx;
  (void)token;
  (void)query;

  if (coap_get_data_large(request, &size, &data, &offset, &total) &&
    size != total) {
    /*
     * A part of the data has been received (COAP_BLOCK_SINGLE_BODY not set).
     * However, total unfortunately is only an indication, so it is not safe to
     * allocate a block based on total.  As per
     * https://tools.ietf.org/html/rfc7959#section-4
     *   o  In a request carrying a Block1 Option, to indicate the current
     *         estimate the client has of the total size of the resource
     *         representation, measured in bytes ("size indication").
     */
    coap_cache_entry_t *cache_entry = coap_cache_get_by_pdu(session,
                                                            request,
                                              COAP_CACHE_IS_SESSION_BASED);

    if (offset == 0) {
      if (!cache_entry) {
        /*
         * Set idle_timeout parameter to COAP_MAX_TRANSMIT_WAIT if you want
         * early removal on transmission failure. 0 means only delete when
         * the session is deleted as session_based is set here.
         */
        cache_entry = coap_new_cache_entry(session, request,
                                         COAP_CACHE_NOT_RECORD_PDU,
                                         COAP_CACHE_IS_SESSION_BASED, 0);
      }
      else {
        data_so_far = coap_cache_get_app_data(cache_entry);
        if (data_so_far) {
          coap_delete_binary(data_so_far);
          data_so_far = NULL;
        }
        coap_cache_set_app_data(cache_entry, NULL, NULL);
      }
    }
    if (!cache_entry) {
      if (offset == 0) {
        coap_log(LOG_WARNING, "Unable to create a new cache entry\n");
      }
      else {
        coap_log(LOG_WARNING,
                 "No cache entry available for the non-first BLOCK\n");
      }
      coap_pdu_set_code(response, COAP_RESPONSE_CODE_INTERNAL_ERROR);
      return;
    }

    if (size) {
      /* Add in the new data to cache entry */
      data_so_far = coap_cache_get_app_data(cache_entry);
      data_so_far = coap_block_build_body(data_so_far, size, data,
                                          offset, total);
      /* Yes, data_so_far can be NULL if error */
      coap_cache_set_app_data(cache_entry, data_so_far, cache_free_app_data);
    }
    if (offset + size == total) {
      /* All the data is now in */
      data_so_far = coap_cache_get_app_data(cache_entry);
      coap_cache_set_app_data(cache_entry, NULL, NULL);
    }
    else {
      /* Give us the next block response */
      coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTINUE);
      return;
    }
  }
  else {
    /* single body of data received */
    data_so_far = coap_new_binary(size);
    if (data_so_far) {
      memcpy(data_so_far->s, data, size);
    }
  }

  if (example_data_ptr) {
    /* pre-existed response */
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
    coap_delete_binary(example_data_ptr);
  }
  else
    /* just generated response */
    coap_pdu_set_code(response, COAP_RESPONSE_CODE_CREATED);

  example_data_ptr = data_so_far;
  if ((option = coap_check_option(request, COAP_OPTION_CONTENT_FORMAT,
                                  &opt_iter)) != NULL) {
    example_data_media_type =
            coap_decode_var_bytes (coap_opt_value (option),
                                   coap_opt_length (option));
  }
  else {
    example_data_media_type = COAP_MEDIATYPE_TEXT_PLAIN;
  }

  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CHANGED);
  coap_resource_notify_observers(resource, NULL);
}

int main(int argc, char* argv[]){
  coap_context_t *ctx = NULL;  /* Set up as normal */
  /* ... */
  uint16_t cache_ignore_options[] = { COAP_OPTION_BLOCK1,
                                      COAP_OPTION_BLOCK2 };

  /* Remove (void) definition if variable is used */
  (void)argc;
  (void)argv;

  /* ... */

  /** Define the options to ignore when setting up cache-keys */
  coap_cache_ignore_options(ctx, cache_ignore_options,
             sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));

  /* ... */

}

lowkey iconic, right? a couple of little things worth pointing at in there, bestie:

  • the idle_timeout on coap_new_cache_entry() β€” set it to COAP_MAX_TRANSMIT_WAIT if you want that cache entry yeeted early when transmission fails 🫠. 0 means it only gets deleted when the session itself is deleted, because session_based is set right there.
  • in main(), coap_cache_ignore_options() is how you tell the library which options to skip when it's building cache-keys. here it's skipping COAP_OPTION_BLOCK1 and COAP_OPTION_BLOCK2, because duh, those change every single block and would make every key unique πŸ’€.

🌈 SEE ALSO 🌈


πŸ¦‹ FURTHER INFORMATION πŸ¦‹

go read these, they're the whole vibe, no cap:

"RFC7252: The Constrained Application Protocol (CoAP)"

"RFC7959: Block-Wise Transfers in the Constrained Application Protocol (CoAP)"

that's your further information, periodt πŸ“šβœ¨


πŸ’€ BUGS πŸ’€

found a bug? do NOT suffer in silence bestie, please report it πŸ’–:


πŸ‘‘ AUTHORS πŸ‘‘

The libcoap project <libcoap-developers@lists.sourceforge.net>


and that's a wrap on COAP_CACHE(3), bestie 🫢 this page is from coap_cache, version 4.3.0, dated 11/29/2021 β€” go forth and cache responsibly, mwah πŸ’‹πŸŒΈ

πŸ“¦ where this page comes from