forked from VrayoSystems/vtrunkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfd_zlib.c
191 lines (157 loc) · 4.2 KB
/
lfd_zlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
vtrunkd - Virtual Tunnel Trunking over TCP/IP network.
Copyright (C) 2011-2016 Vrayo Systems Ltd. team
Vtrunkd has been derived from VTUN package by Maxim Krasnyansky.
vtun Copyright (C) 1998-2000 Maxim Krasnyansky <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
* lfd_zlib.c,v 1.1.1.2.2.6.2.1 2006/11/16 04:03:14 mtbishop Exp
*/
/* ZLIB compression module */
#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <syslog.h>
#include "vtun.h"
#include "linkfd.h"
#include "lib.h"
#include "log.h"
#ifdef HAVE_ZLIB
#include <zlib.h>
static z_stream zi, zd;
static unsigned char *zbuf;
static int zbuf_size = VTUN_FRAME_SIZE + 200;
/*
* Initialize compressor/decompressor.
* Allocate the buffer.
*/
int zlib_alloc(struct vtun_host *host)
{
int zlevel = host->zlevel ? host->zlevel : 1;
zd.zalloc = (alloc_func)0;
zd.zfree = (free_func)0;
zd.opaque = (voidpf)0;
zi.zalloc = (alloc_func)0;
zi.zfree = (free_func)0;
zi.opaque = (voidpf)0;
if( deflateInit(&zd, zlevel ) != Z_OK ){
vlog(LOG_ERR,"Can't initialize compressor");
return 1;
}
if( inflateInit(&zi) != Z_OK ){
vlog(LOG_ERR,"Can't initialize decompressor");
return 1;
}
if( !(zbuf = (void *) lfd_alloc(zbuf_size)) ){
vlog(LOG_ERR,"Can't allocate buffer for the compressor");
return 1;
}
vlog(LOG_INFO,"ZLIB compression[level %d] initialized.", zlevel);
return 0;
}
/*
* Deinitialize compressor/decompressor.
* Free the buffer.
*/
int zlib_free()
{
deflateEnd(&zd);
inflateEnd(&zi);
lfd_free(zbuf); zbuf = NULL;
return 0;
}
static int expand_zbuf(z_stream *zs, int len)
{
if( !(zbuf = lfd_realloc(zbuf,zbuf_size+len)) )
return -1;
zs->next_out = zbuf + zbuf_size;
zs->avail_out = len;
zbuf_size += len;
return 0;
}
/*
* This functions _MUST_ consume all incoming bytes in one pass,
* That's why we expand buffer dynamically.
* Practice shows that buffer will not grow larger that 16K.
*/
int zlib_comp(int len, char *in, char **out)
{
int oavail, olen = 0;
int err;
zd.next_in = (void *) in;
zd.avail_in = len;
zd.next_out = (void *) zbuf;
zd.avail_out = zbuf_size;
while(1) {
oavail = zd.avail_out;
if( (err=deflate(&zd, Z_SYNC_FLUSH)) != Z_OK ){
vlog(LOG_ERR,"Deflate error %d",err);
return -1;
}
olen += oavail - zd.avail_out;
if(!zd.avail_in)
break;
if( expand_zbuf(&zd,100) ) {
vlog( LOG_ERR, "Can't expand compression buffer");
return -1;
}
}
*out = (void *) zbuf;
return olen;
}
int zlib_decomp(int len, char *in, char **out)
{
int oavail = 0, olen = 0;
int err;
zi.next_in = (void *) in;
zi.avail_in = len;
zi.next_out = (void *) zbuf;
zi.avail_out = zbuf_size;
while(1) {
oavail = zi.avail_out;
if( (err=inflate(&zi, Z_SYNC_FLUSH)) != Z_OK ) {
vlog(LOG_ERR,"Inflate error %d len %d", err, len);
return -1;
}
olen += oavail - zi.avail_out;
if(!zi.avail_in)
break;
if( expand_zbuf(&zi,100) ) {
vlog( LOG_ERR, "Can't expand compression buffer");
return -1;
}
}
*out = (void *) zbuf;
return olen;
}
struct lfd_mod lfd_zlib = {
"ZLIB",
zlib_alloc,
zlib_comp,
NULL,
zlib_decomp,
NULL,
zlib_free,
NULL,
NULL
};
#else /* HAVE_ZLIB */
int no_zlib(struct vtun_host *host)
{
vlog(LOG_INFO, "ZLIB compression is not supported");
return -1;
}
struct lfd_mod lfd_zlib = {
"ZLIB",
no_zlib, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
#endif /* HAVE_ZLIB */