Browse code

topos: option to set a context value via modparam or function

- ability to group records

Daniel-Constantin Mierla authored on 31/03/2021 19:46:14
Showing 4 changed files
... ...
@@ -53,6 +53,7 @@
53 53
 #include "../../core/timer_proc.h"
54 54
 #include "../../core/fmsg.h"
55 55
 #include "../../core/onsend.h"
56
+#include "../../core/mod_fix.h"
56 57
 #include "../../core/kemi.h"
57 58
 
58 59
 #include "../../lib/srdb1/db.h"
... ...
@@ -104,6 +105,9 @@ str _tps_bcontact_avp;
104 105
 pv_spec_t _tps_acontact_spec;
105 106
 pv_spec_t _tps_bcontact_spec;
106 107
 
108
+str _tps_context_param = str_init("");
109
+str _tps_context_value = str_init("");
110
+
107 111
 sanity_api_t scb;
108 112
 
109 113
 int tps_msg_received(sr_event_param_t *evp);
... ...
@@ -120,9 +124,14 @@ static int child_init(int rank);
120 124
 /* Module destroy function prototype */
121 125
 static void destroy(void);
122 126
 
127
+static int w_tps_set_context(sip_msg_t* msg, char* pctx, char* p2);
128
+
123 129
 int bind_topos(topos_api_t *api);
124 130
 
125 131
 static cmd_export_t cmds[]={
132
+	{"tps_set_context", (cmd_function)w_tps_set_context,
133
+		1, fixup_spve_null, fixup_free_spve_null, ANY_ROUTE},
134
+
126 135
 	{"bind_topos",  (cmd_function)bind_topos,  0,
127 136
 		0, 0, 0},
128 137
 
... ...
@@ -146,6 +155,7 @@ static param_export_t params[]={
146 155
 	{"b_contact_avp",	PARAM_STR, &_tps_bcontact_avp},
147 156
 	{"contact_host_avp",    PARAM_STR, &_tps_contact_host_avp},
148 157
 	{"rr_update",		PARAM_INT, &_tps_rr_update},
158
+	{"context",			PARAM_STR, &_tps_context_param},
149 159
 	{0,0,0}
150 160
 };
151 161
 
... ...
@@ -298,6 +308,58 @@ static void destroy(void)
298 308
 	tps_storage_lock_set_destroy();
299 309
 }
300 310
 
311
+/**
312
+ *
313
+ */
314
+static int ki_tps_set_context(sip_msg_t* msg, str* ctx)
315
+{
316
+	if(ctx==NULL || ctx->len<=0) {
317
+		if(_tps_context_value.s) {
318
+			pkg_free(_tps_context_value.s);
319
+		}
320
+		_tps_context_value.s = NULL;
321
+		_tps_context_value.len = 0;
322
+		return 1;
323
+	}
324
+
325
+	if(_tps_context_value.len>=ctx->len) {
326
+		memcpy(_tps_context_value.s, ctx->s, ctx->len);
327
+		_tps_context_value.len = ctx->len;
328
+		return 1;
329
+	}
330
+
331
+	if(_tps_context_value.s) {
332
+		pkg_free(_tps_context_value.s);
333
+	}
334
+	_tps_context_value.len = 0;
335
+
336
+	_tps_context_value.s = (char*)pkg_mallocxz(ctx->len + 1);
337
+	if(_tps_context_value.s==NULL) {
338
+		PKG_MEM_ERROR;
339
+		return -1;
340
+	}
341
+
342
+	memcpy(_tps_context_value.s, ctx->s, ctx->len);
343
+	_tps_context_value.len = ctx->len;
344
+
345
+	return 1;
346
+}
347
+
348
+/**
349
+ *
350
+ */
351
+static int w_tps_set_context(sip_msg_t* msg, char* pctx, char* p2)
352
+{
353
+	str sctx = STR_NULL;
354
+
355
+	if(fixup_get_svalue(msg, (gparam_t*)pctx, &sctx)<0) {
356
+		LM_ERR("failed to get context parameter\n");
357
+		return -1;
358
+	}
359
+
360
+	return ki_tps_set_context(msg, &sctx);
361
+}
362
+
301 363
 /**
302 364
  *
303 365
  */
... ...
@@ -612,4 +674,29 @@ int bind_topos(topos_api_t *api)
612 674
 	return 0;
613 675
 }
614 676
 
677
+/**
678
+ *
679
+ */
680
+/* clang-format off */
681
+static sr_kemi_t sr_kemi_topos_exports[] = {
682
+	{ str_init("topos"), str_init("tps_set_context"),
683
+		SR_KEMIP_INT, ki_tps_set_context,
684
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
685
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
686
+	},
687
+
688
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
689
+};
690
+/* clang-format on */
691
+
692
+/**
693
+ *
694
+ */
695
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
696
+{
697
+	sr_kemi_modules_add(sr_kemi_topos_exports);
698
+
699
+	return 0;
700
+}
701
+
615 702
 /** @} */
... ...
@@ -51,6 +51,9 @@ extern int _tps_contact_mode;
51 51
 extern str _tps_cparam_name;
52 52
 extern int _tps_rr_update;
53 53
 
54
+extern str _tps_context_param;
55
+extern str _tps_context_value;
56
+
54 57
 str _sr_hname_xbranch = str_init("P-SR-XBranch");
55 58
 str _sr_hname_xuuid = str_init("P-SR-XUID");
56 59
 
... ...
@@ -546,6 +549,11 @@ int tps_pack_message(sip_msg_t *msg, tps_data_t *ptsd)
546 549
 			ptsd->bs_contact.len, ZSW(ptsd->bs_contact.s), ptsd->bs_contact.len);
547 550
 	ptsd->x_rr = ptsd->a_rr;
548 551
 	ptsd->s_method_id = get_cseq(msg)->method_id;
552
+	if(_tps_context_value.len>0) {
553
+		ptsd->x_context = _tps_context_value;
554
+	} else if(_tps_context_param.len>0) {
555
+		ptsd->x_context = _tps_context_param;
556
+	}
549 557
 	return 0;
550 558
 }
551 559
 
... ...
@@ -612,6 +612,7 @@ str td_col_a_srcaddr = str_init("a_srcaddr");
612 612
 str td_col_b_srcaddr = str_init("b_srcaddr");
613 613
 str td_col_s_method = str_init("s_method");
614 614
 str td_col_s_cseq = str_init("s_cseq");
615
+str td_col_x_context = str_init("x_context");
615 616
 
616 617
 str tt_table_name = str_init("topos_t");
617 618
 str tt_col_rectime = str_init("rectime");
... ...
@@ -634,6 +635,7 @@ str tt_col_a_tag = str_init("a_tag");
634 635
 str tt_col_b_tag = str_init("b_tag");
635 636
 str tt_col_s_method = str_init("s_method");
636 637
 str tt_col_s_cseq = str_init("s_cseq");
638
+str tt_col_x_context = str_init("x_context");
637 639
 
638 640
 #define TPS_NR_KEYS	48
639 641
 
... ...
@@ -763,6 +765,13 @@ int tps_db_insert_dialog(tps_data_t *td)
763 765
 	db_vals[nr_keys].val.str_val = TPS_STRZ(td->s_cseq);
764 766
 	nr_keys++;
765 767
 
768
+	if(td->x_context.len>0) {
769
+		db_keys[nr_keys] = &td_col_x_context;
770
+		db_vals[nr_keys].type = DB1_STR;
771
+		db_vals[nr_keys].val.str_val = TPS_STRZ(td->x_context);
772
+		nr_keys++;
773
+	}
774
+
766 775
 	if (_tpsdbf.use_table(_tps_db_handle, &td_table_name) < 0) {
767 776
 		LM_ERR("failed to perform use table\n");
768 777
 		return -1;
... ...
@@ -949,6 +958,13 @@ int tps_db_insert_branch(tps_data_t *td)
949 958
 	db_vals[nr_keys].val.str_val = TPS_STRZ(td->b_tag);
950 959
 	nr_keys++;
951 960
 
961
+	if(td->x_context.len>0) {
962
+		db_keys[nr_keys] = &tt_col_x_context;
963
+		db_vals[nr_keys].type = DB1_STR;
964
+		db_vals[nr_keys].val.str_val = TPS_STRZ(td->x_context);
965
+		nr_keys++;
966
+	}
967
+
952 968
 	if (_tpsdbf.use_table(_tps_db_handle, &tt_table_name) < 0) {
953 969
 		LM_ERR("failed to perform use table\n");
954 970
 		return -1;
... ...
@@ -1054,9 +1070,9 @@ int tps_db_clean_branches(void)
1054 1070
 int tps_db_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1055 1071
 		uint32_t mode)
1056 1072
 {
1057
-	db_key_t db_keys[4];
1058
-	db_op_t  db_ops[4];
1059
-	db_val_t db_vals[4];
1073
+	db_key_t db_keys[5];
1074
+	db_op_t  db_ops[5];
1075
+	db_val_t db_vals[5];
1060 1076
 	db_key_t db_cols[TPS_NR_KEYS];
1061 1077
 	db1_res_t* db_res = NULL;
1062 1078
 	str sinv = str_init("INVITE");
... ...
@@ -1073,7 +1089,8 @@ int tps_db_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1073 1089
 	nr_keys = 0;
1074 1090
 	nr_cols = 0;
1075 1091
 
1076
-	if((get_cseq(msg)->method_id == METHOD_SUBSCRIBE) || ((get_cseq(msg)->method_id == METHOD_NOTIFY) && (msg->event->len > 0))) {
1092
+	if((get_cseq(msg)->method_id == METHOD_SUBSCRIBE)
1093
+			|| ((get_cseq(msg)->method_id == METHOD_NOTIFY) && (msg->event->len > 0))) {
1077 1094
 		bInviteDlg = 0;
1078 1095
 	}
1079 1096
 
... ...
@@ -1109,6 +1126,15 @@ int tps_db_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1109 1126
 		nr_keys++;
1110 1127
 	}
1111 1128
 
1129
+	if(md->x_context.len>0) {
1130
+		db_keys[nr_keys]=&tt_col_x_context;
1131
+		db_ops[nr_keys]=OP_EQ;
1132
+		db_vals[nr_keys].type = DB1_STR;
1133
+		db_vals[nr_keys].nul = 0;
1134
+		db_vals[nr_keys].val.str_val = TPS_STRZ(md->x_context);
1135
+		nr_keys++;
1136
+	}
1137
+
1112 1138
 	db_cols[nr_cols++] = &tt_col_rectime;
1113 1139
 	db_cols[nr_cols++] = &tt_col_a_callid;
1114 1140
 	db_cols[nr_cols++] = &tt_col_a_uuid;
... ...
@@ -1129,6 +1155,9 @@ int tps_db_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1129 1155
 	db_cols[nr_cols++] = &tt_col_bs_contact;
1130 1156
 	db_cols[nr_cols++] = &tt_col_a_tag;
1131 1157
 	db_cols[nr_cols++] = &tt_col_b_tag;
1158
+	if(md->x_context.len>0) {
1159
+		db_cols[nr_cols++] = &tt_col_x_context;
1160
+	}
1132 1161
 
1133 1162
 	if (_tpsdbf.use_table(_tps_db_handle, &tt_table_name) < 0) {
1134 1163
 		LM_ERR("failed to perform use table\n");
... ...
@@ -1177,6 +1206,9 @@ int tps_db_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1177 1206
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->bs_contact); n++;
1178 1207
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->a_tag); n++;
1179 1208
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->b_tag); n++;
1209
+	if(md->x_context.len>0) {
1210
+		TPS_DATA_APPEND_DB(sd, db_res, n, &sd->x_context); n++;
1211
+	}
1180 1212
 
1181 1213
 done:
1182 1214
 	if ((db_res!=NULL) && _tpsdbf.free_result(_tps_db_handle, db_res)<0)
... ...
@@ -1205,9 +1237,9 @@ int tps_storage_load_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1205 1237
  */
1206 1238
 int tps_db_load_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd)
1207 1239
 {
1208
-	db_key_t db_keys[4];
1209
-	db_op_t  db_ops[4];
1210
-	db_val_t db_vals[4];
1240
+	db_key_t db_keys[5];
1241
+	db_op_t  db_ops[5];
1242
+	db_val_t db_vals[5];
1211 1243
 	db_key_t db_cols[TPS_NR_KEYS];
1212 1244
 	db1_res_t* db_res = NULL;
1213 1245
 	int nr_keys;
... ...
@@ -1253,6 +1285,15 @@ int tps_db_load_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd)
1253 1285
 	}
1254 1286
 	nr_keys++;
1255 1287
 
1288
+	if(md->x_context.len>0) {
1289
+		db_keys[nr_keys]=&td_col_x_context;
1290
+		db_ops[nr_keys]=OP_EQ;
1291
+		db_vals[nr_keys].type = DB1_STR;
1292
+		db_vals[nr_keys].nul = 0;
1293
+		db_vals[nr_keys].val.str_val = TPS_STRZ(md->x_context);
1294
+		nr_keys++;
1295
+	}
1296
+
1256 1297
 	db_cols[nr_cols++] = &td_col_rectime;
1257 1298
 	db_cols[nr_cols++] = &td_col_a_callid;
1258 1299
 	db_cols[nr_cols++] = &td_col_a_uuid;
... ...
@@ -1274,7 +1315,9 @@ int tps_db_load_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd)
1274 1315
 	db_cols[nr_cols++] = &td_col_b_srcaddr;
1275 1316
 	db_cols[nr_cols++] = &td_col_s_method;
1276 1317
 	db_cols[nr_cols++] = &td_col_s_cseq;
1277
-
1318
+	if(md->x_context.len>0) {
1319
+		db_cols[nr_cols++] = &td_col_x_context;
1320
+	}
1278 1321
 
1279 1322
 	if (_tpsdbf.use_table(_tps_db_handle, &td_table_name) < 0) {
1280 1323
 		LM_ERR("failed to perform use table\n");
... ...
@@ -1318,6 +1361,9 @@ int tps_db_load_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd)
1318 1361
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->b_srcaddr); n++;
1319 1362
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->s_method); n++;
1320 1363
 	TPS_DATA_APPEND_DB(sd, db_res, n, &sd->s_cseq); n++;
1364
+	if(md->x_context.len>0) {
1365
+		TPS_DATA_APPEND_DB(sd, db_res, n, &sd->x_context); n++;
1366
+	}
1321 1367
 
1322 1368
 done:
1323 1369
 	if ((db_res!=NULL) && _tpsdbf.free_result(_tps_db_handle, db_res)<0)
... ...
@@ -1388,6 +1434,15 @@ int tps_db_update_branch(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1388 1434
 	}
1389 1435
 	nr_keys++;
1390 1436
 
1437
+	if(sd->x_context.len>0) {
1438
+		db_keys[nr_keys]=&tt_col_x_context;
1439
+		db_ops[nr_keys]=OP_EQ;
1440
+		db_vals[nr_keys].type = DB1_STR;
1441
+		db_vals[nr_keys].nul = 0;
1442
+		db_vals[nr_keys].val.str_val = TPS_STRZ(sd->x_context);
1443
+		nr_keys++;
1444
+	}
1445
+
1391 1446
 	if(mode & TPS_DBU_CONTACT) {
1392 1447
 		TPS_DB_ADD_STRV(db_ucols, db_uvals, nr_ucols,
1393 1448
 				tt_col_a_contact, md->a_contact);
... ...
@@ -1494,6 +1549,15 @@ int tps_db_update_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd,
1494 1549
 	}
1495 1550
 	nr_keys++;
1496 1551
 
1552
+	if(sd->x_context.len>0) {
1553
+		db_keys[nr_keys]=&td_col_x_context;
1554
+		db_ops[nr_keys]=OP_EQ;
1555
+		db_vals[nr_keys].type = DB1_STR;
1556
+		db_vals[nr_keys].nul = 0;
1557
+		db_vals[nr_keys].val.str_val = TPS_STRZ(sd->x_context);
1558
+		nr_keys++;
1559
+	}
1560
+
1497 1561
 	if(mode & TPS_DBU_CONTACT) {
1498 1562
 		TPS_DB_ADD_STRV(db_ucols, db_uvals, nr_ucols,
1499 1563
 				td_col_a_contact, md->a_contact);
... ...
@@ -1638,6 +1702,15 @@ int tps_db_end_dialog(sip_msg_t *msg, tps_data_t *md, tps_data_t *sd)
1638 1702
 	}
1639 1703
 	nr_keys++;
1640 1704
 
1705
+	if(sd->x_context.len>0) {
1706
+		db_keys[nr_keys]=&td_col_x_context;
1707
+		db_ops[nr_keys]=OP_EQ;
1708
+		db_vals[nr_keys].type = DB1_STR;
1709
+		db_vals[nr_keys].nul = 0;
1710
+		db_vals[nr_keys].val.str_val = TPS_STRZ(sd->x_context);
1711
+		nr_keys++;
1712
+	}
1713
+
1641 1714
 	db_ucols[nr_ucols] = &td_col_rectime;
1642 1715
 	db_uvals[nr_ucols].type = DB1_DATETIME;
1643 1716
 	db_uvals[nr_ucols].val.time_val = time(NULL);
... ...
@@ -77,6 +77,7 @@ typedef struct tps_data {
77 77
 	str x_uri;
78 78
 	str s_method;
79 79
 	str s_cseq;
80
+	str x_context;
80 81
 	int32_t iflags;
81 82
 	int32_t direction;
82 83
 	uint32_t s_method_id;