Handle MBEDTLS_ERR_NET_CONN_RESET errors

This commit is contained in:
James Hilliard committed 2022-04-04 17:17:17 -06:00
1 parent 0ed64707f3
commit 3ec17c41be
3 files changed
+46 -12

No files matched your search

+23 -6
View File
@@ -3217,6 +3217,16 @@ bool mg_sock_would_block(void) {
; ;
} }
bool mg_sock_conn_reset(void);
bool mg_sock_conn_reset(void) {
int err = MG_SOCK_ERRNO;
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
return err == WSAECONNRESET;
#else
return err == EPIPE || err == ECONNRESET;
#endif
}
static void iolog(struct mg_connection *c, char *buf, long n, bool r) { static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
if (n == 0) { if (n == 0) {
// Do nothing // Do nothing
@@ -4264,15 +4274,19 @@ void mg_tls_free(struct mg_connection *c) {
} }
bool mg_sock_would_block(void); bool mg_sock_would_block(void);
bool mg_sock_conn_reset(void);
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
struct mg_connection *c = (struct mg_connection *) ctx; struct mg_connection *c = (struct mg_connection *) ctx;
int fd = (int) (size_t) c->fd; int fd = (int) (size_t) c->fd;
int n = (int) send(fd, buf, len, 0); int n = (int) send(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n < 0) {
if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE; if (mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED; if (mg_sock_conn_reset()) return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return n;
} }
static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
@@ -4280,9 +4294,12 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
int n, fd = (int) (size_t) c->fd; int n, fd = (int) (size_t) c->fd;
n = (int) recv(fd, buf, len, 0); n = (int) recv(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n < 0) {
if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ; if (mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED; if (mg_sock_conn_reset()) return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return n;
} }
void mg_tls_handshake(struct mg_connection *c) { void mg_tls_handshake(struct mg_connection *c) {
+10
View File
@@ -88,6 +88,16 @@ bool mg_sock_would_block(void) {
; ;
} }
bool mg_sock_conn_reset(void);
bool mg_sock_conn_reset(void) {
int err = MG_SOCK_ERRNO;
#if MG_ARCH == MG_ARCH_WIN32 && MG_ENABLE_WINSOCK
return err == WSAECONNRESET;
#else
return err == EPIPE || err == ECONNRESET;
#endif
}
static void iolog(struct mg_connection *c, char *buf, long n, bool r) { static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
if (n == 0) { if (n == 0) {
// Do nothing // Do nothing
+13 -6
View File
@@ -24,15 +24,19 @@ void mg_tls_free(struct mg_connection *c) {
} }
bool mg_sock_would_block(void); bool mg_sock_would_block(void);
bool mg_sock_conn_reset(void);
static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) { static int mg_net_send(void *ctx, const unsigned char *buf, size_t len) {
struct mg_connection *c = (struct mg_connection *) ctx; struct mg_connection *c = (struct mg_connection *) ctx;
int fd = (int) (size_t) c->fd; int fd = (int) (size_t) c->fd;
int n = (int) send(fd, buf, len, 0); int n = (int) send(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n < 0) {
if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE; if (mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_WRITE;
return MBEDTLS_ERR_NET_SEND_FAILED; if (mg_sock_conn_reset()) return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_SEND_FAILED;
}
return n;
} }
static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) { static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
@@ -40,9 +44,12 @@ static int mg_net_recv(void *ctx, unsigned char *buf, size_t len) {
int n, fd = (int) (size_t) c->fd; int n, fd = (int) (size_t) c->fd;
n = (int) recv(fd, buf, len, 0); n = (int) recv(fd, buf, len, 0);
MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno)); MG_VERBOSE(("%lu n=%d, errno=%d", c->id, n, errno));
if (n > 0) return n; if (n < 0) {
if (n < 0 && mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ; if (mg_sock_would_block()) return MBEDTLS_ERR_SSL_WANT_READ;
return MBEDTLS_ERR_NET_RECV_FAILED; if (mg_sock_conn_reset()) return MBEDTLS_ERR_NET_CONN_RESET;
return MBEDTLS_ERR_NET_RECV_FAILED;
}
return n;
} }
void mg_tls_handshake(struct mg_connection *c) { void mg_tls_handshake(struct mg_connection *c) {