1 /* $NetBSD: timer.c,v 1.1 2024/02/18 20:57:34 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 /*! \file */
17
18 #include <stdbool.h>
19
20 #include <isc/result.h>
21 #include <isc/time.h>
22 #include <isc/timer.h>
23
24 #include <dns/timer.h>
25 #include <dns/types.h>
26
27 #define CHECK(op) \
28 do { \
29 result = (op); \
30 if (result != ISC_R_SUCCESS) \
31 goto failure; \
32 } while (0)
33
34 isc_result_t
dns_timer_setidle(isc_timer_t * timer,unsigned int maxtime,unsigned int idletime,bool purge)35 dns_timer_setidle(isc_timer_t *timer, unsigned int maxtime,
36 unsigned int idletime, bool purge) {
37 isc_result_t result;
38 isc_interval_t maxinterval, idleinterval;
39 isc_time_t expires;
40
41 /* Compute the time of expiry. */
42 isc_interval_set(&maxinterval, maxtime, 0);
43 CHECK(isc_time_nowplusinterval(&expires, &maxinterval));
44
45 /*
46 * Compute the idle interval, and add a spare nanosecond to
47 * work around the silly limitation of the ISC timer interface
48 * that you cannot specify an idle interval of zero.
49 */
50 isc_interval_set(&idleinterval, idletime, 1);
51
52 CHECK(isc_timer_reset(timer, isc_timertype_once, &expires,
53 &idleinterval, purge));
54 failure:
55 return (result);
56 }
57