From e69ec148ccfc69b2a1af2bc7f32127e9a3ce4008 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sat, 19 Feb 2022 13:46:07 +0000 Subject: [PATCH] Better mbedtls debug --- docs/README.md | 7 ++++++- mongoose.c | 11 +++++++---- mongoose.h | 1 + src/arch_freertos_lwip.h | 1 + src/tls_mbed.c | 11 +++++++---- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/docs/README.md b/docs/README.md index 3e6e0622..145446e8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -668,7 +668,12 @@ void mg_mgr_wakeup(struct mg_connection *pipe, const void *buf, size_len len); Wake up an event manager that sleeps in `mg_mgr_poll()` call. This function must be called from a separate task/thread. A calling thread can pass -some specific data to the IO thread via `buf`, `len`. Parameters: +some specific data to the IO thread via `buf`, `len`. There is a limitation +on the data size that can be sent: first, it is `MG_IO_MAX` build constant, +and second, it is a maximum UDP datagram size, which is 64KiB. If you need +to send a large data to the Mongoose thread, `malloc()` the data and send +a pointer to it, not the data itself. The receiving event handler can receive +a pointer, send a response, and call `free()`. Parameters: Parameters: - `pipe` - a special connection created by the `mg_mkpipe()` call diff --git a/mongoose.c b/mongoose.c index dfc735cb..e133eef9 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4304,6 +4304,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { int n = (int) send(fd, buf, len, 0); if (n > 0) return n; if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; + MG_DEBUG(("n=%d, errno=%d", n, errno)); return MBEDTLS_ERR_NET_SEND_FAILED; } @@ -4312,6 +4313,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { int n = (int) recv(fd, buf, len, 0); if (n > 0) return n; if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; + MG_DEBUG(("n=%d, errno=%d", n, errno)); return MBEDTLS_ERR_NET_RECV_FAILED; } @@ -4340,10 +4342,8 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) { static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) { n = (int) strlen(s2) - 1; - MG_VERBOSE(("%p %.*s", ((struct mg_connection *) c)->fd, n, s2)); + MG_VERBOSE(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2)); (void) s; - (void) c; - (void) lev; } #if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000 @@ -4378,6 +4378,9 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { mbedtls_x509_crt_init(&tls->cert); mbedtls_pk_init(&tls->pk); mbedtls_ssl_conf_dbg(&tls->conf, debug_cb, c); +#if defined(MG_MBEDTLS_DEBUG_LEVEL) + mbedtls_debug_set_threshold(MG_MBEDTLS_DEBUG_LEVEL); +#endif if ((rc = mbedtls_ssl_config_defaults( &tls->conf, c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, @@ -4402,7 +4405,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { tls->cafile = strdup(opts->ca); rc = mbedtls_ssl_conf_ca_chain_file(&tls->conf, tls->cafile, &tls->crl); if (rc != 0) { - mg_error(c, "parse on-disk chain(%s) err %#x", ca, -rc); + mg_error(c, "parse on-disk chain(%s) err %#x", tls->cafile, -rc); goto fail; } #else diff --git a/mongoose.h b/mongoose.h index d4613166..8f11e426 100644 --- a/mongoose.h +++ b/mongoose.h @@ -189,6 +189,7 @@ static inline void *mg_calloc(int cnt, size_t size) { #define calloc(a, b) mg_calloc((a), (b)) #define free(a) vPortFree(a) #define malloc(a) pvPortMalloc(a) + #define mkdir(a, b) (-1) #ifndef MG_IO_SIZE diff --git a/src/arch_freertos_lwip.h b/src/arch_freertos_lwip.h index 8859622c..8abf453d 100644 --- a/src/arch_freertos_lwip.h +++ b/src/arch_freertos_lwip.h @@ -38,6 +38,7 @@ static inline void *mg_calloc(int cnt, size_t size) { #define calloc(a, b) mg_calloc((a), (b)) #define free(a) vPortFree(a) #define malloc(a) pvPortMalloc(a) + #define mkdir(a, b) (-1) #ifndef MG_IO_SIZE diff --git a/src/tls_mbed.c b/src/tls_mbed.c index 93ed3534..77345536 100644 --- a/src/tls_mbed.c +++ b/src/tls_mbed.c @@ -34,6 +34,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { int n = (int) send(fd, buf, len, 0); if (n > 0) return n; if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; + MG_DEBUG(("n=%d, errno=%d", n, errno)); return MBEDTLS_ERR_NET_SEND_FAILED; } @@ -42,6 +43,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { int n = (int) recv(fd, buf, len, 0); if (n > 0) return n; if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; + MG_DEBUG(("n=%d, errno=%d", n, errno)); return MBEDTLS_ERR_NET_RECV_FAILED; } @@ -70,10 +72,8 @@ static int mbed_rng(void *ctx, unsigned char *buf, size_t len) { static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) { n = (int) strlen(s2) - 1; - MG_VERBOSE(("%p %.*s", ((struct mg_connection *) c)->fd, n, s2)); + MG_VERBOSE(("%lu %d %.*s", ((struct mg_connection *) c)->id, lev, n, s2)); (void) s; - (void) c; - (void) lev; } #if defined(MBEDTLS_VERSION_NUMBER) && MBEDTLS_VERSION_NUMBER >= 0x03000000 @@ -108,6 +108,9 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { mbedtls_x509_crt_init(&tls->cert); mbedtls_pk_init(&tls->pk); mbedtls_ssl_conf_dbg(&tls->conf, debug_cb, c); +#if defined(MG_MBEDTLS_DEBUG_LEVEL) + mbedtls_debug_set_threshold(MG_MBEDTLS_DEBUG_LEVEL); +#endif if ((rc = mbedtls_ssl_config_defaults( &tls->conf, c->is_client ? MBEDTLS_SSL_IS_CLIENT : MBEDTLS_SSL_IS_SERVER, @@ -132,7 +135,7 @@ void mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { tls->cafile = strdup(opts->ca); rc = mbedtls_ssl_conf_ca_chain_file(&tls->conf, tls->cafile, &tls->crl); if (rc != 0) { - mg_error(c, "parse on-disk chain(%s) err %#x", ca, -rc); + mg_error(c, "parse on-disk chain(%s) err %#x", tls->cafile, -rc); goto fail; } #else