From patchwork Thu Jan 15 15:40:50 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 429489 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 747AC1400EA for ; Fri, 16 Jan 2015 02:41:29 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id C2803287550; Thu, 15 Jan 2015 16:38:47 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, T_DKIM_INVALID autolearn=no version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id D10B5287550 for ; Thu, 15 Jan 2015 16:38:42 +0100 (CET) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-wg0-f41.google.com (mail-wg0-f41.google.com [74.125.82.41]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Thu, 15 Jan 2015 16:38:42 +0100 (CET) Received: by mail-wg0-f41.google.com with SMTP id l18so15690988wgh.0 for ; Thu, 15 Jan 2015 07:40:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; bh=xWu5XMpDHWdI6UnNnCqFvwpWyxFjo8vOs78SzJ/ARHU=; b=jtKmw3XOnKBGLzcbiLafX6qiA/cBfPVay8jJOIq/sx6e1LW6hjsSgBj4to1sJ9986D 6/OyVHaCSZJKWw7+5FphjaoGb9NZiz3URAmB3GREAVL9nmocmlRwt/BjcnFib+AFgvuh qO8pK7LRNMgVdZ96WPsglZKKmlLZ3OqIuANI8doeAXj4BmZnUvNdRBgrNNibzdHgviWz Hb1HfSKn3dAOyVXlQmga4qEhJJ9TGlkxqBin/dzfF0L1VJcmvO9tfGiqYerqWwi5U1Bs X8/9WaUMNd0MD8QWLo8rxMBxM11En5chNqJukA5QSgRJrWyfQS+n5f9wZWKcmwMSAqM6 eJ0A== X-Received: by 10.180.78.233 with SMTP id e9mr27595887wix.25.1421336457141; Thu, 15 Jan 2015 07:40:57 -0800 (PST) Received: from linux-tdhb.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by mx.google.com with ESMTPSA id qg11sm23213219wic.17.2015.01.15.07.40.55 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Jan 2015 07:40:56 -0800 (PST) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: Felix Fietkau Date: Thu, 15 Jan 2015 16:40:50 +0100 Message-Id: <1421336450-10781-1-git-send-email-zajec5@gmail.com> X-Mailer: git-send-email 1.8.4.5 MIME-Version: 1.0 Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH uclient] support for connection timeout X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" Signed-off-by: Rafał Miłecki --- uclient-fetch.c | 4 ++++ uclient.c | 25 ++++++++++++++++++++++++- uclient.h | 4 ++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/uclient-fetch.c b/uclient-fetch.c index 22f15c6..0617a02 100644 --- a/uclient-fetch.c +++ b/uclient-fetch.c @@ -194,6 +194,10 @@ static void handle_uclient_error(struct uclient *cl, int code) type = "Connection failed"; error_ret = 4; break; + case UCLIENT_ERROR_TIMEDOUT: + type = "Connection timed out"; + error_ret = 4; + break; case UCLIENT_ERROR_SSL_INVALID_CERT: type = "Invalid SSL certificate"; ignore = !verify; diff --git a/uclient.c b/uclient.c index ab2d5b6..a405179 100644 --- a/uclient.c +++ b/uclient.c @@ -207,12 +207,31 @@ int uclient_write(struct uclient *cl, char *buf, int len) return cl->backend->write(cl, buf, len); } +static void uclient_request_timeout(struct uloop_timeout *timeout) +{ + struct uclient *cl = container_of(timeout, struct uclient, request_timeout); + + if (cl->backend->disconnect) + cl->backend->disconnect(cl); + + uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT); +} + int uclient_request(struct uclient *cl) { + int err; + if (!cl->backend->request) return -1; - return cl->backend->request(cl); + err = cl->backend->request(cl); + if (err) + return err; + + cl->request_timeout.cb = uclient_request_timeout; + uloop_timeout_set(&cl->request_timeout, UCLIENT_DEFAULT_CONNECTION_TIMEOUT_MS); + + return 0; } int uclient_read(struct uclient *cl, char *buf, int len) @@ -225,6 +244,8 @@ int uclient_read(struct uclient *cl, char *buf, int len) void uclient_disconnect(struct uclient *cl) { + uloop_timeout_cancel(&cl->request_timeout); + if (!cl->backend->disconnect) return; @@ -243,6 +264,8 @@ static void __uclient_backend_change_state(struct uloop_timeout *timeout) static void uclient_backend_change_state(struct uclient *cl) { + uloop_timeout_cancel(&cl->request_timeout); + cl->timeout.cb = __uclient_backend_change_state; uloop_timeout_set(&cl->timeout, 1); } diff --git a/uclient.h b/uclient.h index d5a0d5b..095d894 100644 --- a/uclient.h +++ b/uclient.h @@ -24,12 +24,15 @@ #include #include +#define UCLIENT_DEFAULT_CONNECTION_TIMEOUT_MS 30000 + struct uclient_cb; struct uclient_backend; enum uclient_error_code { UCLIENT_ERROR_UNKNOWN, UCLIENT_ERROR_CONNECT, + UCLIENT_ERROR_TIMEDOUT, UCLIENT_ERROR_SSL_INVALID_CERT, UCLIENT_ERROR_SSL_CN_MISMATCH, UCLIENT_ERROR_MISSING_SSL_CONTEXT, @@ -67,6 +70,7 @@ struct uclient { int status_code; struct blob_attr *meta; + struct uloop_timeout request_timeout; struct uloop_timeout timeout; };