Skip to contentSkip to conversation

wispr: Complete Every Online Check, So Continuous Mode Never Stalls

PATCH New Grant Erickson 1 participant 6 messages Nov 12

lore
Share & tools
lore
#0 lore raw

cover wispr: Complete Every Online Check, So Continuous Mode Never Stalls

Online checks passes only on HTTP 200 with an 'X-ConnMan-Status' (see related https://patchwork.kernel.org/series/1122950/)
header. Many other outcomes never complete the check at all:

- A status the code doesn't name (204, 403, 5xx, 511)
- A 200 without the 'X-ConnMan-Status' header
- A redirect
- A 505
- A response that stops part way

The check stays active and incomplete, with no log line. Later starts return '-EALREADY', so nothing re-checks until the default service, the link, or an address changes. In the wild, systems stay 'ready' after a router outage or pause or stay 'online' behind a router that starts blocking them.

Each failure is logged once, with its reason, and then retried with the configured backoff:

Interface wlan0 [ wifi ] IPv4 online check to http://…/api/v2/status failed: unexpected HTTP status 403

After 'OnlineCheckFailuresThreshold' failures, the service fails over as designed. No configuration settings change.

Grant Erickson (5):
gweb: Report transport creation errors accurately
wispr: Fail checks ending in an unhandled response
wispr: Fail checks before requesting a browser
gweb: Add a receive inactivity timeout
wispr: Bound online check response gaps

 doc/connman.conf.5.in |  16 ++-
 gweb/gweb.c           | 240 +++++++++++++++++++++++++++++++++++++++---
 gweb/gweb.h           |   3 +
 src/main.conf         |  16 ++-
 src/wispr.c           | 169 ++++++++++++++++++++++++++---
 5 files changed, 401 insertions(+), 43 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH 1/5 gweb: Report transport creation errors accurately

When the transport for a web session cannot be created, handle_resolved_address passes the negated POSIX error returned by create_transport directly to g_io_error_from_errno and g_strerror. Both expect a positive error, so every such failure was reported as G_IO_ERROR_FAILED with a message of "Unknown error -5".

In addition, connect_session_transport collapsed every socket(), bind and connect() failure into -EIO, discarding the actual error. A connect() that fails immediately because, for example, there is no route to the peer (ENETUNREACH) or a policy rule prohibits it (EACCES) was indistinguishable from any other failure.

Return the negated errno from each of those failure points and negate it again before passing it to g_io_error_from_errno and g_strerror, so that a failed online check is reported with, for example, "Network is unreachable" rather than "Unknown error -5".

---
 gweb/gweb.c | 39 ++++++++++++++++++++++++++-------------
 1 file changed, 26 insertions(+), 13 deletions(-)
gweb/gweb.c +26 -13
index 0a87da648a9c..e3a2d2eecb7e 100644
@@ -1587,12 +1587,15 @@ static inline int bind_socket(int sk, int index, int family)
  *                                      network stack.
  *
  *  @retval  0        If successful.
- *  @retval  -EIO     If a socket could not be created for the specified
- *                    session address family, the socket could not be
- *                    bound to the specified session network
- *                    interface, or the socket could not connect to
- *                    the specified session peer address.
  *  @retval  -ENOMEM  If a GLib transport channel could not be created.
+ *  @retval  -errno   The negated POSIX error from socket(), from
+ *                    binding the socket to the specified session
+ *                    network interface, or from connect(), if a
+ *                    socket could not be created for the specified
+ *                    session address family, could not be bound to
+ *                    the specified session network interface, or
+ *                    could not start connecting to the specified
+ *                    session peer address, respectively.
  *
  *  @sa close_session_transport
  *
@@ -1602,18 +1605,23 @@ static int connect_session_transport(struct web_session *session,
 {
 	GIOFlags flags;
 	int sk;
+	int err;
 
 	sk = socket(session->addr->ai_family, SOCK_STREAM | SOCK_CLOEXEC,
 			IPPROTO_TCP);
-	if (sk < 0)
-		return -EIO;
+	if (sk < 0) {
+		err = -errno;
+		debug(session->web, "socket() %s", strerror(-err));
+		return err;
+	}
 
 	if (session->web->index > 0) {
 		if (bind_socket(sk, session->web->index,
 					session->addr->ai_family) < 0) {
-			debug(session->web, "bind() %s", strerror(errno));
+			err = -errno;
+			debug(session->web, "bind() %s", strerror(-err));
 			close(sk);
-			return -EIO;
+			return err;
 		}
 	}
 
@@ -1643,8 +1651,9 @@ static int connect_session_transport(struct web_session *session,
 	if (connect(sk, session->addr->ai_addr,
 			session->addr->ai_addrlen) < 0) {
 		if (errno != EINPROGRESS) {
-			debug(session->web, "connect() %s", strerror(errno));
-			return -EIO;
+			err = -errno;
+			debug(session->web, "connect() %s", strerror(-err));
+			return err;
 		}
 	}
 
@@ -2530,11 +2539,15 @@ static void handle_resolved_address(struct web_session *session)
 
 	call_route_func(session);
 
+	/*
+	 * create_transport returns a negated POSIX error; both
+	 * g_io_error_from_errno and g_strerror require a positive one.
+	 */
 	ret = create_transport(session);
 	if (ret < 0) {
 		local_error = g_error_new_literal(G_IO_ERROR,
-						g_io_error_from_errno(ret),
-						g_strerror(ret));
+						g_io_error_from_errno(-ret),
+						g_strerror(-ret));
 
 		call_result_func_failure(session, local_error);
 	}
Signature · 1 line
2.52.0
#2 lore raw

PATCH 2/5 wispr: Fail checks ending in an unhandled response

An online check completes only through its callback, which reports success or failure to the service. Several outcomes of wispr_portal_web_result_success never invoke it:

* Any HTTP status that the switch does not name, such as 204, 403, 5xx, or 511, falls to the default case, which does nothing.

* A 200 carrying a WISPr message that wispr_manage_message did not act on breaks out of the switch without completing.

* A redirect whose follow-on request cannot be started leaves request_id at zero with no result callback to come. Builds without TLS support never follow redirects, so this arises only with TLS support.

In each case, the check never completes. Because the check is still active, later attempts to start one return -EALREADY. In continuous mode, no further check is scheduled until the default service, the link, or an address changes. A service that is "ready" stays "ready" and one that is "online" stays "online", regardless of whether the online check endpoint becomes reachable, or stops being reachable.

This happens in practice: a home router that pauses or blocks a device commonly answers the online check URL itself with a block page or an error status.

Complete each of these as a failure, with a message that names the reason (for example, "unexpected HTTP status 403"), so that the failure is logged and counted, and continuous mode reschedules the next check with its configured backoff.

---
 src/wispr.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
src/wispr.c +47 -1
index a40cf353b54e..47eb57f1880e 100644
@@ -1065,6 +1065,7 @@ static void wispr_portal_web_result_success(GWebResult *result,
 	guint16 status;
 	const char *str = NULL;
 	const char *redirect = NULL;
+	g_autofree char *message = NULL;
 
 	status = g_web_result_get_status(result);
 
@@ -1078,8 +1079,30 @@ static void wispr_portal_web_result_success(GWebResult *result,
 				wp_context->status_url, wp_context);
 		break;
 	case GWEB_HTTP_STATUS_CODE_OK:
-		if (wp_context->wispr_msg.message_type >= 0)
+		/*
+		 * A WISPr message type of zero or greater means the
+		 * response body carried a WISPr XML message. The type
+		 * starts at -1 (connman_wispr_message_init), is set only
+		 * when the body is parsed, and is reset to -1 after each
+		 * result, so here it describes this response alone.
+		 *
+		 * wispr_portal_web_result hands such a message to
+		 * wispr_manage_message first and skips this handler if
+		 * that acted on it: by requesting login credentials from
+		 * the agent (type 100), or by re-checking after a
+		 * successful login (type 120 or 140 with response code
+		 * 50). Reaching here with a type of zero or greater
+		 * therefore means it did not act: the type is one it does
+		 * not handle, the login request could not be made (for
+		 * example, with no agent registered), or the login was
+		 * rejected. Nothing else will complete this check, so
+		 * complete it as a failure.
+		 */
+		if (wp_context->wispr_msg.message_type >= 0) {
+			portal_manage_failure_status(wp_context, 0,
+				"unhandled WISPr message");
 			break;
+		}
 
 		if (g_web_result_get_header(result, "X-ConnMan-Status",
 						&str)) {
@@ -1118,6 +1141,18 @@ static void wispr_portal_web_result_success(GWebResult *result,
 				redirect, wispr_portal_web_result,
 				wispr_route_request, wp_context, NULL);
 
+		/*
+		 * If the redirect request could not even be started, there
+		 * will be no result callback to complete this check, so
+		 * complete it here as a failure.
+		 */
+		if (wp_context->request_id == 0) {
+			portal_manage_failure_status(wp_context, 0,
+				"could not follow redirect");
+			wispr_portal_context_unref(wp_context);
+			break;
+		}
+
 		return;
 	case GWEB_HTTP_STATUS_CODE_BAD_REQUEST:
 		portal_manage_failure_status(wp_context, 0, "bad request");
@@ -1138,6 +1173,17 @@ static void wispr_portal_web_result_success(GWebResult *result,
 				wp_context->status_url, wp_context);
 		break;
 	default:
+		/*
+		 * Any other status (for example, 204, 403, 5xx, or 511 from
+		 * a router or a block page answering on behalf of the
+		 * online check endpoint) cannot indicate success. Complete
+		 * the check as a failure; otherwise, it would never
+		 * complete and, in continuous mode, no further check would
+		 * be scheduled.
+		 */
+		message = g_strdup_printf("unexpected HTTP status %03u",
+					status);
+		portal_manage_failure_status(wp_context, 0, message);
 		break;
 	}
 
Signature · 1 line
2.52.0
#3 lore raw

PATCH 3/5 wispr: Fail checks before requesting a browser

When an online check response suggests a captive portal, namely a 200 without the "X-ConnMan-Status" header, a redirect that cannot be followed, a 505, or no status at all, wispr asks the agent to open a browser and leaves the check waiting on the agent. None of the outcomes completes the check:

* With no agent registered, __connman_agent_request_browser returns -ESRCH. The return value is ignored and the reference taken for the request leaks.

* If the agent replies with an error or the request times out, wispr_portal_browser_reply_cb releases the context without reporting a result.

* If the agent replies with success, the reply callback restarts the check with __connman_service_wispr_start, which returns -EALREADY because the stalled check is still active.

So, as with an unhandled status, the check never completes and, in continuous mode, no further check is scheduled. On a headless device without an agent, any router block page that answers 200 without the header, or redirects, leaves the service "ready" or "online" indefinitely.

Factor these branches into wispr_portal_request_browser, which requests the browser and then completes the check as a failure, with a message naming the reason, so that the check is logged and counted and its successor scheduled whether or not, or whenever, the agent answers. Release the request reference if the request cannot be made.

Because checks now continue past a portal, request the browser at most once per network interface until a check on it succeeds or the interface's WISPr state is torn down. This keeps the agent-visible behavior as it was, one browser request per portal encounter, rather than one per rescheduled check. If the user completes the portal login, either the reply callback's restart or the next scheduled check observes the success.

---
 src/wispr.c | 112 ++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 96 insertions(+), 16 deletions(-)
src/wispr.c +96 -16
index 47eb57f1880e..013afe241054 100644
@@ -131,6 +131,7 @@ struct connman_wispr_portal_context {
 struct connman_wispr_portal {
 	struct connman_wispr_portal_context *ipv4_context;
 	struct connman_wispr_portal_context *ipv6_context;
+	bool browser_requested;
 };
 
 static bool wispr_portal_web_result(const GError *error, GWebResult *result, gpointer user_data);
@@ -724,6 +725,13 @@ static void portal_manage_success_status(GWebResult *result,
 				&str))
 		connman_info("Client-Timezone: %s", str);
 
+	/*
+	 * The network is reachable, so allow a browser request again
+	 * should a later check on this interface find a portal.
+	 */
+	if (wp_context->wispr_portal)
+		wp_context->wispr_portal->browser_requested = false;
+
 	wp_context->cb(service, type, true, 0, NULL);
 }
 
@@ -897,6 +905,77 @@ static void wispr_portal_browser_reply_cb(struct connman_service *service,
 	wispr_portal_context_unref(wp_context);
 }
 
+/**
+ *  @brief
+ *    Complete an "online" HTTP-based Internet reachability check as a
+ *    failure and request that the agent open a browser.
+ *
+ *  This handles a reachability check response that suggests a
+ *  captive portal requiring user interaction, such as a 200 "OK"
+ *  status without the "X-ConnMan-Status" header or a redirect that
+ *  cannot be followed.
+ *
+ *  If no browser has yet been requested for the network interface
+ *  associated with @a wp_context since its last successful check,
+ *  the agent, if any, is asked to open one at @a url. Regardless,
+ *  the check is then completed as a failure with @a message, so that
+ *  it is logged and counted and, in continuous mode, the next check
+ *  is scheduled whether or not, or whenever, the agent answers.
+ *
+ *  Requesting the browser at most once per interface until a check
+ *  succeeds preserves the behavior from before checks continued
+ *  past a portal: one request per portal encounter, not one per
+ *  rescheduled check.
+ *
+ *  @param[in,out]  wp_context  A pointer to the mutable WISPr portal
+ *                              detection context associated with the
+ *                              "online" HTTP-based Internet
+ *                              reachability check this is
+ *                              completing.
+ *  @param[in]      url         An optional pointer to an immutable
+ *                              null-terminated C string of the URL
+ *                              for the agent to open.
+ *  @param[in]      message     A pointer to an immutable
+ *                              null-terminated C string describing
+ *                              why the check failed.
+ *
+ *  @sa portal_manage_failure_status
+ *  @sa wispr_portal_browser_reply_cb
+ *  @sa wispr_portal_web_result_success
+ *
+ *  @private
+ *
+ */
+static void wispr_portal_request_browser(
+			struct connman_wispr_portal_context *wp_context,
+			const char *url,
+			const char *message)
+{
+	struct connman_wispr_portal *wispr_portal = wp_context->wispr_portal;
+	int err;
+
+	if (wispr_portal && !wispr_portal->browser_requested) {
+		/*
+		 * Retain a reference to the WISPr/portal context for the
+		 * browser request. It is released by the reply callback
+		 * or, if the request could not be made, here.
+		 */
+		wispr_portal_context_ref(wp_context);
+
+		err = __connman_agent_request_browser(wp_context->service,
+				wispr_portal_browser_reply_cb,
+				url, wp_context);
+		if (err == -EINPROGRESS)
+			wispr_portal->browser_requested = true;
+		else {
+			DBG("browser request failed: %d", err);
+			wispr_portal_context_unref(wp_context);
+		}
+	}
+
+	portal_manage_failure_status(wp_context, 0, message);
+}
+
 static void wispr_portal_request_wispr_login(struct connman_service *service,
 				bool success,
 				const char *ssid, int ssid_len,
@@ -1073,10 +1152,9 @@ static void wispr_portal_web_result_success(GWebResult *result,
 
 	switch (status) {
 	case GWEB_HTTP_STATUS_CODE_UNKNOWN:
-		wispr_portal_context_ref(wp_context);
-		__connman_agent_request_browser(wp_context->service,
-				wispr_portal_browser_reply_cb,
-				wp_context->status_url, wp_context);
+		wispr_portal_request_browser(wp_context,
+				wp_context->status_url,
+				"no HTTP status");
 		break;
 	case GWEB_HTTP_STATUS_CODE_OK:
 		/*
@@ -1108,10 +1186,9 @@ static void wispr_portal_web_result_success(GWebResult *result,
 						&str)) {
 			portal_manage_success_status(result, wp_context);
 		} else {
-			wispr_portal_context_ref(wp_context);
-			__connman_agent_request_browser(wp_context->service,
-					wispr_portal_browser_reply_cb,
-					wp_context->redirect_url, wp_context);
+			wispr_portal_request_browser(wp_context,
+					wp_context->redirect_url,
+					"missing X-ConnMan-Status header");
 		}
 
 		break;
@@ -1124,11 +1201,15 @@ static void wispr_portal_web_result_success(GWebResult *result,
 		if (!g_web_supports_tls() ||
 			!g_web_result_get_header(result, "Location",
 							&redirect)) {
+			message = g_strdup_printf("HTTP status %03u redirect "
+					"not followed (%s)", status,
+					!g_web_supports_tls() ?
+						"no TLS support" :
+						"no Location header");
 
-			wispr_portal_context_ref(wp_context);
-			__connman_agent_request_browser(wp_context->service,
-					wispr_portal_browser_reply_cb,
-					wp_context->status_url, wp_context);
+			wispr_portal_request_browser(wp_context,
+					wp_context->status_url,
+					message);
 			break;
 		}
 
@@ -1167,10 +1248,9 @@ static void wispr_portal_web_result_success(GWebResult *result,
 		break;
 
 	case GWEB_HTTP_STATUS_CODE_HTTP_VERSION_NOT_SUPPORTED:
-		wispr_portal_context_ref(wp_context);
-		__connman_agent_request_browser(wp_context->service,
-				wispr_portal_browser_reply_cb,
-				wp_context->status_url, wp_context);
+		wispr_portal_request_browser(wp_context,
+				wp_context->status_url,
+				"HTTP version not supported");
 		break;
 	default:
 		/*
Signature · 1 line
2.52.0
#4 lore raw

PATCH 4/5 gweb: Add a receive inactivity timeout

The connect timeout set with g_web_set_connect_timeout runs from the start of the connection until the first data is received from the peer, at which point received_data cancels it. Nothing bounds the request after that. A peer that sends part of its response, for example its headers and some of a body shorter than its Content-Length, and then nothing more, without closing the connection, leaves the request waiting indefinitely, and its result callback is never invoked.

Add a receive inactivity timeout, set with g_web_set_inactivity_timeout. It is started each time received data is processed and more is expected, and cancelled when the next data or condition arrives, when the request ends, or when the session is freed. If it expires, the transport is closed and the request fails with G_IO_ERROR_TIMED_OUT and the message "no data from <address> for <timeout>ms".

The timeout bounds the time between successive receipts of data, not the total time of the request, so a slow peer that continues to make progress is not failed. This mirrors how TCP itself times out on a lack of progress rather than on total elapsed time. A value of zero, the default, disables it, so existing users are unaffected until they opt in.

---
 gweb/gweb.c | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 gweb/gweb.h |   3 +
 2 files changed, 201 insertions(+), 3 deletions(-)
gweb/gweb.c +198 -3
index e3a2d2eecb7e..681ad3694bf9 100644
@@ -110,6 +110,7 @@ struct web_session {
 
 	GIOChannel *transport_channel;
 	guint connect_timeout;
+	guint inactivity_timeout;
 	guint transport_watch;
 	guint send_watch;
 
@@ -160,6 +161,7 @@ struct _GWeb {
 	char *http_version;
 	bool close_connection;
 	guint connect_timeout_ms;
+	guint inactivity_timeout_ms;
 
 	GWebDebugFunc debug_func;
 	gpointer debug_data;
@@ -413,6 +415,112 @@ static void cancel_connect_timeout(struct web_session *session)
 	}
 }
 
+/**
+ *  @brief
+ *    Handle a receive inactivity timeout.
+ *
+ *  This callback handles a receive inactivity timeout for a GWeb
+ *  session: the peer, having sent some of its response, sent nothing
+ *  more for the inactivity timeout period. The session transport is
+ *  closed and the GWeb transaction is terminated with a
+ *  G_IO_ERROR_TIMED_OUT error.
+ *
+ *  param[in,out]  user_data  A pointer to the mutable GWeb session
+ *                            for which the receive inactivity timeout
+ *                            expired.
+ *
+ *  @returns
+ *    G_SOURCE_REMOVE (that is, FALSE) unconditionally, indicating
+ *    that the timeout source that triggered this callback should be
+ *    removed on callback completion.
+ *
+ *  @sa add_inactivity_timeout
+ *  @sa cancel_inactivity_timeout
+ *
+ */
+static gboolean inactivity_timeout_cb(gpointer user_data)
+{
+	struct web_session *session = user_data;
+	g_autofree char *message = NULL;
+	g_autoptr(GError) local_error = NULL;
+
+	message = g_strdup_printf("no data from %s for %ums",
+		session->address, g_web_get_inactivity_timeout(session->web));
+
+	debug(session->web, "session %p %s",
+			session, message);
+
+	session->inactivity_timeout = 0;
+
+	close_session_transport(session);
+
+	session->result.buffer = NULL;
+	session->result.length = 0;
+
+	local_error = g_error_new_literal(G_IO_ERROR,
+					G_IO_ERROR_TIMED_OUT,
+					message);
+
+	call_result_func_failure(session, local_error);
+
+	return G_SOURCE_REMOVE;
+}
+
+/**
+ *  @brief
+ *    Add a receive inactivity timeout.
+ *
+ *  This attempts to add a receive inactivity timeout and callback to
+ *  the specified GWeb session. The timeout is successfully added if
+ *  @a session is non-null and @a timeout_ms is greater than zero.
+ *
+ *  @param[in,out]  session     A pointer to the mutable GWeb session
+ *                              to add the receive inactivity timeout
+ *                              to.
+ *  @param[in]      timeout_ms  The time, in milliseconds, for the
+ *                              receive inactivity timeout. If zero
+ *                              ('0'), no timeout is added.
+ *
+ *  @sa cancel_inactivity_timeout
+ *  @sa inactivity_timeout_cb
+ *
+ */
+static void add_inactivity_timeout(struct web_session *session,
+						guint timeout_ms)
+{
+	if (!session || !timeout_ms)
+		return;
+
+	session->inactivity_timeout = g_timeout_add(timeout_ms,
+				inactivity_timeout_cb, session);
+}
+
+/**
+ *  @brief
+ *    Cancel a receive inactivity timeout.
+ *
+ *  This attempts to cancel a receive inactivity timeout, if any, for
+ *  the specified GWeb session.
+ *
+ *  @param[in,out]  session  A pointer to the mutable GWeb session
+ *                           for which to cancel the receive
+ *                           inactivity timeout.
+ *
+ *  @sa add_inactivity_timeout
+ *  @sa inactivity_timeout_cb
+ *
+ */
+static void cancel_inactivity_timeout(struct web_session *session)
+{
+	if (!session)
+		return;
+
+	if (session->inactivity_timeout > 0) {
+		g_source_remove(session->inactivity_timeout);
+		session->inactivity_timeout = 0;
+	}
+}
+
 /**
  *  @brief
  *    Close the TCP transport a GWeb sesssion.
@@ -472,6 +580,8 @@ static void free_session(struct web_session *session)
 
 	cancel_connect_timeout(session);
 
+	cancel_inactivity_timeout(session);
+
 	close_session_transport(session);
 
 	g_free(session->result.last_key);
@@ -820,6 +930,78 @@ done:
 	return timeout_ms;
 }
 
+/**
+ *  @brief
+ *    Set the receive inactivity timeout.
+ *
+ *  This sets the receive inactivity timeout, in milliseconds, for the
+ *  specified GWeb object.
+ *
+ *  The connect timeout (see #g_web_set_connect_timeout) runs until
+ *  the first data is received from the peer. The receive inactivity
+ *  timeout takes over from there: it is restarted each time data is
+ *  received and, if it expires, the request fails with
+ *  G_IO_ERROR_TIMED_OUT. Without it, a peer that sends part of its
+ *  response and then nothing more, without closing the connection,
+ *  leaves the request waiting indefinitely.
+ *
+ *  The timeout bounds the time between successive receipts of data,
+ *  not the total time of the request: a slow peer that continues to
+ *  make progress is not failed.
+ *
+ *  param[in,out]  web         A pointer to the mutable GWeb object
+ *                             for which the receive inactivity
+ *                             timeout is to be set.
+ *  param[in]      timeout_ms  The time, in milliseconds, for the
+ *                             receive inactivity timeout. A value of
+ *                             zero ('0') indicates that no receive
+ *                             inactivity timeout will be used.
+ *
+ *  @sa g_web_get_inactivity_timeout
+ *  @sa g_web_set_connect_timeout
+ *
+ */
+void g_web_set_inactivity_timeout(GWeb *web, guint timeout_ms)
+{
+	if (!web)
+		return;
+
+	debug(web, "timeout %ums", timeout_ms);
+
+	web->inactivity_timeout_ms = timeout_ms;
+}
+
+/**
+ *  @brief
+ *    Get the receive inactivity timeout.
+ *
+ *  This returns the receive inactivity timeout, in milliseconds, for
+ *  the specified GWeb object.
+ *
+ *  param[in]  web             A pointer to the immutable GWeb object
+ *                             for which the receive inactivity
+ *                             timeout is to be returned.
+ *
+ *  @returns
+ *    The receive inactivity timeout, in milliseconds. A value of zero
+ *    ('0') indicates that no receive inactivity timeout has been set.
+ *
+ *  @sa g_web_set_inactivity_timeout
+ *
+ */
+guint g_web_get_inactivity_timeout(const GWeb *web)
+{
+	guint timeout_ms = 0;
+
+	if (!web)
+		goto done;
+
+	timeout_ms = web->inactivity_timeout_ms;
+
+done:
+	return timeout_ms;
+}
+
 static bool process_send_buffer(struct web_session *session)
 {
 	GString *buf;
@@ -1461,13 +1643,20 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
 	const gsize bytes_available = session->receive_space - 1;
 	gsize bytes_read;
 	GIOStatus status;
+	bool more;
 	g_autoptr(GError) local_error = NULL;
 	g_autoptr(GError) error = NULL;
 
-	/* We received some data or condition, cancel the connect timeout. */
+	/*
+	 * We received some data or condition, cancel the connect timeout
+	 * and any receive inactivity timeout. The latter is restarted
+	 * below if more data is expected.
+	 */
 
 	cancel_connect_timeout(session);
 
+	cancel_inactivity_timeout(session);
+
 	/* If there was a low-level I/O condition or error, there is
 	 * nothing more to do; simply fail the request.
 	 */
@@ -1507,10 +1696,16 @@ static gboolean received_data(GIOChannel *channel, GIOCondition cond,
 	}
 
 	/* Otherwise, continue the session request and process the
-	 * received data.
+	 * received data and, if more is expected, (re)start the receive
+	 * inactivity timeout.
 	 */
 
-	return received_data_continue(session, bytes_read);
+	more = received_data_continue(session, bytes_read);
+	if (more)
+		add_inactivity_timeout(session,
+			g_web_get_inactivity_timeout(session->web));
+
+	return more;
 }
 
 static int bind_to_address(int sk, const char *interface, int family)
gweb/gweb.h +3 -0
index 75629500ef9c..5597a1e7f97a 100644
@@ -163,6 +163,9 @@ bool g_web_set_http_version(GWeb *web, const char *version);
 void g_web_set_connect_timeout(GWeb *web, guint timeout_ms);
 guint g_web_get_connect_timeout(const GWeb *web);
 
+void g_web_set_inactivity_timeout(GWeb *web, guint timeout_ms);
+guint g_web_get_inactivity_timeout(const GWeb *web);
+
 void g_web_set_close_connection(GWeb *web, bool enabled);
 bool g_web_get_close_connection(GWeb *web);
 
Signature · 1 line
2.52.0
#5 lore raw

PATCH 5/5 wispr: Bound online check response gaps

OnlineCheckConnectTimeout already bounds an online check from the start of its connection until the first data of the response arrives; gweb cancels it then. A response that stops after that, for example one whose headers and part of whose body arrive and then nothing more, without the connection closing, leaves the check incomplete indefinitely and, in continuous mode, no further check is scheduled.

Set the gweb receive inactivity timeout for online checks to the same OnlineCheckConnectTimeout value, so that each wait for further response data is bounded the same way as the wait for the first. Expiry fails the check with "no data from <address> for <timeout>ms", so it is logged and counted like any other failure.

This extends the meaning of an existing setting rather than adding a new one: the setting already bounds more than the TCP connection, and it now bounds every wait for the endpoint rather than only the first. It bounds each wait rather than the total duration of a check, so a slow link or endpoint that continues to make progress is not failed, and there is no new setting whose value could conflict with it. A value of zero continues to mean "no explicit timeout".

Update connman.conf(5) and the sample main.conf to describe the extended meaning.

---
 doc/connman.conf.5.in | 16 +++++++++++-----
 src/main.conf         | 16 +++++++++++-----
 src/wispr.c           |  9 +++++++++
 3 files changed, 31 insertions(+), 10 deletions(-)
doc/connman.conf.5.in +11 -5
index a81ac7bb1e45..f81603196b67 100644
@@ -224,11 +224,17 @@ http://ipv6.connman.net/online/status.html respectively.
 .BI OnlineCheckConnectTimeout= secs[.milliseconds]
 The time, in decimal seconds (for example, 12.3), to wait for a
 successful TCP connection to the host associated with
-\fBOnlineCheckIPv4URL\fR or \fBOnlineCheckIPv6URL\fR (see above). Connections
-that take longer than \fBOnlineCheckConnectTimeout\fR will be aborted. The
-default value is zero ('0') which indicates that no explicit
-connection timeout will be used, leaving the timeout to the underlying
-operating system and network stack.
+\fBOnlineCheckIPv4URL\fR or \fBOnlineCheckIPv6URL\fR (see above) and
+for the first data of its response, and thereafter for each further
+piece of the response. A check that waits longer than
+\fBOnlineCheckConnectTimeout\fR for any of these is aborted and counted
+as a failed check. Because the timeout bounds each wait rather than
+the whole check, a slow endpoint that continues to send its response
+is not failed. The default value is zero ('0') which indicates that
+no explicit timeout will be used, leaving the timeout to the
+underlying operating system and network stack; with zero, an endpoint
+that stops sending part way through its response, without closing
+the connection, leaves the check incomplete indefinitely.
 .TP
 .BI OnlineCheckInitialInterval= interval, OnlineCheckMaxInterval= interval
 Range of intervals between two online check requests.
src/main.conf +11 -5
index da1307d9ed09..e3c96eae3903 100644
@@ -179,11 +179,17 @@
 
 # The time, in decimal seconds (for example, 12.3), to wait for a
 # successful TCP connection to the host associated with
-# "OnlineCheckIPv4URL" or "OnlineCheckIPv6URL". Connections that take
-# longer than "OnlineCheckConnectTimeout" will be aborted. The default
-# value is zero ('0') which indicates that no explicit connection
-# timeout will be used, leaving the timeout to the underlying operating
-# system and network stack.
+# "OnlineCheckIPv4URL" or "OnlineCheckIPv6URL" and for the first data
+# of its response, and thereafter for each further piece of the
+# response. A check that waits longer than "OnlineCheckConnectTimeout"
+# for any of these is aborted and counted as a failed check. Because
+# the timeout bounds each wait rather than the whole check, a slow
+# endpoint that continues to send its response is not failed. The
+# default value is zero ('0') which indicates that no explicit timeout
+# will be used, leaving the timeout to the underlying operating system
+# and network stack; with zero, an endpoint that stops sending part
+# way through its response, without closing the connection, leaves
+# the check incomplete indefinitely.
 # OnlineCheckConnectTimeout=0
 
 # Range of intervals between two online check requests.
src/wispr.c +9 -0
index 013afe241054..ce03aec16858 100644
@@ -1556,6 +1556,15 @@ static int wispr_portal_detect(struct connman_wispr_portal_context *wp_context,
 
 	g_web_set_connect_timeout(wp_context->web, connect_timeout_ms);
 
+	/*
+	 * Use the same timeout to bound each gap between receipts of
+	 * response data once the connect timeout has ended with the
+	 * first of them. Otherwise, an endpoint, or a router answering in
+	 * its place, that sends part of a response and then nothing more
+	 * leaves this check incomplete indefinitely.
+	 */
+	g_web_set_inactivity_timeout(wp_context->web, connect_timeout_ms);
+
 	if (wp_context->type == CONNMAN_IPCONFIG_TYPE_IPV4) {
 		g_web_set_address_family(wp_context->web, AF_INET);
 		wp_context->status_url = online_check_ipv4_url;
Signature · 1 line
2.52.0