Module: kamailio
Branch: master
Commit: 887278c5254e35ac1a617e5fc68347829d0ac185
URL:
https://github.com/kamailio/kamailio/commit/887278c5254e35ac1a617e5fc683478…
Author: Daniel-Constantin Mierla <miconda(a)gmail.com>
Committer: Daniel-Constantin Mierla <miconda(a)gmail.com>
Date: 2018-04-20T11:58:20+02:00
core: xavp - helper function to serialize subfields in a params list
---
Modified: src/core/xavp.c
Modified: src/core/xavp.h
---
Diff:
https://github.com/kamailio/kamailio/commit/887278c5254e35ac1a617e5fc683478…
Patch:
https://github.com/kamailio/kamailio/commit/887278c5254e35ac1a617e5fc683478…
---
diff --git a/src/core/xavp.c b/src/core/xavp.c
index 27a200fbfb..c9529a02f6 100644
--- a/src/core/xavp.c
+++ b/src/core/xavp.c
@@ -843,4 +843,65 @@ sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname)
return vavp;
}
+
+
+/**
+ * serialize the values in subfields of an xavp in name=value; format
+ * - rname - name of the root list xavp
+ * - obuf - buffer were to write the output
+ * - olen - the size of obuf
+ * return: 0 - not found; -1 - error; >0 - length of output
+ */
+
+int xavp_serialize_fields(str *rname, char *obuf, int olen)
+{
+ sr_xavp_t *ravp = NULL;
+ sr_xavp_t *avp = NULL;
+ str ostr;
+ int rlen;
+
+ ravp = xavp_get(rname, NULL);
+ if(ravp==NULL || ravp->val.type!=SR_XTYPE_XAVP) {
+ /* not found or not holding subfields */
+ return 0;
+ }
+
+ rlen = 0;
+ ostr.s = obuf;
+ avp = ravp->val.v.xavp;
+ while(avp) {
+ switch(avp->val.type) {
+ case SR_XTYPE_INT:
+ LM_DBG(" XAVP int value: %d\n", avp->val.v.i);
+ ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%u;",
+ avp->name.len, avp->name.s, (unsigned int)avp->val.v.i);
+ if(ostr.len<=0 || ostr.len>=olen-rlen) {
+ LM_ERR("failed to serialize int value (%d/%d\n",
+ ostr.len, olen-rlen);
+ return -1;
+ }
+ break;
+ case SR_XTYPE_STR:
+ LM_DBG(" XAVP str value: %s\n", avp->val.v.s.s);
+ ostr.len = snprintf(ostr.s, olen-rlen, "%.*s=%.*s;",
+ avp->name.len, avp->name.s,
+ avp->val.v.s.len, avp->val.v.s.s);
+ if(ostr.len<=0 || ostr.len>=olen-rlen) {
+ LM_ERR("failed to serialize int value (%d/%d\n",
+ ostr.len, olen-rlen);
+ return -1;
+ }
+ break;
+ default:
+ LM_DBG("skipping value type: %d\n", avp->val.type);
+ ostr.len = 0;
+ }
+ if(ostr.len>0) {
+ ostr.s += ostr.len;
+ rlen += ostr.len;
+ }
+ avp = avp->next;
+ }
+ return rlen;
+}
#endif
diff --git a/src/core/xavp.h b/src/core/xavp.h
index 42413e70b7..8014bc5694 100644
--- a/src/core/xavp.h
+++ b/src/core/xavp.h
@@ -101,6 +101,8 @@ sr_xavp_t *xavp_clone_level_nodata(sr_xavp_t *xold);
sr_xavp_t* xavp_get_child(str *rname, str *cname);
sr_xavp_t* xavp_get_child_with_ival(str *rname, str *cname);
sr_xavp_t* xavp_get_child_with_sval(str *rname, str *cname);
+int xavp_serialize_fields(str *rname, char *obuf, int olen);
+
#endif
#endif