mirror of
https://github.com/obgm/libcoap.git
synced 2026-12-05 20:02:19 +03:00
For limited stacks, remove large items off the stack and make them static
When there is a limited stack size, having large data items on the stack can easily cause stack overflows. This makes integration with any constrained stack O/S a lot easier. include/coap2/coap_mutex.h: Create coap_ mutex functions which are platform independent to protect the (now) static variables if compiled with "-DCOAP_CONSTRAINED_STACK=1" Makefile.am: Include include/coap2/coap_mutex.h for distribution coap_config.h.contiki: coap_config.h.lwip: As CONTIKI and LWIP have constrained stacks, set "#define COAP_CONSTRAINED_STACK 1" src/coap_debug.c: src/coap_io.c: src/net.c: coap_show_pdu() coap_log_impl() coap_run_once() coap_read_session() coap_read_endpoint() Move large data items out of the functions' stack and make them static variables (defined within the function) if COAP_CONSTRAINED_STACK is defined. Protect the usage of these static variables by mutexes. Note: if "-DCOAP_CONSTRAINED_STACK=0" is used, then the stack will not be constrained.
This commit is contained in:
1 parent
302bbffebd
commit
cb18375df7
7 files changed
+138
-53
No files matched your search
+29
-6
@@ -37,6 +37,7 @@
|
||||
#include "coap_debug.h"
|
||||
#include "encode.h"
|
||||
#include "net.h"
|
||||
#include "coap_mutex.h"
|
||||
|
||||
#ifdef WITH_LWIP
|
||||
# define fprintf(fd, ...) LWIP_PLATFORM_DIAG((__VA_ARGS__))
|
||||
@@ -459,7 +460,14 @@ is_binary(int content_format) {
|
||||
|
||||
void
|
||||
coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
static coap_mutex_t static_show_pdu_mutex = COAP_MUTEX_INITIALIZER;
|
||||
static unsigned char buf[1024]; /* need some space for output creation */
|
||||
static char outbuf[COAP_DEBUG_BUF_SIZE];
|
||||
#else /* ! COAP_CONSTRAINED_STACK */
|
||||
unsigned char buf[1024]; /* need some space for output creation */
|
||||
char outbuf[COAP_DEBUG_BUF_SIZE];
|
||||
#endif /* ! COAP_CONSTRAINED_STACK */
|
||||
size_t buf_len = 0; /* takes the number of bytes written to buf */
|
||||
int encode = 0, have_options = 0, i;
|
||||
coap_opt_iterator_t opt_iter;
|
||||
@@ -467,13 +475,16 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
|
||||
int content_format = -1;
|
||||
size_t data_len;
|
||||
unsigned char *data;
|
||||
char outbuf[COAP_DEBUG_BUF_SIZE];
|
||||
int outbuflen = 0;
|
||||
|
||||
/* Save time if not needed */
|
||||
if (level > coap_get_log_level())
|
||||
return;
|
||||
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
coap_mutex_lock(&static_show_pdu_mutex);
|
||||
#endif /* COAP_CONSTRAINED_STACK */
|
||||
|
||||
snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {",
|
||||
COAP_DEFAULT_VERSION, msg_type_string(pdu->type),
|
||||
msg_code_string(pdu->code), pdu->tid);
|
||||
@@ -646,6 +657,10 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
|
||||
outbuflen = strlen(outbuf);
|
||||
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n");
|
||||
COAP_DO_SHOW_OUTPUT_LINE;
|
||||
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
coap_mutex_unlock(&static_show_pdu_mutex);
|
||||
#endif /* COAP_CONSTRAINED_STACK */
|
||||
}
|
||||
|
||||
void coap_show_tls_version(coap_log_t level)
|
||||
@@ -750,16 +765,24 @@ coap_log_impl(coap_log_t level, const char *format, ...) {
|
||||
return;
|
||||
|
||||
if (log_handler) {
|
||||
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
|
||||
char message[128];
|
||||
#else
|
||||
char message[8 + 1024 * 2]; /* O/H + Max packet payload size * 2 */
|
||||
#endif
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
static coap_mutex_t static_log_mutex = COAP_MUTEX_INITIALIZER;
|
||||
static char message[COAP_DEBUG_BUF_SIZE];
|
||||
#else /* ! COAP_CONSTRAINED_STACK */
|
||||
char message[COAP_DEBUG_BUF_SIZE];
|
||||
#endif /* ! COAP_CONSTRAINED_STACK */
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
coap_mutex_lock(&static_log_mutex);
|
||||
#endif /* COAP_CONSTRAINED_STACK */
|
||||
|
||||
vsnprintf( message, sizeof(message), format, ap);
|
||||
va_end(ap);
|
||||
log_handler(level, message);
|
||||
#if COAP_CONSTRAINED_STACK
|
||||
coap_mutex_unlock(&static_log_mutex);
|
||||
#endif /* COAP_CONSTRAINED_STACK */
|
||||
} else {
|
||||
char timebuf[32];
|
||||
coap_tick_t now;
|
||||
|
||||
Reference in new issue
Block a user