app_mono_mod.c

00001 
00024 #include <stdio.h>
00025 #include <unistd.h>
00026 #include <stdlib.h>
00027 
00028 #include "../../sr_module.h"
00029 #include "../../dprint.h"
00030 #include "../../ut.h"
00031 #include "../../mod_fix.h"
00032 
00033 #include "app_mono_api.h"
00034 
00035 MODULE_VERSION
00036 
00039 /* List of allowed chars for a prefix*/
00040 static int  mod_init(void);
00041 static void mod_destroy(void);
00042 static int  child_init(int rank);
00043 
00044 static int w_app_mono_exec(struct sip_msg *msg, char *script, char *mparam);
00045 static int w_app_mono_run(struct sip_msg *msg, char *mparam, char *extra);
00046 
00047 static int fixup_mono_exec(void** param, int param_no);
00048 
00049 int app_mono_load_param(modparam_t type, void *val);
00050 int app_mono_register_param(modparam_t type, void *val);
00051 
00052 static param_export_t params[]={
00053         {"load",     STR_PARAM|USE_FUNC_PARAM, (void*)app_mono_load_param},
00054         {"register", STR_PARAM|USE_FUNC_PARAM, (void*)app_mono_register_param},
00055         {0, 0, 0}
00056 };
00057 
00058 static cmd_export_t cmds[]={
00059         {"mono_exec", (cmd_function)w_app_mono_exec, 1, fixup_mono_exec,
00060                 0, ANY_ROUTE},
00061         {"mono_exec", (cmd_function)w_app_mono_exec, 2, fixup_mono_exec,
00062                 0, ANY_ROUTE},
00063         {"mono_run",  (cmd_function)w_app_mono_run,  0, 0,
00064                 0, ANY_ROUTE},
00065         {"mono_run", (cmd_function)w_app_mono_run,   1, fixup_spve_null,
00066                 0, ANY_ROUTE},
00067         {0, 0, 0, 0, 0, 0}
00068 };
00069 
00070 struct module_exports exports = {
00071         "app_mono",
00072         RTLD_NOW | RTLD_GLOBAL, /* dlopen flags */
00073         cmds,
00074         params,
00075         0,
00076         0,              /* exported MI functions */
00077         0,              /* exported pseudo-variables */
00078         0,              /* extra processes */
00079         mod_init,       /* module initialization function */
00080         0,              /* response function */
00081         mod_destroy,    /* destroy function */
00082         child_init      /* per child init function */
00083 };
00084 
00085 
00086 
00090 static int mod_init(void)
00091 {
00092         mono_sr_init_mod();
00093         return 0;
00094 }
00095 
00096 
00097 /* each child get a new connection to the database */
00098 static int child_init(int rank)
00099 {
00100         if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
00101                 return 0; /* do nothing for the main process */
00102 
00103         if (rank==PROC_INIT)
00104         {
00105                 /* do a probe before forking */
00106                 if(mono_sr_init_probe()!=0)
00107                         return -1;
00108                 return 0;
00109         }
00110         if(mono_sr_init_child()<0)
00111                 return -1;
00112         if(mono_sr_init_load()<0)
00113                 return -1;
00114         return 0;
00115 }
00116 
00117 
00118 static void mod_destroy(void)
00119 {
00120         mono_sr_destroy();
00121 }
00122 
00123 char _mono_buf_stack[2][512];
00124 
00128 static int w_app_mono_exec(struct sip_msg *msg, char *script, char *mparam)
00129 {
00130         str s;
00131         str p;
00132 
00133         if(!mono_sr_initialized())
00134         {
00135                 LM_ERR("Lua env not intitialized");
00136                 return -1;
00137         }
00138         if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
00139         {
00140                 LM_ERR("cannot get the script\n");
00141                 return -1;
00142         }
00143         if(s.len>=511)
00144         {
00145                 LM_ERR("script too long %d\n", s.len);
00146                 return -1;
00147         }
00148         if(mparam!=NULL)
00149         {
00150                 if(fixup_get_svalue(msg, (gparam_p)mparam, &p)<0)
00151                 {
00152                         LM_ERR("cannot get the parameter\n");
00153                         return -1;
00154                 }
00155                 if(p.len>=511)
00156                 {
00157                         LM_ERR("parameter value too long %d\n", p.len);
00158                         return -1;
00159                 }
00160                 memcpy(_mono_buf_stack[1], p.s, p.len);
00161                 _mono_buf_stack[1][p.len] = '\0';
00162         }
00163         memcpy(_mono_buf_stack[0], s.s, s.len);
00164         _mono_buf_stack[0][s.len] = '\0';
00165         return app_mono_exec(msg, _mono_buf_stack[0],
00166                         (mparam)?_mono_buf_stack[1]:NULL);
00167 }
00168 
00172 static int w_app_mono_run(struct sip_msg *msg, char *mparam, char *extra)
00173 {
00174         str p;
00175 
00176         if(!mono_sr_initialized())
00177         {
00178                 LM_ERR("Lua env not intitialized");
00179                 return -1;
00180         }
00181         if(mparam!=NULL)
00182         {
00183                 if(fixup_get_svalue(msg, (gparam_p)mparam, &p)<0)
00184                 {
00185                         LM_ERR("cannot get the parameter\n");
00186                         return -1;
00187                 }
00188                 if(p.len>=511)
00189                 {
00190                         LM_ERR("parameter value too long %d\n", p.len);
00191                         return -1;
00192                 }
00193                 memcpy(_mono_buf_stack[0], p.s, p.len);
00194                 _mono_buf_stack[0][p.len] = '\0';
00195         }
00196         return app_mono_run(msg, (mparam)?_mono_buf_stack[0]:NULL);
00197 }
00198 
00199 
00200 int app_mono_load_param(modparam_t type, void *val)
00201 {
00202         if(val==NULL)
00203                 return -1;
00204         return sr_mono_load_script((char*)val);
00205 }
00206 
00207 int app_mono_register_param(modparam_t type, void *val)
00208 {
00209         if(val==NULL)
00210                 return -1;
00211         return sr_mono_register_module((char*)val);
00212 }
00213 
00214 static int fixup_mono_exec(void** param, int param_no)
00215 {
00216         if(sr_mono_assembly_loaded())
00217         {
00218                 LM_ERR("cannot use lua_exec(...) when an assembly is loaded\n");
00219                 return -1;
00220         }
00221         return fixup_spve_null(param, 1);
00222 }
00223