Browse code

gzcompress: free old outbound buffer inside SREV_NET_DATA_OUT callback

Daniel-Constantin Mierla authored on 12/08/2019 09:18:21
Showing 1 changed files
... ...
@@ -338,6 +338,7 @@ int gzc_msg_sent(sr_event_param_t *evp)
338 338
 	unsigned long olen;
339 339
 	unsigned long nlen;
340 340
 	int ret;
341
+	str nbuf = STR_NULL;
341 342
 
342 343
 	obuf = (str*)evp->data;
343 344
 	memset(&msg, 0, sizeof(sip_msg_t));
... ...
@@ -383,7 +384,15 @@ int gzc_msg_sent(sr_event_param_t *evp)
383 384
 		goto done;
384 385
 	}
385 386
 
386
-	obuf->s = gzc_msg_update(&msg, (unsigned int*)&obuf->len);
387
+	nbuf.s = gzc_msg_update(&msg, (unsigned int*)&nbuf.len);
388
+	if(nbuf.s!=NULL) {
389
+		LM_DBG("new outbound buffer generated\n");
390
+		pkg_free(obuf->s);
391
+		obuf->s = nbuf.s;
392
+		obuf->len = nbuf.len;
393
+	} else {
394
+		LM_ERR("failed to generate new outbound buffer\n");
395
+	}
387 396
 
388 397
 done:
389 398
 	free_sip_msg(&msg);
Browse code

gzcompress: removed svn id line

Daniel-Constantin Mierla authored on 04/02/2019 19:04:20
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /**
2
- * $Id$
3
- *
4 2
  * Copyright (C) 2013 Daniel-Constantin Mierla (asipto.com)
5 3
  *
6 4
  * This file is part of kamailio, a free SIP server.
... ...
@@ -391,4 +389,3 @@ done:
391 389
 	free_sip_msg(&msg);
392 390
 	return 0;
393 391
 }
394
-
Browse code

gzcompress: adapt to new module interface

Henning Westerholt authored on 27/09/2018 15:38:39 • Victor Seva committed on 28/09/2018 11:03:26
Showing 1 changed files
... ...
@@ -81,18 +81,16 @@ static param_export_t params[]={
81 81
 
82 82
 /** module exports */
83 83
 struct module_exports exports= {
84
-	"gzcompress",
84
+	"gzcompress",    /* module name */
85 85
 	DEFAULT_DLFLAGS, /* dlopen flags */
86
-	0,
87
-	params,
88
-	0,          /* exported statistics */
89
-	0,          /* exported MI functions */
90
-	0,          /* exported pseudo-variables */
91
-	0,          /* extra processes */
92
-	mod_init,   /* module initialization function */
93
-	0,
94
-	0,
95
-	0           /* per-child init function */
86
+	0,               /* cmd (cfg function) exports */
87
+	params,          /* param exports */
88
+	0,               /* RPC method exports */
89
+	0,               /* pseudo-variables exports */
90
+	0,               /* response handling function */
91
+	mod_init,        /* module init function */
92
+	0,               /* per-child init function */
93
+	0                /* module destroy function */
96 94
 };
97 95
 
98 96
 /**
Browse code

gzcompress: updates for core events API changes

Daniel-Constantin Mierla authored on 14/06/2017 15:41:53
Showing 1 changed files
... ...
@@ -58,8 +58,8 @@
58 58
 MODULE_VERSION
59 59
 
60 60
 /** local functions */
61
-int gzc_msg_received(void *data);
62
-int gzc_msg_sent(void *data);
61
+int gzc_msg_received(sr_event_param_t *evp);
62
+int gzc_msg_sent(sr_event_param_t *evp);
63 63
 
64 64
 /** module parameters */
65 65
 static str _gzc_hdr_name = str_init("Content-Encoding");
... ...
@@ -108,7 +108,7 @@ static int mod_init(void)
108 108
 			goto error;
109 109
 		}
110 110
 	}
111
-	
111
+
112 112
 	sr_event_register_cb(SREV_NET_DATA_IN, gzc_msg_received);
113 113
 	sr_event_register_cb(SREV_NET_DATA_OUT, gzc_msg_sent);
114 114
 #ifdef USE_TCP
... ...
@@ -245,7 +245,7 @@ static char _gzc_local_buffer[BUF_SIZE];
245 245
 /**
246 246
  *
247 247
  */
248
-int gzc_msg_received(void *data)
248
+int gzc_msg_received(sr_event_param_t *evp)
249 249
 {
250 250
 	sip_msg_t msg;
251 251
 	str *obuf;
... ...
@@ -256,7 +256,7 @@ int gzc_msg_received(void *data)
256 256
 	unsigned long nlen;
257 257
 	int ret;
258 258
 
259
-	obuf = (str*)data;
259
+	obuf = (str*)evp->data;
260 260
 	memset(&msg, 0, sizeof(sip_msg_t));
261 261
 	msg.buf = obuf->s;
262 262
 	msg.len = obuf->len;
... ...
@@ -333,7 +333,7 @@ done:
333 333
 /**
334 334
  *
335 335
  */
336
-int gzc_msg_sent(void *data)
336
+int gzc_msg_sent(sr_event_param_t *evp)
337 337
 {
338 338
 	sip_msg_t msg;
339 339
 	str *obuf;
... ...
@@ -343,7 +343,7 @@ int gzc_msg_sent(void *data)
343 343
 	unsigned long nlen;
344 344
 	int ret;
345 345
 
346
-	obuf = (str*)data;
346
+	obuf = (str*)evp->data;
347 347
 	memset(&msg, 0, sizeof(sip_msg_t));
348 348
 	msg.buf = obuf->s;
349 349
 	msg.len = obuf->len;
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -41,17 +41,17 @@
41 41
 
42 42
 #include <zlib.h>
43 43
 
44
-#include "../../sr_module.h"
45
-#include "../../events.h"
46
-#include "../../dprint.h"
47
-#include "../../tcp_options.h"
48
-#include "../../ut.h"
49
-#include "../../forward.h"
50
-#include "../../msg_translator.h"
51
-#include "../../data_lump.h"
52
-#include "../../parser/msg_parser.h"
53
-#include "../../parser/parse_to.h"
54
-#include "../../parser/parse_from.h"
44
+#include "../../core/sr_module.h"
45
+#include "../../core/events.h"
46
+#include "../../core/dprint.h"
47
+#include "../../core/tcp_options.h"
48
+#include "../../core/ut.h"
49
+#include "../../core/forward.h"
50
+#include "../../core/msg_translator.h"
51
+#include "../../core/data_lump.h"
52
+#include "../../core/parser/msg_parser.h"
53
+#include "../../core/parser/parse_to.h"
54
+#include "../../core/parser/parse_from.h"
55 55
 
56 56
 #include "../../modules/sanity/api.h"
57 57
 
Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,396 @@
1
+/**
2
+ * $Id$
3
+ *
4
+ * Copyright (C) 2013 Daniel-Constantin Mierla (asipto.com)
5
+ *
6
+ * This file is part of kamailio, a free SIP server.
7
+ *
8
+ * Kamailio is free software; you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation; either version 2 of the License, or
11
+ * (at your option) any later version
12
+ *
13
+ * Kamailio is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program; if not, write to the Free Software
20
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21
+ */
22
+
23
+/*!
24
+ * \file
25
+ * \brief Kamailio gzcompress :: Module interface
26
+ * \ingroup gzcompress
27
+ * Module: \ref gzcompress
28
+ */
29
+
30
+/*! \defgroup gzcompress Kamailio :: compress-decompress message body with zlib
31
+ *
32
+ * This module compresses/decompresses SIP message body using zlib.
33
+ * The script interpreter gets the SIP messages decoded, so all
34
+ * existing functionality is preserved.
35
+ */
36
+
37
+#include <stdio.h>
38
+#include <string.h>
39
+#include <stdlib.h>
40
+#include <unistd.h>
41
+
42
+#include <zlib.h>
43
+
44
+#include "../../sr_module.h"
45
+#include "../../events.h"
46
+#include "../../dprint.h"
47
+#include "../../tcp_options.h"
48
+#include "../../ut.h"
49
+#include "../../forward.h"
50
+#include "../../msg_translator.h"
51
+#include "../../data_lump.h"
52
+#include "../../parser/msg_parser.h"
53
+#include "../../parser/parse_to.h"
54
+#include "../../parser/parse_from.h"
55
+
56
+#include "../../modules/sanity/api.h"
57
+
58
+MODULE_VERSION
59
+
60
+/** local functions */
61
+int gzc_msg_received(void *data);
62
+int gzc_msg_sent(void *data);
63
+
64
+/** module parameters */
65
+static str _gzc_hdr_name = str_init("Content-Encoding");
66
+static str _gzc_hdr_value = str_init("deflate");
67
+
68
+static int _gzc_sanity_checks = 0;
69
+static sanity_api_t scb = {0};
70
+
71
+/** module functions */
72
+static int mod_init(void);
73
+
74
+static param_export_t params[]={
75
+	{"header_name",		PARAM_STR, &_gzc_hdr_name},
76
+	{"header_value",	PARAM_STR, &_gzc_hdr_value},
77
+	{"sanity_checks",	PARAM_INT, &_gzc_sanity_checks},
78
+	{0,0,0}
79
+};
80
+
81
+
82
+/** module exports */
83
+struct module_exports exports= {
84
+	"gzcompress",
85
+	DEFAULT_DLFLAGS, /* dlopen flags */
86
+	0,
87
+	params,
88
+	0,          /* exported statistics */
89
+	0,          /* exported MI functions */
90
+	0,          /* exported pseudo-variables */
91
+	0,          /* extra processes */
92
+	mod_init,   /* module initialization function */
93
+	0,
94
+	0,
95
+	0           /* per-child init function */
96
+};
97
+
98
+/**
99
+ * init module function
100
+ */
101
+static int mod_init(void)
102
+{
103
+	if(_gzc_sanity_checks!=0)
104
+	{
105
+		if(sanity_load_api(&scb)<0)
106
+		{
107
+			LM_ERR("cannot bind to sanity module\n");
108
+			goto error;
109
+		}
110
+	}
111
+	
112
+	sr_event_register_cb(SREV_NET_DATA_IN, gzc_msg_received);
113
+	sr_event_register_cb(SREV_NET_DATA_OUT, gzc_msg_sent);
114
+#ifdef USE_TCP
115
+	tcp_set_clone_rcvbuf(1);
116
+#endif
117
+	return 0;
118
+error:
119
+	return -1;
120
+}
121
+
122
+/**
123
+ *
124
+ */
125
+int gzc_prepare_msg(sip_msg_t *msg)
126
+{
127
+	if (parse_msg(msg->buf, msg->len, msg)!=0)
128
+	{
129
+		LM_DBG("outbuf buffer parsing failed!");
130
+		return 1;
131
+	}
132
+
133
+	if(msg->first_line.type==SIP_REQUEST)
134
+	{
135
+		if(!IS_SIP(msg) && !IS_HTTP(msg))
136
+		{
137
+			LM_DBG("non sip or http request\n");
138
+			return 1;
139
+		}
140
+	} else if(msg->first_line.type==SIP_REPLY) {
141
+		if(!IS_SIP_REPLY(msg) && !IS_HTTP_REPLY(msg))
142
+		{
143
+			LM_DBG("non sip or http response\n");
144
+			return 1;
145
+		}
146
+	} else {
147
+		LM_DBG("non sip or http message\n");
148
+		return 1;
149
+	}
150
+
151
+	if (parse_headers(msg, HDR_EOH_F, 0)==-1)
152
+	{
153
+		LM_DBG("parsing headers failed");
154
+		return 2;
155
+	}
156
+
157
+	return 0;
158
+}
159
+
160
+/**
161
+ *
162
+ */
163
+int gzc_skip_msg(sip_msg_t *msg)
164
+{
165
+	hdr_field_t *h;
166
+	char *sp;
167
+
168
+	if(_gzc_hdr_name.len<=0 || _gzc_hdr_value.len<=0)
169
+		return -1;
170
+	h = get_hdr_by_name(msg, _gzc_hdr_name.s, _gzc_hdr_name.len);
171
+	if(h==NULL)
172
+		return 1;
173
+	
174
+	for (sp = h->body.s; sp <= h->body.s + h->body.len - _gzc_hdr_value.len;
175
+			sp++)
176
+	{
177
+        if (*sp == *_gzc_hdr_value.s
178
+        		&& memcmp(sp, _gzc_hdr_value.s, _gzc_hdr_value.len)==0) {
179
+        	/* found */
180
+            return 0;
181
+        }
182
+    }
183
+
184
+	return 2;
185
+}
186
+
187
+/**
188
+ *
189
+ */
190
+char* gzc_msg_update(sip_msg_t *msg, unsigned int *olen)
191
+{
192
+	struct dest_info dst;
193
+
194
+	init_dest_info(&dst);
195
+	dst.proto = PROTO_UDP;
196
+	return build_req_buf_from_sip_req(msg,
197
+			olen, &dst, BUILD_NO_LOCAL_VIA|BUILD_NO_VIA1_UPDATE);
198
+}
199
+
200
+/**
201
+ *
202
+ */
203
+int gzc_set_msg_body(sip_msg_t *msg, str *obody, str *nbody)
204
+{
205
+	struct lump *anchor;
206
+	char* buf;
207
+
208
+	/* none should be here - just for safety */
209
+	del_nonshm_lump( &(msg->body_lumps) );
210
+	msg->body_lumps = NULL;
211
+
212
+	if(del_lump(msg, obody->s - msg->buf, obody->len, 0) == 0)
213
+	{
214
+		LM_ERR("cannot delete existing body");
215
+		return -1;
216
+	}
217
+
218
+	anchor = anchor_lump(msg, obody->s - msg->buf, 0, 0);
219
+
220
+	if (anchor == 0)
221
+	{
222
+		LM_ERR("failed to get body anchor\n");
223
+		return -1;
224
+	} 
225
+
226
+	buf=pkg_malloc(nbody->len * sizeof(char));
227
+	if (buf==0)
228
+	{
229
+		LM_ERR("out of pkg memory\n");
230
+		return -1;
231
+	}
232
+	memcpy(buf, nbody->s, nbody->len);
233
+	if (insert_new_lump_after(anchor, buf, nbody->len, 0) == 0)
234
+	{
235
+		LM_ERR("failed to insert body lump\n");
236
+		pkg_free(buf);
237
+		return -1;
238
+	}
239
+	return 0;
240
+}
241
+
242
+/* local buffer to use for compressing/decompressing */
243
+static char _gzc_local_buffer[BUF_SIZE];
244
+
245
+/**
246
+ *
247
+ */
248
+int gzc_msg_received(void *data)
249
+{
250
+	sip_msg_t msg;
251
+	str *obuf;
252
+	char *nbuf = NULL;
253
+	str obody;
254
+	str nbody;
255
+	unsigned long olen;
256
+	unsigned long nlen;
257
+	int ret;
258
+
259
+	obuf = (str*)data;
260
+	memset(&msg, 0, sizeof(sip_msg_t));
261
+	msg.buf = obuf->s;
262
+	msg.len = obuf->len;
263
+
264
+	if(gzc_prepare_msg(&msg)!=0)
265
+	{
266
+		goto done;
267
+	}
268
+
269
+	if(gzc_skip_msg(&msg))
270
+	{
271
+		goto done;
272
+	}
273
+
274
+	if(msg.first_line.type==SIP_REQUEST)
275
+	{
276
+		if(_gzc_sanity_checks!=0)
277
+		{
278
+			if(scb.check_defaults(&msg)<1)
279
+			{
280
+				LM_ERR("sanity checks failed\n");
281
+				goto done;
282
+			}
283
+		}
284
+	}
285
+
286
+	obody.s = get_body(&msg);
287
+	if (obody.s==NULL)
288
+	{
289
+		LM_DBG("no body for this SIP message\n");
290
+		goto done;
291
+	}
292
+	obody.len = msg.buf + msg.len - obody.s;
293
+
294
+	/* decompress the body */
295
+	nbody.s = _gzc_local_buffer;
296
+	nlen = BUF_SIZE;
297
+	olen = obody.len;
298
+	ret = uncompress((unsigned char*)nbody.s, &nlen,
299
+			(unsigned char*)obody.s, olen);
300
+	if(ret!=Z_OK)
301
+	{
302
+		LM_ERR("error decompressing body (%d)\n", ret);
303
+		goto done;
304
+	}
305
+	nbody.len = (int)nlen;
306
+	LM_DBG("body decompressed - old size: %d - new size: %d\n",
307
+			obody.len, nbody.len);
308
+
309
+	if(gzc_set_msg_body(&msg, &obody, &nbody)<0)
310
+	{
311
+		LM_ERR("error replacing body\n");
312
+		goto done;
313
+	}
314
+
315
+	nbuf = gzc_msg_update(&msg, (unsigned int*)&obuf->len);
316
+
317
+	if(obuf->len>=BUF_SIZE)
318
+	{
319
+		LM_ERR("new buffer overflow (%d)\n", obuf->len);
320
+		pkg_free(nbuf);
321
+		return -1;
322
+	}
323
+	memcpy(obuf->s, nbuf, obuf->len);
324
+	obuf->s[obuf->len] = '\0';
325
+
326
+done:
327
+	if(nbuf!=NULL)
328
+		pkg_free(nbuf);
329
+	free_sip_msg(&msg);
330
+	return 0;
331
+}
332
+
333
+/**
334
+ *
335
+ */
336
+int gzc_msg_sent(void *data)
337
+{
338
+	sip_msg_t msg;
339
+	str *obuf;
340
+	str obody;
341
+	str nbody;
342
+	unsigned long olen;
343
+	unsigned long nlen;
344
+	int ret;
345
+
346
+	obuf = (str*)data;
347
+	memset(&msg, 0, sizeof(sip_msg_t));
348
+	msg.buf = obuf->s;
349
+	msg.len = obuf->len;
350
+
351
+	if(gzc_prepare_msg(&msg)!=0)
352
+	{
353
+		goto done;
354
+	}
355
+
356
+	if(gzc_skip_msg(&msg))
357
+	{
358
+		goto done;
359
+	}
360
+
361
+	obody.s = get_body(&msg);
362
+	if (obody.s==NULL)
363
+	{
364
+		LM_DBG("no body for this SIP message\n");
365
+		goto done;
366
+	}
367
+	obody.len = msg.buf + msg.len - obody.s;
368
+
369
+	/* decompress the body */
370
+	nbody.s = _gzc_local_buffer;
371
+	nlen = BUF_SIZE;
372
+	olen = obody.len;
373
+	ret = compress((unsigned char*)nbody.s, &nlen,
374
+			(unsigned char*)obody.s, olen);
375
+	if(ret!=Z_OK)
376
+	{
377
+		LM_ERR("error compressing body (%d)\n", ret);
378
+		goto done;
379
+	}
380
+	nbody.len = (int)nlen;
381
+	LM_DBG("body compressed - old size: %d - new size: %d\n",
382
+			obody.len, nbody.len);
383
+
384
+	if(gzc_set_msg_body(&msg, &obody, &nbody)<0)
385
+	{
386
+		LM_ERR("error replacing body\n");
387
+		goto done;
388
+	}
389
+
390
+	obuf->s = gzc_msg_update(&msg, (unsigned int*)&obuf->len);
391
+
392
+done:
393
+	free_sip_msg(&msg);
394
+	return 0;
395
+}
396
+