From patchwork Thu Sep 22 22:58:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arne Schwabe X-Patchwork-Id: 2791 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director13.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id OPtVIWp1LWNLSwAAIUCqbw (envelope-from ) for ; Fri, 23 Sep 2022 04:59:22 -0400 Received: from proxy19.mail.ord1d.rsapps.net ([172.30.191.6]) by director13.mail.ord1d.rsapps.net with LMTP id IGwvIWp1LWOFSQAA91zNiA (envelope-from ) for ; Fri, 23 Sep 2022 04:59:22 -0400 Received: from smtp40.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy19.mail.ord1d.rsapps.net with LMTPS id QH3RIGp1LWOOCwAAyH2SIw (envelope-from ) for ; Fri, 23 Sep 2022 04:59:22 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp40.gate.ord1d.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=rfc2549.org X-Suspicious-Flag: YES X-Classification-ID: 03ac3df2-3b1e-11ed-95a9-525400f204c2-1-1 Received: from [216.105.38.7] ([216.105.38.7:47340] helo=lists.sourceforge.net) by smtp40.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id A1/A7-22876-9657D236; Fri, 23 Sep 2022 04:59:22 -0400 Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1obeWI-0005dF-BC; Fri, 23 Sep 2022 08:58:50 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1obeVn-0005bK-1I for openvpn-devel@lists.sourceforge.net; Fri, 23 Sep 2022 08:58:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=kVLY5qIfPmBTBl8kegDdyPrynk2oMRA0sWyUvjd7ls8=; b=RU/ScBNfOq0wiHrRW84y1H6JHb pMY9fybEsy3IgVRh6NVXuwM/CbmXwHHgxuSGdqtZApiKgByaoLFcnpCbib1NgAVMA8z+Zr0cLkz3v 4WcrzVS2ekDsc3NPzmuK9Y1wWqGbCmwMz6Zrqz1U2AFjT0M1afhiuAsRDsJ3mvIXgBTk=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:To:From: Sender:Reply-To:Cc:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=kVLY5qIfPmBTBl8kegDdyPrynk2oMRA0sWyUvjd7ls8=; b=G US1VF6mCaLoreR4FyiC93nYLy3DPamXNTRO+543y8S/aq61XQsSKGAd/kFA+R5Id5QFcCUaQxAl1O nGOBfEduJrfQ6Ref41NQlbpbC5/E7ff0ZsYMG6z/7bI9XXGV1M8UVwAgQ1t3imXgGvg9uIK+PCvP5 qlnZauCZffIEIoMM=; Received: from mail.blinkt.de ([192.26.174.232]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1obeVj-00HPIY-7I for openvpn-devel@lists.sourceforge.net; Fri, 23 Sep 2022 08:58:18 +0000 Received: from kamera.blinkt.de ([2001:638:502:390:20c:29ff:fec8:535c]) by mail.blinkt.de with smtp (Exim 4.95 (FreeBSD)) (envelope-from ) id 1obeVY-0004Pm-S2 for openvpn-devel@lists.sourceforge.net; Fri, 23 Sep 2022 10:58:04 +0200 Received: (nullmailer pid 3743155 invoked by uid 10006); Fri, 23 Sep 2022 08:58:04 -0000 From: Arne Schwabe To: openvpn-devel@lists.sourceforge.net Date: Fri, 23 Sep 2022 10:58:04 +0200 Message-Id: <20220923085804.3743109-1-arne@rfc2549.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Remove function event_timeout_clear_ret as it is unused. Cleanup event_timeout_trigger a bit. Do an instant return false if the timeout is not defined and inline local_now and use event_timeout_remaining instead of local duplicated code. Content analysis details: (0.3 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.2 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail domains are different 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 SPF_NONE SPF: sender does not publish an SPF Record X-Headers-End: 1obeVj-00HPIY-7I Subject: [Openvpn-devel] [PATCH] Document/cleanup event_timeout functions X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox Remove function event_timeout_clear_ret as it is unused. Cleanup event_timeout_trigger a bit. Do an instant return false if the timeout is not defined and inline local_now and use event_timeout_remaining instead of local duplicated code. Add doxygen comments for all timeout function, especially for the event_timeout_trigger function that is hard to understand otherwise. Signed-off-by: Arne Schwabe --- src/openvpn/interval.c | 50 ++++++++++++----------- src/openvpn/interval.h | 92 +++++++++++++++++++++++++++++------------- src/openvpn/openvpn.h | 4 +- 3 files changed, 92 insertions(+), 54 deletions(-) diff --git a/src/openvpn/interval.c b/src/openvpn/interval.c index 2f0fc42dd..8f3c1c604 100644 --- a/src/openvpn/interval.c +++ b/src/openvpn/interval.c @@ -41,44 +41,46 @@ interval_init(struct interval *top, int horizon, int refresh) top->horizon = horizon; } + bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, const int et_const_retry) { + if (!et->defined) + { + return false; + } + bool ret = false; - const time_t local_now = now; + time_t wakeup = event_timeout_remaining(et); - if (et->defined) + if (wakeup <= 0) { - time_t wakeup = et->last - local_now + et->n; - if (wakeup <= 0) - { #if INTERVAL_DEBUG - dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, - et_const_retry); + dmsg(D_INTERVAL, "EVENT event_timeout_trigger (%d) etcr=%d", et->n, + et_const_retry); #endif - if (et_const_retry < 0) - { - et->last = local_now; - wakeup = et->n; - ret = true; - } - else - { - wakeup = et_const_retry; - } + if (et_const_retry < 0) + { + et->last = now; + wakeup = et->n; + ret = true; } - - if (tv && wakeup < tv->tv_sec) + else { + wakeup = et_const_retry; + } + } + + if (tv && wakeup < tv->tv_sec) + { #if INTERVAL_DEBUG - dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", - (int) wakeup, et->n, et_const_retry); + dmsg(D_INTERVAL, "EVENT event_timeout_wakeup (%d/%d) etcr=%d", + (int) wakeup, et->n, et_const_retry); #endif - tv->tv_sec = wakeup; - tv->tv_usec = 0; - } + tv->tv_sec = wakeup; + tv->tv_usec = 0; } return ret; } diff --git a/src/openvpn/interval.h b/src/openvpn/interval.h index f58bfacf6..f6e40705c 100644 --- a/src/openvpn/interval.h +++ b/src/openvpn/interval.h @@ -135,9 +135,9 @@ interval_action(struct interval *top) struct event_timeout { - bool defined; - interval_t n; - time_t last; /* time of last event */ + bool defined; /**< This timeout is active */ + interval_t n; /**< periodic interval for periodic timeouts */ + time_t last; /**< time of last event */ }; static inline bool @@ -145,7 +145,12 @@ event_timeout_defined(const struct event_timeout *et) { return et->defined; } - +/** + * Clears the timeout and reset all values to 0. Following timer checks will + * not trigger. + * + * @param et timer struct + */ static inline void event_timeout_clear(struct event_timeout *et) { @@ -154,22 +159,30 @@ event_timeout_clear(struct event_timeout *et) et->last = 0; } -static inline struct event_timeout -event_timeout_clear_ret(void) -{ - struct event_timeout ret; - event_timeout_clear(&ret); - return ret; -} +/** + * Initialises a timer struct. The timer will become true/trigger after last + n seconds + * + * + * @param et Timer struct + * @param n Interval of the timer for periodic timer. A negative value for n will be interpreted as 0 + * @param last Sets the base time of the timer. + */ static inline void -event_timeout_init(struct event_timeout *et, interval_t n, const time_t local_now) +event_timeout_init(struct event_timeout *et, interval_t n, const time_t last) { et->defined = true; et->n = (n >= 0) ? n : 0; - et->last = local_now; + et->last = last; } +/** + * Reset a timer. + * + * Sets the last time the timer has been triggered for the calculation of the + * next event. + * @param et + */ static inline void event_timeout_reset(struct event_timeout *et) { @@ -179,6 +192,12 @@ event_timeout_reset(struct event_timeout *et) } } +/** + * Sets the periodic parameter n of a timeout. + * @param et + * @param n set the periodic value of a timeout, negative values + * will be interpreted as 0 + */ static inline void event_timeout_modify_wakeup(struct event_timeout *et, interval_t n) { @@ -189,32 +208,47 @@ event_timeout_modify_wakeup(struct event_timeout *et, interval_t n) } } -/* - * Will return the time left for a timeout, this function does not check - * if the timeout is actually valid +/** + * Return time until the timeout should triggered from from now, + * this function does not check if the timeout is actually valid. */ static inline interval_t event_timeout_remaining(struct event_timeout *et) { - return (interval_t) (et->last - now + et->n); + return (interval_t) ((et->last + et->n) - now); } -/* - * This is the principal function for testing and triggering recurring - * timers and will return true on a timer signal event. - * If et_const_retry == ETT_DEFAULT and a signal occurs, - * the function will return true and *et will be armed for the - * next event. If et_const_retry >= 0 and a signal occurs, - * *et will not be touched, but *tv will be set to - * minimum (*tv, et_const_retry) for a future re-test, - * and the function will return true. - */ - #define ETT_DEFAULT (-1) +/** + * This is the principal function for testing and triggering recurring + * timers. + * + * If *et is not triggered *tv is set to remaining time until the timeout if + * not already lower: + * + * *tv = minimum(*tv, event_timeout_remaining(*et)) + * + * If *et triggers and et_const_retry is negative (ETT_DEFAULT is -1): + * - the function will return true + * - *et will be armed for the next event (et->last set to now). + * - *tv will be lowered to the event period (n) if larger than the + * period of the event (set to *et's next timeout) + * + * If *et triggers and et_const_retry >= 0, *tv be reduced to et_const_try if + * larger: + * + * *tv = *minimum(*tv, et_const_retry) + * + * + * @param et the timeout to check + * @param tv will be set to timeout for next check for this timeout unless already small. + * @param et_const_retry see above + * @return if the timeout has triggered and event has been reset + */ bool event_timeout_trigger(struct event_timeout *et, struct timeval *tv, - const int et_const_retry); + int et_const_retry); /* * Measure time intervals in microseconds diff --git a/src/openvpn/openvpn.h b/src/openvpn/openvpn.h index 00cd652fa..aae1d21b8 100644 --- a/src/openvpn/openvpn.h +++ b/src/openvpn/openvpn.h @@ -386,7 +386,9 @@ struct context_2 * Event loop info */ - /* how long to wait on link/tun read before we will need to be serviced */ + /* time to next event of timers and similar. This is used to determine + * how long to wait on event wait (select/poll on link/tun read) + * before this context wants to be serviced */ struct timeval timeval; /* next wakeup for processing coarse timers (>1 sec resolution) */