From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Christopher Baines Newsgroups: gmane.lisp.guile.bugs Subject: bug#70645: [PATCH 2/2] web: Handle non-blocking ports in tls-wrap. Date: Mon, 29 Apr 2024 12:57:29 +0100 Message-ID: <20240429115729.7228-2-mail@cbaines.net> References: <20240429115729.7228-1-mail@cbaines.net> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9199"; mail-complaints-to="usenet@ciao.gmane.io" To: 70645@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Mon Apr 29 13:58:21 2024 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s1PeG-0002Cu-Dp for guile-bugs@m.gmane-mx.org; Mon, 29 Apr 2024 13:58:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s1Pdl-0005WN-W3; Mon, 29 Apr 2024 07:57:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1s1Pdg-0005VA-P3 for bug-guile@gnu.org; Mon, 29 Apr 2024 07:57:46 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1s1Pde-0003P4-Kd for bug-guile@gnu.org; Mon, 29 Apr 2024 07:57:44 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s1Pdy-0001dt-9i for bug-guile@gnu.org; Mon, 29 Apr 2024 07:58:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Christopher Baines Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Mon, 29 Apr 2024 11:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70645 X-GNU-PR-Package: guile Original-Received: via spool by 70645-submit@debbugs.gnu.org id=B70645.17143918756298 (code B ref 70645); Mon, 29 Apr 2024 11:58:02 +0000 Original-Received: (at 70645) by debbugs.gnu.org; 29 Apr 2024 11:57:55 +0000 Original-Received: from localhost ([127.0.0.1]:56844 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s1Pdq-0001dR-Vr for submit@debbugs.gnu.org; Mon, 29 Apr 2024 07:57:55 -0400 Original-Received: from mira.cbaines.net ([212.71.252.8]:43400) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s1Pdn-0001dK-3D for 70645@debbugs.gnu.org; Mon, 29 Apr 2024 07:57:54 -0400 Original-Received: from localhost (unknown [212.132.255.10]) by mira.cbaines.net (Postfix) with ESMTPSA id 13FBE27BBE9 for <70645@debbugs.gnu.org>; Mon, 29 Apr 2024 12:57:30 +0100 (BST) Original-Received: from localhost (localhost [local]) by localhost (OpenSMTPD) with ESMTPA id bd4edad6 for <70645@debbugs.gnu.org>; Mon, 29 Apr 2024 11:57:29 +0000 (UTC) X-Mailer: git-send-email 2.41.0 In-Reply-To: <20240429115729.7228-1-mail@cbaines.net> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10822 Archived-At: As described in the GnuTLS documentation on Asynchronous operation, GNUTLS_NONBLOCK should be passed to gnutls_init, and the Guile equivalent is passing connection-flag/nonblock to make-session. Additionally, error/again or error/interrupted should lead to a retry of the handshake, after waiting for the appropriate I/O on the port. As record-get-direction is new in Guile-GnuTLS, specifically check if this is defined. * module/web/client.scm (tls-wrap): Call make-session with connection-flag/nonblock if the port is non-blocking, and handle waiting for I/O when performing the handshake. --- module/web/client.scm | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/module/web/client.scm b/module/web/client.scm index f26b5d259..caf8e5f35 100644 --- a/module/web/client.scm +++ b/module/web/client.scm @@ -33,6 +33,7 @@ (define-module (web client) #:use-module (rnrs bytevectors) + #:use-module (ice-9 suspendable-ports) #:use-module (ice-9 binary-ports) #:use-module (ice-9 copy-tree) #:use-module (ice-9 iconv) @@ -225,7 +226,14 @@ host name without trailing dot." (load-gnutls) - (let ((session (make-session connection-end/client)) + (let ((session + (apply + make-session + (cons connection-end/client + (if (zero? (logand O_NONBLOCK (fcntl port F_GETFL))) + '() + ;; If the port is non-blocking, tell GnuTLS + (list connection-flag/nonblock))))) (ca-certs (x509-certificate-directory))) ;; Some servers such as 'cloud.github.com' require the client to support ;; the 'SERVER NAME' extension. However, 'set-session-server-name!' is @@ -261,7 +269,19 @@ host name without trailing dot." (lambda () (handshake session)) (lambda (key err proc . rest) - (cond ((eq? err error/warning-alert-received) + (cond ((and + (or (eq? err error/again) + (eq? err error/interrupted)) + (module-defined? (resolve-interface '(gnutls)) + 'record-get-direction)) ; Guile-GnuTLS >= 4.0.0 + (if (= 0 (record-get-direction session)) + ((current-read-waiter) port) + ((current-write-waiter) port)) + + ;; These errors are expected and just signal that + ;; GnuTLS was interrupted, so don't count the retry + (loop retries)) + ((eq? err error/warning-alert-received) ;; Like Wget, do no stop upon non-fatal alerts such as ;; 'alert-description/unrecognized-name'. (format (current-error-port) -- 2.41.0