Skip to content

Commit

Permalink
[Minor] Various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Nov 10, 2023
1 parent f91e090 commit a0fc561
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
2 changes: 2 additions & 0 deletions lualib/rspamadm/fuzzy_ping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ local function handler(args)
-- Perform ping using a fake task from async stuff provided by rspamadm
local rspamd_task = require "rspamd_task"

-- TODO: this task is not cleared at the end, do something about it some day
local task = rspamd_task.create(rspamd_config, rspamadm_ev_base)
task:set_session(rspamadm_session)
task:set_resolver(rspamadm_dns_resolver)
Expand All @@ -120,6 +121,7 @@ local function handler(args)

local function gen_ping_fuzzy_cb(num)
return function(success, server, latency_or_err)
rspamd_logger.errx(task, 'pinged %s: %s', server, latency_or_err)
if not success then
results[num] = {
success = false,
Expand Down
56 changes: 40 additions & 16 deletions src/plugins/fuzzy_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -4423,6 +4423,39 @@ fuzzy_lua_session_is_completed(struct fuzzy_lua_session *session)
return FALSE;
}

static void
fuzzy_lua_push_result(struct fuzzy_lua_session *session, gdouble latency)
{
lua_rawgeti(session->L, LUA_REGISTRYINDEX, session->cbref);
lua_pushboolean(session->L, TRUE);
rspamd_lua_ip_push(session->L, session->addr);
lua_pushnumber(session->L, latency);

/* TODO: check results maybe? */
lua_pcall(session->L, 3, 0, 0);
}

#ifdef __GNUC__
static void
fuzzy_lua_push_error(struct fuzzy_lua_session *session, const gchar *err_fmt, ...) __attribute__((format(printf, 2, 3)));
#endif

static void
fuzzy_lua_push_error(struct fuzzy_lua_session *session, const gchar *err_fmt, ...)
{
va_list v;

va_start(v, err_fmt);
lua_rawgeti(session->L, LUA_REGISTRYINDEX, session->cbref);
lua_pushboolean(session->L, FALSE);
rspamd_lua_ip_push(session->L, session->addr);
lua_pushvfstring(session->L, err_fmt, v);
va_end(v);

/* TODO: check results maybe? */
lua_pcall(session->L, 3, 0, 0);
}

static gint
fuzzy_lua_try_read(struct fuzzy_lua_session *session)
{
Expand All @@ -4437,6 +4470,7 @@ fuzzy_lua_try_read(struct fuzzy_lua_session *session)
return 0;
}
else {
fuzzy_lua_push_error(session, "cannot read from socket: %s", strerror(errno));
return -1;
}
}
Expand All @@ -4448,31 +4482,18 @@ fuzzy_lua_try_read(struct fuzzy_lua_session *session)
while ((rep = fuzzy_process_reply(&p, &r,
session->commands, session->rule, &cmd, &io)) != NULL) {

lua_rawgeti(session->L, LUA_REGISTRYINDEX, session->cbref);

if (rep->v1.prob > 0.5) {
if (cmd->cmd == FUZZY_PING) {
/* ret, addr, latency */
lua_pushboolean(session->L, TRUE);
rspamd_lua_ip_push(session->L, session->addr);
lua_pushnumber(session->L, fuzzy_milliseconds_since_midnight() - rep->v1.value);
fuzzy_lua_push_result(session, fuzzy_milliseconds_since_midnight() - rep->v1.value);
}
else {
/* TODO: unsupported */
lua_pushboolean(session->L, FALSE);
rspamd_lua_ip_push(session->L, session->addr);
lua_pushstring(session->L, "unsupported");
fuzzy_lua_push_error(session, "unsupported");
}
}
else {
lua_pushboolean(session->L, FALSE);
rspamd_lua_ip_push(session->L, session->addr);
lua_pushfstring(session->L, "invalid reply from server: %d", rep->v1.value);
fuzzy_lua_push_error(session, "invalid reply from server: %d", rep->v1.value);
}

/* TODO: check results maybe? */
lua_pcall(session->L, 3, 0, 0);

ret = 1;
}
}
Expand Down Expand Up @@ -4506,6 +4527,7 @@ fuzzy_lua_io_callback(gint fd, short what, void *arg)
if (what & EV_WRITE) {
/* Retransmit attempt */
if (!fuzzy_cmd_vector_to_wire(fd, session->commands)) {
fuzzy_lua_push_error(session, "cannot write to socket");
ret = return_error;
}
else {
Expand All @@ -4524,6 +4546,7 @@ fuzzy_lua_io_callback(gint fd, short what, void *arg)
}
else if (what & EV_WRITE) {
if (!fuzzy_cmd_vector_to_wire(fd, session->commands)) {
fuzzy_lua_push_error(session, "cannot write to socket");
ret = return_error;
}
else {
Expand All @@ -4532,6 +4555,7 @@ fuzzy_lua_io_callback(gint fd, short what, void *arg)
}
else {
/* Timeout */
fuzzy_lua_push_error(session, "timeout waiting for the reply");
ret = return_error;
}

Expand Down

0 comments on commit a0fc561

Please sign in to comment.