Template
1
0
mirror of https://github.com/bol-van/zapret2.git synced 2026-03-14 06:13:09 +00:00

nfqws2: optimize ctx userdata

This commit is contained in:
bol-van
2026-01-11 14:10:42 +03:00
parent fd288d5e7d
commit 3aad1f9ed9
5 changed files with 38 additions and 38 deletions

View File

@@ -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++;
}
}

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -499,8 +499,6 @@ void cleanup_params(struct params_s *params)
blob_collection_destroy(&params->blobs);
strlist_destroy(&params->lua_init_scripts);
params->desync_ctx = NULL;
#ifdef __CYGWIN__
strlist_destroy(&params->ssid_filter);
strlist_destroy(&params->nlm_filter);

View File

@@ -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;
};