app_lua_mod.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "../../sr_module.h"
  27. #include "../../dprint.h"
  28. #include "../../ut.h"
  29. #include "../../mod_fix.h"
  30. #include "app_lua_api.h"
  31. MODULE_VERSION
  32. /** parameters */
  33. /* List of allowed chars for a prefix*/
  34. static int mod_init(void);
  35. static void mod_destroy(void);
  36. static int child_init(int rank);
  37. static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra);
  38. static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra);
  39. static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra);
  40. static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  41. char *p3);
  42. static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
  43. char *p3);
  44. static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
  45. char *p3);
  46. static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
  47. char *p3);
  48. static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
  49. char *p3);
  50. static int fixup_lua_run(void** param, int param_no);
  51. int app_lua_load_param(modparam_t type, void *val);
  52. int app_lua_register_param(modparam_t type, void *val);
  53. static param_export_t params[]={
  54. {"load", STR_PARAM|USE_FUNC_PARAM, (void*)app_lua_load_param},
  55. {"register", STR_PARAM|USE_FUNC_PARAM, (void*)app_lua_register_param},
  56. {0, 0, 0}
  57. };
  58. static cmd_export_t cmds[]={
  59. {"lua_dostring", (cmd_function)w_app_lua_dostring, 1, fixup_spve_null,
  60. 0, ANY_ROUTE},
  61. {"lua_dofile", (cmd_function)w_app_lua_dofile, 1, fixup_spve_null,
  62. 0, ANY_ROUTE},
  63. {"lua_runstring", (cmd_function)w_app_lua_runstring, 1, fixup_spve_null,
  64. 0, ANY_ROUTE},
  65. {"lua_run", (cmd_function)w_app_lua_run0, 1, fixup_lua_run,
  66. 0, ANY_ROUTE},
  67. {"lua_run", (cmd_function)w_app_lua_run1, 2, fixup_lua_run,
  68. 0, ANY_ROUTE},
  69. {"lua_run", (cmd_function)w_app_lua_run2, 3, fixup_lua_run,
  70. 0, ANY_ROUTE},
  71. {"lua_run", (cmd_function)w_app_lua_run3, 4, fixup_lua_run,
  72. 0, ANY_ROUTE},
  73. {0, 0, 0, 0, 0, 0}
  74. };
  75. struct module_exports exports = {
  76. "app_lua",
  77. RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
  78. cmds,
  79. params,
  80. 0,
  81. 0, /* exported MI functions */
  82. 0, /* exported pseudo-variables */
  83. 0, /* extra processes */
  84. mod_init, /* module initialization function */
  85. 0, /* response function */
  86. mod_destroy, /* destroy function */
  87. child_init /* per child init function */
  88. };
  89. /**
  90. * init module function
  91. */
  92. static int mod_init(void)
  93. {
  94. lua_sr_init_mod();
  95. return 0;
  96. }
  97. /* each child get a new connection to the database */
  98. static int child_init(int rank)
  99. {
  100. if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  101. return 0; /* do nothing for the main process */
  102. if (rank==PROC_INIT)
  103. {
  104. /* do a probe before forking */
  105. if(lua_sr_init_probe()!=0)
  106. return -1;
  107. return 0;
  108. }
  109. return lua_sr_init_child();
  110. }
  111. static void mod_destroy(void)
  112. {
  113. lua_sr_destroy();
  114. }
  115. static char _lua_buf_stack[4][512];
  116. static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
  117. {
  118. str s;
  119. if(!lua_sr_initialized())
  120. {
  121. LM_ERR("Lua env not intitialized");
  122. return -1;
  123. }
  124. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  125. {
  126. LM_ERR("cannot get the script\n");
  127. return -1;
  128. }
  129. if(s.len>=511)
  130. {
  131. LM_ERR("script too long %d\n", s.len);
  132. return -1;
  133. }
  134. memcpy(_lua_buf_stack[0], s.s, s.len);
  135. _lua_buf_stack[0][s.len] = '\0';
  136. return app_lua_dostring(msg, _lua_buf_stack[0]);
  137. }
  138. static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra)
  139. {
  140. str s;
  141. if(!lua_sr_initialized())
  142. {
  143. LM_ERR("Lua env not intitialized");
  144. return -1;
  145. }
  146. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  147. {
  148. LM_ERR("cannot get the script\n");
  149. return -1;
  150. }
  151. if(s.len>=511)
  152. {
  153. LM_ERR("script too long %d\n", s.len);
  154. return -1;
  155. }
  156. memcpy(_lua_buf_stack[0], s.s, s.len);
  157. _lua_buf_stack[0][s.len] = '\0';
  158. return app_lua_dofile(msg, _lua_buf_stack[0]);
  159. }
  160. static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
  161. {
  162. str s;
  163. if(!lua_sr_initialized())
  164. {
  165. LM_ERR("Lua env not intitialized");
  166. return -1;
  167. }
  168. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  169. {
  170. LM_ERR("cannot get the script\n");
  171. return -1;
  172. }
  173. if(s.len>=511)
  174. {
  175. LM_ERR("script too long %d\n", s.len);
  176. return -1;
  177. }
  178. memcpy(_lua_buf_stack[0], s.s, s.len);
  179. _lua_buf_stack[0][s.len] = '\0';
  180. return app_lua_runstring(msg, _lua_buf_stack[0]);
  181. }
  182. static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
  183. char *p3)
  184. {
  185. str s;
  186. if(!lua_sr_initialized())
  187. {
  188. LM_ERR("Lua env not intitialized");
  189. return -1;
  190. }
  191. if(fixup_get_svalue(msg, (gparam_p)func, &s)<0)
  192. {
  193. LM_ERR("cannot get the function\n");
  194. return -1;
  195. }
  196. if(s.len>=511)
  197. {
  198. LM_ERR("function too long %d\n", s.len);
  199. return -1;
  200. }
  201. memcpy(_lua_buf_stack[0], s.s, s.len);
  202. _lua_buf_stack[0][s.len] = '\0';
  203. if(p1!=NULL)
  204. {
  205. if(fixup_get_svalue(msg, (gparam_p)p1, &s)<0)
  206. {
  207. LM_ERR("cannot get p1\n");
  208. return -1;
  209. }
  210. if(s.len>=511)
  211. {
  212. LM_ERR("p1 too long %d\n", s.len);
  213. return -1;
  214. }
  215. memcpy(_lua_buf_stack[1], s.s, s.len);
  216. _lua_buf_stack[1][s.len] = '\0';
  217. if(p2!=NULL)
  218. {
  219. if(fixup_get_svalue(msg, (gparam_p)p2, &s)<0)
  220. {
  221. LM_ERR("cannot get p2\n");
  222. return -1;
  223. }
  224. if(s.len>=511)
  225. {
  226. LM_ERR("p2 too long %d\n", s.len);
  227. return -1;
  228. }
  229. memcpy(_lua_buf_stack[2], s.s, s.len);
  230. _lua_buf_stack[2][s.len] = '\0';
  231. if(p3!=NULL)
  232. {
  233. if(fixup_get_svalue(msg, (gparam_p)p3, &s)<0)
  234. {
  235. LM_ERR("cannot get p3\n");
  236. return -1;
  237. }
  238. if(s.len>=511)
  239. {
  240. LM_ERR("p3 too long %d\n", s.len);
  241. return -1;
  242. }
  243. memcpy(_lua_buf_stack[3], s.s, s.len);
  244. _lua_buf_stack[3][s.len] = '\0';
  245. }
  246. } else {
  247. p3 = NULL;
  248. }
  249. } else {
  250. p2 = NULL;
  251. p3 = NULL;
  252. }
  253. return app_lua_run(msg, _lua_buf_stack[0],
  254. (p1!=NULL)?_lua_buf_stack[1]:NULL,
  255. (p2!=NULL)?_lua_buf_stack[2]:NULL,
  256. (p3!=NULL)?_lua_buf_stack[3]:NULL);
  257. }
  258. static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
  259. char *p3)
  260. {
  261. return w_app_lua_run(msg, func, NULL, NULL, NULL);
  262. }
  263. static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
  264. char *p3)
  265. {
  266. return w_app_lua_run(msg, func, p1, NULL, NULL);
  267. }
  268. static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
  269. char *p3)
  270. {
  271. return w_app_lua_run(msg, func, p1, p2, NULL);
  272. }
  273. static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
  274. char *p3)
  275. {
  276. return w_app_lua_run(msg, func, p1, p2, p3);
  277. }
  278. int app_lua_load_param(modparam_t type, void *val)
  279. {
  280. if(val==NULL)
  281. return -1;
  282. return sr_lua_load_script((char*)val);
  283. }
  284. int app_lua_register_param(modparam_t type, void *val)
  285. {
  286. if(val==NULL)
  287. return -1;
  288. return sr_lua_register_module((char*)val);
  289. }
  290. static int fixup_lua_run(void** param, int param_no)
  291. {
  292. return fixup_spve_null(param, 1);
  293. }