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
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
26
* @file Data Channel Compression module function definitions.
31
#elif defined(_MSC_VER)
32
#include "config-msvc.h"
45
#ifndef ENABLE_LZO_STUB
47
* Perform adaptive compression housekeeping.
49
* @param ac the adaptive compression state structure.
54
lzo_adaptive_compress_test (struct lzo_adaptive_compress *ac)
56
const bool save = ac->compress_state;
57
const time_t local_now = now;
59
if (!ac->compress_state)
61
if (local_now >= ac->next)
63
if (ac->n_total > AC_MIN_BYTES
64
&& (ac->n_total - ac->n_comp) < (ac->n_total / (100 / AC_SAVE_PCT)))
66
ac->compress_state = true;
67
ac->next = local_now + AC_OFF_SEC;
71
ac->next = local_now + AC_SAMP_SEC;
73
dmsg (D_COMP, "lzo_adaptive_compress_test: comp=%d total=%d", ac->n_comp, ac->n_total);
74
ac->n_total = ac->n_comp = 0;
79
if (local_now >= ac->next)
81
ac->next = local_now + AC_SAMP_SEC;
82
ac->n_total = ac->n_comp = 0;
83
ac->compress_state = false;
87
if (ac->compress_state != save)
88
dmsg (D_COMP_LOW, "Adaptive compression state %s", (ac->compress_state ? "OFF" : "ON"));
90
return !ac->compress_state;
94
lzo_adaptive_compress_data (struct lzo_adaptive_compress *ac, int n_total, int n_comp)
96
ac->n_total += n_total;
100
#endif /* ENABLE_LZO_STUB */
102
void lzo_adjust_frame_parameters (struct frame *frame)
104
/* Leave room for our one-byte compressed/didn't-compress prefix byte. */
105
frame_add_to_extra_frame (frame, LZO_PREFIX_LEN);
107
/* Leave room for compression buffer to expand in worst case scenario
108
where data is totally uncompressible */
109
frame_add_to_extra_buffer (frame, LZO_EXTRA_BUFFER (EXPANDED_SIZE(frame)));
113
lzo_compress_init (struct lzo_compress_workspace *lzowork, unsigned int flags)
117
lzowork->flags = flags;
118
#ifndef ENABLE_LZO_STUB
119
lzowork->wmem_size = LZO_WORKSPACE;
121
if (lzo_init () != LZO_E_OK)
122
msg (M_FATAL, "Cannot initialize LZO compression library");
123
lzowork->wmem = (lzo_voidp) lzo_malloc (lzowork->wmem_size);
124
check_malloc_return (lzowork->wmem);
125
msg (D_INIT_MEDIUM, "LZO compression initialized");
127
msg (D_INIT_MEDIUM, "LZO stub compression initialized");
129
lzowork->defined = true;
133
lzo_compress_uninit (struct lzo_compress_workspace *lzowork)
137
ASSERT (lzowork->defined);
138
#ifndef ENABLE_LZO_STUB
139
lzo_free (lzowork->wmem);
140
lzowork->wmem = NULL;
142
lzowork->defined = false;
147
lzo_compression_enabled (struct lzo_compress_workspace *lzowork)
149
#ifndef ENABLE_LZO_STUB
150
if ((lzowork->flags & (LZO_SELECTED|LZO_ON)) == (LZO_SELECTED|LZO_ON))
152
if (lzowork->flags & LZO_ADAPTIVE)
153
return lzo_adaptive_compress_test (&lzowork->ac);
162
lzo_compress (struct buffer *buf, struct buffer work,
163
struct lzo_compress_workspace *lzowork,
164
const struct frame* frame)
166
#ifndef ENABLE_LZO_STUB
169
bool compressed = false;
172
ASSERT (lzowork->defined);
177
#ifndef ENABLE_LZO_STUB
179
* In order to attempt compression, length must be at least COMPRESS_THRESHOLD,
180
* and our adaptive level must give the OK.
182
if (buf->len >= COMPRESS_THRESHOLD && lzo_compression_enabled (lzowork))
184
ASSERT (buf_init (&work, FRAME_HEADROOM (frame)));
185
ASSERT (buf_safe (&work, LZO_EXTRA_BUFFER (PAYLOAD_SIZE (frame))));
187
if (!(buf->len <= PAYLOAD_SIZE (frame)))
189
dmsg (D_COMP_ERRORS, "LZO compression buffer overflow");
194
err = LZO_COMPRESS (BPTR (buf), BLEN (buf), BPTR (&work), &zlen, lzowork->wmem);
197
dmsg (D_COMP_ERRORS, "LZO compression error: %d", err);
202
ASSERT (buf_safe (&work, zlen));
206
dmsg (D_COMP, "compress %d -> %d", buf->len, work.len);
207
lzowork->pre_compress += buf->len;
208
lzowork->post_compress += work.len;
210
/* tell adaptive level about our success or lack thereof in getting any size reduction */
211
if (lzowork->flags & LZO_ADAPTIVE)
212
lzo_adaptive_compress_data (&lzowork->ac, buf->len, work.len);
215
/* did compression save us anything ? */
216
if (compressed && work.len < buf->len)
218
uint8_t *header = buf_prepend (&work, 1);
219
*header = YES_COMPRESS;
225
uint8_t *header = buf_prepend (buf, 1);
226
*header = NO_COMPRESS;
231
lzo_decompress (struct buffer *buf, struct buffer work,
232
struct lzo_compress_workspace *lzowork,
233
const struct frame* frame)
235
#ifndef ENABLE_LZO_STUB
236
lzo_uint zlen = EXPANDED_SIZE (frame);
239
uint8_t c; /* flag indicating whether or not our peer compressed */
241
ASSERT (lzowork->defined);
246
ASSERT (buf_init (&work, FRAME_HEADROOM (frame)));
249
ASSERT (buf_advance (buf, 1));
251
if (c == YES_COMPRESS) /* packet was compressed */
253
#ifndef ENABLE_LZO_STUB
254
ASSERT (buf_safe (&work, zlen));
255
err = LZO_DECOMPRESS (BPTR (buf), BLEN (buf), BPTR (&work), &zlen,
259
dmsg (D_COMP_ERRORS, "LZO decompression error: %d", err);
264
ASSERT (buf_safe (&work, zlen));
267
dmsg (D_COMP, "decompress %d -> %d", buf->len, work.len);
268
lzowork->pre_decompress += buf->len;
269
lzowork->post_decompress += work.len;
273
dmsg (D_COMP_ERRORS, "LZO decompression error: LZO capability not compiled");
278
else if (c == NO_COMPRESS) /* packet was not compressed */
284
dmsg (D_COMP_ERRORS, "Bad LZO decompression header byte: %d", c);
290
lzo_modify_flags (struct lzo_compress_workspace *lzowork, unsigned int flags)
292
ASSERT (lzowork->defined);
293
lzowork->flags = flags;
296
void lzo_print_stats (const struct lzo_compress_workspace *lzo_compwork, struct status_output *so)
298
ASSERT (lzo_compwork->defined);
300
#ifndef ENABLE_LZO_STUB
301
status_printf (so, "pre-compress bytes," counter_format, lzo_compwork->pre_compress);
302
status_printf (so, "post-compress bytes," counter_format, lzo_compwork->post_compress);
303
status_printf (so, "pre-decompress bytes," counter_format, lzo_compwork->pre_decompress);
304
status_printf (so, "post-decompress bytes," counter_format, lzo_compwork->post_decompress);
309
static void dummy(void) {}
310
#endif /* ENABLE_LZO */