app_mono_mod.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2011 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 "../../sr_module.h"
  27. #include "../../dprint.h"
  28. #include "../../ut.h"
  29. #include "../../mod_fix.h"
  30. #include "app_mono_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_mono_exec(struct sip_msg *msg, char *script, char *mparam);
  38. static int w_app_mono_run(struct sip_msg *msg, char *mparam, char *extra);
  39. static int fixup_mono_exec(void** param, int param_no);
  40. int app_mono_load_param(modparam_t type, void *val);
  41. int app_mono_register_param(modparam_t type, void *val);
  42. static param_export_t params[]={
  43. {"load", PARAM_STRING|USE_FUNC_PARAM, (void*)app_mono_load_param},
  44. {"register", PARAM_STRING|USE_FUNC_PARAM, (void*)app_mono_register_param},
  45. {0, 0, 0}
  46. };
  47. static cmd_export_t cmds[]={
  48. {"mono_exec", (cmd_function)w_app_mono_exec, 1, fixup_mono_exec,
  49. 0, ANY_ROUTE},
  50. {"mono_exec", (cmd_function)w_app_mono_exec, 2, fixup_mono_exec,
  51. 0, ANY_ROUTE},
  52. {"mono_run", (cmd_function)w_app_mono_run, 0, 0,
  53. 0, ANY_ROUTE},
  54. {"mono_run", (cmd_function)w_app_mono_run, 1, fixup_spve_null,
  55. 0, ANY_ROUTE},
  56. {0, 0, 0, 0, 0, 0}
  57. };
  58. struct module_exports exports = {
  59. "app_mono",
  60. RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
  61. cmds,
  62. params,
  63. 0,
  64. 0, /* exported MI functions */
  65. 0, /* exported pseudo-variables */
  66. 0, /* extra processes */
  67. mod_init, /* module initialization function */
  68. 0, /* response function */
  69. mod_destroy, /* destroy function */
  70. child_init /* per child init function */
  71. };
  72. /**
  73. * init module function
  74. */
  75. static int mod_init(void)
  76. {
  77. mono_sr_init_mod();
  78. return 0;
  79. }
  80. /* each child get a new connection to the database */
  81. static int child_init(int rank)
  82. {
  83. if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
  84. return 0; /* do nothing for the main process */
  85. if (rank==PROC_INIT)
  86. {
  87. /* do a probe before forking */
  88. if(mono_sr_init_probe()!=0)
  89. return -1;
  90. return 0;
  91. }
  92. if(mono_sr_init_child()<0)
  93. return -1;
  94. if(mono_sr_init_load()<0)
  95. return -1;
  96. return 0;
  97. }
  98. static void mod_destroy(void)
  99. {
  100. mono_sr_destroy();
  101. }
  102. char _mono_buf_stack[2][512];
  103. /**
  104. *
  105. */
  106. static int w_app_mono_exec(struct sip_msg *msg, char *script, char *mparam)
  107. {
  108. str s;
  109. str p;
  110. if(!mono_sr_initialized())
  111. {
  112. LM_ERR("Lua env not intitialized");
  113. return -1;
  114. }
  115. if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
  116. {
  117. LM_ERR("cannot get the script\n");
  118. return -1;
  119. }
  120. if(s.len>=511)
  121. {
  122. LM_ERR("script too long %d\n", s.len);
  123. return -1;
  124. }
  125. if(mparam!=NULL)
  126. {
  127. if(fixup_get_svalue(msg, (gparam_p)mparam, &p)<0)
  128. {
  129. LM_ERR("cannot get the parameter\n");
  130. return -1;
  131. }
  132. if(p.len>=511)
  133. {
  134. LM_ERR("parameter value too long %d\n", p.len);
  135. return -1;
  136. }
  137. memcpy(_mono_buf_stack[1], p.s, p.len);
  138. _mono_buf_stack[1][p.len] = '\0';
  139. }
  140. memcpy(_mono_buf_stack[0], s.s, s.len);
  141. _mono_buf_stack[0][s.len] = '\0';
  142. return app_mono_exec(msg, _mono_buf_stack[0],
  143. (mparam)?_mono_buf_stack[1]:NULL);
  144. }
  145. /**
  146. *
  147. */
  148. static int w_app_mono_run(struct sip_msg *msg, char *mparam, char *extra)
  149. {
  150. str p;
  151. if(!mono_sr_initialized())
  152. {
  153. LM_ERR("Lua env not intitialized");
  154. return -1;
  155. }
  156. if(mparam!=NULL)
  157. {
  158. if(fixup_get_svalue(msg, (gparam_p)mparam, &p)<0)
  159. {
  160. LM_ERR("cannot get the parameter\n");
  161. return -1;
  162. }
  163. if(p.len>=511)
  164. {
  165. LM_ERR("parameter value too long %d\n", p.len);
  166. return -1;
  167. }
  168. memcpy(_mono_buf_stack[0], p.s, p.len);
  169. _mono_buf_stack[0][p.len] = '\0';
  170. }
  171. return app_mono_run(msg, (mparam)?_mono_buf_stack[0]:NULL);
  172. }
  173. int app_mono_load_param(modparam_t type, void *val)
  174. {
  175. if(val==NULL)
  176. return -1;
  177. return sr_mono_load_script((char*)val);
  178. }
  179. int app_mono_register_param(modparam_t type, void *val)
  180. {
  181. if(val==NULL)
  182. return -1;
  183. return sr_mono_register_module((char*)val);
  184. }
  185. static int fixup_mono_exec(void** param, int param_no)
  186. {
  187. if(sr_mono_assembly_loaded())
  188. {
  189. LM_ERR("cannot use lua_exec(...) when an assembly is loaded\n");
  190. return -1;
  191. }
  192. return fixup_spve_null(param, 1);
  193. }