123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422 |
- /**
- * $Id$
- *
- * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
- *
- * This file is part of Kamailio, a free SIP server.
- *
- * Kamailio is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version
- *
- * Kamailio is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/stat.h>
- #include "../../sr_module.h"
- #include "../../dprint.h"
- #include "../../route_struct.h"
- #include "../../action.h"
- #include "../../ut.h"
- #include "../../mem/mem.h"
- #include "../../data_lump.h"
- #include "../../data_lump_rpl.h"
- #include "../../forward.h"
- #include "../../flags.h"
- #include "../../dset.h"
- #include "../../parser/parse_uri.h"
- #include "../../lib/kcore/cmpapi.h"
- #include "../../xavp.h"
- #include "app_lua_api.h"
- #include "app_lua_sr.h"
- /**
- *
- */
- static int lua_sr_probe (lua_State *L)
- {
- LM_DBG("someone probing from lua\n");
- return 0;
- }
- /**
- *
- */
- static int lua_sr_dbg (lua_State *L)
- {
- char *txt;
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- LM_DBG("%s", txt);
- return 0;
- }
- /**
- *
- */
- static int lua_sr_err (lua_State *L)
- {
- char *txt;
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- LM_ERR("%s", txt);
- return 0;
- }
- /**
- *
- */
- static int lua_sr_log (lua_State *L)
- {
- char *txt;
- char *level;
- level = (char*)lua_tostring(L, -2);
- txt = (char*)lua_tostring(L, -1);
- if(txt!=NULL)
- {
- if(level==NULL)
- {
- LM_ERR("%s", txt);
- } else {
- if(strcasecmp(level, "dbg")==0) {
- LM_DBG("%s", txt);
- } else if(strcasecmp(level, "info")==0) {
- LM_INFO("%s", txt);
- } else if(strcasecmp(level, "warn")==0) {
- LM_WARN("%s", txt);
- } else if(strcasecmp(level, "crit")==0) {
- LM_CRIT("%s", txt);
- } else {
- LM_ERR("%s", txt);
- }
- }
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_modf (lua_State *L)
- {
- int ret;
- char *luav[MAX_ACTIONS];
- char *argv[MAX_ACTIONS];
- int argc;
- int i;
- int mod_type;
- struct run_act_ctx ra_ctx;
- unsigned modver;
- struct action *act;
- sr31_cmd_export_t* expf;
- sr_lua_env_t *env_L;
- ret = 1;
- act = NULL;
- argc = 0;
- memset(luav, 0, MAX_ACTIONS*sizeof(char*));
- memset(argv, 0, MAX_ACTIONS*sizeof(char*));
- env_L = sr_lua_env_get();
- if(env_L->msg==NULL)
- goto error;
- #if 0
- app_lua_dump_stack(L);
- #endif
- argc = lua_gettop(L);
- if(argc==0)
- {
- LM_ERR("name of module function not provided\n");
- goto error;
- }
- if(argc>=MAX_ACTIONS)
- {
- LM_ERR("too many parameters\n");
- goto error;
- }
- /* first is function name, then parameters */
- for(i=1; i<=argc; i++)
- {
- if (!lua_isstring(L, i))
- {
- LM_ERR("invalid parameter type (%d)\n", i);
- goto error;
- }
- luav[i-1] = (char*)lua_tostring(L, i);
- }
- /* pkg copy only parameters */
- for(i=1; i<MAX_ACTIONS; i++)
- {
- if(luav[i]!=NULL)
- {
- argv[i] = (char*)pkg_malloc(strlen(luav[i])+1);
- if(argv[i]==NULL)
- {
- LM_ERR("no more pkg\n");
- goto error;
- }
- strcpy(argv[i], luav[i]);
- }
- }
- expf = find_export_record(luav[0], argc-1, 0, &modver);
- if (expf==NULL) {
- LM_ERR("function '%s' is not available\n", luav[0]);
- goto error;
- }
- /* check fixups */
- if (expf->fixup!=NULL && expf->free_fixup==NULL) {
- LM_ERR("function '%s' has fixup - cannot be used\n", luav[0]);
- goto error;
- }
- switch(expf->param_no) {
- case 0:
- mod_type = MODULE0_T;
- break;
- case 1:
- mod_type = MODULE1_T;
- break;
- case 2:
- mod_type = MODULE2_T;
- break;
- case 3:
- mod_type = MODULE3_T;
- break;
- case 4:
- mod_type = MODULE4_T;
- break;
- case 5:
- mod_type = MODULE5_T;
- break;
- case 6:
- mod_type = MODULE6_T;
- break;
- case VAR_PARAM_NO:
- mod_type = MODULEX_T;
- break;
- default:
- LM_ERR("unknown/bad definition for function '%s' (%d params)\n",
- luav[0], expf->param_no);
- goto error;
- }
- act = mk_action(mod_type, argc+1 /* number of (type, value) pairs */,
- MODEXP_ST, expf, /* function */
- NUMBER_ST, argc-1, /* parameter number */
- STRING_ST, argv[1], /* param. 1 */
- STRING_ST, argv[2], /* param. 2 */
- STRING_ST, argv[3], /* param. 3 */
- STRING_ST, argv[4], /* param. 4 */
- STRING_ST, argv[5], /* param. 5 */
- STRING_ST, argv[6] /* param. 6 */
- );
- if (act==NULL) {
- LM_ERR("action structure could not be created for '%s'\n", luav[0]);
- goto error;
- }
- /* handle fixups */
- if (expf->fixup) {
- if(argc==1)
- { /* no parameters */
- if(expf->fixup(0, 0)<0)
- {
- LM_ERR("Error in fixup (0) for '%s'\n", luav[0]);
- goto error;
- }
- } else {
- for(i=1; i<argc; i++)
- {
- if(expf->fixup(&(act->val[i+1].u.data), i)<0)
- {
- LM_ERR("Error in fixup (%d) for '%s'\n", i, luav[0]);
- goto error;
- }
- act->val[i+1].type = MODFIXUP_ST;
- }
- }
- }
- init_run_actions_ctx(&ra_ctx);
- ret = do_action(&ra_ctx, act, env_L->msg);
- /* free fixups */
- if (expf->fixup) {
- for(i=1; i<argc; i++)
- {
- if ((act->val[i+1].type == MODFIXUP_ST) && (act->val[i+1].u.data))
- {
- expf->free_fixup(&(act->val[i+1].u.data), i);
- }
- }
- }
- pkg_free(act);
- for(i=0; i<MAX_ACTIONS; i++)
- {
- if(argv[i]!=NULL) pkg_free(argv[i]);
- argv[i] = 0;
- }
- lua_pushinteger(L, ret);
- return 1;
- error:
- if(act!=NULL)
- pkg_free(act);
- for(i=0; i<MAX_ACTIONS; i++)
- {
- if(argv[i]!=NULL) pkg_free(argv[i]);
- argv[i] = 0;
- }
- lua_pushinteger(L, -1);
- return 1;
- }
- /**
- *
- */
- static int lua_sr_is_myself (lua_State *L)
- {
- str uri;
- struct sip_uri puri;
- int ret;
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(uri.len>4 && (strncmp(uri.s, "sip:", 4)==0
- || strncmp(uri.s, "sips:", 5)==0))
- {
- if(parse_uri(uri.s, uri.len, &puri)!=0)
- {
- LM_ERR("failed to parse uri [%s]\n", uri.s);
- return app_lua_return_false(L);
- }
- ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
- (puri.transport_val.s)?puri.proto:0);
- } else {
- ret = check_self(&uri, 0, 0);
- }
- if(ret==1)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_setflag (lua_State *L)
- {
- int flag;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- setflag(env_L->msg, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetflag (lua_State *L)
- {
- int flag;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- resetflag(env_L->msg, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_isflagset (lua_State *L)
- {
- int flag;
- int ret;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- flag = lua_tointeger(L, -1);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- ret = isflagset(env_L->msg, flag);
- if(ret>0)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_setbflag (lua_State *L)
- {
- int flag;
- int branch;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- setbflag(branch, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetbflag (lua_State *L)
- {
- int flag;
- int branch;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- resetbflag(branch, flag);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_isbflagset (lua_State *L)
- {
- int flag;
- int branch;
- int ret;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(lua_gettop(L)==1)
- {
- flag = lua_tointeger(L, -1);
- branch = 0;
- } else if(lua_gettop(L)==2) {
- flag = lua_tointeger(L, -2);
- branch = lua_tointeger(L, -1);
- } else {
- LM_WARN("invalid number of parameters from Lua\n");
- return app_lua_return_false(L);
- }
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (!flag_in_range(flag))
- {
- LM_ERR("invalid flag parameter %d\n", flag);
- return app_lua_return_false(L);
- }
- ret = isbflagset(branch, flag);
- if(ret>0)
- return app_lua_return_true(L);
- return app_lua_return_false(L);
- }
- /**
- *
- */
- static int lua_sr_seturi (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_URI_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_setuser (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_USER_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_sethost (lua_State *L)
- {
- struct action act;
- struct run_act_ctx h;
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- memset(&act, 0, sizeof(act));
- act.val[0].type = STRING_ST;
- act.val[0].u.string = uri.s;
- act.type = SET_HOST_T;
- init_run_actions_ctx(&h);
- if (do_action(&h, &act, env_L->msg)<0)
- {
- LM_ERR("do action failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_setdsturi (lua_State *L)
- {
- str uri;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- uri.s = (char*)lua_tostring(L, -1);
- if(uri.s==NULL)
- {
- LM_ERR("invalid uri parameter\n");
- return app_lua_return_false(L);
- }
- uri.len = strlen(uri.s);
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- if (set_dst_uri(env_L->msg, &uri)<0)
- {
- LM_ERR("setting dst uri failed\n");
- return app_lua_return_false(L);
- }
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static int lua_sr_resetdsturi (lua_State *L)
- {
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- if(env_L->msg==NULL)
- {
- LM_WARN("invalid parameters from Lua env\n");
- return app_lua_return_false(L);
- }
- reset_dst_uri(env_L->msg);
- return app_lua_return_true(L);
- }
- /**
- *
- */
- static const luaL_Reg _sr_core_Map [] = {
- {"probe", lua_sr_probe},
- {"dbg", lua_sr_dbg},
- {"err", lua_sr_err},
- {"log", lua_sr_log},
- {"modf", lua_sr_modf},
- {"is_myself", lua_sr_is_myself},
- {"setflag", lua_sr_setflag},
- {"resetflag", lua_sr_resetflag},
- {"isflagset", lua_sr_isflagset},
- {"setbflag", lua_sr_setbflag},
- {"resetbflag", lua_sr_resetbflag},
- {"isbflagset", lua_sr_isbflagset},
- {"seturi", lua_sr_seturi},
- {"setuser", lua_sr_setuser},
- {"sethost", lua_sr_sethost},
- {"setdsturi", lua_sr_setdsturi},
- {"resetdsturi", lua_sr_resetdsturi},
- {NULL, NULL}
- };
- /**
- *
- */
- static int lua_sr_hdr_append (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- int len;
- char *hdr;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("append hf: %s\n", txt);
- if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1)
- {
- LM_ERR("error while parsing message\n");
- return 0;
- }
- hf = env_L->msg->last_header;
- len = strlen(txt);
- hdr = (char*)pkg_malloc(len);
- if(hdr==NULL)
- {
- LM_ERR("no pkg memory left\n");
- return 0;
- }
- memcpy(hdr, txt, len);
- anchor = anchor_lump(env_L->msg,
- hf->name.s + hf->len - env_L->msg->buf, 0, 0);
- if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
- {
- LM_ERR("can't insert lump\n");
- pkg_free(hdr);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_remove (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- str hname;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("remove hf: %s\n", txt);
- if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1) {
- LM_ERR("error while parsing message\n");
- return 0;
- }
- hname.s = txt;
- hname.len = strlen(txt);
- for (hf=env_L->msg->headers; hf; hf=hf->next)
- {
- if (cmp_hdrname_str(&hf->name, &hname)==0)
- {
- anchor=del_lump(env_L->msg,
- hf->name.s - env_L->msg->buf, hf->len, 0);
- if (anchor==0)
- {
- LM_ERR("cannot remove hdr %s\n", txt);
- return 0;
- }
- }
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_insert (lua_State *L)
- {
- struct lump* anchor;
- struct hdr_field *hf;
- char *txt;
- int len;
- char *hdr;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("insert hf: %s\n", txt);
- hf = env_L->msg->headers;
- len = strlen(txt);
- hdr = (char*)pkg_malloc(len);
- if(hdr==NULL)
- {
- LM_ERR("no pkg memory left\n");
- return 0;
- }
- memcpy(hdr, txt, len);
- anchor = anchor_lump(env_L->msg,
- hf->name.s + hf->len - env_L->msg->buf, 0, 0);
- if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
- {
- LM_ERR("can't insert lump\n");
- pkg_free(hdr);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_hdr_append_to_reply (lua_State *L)
- {
- char *txt;
- int len;
- sr_lua_env_t *env_L;
- env_L = sr_lua_env_get();
- txt = (char*)lua_tostring(L, -1);
- if(txt==NULL || env_L->msg==NULL)
- return 0;
- LM_DBG("append to reply: %s\n", txt);
- len = strlen(txt);
- if(add_lump_rpl(env_L->msg, txt, len, LUMP_RPL_HDR)==0)
- {
- LM_ERR("unable to add reply lump\n");
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static const luaL_Reg _sr_hdr_Map [] = {
- {"append", lua_sr_hdr_append},
- {"remove", lua_sr_hdr_remove},
- {"insert", lua_sr_hdr_insert},
- {"append_to_reply", lua_sr_hdr_append_to_reply},
- {NULL, NULL}
- };
- /**
- *
- */
- static int lua_sr_pv_get (lua_State *L)
- {
- str pvn;
- pv_spec_t *pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- int pl;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv get: %s\n", pvn.s);
- pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
- LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
- }
- pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
- LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
- {
- LM_ERR("unable to get pv value for [%s]\n", pvn.s);
- return 0;
- }
- if(val.flags&PV_VAL_NULL)
- {
- return 0;
- }
- if(val.flags&PV_TYPE_INT)
- {
- lua_pushinteger(L, val.ri);
- return 1;
- }
- lua_pushlstring(L, val.rs.s, val.rs.len);
- return 1;
- }
- /**
- *
- */
- static int lua_sr_pv_seti (lua_State *L)
- {
- str pvn;
- pv_spec_t *pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- int pl;
- 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;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.ri = lua_tointeger(L, -1);
- val.flags |= PV_TYPE_INT|PV_VAL_INT;
-
- pvn.s = (char*)lua_tostring(L, -2);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv set: %s\n", pvn.s);
- pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
- LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
- }
- pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
- LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
- }
- if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
- {
- LM_ERR("unable to set pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_sets (lua_State *L)
- {
- str pvn;
- pv_spec_t *pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- int pl;
- 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_isstring(L, -1))
- {
- LM_ERR("Cannot convert to a string when assigning value to variable: %s\n",
- lua_tostring(L, -2));
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.rs.s = (char*)lua_tostring(L, -1);
- val.rs.len = strlen(val.rs.s);
- val.flags |= PV_VAL_STR;
-
- pvn.s = (char*)lua_tostring(L, -2);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv set: %s\n", pvn.s);
- pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
- LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
- }
- pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
- LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
- }
- if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
- {
- LM_ERR("unable to set pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_unset (lua_State *L)
- {
- str pvn;
- pv_spec_t *pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- int pl;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv unset: %s\n", pvn.s);
- pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
- LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
- }
- pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
- LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- val.flags |= PV_VAL_NULL;
- if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
- {
- LM_ERR("unable to unset pv [%s]\n", pvn.s);
- return 0;
- }
- return 0;
- }
- /**
- *
- */
- static int lua_sr_pv_is_null (lua_State *L)
- {
- str pvn;
- pv_spec_t *pvs;
- pv_value_t val;
- sr_lua_env_t *env_L;
- int pl;
- env_L = sr_lua_env_get();
- pvn.s = (char*)lua_tostring(L, -1);
- if(pvn.s==NULL || env_L->msg==NULL)
- return 0;
- pvn.len = strlen(pvn.s);
- LM_DBG("pv is null test: %s\n", pvn.s);
- pl = pv_locate_name(&pvn);
- if(pl != pvn.len)
- {
- LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
- return 0;
- }
- pvs = pv_cache_get(&pvn);
- if(pvs==NULL)
- {
- LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
- return 0;
- }
- memset(&val, 0, sizeof(pv_value_t));
- if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
- {
- LM_NOTICE("unable to get pv value for [%s]\n", pvn.s);
- lua_pushboolean(L, 1);
- return 1;
- }
- if(val.flags&PV_VAL_NULL)
- {
- lua_pushboolean(L, 1);
- } else {
- lua_pushboolean(L, 0);
- }
- return 1;
- }
- /**
- *
- */
- static const luaL_Reg _sr_pv_Map [] = {
- {"get", lua_sr_pv_get},
- {"seti", lua_sr_pv_seti},
- {"sets", lua_sr_pv_sets},
- {"unset", lua_sr_pv_unset},
- {"is_null", lua_sr_pv_is_null},
- {NULL, NULL}
- };
- /**
- * creates and push a table to the lua stack with
- * the elements of the list
- */
- static int lua_sr_push_str_list_table(lua_State *L, struct str_list *list) {
- lua_Number i = 1;
- struct str_list *k = list;
- lua_newtable(L);
- while(k!=NULL){
- lua_pushnumber(L, i);
- lua_pushlstring(L, k->s.s, k->s.len);
- lua_settable(L, -3);
- i++;
- k = k->next;
- }
- return 1;
- }
- static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag);
- /**
- * creates and push a table for the key name in xavp
- * if simple_flag is != 0 it will return only the first value
- */
- static void lua_sr_push_xavp_name_table(lua_State *L, sr_xavp_t *xavp,
- str name, const int simple_flag)
- {
- lua_Number i = 1;
- lua_Number elem = 1;
- sr_xavp_t *avp = xavp;
- while(avp!=NULL&&!STR_EQ(avp->name,name))
- {
- avp = avp->next;
- }
- if(simple_flag==0) lua_newtable(L);
- while(avp!=NULL){
- if(simple_flag==0) lua_pushnumber(L, elem);
- switch(avp->val.type) {
- case SR_XTYPE_NULL:
- lua_pushnil(L);
- break;
- case SR_XTYPE_INT:
- i = avp->val.v.i;
- lua_pushnumber(L, i);
- break;
- case SR_XTYPE_STR:
- lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
- break;
- case SR_XTYPE_TIME:
- case SR_XTYPE_LONG:
- case SR_XTYPE_LLONG:
- case SR_XTYPE_DATA:
- lua_pushnil(L);
- LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
- break;
- case SR_XTYPE_XAVP:
- if(!lua_sr_push_xavp_table(L,avp->val.v.xavp, simple_flag)){
- LM_ERR("xavp:%.*s subtable error. Nil value added\n",
- avp->name.len, avp->name.s);
- lua_pushnil(L);
- }
- break;
- default:
- LM_ERR("xavp:%.*s unknown type: %d. Nil value added\n",
- avp->name.len, avp->name.s, avp->val.type);
- lua_pushnil(L);
- break;
- }
- if(simple_flag==0)
- {
- lua_rawset(L, -3);
- elem = elem + 1;
- avp = xavp_get_next(avp);
- }
- else {
- lua_setfield(L, -2, name.s);
- avp = NULL;
- }
- }
- if(simple_flag==0) lua_setfield(L, -2, name.s);
- }
- /**
- * creates and push a table to the lua stack with
- * the elements of the xavp
- */
- static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag) {
- sr_xavp_t *avp = NULL;
- struct str_list *keys;
- struct str_list *k;
- if(xavp->val.type!=SR_XTYPE_XAVP){
- LM_ERR("%s not xavp?\n", xavp->name.s);
- return 0;
- }
- avp = xavp->val.v.xavp;
- keys = xavp_get_list_key_names(xavp);
- lua_newtable(L);
- if(keys!=NULL)
- {
- do
- {
- lua_sr_push_xavp_name_table(L, avp, keys->s, simple_flag);
- k = keys;
- keys = keys->next;
- pkg_free(k);
- }while(keys!=NULL);
- }
- 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 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;
- }
- /**
- * 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;
- }
- /**
- *
- */
- static const luaL_Reg _sr_xavp_Map [] = {
- {"get", lua_sr_xavp_get},
- {"get_keys", lua_sr_xavp_get_keys},
- {NULL, NULL}
- };
- /**
- *
- */
- void lua_sr_core_openlibs(lua_State *L)
- {
- luaL_openlib(L, "sr", _sr_core_Map, 0);
- luaL_openlib(L, "sr.hdr", _sr_hdr_Map, 0);
- luaL_openlib(L, "sr.pv", _sr_pv_Map, 0);
- luaL_openlib(L, "sr.xavp", _sr_xavp_Map, 0);
- }
|