~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to src/openvpn/otime.c

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
7
 *
 
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
9
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
23
 */
 
24
 
 
25
#ifdef HAVE_CONFIG_H
 
26
#include "config.h"
 
27
#elif defined(_MSC_VER)
 
28
#include "config-msvc.h"
 
29
#endif
 
30
 
 
31
#include "syshead.h"
 
32
 
 
33
#include "otime.h"
 
34
 
 
35
#include "memdbg.h"
 
36
 
 
37
time_t now = 0;            /* GLOBAL */
 
38
 
 
39
#if TIME_BACKTRACK_PROTECTION
 
40
 
 
41
static time_t now_adj = 0; /* GLOBAL */
 
42
time_t now_usec = 0;       /* GLOBAL */
 
43
 
 
44
/*
 
45
 * Try to filter out time instability caused by the system
 
46
 * clock backtracking or jumping forward.
 
47
 */
 
48
 
 
49
void
 
50
update_now (const time_t system_time)
 
51
{
 
52
  const int forward_threshold = 86400; /* threshold at which to dampen forward jumps */
 
53
  const int backward_trigger  = 10;    /* backward jump must be >= this many seconds before we adjust */
 
54
  time_t real_time = system_time + now_adj;
 
55
 
 
56
  if (real_time > now)
 
57
    {
 
58
      const time_t overshoot = real_time - now - 1;
 
59
      if (overshoot > forward_threshold && now_adj >= overshoot)
 
60
        {
 
61
          now_adj -= overshoot;
 
62
          real_time -= overshoot;
 
63
        }
 
64
      now = real_time;
 
65
    }
 
66
  else if (real_time < now - backward_trigger)
 
67
    now_adj += (now - real_time);
 
68
}
 
69
 
 
70
void
 
71
update_now_usec (struct timeval *tv)
 
72
{
 
73
  const time_t last = now;
 
74
  update_now (tv->tv_sec);
 
75
  if (now > last || (now == last && tv->tv_usec > now_usec))
 
76
    now_usec = tv->tv_usec;
 
77
}
 
78
 
 
79
#endif /* TIME_BACKTRACK_PROTECTION */
 
80
 
 
81
/* 
 
82
 * Return a numerical string describing a struct timeval.
 
83
 */
 
84
const char *
 
85
tv_string (const struct timeval *tv, struct gc_arena *gc)
 
86
{
 
87
  struct buffer out = alloc_buf_gc (64, gc);
 
88
  buf_printf (&out, "[%d/%d]",
 
89
              (int) tv->tv_sec,
 
90
              (int )tv->tv_usec);
 
91
  return BSTR (&out);
 
92
}
 
93
 
 
94
/* 
 
95
 * Return an ascii string describing an absolute
 
96
 * date/time in a struct timeval.
 
97
 * 
 
98
 */
 
99
const char *
 
100
tv_string_abs (const struct timeval *tv, struct gc_arena *gc)
 
101
{
 
102
  return time_string ((time_t) tv->tv_sec,
 
103
                      (int) tv->tv_usec,
 
104
                      true,
 
105
                      gc);
 
106
}
 
107
 
 
108
/* format a time_t as ascii, or use current time if 0 */
 
109
 
 
110
const char *
 
111
time_string (time_t t, int usec, bool show_usec, struct gc_arena *gc)
 
112
{
 
113
  struct buffer out = alloc_buf_gc (64, gc);
 
114
  struct timeval tv;
 
115
 
 
116
  if (t)
 
117
    {
 
118
      tv.tv_sec = t;
 
119
      tv.tv_usec = usec;
 
120
    }
 
121
  else
 
122
    {
 
123
      gettimeofday (&tv, NULL);
 
124
    }
 
125
 
 
126
  t = tv.tv_sec;
 
127
  buf_printf (&out, "%s", ctime(&t));
 
128
  buf_rmtail (&out, '\n');
 
129
 
 
130
  if (show_usec && tv.tv_usec)
 
131
    buf_printf (&out, " us=%d", (int)tv.tv_usec);
 
132
 
 
133
  return BSTR (&out);
 
134
}
 
135
 
 
136
/*
 
137
 * Limit the frequency of an event stream.
 
138
 *
 
139
 * Used to control maximum rate of new
 
140
 * incoming connections.
 
141
 */
 
142
 
 
143
struct frequency_limit *
 
144
frequency_limit_init (int max, int per)
 
145
{
 
146
  struct frequency_limit *f;
 
147
 
 
148
  ASSERT (max >= 0 && per >= 0);
 
149
 
 
150
  ALLOC_OBJ (f, struct frequency_limit);
 
151
  f->max = max;
 
152
  f->per = per;
 
153
  f->n = 0;
 
154
  f->reset = 0;
 
155
  return f;
 
156
}
 
157
 
 
158
void
 
159
frequency_limit_free (struct frequency_limit *f)
 
160
{
 
161
  free (f);
 
162
}
 
163
 
 
164
bool
 
165
frequency_limit_event_allowed (struct frequency_limit *f)
 
166
{
 
167
  if (f->per)
 
168
    {
 
169
      bool ret;
 
170
      if (now >= f->reset + f->per)
 
171
        {
 
172
          f->reset = now;
 
173
          f->n = 0;
 
174
        }
 
175
      ret = (++f->n <= f->max);
 
176
      return ret;
 
177
    }
 
178
  else
 
179
    return true;
 
180
}
 
181
 
 
182
#ifdef TIME_TEST
 
183
void
 
184
time_test (void)
 
185
{
 
186
  struct timeval tv;
 
187
  time_t t;
 
188
  int i;
 
189
  for (i = 0; i < 10000; ++i)
 
190
    {
 
191
      t = time(NULL);
 
192
      gettimeofday (&tv, NULL);
 
193
#if 1
 
194
      msg (M_INFO, "t=%u s=%u us=%u",
 
195
               (unsigned int)t,
 
196
               (unsigned int)tv.tv_sec,
 
197
               (unsigned int)tv.tv_usec);
 
198
#endif
 
199
    }
 
200
}
 
201
#endif