Browse code

pv: added $shvinc(name) - return incremented value of $shv(inc)

- leverage internal mutex to avoid config locks

Daniel-Constantin Mierla authored on 11/05/2022 14:11:42
Showing 3 changed files
... ...
@@ -526,6 +526,8 @@ static pv_export_t mod_pvs[] = {
526 526
 
527 527
 	{ {"shv", (sizeof("shv")-1)}, PVT_OTHER, pv_get_shvar,
528 528
 		pv_set_shvar, pv_parse_shvar_name, 0, 0, 0},
529
+	{ {"shvinc", (sizeof("shvinc")-1)}, PVT_OTHER, pv_get_shvinc,
530
+		0, pv_parse_shvar_name, 0, 0, 0},
529 531
 	{ {"time", (sizeof("time")-1)}, PVT_CONTEXT, pv_get_local_time,
530 532
 		0, pv_parse_time_name, 0, 0, 0},
531 533
 	{ {"timef", (sizeof("timef")-1)}, PVT_CONTEXT, pv_get_local_strftime,
... ...
@@ -387,6 +387,38 @@ int pv_get_shvar(struct sip_msg *msg,  pv_param_t *param,
387 387
 	return 0;
388 388
 }
389 389
 
390
+int pv_get_shvinc(struct sip_msg *msg,  pv_param_t *param,
391
+		pv_value_t *res)
392
+{
393
+	int len = 0;
394
+	char *sval = NULL;
395
+	sh_var_t *shv=NULL;
396
+
397
+	if(msg==NULL || res==NULL)
398
+		return -1;
399
+
400
+	if(param==NULL || param->pvn.u.dname==0)
401
+		return pv_get_null(msg, param, res);
402
+
403
+	shv= (sh_var_t*)param->pvn.u.dname;
404
+
405
+	lock_shvar(shv);
406
+	if(shv->v.flags&VAR_VAL_STR)
407
+	{
408
+		res->ri = 0;
409
+	} else {
410
+		shv->v.value.n++;
411
+		res->ri = shv->v.value.n;
412
+	}
413
+	unlock_shvar(shv);
414
+
415
+	sval = sint2str(res->ri, &len);
416
+	res->rs.s = sval;
417
+	res->rs.len = len;
418
+	res->flags = PV_VAL_STR|PV_VAL_INT|PV_TYPE_INT;
419
+	return 0;
420
+}
421
+
390 422
 int pv_set_shvar(struct sip_msg* msg, pv_param_t *param,
391 423
 		int op, pv_value_t *val)
392 424
 {
... ...
@@ -57,6 +57,7 @@ int pv_parse_shvar_name(pv_spec_p sp, str *in);
57 57
 int pv_get_shvar(struct sip_msg *msg,  pv_param_t *param, pv_value_t *res);
58 58
 int pv_set_shvar(struct sip_msg* msg, pv_param_t *param, int op,
59 59
 		pv_value_t *val);
60
+int pv_get_shvinc(struct sip_msg *msg,  pv_param_t *param, pv_value_t *res);
60 61
 
61 62
 int shvar_init_locks(void);
62 63
 void shvar_destroy_locks(void);