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
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
33 struct frequency_limit
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
);
46 int gettimeofday(struct timeval
*tv
, void *tz
);
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) */
62 const time_t real_time
= time (NULL
);
68 tv_clear (struct timeval
*tv
)
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 */
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))
89 else if (sec_diff
< -((int)max_seconds
+ 10))
91 return constrain_int (sec_diff
* 1000000 + (tv1
->tv_usec
- tv2
->tv_usec
), -max_usec
, max_usec
);
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;
109 tv_lt (const struct timeval
*t1
, const struct timeval
*t2
)
111 if (t1
->tv_sec
< t2
->tv_sec
)
113 else if (t1
->tv_sec
> t2
->tv_sec
)
116 return t1
->tv_usec
< t2
->tv_usec
;
120 tv_le (const struct timeval
*t1
, const struct timeval
*t2
)
122 if (t1
->tv_sec
< t2
->tv_sec
)
124 else if (t1
->tv_sec
> t2
->tv_sec
)
127 return t1
->tv_usec
<= t2
->tv_usec
;
131 tv_ge (const struct timeval
*t1
, const struct timeval
*t2
)
133 if (t1
->tv_sec
> t2
->tv_sec
)
135 else if (t1
->tv_sec
< t2
->tv_sec
)
138 return t1
->tv_usec
>= t2
->tv_usec
;
142 tv_gt (const struct timeval
*t1
, const struct timeval
*t2
)
144 if (t1
->tv_sec
> t2
->tv_sec
)
146 else if (t1
->tv_sec
< t2
->tv_sec
)
149 return t1
->tv_usec
> t2
->tv_usec
;
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
;
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
;
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?
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
195 interval_earliest_wakeup (interval_t
*wakeup
, time_t at
, time_t current
) {
198 const interval_t delta
= (interval_t
) (at
- current
);