]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Allow to get a port for a specific upstream
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 7 Nov 2021 12:49:50 +0000 (12:49 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 7 Nov 2021 12:54:14 +0000 (12:54 +0000)
src/libutil/upstream.c
src/libutil/upstream.h
src/lua/lua_upstream.c

index 8c194eb7974303f47f42068b241500adc2b27c54..578efd0f480b50f471c85f6db21427bfa12bebf1 100644 (file)
@@ -1021,6 +1021,15 @@ rspamd_upstream_name (struct upstream *up)
        return up->name;
 }
 
+gint
+rspamd_upstream_port (struct upstream *up)
+{
+       struct upstream_addr_elt *elt;
+
+       elt = g_ptr_array_index (up->addrs.addr, up->addrs.cur);
+       return rspamd_inet_address_get_port (elt->addr);
+}
+
 gboolean
 rspamd_upstreams_add_upstream (struct upstream_list *ups, const gchar *str,
                guint16 def_port, enum rspamd_upstream_parse_type parse_type,
index 72a768892965040a9ee6534af08d4a147af8f14b..e81fa2ed74887331c832769e4472b5db3303ad93 100644 (file)
@@ -248,6 +248,13 @@ gboolean rspamd_upstream_add_addr (struct upstream *up,
  */
 const gchar *rspamd_upstream_name (struct upstream *up);
 
+/**
+ * Returns the port of the current addres for the upstream
+ * @param up
+ * @return
+ */
+gint rspamd_upstream_port (struct upstream *up);
+
 /**
  * Sets opaque user data associated with this upstream
  * @param up
index 322b242f870225b3459f3994b7fdc4fe9ac0ddaf..5019f28d349a6e33cd4a7bb4c5594bfc9e512744 100644 (file)
@@ -79,12 +79,14 @@ LUA_FUNCTION_DEF (upstream, ok);
 LUA_FUNCTION_DEF (upstream, fail);
 LUA_FUNCTION_DEF (upstream, get_addr);
 LUA_FUNCTION_DEF (upstream, get_name);
+LUA_FUNCTION_DEF (upstream, get_port);
 LUA_FUNCTION_DEF (upstream, destroy);
 
 static const struct luaL_reg upstream_m[] = {
        LUA_INTERFACE_DEF (upstream, ok),
        LUA_INTERFACE_DEF (upstream, fail),
        LUA_INTERFACE_DEF (upstream, get_addr),
+       LUA_INTERFACE_DEF (upstream, get_port),
        LUA_INTERFACE_DEF (upstream, get_name),
        {"__tostring", rspamd_lua_class_tostring},
        {"__gc", lua_upstream_destroy},
@@ -149,6 +151,27 @@ lua_upstream_get_name (lua_State *L)
        return 1;
 }
 
+/***
+ * @method upstream:get_port()
+ * Get port of upstream
+ * @return {int} port of the upstream
+ */
+static gint
+lua_upstream_get_port (lua_State *L)
+{
+       LUA_TRACE_POINT;
+       struct rspamd_lua_upstream *up = lua_check_upstream (L);
+
+       if (up) {
+               lua_pushinteger (L, rspamd_upstream_port (up->up));
+       }
+       else {
+               lua_pushnil (L);
+       }
+
+       return 1;
+}
+
 /***
  * @method upstream:fail()
  * Indicate upstream failure. After certain amount of failures during specified time frame, an upstream is marked as down and does not participate in rotations.