From patchwork Fri Jan 16 09:42:54 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: 429789 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 8E10A14018C for ; Fri, 16 Jan 2015 20:43:27 +1100 (AEDT) Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id C931128A8C6; Fri, 16 Jan 2015 10:41:03 +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 F14E128A8C6 for ; Fri, 16 Jan 2015 10:40:55 +0100 (CET) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-wg0-f50.google.com (mail-wg0-f50.google.com [74.125.82.50]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Fri, 16 Jan 2015 10:40:55 +0100 (CET) Received: by mail-wg0-f50.google.com with SMTP id a1so19522739wgh.9 for ; Fri, 16 Jan 2015 01:43:04 -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:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=K0ZAHugycRkv2Dx0HNjNrlfE+sg1bMUi6dICl2VuDIU=; b=PTMJDsoAGVXePDFRK2EGPrims6N8ZthwCvUVHQ8vWOGQQstgGOKYiR5tW6pZ2h8hWZ XAaYYMF4q47hxkJ8Lm8vKMqiXvbl2KDndqBRHVHkdwGbdznqEdwcqg92SNixJU4dfqWz VzJrgkUZbO0Rugd13riulXuuaQARYb857gVXgWAkIAPBSvDeVh1hq4eMCKc581Kfqm/g 00WIrgI0ARvGe5ad+ptcRCBRQI48gxuyXMQQRqvwFdAVsqmnOufJExjkh+uXR/GcKhCV 5fnYB81yK+oLpjMv8zGgnTjg1jzwPLUrSqFtiu9mHLXd16CykQCfXJgNYwmfI5JdG0um ho8w== X-Received: by 10.180.19.138 with SMTP id f10mr4414442wie.56.1421401384731; Fri, 16 Jan 2015 01:43:04 -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 n3sm2265215wiw.5.2015.01.16.01.43.03 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 16 Jan 2015 01:43:03 -0800 (PST) From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= To: Felix Fietkau Date: Fri, 16 Jan 2015 10:42:54 +0100 Message-Id: <1421401374-9085-1-git-send-email-zajec5@gmail.com> X-Mailer: git-send-email 1.8.4.5 In-Reply-To: <1421336450-10781-1-git-send-email-zajec5@gmail.com> References: <1421336450-10781-1-git-send-email-zajec5@gmail.com> MIME-Version: 1.0 Cc: openwrt-devel@lists.openwrt.org Subject: [OpenWrt-Devel] [PATCH V2 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 --- V2: Add uclient_set_timeout Stop timer after receiving data Start timer after fetching data by uclient user --- uclient-fetch.c | 4 ++++ uclient-http.c | 12 ++++++++++-- uclient.c | 36 +++++++++++++++++++++++++++++++++++- uclient.h | 20 ++++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) 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-http.c b/uclient-http.c index c25e52f..38e7761 100644 --- a/uclient-http.c +++ b/uclient-http.c @@ -689,8 +689,12 @@ static void __uclient_notify_read(struct uclient_http *uh) if (uh->eof) return; - if (uh->state == HTTP_STATE_RECV_DATA && uc->cb->data_read) - uc->cb->data_read(uc); + if (uh->state == HTTP_STATE_RECV_DATA) + /* Now it's uclient user turn to read some data */ + uloop_timeout_cancel(&uc->connection_timeout); + + if (uc->cb->data_read) + uc->cb->data_read(uc); } static void __uclient_notify_write(struct uclient_http *uh) @@ -1030,6 +1034,10 @@ uclient_http_read(struct uclient *cl, char *buf, unsigned int len) uclient_notify_eof(uh); + /* Now that we consumed something and if this isn't EOF, start timer again */ + if (!uh->uc.eof && !cl->connection_timeout.pending) + uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs); + return len; } diff --git a/uclient.c b/uclient.c index ab2d5b6..d599763 100644 --- a/uclient.c +++ b/uclient.c @@ -141,6 +141,16 @@ free: return NULL; } +static void uclient_connection_timeout(struct uloop_timeout *timeout) +{ + struct uclient *cl = container_of(timeout, struct uclient, connection_timeout); + + if (cl->backend->disconnect) + cl->backend->disconnect(cl); + + uclient_backend_set_error(cl, UCLIENT_ERROR_TIMEDOUT); +} + struct uclient *uclient_new(const char *url_str, const char *auth_str, const struct uclient_cb *cb) { struct uclient *cl; @@ -157,6 +167,8 @@ struct uclient *uclient_new(const char *url_str, const char *auth_str, const str cl->backend = url->backend; cl->cb = cb; cl->url = url; + cl->timeout_msecs = UCLIENT_DEFAULT_TIMEOUT_MS; + cl->connection_timeout.cb = uclient_connection_timeout; return cl; } @@ -182,6 +194,16 @@ int uclient_set_url(struct uclient *cl, const char *url_str, const char *auth_st return 0; } +int uclient_set_timeout(struct uclient *cl, int msecs) +{ + if (msecs <= 0) + return -EINVAL; + + cl->timeout_msecs = msecs; + + return 0; +} + int uclient_connect(struct uclient *cl) { return cl->backend->connect(cl); @@ -209,10 +231,18 @@ int uclient_write(struct uclient *cl, char *buf, int len) 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; + + uloop_timeout_set(&cl->connection_timeout, cl->timeout_msecs); + + return 0; } int uclient_read(struct uclient *cl, char *buf, int len) @@ -225,6 +255,8 @@ int uclient_read(struct uclient *cl, char *buf, int len) void uclient_disconnect(struct uclient *cl) { + uloop_timeout_cancel(&cl->connection_timeout); + if (!cl->backend->disconnect) return; @@ -252,6 +284,7 @@ void __hidden uclient_backend_set_error(struct uclient *cl, int code) if (cl->error_code) return; + uloop_timeout_cancel(&cl->connection_timeout); cl->error_code = code; uclient_backend_change_state(cl); } @@ -261,6 +294,7 @@ void __hidden uclient_backend_set_eof(struct uclient *cl) if (cl->eof || cl->error_code) return; + uloop_timeout_cancel(&cl->connection_timeout); cl->eof = true; uclient_backend_change_state(cl); } diff --git a/uclient.h b/uclient.h index d5a0d5b..5904a38 100644 --- a/uclient.h +++ b/uclient.h @@ -24,12 +24,15 @@ #include #include +#define UCLIENT_DEFAULT_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, @@ -59,6 +62,7 @@ struct uclient { union uclient_addr local_addr, remote_addr; struct uclient_url *url; + int timeout_msecs; void *priv; bool eof; @@ -67,6 +71,7 @@ struct uclient { int status_code; struct blob_attr *meta; + struct uloop_timeout connection_timeout; struct uloop_timeout timeout; }; @@ -82,6 +87,21 @@ struct uclient *uclient_new(const char *url, const char *auth_str, const struct void uclient_free(struct uclient *cl); int uclient_set_url(struct uclient *cl, const char *url, const char *auth); + +/** + * Sets connection timeout. + * + * Provided timeout value will be used for: + * 1) Receiving HTTP response + * 2) Receiving data + * + * In case of timeout uclient will use error callback with + * UCLIENT_ERROR_TIMEDOUT code. + * + * @param msecs timeout in milliseconds + */ +int uclient_set_timeout(struct uclient *cl, int msecs); + int uclient_connect(struct uclient *cl); void uclient_disconnect(struct uclient *cl);