Column sorting fixes, thanks to Tony550
[tomato.git] / release / src / router / openvpn / interval.h
blob4814ec924c7641d3176ad0154129b5c18f00e1bd
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
8 * Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * The interval_ routines are designed to optimize the calling of a routine
27 * (normally tls_multi_process()) which can be called less frequently
28 * between triggers.
31 #ifndef INTERVAL_H
32 #define INTERVAL_H
34 #include "otime.h"
36 #define INTERVAL_DEBUG 0
39 * Designed to limit calls to expensive functions that need to be called
40 * regularly.
43 struct interval
45 interval_t refresh;
46 interval_t horizon;
47 time_t future_trigger;
48 time_t last_action;
49 time_t last_test_true;
52 void interval_init (struct interval *top, int horizon, int refresh);
55 * IF
56 * last_action less than horizon seconds ago
57 * OR last_test_true more than refresh seconds ago
58 * OR hit future_trigger
59 * THEN
60 * return true
61 * ELSE
62 * set wakeup to the number of seconds until a true return
63 * return false
66 static inline bool
67 interval_test (struct interval* top)
69 bool trigger = false;
70 const time_t local_now = now;
72 if (top->future_trigger && local_now >= top->future_trigger)
74 trigger = true;
75 top->future_trigger = 0;
78 if (top->last_action + top->horizon > local_now ||
79 top->last_test_true + top->refresh <= local_now ||
80 trigger)
82 top->last_test_true = local_now;
83 #if INTERVAL_DEBUG
84 dmsg (D_INTERVAL, "INTERVAL interval_test true");
85 #endif
86 return true;
88 else
90 return false;
94 static inline void
95 interval_schedule_wakeup (struct interval* top, interval_t *wakeup)
97 const time_t local_now = now;
98 interval_earliest_wakeup (wakeup, top->last_test_true + top->refresh, local_now);
99 interval_earliest_wakeup (wakeup, top->future_trigger, local_now);
100 #if INTERVAL_DEBUG
101 dmsg (D_INTERVAL, "INTERVAL interval_schedule wakeup=%d", (int)*wakeup);
102 #endif
106 * In wakeup seconds, interval_test will return true once.
108 static inline void
109 interval_future_trigger (struct interval* top, interval_t wakeup) {
110 if (wakeup)
112 #if INTERVAL_DEBUG
113 dmsg (D_INTERVAL, "INTERVAL interval_future_trigger %d", (int)wakeup);
114 #endif
115 top->future_trigger = now + wakeup;
120 * Once an action is triggered, interval_test will remain true for
121 * horizon seconds.
123 static inline void
124 interval_action (struct interval* top)
126 #if INTERVAL_DEBUG
127 dmsg (D_INTERVAL, "INTERVAL action");
128 #endif
129 top->last_action = now;
133 * Measure when n seconds beyond an event have elapsed
136 struct event_timeout
138 bool defined;
139 interval_t n;
140 time_t last; /* time of last event */
143 static inline bool
144 event_timeout_defined (const struct event_timeout* et)
146 return et->defined;
149 static inline void
150 event_timeout_clear (struct event_timeout* et)
152 et->defined = false;
153 et->n = 0;
154 et->last = 0;
157 static inline struct event_timeout
158 event_timeout_clear_ret ()
160 struct event_timeout ret;
161 event_timeout_clear (&ret);
162 return ret;
165 static inline void
166 event_timeout_init (struct event_timeout* et, interval_t n, const time_t local_now)
168 et->defined = true;
169 et->n = (n >= 0) ? n : 0;
170 et->last = local_now;
173 static inline void
174 event_timeout_reset (struct event_timeout* et)
176 if (et->defined)
177 et->last = now;
180 static inline void
181 event_timeout_modify_wakeup (struct event_timeout* et, interval_t n)
183 /* note that you might need to call reset_coarse_timers after this */
184 if (et->defined)
185 et->n = (n >= 0) ? n : 0;
189 * This is the principal function for testing and triggering recurring
190 * timers and will return true on a timer signal event.
191 * If et_const_retry == ETT_DEFAULT and a signal occurs,
192 * the function will return true and *et will be armed for the
193 * next event. If et_const_retry >= 0 and a signal occurs,
194 * *et will not be touched, but *tv will be set to
195 * minimum (*tv, et_const_retry) for a future re-test,
196 * and the function will return true.
199 #define ETT_DEFAULT (-1)
201 bool event_timeout_trigger (struct event_timeout *et,
202 struct timeval *tv,
203 const int et_const_retry);
206 * Measure time intervals in microseconds
209 #define USEC_TIMER_MAX 60 /* maximum interval size in seconds */
211 #define USEC_TIMER_MAX_USEC (USEC_TIMER_MAX * 1000000)
213 struct usec_timer {
214 struct timeval start;
215 struct timeval end;
218 #ifdef HAVE_GETTIMEOFDAY
220 static inline void
221 usec_timer_start (struct usec_timer *obj)
223 CLEAR (*obj);
224 openvpn_gettimeofday (&obj->start, NULL);
227 static inline void
228 usec_timer_end (struct usec_timer *obj)
230 openvpn_gettimeofday (&obj->end, NULL);
233 #endif /* HAVE_GETTIMEOFDAY */
235 static inline bool
236 usec_timer_interval_defined (struct usec_timer *obj)
238 return obj->start.tv_sec && obj->end.tv_sec;
241 static inline int
242 usec_timer_interval (struct usec_timer *obj)
244 return tv_subtract (&obj->end, &obj->start, USEC_TIMER_MAX);
247 #endif /* INTERVAL_H */