From 49768177cfb52e9bf9ea3c5376d26d1247c73b3a Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 7 Nov 2021 12:49:50 +0000 Subject: [PATCH] [Minor] Allow to get a port for a specific upstream --- src/libutil/upstream.c | 9 +++++++++ src/libutil/upstream.h | 7 +++++++ src/lua/lua_upstream.c | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/libutil/upstream.c b/src/libutil/upstream.c index 8c194eb79..578efd0f4 100644 --- a/src/libutil/upstream.c +++ b/src/libutil/upstream.c @@ -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, diff --git a/src/libutil/upstream.h b/src/libutil/upstream.h index 72a768892..e81fa2ed7 100644 --- a/src/libutil/upstream.h +++ b/src/libutil/upstream.h @@ -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 diff --git a/src/lua/lua_upstream.c b/src/lua/lua_upstream.c index 322b242f8..5019f28d3 100644 --- a/src/lua/lua_upstream.c +++ b/src/lua/lua_upstream.c @@ -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. -- 2.39.5