Browse code

pv: added variable $msgbuf(index)

- get/set the character at the position index in the message buffer

Daniel-Constantin Mierla authored on 27/07/2021 13:44:38
Showing 3 changed files
... ...
@@ -552,6 +552,8 @@ static pv_export_t mod_pvs[] = {
552 552
 		pv_parse_via_name, 0, 0, 0},
553 553
 	{{"viaZ", (sizeof("viaZ")-1)}, PVT_OTHER, pv_get_viaZ, 0,
554 554
 		pv_parse_via_name, 0, 0, 0},
555
+	{{"msgbuf", (sizeof("msgbuf")-1)}, PVT_OTHER, pv_get_msgbuf, pv_set_msgbuf,
556
+		pv_parse_msgbuf_name, 0, 0, 0},
555 557
 
556 558
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
557 559
 };
... ...
@@ -4633,3 +4633,115 @@ int pv_get_viaZ(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
4633 4633
 
4634 4634
 	return pv_get_via_attr(msg, vbZ, param, res);
4635 4635
 }
4636
+
4637
+/**
4638
+ *
4639
+ */
4640
+int pv_parse_msgbuf_name(pv_spec_t *sp, str *in)
4641
+{
4642
+	int n;
4643
+	char *p;
4644
+	pv_spec_t *nsp = 0;
4645
+
4646
+	if(in==NULL || in->s==NULL || in->len<=0 || sp==NULL) {
4647
+		return -1;
4648
+	}
4649
+
4650
+	p = in->s;
4651
+	if(*p==PV_MARKER) {
4652
+		nsp = (pv_spec_p)pkg_malloc(sizeof(pv_spec_t));
4653
+		if(nsp==NULL) {
4654
+			LM_ERR("no more memory\n");
4655
+			return -1;
4656
+		}
4657
+		p = pv_parse_spec(in, nsp);
4658
+		if(p==NULL) {
4659
+			LM_ERR("invalid variable [%.*s]\n", in->len, in->s);
4660
+			pv_spec_free(nsp);
4661
+			return -1;
4662
+		}
4663
+		//LM_ERR("dynamic name [%.*s]\n", in->len, in->s);
4664
+		//pv_print_spec(nsp);
4665
+		sp->pvp.pvn.type = PV_NAME_PVAR;
4666
+		sp->pvp.pvn.u.dname = (void*)nsp;
4667
+		return 0;
4668
+	}
4669
+
4670
+	if (str2sint(in, &n) != 0) {
4671
+		LM_ERR("bad index value: [%.*s]\n", in->len, in->s);
4672
+		return -1;
4673
+	}
4674
+
4675
+	sp->pvp.pvn.u.isname.name.n = n;
4676
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
4677
+	sp->pvp.pvn.u.isname.type = 0;
4678
+
4679
+	return 0;
4680
+}
4681
+
4682
+/**
4683
+ *
4684
+ */
4685
+int pv_get_vparam_ival(sip_msg_t *msg, pv_param_t *param)
4686
+{
4687
+	pv_value_t vval;
4688
+
4689
+	if(param->pvn.type==PV_NAME_PVAR) {
4690
+		/* pvar */
4691
+		if(pv_get_spec_value(msg, (pv_spec_t*)(param->pvn.u.dname), &vval)!=0) {
4692
+			LM_ERR("cannot get name value\n");
4693
+			return -1;
4694
+		}
4695
+		return vval.ri;
4696
+	} else {
4697
+		return param->pvn.u.isname.name.n;
4698
+	}
4699
+}
4700
+
4701
+/**
4702
+ *
4703
+ */
4704
+int pv_get_msgbuf(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
4705
+{
4706
+	int n;
4707
+	static char outmsgbuf[4];
4708
+
4709
+	n = pv_get_vparam_ival(msg, param);
4710
+
4711
+	if(n<0 || n>=msg->len) {
4712
+		LM_ERR("index out of range\n");
4713
+		return pv_get_null(msg, param, res);
4714
+	}
4715
+	outmsgbuf[0] = msg->buf[n];
4716
+	outmsgbuf[1] = '\0';
4717
+	return pv_get_strlval(msg, param, res, outmsgbuf, 2);
4718
+}
4719
+
4720
+/**
4721
+ *
4722
+ */
4723
+int pv_set_msgbuf(sip_msg_t *msg, pv_param_t *param,
4724
+		int op, pv_value_t *val)
4725
+{
4726
+	int n;
4727
+
4728
+	n = pv_get_vparam_ival(msg, param);
4729
+
4730
+	if(n<0 || n>=msg->len) {
4731
+		LM_ERR("index out of range\n");
4732
+		return -1;
4733
+	}
4734
+
4735
+	if((val==NULL) || (val->flags&PV_VAL_NULL)) {
4736
+		LM_ERR("null value - skipping\n");
4737
+		return 1;
4738
+	}
4739
+	if(!(val->flags&PV_VAL_STR) || val->rs.len<=0) {
4740
+		LM_ERR("error - str value required\n");
4741
+		return -1;
4742
+	}
4743
+
4744
+	msg->buf[n] = val->rs.s[0];
4745
+
4746
+	return 0;
4747
+}
... ...
@@ -416,5 +416,11 @@ int pv_get_via0(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
416 416
 int pv_get_via1(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
417 417
 int pv_get_viaZ(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
418 418
 
419
+int pv_parse_msgbuf_name(pv_spec_t *sp, str *in);
420
+int pv_get_msgbuf(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
421
+int pv_set_msgbuf(sip_msg_t *msg, pv_param_t *param,
422
+		int op, pv_value_t *val);
423
+
424
+
419 425
 #endif
420 426