Better mbedtls debug

This commit is contained in:
Sergey Lyubka 2022-02-19 13:46:07 +00:00
parent a76ade9bb2
commit e69ec148cc
5 changed files with 22 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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