diff --git a/nfq2/desync.c b/nfq2/desync.c index ddaf856..0ac5b78 100644 --- a/nfq2/desync.c +++ b/nfq2/desync.c @@ -781,19 +781,24 @@ static uint8_t desync( if (LIST_FIRST(&dp->lua_desync)) { - params.desync_ctx->dp = dp; - params.desync_ctx->ctrack = ctrack; - params.desync_ctx->dis = dis; - params.desync_ctx->cancel = false; - params.desync_ctx->incoming = bIncoming; + lua_rawgeti(params.L, LUA_REGISTRYINDEX, params.ref_desync_ctx); + t_lua_desync_context *ctx = (t_lua_desync_context *)luaL_checkudata(params.L, 1, "desync_ctx"); + // this is singleton stored in the registry. safe to pop + lua_pop(params.L,1); + + ctx->dp = dp; + ctx->ctrack = ctrack; + ctx->dis = dis; + ctx->cancel = false; + ctx->incoming = bIncoming; b_cutoff_all = b_unwanted_payload = true; - params.desync_ctx->func_n = 1; + ctx->func_n = 1; LIST_FOREACH(func, &dp->lua_desync, next) { - params.desync_ctx->func = func->func; - desync_instance(func->func, dp->n, params.desync_ctx->func_n, instance, sizeof(instance)); - params.desync_ctx->instance = instance; + ctx->func = func->func; + desync_instance(func->func, dp->n, ctx->func_n, instance, sizeof(instance)); + ctx->instance = instance; range = bIncoming ? &func->range_in : &func->range_out; if (b_unwanted_payload) @@ -801,7 +806,7 @@ static uint8_t desync( if (b_cutoff_all) { - if (lua_instance_cutoff_check(params.L, params.desync_ctx, bIncoming)) + if (lua_instance_cutoff_check(params.L, ctx, bIncoming)) DLOG("* lua '%s' : voluntary cutoff\n", instance); else if (check_pos_cutoff(pos, range)) { @@ -819,7 +824,7 @@ static uint8_t desync( else b_cutoff_all = false; } - params.desync_ctx->func_n++; + ctx->func_n++; } if (b_cutoff_all) { @@ -872,14 +877,14 @@ static uint8_t desync( } ref_arg = luaL_ref(params.L, LUA_REGISTRYINDEX); - params.desync_ctx->func_n = 1; + ctx->func_n = 1; LIST_FOREACH(func, &dp->lua_desync, next) { - params.desync_ctx->func = func->func; - desync_instance(func->func, dp->n, params.desync_ctx->func_n, instance, sizeof(instance)); - params.desync_ctx->instance = instance; + ctx->func = func->func; + desync_instance(func->func, dp->n, ctx->func_n, instance, sizeof(instance)); + ctx->instance = instance; - if (!lua_instance_cutoff_check(params.L, params.desync_ctx, bIncoming)) + if (!lua_instance_cutoff_check(params.L, ctx, bIncoming)) { range = bIncoming ? &func->range_in : &func->range_out; if (check_pos_range(pos, range)) @@ -906,12 +911,12 @@ static uint8_t desync( lua_rawgeti(params.L, LUA_REGISTRYINDEX, ref_arg); lua_pushf_args(params.L, &func->args, -1, true); lua_pushf_str(params.L, "func", func->func); - lua_pushf_int(params.L, "func_n", params.desync_ctx->func_n); + lua_pushf_int(params.L, "func_n", ctx->func_n); lua_pushf_str(params.L, "func_instance", instance); // prevent use of desync ctx object outside of function call - params.desync_ctx->valid = true; + ctx->valid = true; status = lua_pcall(params.L, 2, LUA_MULTRET, 0); - params.desync_ctx->valid = false; + ctx->valid = false; if (status) { @@ -941,8 +946,8 @@ static uint8_t desync( range->upper_cutoff ? '<' : '-', range->to.mode, range->to.pos); } - if (params.desync_ctx->cancel) break; - params.desync_ctx->func_n++; + if (ctx->cancel) break; + ctx->func_n++; } } diff --git a/nfq2/lua.c b/nfq2/lua.c index e3dd96e..dfe72bd 100644 --- a/nfq2/lua.c +++ b/nfq2/lua.c @@ -787,8 +787,8 @@ static void lua_desync_ctx_create(lua_State *L) { LUA_STACK_GUARD_ENTER(L) - params.desync_ctx = (t_lua_desync_context *)lua_newuserdata(L, sizeof(t_lua_desync_context)); - memset(params.desync_ctx, 0, sizeof(t_lua_desync_context)); + t_lua_desync_context *ctx = (t_lua_desync_context *)lua_newuserdata(L, sizeof(t_lua_desync_context)); + memset(ctx, 0, sizeof(*ctx)); luaL_getmetatable(L, "desync_ctx"); lua_setmetatable(L, -2); params.ref_desync_ctx = luaL_ref(params.L, LUA_REGISTRYINDEX); @@ -802,7 +802,6 @@ static void lua_desync_ctx_destroy(lua_State *L) { luaL_unref(L, LUA_REGISTRYINDEX, params.ref_desync_ctx); params.ref_desync_ctx = 0; - params.desync_ctx = NULL; } } diff --git a/nfq2/lua.h b/nfq2/lua.h index a1f76b7..c6f2e50 100644 --- a/nfq2/lua.h +++ b/nfq2/lua.h @@ -14,7 +14,6 @@ #include "pools.h" #include "conntrack.h" #include "darkmagic.h" -#include "params.h" #if LUA_VERSION_NUM < 503 #define lua_isinteger lua_isnumber @@ -102,4 +101,14 @@ bool lua_reconstruct_tcphdr(lua_State *L, int idx, struct tcphdr *tcp, size_t *l bool lua_reconstruct_udphdr(lua_State *L, int idx, struct udphdr *udp); bool lua_reconstruct_dissect(lua_State *L, int idx, uint8_t *buf, size_t *len, bool badsum, bool ip6_preserve_next); +typedef struct { + unsigned int func_n; + const char *func, *instance; + const struct desync_profile *dp; + const struct dissect *dis; + t_ctrack *ctrack; + bool incoming, cancel; + bool valid; +} t_lua_desync_context; + bool lua_instance_cutoff_check(lua_State *L, const t_lua_desync_context *ctx, bool bIn); diff --git a/nfq2/params.c b/nfq2/params.c index 1dedd8c..dbf2f72 100644 --- a/nfq2/params.c +++ b/nfq2/params.c @@ -499,8 +499,6 @@ void cleanup_params(struct params_s *params) blob_collection_destroy(¶ms->blobs); strlist_destroy(¶ms->lua_init_scripts); - params->desync_ctx = NULL; - #ifdef __CYGWIN__ strlist_destroy(¶ms->ssid_filter); strlist_destroy(¶ms->nlm_filter); diff --git a/nfq2/params.h b/nfq2/params.h index d018436..89147ae 100644 --- a/nfq2/params.h +++ b/nfq2/params.h @@ -115,16 +115,6 @@ void dp_clear(struct desync_profile *dp); #define WINDIVERT_MAX 65536 #define WINDIVERT_PORTFILTER_MAX 4096 -typedef struct { - unsigned int func_n; - const char *func, *instance; - const struct desync_profile *dp; - const struct dissect *dis; - t_ctrack *ctrack; - bool incoming, cancel; - bool valid; -} t_lua_desync_context; - struct params_s { #if !defined( __OpenBSD__) && !defined(__ANDROID__) @@ -193,7 +183,6 @@ struct params_s int lua_gc; int ref_desync_ctx; // desync ctx userdata registry ref - t_lua_desync_context *desync_ctx; // desync ctx single object lua_State *L; };