From a7f2e298814d313ad53d33507e68322fe28eb316 Mon Sep 17 00:00:00 2001 From: Felipe Gasper Date: Fri, 15 Oct 2021 09:46:21 -0400 Subject: [PATCH] client: connect report actual socket error to user callback rather than EINPROGRESS https://github.com/warmcat/libwebsockets/pull/2452 --- lib/core-net/client/connect3.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/core-net/client/connect3.c b/lib/core-net/client/connect3.c index a1d7fd117..b7523303d 100644 --- a/lib/core-net/client/connect3.c +++ b/lib/core-net/client/connect3.c @@ -71,7 +71,7 @@ typedef enum { } lcccr_t; static lcccr_t -lws_client_connect_check(struct lws *wsi) +lws_client_connect_check(struct lws *wsi, int *real_errno) { int en = 0; #if !defined(WIN32) @@ -98,6 +98,8 @@ lws_client_connect_check(struct lws *wsi) lwsl_wsi_notice(wsi, "getsockopt fd %d says e %d", wsi->desc.sockfd, e); + *real_errno = e; + return LCCCR_FAILED; } @@ -125,7 +127,8 @@ lws_client_connect_check(struct lws *wsi) } #endif - lwsl_wsi_notice(wsi, "connect check take as FAILED: errno %d", en); + lwsl_wsi_notice(wsi, "connect check FAILED: %d", + *real_errno || en); return LCCCR_FAILED; } @@ -231,7 +234,15 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads, /* no dns results and no ongoing timeout for one */ goto connect_to; - switch (lws_client_connect_check(wsi)) { + /* + * If the connection failed, the OS-level errno may be + * something like EINPROGRESS rather than the actual problem + * that prevented a connection. This value will represent the + * “real” problem that we should report to the caller. + */ + int real_errno = 0; + + switch (lws_client_connect_check(wsi, &real_errno)) { case LCCCR_CONNECTED: /* * Oh, it has happened... @@ -240,8 +251,11 @@ lws_client_connect_3_connect(struct lws *wsi, const char *ads, case LCCCR_CONTINUE: return NULL; default: - lws_snprintf(dcce, sizeof(dcce), "conn fail: errno %d", - LWS_ERRNO); + if (!real_errno) + real_errno = LWS_ERRNO; + lws_snprintf(dcce, sizeof(dcce), "conn fail: %d", + real_errno); + cce = dcce; lwsl_wsi_debug(wsi, "%s", dcce); lws_metrics_caliper_report(wsi->cal_conn, METRES_NOGO);