Browse code

evapi: use local allocated buffer to get data parameter for event relay

- use system malloc given that the data can be passed from jsonrpcs,
which uses system malloc as well
- try to grow the internall buffer as needed based on estimated size of
output for the data parameter

(cherry picked from commit 63334e312d60a5d6a6470b0067c29db672d08c7c)

Daniel-Constantin Mierla authored on 31/10/2019 08:54:22
Showing 1 changed files
... ...
@@ -55,6 +55,9 @@ str _evapi_event_callback = STR_NULL;
55 55
 int _evapi_dispatcher_pid = -1;
56 56
 int _evapi_max_clients = 8;
57 57
 
58
+static str _evapi_data = STR_NULL;
59
+static int _evapi_data_size = 0;
60
+
58 61
 static tm_api_t tmb;
59 62
 
60 63
 static int  mod_init(void);
... ...
@@ -241,6 +244,59 @@ static void mod_destroy(void)
241 244
 {
242 245
 }
243 246
 
247
+#define evapi_malloc malloc
248
+#define evapi_free free
249
+
250
+/**
251
+ *
252
+ */
253
+static int evapi_get_data(sip_msg_t *msg, pv_elem_t *pdata, str *sdata)
254
+{
255
+	int ret;
256
+	int osize;
257
+
258
+	if(_evapi_data.s == NULL) {
259
+		_evapi_data_size = 8*1024;
260
+		_evapi_data.s = (char*)evapi_malloc(_evapi_data_size * sizeof(char));
261
+		if(_evapi_data.s == NULL) {
262
+			LM_ERR("failed to allocate the buffer for data\n");
263
+			_evapi_data_size = 0;
264
+			return -1;
265
+		}
266
+	}
267
+	_evapi_data.len = _evapi_data_size;
268
+	ret = pv_printf_mode(msg, pdata, 0, _evapi_data.s, &_evapi_data.len);
269
+	if(ret<0) {
270
+		if(ret == -2) {
271
+			osize = pv_printf_size(msg, pdata);
272
+			if(osize<0) {
273
+				LM_ERR("failed to get the data parameter\n");
274
+				return -1;
275
+			}
276
+			if(osize < _evapi_data_size - 8) {
277
+				LM_ERR("failed to get data in an existing buffer\n");
278
+				return -1;
279
+			}
280
+			evapi_free(_evapi_data.s);
281
+			_evapi_data_size = osize + 8;
282
+			_evapi_data.s = (char*)evapi_malloc(_evapi_data_size * sizeof(char));
283
+			if(_evapi_data.s == NULL) {
284
+				LM_ERR("failed to allocate the new buffer for data\n");
285
+				_evapi_data_size = 0;
286
+				return -1;
287
+			}
288
+			_evapi_data.len = _evapi_data_size;
289
+			ret = pv_printf_mode(msg, pdata, 0, _evapi_data.s, &_evapi_data.len);
290
+			if(ret<0) {
291
+				LM_ERR("failed to get data in the new buffer\n");
292
+				return -1;
293
+			}
294
+		}
295
+	}
296
+	sdata->s = _evapi_data.s;
297
+	sdata->len = _evapi_data.len;
298
+	return 0;
299
+}
244 300
 /**
245 301
  *
246 302
  */
... ...
@@ -253,7 +309,7 @@ static int w_evapi_relay(sip_msg_t *msg, char *evdata, char *p2)
253 309
 		return -1;
254 310
 	}
255 311
 
256
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
312
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
257 313
 		LM_ERR("unable to get data\n");
258 314
 		return -1;
259 315
 	}
... ...
@@ -311,7 +367,7 @@ static int w_evapi_async_relay(sip_msg_t *msg, char *evdata, char *p2)
311 367
 
312 368
 	LM_DBG("transaction suspended [%u:%u]\n", tindex, tlabel);
313 369
 
314
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
370
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
315 371
 		LM_ERR("unable to get data\n");
316 372
 		return -1;
317 373
 	}
... ...
@@ -340,7 +396,7 @@ static int w_evapi_multicast(sip_msg_t *msg, char *evdata, char *ptag)
340 396
 		return -1;
341 397
 	}
342 398
 
343
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
399
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
344 400
 		LM_ERR("unable to get data\n");
345 401
 		return -1;
346 402
 	}
... ...
@@ -408,7 +464,7 @@ static int w_evapi_async_multicast(sip_msg_t *msg, char *evdata, char *ptag)
408 464
 
409 465
 	LM_DBG("transaction suspended [%u:%u]\n", tindex, tlabel);
410 466
 
411
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
467
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
412 468
 		LM_ERR("unable to get data\n");
413 469
 		return -1;
414 470
 	}
... ...
@@ -447,7 +503,7 @@ static int w_evapi_unicast(sip_msg_t *msg, char *evdata, char *ptag)
447 503
 		return -1;
448 504
 	}
449 505
 
450
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
506
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
451 507
 		LM_ERR("unable to get data\n");
452 508
 		return -1;
453 509
 	}
... ...
@@ -513,7 +569,7 @@ static int w_evapi_async_unicast(sip_msg_t *msg, char *evdata, char *ptag)
513 569
 
514 570
 	LM_DBG("transaction suspended [%u:%u]\n", tindex, tlabel);
515 571
 
516
-	if(fixup_get_svalue(msg, (gparam_t*)evdata, &sdata)!=0) {
572
+	if(evapi_get_data(msg, (pv_elem_t*)evdata, &sdata)<0) {
517 573
 		LM_ERR("unable to get data\n");
518 574
 		return -1;
519 575
 	}
... ...
@@ -543,7 +599,7 @@ static int w_evapi_async_unicast(sip_msg_t *msg, char *evdata, char *ptag)
543 599
  */
544 600
 static int fixup_evapi_relay(void** param, int param_no)
545 601
 {
546
-	return fixup_spve_null(param, param_no);
602
+	return fixup_vstr_all(param, param_no);
547 603
 }
548 604
 
549 605
 /**
... ...
@@ -551,7 +607,10 @@ static int fixup_evapi_relay(void** param, int param_no)
551 607
  */
552 608
 static int fixup_evapi_multicast(void** param, int param_no)
553 609
 {
554
-	return fixup_spve_spve(param, param_no);
610
+	if(param_no == 1) {
611
+		return fixup_vstr_all(param, param_no);
612
+	}
613
+	return fixup_spve_all(param, param_no);
555 614
 }
556 615
 
557 616
 /**