]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dns-client: Move idle_timeout_msecs and cache_ttl_secs from dns_client_settings...
authorsergey.kitov <sergey.kitov@open-xchange.com>
Wed, 5 Feb 2025 11:38:19 +0000 (13:38 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 19 May 2025 08:07:56 +0000 (08:07 +0000)
As a preparation to make dns_client_settings configurable.

12 files changed:
src/auth/auth.c
src/lib-dns-client/dns-lookup.c
src/lib-dns-client/dns-lookup.h
src/lib-dns-client/test-dns-lookup.c
src/lib-doveadm/doveadm-client.c
src/lib-http/http-client-host.c
src/lib-http/test-http-client.c
src/lib-imap-client/imapc-connection.c
src/lib-lua/test-dns-lua.c
src/lib-program-client/program-client-remote.c
src/lib-smtp/smtp-client-connection.c
src/lib-storage/index/pop3c/pop3c-client.c

index 0f3cc6e3066620b9d5188d8711d8b9c02c137229..b962320f07f55c43d6a7f2694e20b618e7521d25 100644 (file)
@@ -377,6 +377,7 @@ static void auth_init(struct auth *auth)
        struct auth_passdb *passdb;
        struct auth_userdb *userdb;
        struct dns_client_settings dns_set;
+       struct dns_client_parameters dns_params;
 
        for (passdb = auth->masterdbs; passdb != NULL; passdb = passdb->next)
                auth_passdb_init(passdb);
@@ -388,10 +389,10 @@ static void auth_init(struct auth *auth)
        i_zero(&dns_set);
        dns_set.dns_client_socket_path = AUTH_DNS_SOCKET_PATH;
        dns_set.timeout_msecs = AUTH_DNS_DEFAULT_TIMEOUT_MSECS;
-       dns_set.idle_timeout_msecs = AUTH_DNS_IDLE_TIMEOUT_MSECS;
-       dns_set.cache_ttl_secs = AUTH_DNS_CACHE_TTL_SECS;
+       dns_params.idle_timeout_msecs = AUTH_DNS_IDLE_TIMEOUT_MSECS;
+       dns_params.cache_ttl_secs = AUTH_DNS_CACHE_TTL_SECS;
 
-       auth->dns_client = dns_client_init(&dns_set, NULL);
+       auth->dns_client = dns_client_init(&dns_set, &dns_params, NULL);
 }
 
 static void auth_deinit(struct auth *auth)
index 03f1cd63850d368cd6b8fa3657ebd5ce49889759..716fee648ae4e58b0fc0af3acf5ee74412228fc7 100644 (file)
@@ -272,13 +272,14 @@ static void dns_lookup_timeout(struct dns_lookup *lookup)
 }
 
 int dns_lookup(const char *host, const struct dns_client_settings *set,
+              const struct dns_client_parameters *params,
               struct event *event_parent, dns_lookup_callback_t *callback,
               void *context, struct dns_lookup **lookup_r)
 {
        struct dns_client *client;
 
-       i_assert(set->cache_ttl_secs == 0);
-       client = dns_client_init(set, event_parent);
+       i_assert(params == NULL || params->cache_ttl_secs == 0);
+       client = dns_client_init(set, params, event_parent);
        client->deinit_client_at_free = TRUE;
        return dns_client_lookup(client, host, client->conn.event, callback,
                                 context, lookup_r);
@@ -286,14 +287,15 @@ int dns_lookup(const char *host, const struct dns_client_settings *set,
 
 int dns_lookup_ptr(const struct ip_addr *ip,
                   const struct dns_client_settings *set,
+                  const struct dns_client_parameters *params,
                   struct event *event_parent,
                   dns_lookup_callback_t *callback, void *context,
                   struct dns_lookup **lookup_r)
 {
        struct dns_client *client;
 
-       i_assert(set->cache_ttl_secs == 0);
-       client = dns_client_init(set, event_parent);
+       i_assert(params == NULL || params->cache_ttl_secs == 0);
+       client = dns_client_init(set, params, event_parent);
        client->deinit_client_at_free = TRUE;
        return dns_client_lookup_ptr(client, ip, client->conn.event,
                                     callback, context, lookup_r);
@@ -388,6 +390,7 @@ static const struct connection_settings dns_client_set = {
 };
 
 struct dns_client *dns_client_init(const struct dns_client_settings *set,
+                                  const struct dns_client_parameters *params,
                                   struct event *event_parent)
 {
        struct dns_client *client;
@@ -396,15 +399,15 @@ struct dns_client *dns_client_init(const struct dns_client_settings *set,
 
        client = i_new(struct dns_client, 1);
        client->timeout_msecs = set->timeout_msecs;
-       client->idle_timeout_msecs = set->idle_timeout_msecs;
+       client->idle_timeout_msecs = params == NULL ? 0 : params->idle_timeout_msecs;
        client->clist = connection_list_init(&dns_client_set, &dns_client_vfuncs);
        client->ioloop = current_ioloop;
        client->path = i_strdup(set->dns_client_socket_path);
        client->conn.event_parent = event_parent;
        connection_init_client_unix(client->clist, &client->conn, client->path);
        event_add_category(client->conn.event, &event_category_dns);
-       if (set->cache_ttl_secs > 0) {
-               client->cache = dns_client_cache_init(set->cache_ttl_secs,
+       if (params != NULL && params->cache_ttl_secs > 0) {
+               client->cache = dns_client_cache_init(params->cache_ttl_secs,
                        dns_client_cache_refresh, client);
        }
        return client;
index 42a740fbc760fa73dcef6c4e28e1801f79d316a8..247a51f87599da40cf3acedc127e890c4d33d8fd 100644 (file)
@@ -14,6 +14,9 @@ struct dns_lookup;
 struct dns_client_settings {
        const char *dns_client_socket_path;
        unsigned int timeout_msecs;
+};
+
+struct dns_client_parameters {
        /* the idle_timeout_msecs works only with the dns_client_* API.
           0 = disconnect immediately */
        unsigned int idle_timeout_msecs;
@@ -46,24 +49,26 @@ typedef void dns_lookup_callback_t(const struct dns_lookup_result *result,
    When failing with -1, the callback is called before returning from the
    function. */
 int dns_lookup(const char *host, const struct dns_client_settings *set,
+              const struct dns_client_parameters *params,
               struct event *event_parent,
               dns_lookup_callback_t *callback, void *context,
               struct dns_lookup **lookup_r) ATTR_NULL(4);
-#define dns_lookup(host, set, event_parent, callback, context, lookup_r) \
+#define dns_lookup(host, set, params, event_parent, callback, context, lookup_r) \
        dns_lookup(host - \
                CALLBACK_TYPECHECK(callback, void (*)( \
                        const struct dns_lookup_result *, typeof(context))), \
-               set, event_parent, (dns_lookup_callback_t *)callback, context, lookup_r)
+               set, params, event_parent, (dns_lookup_callback_t *)callback, context, lookup_r)
 int dns_lookup_ptr(const struct ip_addr *ip,
                   const struct dns_client_settings *set,
+                  const struct dns_client_parameters *params,
                   struct event *event_parent,
                   dns_lookup_callback_t *callback, void *context,
                   struct dns_lookup **lookup_r) ATTR_NULL(4);
-#define dns_lookup_ptr(host, set, event_parent, callback, context, lookup_r) \
+#define dns_lookup_ptr(host, set, params, event_parent, callback, context, lookup_r) \
        dns_lookup_ptr(host - \
                CALLBACK_TYPECHECK(callback, void (*)( \
                        const struct dns_lookup_result *, typeof(context))), \
-               set, event_parent, \
+               set, params, event_parent, \
                (dns_lookup_callback_t *)callback, context, lookup_r)
 /* Abort the DNS lookup without calling the callback. */
 void dns_lookup_abort(struct dns_lookup **lookup);
@@ -72,6 +77,7 @@ void dns_lookup_switch_ioloop(struct dns_lookup *lookup);
 
 /* Alternative API for clients that need to do multiple DNS lookups. */
 struct dns_client *dns_client_init(const struct dns_client_settings *set,
+                                  const struct dns_client_parameters *params,
                                   struct event *event_parent);
 void dns_client_deinit(struct dns_client **client);
 
index 18d8b36cf0e989b0943cbaef75e9072f8ac6c8e1..67e298ecd9646ab5faea8986b50776b334d065db 100644 (file)
@@ -176,7 +176,8 @@ static void test_dns_expect_result_ips(const char *name, const char *result)
                .ret = result == NULL ? -1 : 0,
                .result = result
        };
-       test_assert(dns_lookup(name, &set, NULL, test_callback_ips, &ctx, &lookup) == 0);
+       test_assert(dns_lookup(name, &set, NULL, NULL, test_callback_ips,
+                              &ctx, &lookup) == 0);
        io_loop_run(test_server.loop);
 }
 
@@ -193,7 +194,8 @@ static void test_dns_expect_result_name(const char *name, const char *result)
        };
        struct ip_addr addr;
        i_assert(net_addr2ip(name, &addr) == 0);
-       test_assert(dns_lookup_ptr(&addr, &set, NULL, test_callback_name, &ctx, &lookup) == 0);
+       test_assert(dns_lookup_ptr(&addr, &set, NULL, NULL, test_callback_name,
+                                  &ctx, &lookup) == 0);
        io_loop_run(test_server.loop);
 }
 
@@ -226,7 +228,8 @@ static void test_dns_lookup_timeout(void)
                .result = NULL,
        };
 
-       test_assert(dns_lookup("waitfor1500", &set, NULL, test_callback_ips, &ctx, &lookup) == 0);
+       test_assert(dns_lookup("waitfor1500", &set, NULL, NULL, test_callback_ips,
+                              &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
 
        destroy_dns_server(&test_server);
@@ -248,7 +251,8 @@ static void test_dns_lookup_abort(void)
                .result = NULL,
        };
 
-       test_assert(dns_lookup("waitfor1500", &set, NULL, test_callback_ips, &ctx, &lookup) == 0);
+       test_assert(dns_lookup("waitfor1500", &set, NULL, NULL, test_callback_ips,
+                              &ctx, &lookup) == 0);
        struct timeout *to = timeout_add_short(100, io_loop_stop, current_ioloop);
        io_loop_run(current_ioloop);
        timeout_remove(&to);
@@ -270,11 +274,14 @@ static void test_dns_lookup_cached(void)
        const struct dns_client_settings set = {
                .dns_client_socket_path = TEST_SOCKET_NAME,
                .timeout_msecs = 1000,
+       };
+
+       const struct dns_client_parameters params = {
                .cache_ttl_secs = 4,
        };
 
 
-       struct dns_client *client = dns_client_init(&set, event);
+       struct dns_client *client = dns_client_init(&set, &params, event);
 
        /* lookup localhost */
        ctx.result = "127.0.0.1\t::1";
index 24f136574123d9525a21ff6f38ece8a6b5eaf3d3..771337ef2c84229af5cbd8066f751b4294f4aa1a 100644 (file)
@@ -722,7 +722,7 @@ static int doveadm_client_dns_lookup(struct doveadm_client *conn,
 
        ctx->conn = conn;
 
-       if (dns_lookup(conn->set.hostname, &dns_set, conn->conn.event,
+       if (dns_lookup(conn->set.hostname, &dns_set, NULL, conn->conn.event,
                       doveadm_client_dns_lookup_callback, ctx,
                       &conn->dns_lookup) != 0) {
                *error_r = t_strdup(ctx->error);
index 4f9116f2b8246d7229e831ef7e8c6bfc7f4e0959..07d724ade995644b92c36707e6f18416beb75f68 100644 (file)
@@ -163,7 +163,7 @@ http_client_host_shared_lookup(struct http_client_host_shared *hshared)
                dns_set.dns_client_socket_path = cctx->dns_client_socket_path;
                dns_set.timeout_msecs = cctx->dns_lookup_timeout_msecs;
                io_loop_set_current(cctx->ioloop);
-               (void)dns_lookup(hshared->name, &dns_set, hshared->event,
+               (void)dns_lookup(hshared->name, &dns_set, NULL, hshared->event,
                                 http_client_host_shared_dns_callback,
                                 hshared, &hshared->dns_lookup);
                io_loop_set_current(prev_ioloop);
index 9c317396247a4bef665f9c4d35b2c4cd83c6f388..6950268d90a86a94e226146c2880c9d88260c81e 100644 (file)
@@ -350,6 +350,7 @@ int main(int argc, char *argv[])
 {
        struct dns_client *dns_client;
        struct dns_client_settings dns_set;
+       struct dns_client_parameters dns_params;
        struct http_client_settings http_set;
        struct http_client_context *http_cctx;
        struct http_client *http_client1, *http_client2, *http_client3, *http_client4;
@@ -369,11 +370,11 @@ int main(int argc, char *argv[])
        i_zero_safe(&dns_set);
        dns_set.dns_client_socket_path = PKG_RUNDIR"/dns-client";
        dns_set.timeout_msecs = 30*1000;
-       dns_set.idle_timeout_msecs = UINT_MAX;
+       dns_params.idle_timeout_msecs = UINT_MAX;
 
        /* check if there is a DNS client */
        if (access(dns_set.dns_client_socket_path, R_OK|W_OK) == 0) {
-               dns_client = dns_client_init(&dns_set, NULL);
+               dns_client = dns_client_init(&dns_set, &dns_params, NULL);
 
                if (dns_client_connect(dns_client, &error) < 0)
                        i_fatal("Couldn't initialize DNS client: %s", error);
index fcf5e75ba2a45416fe9d31954f670a6b8e567bbc..d6438efcda7aa06757a5e9ad86727b9822482bd2 100644 (file)
@@ -1981,7 +1981,7 @@ void imapc_connection_connect(struct imapc_connection *conn)
                conn->ips = i_new(struct ip_addr, ips_count);
                memcpy(conn->ips, ips, ips_count * sizeof(*ips));
        } else {
-               (void)dns_lookup(conn->client->set->imapc_host, &dns_set,
+               (void)dns_lookup(conn->client->set->imapc_host, &dns_set, NULL,
                                 conn->event, imapc_connection_dns_callback,
                                 conn, &conn->dns_lookup);
                return;
index 68ff3aa9989b3d64bd433b5744d008259ba62dc4..9cfc1c0e47bda0b43e954c3f97ce8436d2d065db 100644 (file)
@@ -46,7 +46,7 @@ static void test_dns_lua_common(const char *luascript)
        struct settings_simple test_set;
        settings_simple_init(&test_set, NULL);
 
-       struct dns_client *client = dns_client_init(&set, NULL);
+       struct dns_client *client = dns_client_init(&set, NULL, NULL);
 
        struct dlua_script *script;
        const char *error;
index 804509cfd322743d417fd8556cc4de3677c0c89a..c2629c65f3e5f86199d463c1f360bcea360fe786 100644 (file)
@@ -533,7 +533,7 @@ static int program_client_net_connect_init(struct program_client *pclient)
                        prclient->dns_set.timeout_msecs =
                                pclient->params.client_connect_timeout_msecs;
                        (void)dns_lookup(prclient->address, &prclient->dns_set,
-                                        pclient->event,
+                                        NULL, pclient->event,
                                         program_client_net_connect_resolved,
                                         prclient, &prclient->lookup);
                        return 0;
index 5c095f9502ac397a8c75f8b1f4bb247b2ac77860..e2dce2680faa009f975de9234f9895750d6fa722 100644 (file)
@@ -1898,7 +1898,7 @@ smtp_client_connection_lookup_ip(struct smtp_client_connection *conn)
                        conn->set.dns_client_socket_path;
                dns_set.timeout_msecs = conn->set.connect_timeout_msecs;
                e_debug(conn->event, "Performing asynchronous DNS lookup");
-               (void)dns_lookup(conn->host, &dns_set, conn->event,
+               (void)dns_lookup(conn->host, &dns_set, NULL, conn->event,
                                 smtp_client_connection_dns_callback, conn,
                                 &conn->dns_lookup);
        } else {
index 1b893c3c001b602ffc721a0956ff6e592ed54175..46696b9360ca516771efd5d06e3bf1df326a33ee 100644 (file)
@@ -265,7 +265,7 @@ static int pop3c_client_dns_lookup(struct pop3c_client *client)
                dns_set.dns_client_socket_path =
                        client->set.dns_client_socket_path;
                dns_set.timeout_msecs = POP3C_DNS_LOOKUP_TIMEOUT_MSECS;
-               if (dns_lookup(client->set.host, &dns_set, client->event,
+               if (dns_lookup(client->set.host, &dns_set, NULL, client->event,
                               pop3c_dns_callback, client,
                               &client->dns_lookup) < 0)
                        return -1;