]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Rework rspamd_parse_host_port_priority function result
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 26 Jul 2019 17:15:23 +0000 (18:15 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 26 Jul 2019 17:15:23 +0000 (18:15 +0100)
src/libserver/cfg_file.h
src/libserver/cfg_rcl.c
src/libserver/cfg_utils.c
src/libutil/addr.c
src/libutil/addr.h
src/rspamadm/lua_repl.c

index de00ab128cc36893fc52cd7a1b838df9b0ed8b66..39d9977543520e2aa454fac3804e68acc7d9399e 100644 (file)
@@ -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                                                            */
 
index 0a40cdd722e3f9baf22249b1abaee38ffc521504..5b67a5f61ca09a3b1c391b2b130507195d732d04 100644 (file)
@@ -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"))) {
index d6dc95527a4bb7afdb4bec68d4f91dbb4866facf..4348f794017f9844e5d66fff5166c2bc4e57bc8f 100644 (file)
@@ -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;
                }
index 112c5d2cd354f82a2a3894632fce1a99cb1b5bdf..d6a1be471d81fb7fa87056cb498dff7a0e0c3356 100644 (file)
@@ -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*
index fe00ccc8690838d6fd0e4b0a9873e04ab84d9453..06413a51a66f0a070a13a87c868c1819f83510bd 100644 (file)
@@ -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
index 59e3db02c62a46f43368105150a06d1808a2fcc6..4cd68185d6cda346ef75513773cf95e01a766f4f 100644 (file)
@@ -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);
                }