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_lua_api.h"
00034
00035 MODULE_VERSION
00036
00039
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_lua_dostring(struct sip_msg *msg, char *script, char *extra);
00045 static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra);
00046 static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra);
00047 static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
00048 char *p3);
00049 static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
00050 char *p3);
00051 static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
00052 char *p3);
00053 static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
00054 char *p3);
00055 static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
00056 char *p3);
00057
00058 static int fixup_lua_run(void** param, int param_no);
00059
00060 int app_lua_load_param(modparam_t type, void *val);
00061 int app_lua_register_param(modparam_t type, void *val);
00062
00063 static param_export_t params[]={
00064 {"load", STR_PARAM|USE_FUNC_PARAM, (void*)app_lua_load_param},
00065 {"register", STR_PARAM|USE_FUNC_PARAM, (void*)app_lua_register_param},
00066 {0, 0, 0}
00067 };
00068
00069 static cmd_export_t cmds[]={
00070 {"lua_dostring", (cmd_function)w_app_lua_dostring, 1, fixup_spve_null,
00071 0, ANY_ROUTE},
00072 {"lua_dofile", (cmd_function)w_app_lua_dofile, 1, fixup_spve_null,
00073 0, ANY_ROUTE},
00074 {"lua_runstring", (cmd_function)w_app_lua_runstring, 1, fixup_spve_null,
00075 0, ANY_ROUTE},
00076 {"lua_run", (cmd_function)w_app_lua_run0, 1, fixup_lua_run,
00077 0, ANY_ROUTE},
00078 {"lua_run", (cmd_function)w_app_lua_run1, 2, fixup_lua_run,
00079 0, ANY_ROUTE},
00080 {"lua_run", (cmd_function)w_app_lua_run2, 3, fixup_lua_run,
00081 0, ANY_ROUTE},
00082 {"lua_run", (cmd_function)w_app_lua_run3, 4, fixup_lua_run,
00083 0, ANY_ROUTE},
00084 {0, 0, 0, 0, 0, 0}
00085 };
00086
00087 struct module_exports exports = {
00088 "app_lua",
00089 RTLD_NOW | RTLD_GLOBAL,
00090 cmds,
00091 params,
00092 0,
00093 0,
00094 0,
00095 0,
00096 mod_init,
00097 0,
00098 mod_destroy,
00099 child_init
00100 };
00101
00102
00103
00107 static int mod_init(void)
00108 {
00109 if(lua_sr_init_mod()<0)
00110 return -1;
00111 return 0;
00112 }
00113
00114
00115
00116 static int child_init(int rank)
00117 {
00118 if(rank==PROC_MAIN || rank==PROC_TCP_MAIN)
00119 return 0;
00120
00121 if (rank==PROC_INIT)
00122 {
00123
00124 if(lua_sr_init_probe()!=0)
00125 return -1;
00126 return 0;
00127 }
00128 return lua_sr_init_child();
00129 }
00130
00131
00132 static void mod_destroy(void)
00133 {
00134 lua_sr_destroy();
00135 }
00136
00137 static char _lua_buf_stack[4][512];
00138
00139 static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
00140 {
00141 str s;
00142 if(!lua_sr_initialized())
00143 {
00144 LM_ERR("Lua env not intitialized");
00145 return -1;
00146 }
00147 if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
00148 {
00149 LM_ERR("cannot get the script\n");
00150 return -1;
00151 }
00152 if(s.len>=511)
00153 {
00154 LM_ERR("script too long %d\n", s.len);
00155 return -1;
00156 }
00157 memcpy(_lua_buf_stack[0], s.s, s.len);
00158 _lua_buf_stack[0][s.len] = '\0';
00159 return app_lua_dostring(msg, _lua_buf_stack[0]);
00160 }
00161
00162 static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra)
00163 {
00164 str s;
00165 if(!lua_sr_initialized())
00166 {
00167 LM_ERR("Lua env not intitialized");
00168 return -1;
00169 }
00170 if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
00171 {
00172 LM_ERR("cannot get the script\n");
00173 return -1;
00174 }
00175 if(s.len>=511)
00176 {
00177 LM_ERR("script too long %d\n", s.len);
00178 return -1;
00179 }
00180 memcpy(_lua_buf_stack[0], s.s, s.len);
00181 _lua_buf_stack[0][s.len] = '\0';
00182 return app_lua_dofile(msg, _lua_buf_stack[0]);
00183 }
00184
00185 static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
00186 {
00187 str s;
00188 if(!lua_sr_initialized())
00189 {
00190 LM_ERR("Lua env not intitialized");
00191 return -1;
00192 }
00193 if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
00194 {
00195 LM_ERR("cannot get the script\n");
00196 return -1;
00197 }
00198 if(s.len>=511)
00199 {
00200 LM_ERR("script too long %d\n", s.len);
00201 return -1;
00202 }
00203 memcpy(_lua_buf_stack[0], s.s, s.len);
00204 _lua_buf_stack[0][s.len] = '\0';
00205 return app_lua_runstring(msg, _lua_buf_stack[0]);
00206 }
00207
00208 static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
00209 char *p3)
00210 {
00211 str s;
00212 if(!lua_sr_initialized())
00213 {
00214 LM_ERR("Lua env not intitialized");
00215 return -1;
00216 }
00217 if(fixup_get_svalue(msg, (gparam_p)func, &s)<0)
00218 {
00219 LM_ERR("cannot get the function\n");
00220 return -1;
00221 }
00222 if(s.len>=511)
00223 {
00224 LM_ERR("function too long %d\n", s.len);
00225 return -1;
00226 }
00227 memcpy(_lua_buf_stack[0], s.s, s.len);
00228 _lua_buf_stack[0][s.len] = '\0';
00229
00230 if(p1!=NULL)
00231 {
00232 if(fixup_get_svalue(msg, (gparam_p)p1, &s)<0)
00233 {
00234 LM_ERR("cannot get p1\n");
00235 return -1;
00236 }
00237 if(s.len>=511)
00238 {
00239 LM_ERR("p1 too long %d\n", s.len);
00240 return -1;
00241 }
00242 memcpy(_lua_buf_stack[1], s.s, s.len);
00243 _lua_buf_stack[1][s.len] = '\0';
00244
00245 if(p2!=NULL)
00246 {
00247 if(fixup_get_svalue(msg, (gparam_p)p2, &s)<0)
00248 {
00249 LM_ERR("cannot get p2\n");
00250 return -1;
00251 }
00252 if(s.len>=511)
00253 {
00254 LM_ERR("p2 too long %d\n", s.len);
00255 return -1;
00256 }
00257 memcpy(_lua_buf_stack[2], s.s, s.len);
00258 _lua_buf_stack[2][s.len] = '\0';
00259
00260 if(p3!=NULL)
00261 {
00262 if(fixup_get_svalue(msg, (gparam_p)p3, &s)<0)
00263 {
00264 LM_ERR("cannot get p3\n");
00265 return -1;
00266 }
00267 if(s.len>=511)
00268 {
00269 LM_ERR("p3 too long %d\n", s.len);
00270 return -1;
00271 }
00272 memcpy(_lua_buf_stack[3], s.s, s.len);
00273 _lua_buf_stack[3][s.len] = '\0';
00274 }
00275 } else {
00276 p3 = NULL;
00277 }
00278 } else {
00279 p2 = NULL;
00280 p3 = NULL;
00281 }
00282
00283 return app_lua_run(msg, _lua_buf_stack[0],
00284 (p1!=NULL)?_lua_buf_stack[1]:NULL,
00285 (p2!=NULL)?_lua_buf_stack[2]:NULL,
00286 (p3!=NULL)?_lua_buf_stack[3]:NULL);
00287 }
00288
00289 static int w_app_lua_run0(struct sip_msg *msg, char *func, char *p1, char *p2,
00290 char *p3)
00291 {
00292 return w_app_lua_run(msg, func, NULL, NULL, NULL);
00293 }
00294
00295 static int w_app_lua_run1(struct sip_msg *msg, char *func, char *p1, char *p2,
00296 char *p3)
00297 {
00298 return w_app_lua_run(msg, func, p1, NULL, NULL);
00299 }
00300
00301 static int w_app_lua_run2(struct sip_msg *msg, char *func, char *p1, char *p2,
00302 char *p3)
00303 {
00304 return w_app_lua_run(msg, func, p1, p2, NULL);
00305 }
00306
00307 static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
00308 char *p3)
00309 {
00310 return w_app_lua_run(msg, func, p1, p2, p3);
00311 }
00312
00313 int app_lua_load_param(modparam_t type, void *val)
00314 {
00315 if(val==NULL)
00316 return -1;
00317 return sr_lua_load_script((char*)val);
00318 }
00319
00320 int app_lua_register_param(modparam_t type, void *val)
00321 {
00322 if(val==NULL)
00323 return -1;
00324 return sr_lua_register_module((char*)val);
00325 }
00326
00327 static int fixup_lua_run(void** param, int param_no)
00328 {
00329 return fixup_spve_null(param, 1);
00330 }
00331