2
0

app_lua_sr.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <sys/stat.h>
  27. #include "../../sr_module.h"
  28. #include "../../dprint.h"
  29. #include "../../route_struct.h"
  30. #include "../../action.h"
  31. #include "../../ut.h"
  32. #include "../../mem/mem.h"
  33. #include "../../data_lump.h"
  34. #include "../../data_lump_rpl.h"
  35. #include "../../forward.h"
  36. #include "../../flags.h"
  37. #include "../../dset.h"
  38. #include "../../parser/parse_uri.h"
  39. #include "../../lib/kcore/cmpapi.h"
  40. #include "../../xavp.h"
  41. #include "app_lua_api.h"
  42. #include "app_lua_sr.h"
  43. /**
  44. *
  45. */
  46. static int lua_sr_probe (lua_State *L)
  47. {
  48. LM_DBG("someone probing from lua\n");
  49. return 0;
  50. }
  51. /**
  52. *
  53. */
  54. static int lua_sr_dbg (lua_State *L)
  55. {
  56. char *txt;
  57. txt = (char*)lua_tostring(L, -1);
  58. if(txt!=NULL)
  59. LM_DBG("%s", txt);
  60. return 0;
  61. }
  62. /**
  63. *
  64. */
  65. static int lua_sr_err (lua_State *L)
  66. {
  67. char *txt;
  68. txt = (char*)lua_tostring(L, -1);
  69. if(txt!=NULL)
  70. LM_ERR("%s", txt);
  71. return 0;
  72. }
  73. /**
  74. *
  75. */
  76. static int lua_sr_log (lua_State *L)
  77. {
  78. char *txt;
  79. char *level;
  80. level = (char*)lua_tostring(L, -2);
  81. txt = (char*)lua_tostring(L, -1);
  82. if(txt!=NULL)
  83. {
  84. if(level==NULL)
  85. {
  86. LM_ERR("%s", txt);
  87. } else {
  88. if(strcasecmp(level, "dbg")==0) {
  89. LM_DBG("%s", txt);
  90. } else if(strcasecmp(level, "info")==0) {
  91. LM_INFO("%s", txt);
  92. } else if(strcasecmp(level, "warn")==0) {
  93. LM_WARN("%s", txt);
  94. } else if(strcasecmp(level, "crit")==0) {
  95. LM_CRIT("%s", txt);
  96. } else {
  97. LM_ERR("%s", txt);
  98. }
  99. }
  100. }
  101. return 0;
  102. }
  103. /**
  104. *
  105. */
  106. static int lua_sr_modf (lua_State *L)
  107. {
  108. int ret;
  109. char *luav[MAX_ACTIONS];
  110. char *argv[MAX_ACTIONS];
  111. int argc;
  112. int i;
  113. int mod_type;
  114. struct run_act_ctx ra_ctx;
  115. unsigned modver;
  116. struct action *act;
  117. sr31_cmd_export_t* expf;
  118. sr_lua_env_t *env_L;
  119. ret = 1;
  120. act = NULL;
  121. argc = 0;
  122. memset(luav, 0, MAX_ACTIONS*sizeof(char*));
  123. memset(argv, 0, MAX_ACTIONS*sizeof(char*));
  124. env_L = sr_lua_env_get();
  125. if(env_L->msg==NULL)
  126. goto error;
  127. #if 0
  128. app_lua_dump_stack(L);
  129. #endif
  130. argc = lua_gettop(L);
  131. if(argc==0)
  132. {
  133. LM_ERR("name of module function not provided\n");
  134. goto error;
  135. }
  136. if(argc>=MAX_ACTIONS)
  137. {
  138. LM_ERR("too many parameters\n");
  139. goto error;
  140. }
  141. /* first is function name, then parameters */
  142. for(i=1; i<=argc; i++)
  143. {
  144. if (!lua_isstring(L, i))
  145. {
  146. LM_ERR("invalid parameter type (%d)\n", i);
  147. goto error;
  148. }
  149. luav[i-1] = (char*)lua_tostring(L, i);
  150. }
  151. /* pkg copy only parameters */
  152. for(i=1; i<MAX_ACTIONS; i++)
  153. {
  154. if(luav[i]!=NULL)
  155. {
  156. argv[i] = (char*)pkg_malloc(strlen(luav[i])+1);
  157. if(argv[i]==NULL)
  158. {
  159. LM_ERR("no more pkg\n");
  160. goto error;
  161. }
  162. strcpy(argv[i], luav[i]);
  163. }
  164. }
  165. expf = find_export_record(luav[0], argc-1, 0, &modver);
  166. if (expf==NULL) {
  167. LM_ERR("function '%s' is not available\n", luav[0]);
  168. goto error;
  169. }
  170. /* check fixups */
  171. if (expf->fixup!=NULL && expf->free_fixup==NULL) {
  172. LM_ERR("function '%s' has fixup - cannot be used\n", luav[0]);
  173. goto error;
  174. }
  175. switch(expf->param_no) {
  176. case 0:
  177. mod_type = MODULE0_T;
  178. break;
  179. case 1:
  180. mod_type = MODULE1_T;
  181. break;
  182. case 2:
  183. mod_type = MODULE2_T;
  184. break;
  185. case 3:
  186. mod_type = MODULE3_T;
  187. break;
  188. case 4:
  189. mod_type = MODULE4_T;
  190. break;
  191. case 5:
  192. mod_type = MODULE5_T;
  193. break;
  194. case 6:
  195. mod_type = MODULE6_T;
  196. break;
  197. case VAR_PARAM_NO:
  198. mod_type = MODULEX_T;
  199. break;
  200. default:
  201. LM_ERR("unknown/bad definition for function '%s' (%d params)\n",
  202. luav[0], expf->param_no);
  203. goto error;
  204. }
  205. act = mk_action(mod_type, argc+1 /* number of (type, value) pairs */,
  206. MODEXP_ST, expf, /* function */
  207. NUMBER_ST, argc-1, /* parameter number */
  208. STRING_ST, argv[1], /* param. 1 */
  209. STRING_ST, argv[2], /* param. 2 */
  210. STRING_ST, argv[3], /* param. 3 */
  211. STRING_ST, argv[4], /* param. 4 */
  212. STRING_ST, argv[5], /* param. 5 */
  213. STRING_ST, argv[6] /* param. 6 */
  214. );
  215. if (act==NULL) {
  216. LM_ERR("action structure could not be created for '%s'\n", luav[0]);
  217. goto error;
  218. }
  219. /* handle fixups */
  220. if (expf->fixup) {
  221. if(argc==1)
  222. { /* no parameters */
  223. if(expf->fixup(0, 0)<0)
  224. {
  225. LM_ERR("Error in fixup (0) for '%s'\n", luav[0]);
  226. goto error;
  227. }
  228. } else {
  229. for(i=1; i<argc; i++)
  230. {
  231. if(expf->fixup(&(act->val[i+1].u.data), i)<0)
  232. {
  233. LM_ERR("Error in fixup (%d) for '%s'\n", i, luav[0]);
  234. goto error;
  235. }
  236. act->val[i+1].type = MODFIXUP_ST;
  237. }
  238. }
  239. }
  240. init_run_actions_ctx(&ra_ctx);
  241. ret = do_action(&ra_ctx, act, env_L->msg);
  242. /* free fixups */
  243. if (expf->fixup) {
  244. for(i=1; i<argc; i++)
  245. {
  246. if ((act->val[i+1].type == MODFIXUP_ST) && (act->val[i+1].u.data))
  247. {
  248. expf->free_fixup(&(act->val[i+1].u.data), i);
  249. }
  250. }
  251. }
  252. pkg_free(act);
  253. for(i=0; i<MAX_ACTIONS; i++)
  254. {
  255. if(argv[i]!=NULL) pkg_free(argv[i]);
  256. argv[i] = 0;
  257. }
  258. lua_pushinteger(L, ret);
  259. return 1;
  260. error:
  261. if(act!=NULL)
  262. pkg_free(act);
  263. for(i=0; i<MAX_ACTIONS; i++)
  264. {
  265. if(argv[i]!=NULL) pkg_free(argv[i]);
  266. argv[i] = 0;
  267. }
  268. lua_pushinteger(L, -1);
  269. return 1;
  270. }
  271. /**
  272. *
  273. */
  274. static int lua_sr_is_myself (lua_State *L)
  275. {
  276. str uri;
  277. struct sip_uri puri;
  278. int ret;
  279. uri.s = (char*)lua_tostring(L, -1);
  280. if(uri.s==NULL)
  281. {
  282. LM_ERR("invalid uri parameter\n");
  283. return app_lua_return_false(L);
  284. }
  285. uri.len = strlen(uri.s);
  286. if(uri.len>4 && (strncmp(uri.s, "sip:", 4)==0
  287. || strncmp(uri.s, "sips:", 5)==0))
  288. {
  289. if(parse_uri(uri.s, uri.len, &puri)!=0)
  290. {
  291. LM_ERR("failed to parse uri [%s]\n", uri.s);
  292. return app_lua_return_false(L);
  293. }
  294. ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
  295. (puri.transport_val.s)?puri.proto:0);
  296. } else {
  297. ret = check_self(&uri, 0, 0);
  298. }
  299. if(ret==1)
  300. return app_lua_return_true(L);
  301. return app_lua_return_false(L);
  302. }
  303. /**
  304. *
  305. */
  306. static int lua_sr_setflag (lua_State *L)
  307. {
  308. int flag;
  309. sr_lua_env_t *env_L;
  310. env_L = sr_lua_env_get();
  311. flag = lua_tointeger(L, -1);
  312. if(env_L->msg==NULL)
  313. {
  314. LM_WARN("invalid parameters from Lua env\n");
  315. return app_lua_return_false(L);
  316. }
  317. if (!flag_in_range(flag))
  318. {
  319. LM_ERR("invalid flag parameter %d\n", flag);
  320. return app_lua_return_false(L);
  321. }
  322. setflag(env_L->msg, flag);
  323. return app_lua_return_true(L);
  324. }
  325. /**
  326. *
  327. */
  328. static int lua_sr_resetflag (lua_State *L)
  329. {
  330. int flag;
  331. sr_lua_env_t *env_L;
  332. env_L = sr_lua_env_get();
  333. flag = lua_tointeger(L, -1);
  334. if(env_L->msg==NULL)
  335. {
  336. LM_WARN("invalid parameters from Lua env\n");
  337. return app_lua_return_false(L);
  338. }
  339. if (!flag_in_range(flag))
  340. {
  341. LM_ERR("invalid flag parameter %d\n", flag);
  342. return app_lua_return_false(L);
  343. }
  344. resetflag(env_L->msg, flag);
  345. return app_lua_return_true(L);
  346. }
  347. /**
  348. *
  349. */
  350. static int lua_sr_isflagset (lua_State *L)
  351. {
  352. int flag;
  353. int ret;
  354. sr_lua_env_t *env_L;
  355. env_L = sr_lua_env_get();
  356. flag = lua_tointeger(L, -1);
  357. if(env_L->msg==NULL)
  358. {
  359. LM_WARN("invalid parameters from Lua env\n");
  360. return app_lua_return_false(L);
  361. }
  362. if (!flag_in_range(flag))
  363. {
  364. LM_ERR("invalid flag parameter %d\n", flag);
  365. return app_lua_return_false(L);
  366. }
  367. ret = isflagset(env_L->msg, flag);
  368. if(ret>0)
  369. return app_lua_return_true(L);
  370. return app_lua_return_false(L);
  371. }
  372. /**
  373. *
  374. */
  375. static int lua_sr_setbflag (lua_State *L)
  376. {
  377. int flag;
  378. int branch;
  379. sr_lua_env_t *env_L;
  380. env_L = sr_lua_env_get();
  381. if(lua_gettop(L)==1)
  382. {
  383. flag = lua_tointeger(L, -1);
  384. branch = 0;
  385. } else if(lua_gettop(L)==2) {
  386. flag = lua_tointeger(L, -2);
  387. branch = lua_tointeger(L, -1);
  388. } else {
  389. LM_WARN("invalid number of parameters from Lua\n");
  390. return app_lua_return_false(L);
  391. }
  392. if(env_L->msg==NULL)
  393. {
  394. LM_WARN("invalid parameters from Lua env\n");
  395. return app_lua_return_false(L);
  396. }
  397. if (!flag_in_range(flag))
  398. {
  399. LM_ERR("invalid flag parameter %d\n", flag);
  400. return app_lua_return_false(L);
  401. }
  402. setbflag(branch, flag);
  403. return app_lua_return_true(L);
  404. }
  405. /**
  406. *
  407. */
  408. static int lua_sr_resetbflag (lua_State *L)
  409. {
  410. int flag;
  411. int branch;
  412. sr_lua_env_t *env_L;
  413. env_L = sr_lua_env_get();
  414. if(lua_gettop(L)==1)
  415. {
  416. flag = lua_tointeger(L, -1);
  417. branch = 0;
  418. } else if(lua_gettop(L)==2) {
  419. flag = lua_tointeger(L, -2);
  420. branch = lua_tointeger(L, -1);
  421. } else {
  422. LM_WARN("invalid number of parameters from Lua\n");
  423. return app_lua_return_false(L);
  424. }
  425. if(env_L->msg==NULL)
  426. {
  427. LM_WARN("invalid parameters from Lua env\n");
  428. return app_lua_return_false(L);
  429. }
  430. if (!flag_in_range(flag))
  431. {
  432. LM_ERR("invalid flag parameter %d\n", flag);
  433. return app_lua_return_false(L);
  434. }
  435. resetbflag(branch, flag);
  436. return app_lua_return_true(L);
  437. }
  438. /**
  439. *
  440. */
  441. static int lua_sr_isbflagset (lua_State *L)
  442. {
  443. int flag;
  444. int branch;
  445. int ret;
  446. sr_lua_env_t *env_L;
  447. env_L = sr_lua_env_get();
  448. if(lua_gettop(L)==1)
  449. {
  450. flag = lua_tointeger(L, -1);
  451. branch = 0;
  452. } else if(lua_gettop(L)==2) {
  453. flag = lua_tointeger(L, -2);
  454. branch = lua_tointeger(L, -1);
  455. } else {
  456. LM_WARN("invalid number of parameters from Lua\n");
  457. return app_lua_return_false(L);
  458. }
  459. if(env_L->msg==NULL)
  460. {
  461. LM_WARN("invalid parameters from Lua env\n");
  462. return app_lua_return_false(L);
  463. }
  464. if (!flag_in_range(flag))
  465. {
  466. LM_ERR("invalid flag parameter %d\n", flag);
  467. return app_lua_return_false(L);
  468. }
  469. ret = isbflagset(branch, flag);
  470. if(ret>0)
  471. return app_lua_return_true(L);
  472. return app_lua_return_false(L);
  473. }
  474. /**
  475. *
  476. */
  477. static int lua_sr_seturi (lua_State *L)
  478. {
  479. struct action act;
  480. struct run_act_ctx h;
  481. str uri;
  482. sr_lua_env_t *env_L;
  483. env_L = sr_lua_env_get();
  484. uri.s = (char*)lua_tostring(L, -1);
  485. if(uri.s==NULL)
  486. {
  487. LM_ERR("invalid uri parameter\n");
  488. return app_lua_return_false(L);
  489. }
  490. uri.len = strlen(uri.s);
  491. if(env_L->msg==NULL)
  492. {
  493. LM_WARN("invalid parameters from Lua env\n");
  494. return app_lua_return_false(L);
  495. }
  496. memset(&act, 0, sizeof(act));
  497. act.val[0].type = STRING_ST;
  498. act.val[0].u.string = uri.s;
  499. act.type = SET_URI_T;
  500. init_run_actions_ctx(&h);
  501. if (do_action(&h, &act, env_L->msg)<0)
  502. {
  503. LM_ERR("do action failed\n");
  504. return app_lua_return_false(L);
  505. }
  506. return app_lua_return_true(L);
  507. }
  508. /**
  509. *
  510. */
  511. static int lua_sr_setuser (lua_State *L)
  512. {
  513. struct action act;
  514. struct run_act_ctx h;
  515. str uri;
  516. sr_lua_env_t *env_L;
  517. env_L = sr_lua_env_get();
  518. uri.s = (char*)lua_tostring(L, -1);
  519. if(uri.s==NULL)
  520. {
  521. LM_ERR("invalid uri parameter\n");
  522. return app_lua_return_false(L);
  523. }
  524. uri.len = strlen(uri.s);
  525. if(env_L->msg==NULL)
  526. {
  527. LM_WARN("invalid parameters from Lua env\n");
  528. return app_lua_return_false(L);
  529. }
  530. memset(&act, 0, sizeof(act));
  531. act.val[0].type = STRING_ST;
  532. act.val[0].u.string = uri.s;
  533. act.type = SET_USER_T;
  534. init_run_actions_ctx(&h);
  535. if (do_action(&h, &act, env_L->msg)<0)
  536. {
  537. LM_ERR("do action failed\n");
  538. return app_lua_return_false(L);
  539. }
  540. return app_lua_return_true(L);
  541. }
  542. /**
  543. *
  544. */
  545. static int lua_sr_sethost (lua_State *L)
  546. {
  547. struct action act;
  548. struct run_act_ctx h;
  549. str uri;
  550. sr_lua_env_t *env_L;
  551. env_L = sr_lua_env_get();
  552. uri.s = (char*)lua_tostring(L, -1);
  553. if(uri.s==NULL)
  554. {
  555. LM_ERR("invalid uri parameter\n");
  556. return app_lua_return_false(L);
  557. }
  558. uri.len = strlen(uri.s);
  559. if(env_L->msg==NULL)
  560. {
  561. LM_WARN("invalid parameters from Lua env\n");
  562. return app_lua_return_false(L);
  563. }
  564. memset(&act, 0, sizeof(act));
  565. act.val[0].type = STRING_ST;
  566. act.val[0].u.string = uri.s;
  567. act.type = SET_HOST_T;
  568. init_run_actions_ctx(&h);
  569. if (do_action(&h, &act, env_L->msg)<0)
  570. {
  571. LM_ERR("do action failed\n");
  572. return app_lua_return_false(L);
  573. }
  574. return app_lua_return_true(L);
  575. }
  576. /**
  577. *
  578. */
  579. static int lua_sr_setdsturi (lua_State *L)
  580. {
  581. str uri;
  582. sr_lua_env_t *env_L;
  583. env_L = sr_lua_env_get();
  584. uri.s = (char*)lua_tostring(L, -1);
  585. if(uri.s==NULL)
  586. {
  587. LM_ERR("invalid uri parameter\n");
  588. return app_lua_return_false(L);
  589. }
  590. uri.len = strlen(uri.s);
  591. if(env_L->msg==NULL)
  592. {
  593. LM_WARN("invalid parameters from Lua env\n");
  594. return app_lua_return_false(L);
  595. }
  596. if (set_dst_uri(env_L->msg, &uri)<0)
  597. {
  598. LM_ERR("setting dst uri failed\n");
  599. return app_lua_return_false(L);
  600. }
  601. return app_lua_return_true(L);
  602. }
  603. /**
  604. *
  605. */
  606. static int lua_sr_resetdsturi (lua_State *L)
  607. {
  608. sr_lua_env_t *env_L;
  609. env_L = sr_lua_env_get();
  610. if(env_L->msg==NULL)
  611. {
  612. LM_WARN("invalid parameters from Lua env\n");
  613. return app_lua_return_false(L);
  614. }
  615. reset_dst_uri(env_L->msg);
  616. return app_lua_return_true(L);
  617. }
  618. /**
  619. *
  620. */
  621. static const luaL_Reg _sr_core_Map [] = {
  622. {"probe", lua_sr_probe},
  623. {"dbg", lua_sr_dbg},
  624. {"err", lua_sr_err},
  625. {"log", lua_sr_log},
  626. {"modf", lua_sr_modf},
  627. {"is_myself", lua_sr_is_myself},
  628. {"setflag", lua_sr_setflag},
  629. {"resetflag", lua_sr_resetflag},
  630. {"isflagset", lua_sr_isflagset},
  631. {"setbflag", lua_sr_setbflag},
  632. {"resetbflag", lua_sr_resetbflag},
  633. {"isbflagset", lua_sr_isbflagset},
  634. {"seturi", lua_sr_seturi},
  635. {"setuser", lua_sr_setuser},
  636. {"sethost", lua_sr_sethost},
  637. {"setdsturi", lua_sr_setdsturi},
  638. {"resetdsturi", lua_sr_resetdsturi},
  639. {NULL, NULL}
  640. };
  641. /**
  642. *
  643. */
  644. static int lua_sr_hdr_append (lua_State *L)
  645. {
  646. struct lump* anchor;
  647. struct hdr_field *hf;
  648. char *txt;
  649. int len;
  650. char *hdr;
  651. sr_lua_env_t *env_L;
  652. env_L = sr_lua_env_get();
  653. txt = (char*)lua_tostring(L, -1);
  654. if(txt==NULL || env_L->msg==NULL)
  655. return 0;
  656. LM_DBG("append hf: %s\n", txt);
  657. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1)
  658. {
  659. LM_ERR("error while parsing message\n");
  660. return 0;
  661. }
  662. hf = env_L->msg->last_header;
  663. len = strlen(txt);
  664. hdr = (char*)pkg_malloc(len);
  665. if(hdr==NULL)
  666. {
  667. LM_ERR("no pkg memory left\n");
  668. return 0;
  669. }
  670. memcpy(hdr, txt, len);
  671. anchor = anchor_lump(env_L->msg,
  672. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  673. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  674. {
  675. LM_ERR("can't insert lump\n");
  676. pkg_free(hdr);
  677. return 0;
  678. }
  679. return 0;
  680. }
  681. /**
  682. *
  683. */
  684. static int lua_sr_hdr_remove (lua_State *L)
  685. {
  686. struct lump* anchor;
  687. struct hdr_field *hf;
  688. char *txt;
  689. str hname;
  690. sr_lua_env_t *env_L;
  691. env_L = sr_lua_env_get();
  692. txt = (char*)lua_tostring(L, -1);
  693. if(txt==NULL || env_L->msg==NULL)
  694. return 0;
  695. LM_DBG("remove hf: %s\n", txt);
  696. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1) {
  697. LM_ERR("error while parsing message\n");
  698. return 0;
  699. }
  700. hname.s = txt;
  701. hname.len = strlen(txt);
  702. for (hf=env_L->msg->headers; hf; hf=hf->next)
  703. {
  704. if (cmp_hdrname_str(&hf->name, &hname)==0)
  705. {
  706. anchor=del_lump(env_L->msg,
  707. hf->name.s - env_L->msg->buf, hf->len, 0);
  708. if (anchor==0)
  709. {
  710. LM_ERR("cannot remove hdr %s\n", txt);
  711. return 0;
  712. }
  713. }
  714. }
  715. return 0;
  716. }
  717. /**
  718. *
  719. */
  720. static int lua_sr_hdr_insert (lua_State *L)
  721. {
  722. struct lump* anchor;
  723. struct hdr_field *hf;
  724. char *txt;
  725. int len;
  726. char *hdr;
  727. sr_lua_env_t *env_L;
  728. env_L = sr_lua_env_get();
  729. txt = (char*)lua_tostring(L, -1);
  730. if(txt==NULL || env_L->msg==NULL)
  731. return 0;
  732. LM_DBG("insert hf: %s\n", txt);
  733. hf = env_L->msg->headers;
  734. len = strlen(txt);
  735. hdr = (char*)pkg_malloc(len);
  736. if(hdr==NULL)
  737. {
  738. LM_ERR("no pkg memory left\n");
  739. return 0;
  740. }
  741. memcpy(hdr, txt, len);
  742. anchor = anchor_lump(env_L->msg,
  743. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  744. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  745. {
  746. LM_ERR("can't insert lump\n");
  747. pkg_free(hdr);
  748. return 0;
  749. }
  750. return 0;
  751. }
  752. /**
  753. *
  754. */
  755. static int lua_sr_hdr_append_to_reply (lua_State *L)
  756. {
  757. char *txt;
  758. int len;
  759. sr_lua_env_t *env_L;
  760. env_L = sr_lua_env_get();
  761. txt = (char*)lua_tostring(L, -1);
  762. if(txt==NULL || env_L->msg==NULL)
  763. return 0;
  764. LM_DBG("append to reply: %s\n", txt);
  765. len = strlen(txt);
  766. if(add_lump_rpl(env_L->msg, txt, len, LUMP_RPL_HDR)==0)
  767. {
  768. LM_ERR("unable to add reply lump\n");
  769. return 0;
  770. }
  771. return 0;
  772. }
  773. /**
  774. *
  775. */
  776. static const luaL_Reg _sr_hdr_Map [] = {
  777. {"append", lua_sr_hdr_append},
  778. {"remove", lua_sr_hdr_remove},
  779. {"insert", lua_sr_hdr_insert},
  780. {"append_to_reply", lua_sr_hdr_append_to_reply},
  781. {NULL, NULL}
  782. };
  783. /**
  784. *
  785. */
  786. static int lua_sr_pv_get (lua_State *L)
  787. {
  788. str pvn;
  789. pv_spec_t *pvs;
  790. pv_value_t val;
  791. sr_lua_env_t *env_L;
  792. int pl;
  793. env_L = sr_lua_env_get();
  794. pvn.s = (char*)lua_tostring(L, -1);
  795. if(pvn.s==NULL || env_L->msg==NULL)
  796. return 0;
  797. pvn.len = strlen(pvn.s);
  798. LM_DBG("pv get: %s\n", pvn.s);
  799. pl = pv_locate_name(&pvn);
  800. if(pl != pvn.len)
  801. {
  802. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  803. return 0;
  804. }
  805. pvs = pv_cache_get(&pvn);
  806. if(pvs==NULL)
  807. {
  808. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  809. return 0;
  810. }
  811. memset(&val, 0, sizeof(pv_value_t));
  812. if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
  813. {
  814. LM_ERR("unable to get pv value for [%s]\n", pvn.s);
  815. return 0;
  816. }
  817. if(val.flags&PV_VAL_NULL)
  818. {
  819. return 0;
  820. }
  821. if(val.flags&PV_TYPE_INT)
  822. {
  823. lua_pushinteger(L, val.ri);
  824. return 1;
  825. }
  826. lua_pushlstring(L, val.rs.s, val.rs.len);
  827. return 1;
  828. }
  829. /**
  830. *
  831. */
  832. static int lua_sr_pv_seti (lua_State *L)
  833. {
  834. str pvn;
  835. pv_spec_t *pvs;
  836. pv_value_t val;
  837. sr_lua_env_t *env_L;
  838. int pl;
  839. env_L = sr_lua_env_get();
  840. if(lua_gettop(L)<2)
  841. {
  842. LM_ERR("to few parameters [%d]\n", lua_gettop(L));
  843. return 0;
  844. }
  845. if(!lua_isnumber(L, -1))
  846. {
  847. LM_ERR("invalid int parameter\n");
  848. return 0;
  849. }
  850. memset(&val, 0, sizeof(pv_value_t));
  851. val.ri = lua_tointeger(L, -1);
  852. val.flags |= PV_TYPE_INT|PV_VAL_INT;
  853. pvn.s = (char*)lua_tostring(L, -2);
  854. if(pvn.s==NULL || env_L->msg==NULL)
  855. return 0;
  856. pvn.len = strlen(pvn.s);
  857. LM_DBG("pv set: %s\n", pvn.s);
  858. pl = pv_locate_name(&pvn);
  859. if(pl != pvn.len)
  860. {
  861. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  862. return 0;
  863. }
  864. pvs = pv_cache_get(&pvn);
  865. if(pvs==NULL)
  866. {
  867. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  868. return 0;
  869. }
  870. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  871. {
  872. LM_ERR("unable to set pv [%s]\n", pvn.s);
  873. return 0;
  874. }
  875. return 0;
  876. }
  877. /**
  878. *
  879. */
  880. static int lua_sr_pv_sets (lua_State *L)
  881. {
  882. str pvn;
  883. pv_spec_t *pvs;
  884. pv_value_t val;
  885. sr_lua_env_t *env_L;
  886. int pl;
  887. env_L = sr_lua_env_get();
  888. if(lua_gettop(L)<2)
  889. {
  890. LM_ERR("to few parameters [%d]\n",lua_gettop(L));
  891. return 0;
  892. }
  893. if(!lua_isstring(L, -1))
  894. {
  895. LM_ERR("Cannot convert to a string when assigning value to variable: %s\n",
  896. lua_tostring(L, -2));
  897. return 0;
  898. }
  899. memset(&val, 0, sizeof(pv_value_t));
  900. val.rs.s = (char*)lua_tostring(L, -1);
  901. val.rs.len = strlen(val.rs.s);
  902. val.flags |= PV_VAL_STR;
  903. pvn.s = (char*)lua_tostring(L, -2);
  904. if(pvn.s==NULL || env_L->msg==NULL)
  905. return 0;
  906. pvn.len = strlen(pvn.s);
  907. LM_DBG("pv set: %s\n", pvn.s);
  908. pl = pv_locate_name(&pvn);
  909. if(pl != pvn.len)
  910. {
  911. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  912. return 0;
  913. }
  914. pvs = pv_cache_get(&pvn);
  915. if(pvs==NULL)
  916. {
  917. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  918. return 0;
  919. }
  920. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  921. {
  922. LM_ERR("unable to set pv [%s]\n", pvn.s);
  923. return 0;
  924. }
  925. return 0;
  926. }
  927. /**
  928. *
  929. */
  930. static int lua_sr_pv_unset (lua_State *L)
  931. {
  932. str pvn;
  933. pv_spec_t *pvs;
  934. pv_value_t val;
  935. sr_lua_env_t *env_L;
  936. int pl;
  937. env_L = sr_lua_env_get();
  938. pvn.s = (char*)lua_tostring(L, -1);
  939. if(pvn.s==NULL || env_L->msg==NULL)
  940. return 0;
  941. pvn.len = strlen(pvn.s);
  942. LM_DBG("pv unset: %s\n", pvn.s);
  943. pl = pv_locate_name(&pvn);
  944. if(pl != pvn.len)
  945. {
  946. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  947. return 0;
  948. }
  949. pvs = pv_cache_get(&pvn);
  950. if(pvs==NULL)
  951. {
  952. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  953. return 0;
  954. }
  955. memset(&val, 0, sizeof(pv_value_t));
  956. val.flags |= PV_VAL_NULL;
  957. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  958. {
  959. LM_ERR("unable to unset pv [%s]\n", pvn.s);
  960. return 0;
  961. }
  962. return 0;
  963. }
  964. /**
  965. *
  966. */
  967. static int lua_sr_pv_is_null (lua_State *L)
  968. {
  969. str pvn;
  970. pv_spec_t *pvs;
  971. pv_value_t val;
  972. sr_lua_env_t *env_L;
  973. int pl;
  974. env_L = sr_lua_env_get();
  975. pvn.s = (char*)lua_tostring(L, -1);
  976. if(pvn.s==NULL || env_L->msg==NULL)
  977. return 0;
  978. pvn.len = strlen(pvn.s);
  979. LM_DBG("pv is null test: %s\n", pvn.s);
  980. pl = pv_locate_name(&pvn);
  981. if(pl != pvn.len)
  982. {
  983. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  984. return 0;
  985. }
  986. pvs = pv_cache_get(&pvn);
  987. if(pvs==NULL)
  988. {
  989. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  990. return 0;
  991. }
  992. memset(&val, 0, sizeof(pv_value_t));
  993. if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
  994. {
  995. LM_NOTICE("unable to get pv value for [%s]\n", pvn.s);
  996. lua_pushboolean(L, 1);
  997. return 1;
  998. }
  999. if(val.flags&PV_VAL_NULL)
  1000. {
  1001. lua_pushboolean(L, 1);
  1002. } else {
  1003. lua_pushboolean(L, 0);
  1004. }
  1005. return 1;
  1006. }
  1007. /**
  1008. *
  1009. */
  1010. static const luaL_Reg _sr_pv_Map [] = {
  1011. {"get", lua_sr_pv_get},
  1012. {"seti", lua_sr_pv_seti},
  1013. {"sets", lua_sr_pv_sets},
  1014. {"unset", lua_sr_pv_unset},
  1015. {"is_null", lua_sr_pv_is_null},
  1016. {NULL, NULL}
  1017. };
  1018. /**
  1019. * creates and push a table to the lua stack with
  1020. * the elements of the list
  1021. */
  1022. static int lua_sr_push_str_list_table(lua_State *L, struct str_list *list) {
  1023. lua_Number i = 1;
  1024. struct str_list *k = list;
  1025. lua_newtable(L);
  1026. while(k!=NULL){
  1027. lua_pushnumber(L, i);
  1028. lua_pushlstring(L, k->s.s, k->s.len);
  1029. lua_settable(L, -3);
  1030. i++;
  1031. k = k->next;
  1032. }
  1033. return 1;
  1034. }
  1035. static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag);
  1036. /**
  1037. * creates and push a table for the key name in xavp
  1038. * if simple_flag is != 0 it will return only the first value
  1039. */
  1040. static void lua_sr_push_xavp_name_table(lua_State *L, sr_xavp_t *xavp,
  1041. str name, const int simple_flag)
  1042. {
  1043. lua_Number i = 1;
  1044. lua_Number elem = 1;
  1045. sr_xavp_t *avp = xavp;
  1046. while(avp!=NULL&&!STR_EQ(avp->name,name))
  1047. {
  1048. avp = avp->next;
  1049. }
  1050. if(simple_flag==0) lua_newtable(L);
  1051. while(avp!=NULL){
  1052. if(simple_flag==0) lua_pushnumber(L, elem);
  1053. switch(avp->val.type) {
  1054. case SR_XTYPE_NULL:
  1055. lua_pushnil(L);
  1056. break;
  1057. case SR_XTYPE_INT:
  1058. i = avp->val.v.i;
  1059. lua_pushnumber(L, i);
  1060. break;
  1061. case SR_XTYPE_STR:
  1062. lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
  1063. break;
  1064. case SR_XTYPE_TIME:
  1065. case SR_XTYPE_LONG:
  1066. case SR_XTYPE_LLONG:
  1067. case SR_XTYPE_DATA:
  1068. lua_pushnil(L);
  1069. LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
  1070. break;
  1071. case SR_XTYPE_XAVP:
  1072. if(!lua_sr_push_xavp_table(L,avp->val.v.xavp, simple_flag)){
  1073. LM_ERR("xavp:%.*s subtable error. Nil value added\n",
  1074. avp->name.len, avp->name.s);
  1075. lua_pushnil(L);
  1076. }
  1077. break;
  1078. default:
  1079. LM_ERR("xavp:%.*s unknown type: %d. Nil value added\n",
  1080. avp->name.len, avp->name.s, avp->val.type);
  1081. lua_pushnil(L);
  1082. break;
  1083. }
  1084. if(simple_flag==0)
  1085. {
  1086. lua_rawset(L, -3);
  1087. elem = elem + 1;
  1088. avp = xavp_get_next(avp);
  1089. }
  1090. else {
  1091. lua_setfield(L, -2, name.s);
  1092. avp = NULL;
  1093. }
  1094. }
  1095. if(simple_flag==0) lua_setfield(L, -2, name.s);
  1096. }
  1097. /**
  1098. * creates and push a table to the lua stack with
  1099. * the elements of the xavp
  1100. */
  1101. static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag) {
  1102. sr_xavp_t *avp = NULL;
  1103. struct str_list *keys;
  1104. struct str_list *k;
  1105. if(xavp->val.type!=SR_XTYPE_XAVP){
  1106. LM_ERR("%s not xavp?\n", xavp->name.s);
  1107. return 0;
  1108. }
  1109. avp = xavp->val.v.xavp;
  1110. keys = xavp_get_list_key_names(xavp);
  1111. lua_newtable(L);
  1112. if(keys!=NULL)
  1113. {
  1114. do
  1115. {
  1116. lua_sr_push_xavp_name_table(L, avp, keys->s, simple_flag);
  1117. k = keys;
  1118. keys = keys->next;
  1119. pkg_free(k);
  1120. }while(keys!=NULL);
  1121. }
  1122. return 1;
  1123. }
  1124. /**
  1125. * puts a table with content of a xavp
  1126. */
  1127. static int lua_sr_xavp_get(lua_State *L)
  1128. {
  1129. str xavp_name;
  1130. int indx = 0;
  1131. sr_lua_env_t *env_L;
  1132. sr_xavp_t *avp;
  1133. int num_param = 0;
  1134. int param = -1;
  1135. int all_flag = 0;
  1136. int simple_flag = 0;
  1137. lua_Number elem = 1;
  1138. int xavp_size = 0;
  1139. env_L = sr_lua_env_get();
  1140. num_param = lua_gettop(L);
  1141. if(num_param<2 && num_param>3)
  1142. {
  1143. LM_ERR("wrong number of parameters [%d]\n", num_param);
  1144. return 0;
  1145. }
  1146. if(num_param==3)
  1147. {
  1148. if(!lua_isnumber(L, param))
  1149. {
  1150. LM_ERR("invalid int parameter\n");
  1151. return 0;
  1152. }
  1153. simple_flag = lua_tointeger(L, param);
  1154. param = param - 1;
  1155. }
  1156. if(!lua_isnumber(L, param))
  1157. {
  1158. if(lua_isnil(L, param))
  1159. {
  1160. all_flag = 1;
  1161. }
  1162. else
  1163. {
  1164. LM_ERR("invalid parameter, must be int or nil\n");
  1165. return 0;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. indx = lua_tointeger(L, param);
  1171. }
  1172. param = param - 1;
  1173. xavp_name.s = (char*)lua_tostring(L, param);
  1174. if(xavp_name.s==NULL || env_L->msg==NULL)
  1175. {
  1176. LM_ERR("No xavp name in %d param\n", param);
  1177. return 0;
  1178. }
  1179. xavp_name.len = strlen(xavp_name.s);
  1180. if(all_flag>0) {
  1181. indx = 0;
  1182. lua_newtable(L);
  1183. }
  1184. xavp_size = xavp_count(&xavp_name, NULL);
  1185. if(indx<0)
  1186. {
  1187. if((indx*-1)>xavp_size)
  1188. {
  1189. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1190. lua_pushnil(L);
  1191. return 1;
  1192. }
  1193. indx = xavp_size + indx;
  1194. }
  1195. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1196. do
  1197. {
  1198. if(avp==NULL){
  1199. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1200. lua_pushnil(L);
  1201. return 1;
  1202. }
  1203. if(all_flag!=0) {
  1204. lua_pushnumber(L, elem);
  1205. elem = elem + 1;
  1206. }
  1207. lua_sr_push_xavp_table(L, avp, simple_flag);
  1208. if(all_flag!=0) {
  1209. lua_rawset(L, -3);
  1210. indx = indx + 1;
  1211. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1212. }
  1213. else return 1;
  1214. }while(avp!=NULL);
  1215. return 1;
  1216. }
  1217. /**
  1218. * puts a table with the list of keys of the xavp
  1219. */
  1220. static int lua_sr_xavp_get_keys (lua_State *L)
  1221. {
  1222. str xavp_name;
  1223. int indx = 0;
  1224. sr_lua_env_t *env_L;
  1225. sr_xavp_t *avp;
  1226. struct str_list *keys, *k;
  1227. env_L = sr_lua_env_get();
  1228. if(lua_gettop(L)<2)
  1229. {
  1230. LM_ERR("to few parameters [%d]\n",lua_gettop(L));
  1231. return 0;
  1232. }
  1233. if(!lua_isnumber(L, -1))
  1234. {
  1235. LM_ERR("invalid int parameter\n");
  1236. return 0;
  1237. }
  1238. indx = lua_tointeger(L, -1);
  1239. xavp_name.s = (char*)lua_tostring(L, -2);
  1240. if(xavp_name.s==NULL || env_L->msg==NULL)
  1241. return 0;
  1242. xavp_name.len = strlen(xavp_name.s);
  1243. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1244. if(avp==NULL){
  1245. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1246. lua_pushnil(L);
  1247. return 1;
  1248. }
  1249. keys = xavp_get_list_key_names(avp);
  1250. lua_sr_push_str_list_table(L, keys);
  1251. // free list
  1252. while(keys!=NULL){
  1253. k = keys;
  1254. keys = k->next;
  1255. pkg_free(k);
  1256. }
  1257. return 1;
  1258. }
  1259. /**
  1260. *
  1261. */
  1262. static const luaL_Reg _sr_xavp_Map [] = {
  1263. {"get", lua_sr_xavp_get},
  1264. {"get_keys", lua_sr_xavp_get_keys},
  1265. {NULL, NULL}
  1266. };
  1267. /**
  1268. *
  1269. */
  1270. void lua_sr_core_openlibs(lua_State *L)
  1271. {
  1272. luaL_openlib(L, "sr", _sr_core_Map, 0);
  1273. luaL_openlib(L, "sr.hdr", _sr_hdr_Map, 0);
  1274. luaL_openlib(L, "sr.pv", _sr_pv_Map, 0);
  1275. luaL_openlib(L, "sr.xavp", _sr_xavp_Map, 0);
  1276. }