Column sorting fixes, thanks to Tony550
[tomato.git] / release / src / router / openvpn / otime.h
blobfd73bbdd50287babf3180147920fc536dfeff65f
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
25 #ifndef OTIME_H
26 #define OTIME_H
28 #include "common.h"
29 #include "integer.h"
30 #include "buffer.h"
32 struct frequency_limit
34 int max;
35 int per;
36 int n;
37 time_t reset;
40 struct frequency_limit *frequency_limit_init (int max, int per);
41 void frequency_limit_free (struct frequency_limit *f);
42 bool frequency_limit_event_allowed (struct frequency_limit *f);
44 #ifdef WIN32
45 int gettimeofday(struct timeval *tv, void *tz);
46 #endif
48 /* format a time_t as ascii, or use current time if 0 */
49 const char* time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc);
51 /* struct timeval functions */
53 const char *tv_string (const struct timeval *tv, struct gc_arena *gc);
54 const char *tv_string_abs (const struct timeval *tv, struct gc_arena *gc);
56 extern time_t now; /* updated frequently to time(NULL) */
58 void time_test (void);
60 #if TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)
62 void update_now (const time_t system_time);
64 extern time_t now_usec;
65 void update_now_usec (struct timeval *tv);
67 static inline int
68 openvpn_gettimeofday (struct timeval *tv, void *tz)
70 const int status = gettimeofday (tv, tz);
71 if (!status)
73 update_now_usec (tv);
74 tv->tv_sec = now;
75 tv->tv_usec = now_usec;
77 return status;
80 static inline void
81 update_time (void)
83 #ifdef WIN32
84 /* on WIN32, gettimeofday is faster than time(NULL) */
85 struct timeval tv;
86 openvpn_gettimeofday (&tv, NULL);
87 #else
88 update_now (time (NULL));
89 #endif
92 #else /* !(TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY)) */
94 static inline void
95 update_time (void)
97 #if defined(WIN32) && defined(HAVE_GETTIMEOFDAY)
98 struct timeval tv;
99 if (!gettimeofday (&tv, NULL))
101 if (tv.tv_sec != now)
102 now = tv.tv_sec;
104 #else
105 const time_t real_time = time (NULL);
106 if (real_time != now)
107 now = real_time;
108 #endif
111 #ifdef HAVE_GETTIMEOFDAY
113 static inline int
114 openvpn_gettimeofday (struct timeval *tv, void *tz)
116 return gettimeofday (tv, tz);
119 #endif
121 #endif /* TIME_BACKTRACK_PROTECTION && defined(HAVE_GETTIMEOFDAY) */
123 static inline time_t
124 openvpn_time (time_t *t)
126 update_time ();
127 if (t)
128 *t = now;
129 return now;
132 static inline void
133 tv_clear (struct timeval *tv)
135 tv->tv_sec = 0;
136 tv->tv_usec = 0;
139 static inline bool
140 tv_defined (const struct timeval *tv)
142 return tv->tv_sec > 0 && tv->tv_usec > 0;
145 /* return tv1 - tv2 in usec, constrained by max_seconds */
146 static inline int
147 tv_subtract (const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
149 const int max_usec = max_seconds * 1000000;
150 const int sec_diff = tv1->tv_sec - tv2->tv_sec;
152 if (sec_diff > ((int)max_seconds + 10))
153 return max_usec;
154 else if (sec_diff < -((int)max_seconds + 10))
155 return -max_usec;
156 return constrain_int (sec_diff * 1000000 + (tv1->tv_usec - tv2->tv_usec), -max_usec, max_usec);
159 static inline void
160 tv_add (struct timeval *dest, const struct timeval *src)
162 dest->tv_sec += src->tv_sec;
163 dest->tv_usec += src->tv_usec;
164 dest->tv_sec += (dest->tv_usec >> 20);
165 dest->tv_usec &= 0x000FFFFF;
166 if (dest->tv_usec >= 1000000)
168 dest->tv_usec -= 1000000;
169 dest->tv_sec += 1;
173 static inline bool
174 tv_lt (const struct timeval *t1, const struct timeval *t2)
176 if (t1->tv_sec < t2->tv_sec)
177 return true;
178 else if (t1->tv_sec > t2->tv_sec)
179 return false;
180 else
181 return t1->tv_usec < t2->tv_usec;
184 static inline bool
185 tv_le (const struct timeval *t1, const struct timeval *t2)
187 if (t1->tv_sec < t2->tv_sec)
188 return true;
189 else if (t1->tv_sec > t2->tv_sec)
190 return false;
191 else
192 return t1->tv_usec <= t2->tv_usec;
195 static inline bool
196 tv_ge (const struct timeval *t1, const struct timeval *t2)
198 if (t1->tv_sec > t2->tv_sec)
199 return true;
200 else if (t1->tv_sec < t2->tv_sec)
201 return false;
202 else
203 return t1->tv_usec >= t2->tv_usec;
206 static inline bool
207 tv_gt (const struct timeval *t1, const struct timeval *t2)
209 if (t1->tv_sec > t2->tv_sec)
210 return true;
211 else if (t1->tv_sec < t2->tv_sec)
212 return false;
213 else
214 return t1->tv_usec > t2->tv_usec;
217 static inline bool
218 tv_eq (const struct timeval *t1, const struct timeval *t2)
220 return t1->tv_sec == t2->tv_sec && t1->tv_usec == t2->tv_usec;
223 static inline void
224 tv_delta (struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
226 int sec = t2->tv_sec - t1->tv_sec;
227 int usec = t2->tv_usec - t1->tv_usec;
229 while (usec < 0)
231 usec += 1000000;
232 sec -= 1;
235 if (sec < 0)
236 usec = sec = 0;
238 dest->tv_sec = sec;
239 dest->tv_usec = usec;
242 #define TV_WITHIN_SIGMA_MAX_SEC 600
243 #define TV_WITHIN_SIGMA_MAX_USEC (TV_WITHIN_SIGMA_MAX_SEC * 1000000)
246 * Is t1 and t2 within sigma microseconds of each other?
248 static inline bool
249 tv_within_sigma (const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
251 const int delta = tv_subtract (t1, t2, TV_WITHIN_SIGMA_MAX_SEC); /* sigma should be less than 10 minutes */
252 return -(int)sigma <= delta && delta <= (int)sigma;
256 * Used to determine in how many seconds we should be
257 * called again.
259 static inline void
260 interval_earliest_wakeup (interval_t *wakeup, time_t at, time_t current) {
261 if (at > current)
263 const interval_t delta = (interval_t) (at - current);
264 if (delta < *wakeup)
265 *wakeup = delta;
266 if (*wakeup < 0)
267 *wakeup = 0;
271 #endif