From 5b8979a0f17ca73bfcbd3de6ab0b1278c634d735 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Wed, 29 Jun 2022 19:19:19 +0100 Subject: [PATCH] Better connect() result test --- mongoose.c | 19 ++++++++++++++----- src/sock.c | 19 ++++++++++++++----- test/unit_test.c | 1 + 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/mongoose.c b/mongoose.c index 4a6fd5a6..8bd317b0 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4166,6 +4166,19 @@ static void close_conn(struct mg_connection *c) { mg_close_conn(c); } +static void connect_conn(struct mg_connection *c) { + union usa usa; + socklen_t n = sizeof(usa); + // Use getpeername() to test whether we have connected + if (getpeername(FD(c), &usa.sa, &n) == 0) { + c->is_connecting = 0; + mg_call(c, MG_EV_CONNECT, NULL); + if (c->is_tls_hs) mg_tls_handshake(c); + } else { + mg_error(c, "socket error"); + } +} + static void setsockopts(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS || \ MG_ARCH == MG_ARCH_TIRTOS @@ -4453,11 +4466,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { } else if (c->is_listening && c->is_udp == 0) { if (c->is_readable) accept_conn(mgr, c); } else if (c->is_connecting) { - if (c->is_readable || c->is_writable) { - c->is_connecting = 0; - mg_call(c, MG_EV_CONNECT, NULL); - if (c->is_tls_hs) mg_tls_handshake(c); - } + if (c->is_readable || c->is_writable) connect_conn(c); } else if (c->is_tls_hs) { if ((c->is_readable || c->is_writable)) mg_tls_handshake(c); } else { diff --git a/src/sock.c b/src/sock.c index 3c814f63..3e3914ee 100644 --- a/src/sock.c +++ b/src/sock.c @@ -313,6 +313,19 @@ static void close_conn(struct mg_connection *c) { mg_close_conn(c); } +static void connect_conn(struct mg_connection *c) { + union usa usa; + socklen_t n = sizeof(usa); + // Use getpeername() to test whether we have connected + if (getpeername(FD(c), &usa.sa, &n) == 0) { + c->is_connecting = 0; + mg_call(c, MG_EV_CONNECT, NULL); + if (c->is_tls_hs) mg_tls_handshake(c); + } else { + mg_error(c, "socket error"); + } +} + static void setsockopts(struct mg_connection *c) { #if MG_ARCH == MG_ARCH_FREERTOS_TCP || MG_ARCH == MG_ARCH_AZURERTOS || \ MG_ARCH == MG_ARCH_TIRTOS @@ -600,11 +613,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) { } else if (c->is_listening && c->is_udp == 0) { if (c->is_readable) accept_conn(mgr, c); } else if (c->is_connecting) { - if (c->is_readable || c->is_writable) { - c->is_connecting = 0; - mg_call(c, MG_EV_CONNECT, NULL); - if (c->is_tls_hs) mg_tls_handshake(c); - } + if (c->is_readable || c->is_writable) connect_conn(c); } else if (c->is_tls_hs) { if ((c->is_readable || c->is_writable)) mg_tls_handshake(c); } else { diff --git a/test/unit_test.c b/test/unit_test.c index ea0c7c5f..7a2cd3af 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -732,6 +732,7 @@ static void test_http_server(void) { int i, errored = 0; mg_connect(&mgr, "tcp://127.0.0.1:55117", eh9, &errored); for (i = 0; i < 10 && errored == 0; i++) mg_mgr_poll(&mgr, 1); + MG_INFO(("errored: %d, expected: 7", errored)); ASSERT(errored == 7); }