]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Various fixes
authorVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 10 Nov 2023 09:34:55 +0000 (09:34 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Fri, 10 Nov 2023 09:34:55 +0000 (09:34 +0000)
lualib/rspamadm/fuzzy_ping.lua
src/plugins/fuzzy_check.c

index eed509282132b7d73ecf00a768b717957443bdd5..3630771cc9ce991ee99c453e62ca6d72153e0755 100644 (file)
@@ -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)
@@ -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,
index 4c93d1de5fcb74e84dc546ffb54614f4b91924dc..6fd1f168b9f7e6f58f0508a9b308f0a0cfedf1ac 100644 (file)
@@ -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)
 {
@@ -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;
                }
        }
@@ -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;
                }
        }
@@ -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 {
@@ -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 {
@@ -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;
        }