commit 437cef0: [Minor] Rework rspamd_parse_host_port_priority function result
Vsevolod Stakhov
vsevolod at highsecure.ru
Fri Jul 26 17:21:03 UTC 2019
Author: Vsevolod Stakhov
Date: 2019-07-26 18:15:23 +0100
URL: https://github.com/rspamd/rspamd/commit/437cef075caec19a8150e0c4c04ef64a1a5e9d94
[Minor] Rework rspamd_parse_host_port_priority function result
---
src/libserver/cfg_file.h | 1 +
src/libserver/cfg_rcl.c | 6 +++++
src/libserver/cfg_utils.c | 4 ++--
src/libutil/addr.c | 58 +++++++++++++++++++++++------------------------
src/libutil/addr.h | 16 +++++++++----
src/rspamadm/lua_repl.c | 4 ++--
6 files changed, 51 insertions(+), 38 deletions(-)
diff --git a/src/libserver/cfg_file.h b/src/libserver/cfg_file.h
index de00ab128..39d997754 100644
--- a/src/libserver/cfg_file.h
+++ b/src/libserver/cfg_file.h
@@ -450,6 +450,7 @@ struct rspamd_config {
guint upstream_max_errors; /**< upstream max errors before shutting off */
gdouble upstream_error_time; /**< rate of upstream errors */
gdouble upstream_revive_time; /**< revive timeout for upstreams */
+ gdouble upstream_lazy_resolve_time; /**< lazy resolve time for upstreams */
struct upstream_ctx *ups_ctx; /**< upstream context */
struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded */
diff --git a/src/libserver/cfg_rcl.c b/src/libserver/cfg_rcl.c
index 0a40cdd72..5b67a5f61 100644
--- a/src/libserver/cfg_rcl.c
+++ b/src/libserver/cfg_rcl.c
@@ -2223,6 +2223,12 @@ rspamd_rcl_config_init (struct rspamd_config *cfg, GHashTable *skip_sections)
G_STRUCT_OFFSET (struct rspamd_config, upstream_revive_time),
RSPAMD_CL_FLAG_TIME_FLOAT,
"Time before attempting to recover upstream after an error");
+ rspamd_rcl_add_default_handler (ssub,
+ "lazy_resolve_time",
+ rspamd_rcl_parse_struct_time,
+ G_STRUCT_OFFSET (struct rspamd_config, upstream_lazy_resolve_time),
+ RSPAMD_CL_FLAG_TIME_FLOAT,
+ "Time to resolve upstreams addresses in lazy mode");
}
if (!(skip_sections && g_hash_table_lookup (skip_sections, "actions"))) {
diff --git a/src/libserver/cfg_utils.c b/src/libserver/cfg_utils.c
index d6dc95527..4348f7940 100644
--- a/src/libserver/cfg_utils.c
+++ b/src/libserver/cfg_utils.c
@@ -106,8 +106,8 @@ rspamd_parse_bind_line (struct rspamd_config *cfg,
}
}
else {
- if (!rspamd_parse_host_port_priority (str, &cnf->addrs,
- NULL, &cnf->name, DEFAULT_BIND_PORT, cfg->cfg_pool)) {
+ if (rspamd_parse_host_port_priority (str, &cnf->addrs,
+ NULL, &cnf->name, DEFAULT_BIND_PORT, cfg->cfg_pool) == RSPAMD_PARSE_ADDR_FAIL) {
msg_err_config ("cannot parse bind line: %s", str);
ret = FALSE;
}
diff --git a/src/libutil/addr.c b/src/libutil/addr.c
index 112c5d2cd..d6a1be471 100644
--- a/src/libutil/addr.c
+++ b/src/libutil/addr.c
@@ -1211,7 +1211,7 @@ rspamd_check_port_priority (const char *line, guint default_port,
return TRUE;
}
-static gboolean
+static enum rspamd_parse_host_port_result
rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
const gchar *portbuf, gint flags,
rspamd_mempool_t *pool)
@@ -1220,6 +1220,7 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
rspamd_inet_addr_t *cur_addr = NULL;
gint r, addr_cnt;
gchar *addr_cpy = NULL;
+ enum rspamd_parse_host_port_result ret = RSPAMD_PARSE_ADDR_FAIL;
rspamd_ip_check_ipv6 ();
@@ -1236,6 +1237,7 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
rspamd_inet_address_set_port (cur_addr, strtoul (portbuf, NULL, 10));
g_ptr_array_add (*addrs, cur_addr);
+ ret = RSPAMD_PARSE_ADDR_NUMERIC;
}
else {
memset (&hints, 0, sizeof (hints));
@@ -1292,6 +1294,7 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
}
freeaddrinfo (res);
+ ret = RSPAMD_PARSE_ADDR_RESOLVED;
}
else if (addr_cpy) {
msg_err_pool_check ("address resolution for %s failed: %s",
@@ -1302,7 +1305,7 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
g_free (addr_cpy);
}
- return FALSE;
+ return RSPAMD_PARSE_ADDR_FAIL;
}
else {
/* Should never ever happen */
@@ -1310,21 +1313,22 @@ rspamd_resolve_addrs (const char *begin, size_t len, GPtrArray **addrs,
}
}
- return TRUE;
+ return ret;
}
-gboolean
+enum rspamd_parse_host_port_result
rspamd_parse_host_port_priority (const gchar *str,
- GPtrArray **addrs,
- guint *priority,
- gchar **name_ptr,
- guint default_port,
- rspamd_mempool_t *pool)
+ GPtrArray **addrs,
+ guint *priority,
+ gchar **name_ptr,
+ guint default_port,
+ rspamd_mempool_t *pool)
{
gchar portbuf[8];
const gchar *p, *name = NULL;
gsize namelen;
rspamd_inet_addr_t *cur_addr = NULL;
+ enum rspamd_parse_host_port_result ret = RSPAMD_PARSE_ADDR_FAIL;
/*
* In this function, we can have several possibilities:
@@ -1337,15 +1341,17 @@ rspamd_parse_host_port_priority (const gchar *str,
if (str[0] == '*') {
if (!rspamd_check_port_priority (str + 1, default_port, priority,
portbuf, sizeof (portbuf), pool)) {
- return FALSE;
+ return ret;
}
- if (!rspamd_resolve_addrs (str, 0, addrs, portbuf, AI_PASSIVE, pool)) {
- return FALSE;
+ if (rspamd_resolve_addrs (str, 0, addrs, portbuf, AI_PASSIVE, pool)
+ == RSPAMD_PARSE_ADDR_FAIL) {
+ return ret;
}
name = "*";
namelen = 1;
+ ret = RSPAMD_PARSE_ADDR_NUMERIC; /* No resolution here */
}
else if (str[0] == '[') {
/* This is braced IPv6 address */
@@ -1356,7 +1362,7 @@ rspamd_parse_host_port_priority (const gchar *str,
str,
strerror (EINVAL));
- return FALSE;
+ return ret;
}
name = str + 1;
@@ -1364,13 +1370,10 @@ rspamd_parse_host_port_priority (const gchar *str,
if (!rspamd_check_port_priority (p + 1, default_port, priority, portbuf,
sizeof (portbuf), pool)) {
- return FALSE;
+ return ret;
}
- if (!rspamd_resolve_addrs (name, namelen, addrs,
- portbuf, 0, pool)) {
- return FALSE;
- }
+ ret = rspamd_resolve_addrs (name, namelen, addrs, portbuf, 0, pool);
}
else if (str[0] == '/' || str[0] == '.') {
/* Special case of unix socket, as getaddrinfo cannot deal with them */
@@ -1389,12 +1392,13 @@ rspamd_parse_host_port_priority (const gchar *str,
str,
strerror (errno));
- return FALSE;
+ return ret;
}
g_ptr_array_add (*addrs, cur_addr);
name = str;
namelen = strlen (str);
+ ret = RSPAMD_PARSE_ADDR_NUMERIC; /* No resolution here: unix socket */
}
else {
p = strchr (str, ':');
@@ -1406,10 +1410,8 @@ rspamd_parse_host_port_priority (const gchar *str,
rspamd_check_port_priority ("", default_port, priority, portbuf,
sizeof (portbuf), pool);
- if (!rspamd_resolve_addrs (name, namelen, addrs,
- portbuf, 0, pool)) {
- return FALSE;
- }
+ ret = rspamd_resolve_addrs (name, namelen, addrs,
+ portbuf, 0, pool);
}
else {
name = str;
@@ -1417,13 +1419,11 @@ rspamd_parse_host_port_priority (const gchar *str,
if (!rspamd_check_port_priority (p, default_port, priority, portbuf,
sizeof (portbuf), pool)) {
- return FALSE;
+ return ret;
}
- if (!rspamd_resolve_addrs (str, p - str, addrs,
- portbuf, 0, pool)) {
- return FALSE;
- }
+ ret = rspamd_resolve_addrs (str, p - str, addrs,
+ portbuf, 0, pool);
}
}
@@ -1438,7 +1438,7 @@ rspamd_parse_host_port_priority (const gchar *str,
rspamd_strlcpy (*name_ptr, name, namelen + 1);
}
- return TRUE;
+ return ret;
}
guchar*
diff --git a/src/libutil/addr.h b/src/libutil/addr.h
index fe00ccc86..06413a51a 100644
--- a/src/libutil/addr.h
+++ b/src/libutil/addr.h
@@ -240,17 +240,23 @@ gint rspamd_accept_from_socket (gint sock,
rspamd_accept_throttling_handler hdl,
void *hdl_data);
+enum rspamd_parse_host_port_result {
+ RSPAMD_PARSE_ADDR_FAIL = 0,
+ RSPAMD_PARSE_ADDR_RESOLVED = 1,
+ RSPAMD_PARSE_ADDR_NUMERIC = 2,
+};
/**
* Parse host[:port[:priority]] line
* @param ina host address
* @param port port
* @param priority priority
- * @return TRUE if string was parsed
+ * @return RSPAMD_PARSE_ADDR_FAIL in case of error, RSPAMD_PARSE_ADDR_NUMERIC in case of pure ip/unix socket
*/
-gboolean rspamd_parse_host_port_priority (const gchar *str,
- GPtrArray **addrs,
- guint *priority, gchar **name, guint default_port,
- rspamd_mempool_t *pool);
+enum rspamd_parse_host_port_result
+rspamd_parse_host_port_priority (const gchar *str,
+ GPtrArray **addrs,
+ guint *priority, gchar **name, guint default_port,
+ rspamd_mempool_t *pool);
/**
* Destroy the specified IP address
diff --git a/src/rspamadm/lua_repl.c b/src/rspamadm/lua_repl.c
index 59e3db02c..4cd68185d 100644
--- a/src/rspamadm/lua_repl.c
+++ b/src/rspamadm/lua_repl.c
@@ -799,8 +799,8 @@ rspamadm_lua (gint argc, gchar **argv, const struct rspamadm_command *cmd)
gint fd;
struct rspamadm_lua_repl_context *ctx;
- if (!rspamd_parse_host_port_priority (serve, &addrs, NULL, &name,
- 10000, NULL)) {
+ if (rspamd_parse_host_port_priority (serve, &addrs, NULL, &name,
+ 10000, NULL) == RSPAMD_PARSE_ADDR_FAIL) {
fprintf (stderr, "cannot listen on %s", serve);
exit (EXIT_FAILURE);
}
More information about the Commits
mailing list