Quellcode durchsuchen

app_lua: export functions to kemi framework

Daniel-Constantin Mierla vor 8 Jahren
Ursprung
Commit
f5e30811c2
1 geänderte Dateien mit 238 neuen und 42 gelöschten Zeilen
  1. 238 42
      src/modules/app_lua/app_lua_mod.c

+ 238 - 42
src/modules/app_lua/app_lua_mod.c

@@ -167,19 +167,6 @@ int sr_kemi_config_engine_lua(sip_msg_t *msg, int rtype, str *rname,
 	return 1;
 }
 
-/**
- *
- */
-int mod_register(char *path, int *dlflags, void *p1, void *p2)
-{
-	str ename = str_init("lua");
-
-	*dlflags = RTLD_NOW | RTLD_GLOBAL;
-
-	sr_kemi_eng_register(&ename, sr_kemi_config_engine_lua);
-
-	return 0;
-}
 
 /**
  * init module function
@@ -222,52 +209,92 @@ static void mod_destroy(void)
 
 static char _lua_buf_stack[4][512];
 
-static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
+/**
+ *
+ */
+static int ki_app_lua_dostring(sip_msg_t *msg, str *script)
 {
-	str s;
-	if(!lua_sr_initialized())
-	{
+	if(script==NULL || script->s==NULL || script->len>=511) {
+		LM_ERR("script too short or too long %d\n", script->len);
+		return -1;
+	}
+	if(!lua_sr_initialized()) {
 		LM_ERR("Lua env not intitialized");
 		return -1;
 	}
+	memcpy(_lua_buf_stack[0], script->s, script->len);
+	_lua_buf_stack[0][script->len] = '\0';
+	return app_lua_dostring(msg, _lua_buf_stack[0]);
+}
+
+/**
+ *
+ */
+static int w_app_lua_dostring(struct sip_msg *msg, char *script, char *extra)
+{
+	str s;
+
 	if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
 	{
 		LM_ERR("cannot get the script\n");
 		return -1;
 	}
-	if(s.len>=511)
-	{
-		LM_ERR("script too long %d\n", s.len);
+	return ki_app_lua_dostring(msg, &s);
+}
+
+/**
+ *
+ */
+static int ki_app_lua_dofile(sip_msg_t *msg, str *script)
+{
+	if(script==NULL || script->s==NULL || script->len>=511) {
+		LM_ERR("script too short or too long %d\n", script->len);
 		return -1;
 	}
-	memcpy(_lua_buf_stack[0], s.s, s.len);
-	_lua_buf_stack[0][s.len] = '\0';
-	return app_lua_dostring(msg, _lua_buf_stack[0]);
+	if(!lua_sr_initialized()) {
+		LM_ERR("Lua env not intitialized");
+		return -1;
+	}
+	memcpy(_lua_buf_stack[0], script->s, script->len);
+	_lua_buf_stack[0][script->len] = '\0';
+	return app_lua_dofile(msg, _lua_buf_stack[0]);
 }
 
+/**
+ *
+ */
 static int w_app_lua_dofile(struct sip_msg *msg, char *script, char *extra)
 {
 	str s;
-	if(!lua_sr_initialized())
-	{
-		LM_ERR("Lua env not intitialized");
+	if(fixup_get_svalue(msg, (gparam_p)script, &s)<0) {
+		LM_ERR("cannot get the script\n");
 		return -1;
 	}
-	if(fixup_get_svalue(msg, (gparam_p)script, &s)<0)
-	{
-		LM_ERR("cannot get the script\n");
+	return ki_app_lua_dofile(msg, &s);
+}
+
+/**
+ *
+ */
+static int ki_app_lua_runstring(sip_msg_t *msg, str *script)
+{
+	if(script==NULL || script->s==NULL || script->len>=511) {
+		LM_ERR("script too short or too long %d\n", script->len);
 		return -1;
 	}
-	if(s.len>=511)
+	if(!lua_sr_initialized())
 	{
-		LM_ERR("script too long %d\n", s.len);
+		LM_ERR("Lua env not intitialized");
 		return -1;
 	}
-	memcpy(_lua_buf_stack[0], s.s, s.len);
-	_lua_buf_stack[0][s.len] = '\0';
-	return app_lua_dofile(msg, _lua_buf_stack[0]);
+	memcpy(_lua_buf_stack[0], script->s, script->len);
+	_lua_buf_stack[0][script->len] = '\0';
+	return app_lua_runstring(msg, _lua_buf_stack[0]);
 }
 
+/**
+ *
+ */
 static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
 {
 	str s;
@@ -281,16 +308,12 @@ static int w_app_lua_runstring(struct sip_msg *msg, char *script, char *extra)
 		LM_ERR("cannot get the script\n");
 		return -1;
 	}
-	if(s.len>=511)
-	{
-		LM_ERR("script too long %d\n", s.len);
-		return -1;
-	}
-	memcpy(_lua_buf_stack[0], s.s, s.len);
-	_lua_buf_stack[0][s.len] = '\0';
-	return app_lua_runstring(msg, _lua_buf_stack[0]);
+	return ki_app_lua_runstring(msg, &s);
 }
 
+/**
+ *
+ */
 static int w_app_lua_run(struct sip_msg *msg, char *func, char *p1, char *p2,
 		char *p3)
 {
@@ -396,6 +419,119 @@ static int w_app_lua_run3(struct sip_msg *msg, char *func, char *p1, char *p2,
 	return w_app_lua_run(msg, func, p1, p2, p3);
 }
 
+/**
+ *
+ */
+static int ki_app_lua_run(sip_msg_t *msg, str *func)
+{
+	if(func==NULL || func->s==NULL || func->len<0) {
+		LM_ERR("invalid function name\n");
+		return -1;
+	}
+	if(func->s[func->len]!='\0') {
+		LM_ERR("invalid terminated function name\n");
+		return -1;
+	}
+	return app_lua_run(msg, func->s, NULL, NULL, NULL);
+
+}
+
+/**
+ *
+ */
+static int ki_app_lua_run_p1(sip_msg_t *msg, str *func, str *p1)
+{
+	if(func==NULL || func->s==NULL || func->len<=0) {
+		LM_ERR("invalid function name\n");
+		return -1;
+	}
+	if(func->s[func->len]!='\0') {
+		LM_ERR("invalid terminated function name\n");
+		return -1;
+	}
+	if(p1==NULL || p1->s==NULL || p1->len<0) {
+		LM_ERR("invalid p1 value\n");
+		return -1;
+	}
+	if(p1->s[p1->len]!='\0') {
+		LM_ERR("invalid terminated p1 value\n");
+		return -1;
+	}
+	return app_lua_run(msg, func->s, p1->s, NULL, NULL);
+}
+
+/**
+ *
+ */
+static int ki_app_lua_run_p2(sip_msg_t *msg, str *func, str *p1, str *p2)
+{
+	if(func==NULL || func->s==NULL || func->len<=0) {
+		LM_ERR("invalid function name\n");
+		return -1;
+	}
+	if(func->s[func->len]!='\0') {
+		LM_ERR("invalid terminated function name\n");
+		return -1;
+	}
+	if(p1==NULL || p1->s==NULL || p1->len<0) {
+		LM_ERR("invalid p1 value\n");
+		return -1;
+	}
+	if(p1->s[p1->len]!='\0') {
+		LM_ERR("invalid terminated p1 value\n");
+		return -1;
+	}
+	if(p2==NULL || p2->s==NULL || p2->len<0) {
+		LM_ERR("invalid p2 value\n");
+		return -1;
+	}
+	if(p2->s[p2->len]!='\0') {
+		LM_ERR("invalid terminated p2 value\n");
+		return -1;
+	}
+	return app_lua_run(msg, func->s, p1->s, p2->s, NULL);
+}
+
+/**
+ *
+ */
+static int ki_app_lua_run_p3(sip_msg_t *msg, str *func, str *p1, str *p2, str *p3)
+{
+	if(func==NULL || func->s==NULL || func->len<=0) {
+		LM_ERR("invalid function name\n");
+		return -1;
+	}
+	if(func->s[func->len]!='\0') {
+		LM_ERR("invalid terminated function name\n");
+		return -1;
+	}
+	if(p1==NULL || p1->s==NULL || p1->len<0) {
+		LM_ERR("invalid p1 value\n");
+		return -1;
+	}
+	if(p1->s[p1->len]!='\0') {
+		LM_ERR("invalid terminated p1 value\n");
+		return -1;
+	}
+	if(p2==NULL || p2->s==NULL || p2->len<0) {
+		LM_ERR("invalid p2 value\n");
+		return -1;
+	}
+	if(p2->s[p2->len]!='\0') {
+		LM_ERR("invalid terminated p2 value\n");
+		return -1;
+	}
+	if(p3==NULL || p3->s==NULL || p3->len<0) {
+		LM_ERR("invalid p3 value\n");
+		return -1;
+	}
+	if(p3->s[p3->len]!='\0') {
+		LM_ERR("invalid terminated p3 value\n");
+		return -1;
+	}
+	return app_lua_run(msg, func->s, p1->s, p2->s, p3->s);
+}
+
 int app_lua_load_param(modparam_t type, void *val)
 {
 	if(val==NULL)
@@ -543,3 +679,63 @@ static int app_lua_init_rpc(void)
 	}
 	return 0;
 }
+
+/**
+ *
+ */
+/* clang-format off */
+static sr_kemi_t sr_kemi_app_lua_exports[] = {
+	{ str_init("app_lua"), str_init("dostring"),
+		SR_KEMIP_INT, ki_app_lua_dostring,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("dofile"),
+		SR_KEMIP_INT, ki_app_lua_dofile,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("runstring"),
+		SR_KEMIP_INT, ki_app_lua_runstring,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("run"),
+		SR_KEMIP_INT, ki_app_lua_run,
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("run_p1"),
+		SR_KEMIP_INT, ki_app_lua_run_p1,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_NONE,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("run_p2"),
+		SR_KEMIP_INT, ki_app_lua_run_p2,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+	{ str_init("app_lua"), str_init("run_p3"),
+		SR_KEMIP_INT, ki_app_lua_run_p3,
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
+	},
+
+	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
+};
+/* clang-format on */
+
+/**
+ *
+ */
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+	str ename = str_init("lua");
+
+	*dlflags = RTLD_NOW | RTLD_GLOBAL;
+
+	sr_kemi_eng_register(&ename, sr_kemi_config_engine_lua);
+	sr_kemi_modules_add(sr_kemi_app_lua_exports);
+
+	return 0;
+}