From 0fbc4b8ff4fd7c36a1b566109636ca70754eeb3b Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Mon, 21 Mar 2022 14:39:27 +0000 Subject: [PATCH] Reuse sock.c logic in mbedtls integration --- mongoose.c | 12 +++++------- src/sock.c | 3 ++- src/tls_mbed.c | 9 +++------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/mongoose.c b/mongoose.c index bd4a45ff..4d835191 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3238,7 +3238,8 @@ static void tomgaddr(union usa *usa, struct mg_addr *a, bool is_ip6) { #endif } -static bool mg_sock_would_block(void) { +bool mg_sock_would_block(void); +bool mg_sock_would_block(void) { int err = MG_SOCK_ERRNO; return err == EINPROGRESS || err == EWOULDBLOCK #ifndef WINCE @@ -4307,10 +4308,7 @@ void mg_tls_free(struct mg_connection *c) { } } -static bool mg_wouldblock(int n) { - return n < 0 && - (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK); -} +bool mg_sock_would_block(void); static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { struct mg_connection *c = (struct mg_connection *) ctx; @@ -4318,7 +4316,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { int n = (int) send(fd, buf, len, 0); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); if (n > 0) return n; - if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; + if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE; return MBEDTLS_ERR_NET_SEND_FAILED; } @@ -4328,7 +4326,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { n = (int) recv(fd, buf, len, 0); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); if (n > 0) return n; - if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; + if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ; return MBEDTLS_ERR_NET_RECV_FAILED; } diff --git a/src/sock.c b/src/sock.c index 2db311d4..99af5ed4 100644 --- a/src/sock.c +++ b/src/sock.c @@ -75,7 +75,8 @@ static void tomgaddr(union usa *usa, struct mg_addr *a, bool is_ip6) { #endif } -static bool mg_sock_would_block(void) { +bool mg_sock_would_block(void); +bool mg_sock_would_block(void) { int err = MG_SOCK_ERRNO; return err == EINPROGRESS || err == EWOULDBLOCK #ifndef WINCE diff --git a/src/tls_mbed.c b/src/tls_mbed.c index f4ba6155..5ff27347 100644 --- a/src/tls_mbed.c +++ b/src/tls_mbed.c @@ -24,10 +24,7 @@ void mg_tls_free(struct mg_connection *c) { } } -static bool mg_wouldblock(int n) { - return n < 0 && - (errno == EINPROGRESS || errno == EAGAIN || errno == EWOULDBLOCK); -} +bool mg_sock_would_block(void); static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { struct mg_connection *c = (struct mg_connection *) ctx; @@ -35,7 +32,7 @@ static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { int n = (int) send(fd, buf, len, 0); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); if (n > 0) return n; - if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_WRITE; + if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE; return MBEDTLS_ERR_NET_SEND_FAILED; } @@ -45,7 +42,7 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { n = (int) recv(fd, buf, len, 0); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); if (n > 0) return n; - if (mg_wouldblock(n)) return MBEDTLS_ERR_SSL_WANT_READ; + if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ; return MBEDTLS_ERR_NET_RECV_FAILED; }