OpenVPN
shaper.c
Go to the documentation of this file.
1 /*
2  * OpenVPN -- An application to securely tunnel IP networks
3  * over a single 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-2018 OpenVPN 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 along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #elif defined(_MSC_VER)
27 #include "config-msvc.h"
28 #endif
29 
30 #include "syshead.h"
31 #include "shaper.h"
32 #include "memdbg.h"
33 
34 #ifdef ENABLE_FEATURE_SHAPER
35 
36 /*
37  * We want to wake up in delay microseconds. If timeval is larger
38  * than delay, set timeval to delay.
39  */
40 bool
41 shaper_soonest_event(struct timeval *tv, int delay)
42 {
43  bool ret = false;
44  if (delay < 1000000)
45  {
46  if (tv->tv_sec)
47  {
48  tv->tv_sec = 0;
49  tv->tv_usec = delay;
50  ret = true;
51  }
52  else if (delay < tv->tv_usec)
53  {
54  tv->tv_usec = delay;
55  ret = true;
56  }
57  }
58  else
59  {
60  const int sec = delay / 1000000;
61  const int usec = delay % 1000000;
62 
63  if (sec < tv->tv_sec)
64  {
65  tv->tv_sec = sec;
66  tv->tv_usec = usec;
67  ret = true;
68  }
69  else if (sec == tv->tv_sec)
70  {
71  if (usec < tv->tv_usec)
72  {
73  tv->tv_usec = usec;
74  ret = true;
75  }
76  }
77  }
78 #ifdef SHAPER_DEBUG
79  dmsg(D_SHAPER_DEBUG, "SHAPER shaper_soonest_event sec=%" PRIi64 " usec=%ld ret=%d",
80  (int64_t)tv->tv_sec, (long)tv->tv_usec, (int)ret);
81 #endif
82  return ret;
83 }
84 
85 void
86 shaper_reset_wakeup(struct shaper *s)
87 {
88  CLEAR(s->wakeup);
89 }
90 
91 void
92 shaper_msg(struct shaper *s)
93 {
94  msg(M_INFO, "Output Traffic Shaping initialized at %d bytes per second",
95  s->bytes_per_second);
96 }
97 
98 #else /* ifdef ENABLE_FEATURE_SHAPER */
99 static void
100 dummy(void)
101 {
102 }
103 #endif /* ENABLE_FEATURE_SHAPER */
#define M_INFO
Definition: errlevel.h:55
#define dmsg
Definition: error.h:174
static void dummy(void)
Definition: comp-lz4.c:319
#define D_SHAPER_DEBUG
Definition: errlevel.h:170
#define CLEAR(x)
Definition: basic.h:33
void shaper_reset_wakeup(struct shaper *s)
Definition: shaper.c:86
#define msg
Definition: error.h:173
bool shaper_soonest_event(struct timeval *tv, int delay)
Definition: shaper.c:41
__int64 int64_t
Definition: config-msvc.h:124
void shaper_msg(struct shaper *s)
Definition: shaper.c:92