OpenVPN
lzo.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-2024 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
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "syshead.h"
34
35#if defined(ENABLE_LZO)
36
37#include "comp.h"
38#include "error.h"
39#include "otime.h"
40
41#include "memdbg.h"
42
43
44static void
45lzo_compress_init(struct compress_context *compctx)
46{
47 msg(D_INIT_MEDIUM, "LZO compression initializing");
48 ASSERT(!(compctx->flags & COMP_F_SWAP));
49 compctx->wu.lzo.wmem_size = LZO_WORKSPACE;
50
51 int lzo_status = lzo_init();
52 if (lzo_status != LZO_E_OK)
53 {
54 msg(M_FATAL, "Cannot initialize LZO compression library (lzo_init() returns %d)", lzo_status);
55 }
56 compctx->wu.lzo.wmem = (lzo_voidp) malloc(compctx->wu.lzo.wmem_size);
57 check_malloc_return(compctx->wu.lzo.wmem);
58}
59
60static void
61lzo_compress_uninit(struct compress_context *compctx)
62{
63 free(compctx->wu.lzo.wmem);
64 compctx->wu.lzo.wmem = NULL;
65}
66
67static void
68lzo_compress(struct buffer *buf, struct buffer work,
69 struct compress_context *compctx,
70 const struct frame *frame)
71{
72 uint8_t *header = buf_prepend(buf, 1);
73 *header = NO_COMPRESS_BYTE;
74}
75
76static void
77lzo_decompress(struct buffer *buf, struct buffer work,
78 struct compress_context *compctx,
79 const struct frame *frame)
80{
81 lzo_uint zlen = frame->buf.payload_size;
82 int err;
83 uint8_t c; /* flag indicating whether or not our peer compressed */
84
85 if (buf->len <= 0)
86 {
87 return;
88 }
89
91
92 c = *BPTR(buf);
93 ASSERT(buf_advance(buf, 1));
94
95 if (c == LZO_COMPRESS_BYTE) /* packet was compressed */
96 {
97 ASSERT(buf_safe(&work, zlen));
98 err = LZO_DECOMPRESS(BPTR(buf), BLEN(buf), BPTR(&work), &zlen,
99 compctx->wu.lzo.wmem);
100 if (err != LZO_E_OK)
101 {
102 dmsg(D_COMP_ERRORS, "LZO decompression error: %d", err);
103 buf->len = 0;
104 return;
105 }
106
107 ASSERT(buf_safe(&work, zlen));
108 work.len = zlen;
109
110 dmsg(D_COMP, "LZO decompress %d -> %d", buf->len, work.len);
111 compctx->pre_decompress += buf->len;
112 compctx->post_decompress += work.len;
113
114 *buf = work;
115 }
116 else if (c == NO_COMPRESS_BYTE) /* packet was not compressed */
117 {
118 /* nothing to do */
119 }
120 else
121 {
122 dmsg(D_COMP_ERRORS, "Bad LZO decompression header byte: %d", c);
123 buf->len = 0;
124 }
125}
126
127const struct compress_alg lzo_alg = {
128 "lzo",
129 lzo_compress_init,
130 lzo_compress_uninit,
131 lzo_compress,
132 lzo_decompress
133};
134#endif /* ENABLE_LZO */
#define BPTR(buf)
Definition buffer.h:124
static bool buf_safe(const struct buffer *buf, size_t len)
Definition buffer.h:520
static bool buf_advance(struct buffer *buf, int size)
Definition buffer.h:618
static uint8_t * buf_prepend(struct buffer *buf, int size)
Definition buffer.h:606
#define BLEN(buf)
Definition buffer.h:127
static void check_malloc_return(void *p)
Definition buffer.h:1103
#define buf_init(buf, offset)
Definition buffer.h:209
#define COMP_F_SWAP
Definition comp.h:39
#define D_COMP_ERRORS
Definition errlevel.h:61
#define D_COMP
Definition errlevel.h:166
#define D_INIT_MEDIUM
Definition errlevel.h:104
#define M_FATAL
Definition error.h:89
#define dmsg(flags,...)
Definition error.h:148
#define msg(flags,...)
Definition error.h:144
#define ASSERT(x)
Definition error.h:195
Wrapper structure for dynamically allocated memory.
Definition buffer.h:61
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:66
Packet geometry parameters.
Definition mtu.h:98
int payload_size
the maximum size that a payload that our buffers can hold from either tun device or network link.
Definition mtu.h:102
int headroom
the headroom in the buffer, this is choosen to allow all potential header to be added before the pack...
Definition mtu.h:108
struct frame::@8 buf