coap_cache: Add in Cache Key and Cache Entry support

This updates the initial work done for adding in cache support

include/coap2/coap_cache.h: (new)
include/coap2/coap_cache_internal.h: (new)
src/coap_cache.c: (new)

New files to support cache support.

CMakeLists.txt:
Makefile.amMakefile.am:
Makefile.libcoap:
configure.ac:
examples/lwip/Makefile:
include/coap2/coap.h.in:
include/coap2/coap.h.windows:
win32/libcoap.vcxproj:
win32/libcoap.vcxproj.filters:

Add in the new files.

examples/client.c:

Fix valgrind reported memory leak.

examples/coap-server.c:

Add in new example_data resource that uses the new caching logic.
Update hnd_put() to use the new cache logic for BLOCK1 PUTs.

include/coap2/coap_forward_decls.h:
include/coap2/coap_internal.h:
include/coap2/coap_session.h:
include/coap2/net.h:

Add in new cache support.

libcoap-2.map:
libcoap-2.sym:

Include the new functions.

man/Makefile.am:
man/coap.txt.in:
man/coap_cache.txt.in: (new)

Update documentation.

src/coap_io.c:

Idle timeout cache entries.

src/coap_session.c:

Delete cache entry when session is deleted.

src/net.c:

Delete cache entries when context is freed off.

src/coap_gnutls.c:
src/coap_mbedtls.c:
src/coap_notls.c:
src/coap_openssl.c:
src/coap_tinydtls.c:

Add in SHA256 digest support.
This commit is contained in:
Jon Shallow committed 2020-10-14 10:04:12 -04:00
1 parent 0b142f9eda
commit 1a349d5cb4
32 files changed
+1526 -183

No files matched your search

+1
View File
@@ -37,6 +37,7 @@ extern "C" {
#include "async.h"
#include "bits.h"
#include "block.h"
#include "coap_cache.h"
#include "coap_debug.h"
#include "coap_dtls.h"
#include "coap_event.h"
+143 -42
View File
@@ -8,82 +8,183 @@
/**
* @file coap_cache.h
* @brief Provides a simple request storage for CoAP requests
* @brief Provides a simple cache request storage for CoAP requests
*/
#ifndef COAP_CACHE_H_
#define COAP_CACHE_H_
#include <limits.h>
#include "coap_forward_decls.h"
typedef enum {
COAP_CACHE_INVALID=-1,
COAP_CACHE_MAXKEY=INT_MAX
} coap_cache_key_t;
/**
* Callback to free off the app data when the cache-entry is
* being deleted / freed off.
*
* @param data The app data to be freed off.
*/
typedef void (*coap_cache_app_data_free_callback_t)(void *data);
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;
/**
* Calculates a cache key for the given CoAP PDU. See
* Calculates a cache-key for the given CoAP PDU. See
* https://tools.ietf.org/html/rfc7252#section-5.6
* for an explanation of CoAP cache keys.
*
* @param pdu The CoAP PDU for which a cache key is to be
* calculated.
* Specific CoAP options can be removed from the cache-key. Examples of
* this are the BLOCK1 and BLOCK2 options - which make no real sense including
* them in a client or server environment, but should be included in a proxy
* caching environment where things are cached on a per block basis.
* This is done globally by calling the coap_cache_ignore_options()
* function.
*
* @return The calculcated cache key.
* NOTE: The returned cache-key needs to be freed off by the caller by
* calling coap_cache_delete_key().
*
* @param session The session to add into cache-key if @p session_based
* is set.
* @param pdu The CoAP PDU for which a cache-key is to be
* calculated.
* @param session_based COAP_CACHE_IS_SESSION_BASED if session based
* cache-key, else COAP_CACHE_NOT_SESSION_BASED.
*
* @return The returned cache-key or @c NULL if failure.
*/
coap_cache_key_t coap_cache_key(const coap_pdu_t *pdu);
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);
/**
* Marks a request for permanent storage. The request may be retrieved
* through its cache-key.
* Delete the cache-key.
*
* @param ctx The context to use.
* @param request The request to be stored.
*
* @return The cache key that corresponds to the newly stored
* request or @c COAP_CACHE_INVALID if the request could
* not be stored.
* @param cache_key The cache-key to delete.
*/
coap_cache_key_t coap_cache_mark_request(coap_context_t *ctx,
const coap_pdu_t *request);
void coap_delete_cache_key(coap_cache_key_t *cache_key);
/**
* Clears a mark from a request.
* Define the CoAP options that are not to be included when calculating
* the cache-key. Options that are defined as Non-Cache and the Observe
* option are always ignored.
*
* @param ctx The context to use.
* @param cache_key The cache key for the request which can be
* unmarked.
* @param context The context to save the ignored options information in.
* @param options The array of options to ignore.
* @param count The number of options to ignore. Use 0 to reset the
* options matching.
*
* @return @return @c 1 if successful, else @c 0.
*/
void coap_cache_unmark_request(coap_context_t *ctx,
coap_cache_key_t cache_key);
int coap_cache_ignore_options(coap_context_t *context,
const uint16_t *options, size_t count);
/**
* Searches for a cache entry identified by @p cache_key. This
* function returns the corresponding cache entry or @c NULL
* Create a new cache-entry hash keyed by cache-key derived from the PDU.
*
* If @p session_based is set, then this cache-entry will get deleted when
* the session is freed off.
* If @p record_pdu is set, then the copied PDU will get freed off when
* this cache-entry is deleted.
*
* The cache-entry is maintained on a context hash list.
*
* @param session The session to use to derive the context from.
* @param pdu The pdu to use to generate the cache-key.
* @param record_pdu COAP_CACHE_RECORD_PDU if to take a copy of the PDU for
* later use, else COAP_CACHE_NOT_RECORD_PDU.
* @param session_based COAP_CACHE_IS_SESSION_BASED if to associate this
* cache-entry with the the session (which is embedded
* in the cache-entry), else COAP_CACHE_NOT_SESSION_BASED.
* @param idle_time Idle time in seconds before cache-entry is expired.
* If set to 0, it does not expire (but will get
* deleted if the session is deleted and it is session_based).
*
* @return The returned cache-key or @c NULL if failure.
*/
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);
/**
* Remove a cache-entry from the hash list and free off all the appropriate
* contents apart from app_data.
*
* @param context The context to use.
* @param cache_entry The cache-entry to remove.
*/
void coap_delete_cache_entry(coap_context_t *context,
coap_cache_entry_t *cache_entry);
/**
* Searches for a cache-entry identified by @p cache_key. This
* function returns the corresponding cache-entry or @c NULL
* if not found.
*
* @param ctx The context to use.
* @param cache_key The cache key to look up.
* @param context The context to use.
* @param cache_key The cache-key to get the hashed coap-entry.
*
* @return The cache entry for @p cache_key or @c NULL if not found.
* @return The cache-entry for @p cache_key or @c NULL if not found.
*/
struct coap_cache_entry_t *coap_cache_lookup_key(coap_context_t *ctx,
coap_cache_key_t cache_key);
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 corresponding to @p request. This
* function returns the corresponding cache entry or @c NULL if not
* Searches for a cache-entry corresponding to @p pdu. This
* function returns the corresponding cache-entry or @c NULL if not
* found.
*
* @param ctx The context to use.
* @param request The CoAP request to search for.
* @param session The session to use.
* @param pdu The CoAP request to search for.
* @param session_based COAP_CACHE_IS_SESSION_BASED if session based
* cache-key to be used, else COAP_CACHE_NOT_SESSION_BASED.
*
* @return The cache entry for @p request or @c NULL if not found.
* @return The cache-entry for @p request or @c NULL if not found.
*/
struct coap_cache_entry_t *coap_cache_lookup_request(coap_context_t *ctx,
const coap_pdu_t *request);
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);
/**
* Returns the PDU information stored in the @p coap_cache entry.
*
* @param cache_entry The CoAP cache entry.
*
* @return The PDU information stored in the cache_entry or NULL
* if the PDU was not initially copied.
*/
const coap_pdu_t *coap_cache_get_pdu(const coap_cache_entry_t *cache_entry);
/**
* Stores @p data with the given cache entry. This function
* overwrites any value that has previously been stored with @p
* cache_entry.
*
* @param cache_entry The CoAP cache entry.
* @param data The data pointer to store with wih the cache entry. Note that
* this data must be valid during the lifetime of @p cache_entry.
* @param callback The callback to call to free off this data when the
* cache-entry is deleted, or @c NULL if not required.
*/
void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void *data,
coap_cache_app_data_free_callback_t callback);
/**
* Returns any application-specific data that has been stored with @p
* cache_entry using the function coap_cache_set_app_data(). This function will
* return @c NULL if no data has been stored.
*
* @param cache_entry The CoAP cache entry.
*
* @return The data pointer previously stored or @c NULL if no data stored.
*/
void *coap_cache_get_app_data(const coap_cache_entry_t *cache_entry);
#endif /* COAP_CACHE_H */
+108
View File
@@ -0,0 +1,108 @@
/*
* coap_cache_internal.h -- Cache functions for libcoap
*
* Copyright (C) 2019--2020 Olaf Bergmann <bergmann@tzi.org> and others
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_cache_internal.h
* @brief COAP cache internal information
*/
#ifndef COAP_CACHE_INTERNAL_H_
#define COAP_CACHE_INTERNAL_H_
#include "coap_io.h"
/**
* @defgroup cache Cache Support (Internal)
* API functions for CoAP Caching
* @{
*/
/* Holds a digest in binary typically sha256 except for notls */
typedef struct coap_digest_t {
uint8_t key[32];
} coap_digest_t;
struct coap_cache_key_t {
uint8_t key[32];
};
struct coap_cache_entry_t {
UT_hash_handle hh;
coap_cache_key_t *cache_key;
coap_session_t *session;
coap_pdu_t *pdu;
void* app_data;
coap_tick_t expire_ticks;
unsigned int idle_timeout;
coap_cache_app_data_free_callback_t callback;
};
/**
* Expire coap_cache_entry_t entries
*
* Internal function.
*
* @param context The context holding the coap-entries to exire
*/
void coap_expire_cache_entries(coap_context_t *context);
typedef void coap_digest_ctx_t;
/**
* Initialize a coap_digest
*
* Internal function.
*
* @return The digest context or @c NULL if failure.
*/
coap_digest_ctx_t *coap_digest_setup(void);
/**
* Free off coap_digest_ctx_t. Always done by
* coap_digest_final()
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
*/
void coap_digest_free(coap_digest_ctx_t *digest_ctx);
/**
* Update the coap_digest information with the next chunk of data
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
* @param data Pointer to data.
* @param data_len Number of bytes.
*
* @return @c 1 success, @c 0 failure.
*/
int coap_digest_update(coap_digest_ctx_t *digest_ctx,
const uint8_t *data,
size_t data_len
);
/**
* Finalize the coap_digest information into the provided
* @p digest_buffer.
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
* @param digest_buffer Pointer to digest buffer to update
*
* @return @c 1 success, @c 0 failure.
*/
int coap_digest_final(coap_digest_ctx_t *digest_ctx,
coap_digest_t *digest_buffer);
/** @} */
#endif /* COAP_CACHE_INTERNAL_H_ */
+9 -2
View File
@@ -21,6 +21,8 @@
* so that applications (using coap.h) as well as libcoap builds
* can reference them (and makes .h file dependencies a lot simpler).
*/
struct coap_cache_entry_t;
struct coap_cache_key_t;
struct coap_context_t;
struct coap_dtls_pki_t;
struct coap_endpoint_t;
@@ -28,12 +30,17 @@ struct coap_queue_t;
struct coap_session_t;
struct coap_string_t;
struct coap_subscription_t;
struct coap_cache_entry_t;
/*
* typedef all the structures that are defined in coap_*_internal.h
* typedef all the opaque structures that are defined in coap_*_internal.h
*/
/* ************* coap_cache_internal.h ***************** */
/** Cache Entry information */
typedef struct coap_cache_entry_t coap_cache_entry_t;
typedef struct coap_cache_key_t coap_cache_key_t;
/* ************* coap_session_internal.h ***************** */
/**
+2 -1
View File
@@ -2,7 +2,7 @@
* coap_internal.h -- Structures, Enums & Functions that are not exposed to
* application programming
*
* Copyright (C) 2019 Jon Shallow <supjps-libcoap@jpshallow.com>
* Copyright (C) 2019-2020 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
@@ -46,6 +46,7 @@
#include "coap_mutex.h"
/* Specifically defined internal .h files */
#include "coap_cache_internal.h"
#include "coap_session_internal.h"
#include "coap_subscribe_internal.h"
#include "coap_tcp_internal.h"
+89 -14
View File
@@ -447,29 +447,104 @@ void coap_session_mfree(coap_session_t *session);
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
* RFC 7252, Section 4.8 Default value of ACK_TIMEOUT is 2
*
* Configurable using coap_session_set_ack_timeout()
*/
#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
* RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5
*/
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
* RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5
*
* Configurable using coap_session_set_ack_random_factor()
*/
#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
/**
* Number of message retransmissions before message sending is stopped
* RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4
*/
/**
* Number of message retransmissions before message sending is stopped
* RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4
*
* Configurable using coap_session_set_max_retransmit()
*/
#define COAP_DEFAULT_MAX_RETRANSMIT 4
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
* RFC 7252, Section 4.8 Default value of NSTART is 1
*/
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
* RFC 7252, Section 4.8 Default value of NSTART is 1
*/
#define COAP_DEFAULT_NSTART 1
/**
* The MAX_TRANSMIT_SPAN definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_SPAN
* ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT)) - 1) * ACK_RANDOM_FACTOR
*/
#define COAP_MAX_TRANSMIT_SPAN(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
/**
* The MAX_TRANSMIT_WAIT definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_WAIT
* ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT + 1)) - 1) * ACK_RANDOM_FACTOR
*/
#define COAP_MAX_TRANSMIT_WAIT(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit + 1)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
/**
* The MAX_LATENCY definition.
* RFC 7252, Section 4.8.2 MAX_LATENCY is 100.
*/
#define COAP_MAX_LATENCY 100
/**
* The PROCESSING_DELAY definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of PROCESSING_DELAY
* PROCESSING_DELAY set to ACK_TIMEOUT
*/
#define COAP_PROCESSING_DELAY(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part + 500) \
/ 1000)
/**
* The MAX_RTT definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_RTT
* (2 * MAX_LATENCY) + PROCESSING_DELAY
*/
#define COAP_MAX_RTT(s) \
((2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
/**
* The EXCHANGE_LIFETIME definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of EXCHANGE_LIFETIME
* MAX_TRANSMIT_SPAN + (2 * MAX_LATENCY) + PROCESSING_DELAY
*/
#define COAP_EXCHANGE_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
/**
* The NON_LIFETIME definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of NON_LIFETIME
* MAX_TRANSMIT_SPAN + MAX_LATENCY
*/
#define COAP_NON_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + COAP_MAX_LATENCY)
/** @} */
/**
+2 -2
View File
@@ -62,9 +62,9 @@ typedef enum {
void *coap_malloc_type(coap_memory_tag_t type, size_t size);
/**
* Reallocates a chunk of @p p bytes created by coap_malloc_type() or
* Reallocates a chunk @p p of bytes created by coap_malloc_type() or
* coap_realloc_type() and returns a pointer to the newly allocated memory of
* @p size..
* @p size.
* The @p type is used to select the appropriate storage
* container on constrained devices. The storage allocated by coap_malloc_type()
* coap_realloc_type() must be released with coap_free_type().
+3 -1
View File
@@ -209,7 +209,9 @@ typedef struct coap_context_t {
unsigned int csm_timeout; /**< Timeout for waiting for a CSM from the remote side. 0 means disabled. */
int observe_pending; /**< Observe response pending */
struct coap_cache_entry_t *cache; /**< CoAP message cache */
coap_cache_entry_t *cache; /**< CoAP cache-entry cache */
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 */
void *app; /**< application-specific data */
#ifdef COAP_EPOLL_SUPPORT
int epfd; /**< External FD for epoll */