int pv_xavp_to_var(str *xname) { sr_xavp_t *xavp; sr_xavp_t *avp; LM_DBG("xname:%.*s\n", xname->len, xname->s); xavp = xavp_get_by_index(xname, 0, NULL); if(!xavp) { LM_ERR("xavp [%.*s] not found\n", xname->len, xname->s); return -1; } if(xavp->val.type!=SR_XTYPE_XAVP){ LM_ERR("%.*s not xavp type?\n", xname->len, xname->s); return -1; } avp = xavp->val.v.xavp; if (avp) { if(pv_xavp_to_var_helper(avp)<0) return -1; avp = avp->next; } while(avp) { if(pv_xavp_to_var_helper(avp)<0) return -1; avp = avp->next; } return 1; }
/** * puts a table with content of a xavp */ static int lua_sr_xavp_get(lua_State *L) { str xavp_name; int indx = 0; sr_lua_env_t *env_L; sr_xavp_t *avp; int num_param = 0; int param = -1; int simple_flag = 0; env_L = sr_lua_env_get(); num_param = lua_gettop(L); if(num_param<2 && num_param>3) { LM_ERR("wrong number of parameters [%d]\n", num_param); return 0; } if(num_param==3) { if(!lua_isnumber(L, param)) { LM_ERR("invalid int parameter\n"); return 0; } simple_flag = lua_tointeger(L, param); param = param - 1; } if(!lua_isnumber(L, param)) { LM_ERR("invalid int parameter\n"); return 0; } indx = lua_tointeger(L, param); param = param - 1; xavp_name.s = (char*)lua_tostring(L, param); if(xavp_name.s==NULL || env_L->msg==NULL) return 0; xavp_name.len = strlen(xavp_name.s); avp = xavp_get_by_index(&xavp_name, indx, NULL); if(avp==NULL){ LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx); lua_pushnil(L); return 1; } if (simple_flag != 0) { lua_sr_push_xavp_table_simple(L, avp); } else { lua_sr_push_xavp_table(L, avp); } return 1; }
/** * puts a table with the list of keys of the xavp */ static int lua_sr_xavp_get_keys (lua_State *L) { str xavp_name; int indx = 0; sr_lua_env_t *env_L; sr_xavp_t *avp; struct str_list *keys, *k; env_L = sr_lua_env_get(); if(lua_gettop(L)<2) { LM_ERR("to few parameters [%d]\n",lua_gettop(L)); return 0; } if(!lua_isnumber(L, -1)) { LM_ERR("invalid int parameter\n"); return 0; } indx = lua_tointeger(L, -1); xavp_name.s = (char*)lua_tostring(L, -2); if(xavp_name.s==NULL || env_L->msg==NULL) return 0; xavp_name.len = strlen(xavp_name.s); avp = xavp_get_by_index(&xavp_name, indx, NULL); if(avp==NULL){ LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx); lua_pushnil(L); return 1; } keys = xavp_get_list_key_names(avp); lua_sr_push_str_list_table(L, keys); // free list while(keys!=NULL){ k = keys; keys = k->next; pkg_free(k); } return 1; }
int _cfgt_get_obj_xavp_vals(struct sip_msg *msg, pv_param_t *param, srjson_doc_t *jdoc, srjson_t **jobjr, str *item_name) { pv_xavp_name_t *xname = (pv_xavp_name_t*)param->pvn.u.dname; sr_xavp_t *xavp = NULL; sr_xavp_t *avp = NULL; srjson_t *jobj = NULL; srjson_t *jobjt = NULL; struct str_list *keys; struct str_list *k; *jobjr = srjson_CreateArray(jdoc); if(*jobjr==NULL) { LM_ERR("cannot create json object\n"); return -1; } item_name->s = xname->name.s; item_name->len = xname->name.len; xavp = xavp_get_by_index(&xname->name, 0, NULL); if(xavp==NULL) { return 0; /* empty */ } do { if(xavp->val.type==SR_XTYPE_XAVP) { avp = xavp->val.v.xavp; jobj = srjson_CreateObject(jdoc); if(jobj==NULL) { LM_ERR("cannot create json object\n"); return -1; } keys = xavp_get_list_key_names(xavp); if(keys!=NULL) { do { _cfgt_get_obj_avp_vals(keys->s, avp, jdoc, &jobjt); srjson_AddStrItemToObject(jdoc, jobj, keys->s.s, keys->s.len, jobjt); k = keys; keys = keys->next; pkg_free(k); jobjt = NULL; }while(keys!=NULL); } } if(jobj!=NULL) { srjson_AddItemToArray(jdoc, *jobjr, jobj); jobj = NULL; } }while((xavp = xavp_get_next(xavp))!=0); return 0; }
int pv_get_xavp(struct sip_msg *msg, pv_param_t *param, pv_value_t *res) { pv_xavp_name_t *xname=NULL; sr_xavp_t *avp=NULL; int idxf = 0; int idx = 0; int count; char *p, *p_ini; int p_size; if(param==NULL) { LM_ERR("bad parameters\n"); return -1; } xname = (pv_xavp_name_t*)param->pvn.u.dname; if(xname->index.type==PVT_EXTRA) { /* get the index */ if(pv_get_spec_index(msg, &xname->index.pvp, &idx, &idxf)!=0) { LM_ERR("invalid index\n"); return -1; } } /* fix the index */ if(idx<0) { count = xavp_count(&xname->name, NULL); idx = count + idx; } avp = xavp_get_by_index(&xname->name, idx, NULL); if(avp==NULL) return pv_get_null(msg, param, res); if(xname->next==NULL) return pv_xavp_get_value(msg, param, res, avp); if(avp->val.type != SR_XTYPE_XAVP) return pv_get_null(msg, param, res); idx = 0; idxf = 0; if(xname->next->index.type==PVT_EXTRA) { /* get the index */ if(pv_get_spec_index(msg, &xname->next->index.pvp, &idx, &idxf)!=0) { LM_ERR("invalid index\n"); return -1; } } /* fix the index */ if(idx<0) { count = xavp_count(&xname->next->name, &avp->val.v.xavp); idx = count + idx; } avp = xavp_get_by_index(&xname->next->name, idx, &avp->val.v.xavp); if(avp==NULL) return pv_get_null(msg, param, res); /* get all values of second key */ if(idxf==PV_IDX_ALL) { p_ini = pv_get_buffer(); p = p_ini; p_size = pv_get_buffer_size(); do { if(p!=p_ini) { if(p-p_ini+PV_FIELD_DELIM_LEN+1>p_size) { LM_ERR("local buffer length exceeded\n"); return pv_get_null(msg, param, res); } memcpy(p, PV_FIELD_DELIM, PV_FIELD_DELIM_LEN); p += PV_FIELD_DELIM_LEN; } if(pv_xavp_get_value(msg, param, res, avp)<0) { LM_ERR("can get value\n"); return pv_get_null(msg, param, res); } if(p-p_ini+res->rs.len+1>p_size) { LM_ERR("local buffer length exceeded!\n"); return pv_get_null(msg, param, res); } memcpy(p, res->rs.s, res->rs.len); p += res->rs.len; } while ((avp=xavp_get_next(avp))!=0); res->rs.s = p_ini; res->rs.len = p - p_ini; return 0; } return pv_xavp_get_value(msg, param, res, avp); }
/** * $xavp(name1[idx1]=>name2[idx2]) */ int pv_set_xavp(struct sip_msg* msg, pv_param_t *param, int op, pv_value_t *val) { pv_xavp_name_t *xname=NULL; sr_xavp_t *avp=NULL; sr_xavp_t *list=NULL; sr_xval_t xval; int idxf = 0; int idx = 0; int idxf1 = 0; int idx1 = 0; int count; if(param==NULL) { LM_ERR("bad parameters\n"); return -1; } xname = (pv_xavp_name_t*)param->pvn.u.dname; if(xname->index.type==PVT_EXTRA) { /* get the index */ if(pv_get_spec_index(msg, &xname->index.pvp, &idx, &idxf)!=0) { LM_ERR("invalid index\n"); return -1; } } if((val==NULL) || (val->flags&PV_VAL_NULL)) { if(xname->next==NULL) { if(xname->index.type==PVT_EXTRA) { if(idxf==PV_IDX_ALL) { xavp_rm_by_name(&xname->name, 1, NULL); return 0; } } if(idx==0) { xavp_rm_by_name(&xname->name, 0, NULL); return 0; } /* fix the index */ if(idx<0) { count = xavp_count(&xname->name, NULL); idx = count + idx + 1; } xavp_rm_by_index(&xname->name, idx, NULL); return 0; } if(xname->next->index.type==PVT_EXTRA) { /* get the index */ if(pv_get_spec_index(msg,&xname->next->index.pvp,&idx1,&idxf1)!=0) { LM_ERR("invalid index!\n"); return -1; } } if(idxf==PV_IDX_ALL) { /* iterate */ avp = xavp_get(&xname->name, NULL); while(avp) { if(avp->val.type==SR_XTYPE_XAVP) { if(xname->next->index.type==PVT_EXTRA) { if(idxf1==PV_IDX_ALL) { xavp_rm_by_name(&xname->next->name, 1, &avp->val.v.xavp); } else { /* fix the index */ idx = idx1; if(idx<0) { count = xavp_count(&xname->next->name, &avp->val.v.xavp); idx = count + idx1 + 1; } xavp_rm_by_index(&xname->next->name, idx, &avp->val.v.xavp); } } else { xavp_rm_by_name(&xname->next->name, 0, &avp->val.v.xavp); } } avp = xavp_get_next(avp); } return 0; } if(idx==0) { avp = xavp_get(&xname->name, NULL); } else { /* fix the index */ if(idx<0) { count = xavp_count(&xname->name, NULL); idx = count + idx + 1; } avp = xavp_get_by_index(&xname->name, idx, NULL); } if(avp) { if(avp->val.type==SR_XTYPE_XAVP) { if(xname->next->index.type==PVT_EXTRA) { if(idxf1==PV_IDX_ALL) { xavp_rm_by_name(&xname->next->name, 1, &avp->val.v.xavp); } else { /* fix the index */ idx = idx1; if(idx<0) { count = xavp_count(&xname->next->name, &avp->val.v.xavp); idx = count + idx1 + 1; } xavp_rm_by_index(&xname->next->name, idx, &avp->val.v.xavp); } } else { xavp_rm_by_name(&xname->next->name, 0, &avp->val.v.xavp); } } } return 0; } /* NULL assignment */ /* build xavp value */ memset(&xval, 0, sizeof(sr_xval_t)); if(val->flags&PV_TYPE_INT) { xval.type = SR_XTYPE_INT; xval.v.i = val->ri; } else { xval.type = SR_XTYPE_STR; xval.v.s = val->rs; } /* where to add */ if(xname->next==NULL) { /* xavp with single value */ if(xname->index.type==PVT_EXTRA) { if(idxf==PV_IDX_ALL) { /* ignore: should iterate and set same value to all xavps * with same name?!?! */ return -1; } /* fix the index */ if(idx<0) { count = xavp_count(&xname->name, NULL); idx = count + idx + 1; } /* set the value */ if(xavp_set_value(&xname->name, idx, &xval, NULL)==NULL) return -1; return 0; } /* add new value */ if(xavp_add_value(&xname->name, &xval, NULL)==NULL) return -1; return 0; } /* xavp with xavp list value */ if(xname->next->index.type==PVT_EXTRA) { /* get the index */ if(pv_get_spec_index(msg,&xname->next->index.pvp,&idx1,&idxf1)!=0) { LM_ERR("invalid index!\n"); return -1; } } if(xname->index.type==PVT_EXTRA) { /* set the value */ if(idxf==PV_IDX_ALL) { /* ignore: should iterate and set same value to all xavps * with same name?!?! */ return 0; } if(idx==0) { avp = xavp_get(&xname->name, NULL); } else { /* fix the index */ if(idx<0) { count = xavp_count(&xname->name, NULL); idx = count + idx + 1; } avp = xavp_get_by_index(&xname->name, idx, NULL); } if(avp==NULL) return 0; if(avp->val.type!=SR_XTYPE_XAVP) return -1; if(xname->next->index.type==PVT_EXTRA) { if(idxf1==PV_IDX_ALL) { /* ignore: should iterate and set same value to all xavps * with same name?!?! */ return 0; } /* fix the index */ idx = idx1; if(idx<0) { count = xavp_count(&xname->next->name, &avp->val.v.xavp); idx = count + idx1 + 1; } /* set value */ xavp_set_value(&xname->next->name, idx, &xval, &avp->val.v.xavp); return 0; } /* add new value in sublist */ if(xavp_add_value(&xname->next->name, &xval, &avp->val.v.xavp)==NULL) return -1; return 0; } /* add new xavp with xavp list */ if(xavp_add_value(&xname->next->name, &xval, &list)==NULL) return -1; /* build xavp value */ memset(&xval, 0, sizeof(sr_xval_t)); xval.type = SR_XTYPE_XAVP; xval.v.xavp = list; xavp_add_value(&xname->name, &xval, NULL); return 0; }
/** * puts a table with content of a xavp */ static int lua_sr_xavp_get(lua_State *L) { str xavp_name; int indx = 0; sr_lua_env_t *env_L; sr_xavp_t *avp; int num_param = 0; int param = -1; int all_flag = 0; int simple_flag = 0; lua_Number elem = 1; int xavp_size = 0; env_L = sr_lua_env_get(); num_param = lua_gettop(L); if(num_param<2 && num_param>3) { LM_ERR("wrong number of parameters [%d]\n", num_param); return 0; } if(num_param==3) { if(!lua_isnumber(L, param)) { LM_ERR("invalid int parameter\n"); return 0; } simple_flag = lua_tointeger(L, param); param = param - 1; } if(!lua_isnumber(L, param)) { if(lua_isnil(L, param)) { all_flag = 1; } else { LM_ERR("invalid parameter, must be int or nil\n"); return 0; } } else { indx = lua_tointeger(L, param); } param = param - 1; xavp_name.s = (char*)lua_tostring(L, param); if(xavp_name.s==NULL || env_L->msg==NULL) { LM_ERR("No xavp name in %d param\n", param); return 0; } xavp_name.len = strlen(xavp_name.s); if(all_flag>0) { indx = 0; lua_newtable(L); } xavp_size = xavp_count(&xavp_name, NULL); if(indx<0) { if((indx*-1)>xavp_size) { LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx); lua_pushnil(L); return 1; } indx = xavp_size + indx; } avp = xavp_get_by_index(&xavp_name, indx, NULL); do { if(avp==NULL){ LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx); lua_pushnil(L); return 1; } if(all_flag!=0) { lua_pushnumber(L, elem); elem = elem + 1; } lua_sr_push_xavp_table(L, avp, simple_flag); if(all_flag!=0) { lua_rawset(L, -3); indx = indx + 1; avp = xavp_get_by_index(&xavp_name, indx, NULL); } else return 1; }while(avp!=NULL); return 1; }