app_lua_sr.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  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. lua_pushinteger(L, ret);
  254. return 1;
  255. error:
  256. if(act!=NULL)
  257. pkg_free(act);
  258. for(i=0; i<MAX_ACTIONS; i++)
  259. {
  260. if(argv[i]!=NULL) pkg_free(argv[i]);
  261. argv[i] = 0;
  262. }
  263. lua_pushinteger(L, -1);
  264. return 1;
  265. }
  266. /**
  267. *
  268. */
  269. static int lua_sr_is_myself (lua_State *L)
  270. {
  271. str uri;
  272. struct sip_uri puri;
  273. int ret;
  274. uri.s = (char*)lua_tostring(L, -1);
  275. if(uri.s==NULL)
  276. {
  277. LM_ERR("invalid uri parameter\n");
  278. return app_lua_return_false(L);
  279. }
  280. uri.len = strlen(uri.s);
  281. if(uri.len>4 && (strncmp(uri.s, "sip:", 4)==0
  282. || strncmp(uri.s, "sips:", 5)==0))
  283. {
  284. if(parse_uri(uri.s, uri.len, &puri)!=0)
  285. {
  286. LM_ERR("failed to parse uri [%s]\n", uri.s);
  287. return app_lua_return_false(L);
  288. }
  289. ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
  290. (puri.transport_val.s)?puri.proto:0);
  291. } else {
  292. ret = check_self(&uri, 0, 0);
  293. }
  294. if(ret==1)
  295. return app_lua_return_true(L);
  296. return app_lua_return_false(L);
  297. }
  298. /**
  299. *
  300. */
  301. static int lua_sr_setflag (lua_State *L)
  302. {
  303. int flag;
  304. sr_lua_env_t *env_L;
  305. env_L = sr_lua_env_get();
  306. flag = lua_tointeger(L, -1);
  307. if(env_L->msg==NULL)
  308. {
  309. LM_WARN("invalid parameters from Lua env\n");
  310. return app_lua_return_false(L);
  311. }
  312. if (!flag_in_range(flag))
  313. {
  314. LM_ERR("invalid flag parameter %d\n", flag);
  315. return app_lua_return_false(L);
  316. }
  317. setflag(env_L->msg, flag);
  318. return app_lua_return_true(L);
  319. }
  320. /**
  321. *
  322. */
  323. static int lua_sr_resetflag (lua_State *L)
  324. {
  325. int flag;
  326. sr_lua_env_t *env_L;
  327. env_L = sr_lua_env_get();
  328. flag = lua_tointeger(L, -1);
  329. if(env_L->msg==NULL)
  330. {
  331. LM_WARN("invalid parameters from Lua env\n");
  332. return app_lua_return_false(L);
  333. }
  334. if (!flag_in_range(flag))
  335. {
  336. LM_ERR("invalid flag parameter %d\n", flag);
  337. return app_lua_return_false(L);
  338. }
  339. resetflag(env_L->msg, flag);
  340. return app_lua_return_true(L);
  341. }
  342. /**
  343. *
  344. */
  345. static int lua_sr_isflagset (lua_State *L)
  346. {
  347. int flag;
  348. int ret;
  349. sr_lua_env_t *env_L;
  350. env_L = sr_lua_env_get();
  351. flag = lua_tointeger(L, -1);
  352. if(env_L->msg==NULL)
  353. {
  354. LM_WARN("invalid parameters from Lua env\n");
  355. return app_lua_return_false(L);
  356. }
  357. if (!flag_in_range(flag))
  358. {
  359. LM_ERR("invalid flag parameter %d\n", flag);
  360. return app_lua_return_false(L);
  361. }
  362. ret = isflagset(env_L->msg, flag);
  363. if(ret>0)
  364. return app_lua_return_true(L);
  365. return app_lua_return_false(L);
  366. }
  367. /**
  368. *
  369. */
  370. static int lua_sr_setbflag (lua_State *L)
  371. {
  372. int flag;
  373. int branch;
  374. sr_lua_env_t *env_L;
  375. env_L = sr_lua_env_get();
  376. if(lua_gettop(L)==1)
  377. {
  378. flag = lua_tointeger(L, -1);
  379. branch = 0;
  380. } else if(lua_gettop(L)==2) {
  381. flag = lua_tointeger(L, -2);
  382. branch = lua_tointeger(L, -1);
  383. } else {
  384. LM_WARN("invalid number of parameters from Lua\n");
  385. return app_lua_return_false(L);
  386. }
  387. if(env_L->msg==NULL)
  388. {
  389. LM_WARN("invalid parameters from Lua env\n");
  390. return app_lua_return_false(L);
  391. }
  392. if (!flag_in_range(flag))
  393. {
  394. LM_ERR("invalid flag parameter %d\n", flag);
  395. return app_lua_return_false(L);
  396. }
  397. setbflag(branch, flag);
  398. return app_lua_return_true(L);
  399. }
  400. /**
  401. *
  402. */
  403. static int lua_sr_resetbflag (lua_State *L)
  404. {
  405. int flag;
  406. int branch;
  407. sr_lua_env_t *env_L;
  408. env_L = sr_lua_env_get();
  409. if(lua_gettop(L)==1)
  410. {
  411. flag = lua_tointeger(L, -1);
  412. branch = 0;
  413. } else if(lua_gettop(L)==2) {
  414. flag = lua_tointeger(L, -2);
  415. branch = lua_tointeger(L, -1);
  416. } else {
  417. LM_WARN("invalid number of parameters from Lua\n");
  418. return app_lua_return_false(L);
  419. }
  420. if(env_L->msg==NULL)
  421. {
  422. LM_WARN("invalid parameters from Lua env\n");
  423. return app_lua_return_false(L);
  424. }
  425. if (!flag_in_range(flag))
  426. {
  427. LM_ERR("invalid flag parameter %d\n", flag);
  428. return app_lua_return_false(L);
  429. }
  430. resetbflag(branch, flag);
  431. return app_lua_return_true(L);
  432. }
  433. /**
  434. *
  435. */
  436. static int lua_sr_isbflagset (lua_State *L)
  437. {
  438. int flag;
  439. int branch;
  440. int ret;
  441. sr_lua_env_t *env_L;
  442. env_L = sr_lua_env_get();
  443. if(lua_gettop(L)==1)
  444. {
  445. flag = lua_tointeger(L, -1);
  446. branch = 0;
  447. } else if(lua_gettop(L)==2) {
  448. flag = lua_tointeger(L, -2);
  449. branch = lua_tointeger(L, -1);
  450. } else {
  451. LM_WARN("invalid number of parameters from Lua\n");
  452. return app_lua_return_false(L);
  453. }
  454. if(env_L->msg==NULL)
  455. {
  456. LM_WARN("invalid parameters from Lua env\n");
  457. return app_lua_return_false(L);
  458. }
  459. if (!flag_in_range(flag))
  460. {
  461. LM_ERR("invalid flag parameter %d\n", flag);
  462. return app_lua_return_false(L);
  463. }
  464. ret = isbflagset(branch, flag);
  465. if(ret>0)
  466. return app_lua_return_true(L);
  467. return app_lua_return_false(L);
  468. }
  469. /**
  470. *
  471. */
  472. static int lua_sr_seturi (lua_State *L)
  473. {
  474. struct action act;
  475. struct run_act_ctx h;
  476. str uri;
  477. sr_lua_env_t *env_L;
  478. env_L = sr_lua_env_get();
  479. uri.s = (char*)lua_tostring(L, -1);
  480. if(uri.s==NULL)
  481. {
  482. LM_ERR("invalid uri parameter\n");
  483. return app_lua_return_false(L);
  484. }
  485. uri.len = strlen(uri.s);
  486. if(env_L->msg==NULL)
  487. {
  488. LM_WARN("invalid parameters from Lua env\n");
  489. return app_lua_return_false(L);
  490. }
  491. memset(&act, 0, sizeof(act));
  492. act.val[0].type = STRING_ST;
  493. act.val[0].u.string = uri.s;
  494. act.type = SET_URI_T;
  495. init_run_actions_ctx(&h);
  496. if (do_action(&h, &act, env_L->msg)<0)
  497. {
  498. LM_ERR("do action failed\n");
  499. return app_lua_return_false(L);
  500. }
  501. return app_lua_return_true(L);
  502. }
  503. /**
  504. *
  505. */
  506. static int lua_sr_setuser (lua_State *L)
  507. {
  508. struct action act;
  509. struct run_act_ctx h;
  510. str uri;
  511. sr_lua_env_t *env_L;
  512. env_L = sr_lua_env_get();
  513. uri.s = (char*)lua_tostring(L, -1);
  514. if(uri.s==NULL)
  515. {
  516. LM_ERR("invalid uri parameter\n");
  517. return app_lua_return_false(L);
  518. }
  519. uri.len = strlen(uri.s);
  520. if(env_L->msg==NULL)
  521. {
  522. LM_WARN("invalid parameters from Lua env\n");
  523. return app_lua_return_false(L);
  524. }
  525. memset(&act, 0, sizeof(act));
  526. act.val[0].type = STRING_ST;
  527. act.val[0].u.string = uri.s;
  528. act.type = SET_USER_T;
  529. init_run_actions_ctx(&h);
  530. if (do_action(&h, &act, env_L->msg)<0)
  531. {
  532. LM_ERR("do action failed\n");
  533. return app_lua_return_false(L);
  534. }
  535. return app_lua_return_true(L);
  536. }
  537. /**
  538. *
  539. */
  540. static int lua_sr_sethost (lua_State *L)
  541. {
  542. struct action act;
  543. struct run_act_ctx h;
  544. str uri;
  545. sr_lua_env_t *env_L;
  546. env_L = sr_lua_env_get();
  547. uri.s = (char*)lua_tostring(L, -1);
  548. if(uri.s==NULL)
  549. {
  550. LM_ERR("invalid uri parameter\n");
  551. return app_lua_return_false(L);
  552. }
  553. uri.len = strlen(uri.s);
  554. if(env_L->msg==NULL)
  555. {
  556. LM_WARN("invalid parameters from Lua env\n");
  557. return app_lua_return_false(L);
  558. }
  559. memset(&act, 0, sizeof(act));
  560. act.val[0].type = STRING_ST;
  561. act.val[0].u.string = uri.s;
  562. act.type = SET_HOST_T;
  563. init_run_actions_ctx(&h);
  564. if (do_action(&h, &act, env_L->msg)<0)
  565. {
  566. LM_ERR("do action failed\n");
  567. return app_lua_return_false(L);
  568. }
  569. return app_lua_return_true(L);
  570. }
  571. /**
  572. *
  573. */
  574. static int lua_sr_setdsturi (lua_State *L)
  575. {
  576. str uri;
  577. sr_lua_env_t *env_L;
  578. env_L = sr_lua_env_get();
  579. uri.s = (char*)lua_tostring(L, -1);
  580. if(uri.s==NULL)
  581. {
  582. LM_ERR("invalid uri parameter\n");
  583. return app_lua_return_false(L);
  584. }
  585. uri.len = strlen(uri.s);
  586. if(env_L->msg==NULL)
  587. {
  588. LM_WARN("invalid parameters from Lua env\n");
  589. return app_lua_return_false(L);
  590. }
  591. if (set_dst_uri(env_L->msg, &uri)<0)
  592. {
  593. LM_ERR("setting dst uri failed\n");
  594. return app_lua_return_false(L);
  595. }
  596. return app_lua_return_true(L);
  597. }
  598. /**
  599. *
  600. */
  601. static int lua_sr_resetdsturi (lua_State *L)
  602. {
  603. sr_lua_env_t *env_L;
  604. env_L = sr_lua_env_get();
  605. if(env_L->msg==NULL)
  606. {
  607. LM_WARN("invalid parameters from Lua env\n");
  608. return app_lua_return_false(L);
  609. }
  610. reset_dst_uri(env_L->msg);
  611. return app_lua_return_true(L);
  612. }
  613. /**
  614. *
  615. */
  616. static const luaL_reg _sr_core_Map [] = {
  617. {"probe", lua_sr_probe},
  618. {"dbg", lua_sr_dbg},
  619. {"err", lua_sr_err},
  620. {"log", lua_sr_log},
  621. {"modf", lua_sr_modf},
  622. {"is_myself", lua_sr_is_myself},
  623. {"setflag", lua_sr_setflag},
  624. {"resetflag", lua_sr_resetflag},
  625. {"isflagset", lua_sr_isflagset},
  626. {"setbflag", lua_sr_setbflag},
  627. {"resetbflag", lua_sr_resetbflag},
  628. {"isbflagset", lua_sr_isbflagset},
  629. {"seturi", lua_sr_seturi},
  630. {"setuser", lua_sr_setuser},
  631. {"sethost", lua_sr_sethost},
  632. {"setdsturi", lua_sr_setdsturi},
  633. {"resetdsturi", lua_sr_resetdsturi},
  634. {NULL, NULL}
  635. };
  636. /**
  637. *
  638. */
  639. static int lua_sr_hdr_append (lua_State *L)
  640. {
  641. struct lump* anchor;
  642. struct hdr_field *hf;
  643. char *txt;
  644. int len;
  645. char *hdr;
  646. sr_lua_env_t *env_L;
  647. env_L = sr_lua_env_get();
  648. txt = (char*)lua_tostring(L, -1);
  649. if(txt==NULL || env_L->msg==NULL)
  650. return 0;
  651. LM_DBG("append hf: %s\n", txt);
  652. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1)
  653. {
  654. LM_ERR("error while parsing message\n");
  655. return 0;
  656. }
  657. hf = env_L->msg->last_header;
  658. len = strlen(txt);
  659. hdr = (char*)pkg_malloc(len);
  660. if(hdr==NULL)
  661. {
  662. LM_ERR("no pkg memory left\n");
  663. return 0;
  664. }
  665. memcpy(hdr, txt, len);
  666. anchor = anchor_lump(env_L->msg,
  667. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  668. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  669. {
  670. LM_ERR("can't insert lump\n");
  671. pkg_free(hdr);
  672. return 0;
  673. }
  674. return 0;
  675. }
  676. /**
  677. *
  678. */
  679. static int lua_sr_hdr_remove (lua_State *L)
  680. {
  681. struct lump* anchor;
  682. struct hdr_field *hf;
  683. char *txt;
  684. str hname;
  685. sr_lua_env_t *env_L;
  686. env_L = sr_lua_env_get();
  687. txt = (char*)lua_tostring(L, -1);
  688. if(txt==NULL || env_L->msg==NULL)
  689. return 0;
  690. LM_DBG("remove hf: %s\n", txt);
  691. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1) {
  692. LM_ERR("error while parsing message\n");
  693. return 0;
  694. }
  695. hname.s = txt;
  696. hname.len = strlen(txt);
  697. for (hf=env_L->msg->headers; hf; hf=hf->next)
  698. {
  699. if (cmp_hdrname_str(&hf->name, &hname)==0)
  700. {
  701. anchor=del_lump(env_L->msg,
  702. hf->name.s - env_L->msg->buf, hf->len, 0);
  703. if (anchor==0)
  704. {
  705. LM_ERR("cannot remove hdr %s\n", txt);
  706. return 0;
  707. }
  708. }
  709. }
  710. return 0;
  711. }
  712. /**
  713. *
  714. */
  715. static int lua_sr_hdr_insert (lua_State *L)
  716. {
  717. struct lump* anchor;
  718. struct hdr_field *hf;
  719. char *txt;
  720. int len;
  721. char *hdr;
  722. sr_lua_env_t *env_L;
  723. env_L = sr_lua_env_get();
  724. txt = (char*)lua_tostring(L, -1);
  725. if(txt==NULL || env_L->msg==NULL)
  726. return 0;
  727. LM_DBG("insert hf: %s\n", txt);
  728. hf = env_L->msg->headers;
  729. len = strlen(txt);
  730. hdr = (char*)pkg_malloc(len);
  731. if(hdr==NULL)
  732. {
  733. LM_ERR("no pkg memory left\n");
  734. return 0;
  735. }
  736. memcpy(hdr, txt, len);
  737. anchor = anchor_lump(env_L->msg,
  738. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  739. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  740. {
  741. LM_ERR("can't insert lump\n");
  742. pkg_free(hdr);
  743. return 0;
  744. }
  745. return 0;
  746. }
  747. /**
  748. *
  749. */
  750. static int lua_sr_hdr_append_to_reply (lua_State *L)
  751. {
  752. char *txt;
  753. int len;
  754. sr_lua_env_t *env_L;
  755. env_L = sr_lua_env_get();
  756. txt = (char*)lua_tostring(L, -1);
  757. if(txt==NULL || env_L->msg==NULL)
  758. return 0;
  759. LM_DBG("append to reply: %s\n", txt);
  760. len = strlen(txt);
  761. if(add_lump_rpl(env_L->msg, txt, len, LUMP_RPL_HDR)==0)
  762. {
  763. LM_ERR("unable to add reply lump\n");
  764. return 0;
  765. }
  766. return 0;
  767. }
  768. /**
  769. *
  770. */
  771. static const luaL_reg _sr_hdr_Map [] = {
  772. {"append", lua_sr_hdr_append},
  773. {"remove", lua_sr_hdr_remove},
  774. {"insert", lua_sr_hdr_insert},
  775. {"append_to_reply", lua_sr_hdr_append_to_reply},
  776. {NULL, NULL}
  777. };
  778. /**
  779. *
  780. */
  781. static int lua_sr_pv_get (lua_State *L)
  782. {
  783. str pvn;
  784. pv_spec_t *pvs;
  785. pv_value_t val;
  786. sr_lua_env_t *env_L;
  787. int pl;
  788. env_L = sr_lua_env_get();
  789. pvn.s = (char*)lua_tostring(L, -1);
  790. if(pvn.s==NULL || env_L->msg==NULL)
  791. return 0;
  792. pvn.len = strlen(pvn.s);
  793. LM_DBG("pv get: %s\n", pvn.s);
  794. pl = pv_locate_name(&pvn);
  795. if(pl != pvn.len)
  796. {
  797. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  798. return 0;
  799. }
  800. pvs = pv_cache_get(&pvn);
  801. if(pvs==NULL)
  802. {
  803. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  804. return 0;
  805. }
  806. memset(&val, 0, sizeof(pv_value_t));
  807. if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
  808. {
  809. LM_ERR("unable to get pv value for [%s]\n", pvn.s);
  810. return 0;
  811. }
  812. if(val.flags&PV_VAL_NULL)
  813. {
  814. return 0;
  815. }
  816. if(val.flags&PV_TYPE_INT)
  817. {
  818. lua_pushinteger(L, val.ri);
  819. return 1;
  820. }
  821. lua_pushlstring(L, val.rs.s, val.rs.len);
  822. return 1;
  823. }
  824. /**
  825. *
  826. */
  827. static int lua_sr_pv_seti (lua_State *L)
  828. {
  829. str pvn;
  830. pv_spec_t *pvs;
  831. pv_value_t val;
  832. sr_lua_env_t *env_L;
  833. int pl;
  834. env_L = sr_lua_env_get();
  835. if(lua_gettop(L)<2)
  836. {
  837. LM_ERR("to few parameters [%d]\n", lua_gettop(L));
  838. return 0;
  839. }
  840. if(!lua_isnumber(L, -1))
  841. {
  842. LM_ERR("invalid int parameter\n");
  843. return 0;
  844. }
  845. memset(&val, 0, sizeof(pv_value_t));
  846. val.ri = lua_tointeger(L, -1);
  847. val.flags |= PV_TYPE_INT|PV_VAL_INT;
  848. pvn.s = (char*)lua_tostring(L, -2);
  849. if(pvn.s==NULL || env_L->msg==NULL)
  850. return 0;
  851. pvn.len = strlen(pvn.s);
  852. LM_DBG("pv set: %s\n", pvn.s);
  853. pl = pv_locate_name(&pvn);
  854. if(pl != pvn.len)
  855. {
  856. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  857. return 0;
  858. }
  859. pvs = pv_cache_get(&pvn);
  860. if(pvs==NULL)
  861. {
  862. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  863. return 0;
  864. }
  865. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  866. {
  867. LM_ERR("unable to set pv [%s]\n", pvn.s);
  868. return 0;
  869. }
  870. return 0;
  871. }
  872. /**
  873. *
  874. */
  875. static int lua_sr_pv_sets (lua_State *L)
  876. {
  877. str pvn;
  878. pv_spec_t *pvs;
  879. pv_value_t val;
  880. sr_lua_env_t *env_L;
  881. int pl;
  882. env_L = sr_lua_env_get();
  883. if(lua_gettop(L)<2)
  884. {
  885. LM_ERR("to few parameters [%d]\n",lua_gettop(L));
  886. return 0;
  887. }
  888. if(!lua_isstring(L, -1))
  889. {
  890. LM_ERR("Cannot convert to a string when assigning value to variable: %s\n",
  891. lua_tostring(L, -2));
  892. return 0;
  893. }
  894. memset(&val, 0, sizeof(pv_value_t));
  895. val.rs.s = (char*)lua_tostring(L, -1);
  896. val.rs.len = strlen(val.rs.s);
  897. val.flags |= PV_VAL_STR;
  898. pvn.s = (char*)lua_tostring(L, -2);
  899. if(pvn.s==NULL || env_L->msg==NULL)
  900. return 0;
  901. pvn.len = strlen(pvn.s);
  902. LM_DBG("pv set: %s\n", pvn.s);
  903. pl = pv_locate_name(&pvn);
  904. if(pl != pvn.len)
  905. {
  906. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  907. return 0;
  908. }
  909. pvs = pv_cache_get(&pvn);
  910. if(pvs==NULL)
  911. {
  912. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  913. return 0;
  914. }
  915. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  916. {
  917. LM_ERR("unable to set pv [%s]\n", pvn.s);
  918. return 0;
  919. }
  920. return 0;
  921. }
  922. /**
  923. *
  924. */
  925. static int lua_sr_pv_unset (lua_State *L)
  926. {
  927. str pvn;
  928. pv_spec_t *pvs;
  929. pv_value_t val;
  930. sr_lua_env_t *env_L;
  931. int pl;
  932. env_L = sr_lua_env_get();
  933. pvn.s = (char*)lua_tostring(L, -1);
  934. if(pvn.s==NULL || env_L->msg==NULL)
  935. return 0;
  936. pvn.len = strlen(pvn.s);
  937. LM_DBG("pv unset: %s\n", pvn.s);
  938. pl = pv_locate_name(&pvn);
  939. if(pl != pvn.len)
  940. {
  941. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  942. return 0;
  943. }
  944. pvs = pv_cache_get(&pvn);
  945. if(pvs==NULL)
  946. {
  947. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  948. return 0;
  949. }
  950. memset(&val, 0, sizeof(pv_value_t));
  951. val.flags |= PV_VAL_NULL;
  952. if(pv_set_spec_value(env_L->msg, pvs, 0, &val)<0)
  953. {
  954. LM_ERR("unable to unset pv [%s]\n", pvn.s);
  955. return 0;
  956. }
  957. return 0;
  958. }
  959. /**
  960. *
  961. */
  962. static int lua_sr_pv_is_null (lua_State *L)
  963. {
  964. str pvn;
  965. pv_spec_t *pvs;
  966. pv_value_t val;
  967. sr_lua_env_t *env_L;
  968. int pl;
  969. env_L = sr_lua_env_get();
  970. pvn.s = (char*)lua_tostring(L, -1);
  971. if(pvn.s==NULL || env_L->msg==NULL)
  972. return 0;
  973. pvn.len = strlen(pvn.s);
  974. LM_DBG("pv is null test: %s\n", pvn.s);
  975. pl = pv_locate_name(&pvn);
  976. if(pl != pvn.len)
  977. {
  978. LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
  979. return 0;
  980. }
  981. pvs = pv_cache_get(&pvn);
  982. if(pvs==NULL)
  983. {
  984. LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
  985. return 0;
  986. }
  987. memset(&val, 0, sizeof(pv_value_t));
  988. if(pv_get_spec_value(env_L->msg, pvs, &val) != 0)
  989. {
  990. LM_NOTICE("unable to get pv value for [%s]\n", pvn.s);
  991. lua_pushboolean(L, 1);
  992. return 1;
  993. }
  994. if(val.flags&PV_VAL_NULL)
  995. {
  996. lua_pushboolean(L, 1);
  997. } else {
  998. lua_pushboolean(L, 0);
  999. }
  1000. return 1;
  1001. }
  1002. /**
  1003. *
  1004. */
  1005. static const luaL_reg _sr_pv_Map [] = {
  1006. {"get", lua_sr_pv_get},
  1007. {"seti", lua_sr_pv_seti},
  1008. {"sets", lua_sr_pv_sets},
  1009. {"unset", lua_sr_pv_unset},
  1010. {"is_null", lua_sr_pv_is_null},
  1011. {NULL, NULL}
  1012. };
  1013. /**
  1014. * creates and push a table to the lua stack with
  1015. * the elements of the list
  1016. */
  1017. static int lua_sr_push_str_list_table(lua_State *L, struct str_list *list) {
  1018. lua_Number i = 1;
  1019. struct str_list *k = list;
  1020. lua_newtable(L);
  1021. while(k!=NULL){
  1022. lua_pushnumber(L, i);
  1023. lua_pushlstring(L, k->s.s, k->s.len);
  1024. lua_settable(L, -3);
  1025. i++;
  1026. k = k->next;
  1027. }
  1028. return 1;
  1029. }
  1030. static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag);
  1031. /**
  1032. * creates and push a table for the key name in xavp
  1033. * if simple_flag is != 0 it will return only the first value
  1034. */
  1035. static void lua_sr_push_xavp_name_table(lua_State *L, sr_xavp_t *xavp,
  1036. str name, const int simple_flag)
  1037. {
  1038. lua_Number i = 1;
  1039. lua_Number elem = 1;
  1040. sr_xavp_t *avp = xavp;
  1041. while(avp!=NULL&&!STR_EQ(avp->name,name))
  1042. {
  1043. avp = avp->next;
  1044. }
  1045. if(simple_flag==0) lua_newtable(L);
  1046. while(avp!=NULL){
  1047. if(simple_flag==0) lua_pushnumber(L, elem);
  1048. switch(avp->val.type) {
  1049. case SR_XTYPE_NULL:
  1050. lua_pushnil(L);
  1051. break;
  1052. case SR_XTYPE_INT:
  1053. i = avp->val.v.i;
  1054. lua_pushnumber(L, i);
  1055. break;
  1056. case SR_XTYPE_STR:
  1057. lua_pushlstring(L, avp->val.v.s.s, avp->val.v.s.len);
  1058. break;
  1059. case SR_XTYPE_TIME:
  1060. case SR_XTYPE_LONG:
  1061. case SR_XTYPE_LLONG:
  1062. case SR_XTYPE_DATA:
  1063. lua_pushnil(L);
  1064. LM_WARN("XAVP type:%d value not supported\n", avp->val.type);
  1065. break;
  1066. case SR_XTYPE_XAVP:
  1067. if(!lua_sr_push_xavp_table(L,avp->val.v.xavp, simple_flag)){
  1068. LM_ERR("xavp:%.*s subtable error. Nil value added\n",
  1069. avp->name.len, avp->name.s);
  1070. lua_pushnil(L);
  1071. }
  1072. break;
  1073. default:
  1074. LM_ERR("xavp:%.*s unknown type: %d. Nil value added\n",
  1075. avp->name.len, avp->name.s, avp->val.type);
  1076. lua_pushnil(L);
  1077. break;
  1078. }
  1079. if(simple_flag==0)
  1080. {
  1081. lua_rawset(L, -3);
  1082. elem = elem + 1;
  1083. avp = xavp_get_next(avp);
  1084. }
  1085. else {
  1086. lua_setfield(L, -2, name.s);
  1087. avp = NULL;
  1088. }
  1089. }
  1090. if(simple_flag==0) lua_setfield(L, -2, name.s);
  1091. }
  1092. /**
  1093. * creates and push a table to the lua stack with
  1094. * the elements of the xavp
  1095. */
  1096. static int lua_sr_push_xavp_table(lua_State *L, sr_xavp_t *xavp, const int simple_flag) {
  1097. sr_xavp_t *avp = NULL;
  1098. struct str_list *keys;
  1099. struct str_list *k;
  1100. if(xavp->val.type!=SR_XTYPE_XAVP){
  1101. LM_ERR("%s not xavp?\n", xavp->name.s);
  1102. return 0;
  1103. }
  1104. avp = xavp->val.v.xavp;
  1105. keys = xavp_get_list_key_names(xavp);
  1106. lua_newtable(L);
  1107. if(keys!=NULL)
  1108. {
  1109. do
  1110. {
  1111. lua_sr_push_xavp_name_table(L, avp, keys->s, simple_flag);
  1112. k = keys;
  1113. keys = keys->next;
  1114. pkg_free(k);
  1115. }while(keys!=NULL);
  1116. }
  1117. return 1;
  1118. }
  1119. /**
  1120. * puts a table with content of a xavp
  1121. */
  1122. static int lua_sr_xavp_get(lua_State *L)
  1123. {
  1124. str xavp_name;
  1125. int indx = 0;
  1126. sr_lua_env_t *env_L;
  1127. sr_xavp_t *avp;
  1128. int num_param = 0;
  1129. int param = -1;
  1130. int all_flag = 0;
  1131. int simple_flag = 0;
  1132. lua_Number elem = 1;
  1133. int xavp_size = 0;
  1134. env_L = sr_lua_env_get();
  1135. num_param = lua_gettop(L);
  1136. if(num_param<2 && num_param>3)
  1137. {
  1138. LM_ERR("wrong number of parameters [%d]\n", num_param);
  1139. return 0;
  1140. }
  1141. if(num_param==3)
  1142. {
  1143. if(!lua_isnumber(L, param))
  1144. {
  1145. LM_ERR("invalid int parameter\n");
  1146. return 0;
  1147. }
  1148. simple_flag = lua_tointeger(L, param);
  1149. param = param - 1;
  1150. }
  1151. if(!lua_isnumber(L, param))
  1152. {
  1153. if(lua_isnil(L, param))
  1154. {
  1155. all_flag = 1;
  1156. }
  1157. else
  1158. {
  1159. LM_ERR("invalid parameter, must be int or nil\n");
  1160. return 0;
  1161. }
  1162. }
  1163. else
  1164. {
  1165. indx = lua_tointeger(L, param);
  1166. }
  1167. param = param - 1;
  1168. xavp_name.s = (char*)lua_tostring(L, param);
  1169. if(xavp_name.s==NULL || env_L->msg==NULL)
  1170. {
  1171. LM_ERR("No xavp name in %d param\n", param);
  1172. return 0;
  1173. }
  1174. xavp_name.len = strlen(xavp_name.s);
  1175. if(all_flag>0) {
  1176. indx = 0;
  1177. lua_newtable(L);
  1178. }
  1179. xavp_size = xavp_count(&xavp_name, NULL);
  1180. if(indx<0)
  1181. {
  1182. if((indx*-1)>xavp_size)
  1183. {
  1184. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1185. lua_pushnil(L);
  1186. return 1;
  1187. }
  1188. indx = xavp_size + indx;
  1189. }
  1190. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1191. do
  1192. {
  1193. if(avp==NULL){
  1194. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1195. lua_pushnil(L);
  1196. return 1;
  1197. }
  1198. if(all_flag!=0) {
  1199. lua_pushnumber(L, elem);
  1200. elem = elem + 1;
  1201. }
  1202. lua_sr_push_xavp_table(L, avp, simple_flag);
  1203. if(all_flag!=0) {
  1204. lua_rawset(L, -3);
  1205. indx = indx + 1;
  1206. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1207. }
  1208. else return 1;
  1209. }while(avp!=NULL);
  1210. return 1;
  1211. }
  1212. /**
  1213. * puts a table with the list of keys of the xavp
  1214. */
  1215. static int lua_sr_xavp_get_keys (lua_State *L)
  1216. {
  1217. str xavp_name;
  1218. int indx = 0;
  1219. sr_lua_env_t *env_L;
  1220. sr_xavp_t *avp;
  1221. struct str_list *keys, *k;
  1222. env_L = sr_lua_env_get();
  1223. if(lua_gettop(L)<2)
  1224. {
  1225. LM_ERR("to few parameters [%d]\n",lua_gettop(L));
  1226. return 0;
  1227. }
  1228. if(!lua_isnumber(L, -1))
  1229. {
  1230. LM_ERR("invalid int parameter\n");
  1231. return 0;
  1232. }
  1233. indx = lua_tointeger(L, -1);
  1234. xavp_name.s = (char*)lua_tostring(L, -2);
  1235. if(xavp_name.s==NULL || env_L->msg==NULL)
  1236. return 0;
  1237. xavp_name.len = strlen(xavp_name.s);
  1238. avp = xavp_get_by_index(&xavp_name, indx, NULL);
  1239. if(avp==NULL){
  1240. LM_ERR("can't get xavp:%.*s index:%d\n", xavp_name.len, xavp_name.s, indx);
  1241. lua_pushnil(L);
  1242. return 1;
  1243. }
  1244. keys = xavp_get_list_key_names(avp);
  1245. lua_sr_push_str_list_table(L, keys);
  1246. // free list
  1247. while(keys!=NULL){
  1248. k = keys;
  1249. keys = k->next;
  1250. pkg_free(k);
  1251. }
  1252. return 1;
  1253. }
  1254. /**
  1255. *
  1256. */
  1257. static const luaL_reg _sr_xavp_Map [] = {
  1258. {"get", lua_sr_xavp_get},
  1259. {"get_keys", lua_sr_xavp_get_keys},
  1260. {NULL, NULL}
  1261. };
  1262. /**
  1263. *
  1264. */
  1265. void lua_sr_core_openlibs(lua_State *L)
  1266. {
  1267. luaL_openlib(L, "sr", _sr_core_Map, 0);
  1268. luaL_openlib(L, "sr.hdr", _sr_hdr_Map, 0);
  1269. luaL_openlib(L, "sr.pv", _sr_pv_Map, 0);
  1270. luaL_openlib(L, "sr.xavp", _sr_xavp_Map, 0);
  1271. }