Browse Source

app_lua: backup and restore the sip msg structure in lua env

- nested execution of callbacks can happen (e.g., branch route callback
  executed inside the request_route callback)
Daniel-Constantin Mierla 9 năm trước cách đây
mục cha
commit
187a296010
1 tập tin đã thay đổi với 15 bổ sung7 xóa
  1. 15 7
      modules/app_lua/app_lua_api.c

+ 15 - 7
modules/app_lua/app_lua_api.c

@@ -549,13 +549,15 @@ int app_lua_return_true(lua_State *L)
 /**
 /**
  *
  *
  */
  */
-int app_lua_dostring(struct sip_msg *msg, char *script)
+int app_lua_dostring(sip_msg_t *msg, char *script)
 {
 {
 	int ret;
 	int ret;
 	char *txt;
 	char *txt;
+	sip_msg_t *bmsg;
 
 
 	LM_DBG("executing Lua string: [[%s]]\n", script);
 	LM_DBG("executing Lua string: [[%s]]\n", script);
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
+	bmsg = _sr_L_env.msg;
 	_sr_L_env.msg = msg;
 	_sr_L_env.msg = msg;
 	ret = luaL_dostring(_sr_L_env.L, script);
 	ret = luaL_dostring(_sr_L_env.L, script);
 	if(ret!=0)
 	if(ret!=0)
@@ -564,20 +566,22 @@ int app_lua_dostring(struct sip_msg *msg, char *script)
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		lua_pop (_sr_L_env.L, 1);
 		lua_pop (_sr_L_env.L, 1);
 	}
 	}
-	_sr_L_env.msg = 0;
+	_sr_L_env.msg = bmsg;
 	return (ret==0)?1:-1;
 	return (ret==0)?1:-1;
 }
 }
 
 
 /**
 /**
  *
  *
  */
  */
-int app_lua_dofile(struct sip_msg *msg, char *script)
+int app_lua_dofile(sip_msg_t *msg, char *script)
 {
 {
 	int ret;
 	int ret;
 	char *txt;
 	char *txt;
+	sip_msg_t *bmsg;
 
 
 	LM_DBG("executing Lua file: [[%s]]\n", script);
 	LM_DBG("executing Lua file: [[%s]]\n", script);
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.L));
+	bmsg = _sr_L_env.msg;
 	_sr_L_env.msg = msg;
 	_sr_L_env.msg = msg;
 	ret = luaL_dofile(_sr_L_env.L, script);
 	ret = luaL_dofile(_sr_L_env.L, script);
 	if(ret!=0)
 	if(ret!=0)
@@ -586,17 +590,18 @@ int app_lua_dofile(struct sip_msg *msg, char *script)
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		lua_pop(_sr_L_env.L, 1);
 		lua_pop(_sr_L_env.L, 1);
 	}
 	}
-	_sr_L_env.msg = 0;
+	_sr_L_env.msg = bmsg;
 	return (ret==0)?1:-1;
 	return (ret==0)?1:-1;
 }
 }
 
 
 /**
 /**
  *
  *
  */
  */
-int app_lua_runstring(struct sip_msg *msg, char *script)
+int app_lua_runstring(sip_msg_t *msg, char *script)
 {
 {
 	int ret;
 	int ret;
 	char *txt;
 	char *txt;
+	sip_msg_t *bmsg;
 
 
 	if(_sr_L_env.LL==NULL)
 	if(_sr_L_env.LL==NULL)
 	{
 	{
@@ -606,6 +611,7 @@ int app_lua_runstring(struct sip_msg *msg, char *script)
 
 
 	LM_DBG("running Lua string: [[%s]]\n", script);
 	LM_DBG("running Lua string: [[%s]]\n", script);
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
 	LM_DBG("lua top index is: %d\n", lua_gettop(_sr_L_env.LL));
+	bmsg = _sr_L_env.msg;
 	_sr_L_env.msg = msg;
 	_sr_L_env.msg = msg;
 	ret = luaL_dostring(_sr_L_env.LL, script);
 	ret = luaL_dostring(_sr_L_env.LL, script);
 	if(ret!=0)
 	if(ret!=0)
@@ -614,7 +620,7 @@ int app_lua_runstring(struct sip_msg *msg, char *script)
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		LM_ERR("error from Lua: %s\n", (txt)?txt:"unknown");
 		lua_pop (_sr_L_env.LL, 1);
 		lua_pop (_sr_L_env.LL, 1);
 	}
 	}
-	_sr_L_env.msg = 0;
+	_sr_L_env.msg = bmsg;
 	return (ret==0)?1:-1;
 	return (ret==0)?1:-1;
 }
 }
 
 
@@ -627,6 +633,7 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
 	int n;
 	int n;
 	int ret;
 	int ret;
 	char *txt;
 	char *txt;
+	sip_msg_t *bmsg;
 
 
 	if(_sr_L_env.LL==NULL)
 	if(_sr_L_env.LL==NULL)
 	{
 	{
@@ -676,9 +683,10 @@ int app_lua_run_ex(sip_msg_t *msg, char *func, char *p1, char *p2,
 			}
 			}
 		}
 		}
 	}
 	}
+	bmsg = _sr_L_env.msg;
 	_sr_L_env.msg = msg;
 	_sr_L_env.msg = msg;
 	ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
 	ret = lua_pcall(_sr_L_env.LL, n, 0, 0);
-	_sr_L_env.msg = 0;
+	_sr_L_env.msg = bmsg;
 	if(ret!=0)
 	if(ret!=0)
 	{
 	{
 		LM_ERR("error executing: %s (err: %d)\n", func, ret);
 		LM_ERR("error executing: %s (err: %d)\n", func, ret);