Browse code

cnxcc: minor changes

* use [PKG|SHM]_MEM_ERROR
* use memset and avoid initialization per field
* init variables
* add cnxcc to lgtm build
* use localtime_r() for a safer multi-thread usage
* replaced deprecated STR_PARAM with PARAM_STR
* clean includes
* clang-format

Victor Seva authored on 02/06/2020 17:12:31
Showing 12 changed files
... ...
@@ -8,6 +8,7 @@ extraction:
8 8
       - "libcurl4-openssl-dev"
9 9
       - "libjansson-dev"
10 10
       - "libhiredis-dev"
11
+      - "libevent-dev"
11 12
       - "liblua5.1-0-dev"
12 13
       - "libpcre3-dev"
13 14
       - "libncurses5-dev"
... ...
@@ -24,6 +25,6 @@ extraction:
24 25
     after_prepare:
25 26
     - "export PKG_CONFIG_PATH=$LGTM_WORKSPACE/usr/lib/pkgconfig:$PKG_CONFIG_PATH"
26 27
     configure:
27
-      command: "make include_modules='app_lua app_python3 db_mysql db_postgres db_redis dialplan http_client jansson lcr ndb_redis presence presence_xml presence_dialoginfo pua pua_dialoginfo topos_redis uuid websocket xmlops' cfg"
28
+      command: "make include_modules='app_lua app_python3 cnxcc db_mysql db_postgres db_redis dialplan http_client jansson lcr ndb_redis presence presence_xml presence_dialoginfo pua pua_dialoginfo topos_redis uuid websocket xmlops' cfg"
28 29
     index:
29 30
       build_command: "make all"
... ...
@@ -19,10 +19,8 @@
19 19
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 20
  *
21 21
  */
22
-#include <time.h>
23
-#include <string.h>
22
+
24 23
 #include <stdlib.h>
25
-#include <sys/time.h>
26 24
 
27 25
 #include "cnxcc.h"
28 26
 
... ...
@@ -23,6 +23,8 @@
23 23
 #ifndef _CNXCC_H
24 24
 #define _CNXCC_H
25 25
 
26
+#include <time.h>
27
+
26 28
 #include "../../core/str.h"
27 29
 
28 30
 #define DATETIME_SIZE sizeof("0001-01-01 00:00:00")
... ...
@@ -37,12 +39,12 @@ static inline unsigned int get_current_timestamp()
37 39
 static inline int timestamp2isodt(str *dest, unsigned int timestamp)
38 40
 {
39 41
 	time_t tim;
40
-	struct tm *tmPtr;
42
+	struct tm tmPtr;
41 43
 
42 44
 	tim = timestamp;
43
-	tmPtr = localtime(&tim);
45
+	localtime_r(&tim, &tmPtr);
44 46
 
45
-	strftime(dest->s, DATETIME_SIZE, "%Y-%m-%d %H:%M:%S", tmPtr);
47
+	strftime(dest->s, DATETIME_SIZE, "%Y-%m-%d %H:%M:%S", &tmPtr);
46 48
 	dest->len = DATETIME_LENGTH;
47 49
 
48 50
 	return 0;
... ...
@@ -27,32 +27,28 @@
27 27
 #include <sys/ipc.h>
28 28
 #include <unistd.h>
29 29
 #include <fcntl.h>
30
-#include <time.h>
31 30
 #include <ctype.h>
32 31
 
33 32
 #include "../../core/sr_module.h"
33
+#include "../../core/mod_fix.h"
34 34
 #include "../../core/dprint.h"
35 35
 #include "../../core/error.h"
36 36
 #include "../../core/mem/mem.h"
37
-#include "../../core/shm_init.h"
38 37
 #include "../../core/mem/shm_mem.h"
39
-#include "../../core/pvar.h"
40
-#include "../../core/locking.h"
38
+
41 39
 #include "../../core/lock_ops.h"
42
-#include "../../core/str_hash.h"
43 40
 #include "../../core/timer_proc.h"
44
-#include "../../modules/tm/tm_load.h"
45 41
 #include "../../core/parser/parse_from.h"
46 42
 #include "../../core/parser/parse_to.h"
47 43
 #include "../../core/parser/parse_uri.h"
48 44
 #include "../../core/parser/parse_cseq.h"
49 45
 #include "../../core/parser/contact/parse_contact.h"
50 46
 #include "../../core/parser/contact/contact.h"
51
-#include "../../core/parser/parse_rr.h"
52
-#include "../../core/mod_fix.h"
47
+
48
+#include "../tm/tm_load.h"
53 49
 #include "../dialog/dlg_load.h"
54 50
 #include "../dialog/dlg_hash.h"
55
-#include "../../core/fmsg.h"
51
+
56 52
 #include "../../core/rpc.h"
57 53
 #include "../../core/rpc_lookup.h"
58 54
 #include "../../core/kemi.h"
... ...
@@ -170,7 +166,7 @@ static cmd_export_t cmds[] = {
170 166
 static param_export_t params[] = {
171 167
 	{"dlg_flag", INT_PARAM,	&_data.ctrl_flag },
172 168
 	{"credit_check_period", INT_PARAM,	&_data.check_period },
173
-	{"redis", STR_PARAM, &_data.redis_cnn_str.s },
169
+	{"redis", PARAM_STR, &_data.redis_cnn_str },
174 170
 	{ 0, 0, 0 }
175 171
 };
176 172
 /* clang-format on */
... ...
@@ -262,9 +258,6 @@ static int __mod_init(void)
262 258
 		return -1;
263 259
 	}
264 260
 
265
-	if(_data.redis_cnn_str.s)
266
-		_data.redis_cnn_str.len = strlen(_data.redis_cnn_str.s);
267
-
268 261
 	_data.time.credit_data_by_client =
269 262
 			shm_malloc(sizeof(struct str_hash_table));
270 263
 	_data.time.call_data_by_cid = shm_malloc(sizeof(struct str_hash_table));
... ...
@@ -284,15 +277,11 @@ static int __mod_init(void)
284 277
 	memset(_data.channel.call_data_by_cid, 0, sizeof(struct str_hash_table));
285 278
 
286 279
 	_data.stats = (stats_t *)shm_malloc(sizeof(stats_t));
287
-
288 280
 	if(!_data.stats) {
289
-		LM_ERR("Error allocating shared memory stats\n");
281
+		SHM_MEM_ERROR;
290 282
 		return -1;
291 283
 	}
292
-
293
-	_data.stats->active = 0;
294
-	_data.stats->dropped = 0;
295
-	_data.stats->total = 0;
284
+	memset(_data.stats, 0, sizeof(stats_t));
296 285
 
297 286
 	if(__init_hashtable(_data.time.credit_data_by_client) != 0)
298 287
 		return -1;
... ...
@@ -312,9 +301,7 @@ static int __mod_init(void)
312 301
 	if(__init_hashtable(_data.channel.call_data_by_cid) != 0)
313 302
 		return -1;
314 303
 
315
-
316 304
 	cnxcc_lock_init(_data.lock);
317
-
318 305
 	cnxcc_lock_init(_data.time.lock);
319 306
 	cnxcc_lock_init(_data.money.lock);
320 307
 	cnxcc_lock_init(_data.channel.lock);
... ...
@@ -433,21 +420,22 @@ static int __child_init(int rank)
433 420
 
434 421
 static int __init_hashtable(struct str_hash_table *ht)
435 422
 {
436
-	if(__shm_str_hash_alloc(ht, HT_SIZE) != 0) {
437
-		LM_ERR("Error allocating shared memory hashtable\n");
423
+	if(ht == NULL)
424
+		return -1;
425
+
426
+	if(__shm_str_hash_alloc(ht, HT_SIZE) != 0)
438 427
 		return -1;
439
-	}
440 428
 
441 429
 	str_hash_init(ht);
442 430
 	return 0;
443 431
 }
444 432
 
445 433
 static void __dialog_created_callback(
446
-		struct dlg_cell *cell, int type, struct dlg_cb_params *params)
434
+		struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
447 435
 {
448 436
 	struct sip_msg *msg = NULL;
449 437
 
450
-	msg = params->direction == SIP_REPLY ? params->rpl : params->req;
438
+	msg = _params->direction == SIP_REPLY ? _params->rpl : _params->req;
451 439
 
452 440
 	if(msg == NULL) {
453 441
 		LM_ERR("Error getting direction of SIP msg\n");
... ...
@@ -471,7 +459,7 @@ static void __dialog_created_callback(
471 459
 }
472 460
 
473 461
 static void __dialog_confirmed_callback(
474
-		struct dlg_cell *cell, int type, struct dlg_cb_params *params)
462
+		struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
475 463
 {
476 464
 	LM_DBG("Dialog confirmed for CID [%.*s]\n", cell->callid.len,
477 465
 			cell->callid.s);
... ...
@@ -480,7 +468,7 @@ static void __dialog_confirmed_callback(
480 468
 }
481 469
 
482 470
 static void __dialog_terminated_callback(
483
-		struct dlg_cell *cell, int type, struct dlg_cb_params *params)
471
+		struct dlg_cell *cell, int type, struct dlg_cb_params *_params)
484 472
 {
485 473
 	LM_DBG("Dialog terminated for CID [%.*s]\n", cell->callid.len,
486 474
 			cell->callid.s);
... ...
@@ -915,7 +903,7 @@ static void __start_billing(
915 903
 	 * Store from-tag value
916 904
 	 */
917 905
 	if(shm_str_dup(&call->sip_data.from_tag, &tags[0]) != 0) {
918
-		LM_ERR("No more pkg memory\n");
906
+		SHM_MEM_ERROR;
919 907
 		goto exit;
920 908
 	}
921 909
 
... ...
@@ -923,13 +911,13 @@ static void __start_billing(
923 911
 	 * Store to-tag value
924 912
 	 */
925 913
 	if(shm_str_dup(&call->sip_data.to_tag, &tags[1]) != 0) {
926
-		LM_ERR("No more pkg memory\n");
914
+		SHM_MEM_ERROR;
927 915
 		goto exit;
928 916
 	}
929 917
 
930 918
 	if(shm_str_dup(&call->sip_data.from_uri, from_uri) != 0
931 919
 			|| shm_str_dup(&call->sip_data.to_uri, to_uri) != 0) {
932
-		LM_ERR("No more pkg memory\n");
920
+		SHM_MEM_ERROR;
933 921
 		goto exit;
934 922
 	}
935 923
 
... ...
@@ -1089,9 +1077,10 @@ static int __shm_str_hash_alloc(struct str_hash_table *ht, int size)
1089 1077
 {
1090 1078
 	ht->table = shm_malloc(sizeof(struct str_hash_head) * size);
1091 1079
 
1092
-	if(!ht->table)
1080
+	if(!ht->table) {
1081
+		SHM_MEM_ERROR;
1093 1082
 		return -1;
1094
-
1083
+	}
1095 1084
 	ht->size = size;
1096 1085
 	return 0;
1097 1086
 }
... ...
@@ -1110,7 +1099,7 @@ int terminate_call(call_t *call)
1110 1099
 			call->dlg_h_entry);
1111 1100
 
1112 1101
 	data = &call->sip_data;
1113
-	if(faked_msg_init_with_dlg_info(&data->callid, &data->from_uri,
1102
+	if(cnxcc_faked_msg_init_with_dlg_info(&data->callid, &data->from_uri,
1114 1103
 			   &data->from_tag, &data->to_uri, &data->to_tag, &dmsg)
1115 1104
 			!= 0) {
1116 1105
 		LM_ERR("[%.*s]: error generating faked sip message\n", data->callid.len,
... ...
@@ -1197,7 +1186,7 @@ static credit_data_t *__get_or_create_credit_data_entry(
1197 1186
 	return (credit_data_t *)e->u.p;
1198 1187
 
1199 1188
 no_memory:
1200
-	LM_ERR("No shared memory left\n");
1189
+	SHM_MEM_ERROR;
1201 1190
 	return NULL;
1202 1191
 }
1203 1192
 
... ...
@@ -1205,17 +1194,13 @@ static credit_data_t *__alloc_new_credit_data(
1205 1194
 		str *client_id, credit_type_t type)
1206 1195
 {
1207 1196
 	credit_data_t *credit_data = shm_malloc(sizeof(credit_data_t));
1208
-	;
1209
-
1210 1197
 	if(credit_data == NULL)
1211 1198
 		goto no_memory;
1212
-
1213 1199
 	memset(credit_data, 0, sizeof(credit_data_t));
1214 1200
 
1215 1201
 	cnxcc_lock_init(credit_data->lock);
1216 1202
 
1217 1203
 	credit_data->call_list = shm_malloc(sizeof(call_t));
1218
-
1219 1204
 	if(credit_data->call_list == NULL)
1220 1205
 		goto no_memory;
1221 1206
 
... ...
@@ -1231,7 +1216,6 @@ static credit_data_t *__alloc_new_credit_data(
1231 1216
 
1232 1217
 	if(_data.redis) {
1233 1218
 		credit_data->str_id = shm_malloc(client_id->len + 1);
1234
-
1235 1219
 		if(!credit_data->str_id)
1236 1220
 			goto no_memory;
1237 1221
 
... ...
@@ -1239,14 +1223,7 @@ static credit_data_t *__alloc_new_credit_data(
1239 1223
 		snprintf(credit_data->str_id, client_id->len + 1, "%.*s",
1240 1224
 				client_id->len, client_id->s);
1241 1225
 	}
1242
-
1243
-	credit_data->max_amount = 0;
1244
-	credit_data->concurrent_calls = 0;
1245
-	credit_data->consumed_amount = 0;
1246
-	credit_data->ended_calls_consumed_amount = 0;
1247
-	credit_data->number_of_calls = 0;
1248 1226
 	credit_data->type = type;
1249
-	credit_data->deallocating = 0;
1250 1227
 
1251 1228
 	if(!_data.redis)
1252 1229
 		return credit_data;
... ...
@@ -1257,7 +1234,7 @@ static credit_data_t *__alloc_new_credit_data(
1257 1234
 	return credit_data;
1258 1235
 
1259 1236
 no_memory:
1260
-	LM_ERR("No shared memory left\n");
1237
+	SHM_MEM_ERROR;
1261 1238
 error:
1262 1239
 	return NULL;
1263 1240
 }
... ...
@@ -1277,9 +1254,10 @@ static call_t *__alloc_new_call_by_money(credit_data_t *credit_data,
1277 1254
 
1278 1255
 	call = shm_malloc(sizeof(call_t));
1279 1256
 	if(call == NULL) {
1280
-		LM_ERR("No shared memory left\n");
1257
+		SHM_MEM_ERROR;
1281 1258
 		goto error;
1282 1259
 	}
1260
+	memset(call, 0, sizeof(call_t));
1283 1261
 
1284 1262
 	if((!msg->callid && parse_headers(msg, HDR_CALLID_F, 0) != 0)
1285 1263
 			|| shm_str_dup(&call->sip_data.callid, &msg->callid->body) != 0) {
... ...
@@ -1287,16 +1265,6 @@ static call_t *__alloc_new_call_by_money(credit_data_t *credit_data,
1287 1265
 		goto error;
1288 1266
 	}
1289 1267
 
1290
-	call->sip_data.to_uri.s = NULL;
1291
-	call->sip_data.to_uri.len = 0;
1292
-	call->sip_data.to_tag.s = NULL;
1293
-	call->sip_data.to_tag.len = 0;
1294
-
1295
-	call->sip_data.from_uri.s = NULL;
1296
-	call->sip_data.from_uri.len = 0;
1297
-	call->sip_data.from_tag.s = NULL;
1298
-	call->sip_data.from_tag.len = 0;
1299
-
1300 1268
 	call->consumed_amount = initial_pulse * cost_per_second;
1301 1269
 	call->connect_amount = connect_cost;
1302 1270
 	call->confirmed = FALSE;
... ...
@@ -1356,6 +1324,7 @@ static call_t *__alloc_new_call_by_time(
1356 1324
 		LM_ERR("No shared memory left\n");
1357 1325
 		goto error;
1358 1326
 	}
1327
+	memset(call, 0, sizeof(call_t));
1359 1328
 
1360 1329
 	if((!msg->callid && parse_headers(msg, HDR_CALLID_F, 0) != 0)
1361 1330
 			|| shm_str_dup(&call->sip_data.callid, &msg->callid->body) != 0) {
... ...
@@ -1363,16 +1332,6 @@ static call_t *__alloc_new_call_by_time(
1363 1332
 		goto error;
1364 1333
 	}
1365 1334
 
1366
-	call->sip_data.to_uri.s = NULL;
1367
-	call->sip_data.to_uri.len = 0;
1368
-	call->sip_data.to_tag.s = NULL;
1369
-	call->sip_data.to_tag.len = 0;
1370
-
1371
-	call->sip_data.from_uri.s = NULL;
1372
-	call->sip_data.from_uri.len = 0;
1373
-	call->sip_data.from_tag.s = NULL;
1374
-	call->sip_data.from_tag.len = 0;
1375
-
1376 1335
 	call->consumed_amount = 0;
1377 1336
 	call->confirmed = FALSE;
1378 1337
 	call->max_amount = max_secs;
... ...
@@ -1423,9 +1382,10 @@ static call_t *alloc_new_call_by_channel(
1423 1382
 
1424 1383
 	call = shm_malloc(sizeof(call_t));
1425 1384
 	if(call == NULL) {
1426
-		LM_ERR("No shared memory left\n");
1385
+		SHM_MEM_ERROR;
1427 1386
 		goto error;
1428 1387
 	}
1388
+	memset(call, 0, sizeof(call_t));
1429 1389
 
1430 1390
 	if((!msg->callid && parse_headers(msg, HDR_CALLID_F, 0) != 0)
1431 1391
 			|| shm_str_dup(&call->sip_data.callid, &msg->callid->body) != 0) {
... ...
@@ -1433,16 +1393,6 @@ static call_t *alloc_new_call_by_channel(
1433 1393
 		goto error;
1434 1394
 	}
1435 1395
 
1436
-	call->sip_data.to_uri.s = NULL;
1437
-	call->sip_data.to_uri.len = 0;
1438
-	call->sip_data.to_tag.s = NULL;
1439
-	call->sip_data.to_tag.len = 0;
1440
-
1441
-	call->sip_data.from_uri.s = NULL;
1442
-	call->sip_data.from_uri.len = 0;
1443
-	call->sip_data.from_tag.s = NULL;
1444
-	call->sip_data.from_tag.len = 0;
1445
-
1446 1396
 	call->consumed_amount = 0;
1447 1397
 	call->confirmed = FALSE;
1448 1398
 	call->max_amount = max_chan;
... ...
@@ -1534,14 +1484,13 @@ static int __add_call_by_cid(str *cid, call_t *call, credit_type_t type)
1534 1484
 	}
1535 1485
 
1536 1486
 	e = shm_malloc(sizeof(struct str_hash_entry));
1537
-
1538 1487
 	if(e == NULL) {
1539
-		LM_ERR("No shared memory left\n");
1488
+		SHM_MEM_ERROR;
1540 1489
 		return -1;
1541 1490
 	}
1542 1491
 
1543 1492
 	if(shm_str_dup(&e->key, cid) != 0) {
1544
-		LM_ERR("No shared memory left\n");
1493
+		SHM_MEM_ERROR;
1545 1494
 		return -1;
1546 1495
 	}
1547 1496
 
... ...
@@ -1961,6 +1910,10 @@ static int __set_max_time(sip_msg_t *msg, char *pclient, char *pmaxsecs)
1961 1910
 static int ki_update_max_time(sip_msg_t *msg, str *sclient, int secs)
1962 1911
 {
1963 1912
 	credit_data_t *credit_data = NULL;
1913
+	struct str_hash_table *ht = NULL;
1914
+	struct str_hash_entry *e = NULL;
1915
+	double update_fraction = secs;
1916
+	call_t *call = NULL, *tmp_call = NULL;
1964 1917
 
1965 1918
 	set_ctrl_flag(msg);
1966 1919
 
... ...
@@ -1985,12 +1938,7 @@ static int ki_update_max_time(sip_msg_t *msg, str *sclient, int secs)
1985 1938
 			sclient->len, sclient->s, secs, msg->callid->body.len,
1986 1939
 			msg->callid->body.s);
1987 1940
 
1988
-
1989
-	struct str_hash_table *ht = NULL;
1990
-	struct str_hash_entry *e = NULL;
1991 1941
 	ht = _data.time.credit_data_by_client;
1992
-	double update_fraction = secs;
1993
-	call_t *call = NULL, *tmp_call = NULL;
1994 1942
 
1995 1943
 	cnxcc_lock(_data.time.lock);
1996 1944
 	e = str_hash_get(ht, sclient->s, sclient->len);
... ...
@@ -25,7 +25,7 @@
25 25
 #include "../../core/locking.h"
26 26
 #include "../../core/atomic_ops.h"
27 27
 #include "../../core/str_hash.h"
28
-#include "../../core/parser/parse_rr.h"
28
+#include "../../core/flags.h"
29 29
 
30 30
 #define str_shm_free_if_not_null(_var_) \
31 31
 	if(_var_.s != NULL) {               \
... ...
@@ -35,7 +35,7 @@
35 35
 	}
36 36
 
37 37
 /*!
38
- * \brief Init a cnxcc_lock 
38
+ * \brief Init a cnxcc_lock
39 39
  * \param _entry locked entry
40 40
  */
41 41
 #define cnxcc_lock_init(_entry) \
... ...
@@ -89,13 +89,15 @@ typedef struct stats
89 89
 	unsigned int dropped;
90 90
 } stats_t;
91 91
 
92
-typedef enum cnxpvtypes {
92
+typedef enum cnxpvtypes
93
+{
93 94
 	CNX_PV_ACTIVE = 1,
94 95
 	CNX_PV_TOTAL,
95 96
 	CNX_PV_DROPPED
96 97
 } cnxpvtypes_t;
97 98
 
98
-typedef enum credit_type {
99
+typedef enum credit_type
100
+{
99 101
 	CREDIT_TIME,
100 102
 	CREDIT_MONEY,
101 103
 	CREDIT_CHANNEL
... ...
@@ -212,7 +214,7 @@ typedef struct credit_data
212 214
 
213 215
 	char *str_id;
214 216
 	// flag to mark this instance in the process of being eliminated
215
-	int deallocating : 1;
217
+	unsigned int deallocating : 1;
216 218
 } credit_data_t;
217 219
 
218 220
 
... ...
@@ -144,9 +144,18 @@ error:
144 144
 static struct redis *__alloc_redis(char *ip, int port, int db)
145 145
 {
146 146
 	struct redis *redis = pkg_malloc(sizeof(struct redis));
147
+	if(!redis) {
148
+		PKG_MEM_ERROR;
149
+		return NULL;
150
+	}
147 151
 	int len = strlen(ip);
148 152
 
149 153
 	redis->ip = pkg_malloc(len + 1);
154
+	if(!redis->ip) {
155
+		PKG_MEM_ERROR;
156
+		pkg_free(redis);
157
+		return NULL;
158
+	}
150 159
 	strcpy(redis->ip, ip);
151 160
 
152 161
 	redis->port = port;
... ...
@@ -226,8 +235,7 @@ static struct redis *__redis_connect(struct redis *redis)
226 235
 
227 236
 static int __redis_select_db(redisContext *ctxt, int db)
228 237
 {
229
-	redisReply *rpl;
230
-	rpl = redisCommand(ctxt, "SELECT %d", db);
238
+	redisReply *rpl = redisCommand(ctxt, "SELECT %d", db);
231 239
 
232 240
 	if(!rpl || rpl->type == REDIS_REPLY_ERROR) {
233 241
 		if(!rpl)
... ...
@@ -245,7 +253,7 @@ static int __redis_select_db(redisContext *ctxt, int db)
245 253
 static int __redis_exec(
246 254
 		credit_data_t *credit_data, const char *cmd, redisReply **rpl)
247 255
 {
248
-	redisReply *rpl_aux;
256
+	redisReply *rpl_aux = NULL;
249 257
 	char cmd_buffer[1024];
250 258
 
251 259
 	*rpl = redisCommand(_data.redis->ctxt, cmd);
... ...
@@ -300,7 +308,7 @@ int redis_incr_by_double(
300 308
 int redis_get_double(credit_data_t *credit_data, const char *instruction,
301 309
 		const char *key, double *value)
302 310
 {
303
-	str str_double = {0, 0};
311
+	str str_double = STR_NULL;
304 312
 	char buffer[128];
305 313
 
306 314
 	if(redis_get_str(credit_data, instruction, key, &str_double) < 0)
... ...
@@ -389,6 +397,11 @@ int redis_get_str(credit_data_t *credit_data, const char *instruction,
389 397
 	}
390 398
 
391 399
 	value->s = pkg_malloc(rpl->len);
400
+	if(!value->s) {
401
+		PKG_MEM_ERROR;
402
+		freeReplyObject(rpl);
403
+		return -1;
404
+	}
392 405
 	value->len = rpl->len;
393 406
 	memcpy(value->s, rpl->str, rpl->len);
394 407
 
... ...
@@ -516,7 +529,7 @@ int redis_insert_double_value(
516 529
 
517 530
 int redis_kill_list_member_exists(credit_data_t *credit_data)
518 531
 {
519
-	redisReply *rpl;
532
+	redisReply *rpl = NULL;
520 533
 	int exists = 0;
521 534
 	char cmd_buffer[1024];
522 535
 
... ...
@@ -552,7 +565,7 @@ static void __redis_subscribe_to_kill_list(struct redis *redis)
552 565
 
553 566
 int redis_publish_to_kill_list(credit_data_t *credit_data)
554 567
 {
555
-	redisReply *rpl;
568
+	redisReply *rpl = NULL;
556 569
 	char cmd_buffer[1024];
557 570
 	snprintf(cmd_buffer, sizeof(cmd_buffer), "PUBLISH cnxcc:kill_list %s",
558 571
 			credit_data->str_id);
... ...
@@ -578,8 +591,8 @@ static void __async_disconnect_cb(const redisAsyncContext *c, int status)
578 591
 static void __subscription_cb(redisAsyncContext *c, void *r, void *privdata)
579 592
 {
580 593
 	redisReply *reply = r;
581
-	str key;
582
-	credit_data_t *credit_data;
594
+	str key = STR_NULL;
595
+	credit_data_t *credit_data = NULL;
583 596
 
584 597
 	if(reply == NULL) {
585 598
 		LM_ERR("reply is NULL\n");
... ...
@@ -22,18 +22,16 @@
22 22
 
23 23
 #include <stdio.h>
24 24
 
25
-#include "../../core/rpc.h"
26
-#include "../../core/rpc_lookup.h"
27
-
25
+#include "cnxcc_rpc.h"
28 26
 #include "cnxcc_mod.h"
29 27
 
30 28
 extern data_t _data;
31 29
 
32 30
 void rpc_kill_call(rpc_t *rpc, void *ctx)
33 31
 {
34
-	call_t *call;
35
-	hash_tables_t *hts;
36
-	str callid;
32
+	call_t *call = NULL;
33
+	hash_tables_t *hts = NULL;
34
+	str callid = STR_NULL;
37 35
 
38 36
 	if(!rpc->scan(ctx, "S", &callid)) {
39 37
 		LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
... ...
@@ -65,11 +63,12 @@ void rpc_kill_call(rpc_t *rpc, void *ctx)
65 63
 
66 64
 void rpc_check_client_stats(rpc_t *rpc, void *ctx)
67 65
 {
68
-	call_t *call, *tmp;
66
+	call_t *call = NULL, *tmp = NULL;
69 67
 	int index = 0;
70
-	str client_id, rows;
68
+	str client_id = STR_NULL;
69
+	str rows = STR_NULL;
71 70
 	char row_buffer[512];
72
-	credit_data_t *credit_data;
71
+	credit_data_t *credit_data = NULL;
73 72
 
74 73
 	if(!rpc->scan(ctx, "S", &client_id)) {
75 74
 		LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
... ...
@@ -98,9 +97,7 @@ void rpc_check_client_stats(rpc_t *rpc, void *ctx)
98 97
 		return;
99 98
 	}
100 99
 
101
-	rows.len = 0;
102 100
 	rows.s = pkg_malloc(10);
103
-
104 101
 	if(rows.s == NULL)
105 102
 		goto nomem;
106 103
 
... ...
@@ -137,7 +134,6 @@ void rpc_check_client_stats(rpc_t *rpc, void *ctx)
137 134
 
138 135
 		row_len = strlen(row_buffer);
139 136
 		rows.s = pkg_reallocxf(rows.s, rows.len + row_len);
140
-
141 137
 		if(rows.s == NULL) {
142 138
 			cnxcc_unlock(credit_data->lock);
143 139
 			goto nomem;
... ...
@@ -161,14 +157,14 @@ void rpc_check_client_stats(rpc_t *rpc, void *ctx)
161 157
 	return;
162 158
 
163 159
 nomem:
164
-	LM_ERR("No more pkg memory\n");
160
+	PKG_MEM_ERROR;
165 161
 	rpc->fault(ctx, 500, "No more memory\n");
166 162
 }
167 163
 
168 164
 static int iterate_over_table(
169 165
 		hash_tables_t *hts, str *result, credit_type_t type)
170 166
 {
171
-	struct str_hash_entry *h_entry, *tmp;
167
+	struct str_hash_entry *h_entry = NULL, *tmp = NULL;
172 168
 	char row_buffer[512];
173 169
 	int index = 0;
174 170
 
... ...
@@ -223,7 +219,6 @@ static int iterate_over_table(
223 219
 
224 220
 				row_len = strlen(row_buffer);
225 221
 				result->s = pkg_reallocxf(result->s, result->len + row_len);
226
-
227 222
 				if(result->s == NULL) {
228 223
 					cnxcc_unlock(hts->lock);
229 224
 					goto nomem;
... ...
@@ -238,21 +233,18 @@ static int iterate_over_table(
238 233
 	return 0;
239 234
 
240 235
 nomem:
241
-	LM_ERR("No more pkg memory\n");
236
+	PKG_MEM_ERROR;
242 237
 	return -1;
243 238
 }
244 239
 
245 240
 void rpc_active_clients(rpc_t *rpc, void *ctx)
246 241
 {
247
-	str rows;
242
+	str rows = STR_NULL;
248 243
 
249 244
 	rows.s = pkg_malloc(10);
250
-
251 245
 	if(rows.s == NULL)
252 246
 		goto nomem;
253 247
 
254
-	rows.len = 0;
255
-
256 248
 	iterate_over_table(&_data.time, &rows, CREDIT_TIME);
257 249
 	iterate_over_table(&_data.money, &rows, CREDIT_MONEY);
258 250
 
... ...
@@ -266,6 +258,6 @@ void rpc_active_clients(rpc_t *rpc, void *ctx)
266 258
 	return;
267 259
 
268 260
 nomem:
269
-	LM_ERR("No more pkg memory\n");
261
+	PKG_MEM_ERROR;
270 262
 	rpc->fault(ctx, 500, "No more memory\n");
271 263
 }
... ...
@@ -23,6 +23,8 @@
23 23
 #ifndef CNXCC_RPC_H_
24 24
 #define CNXCC_RPC_H_
25 25
 
26
+#include "../../core/rpc.h"
27
+
26 28
 void rpc_active_clients(rpc_t *rpc, void *ctx);
27 29
 void rpc_kill_call(rpc_t *rpc, void *ctx);
28 30
 void rpc_active_clients(rpc_t *rpc, void *ctx);
... ...
@@ -20,9 +20,9 @@
20 20
  *
21 21
  */
22 22
 
23
-#include "../../core/select.h"
24
-#include "../../core/select_buf.h"
23
+#include "../../core/ut.h"
25 24
 
25
+#include "cnxcc_select.h"
26 26
 #include "cnxcc_mod.h"
27 27
 
28 28
 extern data_t _data;
... ...
@@ -23,6 +23,9 @@
23 23
 #ifndef CNXCC_SELECT_H_
24 24
 #define CNXCC_SELECT_H_
25 25
 
26
+#include "../../core/str.h"
27
+#include "../../core/select.h"
28
+
26 29
 int sel_root(str *res, select_t *s, struct sip_msg *msg);
27 30
 int sel_channels(str *res, select_t *s, struct sip_msg *msg);
28 31
 int sel_channels_count(str *res, select_t *s, struct sip_msg *msg);
... ...
@@ -20,29 +20,30 @@
20 20
  *
21 21
  */
22 22
 
23
+#include <sys/socket.h>
24
+
23 25
 #include "../../core/parser/msg_parser.h"
24 26
 #include "../../core/globals.h"
25 27
 
26
-#include <sys/socket.h>
27
-
28 28
 #define FAKED_SIP_MSG_FORMAT                                                 \
29 29
 	"OPTIONS sip:you@kamailio.org SIP/2.0\r\nVia: SIP/2.0/UDP "              \
30 30
 	"127.0.0.1\r\nFrom: <%.*s>;tag=%.*s\r\nTo: <%.*s>;tag=%.*s\r\nCall-ID: " \
31 31
 	"%.*s\r\nCSeq: 1 OPTIONS\r\nContent-Length: 0\r\n\r\n"
32 32
 
33 33
 #define FAKED_SIP_MSG_BUF_LEN 1024
34
-char _faked_sip_msg_buf[FAKED_SIP_MSG_BUF_LEN];
34
+static char _faked_sip_msg_buf[FAKED_SIP_MSG_BUF_LEN];
35 35
 
36 36
 static struct sip_msg _faked_msg;
37 37
 
38
-int faked_msg_init_with_dlg_info(str *callid, str *from_uri, str *from_tag,
39
-		str *to_uri, str *to_tag, struct sip_msg **msg)
38
+int cnxcc_faked_msg_init_with_dlg_info(str *callid, str *from_uri,
39
+		str *from_tag, str *to_uri, str *to_tag, struct sip_msg **msg)
40 40
 {
41 41
 	memset(_faked_sip_msg_buf, 0, FAKED_SIP_MSG_BUF_LEN);
42
+	memset(&_faked_msg, 0, sizeof(struct sip_msg));
42 43
 
43
-	snprintf(_faked_sip_msg_buf, FAKED_SIP_MSG_BUF_LEN, FAKED_SIP_MSG_FORMAT, from_uri->len,
44
-			from_uri->s, from_tag->len, from_tag->s, to_uri->len, to_uri->s,
45
-			to_tag->len, to_tag->s, callid->len, callid->s);
44
+	snprintf(_faked_sip_msg_buf, FAKED_SIP_MSG_BUF_LEN, FAKED_SIP_MSG_FORMAT,
45
+			from_uri->len, from_uri->s, from_tag->len, from_tag->s, to_uri->len,
46
+			to_uri->s, to_tag->len, to_tag->s, callid->len, callid->s);
46 47
 
47 48
 	LM_DBG("fake msg:\n%s\n", _faked_sip_msg_buf);
48 49
 
... ...
@@ -22,7 +22,9 @@
22 22
 #ifndef CNXCC_SIP_MSG_FAKER_H_
23 23
 #define CNXCC_SIP_MSG_FAKER_H_
24 24
 
25
-int faked_msg_init_with_dlg_info(str *callid, str *from_uri, str *from_tag,
26
-		str *to_uri, str *to_tag, struct sip_msg **msg);
25
+#include "../../core/str.h"
26
+
27
+int cnxcc_faked_msg_init_with_dlg_info(str *callid, str *from_uri,
28
+		str *from_tag, str *to_uri, str *to_tag, struct sip_msg **msg);
27 29
 
28 30
 #endif /* CNXCC_SIP_MSG_FAKER_H_ */