OpenVPN
compstub.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 
32 #if defined(USE_COMP)
33 
34 #include "comp.h"
35 #include "error.h"
36 #include "otime.h"
37 
38 #include "memdbg.h"
39 
40 static void
41 stub_compress_init(struct compress_context *compctx)
42 {
43 }
44 
45 static void
46 stub_compress_uninit(struct compress_context *compctx)
47 {
48 }
49 
50 static void
51 stub_compress(struct buffer *buf, struct buffer work,
52  struct compress_context *compctx,
53  const struct frame *frame)
54 {
55  if (buf->len <= 0)
56  {
57  return;
58  }
59  if (compctx->flags & COMP_F_SWAP)
60  {
61  uint8_t *head = BPTR(buf);
62  uint8_t *tail = BEND(buf);
63  ASSERT(buf_safe(buf, 1));
64  ++buf->len;
65 
66  /* move head byte of payload to tail */
67  *tail = *head;
68  *head = NO_COMPRESS_BYTE_SWAP;
69  }
70  else
71  {
72  uint8_t *header = buf_prepend(buf, 1);
73  *header = NO_COMPRESS_BYTE;
74  }
75 }
76 
77 static void
78 stub_decompress(struct buffer *buf, struct buffer work,
79  struct compress_context *compctx,
80  const struct frame *frame)
81 {
82  uint8_t c;
83  if (buf->len <= 0)
84  {
85  return;
86  }
87  if (compctx->flags & COMP_F_SWAP)
88  {
89  uint8_t *head = BPTR(buf);
90  c = *head;
91  --buf->len;
92  *head = *BEND(buf);
93  if (c != NO_COMPRESS_BYTE_SWAP)
94  {
95  dmsg(D_COMP_ERRORS, "Bad compression stub (swap) decompression header byte: %d", c);
96  buf->len = 0;
97  }
98  }
99  else
100  {
101  c = *BPTR(buf);
102  ASSERT(buf_advance(buf, 1));
103  if (c != NO_COMPRESS_BYTE)
104  {
105  dmsg(D_COMP_ERRORS, "Bad compression stub decompression header byte: %d", c);
106  buf->len = 0;
107  }
108  }
109 }
110 
111 
112 static void
113 stubv2_compress(struct buffer *buf, struct buffer work,
114  struct compress_context *compctx,
115  const struct frame *frame)
116 {
117  if (buf->len <= 0)
118  {
119  return;
120  }
121 
122  compv2_escape_data_ifneeded(buf);
123 }
124 
125 static void
126 stubv2_decompress(struct buffer *buf, struct buffer work,
127  struct compress_context *compctx,
128  const struct frame *frame)
129 {
130  if (buf->len <= 0)
131  {
132  return;
133  }
134 
135  uint8_t *head = BPTR(buf);
136 
137  /* no compression or packet to short*/
138  if (head[0] != COMP_ALGV2_INDICATOR_BYTE)
139  {
140  return;
141  }
142 
143  /* compression header (0x50) is present */
144  buf_advance(buf, 1);
145 
146  /* Packet buffer too short (only 1 byte) */
147  if (buf->len <= 0)
148  {
149  return;
150  }
151 
152  head = BPTR(buf);
153  buf_advance(buf, 1);
154 
155  if (head[0] != COMP_ALGV2_UNCOMPRESSED_BYTE)
156  {
157  dmsg(D_COMP_ERRORS, "Bad compression stubv2 decompression header byte: %d", *head);
158  buf->len = 0;
159  return;
160  }
161 }
162 
163 const struct compress_alg compv2_stub_alg = {
164  "stubv2",
165  stub_compress_init,
166  stub_compress_uninit,
167  stubv2_compress,
168  stubv2_decompress
169 };
170 
171 const struct compress_alg comp_stub_alg = {
172  "stub",
173  stub_compress_init,
174  stub_compress_uninit,
175  stub_compress,
176  stub_decompress
177 };
178 
179 #else /* if defined(USE_COMP) */
180 static void
181 dummy(void)
182 {
183 }
184 #endif /* USE_STUB */
static bool buf_advance(struct buffer *buf, int size)
Definition: buffer.h:639
Packet geometry parameters.
Definition: mtu.h:93
#define D_COMP_ERRORS
Definition: errlevel.h:61
#define dmsg
Definition: error.h:174
static bool buf_safe(const struct buffer *buf, int len)
Definition: buffer.h:541
#define ASSERT(x)
Definition: error.h:221
int len
Length in bytes of the actual content within the allocated memory.
Definition: buffer.h:66
#define BPTR(buf)
Definition: buffer.h:124
static uint8_t * buf_prepend(struct buffer *buf, int size)
Definition: buffer.h:627
unsigned __int8 uint8_t
Definition: config-msvc.h:123
static void dummy(void)
Definition: compstub.c:181
Wrapper structure for dynamically allocated memory.
Definition: buffer.h:60
#define BEND(buf)
Definition: buffer.h:125