]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-dns: Add per-lookup event for dns client
authorAki Tuomi <aki.tuomi@open-xchange.com>
Fri, 10 Sep 2021 06:38:17 +0000 (09:38 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 17 Jan 2022 11:52:09 +0000 (13:52 +0200)
src/lib-dns/dns-lookup.c
src/lib-dns/dns-lookup.h
src/lib-dns/test-dns-lookup.c
src/lib-http/http-client-host.c
src/lib-smtp/smtp-client-connection.c

index 176f9dfa5369c0f15e6e2fb576804ef5cfba7b8c..2190c47e0c909905dd6c290f99f98a44af43b191 100644 (file)
@@ -161,7 +161,7 @@ static void dns_client_cache_entry_refresh(struct dns_client *client,
                        i_unreached();
                ctx = i_new(struct dns_cache_lookup, 1);
                ctx->key = i_strdup(entry->cache_key);
-               if (dns_client_lookup_ptr(client, &ip,
+               if (dns_client_lookup_ptr(client, &ip, client->conn.event,
                                          dns_client_cache_callback,
                                          ctx, &lookup) < 0) {
                        e_debug(client->conn.event,
@@ -177,6 +177,7 @@ static void dns_client_cache_entry_refresh(struct dns_client *client,
                ctx = i_new(struct dns_cache_lookup, 1);
                ctx->key = i_strdup(entry->cache_key);
                if (dns_client_lookup(client, entry->cache_key + 1,
+                                     client->conn.event,
                                      dns_client_cache_callback,
                                      ctx, &lookup) < 0) {
                        e_debug(client->conn.event,
@@ -426,7 +427,8 @@ int dns_lookup(const char *host, const struct dns_lookup_settings *set,
        client = dns_client_init(set);
        event_add_category(client->conn.event, &event_category_dns);
        client->deinit_client_at_free = TRUE;
-       return dns_client_lookup(client, host, callback, context, lookup_r);
+       return dns_client_lookup(client, host, client->conn.event, callback,
+                                context, lookup_r);
 }
 
 int dns_lookup_ptr(const struct ip_addr *ip,
@@ -440,7 +442,8 @@ int dns_lookup_ptr(const struct ip_addr *ip,
        client = dns_client_init(set);
        event_add_category(client->conn.event, &event_category_dns);
        client->deinit_client_at_free = TRUE;
-       return dns_client_lookup_ptr(client, ip, callback, context, lookup_r);
+       return dns_client_lookup_ptr(client, ip, client->conn.event,
+                                    callback, context, lookup_r);
 }
 
 static void dns_client_idle_timeout(struct dns_client *client)
@@ -589,6 +592,7 @@ dns_client_send_request(struct dns_client *client, const char *cmd,
 static int
 dns_client_lookup_common(struct dns_client *client,
                         const char *cmd, const char *param, bool ptr_lookup,
+                        struct event *event,
                         dns_lookup_callback_t *callback, void *context,
                         struct dns_lookup **lookup_r)
 {
@@ -609,7 +613,8 @@ dns_client_lookup_common(struct dns_client *client,
        lookup->context = context;
        lookup->ptr_lookup = ptr_lookup;
        lookup->result.ret = EAI_FAIL;
-       lookup->event = event_create(client->conn.event);
+       lookup->event = event_create(event);
+       event_add_category(lookup->event, &event_category_dns);
        lookup->cache_key = p_strdup_printf(lookup->pool, "%c%s",
                                      ptr_lookup ? 'I' : 'N', param);
        event_set_append_log_prefix(lookup->event, t_strconcat("dns(", param, "): ", NULL));
@@ -649,19 +654,21 @@ dns_client_lookup_common(struct dns_client *client,
 }
 
 int dns_client_lookup(struct dns_client *client, const char *host,
+                     struct event *event,
                      dns_lookup_callback_t *callback, void *context,
                      struct dns_lookup **lookup_r)
 {
-       return dns_client_lookup_common(client, "IP", host, FALSE,
+       return dns_client_lookup_common(client, "IP", host, FALSE, event,
                                        callback, context, lookup_r);
 }
 
 int dns_client_lookup_ptr(struct dns_client *client, const struct ip_addr *ip,
+                         struct event *event,
                          dns_lookup_callback_t *callback, void *context,
                          struct dns_lookup **lookup_r)
 {
        return dns_client_lookup_common(client, "NAME", net_ip2addr(ip), TRUE,
-                                       callback, context, lookup_r);
+                                       event, callback, context, lookup_r);
 }
 
 void dns_client_switch_ioloop(struct dns_client *client)
index 615528baa92b3186b7bc30ff229470f0041b9529..549bc9f0539b4cc6e6dc21bc6485cd947d798cb1 100644 (file)
@@ -72,21 +72,23 @@ void dns_client_deinit(struct dns_client **client);
 /* Connect immediately to the dns-lookup socket. */
 int dns_client_connect(struct dns_client *client, const char **error_r);
 int dns_client_lookup(struct dns_client *client, const char *host,
+                     struct event *event,
                      dns_lookup_callback_t *callback, void *context,
                      struct dns_lookup **lookup_r) ATTR_NULL(4);
-#define dns_client_lookup(client, host, callback, context, lookup_r) \
+#define dns_client_lookup(client, host, event, callback, context, lookup_r) \
        dns_client_lookup(client, host - \
                CALLBACK_TYPECHECK(callback, void (*)( \
                        const struct dns_lookup_result *, typeof(context))), \
-               (dns_lookup_callback_t *)callback, context, lookup_r)
+               event, (dns_lookup_callback_t *)callback, context, lookup_r)
 int dns_client_lookup_ptr(struct dns_client *client, const struct ip_addr *ip,
+                         struct event *event,
                          dns_lookup_callback_t *callback, void *context,
                          struct dns_lookup **lookup_r) ATTR_NULL(4);
-#define dns_client_lookup_ptr(client, host, callback, context, lookup_r) \
-       dns_client_lookup_ptr(client, host - \
+#define dns_client_lookup_ptr(client, ip, event, callback, context, lookup_r) \
+       dns_client_lookup_ptr(client, ip - \
                CALLBACK_TYPECHECK(callback, void (*)( \
                        const struct dns_lookup_result *, typeof(context))), \
-               (dns_lookup_callback_t *)callback, context, lookup_r)
+               event, (dns_lookup_callback_t *)callback, context, lookup_r)
 
 /* Returns true if the DNS client has any pending queries */
 bool dns_client_has_pending_queries(struct dns_client *client);
index a2a982419dd2a665ad530b4184dba8abb8cc8233..bba7432373678b4e7192f40459fed3c8c47c82a7 100644 (file)
@@ -257,6 +257,7 @@ static void test_dns_lookup_cached(void)
        struct test_expect_result ctx;
        struct dns_lookup *lookup;
        struct timeout *to;
+       struct event *event = event_create(NULL);
 
        test_begin("dns lookup (cached)");
        create_dns_server(&test_server);
@@ -265,6 +266,7 @@ static void test_dns_lookup_cached(void)
                .ioloop = test_server.loop,
                .timeout_msecs = 1000,
                .cache_ttl_secs = 4,
+               .event_parent = event,
        };
 
 
@@ -275,11 +277,11 @@ static void test_dns_lookup_cached(void)
        ctx.ret = 0;
 
        /* should cause only one lookup */
-       test_assert(dns_client_lookup(client, "localhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "localhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
-       test_assert(dns_client_lookup(client, "localhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "localhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
        test_assert_cmp(test_server.lookup_counter, ==, 1);
 
@@ -288,8 +290,8 @@ static void test_dns_lookup_cached(void)
        timeout_remove(&to);
 
        /* entry should get refreshed */
-       test_assert(dns_client_lookup(client, "localhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "localhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
        while (dns_client_has_pending_queries(client)) {
                io_loop_handler_run(current_ioloop);
@@ -302,8 +304,8 @@ static void test_dns_lookup_cached(void)
        io_loop_run(current_ioloop);
        timeout_remove(&to);
 
-       test_assert(dns_client_lookup(client, "localhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "localhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
 
        test_assert_cmp(test_server.lookup_counter, ==, 3);
@@ -311,18 +313,19 @@ static void test_dns_lookup_cached(void)
        /* Ensure failures do not get cached */
        ctx.result = NULL;
        ctx.ret = -1;
-       test_assert(dns_client_lookup(client, "failhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "failhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
        test_assert_cmp(test_server.lookup_counter, ==, 4);
 
-       test_assert(dns_client_lookup(client, "failhost", test_callback_ips,
-                                     &ctx, &lookup) == 0);
+       test_assert(dns_client_lookup(client, "failhost", event,
+                                     test_callback_ips, &ctx, &lookup) == 0);
        io_loop_run(current_ioloop);
        test_assert_cmp(test_server.lookup_counter, ==, 5);
 
        dns_client_deinit(&client);
        destroy_dns_server(&test_server);
+       event_unref(&event);
 
        test_end();
 }
index 647ab663a1365f0077d80f0f46654115c9f476e5..9af88af2c9da3c3b7cc4bff6f8882fb702cd9237 100644 (file)
@@ -149,6 +149,7 @@ http_client_host_shared_lookup(struct http_client_host_shared *hshared)
        if (cctx->dns_client != NULL) {
                e_debug(hshared->event, "Performing asynchronous DNS lookup");
                (void)dns_client_lookup(cctx->dns_client, hshared->name,
+                                       hshared->event,
                                        http_client_host_shared_dns_callback,
                                        hshared, &hshared->dns_lookup);
        } else if (cctx->dns_client_socket_path != NULL) {
index ac4319e4cc4efb54174be3e9561ace7ce9059531..c9dc22f90155c650335db8a5b589a4719c5c2f23 100644 (file)
@@ -1834,6 +1834,7 @@ smtp_client_connection_lookup_ip(struct smtp_client_connection *conn)
                e_debug(conn->event, "Performing asynchronous DNS lookup");
                (void)dns_client_lookup(
                        conn->set.dns_client, conn->host,
+                       conn->event,
                        smtp_client_connection_dns_callback, conn,
                        &conn->dns_lookup);
        } else if (conn->set.dns_client_socket_path != NULL) {