app_lua_sr.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "app_lua_api.h"
  41. #include "app_lua_sr.h"
  42. /**
  43. *
  44. */
  45. static int lua_sr_probe (lua_State *L)
  46. {
  47. LM_DBG("someone probing from lua\n");
  48. return 0;
  49. }
  50. /**
  51. *
  52. */
  53. static int lua_sr_dbg (lua_State *L)
  54. {
  55. char *txt;
  56. txt = (char*)lua_tostring(L, -1);
  57. if(txt!=NULL)
  58. LM_DBG("%s", txt);
  59. return 0;
  60. }
  61. /**
  62. *
  63. */
  64. static int lua_sr_err (lua_State *L)
  65. {
  66. char *txt;
  67. txt = (char*)lua_tostring(L, -1);
  68. if(txt!=NULL)
  69. LM_ERR("%s", txt);
  70. return 0;
  71. }
  72. /**
  73. *
  74. */
  75. static int lua_sr_log (lua_State *L)
  76. {
  77. char *txt;
  78. char *level;
  79. level = (char*)lua_tostring(L, -2);
  80. txt = (char*)lua_tostring(L, -1);
  81. if(txt!=NULL)
  82. {
  83. if(level==NULL)
  84. {
  85. LM_ERR("%s", txt);
  86. } else {
  87. if(strcasecmp(level, "dbg")==0) {
  88. LM_DBG("%s", txt);
  89. } else if(strcasecmp(level, "info")==0) {
  90. LM_INFO("%s", txt);
  91. } else if(strcasecmp(level, "warn")==0) {
  92. LM_WARN("%s", txt);
  93. } else if(strcasecmp(level, "crit")==0) {
  94. LM_CRIT("%s", txt);
  95. } else {
  96. LM_ERR("%s", txt);
  97. }
  98. }
  99. }
  100. return 0;
  101. }
  102. /**
  103. *
  104. */
  105. static int lua_sr_modf (lua_State *L)
  106. {
  107. int ret;
  108. char *luav[MAX_ACTIONS];
  109. char *argv[MAX_ACTIONS];
  110. int argc;
  111. int i;
  112. int mod_type;
  113. struct run_act_ctx ra_ctx;
  114. unsigned modver;
  115. struct action *act;
  116. sr31_cmd_export_t* expf;
  117. sr_lua_env_t *env_L;
  118. ret = 1;
  119. act = NULL;
  120. argc = 0;
  121. memset(luav, 0, MAX_ACTIONS*sizeof(char*));
  122. memset(argv, 0, MAX_ACTIONS*sizeof(char*));
  123. env_L = sr_lua_env_get();
  124. if(env_L->msg==NULL)
  125. goto error;
  126. #if 0
  127. app_lua_dump_stack(L);
  128. #endif
  129. argc = lua_gettop(L);
  130. if(argc==0)
  131. {
  132. LM_ERR("name of module function not provided\n");
  133. goto error;
  134. }
  135. if(argc>=MAX_ACTIONS)
  136. {
  137. LM_ERR("too many parameters\n");
  138. goto error;
  139. }
  140. /* first is function name, then parameters */
  141. for(i=1; i<=argc; i++)
  142. {
  143. if (!lua_isstring(L, i))
  144. {
  145. LM_ERR("invalid parameter type (%d)\n", i);
  146. goto error;
  147. }
  148. luav[i-1] = (char*)lua_tostring(L, i);
  149. }
  150. /* pkg copy only parameters */
  151. for(i=1; i<MAX_ACTIONS; i++)
  152. {
  153. if(luav[i]!=NULL)
  154. {
  155. argv[i] = (char*)pkg_malloc(strlen(luav[i])+1);
  156. if(argv[i]==NULL)
  157. {
  158. LM_ERR("no more pkg\n");
  159. goto error;
  160. }
  161. strcpy(argv[i], luav[i]);
  162. }
  163. }
  164. expf = find_export_record(luav[0], argc-1, 0, &modver);
  165. if (expf==NULL) {
  166. LM_ERR("function '%s' is not available\n", luav[0]);
  167. goto error;
  168. }
  169. /* check fixups */
  170. if (expf->fixup!=NULL && expf->free_fixup==NULL) {
  171. LM_ERR("function '%s' has fixup - cannot be used\n", luav[0]);
  172. goto error;
  173. }
  174. switch(expf->param_no) {
  175. case 0:
  176. mod_type = MODULE0_T;
  177. break;
  178. case 1:
  179. mod_type = MODULE1_T;
  180. break;
  181. case 2:
  182. mod_type = MODULE2_T;
  183. break;
  184. case 3:
  185. mod_type = MODULE3_T;
  186. break;
  187. case 4:
  188. mod_type = MODULE4_T;
  189. break;
  190. case 5:
  191. mod_type = MODULE5_T;
  192. break;
  193. case 6:
  194. mod_type = MODULE6_T;
  195. break;
  196. case VAR_PARAM_NO:
  197. mod_type = MODULEX_T;
  198. break;
  199. default:
  200. LM_ERR("unknown/bad definition for function '%s' (%d params)\n",
  201. luav[0], expf->param_no);
  202. goto error;
  203. }
  204. act = mk_action(mod_type, argc+1 /* number of (type, value) pairs */,
  205. MODEXP_ST, expf, /* function */
  206. NUMBER_ST, argc-1, /* parameter number */
  207. STRING_ST, argv[1], /* param. 1 */
  208. STRING_ST, argv[2], /* param. 2 */
  209. STRING_ST, argv[3], /* param. 3 */
  210. STRING_ST, argv[4], /* param. 4 */
  211. STRING_ST, argv[5], /* param. 5 */
  212. STRING_ST, argv[6] /* param. 6 */
  213. );
  214. if (act==NULL) {
  215. LM_ERR("action structure could not be created for '%s'\n", luav[0]);
  216. goto error;
  217. }
  218. /* handle fixups */
  219. if (expf->fixup) {
  220. if(argc==1)
  221. { /* no parameters */
  222. if(expf->fixup(0, 0)<0)
  223. {
  224. LM_ERR("Error in fixup (0) for '%s'\n", luav[0]);
  225. goto error;
  226. }
  227. } else {
  228. for(i=1; i<=argc; i++)
  229. {
  230. if(expf->fixup(&(act->val[i+1].u.data), i)<0)
  231. {
  232. LM_ERR("Error in fixup (%d) for '%s'\n", i, luav[0]);
  233. goto error;
  234. }
  235. act->val[i+1].type = MODFIXUP_ST;
  236. }
  237. }
  238. }
  239. init_run_actions_ctx(&ra_ctx);
  240. ret = do_action(&ra_ctx, act, env_L->msg);
  241. /* free fixups */
  242. if (expf->fixup) {
  243. for(i=1; i<=argc; i++)
  244. {
  245. if ((act->val[i+1].type == MODFIXUP_ST) && (act->val[i+1].u.data))
  246. {
  247. expf->free_fixup(&(act->val[i+1].u.data), i);
  248. }
  249. }
  250. }
  251. pkg_free(act);
  252. lua_pushinteger(L, ret);
  253. return 1;
  254. error:
  255. if(act!=NULL)
  256. pkg_free(act);
  257. for(i=0; i<MAX_ACTIONS; i++)
  258. {
  259. if(argv[i]!=NULL) pkg_free(argv[i]);
  260. argv[i] = 0;
  261. }
  262. lua_pushinteger(L, -1);
  263. return 1;
  264. }
  265. /**
  266. *
  267. */
  268. static int lua_sr_is_myself (lua_State *L)
  269. {
  270. str uri;
  271. struct sip_uri puri;
  272. int ret;
  273. uri.s = (char*)lua_tostring(L, -1);
  274. if(uri.s==NULL)
  275. {
  276. LM_ERR("invalid uri parameter\n");
  277. return app_lua_return_false(L);
  278. }
  279. uri.len = strlen(uri.s);
  280. if(uri.len>4 && (strncmp(uri.s, "sip:", 4)==0
  281. || strncmp(uri.s, "sips:", 5)==0))
  282. {
  283. if(parse_uri(uri.s, uri.len, &puri)!=0)
  284. {
  285. LM_ERR("failed to parse uri [%s]\n", uri.s);
  286. return app_lua_return_false(L);
  287. }
  288. ret = check_self(&puri.host, (puri.port.s)?puri.port_no:0,
  289. (puri.transport_val.s)?puri.proto:0);
  290. } else {
  291. ret = check_self(&uri, 0, 0);
  292. }
  293. if(ret==1)
  294. return app_lua_return_true(L);
  295. return app_lua_return_false(L);
  296. }
  297. /**
  298. *
  299. */
  300. static int lua_sr_setflag (lua_State *L)
  301. {
  302. int flag;
  303. sr_lua_env_t *env_L;
  304. env_L = sr_lua_env_get();
  305. flag = lua_tointeger(L, -1);
  306. if(env_L->msg==NULL)
  307. {
  308. LM_WARN("invalid parameters from Lua env\n");
  309. return app_lua_return_false(L);
  310. }
  311. if (!flag_in_range(flag))
  312. {
  313. LM_ERR("invalid flag parameter %d\n", flag);
  314. return app_lua_return_false(L);
  315. }
  316. setflag(env_L->msg, flag);
  317. return app_lua_return_true(L);
  318. }
  319. /**
  320. *
  321. */
  322. static int lua_sr_resetflag (lua_State *L)
  323. {
  324. int flag;
  325. sr_lua_env_t *env_L;
  326. env_L = sr_lua_env_get();
  327. flag = lua_tointeger(L, -1);
  328. if(env_L->msg==NULL)
  329. {
  330. LM_WARN("invalid parameters from Lua env\n");
  331. return app_lua_return_false(L);
  332. }
  333. if (!flag_in_range(flag))
  334. {
  335. LM_ERR("invalid flag parameter %d\n", flag);
  336. return app_lua_return_false(L);
  337. }
  338. resetflag(env_L->msg, flag);
  339. return app_lua_return_true(L);
  340. }
  341. /**
  342. *
  343. */
  344. static int lua_sr_isflagset (lua_State *L)
  345. {
  346. int flag;
  347. int ret;
  348. sr_lua_env_t *env_L;
  349. env_L = sr_lua_env_get();
  350. flag = lua_tointeger(L, -1);
  351. if(env_L->msg==NULL)
  352. {
  353. LM_WARN("invalid parameters from Lua env\n");
  354. return app_lua_return_false(L);
  355. }
  356. if (!flag_in_range(flag))
  357. {
  358. LM_ERR("invalid flag parameter %d\n", flag);
  359. return app_lua_return_false(L);
  360. }
  361. ret = isflagset(env_L->msg, flag);
  362. if(ret>0)
  363. return app_lua_return_true(L);
  364. return app_lua_return_false(L);
  365. }
  366. /**
  367. *
  368. */
  369. static int lua_sr_setbflag (lua_State *L)
  370. {
  371. int flag;
  372. int branch;
  373. sr_lua_env_t *env_L;
  374. env_L = sr_lua_env_get();
  375. if(lua_gettop(L)==1)
  376. {
  377. flag = lua_tointeger(L, -1);
  378. branch = 0;
  379. } else if(lua_gettop(L)==2) {
  380. flag = lua_tointeger(L, -2);
  381. branch = lua_tointeger(L, -1);
  382. } else {
  383. LM_WARN("invalid number of parameters from Lua\n");
  384. return app_lua_return_false(L);
  385. }
  386. if(env_L->msg==NULL)
  387. {
  388. LM_WARN("invalid parameters from Lua env\n");
  389. return app_lua_return_false(L);
  390. }
  391. if (!flag_in_range(flag))
  392. {
  393. LM_ERR("invalid flag parameter %d\n", flag);
  394. return app_lua_return_false(L);
  395. }
  396. setbflag(branch, flag);
  397. return app_lua_return_true(L);
  398. }
  399. /**
  400. *
  401. */
  402. static int lua_sr_resetbflag (lua_State *L)
  403. {
  404. int flag;
  405. int branch;
  406. sr_lua_env_t *env_L;
  407. env_L = sr_lua_env_get();
  408. if(lua_gettop(L)==1)
  409. {
  410. flag = lua_tointeger(L, -1);
  411. branch = 0;
  412. } else if(lua_gettop(L)==2) {
  413. flag = lua_tointeger(L, -2);
  414. branch = lua_tointeger(L, -1);
  415. } else {
  416. LM_WARN("invalid number of parameters from Lua\n");
  417. return app_lua_return_false(L);
  418. }
  419. if(env_L->msg==NULL)
  420. {
  421. LM_WARN("invalid parameters from Lua env\n");
  422. return app_lua_return_false(L);
  423. }
  424. if (!flag_in_range(flag))
  425. {
  426. LM_ERR("invalid flag parameter %d\n", flag);
  427. return app_lua_return_false(L);
  428. }
  429. resetbflag(branch, flag);
  430. return app_lua_return_true(L);
  431. }
  432. /**
  433. *
  434. */
  435. static int lua_sr_isbflagset (lua_State *L)
  436. {
  437. int flag;
  438. int branch;
  439. int ret;
  440. sr_lua_env_t *env_L;
  441. env_L = sr_lua_env_get();
  442. if(lua_gettop(L)==1)
  443. {
  444. flag = lua_tointeger(L, -1);
  445. branch = 0;
  446. } else if(lua_gettop(L)==2) {
  447. flag = lua_tointeger(L, -2);
  448. branch = lua_tointeger(L, -1);
  449. } else {
  450. LM_WARN("invalid number of parameters from Lua\n");
  451. return app_lua_return_false(L);
  452. }
  453. if(env_L->msg==NULL)
  454. {
  455. LM_WARN("invalid parameters from Lua env\n");
  456. return app_lua_return_false(L);
  457. }
  458. if (!flag_in_range(flag))
  459. {
  460. LM_ERR("invalid flag parameter %d\n", flag);
  461. return app_lua_return_false(L);
  462. }
  463. ret = isbflagset(branch, flag);
  464. if(ret>0)
  465. return app_lua_return_true(L);
  466. return app_lua_return_false(L);
  467. }
  468. /**
  469. *
  470. */
  471. static int lua_sr_seturi (lua_State *L)
  472. {
  473. struct action act;
  474. struct run_act_ctx h;
  475. str uri;
  476. sr_lua_env_t *env_L;
  477. env_L = sr_lua_env_get();
  478. uri.s = (char*)lua_tostring(L, -1);
  479. if(uri.s==NULL)
  480. {
  481. LM_ERR("invalid uri parameter\n");
  482. return app_lua_return_false(L);
  483. }
  484. uri.len = strlen(uri.s);
  485. if(env_L->msg==NULL)
  486. {
  487. LM_WARN("invalid parameters from Lua env\n");
  488. return app_lua_return_false(L);
  489. }
  490. memset(&act, 0, sizeof(act));
  491. act.val[0].type = STRING_ST;
  492. act.val[0].u.string = uri.s;
  493. act.type = SET_URI_T;
  494. init_run_actions_ctx(&h);
  495. if (do_action(&h, &act, env_L->msg)<0)
  496. {
  497. LM_ERR("do action failed\n");
  498. return app_lua_return_false(L);
  499. }
  500. return app_lua_return_true(L);
  501. }
  502. /**
  503. *
  504. */
  505. static int lua_sr_setuser (lua_State *L)
  506. {
  507. struct action act;
  508. struct run_act_ctx h;
  509. str uri;
  510. sr_lua_env_t *env_L;
  511. env_L = sr_lua_env_get();
  512. uri.s = (char*)lua_tostring(L, -1);
  513. if(uri.s==NULL)
  514. {
  515. LM_ERR("invalid uri parameter\n");
  516. return app_lua_return_false(L);
  517. }
  518. uri.len = strlen(uri.s);
  519. if(env_L->msg==NULL)
  520. {
  521. LM_WARN("invalid parameters from Lua env\n");
  522. return app_lua_return_false(L);
  523. }
  524. memset(&act, 0, sizeof(act));
  525. act.val[0].type = STRING_ST;
  526. act.val[0].u.string = uri.s;
  527. act.type = SET_USER_T;
  528. init_run_actions_ctx(&h);
  529. if (do_action(&h, &act, env_L->msg)<0)
  530. {
  531. LM_ERR("do action failed\n");
  532. return app_lua_return_false(L);
  533. }
  534. return app_lua_return_true(L);
  535. }
  536. /**
  537. *
  538. */
  539. static int lua_sr_sethost (lua_State *L)
  540. {
  541. struct action act;
  542. struct run_act_ctx h;
  543. str uri;
  544. sr_lua_env_t *env_L;
  545. env_L = sr_lua_env_get();
  546. uri.s = (char*)lua_tostring(L, -1);
  547. if(uri.s==NULL)
  548. {
  549. LM_ERR("invalid uri parameter\n");
  550. return app_lua_return_false(L);
  551. }
  552. uri.len = strlen(uri.s);
  553. if(env_L->msg==NULL)
  554. {
  555. LM_WARN("invalid parameters from Lua env\n");
  556. return app_lua_return_false(L);
  557. }
  558. memset(&act, 0, sizeof(act));
  559. act.val[0].type = STRING_ST;
  560. act.val[0].u.string = uri.s;
  561. act.type = SET_HOST_T;
  562. init_run_actions_ctx(&h);
  563. if (do_action(&h, &act, env_L->msg)<0)
  564. {
  565. LM_ERR("do action failed\n");
  566. return app_lua_return_false(L);
  567. }
  568. return app_lua_return_true(L);
  569. }
  570. /**
  571. *
  572. */
  573. static int lua_sr_setdsturi (lua_State *L)
  574. {
  575. str uri;
  576. sr_lua_env_t *env_L;
  577. env_L = sr_lua_env_get();
  578. uri.s = (char*)lua_tostring(L, -1);
  579. if(uri.s==NULL)
  580. {
  581. LM_ERR("invalid uri parameter\n");
  582. return app_lua_return_false(L);
  583. }
  584. uri.len = strlen(uri.s);
  585. if(env_L->msg==NULL)
  586. {
  587. LM_WARN("invalid parameters from Lua env\n");
  588. return app_lua_return_false(L);
  589. }
  590. if (set_dst_uri(env_L->msg, &uri)<0)
  591. {
  592. LM_ERR("setting dst uri failed\n");
  593. return app_lua_return_false(L);
  594. }
  595. return app_lua_return_true(L);
  596. }
  597. /**
  598. *
  599. */
  600. static int lua_sr_resetdsturi (lua_State *L)
  601. {
  602. sr_lua_env_t *env_L;
  603. env_L = sr_lua_env_get();
  604. if(env_L->msg==NULL)
  605. {
  606. LM_WARN("invalid parameters from Lua env\n");
  607. return app_lua_return_false(L);
  608. }
  609. reset_dst_uri(env_L->msg);
  610. return app_lua_return_true(L);
  611. }
  612. /**
  613. *
  614. */
  615. static const luaL_reg _sr_core_Map [] = {
  616. {"probe", lua_sr_probe},
  617. {"dbg", lua_sr_dbg},
  618. {"err", lua_sr_err},
  619. {"log", lua_sr_log},
  620. {"modf", lua_sr_modf},
  621. {"is_myself", lua_sr_is_myself},
  622. {"setflag", lua_sr_setflag},
  623. {"resetflag", lua_sr_resetflag},
  624. {"isflagset", lua_sr_isflagset},
  625. {"setbflag", lua_sr_setbflag},
  626. {"resetbflag", lua_sr_resetbflag},
  627. {"isbflagset", lua_sr_isbflagset},
  628. {"seturi", lua_sr_seturi},
  629. {"setuser", lua_sr_setuser},
  630. {"sethost", lua_sr_sethost},
  631. {"setdsturi", lua_sr_setdsturi},
  632. {"resetdsturi", lua_sr_resetdsturi},
  633. {NULL, NULL}
  634. };
  635. /**
  636. *
  637. */
  638. static int lua_sr_hdr_append (lua_State *L)
  639. {
  640. struct lump* anchor;
  641. struct hdr_field *hf;
  642. char *txt;
  643. int len;
  644. char *hdr;
  645. sr_lua_env_t *env_L;
  646. env_L = sr_lua_env_get();
  647. txt = (char*)lua_tostring(L, -1);
  648. if(txt==NULL || env_L->msg==NULL)
  649. return 0;
  650. LM_DBG("append hf: %s\n", txt);
  651. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1)
  652. {
  653. LM_ERR("error while parsing message\n");
  654. return 0;
  655. }
  656. hf = env_L->msg->last_header;
  657. len = strlen(txt);
  658. hdr = (char*)pkg_malloc(len);
  659. if(hdr==NULL)
  660. {
  661. LM_ERR("no pkg memory left\n");
  662. return 0;
  663. }
  664. memcpy(hdr, txt, len);
  665. anchor = anchor_lump(env_L->msg,
  666. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  667. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  668. {
  669. LM_ERR("can't insert lump\n");
  670. pkg_free(hdr);
  671. return 0;
  672. }
  673. return 0;
  674. }
  675. /**
  676. *
  677. */
  678. static int lua_sr_hdr_remove (lua_State *L)
  679. {
  680. struct lump* anchor;
  681. struct hdr_field *hf;
  682. char *txt;
  683. str hname;
  684. sr_lua_env_t *env_L;
  685. env_L = sr_lua_env_get();
  686. txt = (char*)lua_tostring(L, -1);
  687. if(txt==NULL || env_L->msg==NULL)
  688. return 0;
  689. LM_DBG("remove hf: %s\n", txt);
  690. if (parse_headers(env_L->msg, HDR_EOH_F, 0) == -1) {
  691. LM_ERR("error while parsing message\n");
  692. return 0;
  693. }
  694. hname.s = txt;
  695. hname.len = strlen(txt);
  696. for (hf=env_L->msg->headers; hf; hf=hf->next)
  697. {
  698. if (cmp_hdrname_str(&hf->name, &hname)==0)
  699. {
  700. anchor=del_lump(env_L->msg,
  701. hf->name.s - env_L->msg->buf, hf->len, 0);
  702. if (anchor==0)
  703. {
  704. LM_ERR("cannot remove hdr %s\n", txt);
  705. return 0;
  706. }
  707. }
  708. }
  709. return 0;
  710. }
  711. /**
  712. *
  713. */
  714. static int lua_sr_hdr_insert (lua_State *L)
  715. {
  716. struct lump* anchor;
  717. struct hdr_field *hf;
  718. char *txt;
  719. int len;
  720. char *hdr;
  721. sr_lua_env_t *env_L;
  722. env_L = sr_lua_env_get();
  723. txt = (char*)lua_tostring(L, -1);
  724. if(txt==NULL || env_L->msg==NULL)
  725. return 0;
  726. LM_DBG("insert hf: %s\n", txt);
  727. hf = env_L->msg->headers;
  728. len = strlen(txt);
  729. hdr = (char*)pkg_malloc(len);
  730. if(hdr==NULL)
  731. {
  732. LM_ERR("no pkg memory left\n");
  733. return 0;
  734. }
  735. memcpy(hdr, txt, len);
  736. anchor = anchor_lump(env_L->msg,
  737. hf->name.s + hf->len - env_L->msg->buf, 0, 0);
  738. if(insert_new_lump_before(anchor, hdr, len, 0) == 0)
  739. {
  740. LM_ERR("can't insert lump\n");
  741. pkg_free(hdr);
  742. return 0;
  743. }
  744. return 0;
  745. }
  746. /**
  747. *
  748. */
  749. static int lua_sr_hdr_append_to_reply (lua_State *L)
  750. {
  751. char *txt;
  752. int len;
  753. sr_lua_env_t *env_L;
  754. env_L = sr_lua_env_get();
  755. txt = (char*)lua_tostring(L, -1);
  756. if(txt==NULL || env_L->msg==NULL)
  757. return 0;
  758. LM_DBG("append to reply: %s\n", txt);
  759. len = strlen(txt);
  760. if(add_lump_rpl(env_L->msg, txt, len, LUMP_RPL_HDR)==0)
  761. {
  762. LM_ERR("unable to add reply lump\n");
  763. return 0;
  764. }
  765. return 0;
  766. }
  767. /**
  768. *
  769. */
  770. static const luaL_reg _sr_hdr_Map [] = {
  771. {"append", lua_sr_hdr_append},
  772. {"remove", lua_sr_hdr_remove},
  773. {"insert", lua_sr_hdr_insert},
  774. {"append_to_reply", lua_sr_hdr_append_to_reply},
  775. {NULL, NULL}
  776. };
  777. /**
  778. *
  779. */
  780. static int lua_sr_pv_get (lua_State *L)
  781. {
  782. str pvn;
  783. pv_spec_t pvs;
  784. pv_value_t val;
  785. sr_lua_env_t *env_L;
  786. env_L = sr_lua_env_get();
  787. pvn.s = (char*)lua_tostring(L, -1);
  788. if(pvn.s==NULL || env_L->msg==NULL)
  789. return 0;
  790. pvn.len = strlen(pvn.s);
  791. LM_DBG("pv get: %s\n", pvn.s);
  792. if(pv_parse_spec(&pvn, &pvs)<0)
  793. {
  794. LM_ERR("unable to parse pv [%s]\n", pvn.s);
  795. return 0;
  796. }
  797. memset(&val, 0, sizeof(pv_value_t));
  798. if(pv_get_spec_value(env_L->msg, &pvs, &val) != 0)
  799. {
  800. LM_ERR("unable to get pv value for [%s]\n", pvn.s);
  801. return 0;
  802. }
  803. if(val.flags&PV_VAL_NULL)
  804. {
  805. return 0;
  806. }
  807. if(val.flags&PV_TYPE_INT)
  808. {
  809. lua_pushinteger(L, val.ri);
  810. return 1;
  811. }
  812. lua_pushlstring(L, val.rs.s, val.rs.len);
  813. return 1;
  814. }
  815. /**
  816. *
  817. */
  818. static int lua_sr_pv_seti (lua_State *L)
  819. {
  820. str pvn;
  821. pv_spec_t pvs;
  822. pv_value_t val;
  823. sr_lua_env_t *env_L;
  824. env_L = sr_lua_env_get();
  825. if(lua_gettop(L)<2)
  826. {
  827. LM_ERR("to few parameters [%d]\n", lua_gettop(L));
  828. return 0;
  829. }
  830. if(!lua_isnumber(L, -1))
  831. {
  832. LM_ERR("invalid int parameter\n");
  833. return 0;
  834. }
  835. memset(&val, 0, sizeof(pv_value_t));
  836. val.ri = lua_tointeger(L, -1);
  837. val.flags |= PV_TYPE_INT|PV_VAL_INT;
  838. pvn.s = (char*)lua_tostring(L, -2);
  839. if(pvn.s==NULL || env_L->msg==NULL)
  840. return 0;
  841. pvn.len = strlen(pvn.s);
  842. LM_DBG("pv set: %s\n", pvn.s);
  843. if(pv_parse_spec(&pvn, &pvs)<0)
  844. {
  845. LM_ERR("unable to parse pv [%s]\n", pvn.s);
  846. return 0;
  847. }
  848. if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
  849. {
  850. LM_ERR("unable to set pv [%s]\n", pvn.s);
  851. return 0;
  852. }
  853. return 0;
  854. }
  855. /**
  856. *
  857. */
  858. static int lua_sr_pv_sets (lua_State *L)
  859. {
  860. str pvn;
  861. pv_spec_t pvs;
  862. pv_value_t val;
  863. sr_lua_env_t *env_L;
  864. env_L = sr_lua_env_get();
  865. if(lua_gettop(L)<2)
  866. {
  867. LM_ERR("to few parameters [%d]\n",lua_gettop(L));
  868. return 0;
  869. }
  870. memset(&val, 0, sizeof(pv_value_t));
  871. val.rs.s = (char*)lua_tostring(L, -1);
  872. val.rs.len = strlen(val.rs.s);
  873. val.flags |= PV_VAL_STR;
  874. pvn.s = (char*)lua_tostring(L, -2);
  875. if(pvn.s==NULL || env_L->msg==NULL)
  876. return 0;
  877. pvn.len = strlen(pvn.s);
  878. LM_DBG("pv set: %s\n", pvn.s);
  879. if(pv_parse_spec(&pvn, &pvs)<0)
  880. {
  881. LM_ERR("unable to parse pv [%s]\n", pvn.s);
  882. return 0;
  883. }
  884. if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
  885. {
  886. LM_ERR("unable to set pv [%s]\n", pvn.s);
  887. return 0;
  888. }
  889. return 0;
  890. }
  891. /**
  892. *
  893. */
  894. static int lua_sr_pv_unset (lua_State *L)
  895. {
  896. str pvn;
  897. pv_spec_t pvs;
  898. pv_value_t val;
  899. sr_lua_env_t *env_L;
  900. env_L = sr_lua_env_get();
  901. pvn.s = (char*)lua_tostring(L, -1);
  902. if(pvn.s==NULL || env_L->msg==NULL)
  903. return 0;
  904. pvn.len = strlen(pvn.s);
  905. LM_DBG("pv unset: %s\n", pvn.s);
  906. if(pv_parse_spec(&pvn, &pvs)<0)
  907. {
  908. LM_ERR("unable to parse pv [%s]\n", pvn.s);
  909. return 0;
  910. }
  911. memset(&val, 0, sizeof(pv_value_t));
  912. val.flags |= PV_VAL_NULL;
  913. if(pv_set_spec_value(env_L->msg, &pvs, 0, &val)<0)
  914. {
  915. LM_ERR("unable to unset pv [%s]\n", pvn.s);
  916. return 0;
  917. }
  918. return 0;
  919. }
  920. /**
  921. *
  922. */
  923. static int lua_sr_pv_is_null (lua_State *L)
  924. {
  925. str pvn;
  926. pv_spec_t pvs;
  927. pv_value_t val;
  928. sr_lua_env_t *env_L;
  929. env_L = sr_lua_env_get();
  930. pvn.s = (char*)lua_tostring(L, -1);
  931. if(pvn.s==NULL || env_L->msg==NULL)
  932. return 0;
  933. pvn.len = strlen(pvn.s);
  934. LM_DBG("pv is null test: %s\n", pvn.s);
  935. if(pv_parse_spec(&pvn, &pvs)<0)
  936. {
  937. LM_ERR("unable to parse pv [%s]\n", pvn.s);
  938. return 0;
  939. }
  940. memset(&val, 0, sizeof(pv_value_t));
  941. if(pv_get_spec_value(env_L->msg, &pvs, &val) != 0)
  942. {
  943. LM_ERR("unable to get pv value for [%s]\n", pvn.s);
  944. return 0;
  945. }
  946. if(val.flags&PV_VAL_NULL)
  947. {
  948. lua_pushboolean(L, 1);
  949. } else {
  950. lua_pushboolean(L, 0);
  951. }
  952. return 1;
  953. }
  954. /**
  955. *
  956. */
  957. static const luaL_reg _sr_pv_Map [] = {
  958. {"get", lua_sr_pv_get},
  959. {"seti", lua_sr_pv_seti},
  960. {"sets", lua_sr_pv_sets},
  961. {"unset", lua_sr_pv_unset},
  962. {"is_null", lua_sr_pv_is_null},
  963. {NULL, NULL}
  964. };
  965. /**
  966. *
  967. */
  968. void lua_sr_core_openlibs(lua_State *L)
  969. {
  970. luaL_openlib(L, "sr", _sr_core_Map, 0);
  971. luaL_openlib(L, "sr.hdr", _sr_hdr_Map, 0);
  972. luaL_openlib(L, "sr.pv", _sr_pv_Map, 0);
  973. }