svn cleanup
[anytun.git] / openvpn / otime.h
blob974d0afe07b21b76f48b45f220946335209dede8
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-2005 OpenVPN Solutions LLC <info@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"
31 #include "thread.h"
33 struct frequency_limit
35 int max;
36 int per;
37 int n;
38 time_t reset;
41 struct frequency_limit *frequency_limit_init (int max, int per);
42 void frequency_limit_free (struct frequency_limit *f);
43 bool frequency_limit_event_allowed (struct frequency_limit *f);
45 #ifdef WIN32
46 int gettimeofday(struct timeval *tv, void *tz);
47 #endif
49 /* format a time_t as ascii, or use current time if 0 */
50 const char* time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc);
52 /* struct timeval functions */
54 const char *tv_string (const struct timeval *tv, struct gc_arena *gc);
55 const char *tv_string_abs (const struct timeval *tv, struct gc_arena *gc);
57 extern volatile time_t now; /* updated frequently to time(NULL) */
59 static inline void
60 update_time (void)
62 const time_t real_time = time (NULL);
63 if (real_time != now)
64 now = real_time;
67 static inline void
68 tv_clear (struct timeval *tv)
70 tv->tv_sec = 0;
71 tv->tv_usec = 0;
74 static inline bool
75 tv_defined (const struct timeval *tv)
77 return tv->tv_sec > 0 && tv->tv_usec > 0;
80 /* return tv1 - tv2 in usec, constrained by max_seconds */
81 static inline int
82 tv_subtract (const struct timeval *tv1, const struct timeval *tv2, const unsigned int max_seconds)
84 const int max_usec = max_seconds * 1000000;
85 const int sec_diff = tv1->tv_sec - tv2->tv_sec;
87 if (sec_diff > ((int)max_seconds + 10))
88 return max_usec;
89 else if (sec_diff < -((int)max_seconds + 10))
90 return -max_usec;
91 return constrain_int (sec_diff * 1000000 + (tv1->tv_usec - tv2->tv_usec), -max_usec, max_usec);
94 static inline void
95 tv_add (struct timeval *dest, const struct timeval *src)
97 dest->tv_sec += src->tv_sec;
98 dest->tv_usec += src->tv_usec;
99 dest->tv_sec += (dest->tv_usec >> 20);
100 dest->tv_usec &= 0x000FFFFF;
101 if (dest->tv_usec >= 1000000)
103 dest->tv_usec -= 1000000;
104 dest->tv_sec += 1;
108 static inline bool
109 tv_lt (const struct timeval *t1, const struct timeval *t2)
111 if (t1->tv_sec < t2->tv_sec)
112 return true;
113 else if (t1->tv_sec > t2->tv_sec)
114 return false;
115 else
116 return t1->tv_usec < t2->tv_usec;
119 static inline bool
120 tv_le (const struct timeval *t1, const struct timeval *t2)
122 if (t1->tv_sec < t2->tv_sec)
123 return true;
124 else if (t1->tv_sec > t2->tv_sec)
125 return false;
126 else
127 return t1->tv_usec <= t2->tv_usec;
130 static inline bool
131 tv_ge (const struct timeval *t1, const struct timeval *t2)
133 if (t1->tv_sec > t2->tv_sec)
134 return true;
135 else if (t1->tv_sec < t2->tv_sec)
136 return false;
137 else
138 return t1->tv_usec >= t2->tv_usec;
141 static inline bool
142 tv_gt (const struct timeval *t1, const struct timeval *t2)
144 if (t1->tv_sec > t2->tv_sec)
145 return true;
146 else if (t1->tv_sec < t2->tv_sec)
147 return false;
148 else
149 return t1->tv_usec > t2->tv_usec;
152 static inline bool
153 tv_eq (const struct timeval *t1, const struct timeval *t2)
155 return t1->tv_sec == t2->tv_sec && t1->tv_usec == t2->tv_usec;
158 static inline void
159 tv_delta (struct timeval *dest, const struct timeval *t1, const struct timeval *t2)
161 int sec = t2->tv_sec - t1->tv_sec;
162 int usec = t2->tv_usec - t1->tv_usec;
164 while (usec < 0)
166 usec += 1000000;
167 sec -= 1;
170 if (sec < 0)
171 usec = sec = 0;
173 dest->tv_sec = sec;
174 dest->tv_usec = usec;
177 #define TV_WITHIN_SIGMA_MAX_SEC 600
178 #define TV_WITHIN_SIGMA_MAX_USEC (TV_WITHIN_SIGMA_MAX_SEC * 1000000)
181 * Is t1 and t2 within sigma microseconds of each other?
183 static inline bool
184 tv_within_sigma (const struct timeval *t1, const struct timeval *t2, unsigned int sigma)
186 const int delta = tv_subtract (t1, t2, TV_WITHIN_SIGMA_MAX_SEC); /* sigma should be less than 10 minutes */
187 return -(int)sigma <= delta && delta <= (int)sigma;
191 * Used to determine in how many seconds we should be
192 * called again.
194 static inline void
195 interval_earliest_wakeup (interval_t *wakeup, time_t at, time_t current) {
196 if (at > current)
198 const interval_t delta = (interval_t) (at - current);
199 if (delta < *wakeup)
200 *wakeup = delta;
201 if (*wakeup < 0)
202 *wakeup = 0;
206 #endif