[sr-dev] git:master: pv: new variable $cnt(...) to count the number of other array variables

Daniel-Constantin Mierla miconda at gmail.com
Tue Jun 19 15:08:07 CEST 2012


Module: sip-router
Branch: master
Commit: 06982365e6d361d8fb78e1ece579eb1de87db643
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=06982365e6d361d8fb78e1ece579eb1de87db643

Author: Daniel-Constantin Mierla <miconda at gmail.com>
Committer: Daniel-Constantin Mierla <miconda at gmail.com>
Date:   Tue Jun 19 15:06:03 2012 +0200

pv: new variable $cnt(...) to count the number of other array variables

- for now it supports counting AVPS - $cnt($avp(x) - returns the number
  of how many AVPs with name x exist
- future plans - count headers with same name, xavps ...

---

 modules_k/pv/pv.c      |    3 ++
 modules_k/pv/pv_core.c |   60 ++++++++++++++++++++++++++++++++++++++++++++++++
 modules_k/pv/pv_core.h |    6 ++++
 3 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/modules_k/pv/pv.c b/modules_k/pv/pv.c
index 93955a1..6b7dccb 100644
--- a/modules_k/pv/pv.c
+++ b/modules_k/pv/pv.c
@@ -132,6 +132,9 @@ static pv_export_t mod_pvs[] = {
 	{{"cl", (sizeof("cl")-1)}, /* */
 		PVT_OTHER, pv_get_content_length, 0,
 		0, 0, 0, 0},
+	{{"cnt", sizeof("cnt")-1},
+		PVT_OTHER, pv_get_cnt, 0,
+		pv_parse_cnt_name, 0, 0, 0 },
 	{{"cs", (sizeof("cs")-1)}, /* */
 		PVT_OTHER, pv_get_cseq, 0,
 		0, 0, 0, 0},
diff --git a/modules_k/pv/pv_core.c b/modules_k/pv/pv_core.c
index 370ec1a..2a27490 100644
--- a/modules_k/pv/pv_core.c
+++ b/modules_k/pv/pv_core.c
@@ -1587,6 +1587,34 @@ int pv_get_server_id(struct sip_msg *msg, pv_param_t *param,
 	return pv_get_sintval(msg, param, res, server_id);
 }
 
+int pv_get_cnt(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res)
+{
+	int_str avp_name;
+	unsigned short avp_type = 0;
+	avp_search_state_t state;
+	pv_spec_t *pv=NULL;
+	unsigned int n = 0;
+	avp_t *avp;
+
+	pv = (pv_spec_t*)param->pvn.u.dname;
+	if(pv==NULL)
+		return pv_get_null(msg, param, res);
+
+	if(pv_get_avp_name(0, &pv->pvp, &avp_name, &avp_type)!=0)
+	{
+		LM_ERR("invalid AVP definition\n");
+		return pv_get_null(msg, param, res);
+	}
+	avp=search_first_avp(avp_type, avp_name, NULL, &state);
+	while(avp) {
+		n++;
+		avp=search_next_avp(&state, NULL); 
+	}
+
+	return pv_get_uintval(msg, param, res, n);
+}
+
 
 /********* end PV get functions *********/
 
@@ -2398,3 +2426,35 @@ error:
 	return -1;
 }
 
+int pv_parse_cnt_name(pv_spec_p sp, str *in)
+{
+	pv_spec_t *pv=NULL;
+
+	if(in->s==NULL || in->len<=0)
+		return -1;
+
+	pv = (pv_spec_t*)pkg_malloc(sizeof(pv_spec_t));
+	if(pv==NULL)
+		return -1;
+
+	memset(pv, 0, sizeof(pv_spec_t));
+
+	if(pv_parse_spec(in, pv)==NULL)
+		goto error;
+
+	if(pv->type!=PVT_AVP)
+	{
+		LM_ERR("expected avp name instead of [%.*s]\n", in->len, in->s);
+		goto error;
+	}
+
+	sp->pvp.pvn.u.dname = (void*)pv;
+	sp->pvp.pvn.type = PV_NAME_PVAR;
+	return 0;
+
+error:
+	LM_ERR("invalid pv name [%.*s]\n", in->len, in->s);
+	if(pv!=NULL)
+		pkg_free(pv);
+	return -1;
+}
diff --git a/modules_k/pv/pv_core.h b/modules_k/pv/pv_core.h
index 4fb83d5..0149d24 100644
--- a/modules_k/pv/pv_core.h
+++ b/modules_k/pv/pv_core.h
@@ -201,6 +201,10 @@ int pv_get_version(struct sip_msg *msg, pv_param_t *param,
 
 int pv_get_server_id(struct sip_msg *msg, pv_param_t *param,
 		pv_value_t *res);
+
+int pv_get_cnt(struct sip_msg *msg, pv_param_t *param,
+		pv_value_t *res);
+
 /********* end PV get functions *********/
 
 /********* start PV set functions *********/
@@ -270,5 +274,7 @@ int pv_parse_scriptvar_name(pv_spec_p sp, str *in);
 
 int pv_parse_hdr_name(pv_spec_p sp, str *in);
 
+int pv_parse_cnt_name(pv_spec_p sp, str *in);
+
 #endif
 




More information about the sr-dev mailing list